Skip to content

Commit

Permalink
Don't rely on current directory as base in file-system tests
Browse files Browse the repository at this point in the history
Summary:
To de-flaky the tests

https://www.internalfb.com/intern/test/281475092932540?ref_report_id=0

Differential Revision: D58868495
  • Loading branch information
inesusvet authored and facebook-github-bot committed Jun 24, 2024
1 parent cdc8a84 commit 27c139e
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions pkg/blocks/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ THE SOFTWARE.
package blocks

import (
"fmt"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -100,22 +101,22 @@ func TestFetchAbs(t *testing.T) {
}

func TestFindFilePath(t *testing.T) {
workdir, err := os.Getwd()
assert.NoError(t, err)

tempDir := filepath.Join(workdir, "temp_test_directory")
err = os.MkdirAll(tempDir, 0755)
tempDir, err := os.MkdirTemp("", "temp_test_directory")
tempDirName := filepath.Base(tempDir)
assert.NoError(t, err)
defer os.RemoveAll(tempDir)
workDir := filepath.Dir(tempDir)

tempFile := filepath.Join(tempDir, "test_file.txt")
testFileName := "test_file.txt"
tempFile := filepath.Join(tempDir, testFileName)
f, err := os.Create(tempFile)
assert.NoError(t, err)
f.Close()
defer os.Remove(tempFile)

// Create a tilde file for testing
tildeFile := filepath.Join(os.Getenv("HOME"), "tilde_test_file.txt")
uniqueFileName := fmt.Sprintf("tilde_test_file%d.txt", os.Getpid())
tildeFile := filepath.Join(os.Getenv("HOME"), uniqueFileName)
f, err = os.Create(tildeFile)
assert.NoError(t, err)
f.Close()
Expand All @@ -137,8 +138,8 @@ func TestFindFilePath(t *testing.T) {
},
{
name: "Relative path",
inputPath: "temp_test_directory/test_file.txt",
inputWorkdir: workdir,
inputPath: filepath.Join(tempDirName, testFileName),
inputWorkdir: workDir,
fsStat: nil,
expectError: false,
},
Expand All @@ -151,7 +152,7 @@ func TestFindFilePath(t *testing.T) {
},
{
name: "Tilde path",
inputPath: "~/tilde_test_file.txt",
inputPath: filepath.Join("~", uniqueFileName),
inputWorkdir: "",
fsStat: nil,
expectError: false,
Expand Down

0 comments on commit 27c139e

Please sign in to comment.