Skip to content

Commit

Permalink
Pipeline: cleanup test code
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
  • Loading branch information
rkosegi committed Aug 12, 2024
1 parent 5a156e0 commit bfb0197
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 36 deletions.
7 changes: 1 addition & 6 deletions pipeline/exec_op_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ func TestExecOpDo(t *testing.T) {
)
fout, err := os.CreateTemp("", "yt.*.txt")
ferr, err := os.CreateTemp("", "yt.*.txt")
defer func() {
t.Logf("removing %s", fout.Name())
_ = os.Remove(fout.Name())
t.Logf("removing %s", ferr.Name())
_ = os.Remove(ferr.Name())
}()
removeFilesLater(t, fout, ferr)
assert.NoError(t, err)
eo = &ExecOp{
Program: "sh",
Expand Down
10 changes: 2 additions & 8 deletions pipeline/export_op_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ func TestExportOpDo(t *testing.T) {
if err != nil {
return
}
t.Cleanup(func() {
t.Logf("cleanup temporary file %s", f.Name())
_ = os.Remove(f.Name())
})
removeFilesLater(t, f)
t.Logf("created temporary file: %s", f.Name())
eo = &ExportOp{
File: f.Name(),
Expand Down Expand Up @@ -90,10 +87,7 @@ func TestExportOpDoNonExistentPath(t *testing.T) {
if err != nil {
return
}
t.Cleanup(func() {
t.Logf("cleanup temporary file %s", f.Name())
_ = os.Remove(f.Name())
})
removeFilesLater(t, f)
eo := &ExportOp{
File: f.Name(),
Path: "this.path.does.not.exist",
Expand Down
27 changes: 5 additions & 22 deletions pipeline/template_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,7 @@ func TestTemplateFuncFileExists(t *testing.T) {
if err != nil {
return
}
t.Cleanup(func() {
t.Logf("cleanup temporary file %s", f.Name())
_ = os.Remove(f.Name())
})
removeFilesLater(t, f)
assert.True(t, fileExistsFunc(f.Name()))
}

Expand All @@ -162,12 +159,7 @@ func TestTemplateFuncMergeFiles(t *testing.T) {
res, err := mergeFilesFunc(f1.Name(), f2.Name())
assert.NoError(t, err)
assert.NotNil(t, res)
t.Cleanup(func() {
t.Logf("cleanup temporary file %s", f1.Name())
_ = os.Remove(f1.Name())
t.Logf("cleanup temporary file %s", f2.Name())
_ = os.Remove(f2.Name())
})
removeFilesLater(t, f1, f2)
}

func TestTemplateFuncMergeFilesInvalid(t *testing.T) {
Expand All @@ -177,19 +169,13 @@ func TestTemplateFuncMergeFilesInvalid(t *testing.T) {
res, err := mergeFilesFunc(f2.Name())
assert.Error(t, err)
assert.Nil(t, res)
t.Cleanup(func() {
t.Logf("cleanup temporary file %s", f2.Name())
_ = os.Remove(f2.Name())
})
removeFilesLater(t, f2)
}

func TestTemplateFuncIsDir(t *testing.T) {
d, err := os.MkdirTemp("", "yt*")
assert.NoError(t, err)
t.Cleanup(func() {
t.Logf("deleting temporary directory %s", d)
_ = os.RemoveAll(d)
})
removeDirsLater(t, d)
assert.True(t, isDirFunc(d))
assert.False(t, isDirFunc("/i hope/this/path/does/not/exist"))
}
Expand All @@ -198,10 +184,7 @@ func TestTemplateFuncGlob(t *testing.T) {
d, err := os.MkdirTemp("", "yt*")
assert.NoError(t, err)
assert.NoError(t, os.WriteFile(d+"/1.yaml", []byte{}, 0o664))
t.Cleanup(func() {
t.Logf("deleting temporary directory %s", d)
_ = os.RemoveAll(d)
})
removeDirsLater(t, d)
files, err := globFunc(d + "/*.yaml")
assert.NoError(t, err)
assert.Equal(t, 1, len(files))
Expand Down
19 changes: 19 additions & 0 deletions pipeline/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package pipeline
import (
"github.com/rkosegi/yaml-toolkit/dom"
"github.com/stretchr/testify/assert"
"os"
"regexp"
"testing"
)
Expand All @@ -35,6 +36,24 @@ func mockActCtx(data dom.ContainerBuilder) ActionContext {
return New(WithData(data)).(*exec).newCtx(nil)
}

func removeFilesLater(t *testing.T, files ...*os.File) {
t.Cleanup(func() {
for _, f := range files {
t.Logf("cleanup temporary file %s", f.Name())
_ = os.Remove(f.Name())
}
})
}

func removeDirsLater(t *testing.T, dirs ...string) {
t.Cleanup(func() {
for _, f := range dirs {
t.Logf("delete temporary directory %s", f)
_ = os.RemoveAll(f)
}
})
}

func TestGetActionFromContext(t *testing.T) {
ac := mockEmptyActCtx().(*actContext)
ac.c = &ExportOp{}
Expand Down

0 comments on commit bfb0197

Please sign in to comment.