mirror of
https://gitlab.com/TECHNOFAB/nixtest.git
synced 2025-12-12 02:00:18 +01:00
chore!: default to pure mode, rename --pure flag to --impure for switching
This commit is contained in:
parent
22b43c9fe8
commit
c9298b91f4
11 changed files with 29 additions and 29 deletions
|
|
@ -16,7 +16,7 @@ type AppConfig struct {
|
|||
JunitPath string
|
||||
UpdateSnapshots bool
|
||||
SkipPattern string
|
||||
PureEnv bool
|
||||
ImpureEnv bool
|
||||
NoColor bool
|
||||
}
|
||||
|
||||
|
|
@ -29,7 +29,7 @@ func Load() AppConfig {
|
|||
flag.StringVar(&cfg.JunitPath, "junit", "", "Path to generate JUNIT report to, leave empty to disable")
|
||||
flag.BoolVarP(&cfg.UpdateSnapshots, "update-snapshots", "u", false, "Update all snapshots")
|
||||
flag.StringVarP(&cfg.SkipPattern, "skip", "s", "", "Regular expression to skip tests (e.g., 'test-.*|.*-b')")
|
||||
flag.BoolVar(&cfg.PureEnv, "pure", false, "Unset all env vars before running script tests")
|
||||
flag.BoolVar(&cfg.ImpureEnv, "impure", false, "Don't unset all env vars before running script tests")
|
||||
flag.BoolVar(&cfg.NoColor, "no-color", false, "Disable coloring")
|
||||
helpRequested := flag.BoolP("help", "h", false, "Show this menu")
|
||||
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ func TestLoad_CustomValues(t *testing.T) {
|
|||
"--junit", "report.xml",
|
||||
"-u",
|
||||
"--skip", "specific-test",
|
||||
"--pure",
|
||||
"--impure",
|
||||
"--no-color",
|
||||
}
|
||||
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError) // Reset flags
|
||||
|
|
@ -83,7 +83,7 @@ func TestLoad_CustomValues(t *testing.T) {
|
|||
if cfg.SkipPattern != "specific-test" {
|
||||
t.Errorf("SkipPattern: got %s, want specific-test", cfg.SkipPattern)
|
||||
}
|
||||
if !cfg.PureEnv {
|
||||
t.Errorf("PureEnv: got %v, want true", cfg.PureEnv)
|
||||
if !cfg.ImpureEnv {
|
||||
t.Errorf("ImpureEnv: got %v, want true", cfg.ImpureEnv)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ import (
|
|||
type Service interface {
|
||||
BuildDerivation(derivation string) (string, error)
|
||||
BuildAndParseJSON(derivation string) (any, error)
|
||||
BuildAndRunScript(derivation string, pureEnv bool) (exitCode int, stdout string, stderr string, err error)
|
||||
BuildAndRunScript(derivation string, impureEnv bool) (exitCode int, stdout string, stderr string, err error)
|
||||
}
|
||||
|
||||
type DefaultService struct {
|
||||
|
|
@ -72,7 +72,7 @@ func (s *DefaultService) BuildAndParseJSON(derivation string) (any, error) {
|
|||
}
|
||||
|
||||
// BuildAndRunScript builds a derivation and runs it as a script
|
||||
func (s *DefaultService) BuildAndRunScript(derivation string, pureEnv bool) (exitCode int, stdout string, stderr string, err error) {
|
||||
func (s *DefaultService) BuildAndRunScript(derivation string, impureEnv bool) (exitCode int, stdout string, stderr string, err error) {
|
||||
exitCode = -1
|
||||
path, err := s.BuildDerivation(derivation)
|
||||
if err != nil {
|
||||
|
|
@ -80,10 +80,10 @@ func (s *DefaultService) BuildAndRunScript(derivation string, pureEnv bool) (exi
|
|||
}
|
||||
|
||||
var cmdArgs []string
|
||||
if pureEnv {
|
||||
cmdArgs = append([]string{"env", "-i"}, "bash", path)
|
||||
} else {
|
||||
if impureEnv {
|
||||
cmdArgs = []string{"bash", path}
|
||||
} else {
|
||||
cmdArgs = append([]string{"env", "-i"}, "bash", path)
|
||||
}
|
||||
|
||||
cmd := s.commandExecutor(cmdArgs[0], cmdArgs[1:]...)
|
||||
|
|
|
|||
|
|
@ -232,7 +232,7 @@ func TestDefaultService_BuildAndRunScript(t *testing.T) {
|
|||
tests := []struct {
|
||||
name string
|
||||
derivation string
|
||||
pureEnv bool
|
||||
impureEnv bool
|
||||
mockBuildDrvOutput string
|
||||
mockBuildDrvError string
|
||||
mockBuildDrvExitCode string
|
||||
|
|
@ -252,7 +252,7 @@ func TestDefaultService_BuildAndRunScript(t *testing.T) {
|
|||
0, "Hello", "ErrOut", false, nil, "",
|
||||
},
|
||||
{
|
||||
"Success pure", "script.drv#sh", true, mockScriptPath, "", "0",
|
||||
"Success impure", "script.drv#sh", true, mockScriptPath, "", "0",
|
||||
"Hello", "ErrOut", "0",
|
||||
0, "Hello", "ErrOut", false, nil, "",
|
||||
},
|
||||
|
|
@ -277,7 +277,7 @@ func TestDefaultService_BuildAndRunScript(t *testing.T) {
|
|||
os.Setenv("MOCK_SCRIPT_STDERR", tt.mockScriptStderr)
|
||||
os.Setenv("MOCK_SCRIPT_EXIT_CODE", tt.mockScriptExitCode)
|
||||
|
||||
exitCode, stdout, stderr, err := service.BuildAndRunScript(tt.derivation, tt.pureEnv)
|
||||
exitCode, stdout, stderr, err := service.BuildAndRunScript(tt.derivation, tt.impureEnv)
|
||||
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Fatalf("BuildAndRunScript() error = %v, wantErr %v", err, tt.wantErr)
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ type Config struct {
|
|||
SnapshotDir string
|
||||
UpdateSnapshots bool
|
||||
SkipPattern string
|
||||
PureEnv bool
|
||||
ImpureEnv bool
|
||||
}
|
||||
|
||||
func New(cfg Config, nixService nix.Service, snapService snapshot.Service) (*Runner, error) {
|
||||
|
|
@ -181,7 +181,7 @@ func (r *Runner) handleUnitTest(result *types.TestResult, spec types.TestSpec, a
|
|||
|
||||
// handleScriptTest processes script type tests
|
||||
func (r *Runner) handleScriptTest(result *types.TestResult, spec types.TestSpec) {
|
||||
exitCode, stdout, stderrStr, err := r.nixService.BuildAndRunScript(spec.Script, r.config.PureEnv)
|
||||
exitCode, stdout, stderrStr, err := r.nixService.BuildAndRunScript(spec.Script, r.config.ImpureEnv)
|
||||
if err != nil {
|
||||
result.Status = types.StatusError
|
||||
result.ErrorMessage = fmt.Sprintf("[system] failed to run script derivation %s: %v", spec.Script, err)
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ import (
|
|||
type mockNixService struct {
|
||||
BuildDerivationFunc func(derivation string) (string, error)
|
||||
BuildAndParseJSONFunc func(derivation string) (any, error)
|
||||
BuildAndRunScriptFunc func(derivation string, pureEnv bool) (exitCode int, stdout string, stderr string, err error)
|
||||
BuildAndRunScriptFunc func(derivation string, impureEnv bool) (exitCode int, stdout string, stderr string, err error)
|
||||
}
|
||||
|
||||
func (m *mockNixService) BuildDerivation(d string) (string, error) {
|
||||
|
|
@ -253,7 +253,7 @@ func TestRunner_executeTest(t *testing.T) {
|
|||
spec: types.TestSpec{Name: "ScriptSuccess", Type: types.TestTypeScript, Script: "script.sh"},
|
||||
runnerConfig: Config{},
|
||||
setupMockServices: func(t *testing.T, mNix *mockNixService, mSnap *mockSnapshotService, s types.TestSpec, c Config) {
|
||||
mNix.BuildAndRunScriptFunc = func(derivation string, pureEnv bool) (int, string, string, error) {
|
||||
mNix.BuildAndRunScriptFunc = func(derivation string, impureEnv bool) (int, string, string, error) {
|
||||
return 0, "stdout", "stderr", nil
|
||||
}
|
||||
},
|
||||
|
|
@ -264,7 +264,7 @@ func TestRunner_executeTest(t *testing.T) {
|
|||
spec: types.TestSpec{Name: "ScriptFail", Type: types.TestTypeScript, Script: "script.sh"},
|
||||
runnerConfig: Config{},
|
||||
setupMockServices: func(t *testing.T, mNix *mockNixService, mSnap *mockSnapshotService, s types.TestSpec, c Config) {
|
||||
mNix.BuildAndRunScriptFunc = func(derivation string, pureEnv bool) (int, string, string, error) {
|
||||
mNix.BuildAndRunScriptFunc = func(derivation string, impureEnv bool) (int, string, string, error) {
|
||||
return 1, "out on fail", "err on fail", nil
|
||||
}
|
||||
},
|
||||
|
|
@ -313,7 +313,7 @@ func TestRunner_RunTests(t *testing.T) {
|
|||
mockSnapSvc := &mockSnapshotService{}
|
||||
|
||||
mockNixSvc.BuildAndParseJSONFunc = func(derivation string) (any, error) { return "parsed", nil }
|
||||
mockNixSvc.BuildAndRunScriptFunc = func(derivation string, pureEnv bool) (int, string, string, error) { return 0, "", "", nil }
|
||||
mockNixSvc.BuildAndRunScriptFunc = func(derivation string, impureEnv bool) (int, string, string, error) { return 0, "", "", nil }
|
||||
mockSnapSvc.StatFunc = func(name string) (os.FileInfo, error) { return mockFileInfo{}, nil }
|
||||
mockSnapSvc.LoadFileFunc = func(filePath string) (any, error) { return "snapshot", nil }
|
||||
mockSnapSvc.CreateFileFunc = func(filePath string, data any) error { return nil }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue