Skip to content

Commit

Permalink
test: resolve windows test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
tyhopp committed Sep 20, 2024
1 parent 48db0a6 commit 9f87981
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions lenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,26 @@ func clean() {
}
}

func cleanupTempFile(f *os.File) error {
err := f.Close()
if err != nil {
return fmt.Errorf("failed to close temp file: %v", err)
}
err = os.Remove(f.Name())
if err != nil {
return fmt.Errorf("failed to remove temp file: %v", err)
}
return nil
}

func TestGetEnvFilePath(t *testing.T) {
t.Cleanup(clean)

tmpEnvFile, err := os.Create(paths["env"])
if err != nil {
t.Fatalf("failed to create temp .env file: %v", err)
}
defer os.Remove(tmpEnvFile.Name())
defer cleanupTempFile(tmpEnvFile)

path, err := GetEnvFilePath(paths["env"])
fmt.Printf("path: %s\n", path)
Expand Down Expand Up @@ -75,7 +87,7 @@ func TestReadLenvFile(t *testing.T) {
}
expectedDestinations := []string{"a", "b"}
tmpLenvFile.WriteString(strings.Join(expectedDestinations, "\n"))
defer os.Remove(tmpLenvFile.Name())
defer cleanupTempFile(tmpLenvFile)

destinations, err := ReadLenvFile()
if err != nil {
Expand Down Expand Up @@ -113,7 +125,7 @@ func TestCheck(t *testing.T) {
if err != nil {
t.Fatalf("failed to create temp .env file: %v", err)
}
defer os.Remove(tmpEnvFile.Name())
defer cleanupTempFile(tmpEnvFile)

for _, dest := range destinations {
err := os.Symlink(source, dest)
Expand All @@ -139,14 +151,14 @@ func TestCheck_SymlinkPointsToDifferentSource(t *testing.T) {
if err != nil {
t.Fatalf("failed to create temp .env file: %v", err)
}
defer os.Remove(tmpEnvFile.Name())
defer cleanupTempFile(tmpEnvFile)

differentSource := paths["env2"]
tmpEnvFile2, err := os.Create(differentSource)
if err != nil {
t.Fatalf("failed to create temp .env2 file: %v", err)
}
defer os.Remove(tmpEnvFile2.Name())
defer cleanupTempFile(tmpEnvFile2)

err = os.Symlink(differentSource, destinations[0])
if err != nil {
Expand All @@ -170,13 +182,13 @@ func TestCheck_PhysicalFileExists(t *testing.T) {
if err != nil {
t.Fatalf("failed to create temp .env file: %v", err)
}
defer os.Remove(tmpEnvFile.Name())
defer cleanupTempFile(tmpEnvFile)

tmpDestFile, err := os.Create(destinations[0])
if err != nil {
t.Fatalf("failed to create temp destination file: %v", err)
}
defer os.Remove(tmpDestFile.Name())
defer cleanupTempFile(tmpDestFile)

err = Check(source, destinations)
if err == nil {
Expand All @@ -194,7 +206,7 @@ func TestLink(t *testing.T) {
if err != nil {
t.Fatalf("failed to create temp .env file: %v", err)
}
defer os.Remove(tmpEnvFile.Name())
defer cleanupTempFile(tmpEnvFile)

err = Link(source, destinations)
if err != nil {
Expand All @@ -212,13 +224,13 @@ func TestLink_PhysicalFileExists(t *testing.T) {
if err != nil {
t.Fatalf("failed to create temp .env file: %v", err)
}
defer os.Remove(tmpEnvFile.Name())
defer cleanupTempFile(tmpEnvFile)

tmpDestFile, err := os.Create(destinations[0])
if err != nil {
t.Fatalf("failed to create temp destination file: %v", err)
}
defer os.Remove(tmpDestFile.Name())
defer cleanupTempFile(tmpDestFile)

err = Link(source, destinations)
if err == nil {
Expand All @@ -236,7 +248,7 @@ func TestUnlink(t *testing.T) {
if err != nil {
t.Fatalf("failed to create temp .env file: %v", err)
}
defer os.Remove(tmpEnvFile.Name())
defer cleanupTempFile(tmpEnvFile)

for _, dest := range destinations {
err := os.Symlink(source, dest)
Expand Down

0 comments on commit 9f87981

Please sign in to comment.