Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tester.SetupDir #19

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions tester/setupdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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))
}
}

Expand Down
16 changes: 8 additions & 8 deletions tester/setupdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -27,15 +27,15 @@ 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))
sourceFile := filepath.Join(sourceDir, "file.txt")
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))
Expand All @@ -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")
Expand Down Expand Up @@ -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()
})
}
Loading