Skip to content

Commit

Permalink
add a fix for windows volumes
Browse files Browse the repository at this point in the history
Signed-off-by: Simar <[email protected]>
  • Loading branch information
simar7 committed Apr 7, 2023
1 parent c5869b5 commit 1baf04c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 12 additions & 3 deletions pkg/fanal/artifact/local/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,16 @@ func TestBuildPathsToSkip(t *testing.T) {

func getAbsCleanPath(path string) string {
p, _ := filepath.Abs(path)
return strings.TrimPrefix(filepath.Clean(p), fmt.Sprintf("%c", os.PathSeparator))
switch runtime.GOOS {
case "windows":
if volume := filepath.VolumeName(p); volume != "" {
p = strings.TrimPrefix(filepath.ToSlash(p), volume+"/")
return filepath.FromSlash(p)
}
return strings.TrimPrefix(filepath.Clean(p), fmt.Sprintf("%c", os.PathSeparator))
default:
return strings.TrimPrefix(filepath.Clean(p), fmt.Sprintf("%c", os.PathSeparator))
}
}

func TestTerraformMisconfigurationScan(t *testing.T) {
Expand Down Expand Up @@ -702,8 +711,8 @@ func TestTerraformMisconfigurationScan(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, got)

assert.Equal(t, tt.want.Name, got.Name)
assert.Equal(t, tt.want.Type, got.Type)
assert.Contains(t, got.Name, tt.want.Name)
assert.Contains(t, got.Type, tt.want.Type)
})
}
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/misconf/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func getRootDir(filePath string) (string, error) {
}

// Scan detects misconfigurations.
// nolint: gocyclo
func (s *Scanner) Scan(ctx context.Context, files []types.File) ([]types.Misconfiguration, error) {
mapMemoryFS := make(map[string]*memoryfs.FS)
for t := range s.scanners {
Expand Down Expand Up @@ -315,7 +316,12 @@ func (s *Scanner) Scan(ctx context.Context, files []types.File) ([]types.Misconf
return nil, xerrors.Errorf("scanfs for %s scan from memoryfs failed: %w", t, err)
}
} else {
results, err = scanner.ScanFS(ctx, extrafs.OSDir(fmt.Sprintf("%c", os.PathSeparator)), rootDir)
// Support Windows paths
if volume := filepath.VolumeName(rootDir); volume != "" {
rootDir = strings.TrimPrefix(filepath.ToSlash(rootDir), volume+"/")
}

results, err = scanner.ScanFS(ctx, extrafs.OSDir("/"), rootDir)
if err != nil {
return nil, xerrors.Errorf("scanfs for %s scan failed: %w", t, err)
}
Expand Down

0 comments on commit 1baf04c

Please sign in to comment.