Skip to content

Commit

Permalink
fix: fix failing tests on macos, due to symlinks to temp dir (rollkit…
Browse files Browse the repository at this point in the history
…#1719)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Bug Fixes**
- Improved handling of directory paths and symbolic links in test
functions to ensure accurate path evaluation.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
yarikbratashchuk authored Jun 20, 2024
1 parent 77e1923 commit d674e80
Showing 1 changed file with 38 additions and 18 deletions.
56 changes: 38 additions & 18 deletions config/toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (

func TestFindentrypoint(t *testing.T) {
t.Run("finds entrypoint in current directory", func(t *testing.T) {
dir := t.TempDir()
dir, err := filepath.EvalSymlinks(t.TempDir())
require.NoError(t, err)

require.NoError(t, os.Chdir(dir))

entrypointPath := filepath.Join(dir, "main.go")
err := os.WriteFile(entrypointPath, []byte{}, 0600)
err = os.WriteFile(entrypointPath, []byte{}, 0600)
require.NoError(t, err)

dirName, fullDirPath := FindEntrypoint()
Expand All @@ -23,7 +25,9 @@ func TestFindentrypoint(t *testing.T) {
})

t.Run("returns error if entrypoint not found", func(t *testing.T) {
dir := t.TempDir()
dir, err := filepath.EvalSymlinks(t.TempDir())
require.NoError(t, err)

require.NoError(t, os.Chdir(dir))

dirName, fullDirPath := FindEntrypoint()
Expand All @@ -32,10 +36,11 @@ func TestFindentrypoint(t *testing.T) {
})

t.Run("finds entrypoint in subdirectory", func(t *testing.T) {
parentDir := t.TempDir()
parentDir, err := filepath.EvalSymlinks(t.TempDir())
require.NoError(t, err)

dir := filepath.Join(parentDir, "child")
err := os.Mkdir(dir, 0750)
err = os.Mkdir(dir, 0750)
require.NoError(t, err)

require.NoError(t, os.Chdir(dir))
Expand All @@ -52,9 +57,11 @@ func TestFindentrypoint(t *testing.T) {

func TestFindConfigFile(t *testing.T) {
t.Run("finds config file in current directory", func(t *testing.T) {
dir := t.TempDir()
dir, err := filepath.EvalSymlinks(t.TempDir())
require.NoError(t, err)

configPath := filepath.Join(dir, RollkitToml)
err := os.WriteFile(configPath, []byte{}, 0600)
err = os.WriteFile(configPath, []byte{}, 0600)
require.NoError(t, err)

foundPath, err := findConfigFile(dir)
Expand All @@ -63,9 +70,11 @@ func TestFindConfigFile(t *testing.T) {
})

t.Run("finds config file in parent directory", func(t *testing.T) {
parentDir := t.TempDir()
parentDir, err := filepath.EvalSymlinks(t.TempDir())
require.NoError(t, err)

dir := filepath.Join(parentDir, "child")
err := os.Mkdir(dir, 0750)
err = os.Mkdir(dir, 0750)
require.NoError(t, err)

configPath := filepath.Join(parentDir, RollkitToml)
Expand All @@ -78,16 +87,21 @@ func TestFindConfigFile(t *testing.T) {
})

t.Run("returns error if config file not found", func(t *testing.T) {
_, err := findConfigFile(t.TempDir())
dir, err := filepath.EvalSymlinks(t.TempDir())
require.NoError(t, err)

_, err = findConfigFile(dir)
require.Error(t, err)
})
}

func TestReadToml(t *testing.T) {
t.Run("reads TOML configuration from file", func(t *testing.T) {
dir := t.TempDir()
dir, err := filepath.EvalSymlinks(t.TempDir())
require.NoError(t, err)

configPath := filepath.Join(dir, RollkitToml)
err := os.WriteFile(configPath, []byte(`
err = os.WriteFile(configPath, []byte(`
entrypoint = "./cmd/gm/main.go"
[chain]
Expand All @@ -108,17 +122,21 @@ config_dir = "config"
})

t.Run("returns error if config file not found", func(t *testing.T) {
dir := t.TempDir()
dir, err := filepath.EvalSymlinks(t.TempDir())
require.NoError(t, err)

require.NoError(t, os.Chdir(dir))

_, err := ReadToml()
_, err = ReadToml()
require.Error(t, err)
})

t.Run("sets RootDir even if empty toml", func(t *testing.T) {
dir := t.TempDir()
dir, err := filepath.EvalSymlinks(t.TempDir())
require.NoError(t, err)

configPath := filepath.Join(dir, RollkitToml)
err := os.WriteFile(configPath, []byte{}, 0600)
err = os.WriteFile(configPath, []byte{}, 0600)
require.NoError(t, err)

require.NoError(t, os.Chdir(dir))
Expand All @@ -130,14 +148,16 @@ config_dir = "config"
})

t.Run("returns error if config file cannot be decoded", func(t *testing.T) {
dir := t.TempDir()
dir, err := filepath.EvalSymlinks(t.TempDir())
require.NoError(t, err)

configPath := filepath.Join(dir, RollkitToml)
require.NoError(t, os.WriteFile(configPath, []byte(`
blablabla
`), 0600))

require.NoError(t, os.Chdir(dir))
_, err := ReadToml()
_, err = ReadToml()
require.Error(t, err)
})
}

0 comments on commit d674e80

Please sign in to comment.