tests: Reimplement test harness in Go

And make it run in parallell
This commit is contained in:
adisbladis 2022-05-28 22:56:09 +08:00
parent ecb0d11088
commit 3c029180a3
4 changed files with 123 additions and 36 deletions

View file

@ -16,16 +16,11 @@
)
}:
let
pythonEnv = pkgs.python3.withPackages (_: [ ]);
in
pkgs.mkShell {
buildInputs = [
pkgs.nix-prefetch-git
pkgs.nixpkgs-fmt
pkgs.go
pkgs.gomod2nix
pythonEnv
];
}

View file

@ -1,2 +1,2 @@
all:
python ./run.py
go run ./run.go

122
tests/run.go Normal file
View file

@ -0,0 +1,122 @@
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"sync"
)
var cwd = func() string {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
return cwd
}()
type testError struct {
testDir string
stdout bytes.Buffer
stderr bytes.Buffer
}
func runProcess(prefix string, command string, args ...string) error {
fmt.Println(fmt.Sprintf("%s: Executing %s %s", prefix, command, args))
cmd := exec.Command(command, args...)
stdoutReader, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderrReader, err := cmd.StderrPipe()
if err != nil {
return err
}
done := make(chan struct{})
go func() {
reader := io.MultiReader(stdoutReader, stderrReader)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Bytes()
fmt.Println(fmt.Sprintf("%s: %s", prefix, line))
}
done <- struct{}{}
}()
err = cmd.Start()
if err != nil {
return err
}
<-done
return cmd.Wait()
}
func runTest(testDir string) error {
rootDir := filepath.Join(cwd, "..")
cmdPath := filepath.Join(rootDir, "gomod2nix")
err := runProcess(testDir, cmdPath, "--dir", testDir, "--outdir", testDir)
if err != nil {
return err
}
buildExpr := fmt.Sprintf("with (import <nixpkgs> { overlays = [ (import %s/overlay.nix) ]; }); callPackage ./%s {}", rootDir, testDir)
err = runProcess(testDir, "nix-build", "--no-out-link", "--expr", buildExpr)
if err != nil {
return err
}
return nil
}
func main() {
files, err := os.ReadDir(".")
if err != nil {
panic(err)
}
testDirs := []string{}
for _, f := range files {
if f.IsDir() {
testDirs = append(testDirs, f.Name())
}
}
var wg sync.WaitGroup
cmdErrChan := make(chan error)
for _, testDir := range testDirs {
testDir := testDir
fmt.Println(fmt.Sprintf("Running test for: '%s'", testDir))
wg.Add(1)
go func() {
defer wg.Done()
err := runTest(testDir)
if err != nil {
cmdErrChan <- err
}
}()
}
go func() {
wg.Wait()
close(cmdErrChan)
}()
for cmdErr := range cmdErrChan {
fmt.Println(fmt.Sprintf("Test for '%s' failed:", cmdErr))
os.Exit(1)
}
}

View file

@ -1,30 +0,0 @@
#!/usr/bin/env python
import subprocess
import os.path
import sys
if __name__ == '__main__':
script_dir = os.path.dirname(os.path.abspath(__file__))
root_dir = os.path.dirname(script_dir)
cmd = os.path.join(root_dir, "gomod2nix")
def run(directory):
print(f"Running {directory}")
subprocess.run([cmd, "--dir", directory, "--outdir", directory], check=True)
build_expr = ("""
with (import <nixpkgs> { overlays = [ (import %s/overlay.nix) ]; }); callPackage %s {}
""".replace("\n", " ") % (root_dir, directory))
subprocess.run(["nix-build", "--expr", build_expr], check=True)
for f in os.listdir(script_dir):
d = os.path.join(script_dir, f)
if os.path.isdir(d):
try:
run(d)
except Exception:
sys.stderr.write(f"Error running {d}\n")