diff --git a/tool/internal/setup/setup.go b/tool/internal/setup/setup.go index 79aa88db..ae20bef9 100644 --- a/tool/internal/setup/setup.go +++ b/tool/internal/setup/setup.go @@ -43,10 +43,12 @@ func (sp *SetupPhase) keepForDebug(srcPath string) { } } -// This function can be used to check if the setup has been completed. +// isSetup reports whether setup has already been completed for the current +// build by checking for the presence of the matched-rules file written by +// store() at the end of a successful Setup run. func isSetup() bool { - // TODO: Implement Task - return false + _, err := os.Stat(util.GetMatchedRuleFile()) + return err == nil } // flagsWithPathValues contains flags that accept a value from "go build" command. diff --git a/tool/internal/setup/setup_test.go b/tool/internal/setup/setup_test.go index ab7aac34..74ef9d08 100644 --- a/tool/internal/setup/setup_test.go +++ b/tool/internal/setup/setup_test.go @@ -226,6 +226,33 @@ func TestSetupGoCache(t *testing.T) { }) } +func TestIsSetup(t *testing.T) { + t.Run("returns false when matched.json does not exist", func(t *testing.T) { + tempDir := t.TempDir() + t.Setenv(util.EnvOtelcWorkDir, tempDir) + if err := os.MkdirAll(util.GetBuildTempDir(), 0o755); err != nil { + t.Fatal(err) + } + if isSetup() { + t.Error("isSetup() = true, expected false when matched.json is absent") + } + }) + + t.Run("returns true when matched.json exists", func(t *testing.T) { + tempDir := t.TempDir() + t.Setenv(util.EnvOtelcWorkDir, tempDir) + if err := os.MkdirAll(util.GetBuildTempDir(), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(util.GetMatchedRuleFile(), []byte("[]"), 0o644); err != nil { + t.Fatal(err) + } + if !isSetup() { + t.Error("isSetup() = false, expected true when matched.json exists") + } + }) +} + func TestExtractBuildFlags(t *testing.T) { tests := []struct { name string