From 2fd1764cce505e2c51b96e6310a6b18824753913 Mon Sep 17 00:00:00 2001 From: Matthew Rich Date: Fri, 27 Sep 2024 00:54:27 +0000 Subject: [PATCH] use tempdir pkg to manage test files --- internal/resource/file.go | 6 ++- internal/resource/file_test.go | 61 +++++++++++++++--------------- internal/resource/resource_test.go | 8 ++-- 3 files changed, 39 insertions(+), 36 deletions(-) diff --git a/internal/resource/file.go b/internal/resource/file.go index 06eca6a..c94f367 100644 --- a/internal/resource/file.go +++ b/internal/resource/file.go @@ -195,8 +195,10 @@ func (f *File) Notify(m *machine.EventMessage) { if triggerErr := f.StateMachine().Trigger("created"); triggerErr == nil { return } + } else { + f.State = "absent" + panic(e) } - f.State = "absent" case "start_delete": if deleteErr := f.Delete(ctx); deleteErr == nil { if triggerErr := f.StateMachine().Trigger("deleted"); triggerErr == nil { @@ -417,7 +419,7 @@ func (f *File) Create(ctx context.Context) error { } gid, gidErr := LookupGID(f.Group) if gidErr != nil { - return gidErr + return fmt.Errorf("%w: unkwnon group %d", ErrInvalidFileGroup, gid) } mode, modeErr := f.Mode.GetMode() diff --git a/internal/resource/file_test.go b/internal/resource/file_test.go index dfda0c2..e844ee9 100644 --- a/internal/resource/file_test.go +++ b/internal/resource/file_test.go @@ -32,16 +32,17 @@ func TestNewFileResource(t *testing.T) { } func TestNewFileNormalized(t *testing.T) { - file := fmt.Sprintf("%s/%s", TempDir, "bar/../fooread.txt") - absFilePath,_ := filepath.Abs(file) + indirectFile := fmt.Sprintf("%s/%s", string(TempDir), "bar/../fooread.txt") + absFilePath,_ := filepath.Abs(indirectFile) + f := NewNormalizedFile() assert.NotNil(t, f) - assert.Nil(t, f.SetURI("file://" + file)) + assert.Nil(t, f.SetURI("file://" + indirectFile)) - assert.NotEqual(t, file, f.Path) + assert.NotEqual(t, indirectFile, f.Path) assert.Equal(t, absFilePath, f.Path) - assert.NotEqual(t, "file://" + file, f.URI()) + assert.NotEqual(t, "file://" + indirectFile, f.URI()) assert.Equal(t, "file://" + absFilePath, f.URI()) } @@ -55,7 +56,7 @@ func TestApplyResourceTransformation(t *testing.T) { func TestReadFile(t *testing.T) { ctx := context.Background() - file, _ := filepath.Abs(filepath.Join(TempDir, "fooread.txt")) + file, _ := filepath.Abs(TempDir.FilePath("fooread.txt")) expectedTime, timeErr := time.Parse(time.RFC3339Nano, "2001-12-15T01:01:01.000000001Z") assert.Nil(t, timeErr) @@ -106,7 +107,7 @@ func TestReadFile(t *testing.T) { func TestUseConfig(t *testing.T) { - file, _ := filepath.Abs(filepath.Join(TempDir, "missingfile.txt")) + file, _ := filepath.Abs(TempDir.FilePath("missingfile.txt")) f := NewFile() assert.NotNil(t, f) @@ -123,7 +124,7 @@ func TestUseConfig(t *testing.T) { func TestReadFileError(t *testing.T) { ctx := context.Background() - file, _ := filepath.Abs(filepath.Join(TempDir, "missingfile.txt")) + file, _ := filepath.Abs(TempDir.FilePath("missingfile.txt")) f := NewFile() assert.NotNil(t, f) @@ -134,7 +135,7 @@ func TestReadFileError(t *testing.T) { } func TestCreateFile(t *testing.T) { - file, _ := filepath.Abs(filepath.Join(TempDir, "foo.txt")) + file, _ := filepath.Abs(TempDir.FilePath("foo.txt")) decl := fmt.Sprintf(` path: "%s" @@ -166,7 +167,7 @@ func TestCreateFile(t *testing.T) { } func TestLoadFile(t *testing.T) { - file, _ := filepath.Abs(filepath.Join(TempDir, "foo.txt")) + file, _ := filepath.Abs(TempDir.FilePath("foo.txt")) decl := fmt.Sprintf(` path: "%s" @@ -210,7 +211,7 @@ filetype: "directory" } func TestFileDirectory(t *testing.T) { - file, _ := filepath.Abs(filepath.Join(TempDir, "testdir")) + file, _ := filepath.Abs(TempDir.FilePath("testdir")) decl := fmt.Sprintf(` path: "%s" @@ -237,7 +238,7 @@ func TestFileDirectory(t *testing.T) { } func TestFileTimes(t *testing.T) { - file, _ := filepath.Abs(filepath.Join(TempDir, "testtimes.txt")) + file, _ := filepath.Abs(TempDir.FilePath("testtimes.txt")) decl := fmt.Sprintf(` path: "%s" owner: "%s" @@ -259,7 +260,7 @@ func TestFileTimes(t *testing.T) { } func TestFileSetURI(t *testing.T) { - file, _ := filepath.Abs(filepath.Join(TempDir, "testuri.txt")) + file, _ := filepath.Abs(TempDir.FilePath("testuri.txt")) f := NewFile() assert.NotNil(t, f) e := f.SetURI("file://" + file) @@ -269,10 +270,10 @@ func TestFileSetURI(t *testing.T) { } func TestFileNormalizePath(t *testing.T) { - absFile, absFilePathErr := filepath.Abs(filepath.Join(TempDir, "./testuri.txt")) + absFile, absFilePathErr := filepath.Abs(TempDir.FilePath("./testuri.txt")) assert.Nil(t, absFilePathErr) - file := filepath.Join(TempDir, "./testuri.txt") + file := TempDir.FilePath("./testuri.txt") f := NewFile() assert.NotNil(t, f) @@ -288,7 +289,7 @@ func TestFileUpdateAttributesFromFileInfo(t *testing.T) { f := NewFile() assert.NotNil(t, f) - info, e := os.Lstat(TempDir) + info, e := os.Lstat(string(TempDir)) assert.Nil(t, e) updateAttributesErr := f.UpdateAttributesFromFileInfo(info) @@ -301,8 +302,8 @@ func TestFileUpdateAttributesFromFileInfo(t *testing.T) { func TestFileReadStat(t *testing.T) { ctx := context.Background() - link := filepath.Join(TempDir, "link.txt") - linkTargetFile := filepath.Join(TempDir, "testuri.txt") + link := TempDir.FilePath("link.txt") + linkTargetFile := TempDir.FilePath("testuri.txt") f := NewFile() assert.NotNil(t, f) @@ -345,7 +346,7 @@ func TestFileReadStat(t *testing.T) { } func TestFileResourceFileInfo(t *testing.T) { - testFile := filepath.Join(TempDir, "testuri.txt") + testFile := TempDir.FilePath("testuri.txt") f := NewFile() assert.NotNil(t, f) @@ -370,8 +371,8 @@ func TestFileResourceFileInfo(t *testing.T) { func TestFileClone(t *testing.T) { ctx := context.Background() - testFile := filepath.Join(TempDir, "testorig.txt") - testCloneFile := filepath.Join(TempDir, "testclone.txt") + testFile := TempDir.FilePath("testorig.txt") + testCloneFile := TempDir.FilePath("testclone.txt") f := NewFile() assert.NotNil(t, f) @@ -404,7 +405,7 @@ func TestFileClone(t *testing.T) { func TestFileErrors(t *testing.T) { //ctx := context.Background() - testFile := filepath.Join(TempDir, "testerr.txt") + testFile := TempDir.FilePath("testerr.txt") f := NewFile() assert.NotNil(t, f) @@ -435,7 +436,7 @@ func TestFileErrors(t *testing.T) { } func TestFileDelete(t *testing.T) { - file, _ := filepath.Abs(filepath.Join(TempDir, "foo.txt")) + file, _ := filepath.Abs(TempDir.FilePath("foo.txt")) decl := fmt.Sprintf(` path: "%s" @@ -466,8 +467,8 @@ func TestFileDelete(t *testing.T) { } func TestFileContentRef(t *testing.T) { - file, _ := filepath.Abs(filepath.Join(TempDir, "src.txt")) - copyFile, _ := filepath.Abs(filepath.Join(TempDir, "copy.txt")) + file, _ := filepath.Abs(TempDir.FilePath("src.txt")) + copyFile, _ := filepath.Abs(TempDir.FilePath("copy.txt")) decl := fmt.Sprintf(` path: "%s" @@ -487,7 +488,7 @@ func TestFileContentRef(t *testing.T) { mode: "0600" sourceref: "file://%s" state: present -`, file, ProcessTestUserName, ProcessTestGroupName, copyFile) +`, copyFile, ProcessTestUserName, ProcessTestGroupName, file) f := NewFile() stater := f.StateMachine() @@ -537,7 +538,7 @@ func TestFilePathURI(t *testing.T) { } func TestFileAbsent(t *testing.T) { - file, _ := filepath.Abs(filepath.Join(TempDir, "testabsentstate.txt")) + file, _ := filepath.Abs(TempDir.FilePath("testabsentstate.txt")) decl := fmt.Sprintf(` path: "%s" @@ -563,7 +564,7 @@ func TestFileAbsent(t *testing.T) { func TestFileReader(t *testing.T) { expected := "datatoreadusinganio.readers" dataReader := strings.NewReader(expected) - file, _ := filepath.Abs(filepath.Join(TempDir, "testabsentstate.txt")) + file, _ := filepath.Abs(TempDir.FilePath("testabsentstate.txt")) decl := fmt.Sprintf(` path: "%s" owner: "%s" @@ -589,7 +590,7 @@ func TestFileReader(t *testing.T) { } func TestFileSetURIError(t *testing.T) { - file := fmt.Sprintf("%s/%s", TempDir, "fooread.txt") + file := TempDir.FilePath("fooread.txt") f := NewFile() assert.NotNil(t, f) e := f.SetURI("foo://" + file) @@ -598,7 +599,7 @@ func TestFileSetURIError(t *testing.T) { } func TestFileContentType(t *testing.T) { - file := fmt.Sprintf("%s/%s", TempDir, "fooread.txt") + file := TempDir.FilePath("fooread.txt") f := NewFile() assert.NotNil(t, f) e := f.SetURI("file://" + file) diff --git a/internal/resource/resource_test.go b/internal/resource/resource_test.go index 6d88e5e..e4271dc 100644 --- a/internal/resource/resource_test.go +++ b/internal/resource/resource_test.go @@ -12,16 +12,16 @@ import ( "os/exec" "path/filepath" "testing" + "decl/internal/tempdir" ) -var TempDir string +var TempDir tempdir.Path = "testresourcefile" var ProcessTestUserName string var ProcessTestGroupName string func TestMain(m *testing.M) { - var err error - TempDir, err = os.MkdirTemp("", "testresourcefile") + err := TempDir.Create() if err != nil || TempDir == "" { log.Fatal(err) } @@ -31,7 +31,7 @@ func TestMain(m *testing.M) { ProcessTestUserName, ProcessTestGroupName = ProcessUserName() rc := m.Run() - os.RemoveAll(TempDir) + TempDir.Remove() os.Exit(rc) }