diff --git a/shell.nix b/shell.nix index b984a47..0361a9d 100644 --- a/shell.nix +++ b/shell.nix @@ -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 ]; } diff --git a/tests/Makefile b/tests/Makefile index 75f1ca9..7a5b360 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,2 +1,2 @@ all: - python ./run.py + go run ./run.go diff --git a/tests/run.go b/tests/run.go new file mode 100644 index 0000000..7c00e4c --- /dev/null +++ b/tests/run.go @@ -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 { 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) + } + +} diff --git a/tests/run.py b/tests/run.py deleted file mode 100644 index 62cd860..0000000 --- a/tests/run.py +++ /dev/null @@ -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 { 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")