Skip to content

Commit

Permalink
fix: relative paths to dirs (#576)
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 authored Nov 14, 2022
1 parent 5c3cc38 commit b15966c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions files/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,15 @@ func TestGlobbingFilesWithDifferentSizesWithFileInfo(t *testing.T) {
t.Fatal("test FileInfos have the same size, expected different")
}
}

func TestDestEndsWithSlash(t *testing.T) {
result, err := files.ExpandContentGlobs(files.Contents{
{
Source: "./testdata/globtest/a.txt",
Destination: "./foo/",
},
}, false)
require.NoError(t, err)
require.Len(t, result, 1)
require.Equal(t, "foo/a.txt", result[0].Destination)
}
5 changes: 5 additions & 0 deletions internal/glob/glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ func Glob(pattern, dst string, ignoreMatchers bool) (map[string]string, error) {
continue
}

if strings.HasSuffix(dst, "/") {
files[src] = filepath.Join(dst, filepath.Base(src))
continue
}

relpath, err := filepath.Rel(prefix, src)
if err != nil {
// since prefix is a prefix of src a relative path should always be found
Expand Down
7 changes: 7 additions & 0 deletions internal/glob/glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ func TestGlob(t *testing.T) {
require.Equal(t, "/foo/bar/dir_c/test_c.txt", files["testdata/dir_a/dir_c/test_c.txt"])
})

t.Run("dst is a dir", func(t *testing.T) {
files, err := Glob("./testdata/dir_a/dir_b/test_b.txt", "/foo/bar/", false)
require.NoError(t, err)
require.Len(t, files, 1)
require.Equal(t, "/foo/bar/test_b.txt", files["testdata/dir_a/dir_b/test_b.txt"])
})

t.Run("to parent", func(t *testing.T) {
pattern := "../../testdata/fake"
abs, err := filepath.Abs(pattern)
Expand Down

0 comments on commit b15966c

Please sign in to comment.