mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Go: Better check for path prefixes
This commit is contained in:
@@ -481,7 +481,7 @@ func getBuildRoots(emitDiagnostics bool) (goWorkspaces []GoWorkspace, totalModul
|
||||
// Determines whether `str` starts with any of `prefixes`.
|
||||
func startsWithAnyOf(str string, prefixes []string) bool {
|
||||
for _, prefix := range prefixes {
|
||||
if strings.HasPrefix(str, prefix) {
|
||||
if relPath, err := filepath.Rel(str, prefix); err == nil && !strings.HasPrefix(relPath, "..") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
27
go/extractor/project/project_test.go
Normal file
27
go/extractor/project/project_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package project
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testStartsWithAnyOf(t *testing.T, path string, prefix string, expectation bool) {
|
||||
result := startsWithAnyOf(path, []string{prefix})
|
||||
if result != expectation {
|
||||
t.Errorf("Expected startsWithAnyOf(%s, %s) to be %t, but it is %t.", path, prefix, expectation, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartsWithAnyOf(t *testing.T) {
|
||||
testStartsWithAnyOf(t, ".", ".", true)
|
||||
testStartsWithAnyOf(t, ".", "dir", true)
|
||||
testStartsWithAnyOf(t, ".", filepath.Join("foo", "bar"), true)
|
||||
testStartsWithAnyOf(t, "dir", "dir", true)
|
||||
testStartsWithAnyOf(t, "foo", filepath.Join("foo", "bar"), true)
|
||||
testStartsWithAnyOf(t, filepath.Join("foo", "bar"), filepath.Join("foo", "bar"), true)
|
||||
testStartsWithAnyOf(t, filepath.Join("foo", "bar"), filepath.Join("foo", "bar", "baz"), true)
|
||||
|
||||
testStartsWithAnyOf(t, filepath.Join("foo", "bar"), "foo", false)
|
||||
testStartsWithAnyOf(t, filepath.Join("foo", "bar"), "bar", false)
|
||||
testStartsWithAnyOf(t, filepath.Join("foo", "bar"), filepath.Join("foo", "baz"), false)
|
||||
}
|
||||
Reference in New Issue
Block a user