From a8fbacc7ae95442b3b7fe7a92144811bce12393f Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Wed, 28 Sep 2022 13:59:27 +0200 Subject: [PATCH] refactor: simplify the mutator run in the correct folder test (#173) --- internal/engine/executor_test.go | 23 +++++++++---------- internal/engine/stubs_test.go | 38 ++++---------------------------- 2 files changed, 15 insertions(+), 46 deletions(-) diff --git a/internal/engine/executor_test.go b/internal/engine/executor_test.go index b01d843a..f3d00b52 100644 --- a/internal/engine/executor_test.go +++ b/internal/engine/executor_test.go @@ -21,7 +21,6 @@ import ( "fmt" "os" "os/exec" - "path/filepath" "strings" "sync" "testing" @@ -202,6 +201,7 @@ func TestMutatorTestExecution(t *testing.T) { const expectedTimeout = 10 * time.Second type commandHolder struct { + cmd *exec.Cmd command string args []string timeout time.Duration @@ -455,11 +455,8 @@ func TestMutatorRunInTheCorrectFolder(t *testing.T) { executor.Start(w) wg.Wait() - workerName := fmt.Sprintf("%s-%d", w.Name, w.ID) - rootDir, _ := wdDealer.Get(workerName) - execDir := filepath.Join(rootDir, callingDir) - if mut.Workdir() != execDir { - t.Errorf("expected working dir to be %s, got %s", execDir, mut.Workdir()) + if mut.Workdir() != holder.cmd.Dir { + t.Errorf("expected working dir to be %s, got %s", holder.cmd.Dir, mut.Workdir()) } }) } @@ -479,15 +476,17 @@ func fakeExecCommandSuccessWithHolder(got *commandHolder) execContext { dl, _ := ctx.Deadline() got.m.Lock() defer got.m.Unlock() - if got != nil { - got.command = command - got.args = args - got.timeout = time.Until(dl) - } + cs := []string{"-test.run=TestCoverageProcessSuccess", "--", command} cs = append(cs, args...) + cmd := getCmd(ctx, cs) + + got.cmd = cmd + got.command = command + got.args = args + got.timeout = time.Until(dl) - return getCmd(ctx, cs) + return cmd } } diff --git a/internal/engine/stubs_test.go b/internal/engine/stubs_test.go index e23293c3..38df9d16 100644 --- a/internal/engine/stubs_test.go +++ b/internal/engine/stubs_test.go @@ -77,49 +77,19 @@ func filenameFromFixture(fix string) string { type dealerStub struct { t *testing.T - - mutex *sync.RWMutex - cacheDirs map[string]string } func newWdDealerStub(t *testing.T) *dealerStub { t.Helper() - return &dealerStub{ - t: t, - mutex: &sync.RWMutex{}, - cacheDirs: make(map[string]string), - } -} - -func (d *dealerStub) Get(idf string) (string, error) { - dir, ok := d.fromCache(idf) - if ok { - return dir, nil - } - - tmpDir := d.t.TempDir() - d.setCache(idf, tmpDir) - - return tmpDir, nil - -} - -func (d *dealerStub) fromCache(key string) (string, bool) { - d.mutex.RLock() - defer d.mutex.RUnlock() - dir, ok := d.cacheDirs[key] - - return dir, ok + return &dealerStub{t: t} } -func (d *dealerStub) setCache(key, value string) { - d.mutex.Lock() - defer d.mutex.Unlock() - d.cacheDirs[key] = value +func (d dealerStub) Get(_ string) (string, error) { + return d.t.TempDir(), nil } -func (*dealerStub) Clean() {} +func (dealerStub) Clean() {} type executorDealerStub struct { gotMutants []mutator.Mutator