Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(stapel/imports): processing of includePaths/excludePaths with glo…
…bs in file/directory name

includePaths/excludePaths were mishandled if their file or directory name contained a glob. E.g.: "directory/pathprefix-*".

Signed-off-by: Aleksei Igrychev <aleksei.igrychev@palark.com>
  • Loading branch information
alexey-igrychev committed Apr 25, 2023
1 parent 2f98135 commit 7046ca7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 13 deletions.
8 changes: 2 additions & 6 deletions pkg/build/stage/dependencies.go
Expand Up @@ -339,7 +339,7 @@ func generateChecksumCommand(from string, includePaths, excludePaths []string, r

var nameIncludeArgs []string
for _, includePath := range includePaths {
formattedPath := formatIncludeAndExcludePath(includePath)
formattedPath := util.SafeTrimGlobsAndSlashesFromPath(includePath)
nameIncludeArgs = append(
nameIncludeArgs,
fmt.Sprintf("-wholename \"%s\"", path.Join(from, formattedPath)),
Expand All @@ -353,7 +353,7 @@ func generateChecksumCommand(from string, includePaths, excludePaths []string, r

var nameExcludeArgs []string
for _, excludePath := range excludePaths {
formattedPath := formatIncludeAndExcludePath(excludePath)
formattedPath := util.SafeTrimGlobsAndSlashesFromPath(excludePath)
nameExcludeArgs = append(
nameExcludeArgs,
fmt.Sprintf("! -wholename \"%s\"", path.Join(from, formattedPath)),
Expand Down Expand Up @@ -388,10 +388,6 @@ func generateChecksumCommand(from string, includePaths, excludePaths []string, r
return command
}

func formatIncludeAndExcludePath(path string) string {
return strings.TrimRight(path, "*/")
}

func getDependencyImportID(dependencyImport *config.DependencyImport) string {
return util.Sha256Hash(
"Type", string(dependencyImport.Type),
Expand Down
10 changes: 3 additions & 7 deletions pkg/path_matcher/common.go
Expand Up @@ -46,7 +46,7 @@ func hasUniversalGlob(globs []string) bool {
return true
}

if trimRightAsterisks(glob) == "" {
if util.SafeTrimGlobsAndSlashesFromFilepath(glob) == "" {
return true
}
}
Expand All @@ -70,8 +70,8 @@ func isPathMatched(filePath, glob string) bool {
// The previous glob with the universal part `**/*` (path/*/dir/**/*).
for _, g := range []string{
glob,
trimRightAsterisks(glob),
filepath.Join(trimRightAsterisks(glob), "**", "*"),
util.SafeTrimGlobsAndSlashesFromFilepath(glob),
filepath.Join(util.SafeTrimGlobsAndSlashesFromFilepath(glob), "**", "*"),
} {
matched, err := doublestar.PathMatch(g, filePath)
if err != nil {
Expand All @@ -86,10 +86,6 @@ func isPathMatched(filePath, glob string) bool {
return false
}

func trimRightAsterisks(pattern string) string {
return strings.TrimRight(pattern, "*\\/")
}

func formatPaths(paths []string) []string {
var result []string
for _, path := range paths {
Expand Down
21 changes: 21 additions & 0 deletions pkg/util/path.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/user"
"path"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -151,3 +152,23 @@ func FilepathsWithParents(path string) []string {

return res
}

// SafeTrimGlobsAndSlashesFromFilepath trims any trailing globs and/or slashes from the path,
// while ignoring globs that are part of a directory or file name.
func SafeTrimGlobsAndSlashesFromFilepath(p string) string {
return filepath.FromSlash(SafeTrimGlobsAndSlashesFromPath(p))
}

func SafeTrimGlobsAndSlashesFromPath(p string) string {
parts := SplitFilepath(p)
for i := len(parts) - 1; i >= 0; i-- {
if partWOGlobs := strings.TrimRight(parts[i], "*"); partWOGlobs != "" {
parts = parts[:i+1]
break
} else {
parts = parts[:i]
}
}

return path.Join(parts...)
}
57 changes: 57 additions & 0 deletions pkg/util/path_test.go
@@ -0,0 +1,57 @@
package util

import (
"testing"
)

func TestSafeTrimGlobsAndSlashes(t *testing.T) {
tests := []struct {
name string
path string
want string
}{
{
name: "globs",
path: "**/*",
want: "",
},
{
name: "empty path",
path: "",
want: "",
},
{
name: "path",
path: "path",
want: "path",
},
{
name: "path with trailing slash",
path: "path/",
want: "path",
},
{
name: "path with globs and slashes",
path: "path/**/*",
want: "path",
},
{
name: "path with glob as part of a directory or file name 1",
path: "path/name-*",
want: "path/name-*",
},
{
name: "path with glob as part of a directory or file name 2",
path: "path/*.tmp",
want: "path/*.tmp",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := SafeTrimGlobsAndSlashesFromPath(tt.path); got != tt.want {
t.Errorf("SafeTrimGlobsAndSlashesFromPath() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 7046ca7

Please sign in to comment.