From 04632c868e1a299c2fba1226c328394290abf49a Mon Sep 17 00:00:00 2001 From: jmnote Date: Sun, 6 Oct 2024 04:39:35 +0900 Subject: [PATCH] SetupDir --- tester/setupdir.go | 12 ++++++------ tester/setupdir_test.go | 16 ++++++++-------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tester/setupdir.go b/tester/setupdir.go index 9267099..4724d3d 100644 --- a/tester/setupdir.go +++ b/tester/setupdir.go @@ -10,7 +10,7 @@ import ( ) // mustFindProjectRoot finds the project root by looking for "go.mod" file. -func mustFindProjectRoot() string { +func FindProjectRoot() string { dir, err := os.Getwd() if err != nil { panic(fmt.Errorf("failed to os.Getwd: %v", err)) @@ -30,11 +30,11 @@ func mustFindProjectRoot() string { } // SetupDir sets up a temporary test environment by copying necessary files and directories. -func MustSetupDir(t *testing.T, pathsToCopy map[string]string) (string, func()) { +func SetupDir(t *testing.T, pathsToCopy map[string]string) (string, func()) { // Store the current working directory wd, err := os.Getwd() if err != nil { - panic(fmt.Errorf("MustSetupDir: failed to os.Getwd: %v", err)) + panic(fmt.Errorf("SetupDir: failed to os.Getwd: %v", err)) } // Create a temporary directory @@ -46,11 +46,11 @@ func MustSetupDir(t *testing.T, pathsToCopy map[string]string) (string, func()) } defer func() { if err := os.Chdir(tempDir); err != nil { - panic(fmt.Errorf("MustSetupDir: failed to os.Chdir: %v", err)) + panic(fmt.Errorf("SetupDir: failed to os.Chdir: %v", err)) } }() - projectRoot := mustFindProjectRoot() + projectRoot := FindProjectRoot() // Copy specified paths to the temporary directory for source, destination := range pathsToCopy { @@ -62,7 +62,7 @@ func MustSetupDir(t *testing.T, pathsToCopy map[string]string) (string, func()) } if err := copyPath(sourcePath, destination); err != nil { - panic(fmt.Errorf("MustSetupDir: failed to copyPath: %v", err)) + panic(fmt.Errorf("SetupDir: failed to copyPath: %v", err)) } } diff --git a/tester/setupdir_test.go b/tester/setupdir_test.go index f3a8892..1e4c4e5 100644 --- a/tester/setupdir_test.go +++ b/tester/setupdir_test.go @@ -8,14 +8,14 @@ import ( "github.com/stretchr/testify/assert" ) -func TestMustSetupDir_ValidFiles(t *testing.T) { +func TestSetupDir_ValidFiles(t *testing.T) { tempDir := t.TempDir() sourceFile := filepath.Join(tempDir, "source.txt") err := os.WriteFile(sourceFile, []byte("test data"), 0644) assert.NoError(t, err) pathsToCopy := map[string]string{sourceFile: ""} - destDir, cleanup := MustSetupDir(t, pathsToCopy) + destDir, cleanup := SetupDir(t, pathsToCopy) defer cleanup() assert.NoError(t, err) @@ -27,7 +27,7 @@ func TestMustSetupDir_ValidFiles(t *testing.T) { assert.Equal(t, "test data", string(content)) } -func TestMustSetupDir_DirectoryCopy(t *testing.T) { +func TestSetupDir_DirectoryCopy(t *testing.T) { tempDir := t.TempDir() sourceDir := filepath.Join(tempDir, "sourceDir") assert.NoError(t, os.Mkdir(sourceDir, 0755)) @@ -35,7 +35,7 @@ func TestMustSetupDir_DirectoryCopy(t *testing.T) { assert.NoError(t, os.WriteFile(sourceFile, []byte("test data"), 0644)) pathsToCopy := map[string]string{sourceDir: ""} - destDir, cleanup := MustSetupDir(t, pathsToCopy) + destDir, cleanup := SetupDir(t, pathsToCopy) defer cleanup() destPath := filepath.Join(destDir, filepath.Base(sourceDir)) @@ -49,14 +49,14 @@ func TestMustSetupDir_DirectoryCopy(t *testing.T) { assert.Equal(t, "test data", string(content)) } -func TestMustSetupDir_DestinationPath(t *testing.T) { +func TestSetupDir_DestinationPath(t *testing.T) { tempDir := t.TempDir() sourceFile := filepath.Join(tempDir, "source.txt") err := os.WriteFile(sourceFile, []byte("test data"), 0644) assert.NoError(t, err) pathsToCopy := map[string]string{sourceFile: "custom/destination.txt"} - destDir, cleanup := MustSetupDir(t, pathsToCopy) + destDir, cleanup := SetupDir(t, pathsToCopy) defer cleanup() destFile := filepath.Join(destDir, "custom/destination.txt") @@ -113,10 +113,10 @@ func TestCopyFile(t *testing.T) { assert.Equal(t, content, copiedContent) } -func TestMustSetupDir_NonExistentSource(t *testing.T) { +func TestSetupDir_NonExistentSource(t *testing.T) { assert.Panics(t, func() { pathsToCopy := map[string]string{"nonexistent.txt": ""} - _, cleanup := MustSetupDir(t, pathsToCopy) + _, cleanup := SetupDir(t, pathsToCopy) defer cleanup() }) }