Skip to content

Commit

Permalink
fix(misconf): handle dot files better (#3550)
Browse files Browse the repository at this point in the history
  • Loading branch information
simar7 committed Feb 5, 2023
1 parent 200e04a commit 439c541
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
4 changes: 1 addition & 3 deletions pkg/misconf/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,7 @@ func createPolicyFS(policyPaths []string) (fs.FS, []string, error) {
}
var cleanPaths []string
for _, path := range policyPaths {
path = strings.TrimPrefix(path, ".")
path = strings.TrimPrefix(path, "/")
cleanPaths = append(cleanPaths, path)
cleanPaths = append(cleanPaths, filepath.Clean(path))
}
return os.DirFS(cwd), cleanPaths, nil
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/misconf/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,33 @@ func Test_FindingFSTarget(t *testing.T) {
})
}
}

func Test_createPolicyFS(t *testing.T) {
t.Run("inside cwd", func(t *testing.T) {
cwd, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.MkdirAll(filepath.Join(cwd, "testdir"), 0750))
require.NoError(t, os.MkdirAll(filepath.Join(cwd, ".testdir"), 0750))
defer func() {
os.RemoveAll(filepath.Join(cwd, "testdir"))
os.RemoveAll(filepath.Join(cwd, ".testdir"))
}()

_, got1, err := createPolicyFS([]string{"testdir"})
require.NoError(t, err)

_, got2, err := createPolicyFS([]string{".testdir"})
require.NoError(t, err)

assert.NotEqual(t, got1, got2, "testdir and .testdir are different dirs and should not be equal")
})

t.Run("outside cwd", func(t *testing.T) {
tmpDir := t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(tmpDir, "subdir/testdir"), 0750))
f, got, err := createPolicyFS([]string{filepath.Join(tmpDir, "subdir/testdir")})
require.NoError(t, err)
assert.Equal(t, []string{"."}, got)
assert.Contains(t, f, "testdir")
})
}

0 comments on commit 439c541

Please sign in to comment.