Skip to content

Commit

Permalink
pkg/entrypoint: clean after running tests
Browse files Browse the repository at this point in the history
TestEntrypointer_OnError leaves some created file behind :
`termination`. This change does 2 things:

- Use a temporary file in order to not create anything in the current
directory
- Make sure we remove the file once the test is done

This also uses a temporary file for TestEntrypointer

Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester authored and tekton-robot committed Aug 20, 2021
1 parent 67b318b commit 44fb536
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions pkg/entrypoint/entrypointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ func TestEntrypointer(t *testing.T) {
t.Run(c.desc, func(t *testing.T) {
fw, fr, fpw := &fakeWaiter{}, &fakeRunner{}, &fakePostWriter{}
timeout := time.Duration(0)
terminationPath := "termination"
if terminationFile, err := ioutil.TempFile("", "termination"); err != nil {
t.Fatalf("unexpected error creating temporary termination file: %v", err)
} else {
terminationPath = terminationFile.Name()
defer os.Remove(terminationFile.Name())
}
err := Entrypointer{
Entrypoint: c.entrypoint,
WaitFiles: c.waitFiles,
Expand All @@ -175,7 +182,7 @@ func TestEntrypointer(t *testing.T) {
Waiter: fw,
Runner: fr,
PostWriter: fpw,
TerminationPath: "termination",
TerminationPath: terminationPath,
Timeout: &timeout,
BreakpointOnFailure: c.breakpointOnFailure,
StepMetadataDir: c.stepDir,
Expand Down Expand Up @@ -221,7 +228,7 @@ func TestEntrypointer(t *testing.T) {
if c.postFile == "" && fpw.wrote != nil {
t.Errorf("Wrote post file when not required")
}
fileContents, err := ioutil.ReadFile("termination")
fileContents, err := ioutil.ReadFile(terminationPath)
if err == nil {
var entries []v1alpha1.PipelineResourceResult
if err := json.Unmarshal([]byte(fileContents), &entries); err == nil {
Expand All @@ -239,7 +246,7 @@ func TestEntrypointer(t *testing.T) {
} else if !os.IsNotExist(err) {
t.Error("Wanted termination file written, got nil")
}
if err := os.Remove("termination"); err != nil {
if err := os.Remove(terminationPath); err != nil {
t.Errorf("Could not remove termination path: %s", err)
}

Expand Down Expand Up @@ -309,6 +316,13 @@ func TestEntrypointer_OnError(t *testing.T) {
}} {
t.Run(c.desc, func(t *testing.T) {
fpw := &fakePostWriter{}
terminationPath := "termination"
if terminationFile, err := ioutil.TempFile("", "termination"); err != nil {
t.Fatalf("unexpected error creating temporary termination file: %v", err)
} else {
terminationPath = terminationFile.Name()
defer os.Remove(terminationFile.Name())
}
err := Entrypointer{
Entrypoint: "echo",
WaitFiles: []string{},
Expand All @@ -317,7 +331,7 @@ func TestEntrypointer_OnError(t *testing.T) {
Waiter: &fakeWaiter{},
Runner: c.runner,
PostWriter: fpw,
TerminationPath: "termination",
TerminationPath: terminationPath,
OnError: c.onError,
}.Go()

Expand Down

0 comments on commit 44fb536

Please sign in to comment.