2022-05-28 14:56:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2022-05-29 07:02:50 +00:00
|
|
|
"flag"
|
2022-05-28 14:56:09 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2022-05-29 07:02:50 +00:00
|
|
|
"path"
|
2022-05-28 14:56:09 +00:00
|
|
|
"path/filepath"
|
2022-05-29 07:02:50 +00:00
|
|
|
"runtime"
|
2022-05-28 14:56:09 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2022-06-13 19:26:50 +00:00
|
|
|
func runProcess(prefix string, env []string, command string, args ...string) error {
|
2022-05-30 10:36:26 +00:00
|
|
|
fmt.Printf("%s: Executing %s %s\n", prefix, command, args)
|
2022-05-28 14:56:09 +00:00
|
|
|
|
|
|
|
cmd := exec.Command(command, args...)
|
|
|
|
|
2022-06-13 19:26:50 +00:00
|
|
|
if env != nil {
|
|
|
|
cmd.Env = env
|
|
|
|
}
|
|
|
|
|
2022-05-28 14:56:09 +00:00
|
|
|
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()
|
2022-05-30 10:36:26 +00:00
|
|
|
fmt.Printf("%s: %s\n", prefix, line)
|
2022-05-28 14:56:09 +00:00
|
|
|
}
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
<-done
|
|
|
|
|
|
|
|
return cmd.Wait()
|
|
|
|
}
|
|
|
|
|
2022-05-28 19:28:20 +00:00
|
|
|
func contains(haystack []string, needle string) bool {
|
|
|
|
for _, s := range haystack {
|
|
|
|
if s == needle {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-29 07:02:50 +00:00
|
|
|
func runTest(rootDir string, testDir string) error {
|
|
|
|
prefix := testDir
|
|
|
|
testDir = filepath.Join(rootDir, testDir)
|
2022-06-13 19:26:50 +00:00
|
|
|
cmdPath := filepath.Join(rootDir, "..", "gomod2nix")
|
2022-05-30 10:33:22 +00:00
|
|
|
|
2022-06-13 19:26:50 +00:00
|
|
|
if _, err := os.Stat(filepath.Join(testDir, "script")); err == nil {
|
|
|
|
env := append(os.Environ(), "GOMOD2NIX="+cmdPath)
|
|
|
|
err := runProcess(prefix, env, filepath.Join(testDir, "script"))
|
2022-05-30 10:33:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-13 19:26:50 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
if _, err := os.Stat(filepath.Join(testDir, "go.mod")); err == nil {
|
|
|
|
err := runProcess(prefix, nil, cmdPath, "--dir", testDir, "--outdir", testDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-05-28 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
2022-05-29 07:02:50 +00:00
|
|
|
buildExpr := fmt.Sprintf("with (import <nixpkgs> { overlays = [ (import %s/../overlay.nix) ]; }); callPackage %s {}", rootDir, testDir)
|
2022-06-13 19:26:50 +00:00
|
|
|
err := runProcess(prefix, nil, "nix-build", "--no-out-link", "--expr", buildExpr)
|
2022-05-28 14:56:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-29 07:02:50 +00:00
|
|
|
func getTestDirs(rootDir string) ([]string, error) {
|
2022-05-28 19:28:20 +00:00
|
|
|
// Takes too long for Github Actions
|
|
|
|
var blacklist []string
|
|
|
|
if os.Getenv("GITHUB_ACTIONS") == "true" {
|
|
|
|
blacklist = []string{
|
|
|
|
"helm",
|
|
|
|
"minikube",
|
2022-05-30 10:12:27 +00:00
|
|
|
"cross",
|
2022-05-28 19:28:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-29 07:02:50 +00:00
|
|
|
files, err := os.ReadDir(rootDir)
|
2022-05-28 14:56:09 +00:00
|
|
|
if err != nil {
|
2022-05-29 07:02:50 +00:00
|
|
|
return nil, err
|
2022-05-28 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testDirs := []string{}
|
|
|
|
for _, f := range files {
|
2022-05-28 19:28:20 +00:00
|
|
|
if f.IsDir() && !contains(blacklist, f.Name()) {
|
2022-05-28 14:56:09 +00:00
|
|
|
testDirs = append(testDirs, f.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-29 07:02:50 +00:00
|
|
|
return testDirs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runTests(rootDir string, testDirs []string) error {
|
2022-05-28 14:56:09 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
cmdErrChan := make(chan error)
|
|
|
|
for _, testDir := range testDirs {
|
|
|
|
testDir := testDir
|
2022-05-30 10:36:26 +00:00
|
|
|
fmt.Printf("Running test for: '%s'\n", testDir)
|
2022-05-28 14:56:09 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2022-05-29 07:02:50 +00:00
|
|
|
err := runTest(rootDir, testDir)
|
2022-05-28 14:56:09 +00:00
|
|
|
if err != nil {
|
2022-05-29 07:02:50 +00:00
|
|
|
cmdErrChan <- fmt.Errorf("Test for '%s' failed: %w", testDir, err)
|
2022-05-28 14:56:09 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(cmdErrChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for cmdErr := range cmdErrChan {
|
2022-05-29 07:02:50 +00:00
|
|
|
return cmdErr
|
|
|
|
}
|
|
|
|
|
|
|
|
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)))
|
2022-05-28 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|