Add test listing capabilites to harness

We will use this to generate the Github Actions matrix
This commit is contained in:
adisbladis 2022-05-29 15:02:50 +08:00
parent 5c1f8f8467
commit d5d8bbba2d

View file

@ -3,22 +3,17 @@ package main
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"flag"
"fmt" "fmt"
"io" "io"
"os" "os"
"os/exec" "os/exec"
"path"
"path/filepath" "path/filepath"
"runtime"
"sync" "sync"
) )
var cwd = func() string {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
return cwd
}()
type testError struct { type testError struct {
testDir string testDir string
stdout bytes.Buffer stdout bytes.Buffer
@ -71,17 +66,17 @@ func contains(haystack []string, needle string) bool {
return false return false
} }
func runTest(testDir string) error { func runTest(rootDir string, testDir string) error {
rootDir := filepath.Join(cwd, "..") prefix := testDir
cmdPath := filepath.Join(rootDir, "..", "gomod2nix")
cmdPath := filepath.Join(rootDir, "gomod2nix") testDir = filepath.Join(rootDir, testDir)
err := runProcess(testDir, cmdPath, "--dir", testDir, "--outdir", testDir) err := runProcess(prefix, cmdPath, "--dir", testDir, "--outdir", testDir)
if err != nil { if err != nil {
return err return err
} }
buildExpr := fmt.Sprintf("with (import <nixpkgs> { overlays = [ (import %s/overlay.nix) ]; }); callPackage ./%s {}", rootDir, testDir) 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) err = runProcess(prefix, "nix-build", "--no-out-link", "--expr", buildExpr)
if err != nil { if err != nil {
return err return err
} }
@ -89,8 +84,7 @@ func runTest(testDir string) error {
return nil return nil
} }
func main() { func getTestDirs(rootDir string) ([]string, error) {
// Takes too long for Github Actions // Takes too long for Github Actions
var blacklist []string var blacklist []string
if os.Getenv("GITHUB_ACTIONS") == "true" { if os.Getenv("GITHUB_ACTIONS") == "true" {
@ -100,9 +94,9 @@ func main() {
} }
} }
files, err := os.ReadDir(".") files, err := os.ReadDir(rootDir)
if err != nil { if err != nil {
panic(err) return nil, err
} }
testDirs := []string{} testDirs := []string{}
@ -112,6 +106,10 @@ func main() {
} }
} }
return testDirs, nil
}
func runTests(rootDir string, testDirs []string) error {
var wg sync.WaitGroup var wg sync.WaitGroup
cmdErrChan := make(chan error) cmdErrChan := make(chan error)
for _, testDir := range testDirs { for _, testDir := range testDirs {
@ -120,9 +118,9 @@ func main() {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
err := runTest(testDir) err := runTest(rootDir, testDir)
if err != nil { if err != nil {
cmdErrChan <- err cmdErrChan <- fmt.Errorf("Test for '%s' failed: %w", testDir, err)
} }
}() }()
} }
@ -133,8 +131,67 @@ func main() {
}() }()
for cmdErr := range cmdErrChan { for cmdErr := range cmdErrChan {
fmt.Println(fmt.Sprintf("Test for '%s' failed:", cmdErr)) return cmdErr
os.Exit(1) }
return nil
}
func main() {
var rootDir string
{
_, file, _, ok := runtime.Caller(0)
if !ok {
panic("Couldn't get test directory")
}
rootDir = path.Dir(file)
}
flag.Parse()
nArgs := flag.NArg()
action := "run"
if nArgs >= 1 {
action = flag.Arg(0)
}
switch action {
case "list":
testDirs, err := getTestDirs(rootDir)
if err != nil {
panic(err)
}
for _, testDir := range testDirs {
fmt.Println(testDir)
}
return
case "run":
var testDirs []string
var err error
if nArgs > 1 {
args := flag.Args()
testDirs = args[1:nArgs]
} else {
testDirs, err = getTestDirs(rootDir)
if err != nil {
panic(err)
}
}
err = runTests(rootDir, testDirs)
if err != nil {
panic(err)
}
return
default:
panic(fmt.Errorf("Unhandled action: %s", flag.Arg(0)))
} }
} }