Skip to content

Commit

Permalink
FS tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Jan 6, 2025
1 parent 0566464 commit be85f5c
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/config/alt/alt.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (a *Alt) GenerateAndSave() error {
}

func (a *Alt) generateExecutable() string {
if runtime.GOOS == "windows" {
if runtime.GOOS == "windows" { //nolint:goconst
return "@echo off\r\n" +
":: " + a.comment + "\r\n" +
`set "CLI_CONFIG_FILE=` + a.configPath + `"` + "\r\n" +
Expand Down
107 changes: 107 additions & 0 deletions internal/config/alt/fs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package alt

import (
"os"
"path/filepath"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFindConfigDir(t *testing.T) {
tempDir := t.TempDir()

t.Run("XDG_CONFIG_HOME exists", func(t *testing.T) {
switch runtime.GOOS {
case "windows", "darwin", "ios", "plan9":
t.Skip()
}
err := os.Setenv("XDG_CONFIG_HOME", tempDir)
require.NoError(t, err)
defer os.Unsetenv("XDG_CONFIG_HOME")

err = os.Mkdir(filepath.Join(tempDir, subDir), 0o755)
require.NoError(t, err)

result, err := FindConfigDir()
assert.NoError(t, err)
assert.Equal(t, filepath.Join(tempDir, subDir), result)
})

t.Run("HOME fallback", func(t *testing.T) {
err := os.Setenv("HOME", tempDir)
require.NoError(t, err)
defer os.Unsetenv("HOME")

result, err := FindConfigDir()
assert.NoError(t, err)
assert.Equal(t, filepath.Join(tempDir, homeSubDir), result)
})
}

func TestFindBinDir(t *testing.T) {
tempDir := t.TempDir()

err := os.Setenv("HOME", tempDir)
require.NoError(t, err)
defer os.Unsetenv("HOME")

result, err := FindBinDir()
assert.NoError(t, err)
assert.Equal(t, filepath.Join(tempDir, homeSubDir, "bin"), result)

var standardDir string
if runtime.GOOS == "windows" {
standardDir = filepath.Join("AppData", "Local", "Programs")
} else {
standardDir = filepath.Join(".local", "bin")
}
err = os.MkdirAll(filepath.Join(tempDir, standardDir), 0o755)
require.NoError(t, err)

result, err = FindBinDir()
assert.NoError(t, err)
assert.Equal(t, filepath.Join(tempDir, standardDir), result)
}

func TestFSHelpers(t *testing.T) {
tempDir := t.TempDir()

require.NoError(t, writeFile(filepath.Join(tempDir, "test.txt"), []byte("test"), 0, 0o644))
require.NoError(t, writeFile(filepath.Join(tempDir, "subdir", "test2.txt"), []byte("test2"), 0o755, 0o644))
require.NoError(t, os.Symlink(filepath.Join(tempDir, "subdir", "test2.txt"), filepath.Join(tempDir, "link")))

dirExists, err := isExistingDirectory(filepath.Join(tempDir, "subdir"))
assert.NoError(t, err)
assert.True(t, dirExists)

dirExists, err = isExistingDirectory(filepath.Join(tempDir, "not-a-subdir"))
assert.NoError(t, err)
assert.False(t, dirExists)

dirExists, err = isExistingDirectory(filepath.Join(tempDir, "test.txt"))
assert.NoError(t, err)
assert.False(t, dirExists)

cases := []struct {
path string
normalized string
shouldError bool
}{
{tempDir + "/foo/../test.txt", filepath.Join(tempDir, "test.txt"), false},
{tempDir + "/./foo//../test.txt", filepath.Join(tempDir, "test.txt"), false},
{tempDir + "/foo/bar/../../link", filepath.Join(tempDir, "subdir", "test2.txt"), false},
}

for i, c := range cases {
n, err := normalize(c.path)
if c.shouldError {
assert.Error(t, err, i)
} else {
assert.NoError(t, err, i)
assert.Equal(t, c.normalized, n, i)
}
}
}

0 comments on commit be85f5c

Please sign in to comment.