Skip to content

Commit 7e09a9f

Browse files
authored
fix: fixed skipFiles/skipDirs flags for relative path (fanal#342)
1 parent f733307 commit 7e09a9f

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

artifact/local/fs.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func NewArtifact(dir string, c cache.ArtifactCache, artifactOpt artifact.Option,
5454
return Artifact{
5555
dir: dir,
5656
cache: c,
57-
walker: walker.NewDir(artifactOpt.SkipFiles, artifactOpt.SkipDirs),
57+
walker: walker.NewDir(buildAbsPaths(dir, artifactOpt.SkipFiles), buildAbsPaths(dir, artifactOpt.SkipDirs)),
5858
analyzer: analyzer.NewAnalyzer(artifactOpt.DisabledAnalyzers),
5959
hookManager: hook.NewManager(artifactOpt.DisabledHooks),
6060
scanner: s,
@@ -64,6 +64,18 @@ func NewArtifact(dir string, c cache.ArtifactCache, artifactOpt artifact.Option,
6464
}, nil
6565
}
6666

67+
func buildAbsPaths(base string, paths []string) []string {
68+
absPaths := []string{}
69+
for _, path := range paths {
70+
if filepath.IsAbs(path) {
71+
absPaths = append(absPaths, path)
72+
} else {
73+
absPaths = append(absPaths, filepath.Join(base, path))
74+
}
75+
}
76+
return absPaths
77+
}
78+
6779
func (a Artifact) Inspect(ctx context.Context) (types.ArtifactReference, error) {
6880
var wg sync.WaitGroup
6981
result := new(analyzer.AnalysisResult)

artifact/local/fs_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,32 @@ func TestArtifact_Inspect(t *testing.T) {
155155
})
156156
}
157157
}
158+
159+
func TestBuildAbsPath(t *testing.T) {
160+
tests := []struct {
161+
name string
162+
base string
163+
paths []string
164+
expectedPaths []string
165+
}{
166+
{"absolute path", "/testBase", []string{"/testPath"}, []string{"/testPath"}},
167+
{"relative path", "/testBase", []string{"testPath"}, []string{"/testBase/testPath"}},
168+
{"path have '.'", "/testBase", []string{"./testPath"}, []string{"/testBase/testPath"}},
169+
{"path have '..'", "/testBase", []string{"../testPath/"}, []string{"/testPath"}},
170+
}
171+
172+
for _, test := range tests {
173+
t.Run(test.name, func(t *testing.T) {
174+
got := buildAbsPaths(test.base, test.paths)
175+
if len(test.paths) != len(got) {
176+
t.Errorf("paths not equals, expected: %s, got: %s", test.expectedPaths, got)
177+
} else {
178+
for i, path := range test.expectedPaths {
179+
if path != got[i] {
180+
t.Errorf("paths not equals, expected: %s, got: %s", test.expectedPaths, got)
181+
}
182+
}
183+
}
184+
})
185+
}
186+
}

0 commit comments

Comments
 (0)