Skip to content

Commit 7ac474a

Browse files
author
Romuald Atchadé
committed
Upgrade github.com/bmatcuk/doublestar/v4 to latest
1 parent e84bcf0 commit 7ac474a

File tree

5 files changed

+157
-4
lines changed

5 files changed

+157
-4
lines changed

commands/helpers/file_archiver.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ func (c *fileArchiver) processPath(path string) {
156156
return
157157
}
158158

159-
matches, err := doublestar.FilepathGlob(rel)
159+
// Use WithNoFollow option to prevent symlink cycles during the initial glob
160+
matches, err := doublestar.FilepathGlob(rel, doublestar.WithNoFollow())
160161
if err != nil {
161162
logrus.Warningf("%s: %v", path, err)
162163
return
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
//go:build integration
2+
3+
package helpers_test
4+
5+
import (
6+
"fmt"
7+
"maps"
8+
"os"
9+
"path/filepath"
10+
"slices"
11+
"strings"
12+
"testing"
13+
"time"
14+
15+
"github.com/stretchr/testify/require"
16+
"github.com/urfave/cli"
17+
18+
"gitlab.com/gitlab-org/gitlab-runner/commands/helpers"
19+
)
20+
21+
func newFileArchiveInitTestApp(file string, paths []string) (*cli.App, *helpers.CacheArchiverCommand) {
22+
cmd := helpers.NewCacheArchiverCommandForTest(file, paths)
23+
app := cli.NewApp()
24+
app.Name = filepath.Base(os.Args[0])
25+
app.Commands = append(app.Commands, cli.Command{
26+
Name: "cache-archiver",
27+
Action: cmd.Execute,
28+
})
29+
30+
return app, &cmd
31+
}
32+
33+
func TestFileArchiver(t *testing.T) {
34+
var err error
35+
36+
// Create a temporary directory to hold our project
37+
parentDir, err := os.Getwd()
38+
require.NoError(t, err, "Error retrieving working directory")
39+
40+
dir := filepath.Join(
41+
parentDir,
42+
fmt.Sprintf("test-%s-%s", t.Name(), time.Now().Format("20060102-150405.000")),
43+
)
44+
err = os.MkdirAll(dir, 0755)
45+
require.NoError(t, err, "Error creating directory")
46+
47+
archive := fmt.Sprintf("%s.%s", dir, "zip")
48+
paths := []string{"**/project"}
49+
50+
t.Cleanup(func() {
51+
t.Logf("Removing temporary directory: %s", dir)
52+
os.RemoveAll(dir)
53+
t.Logf("Removing archive: %s", archive)
54+
os.RemoveAll(archive)
55+
})
56+
57+
files := setupEnvironment(t, fmt.Sprintf("%s", parentDir), dir)
58+
59+
// Start a new cli with the arguments for the command.
60+
args := []string{os.Args[0], "cache-archiver"}
61+
62+
app, cmd := newFileArchiveInitTestApp(archive, paths)
63+
err = app.Run(args)
64+
65+
matches := helpers.GetMatches(cmd)
66+
require.ElementsMatch(t, files, slices.Collect(maps.Keys(matches)), "Elements in archive don't match with expected")
67+
}
68+
69+
func setupEnvironment(t *testing.T, parentDir, dir string) []string {
70+
t.Helper()
71+
72+
t.Logf("Creating project structure in: %s", dir)
73+
74+
// Define project root path
75+
projectRoot := filepath.Join(dir, "project")
76+
77+
dirs := []string{
78+
projectRoot,
79+
filepath.Join(projectRoot, "folder1"),
80+
filepath.Join(projectRoot, "folder1", "subfolder"),
81+
filepath.Join(projectRoot, "folder2"),
82+
filepath.Join(projectRoot, "folder3"),
83+
filepath.Join(projectRoot, "selfreferential"),
84+
}
85+
86+
for _, d := range dirs {
87+
err := os.MkdirAll(d, 0755)
88+
require.NoError(t, err, "Error creating directory")
89+
}
90+
91+
files := []string{
92+
filepath.Join(projectRoot, "folder1", "file1.txt"),
93+
filepath.Join(projectRoot, "folder1", "subfolder", "data.csv"),
94+
filepath.Join(projectRoot, "folder2", "file2.txt"),
95+
filepath.Join(projectRoot, "folder2", "report.csv"),
96+
filepath.Join(projectRoot, "folder3", "file3.csv"),
97+
}
98+
99+
for _, f := range files {
100+
createFile(t, f)
101+
}
102+
103+
symlinks := []struct{ target, link string }{
104+
{"../folder2", filepath.Join(projectRoot, "folder1", "loop")},
105+
{"../folder1/subfolder", filepath.Join(projectRoot, "folder2", "subfolder")},
106+
{"../../folder1", filepath.Join(projectRoot, "folder1", "subfolder", "back")},
107+
{"../folder3", filepath.Join(projectRoot, "folder2", "another")},
108+
{"../folder1", filepath.Join(projectRoot, "folder3", "link_to_folder1")},
109+
{".", filepath.Join(projectRoot, "selfreferential", "myself")},
110+
}
111+
112+
for _, s := range symlinks {
113+
err := os.Symlink(s.target, s.link)
114+
require.NoError(t, err, "Error creating symlink")
115+
}
116+
117+
var createdPaths []string
118+
allPaths := append(dirs, files...)
119+
for _, s := range symlinks {
120+
allPaths = append(allPaths, s.link)
121+
}
122+
123+
for _, path := range allPaths {
124+
relPath := trimPrefixes(path, parentDir, "/", "\\")
125+
createdPaths = append(createdPaths, strings.ReplaceAll(relPath, "\\", "/"))
126+
}
127+
128+
return createdPaths
129+
}
130+
131+
func trimPrefixes(s string, prefixes ...string) string {
132+
for _, prefix := range prefixes {
133+
if strings.HasPrefix(s, prefix) {
134+
s = strings.TrimPrefix(s, prefix)
135+
}
136+
}
137+
138+
return s
139+
}
140+
141+
func createFile(t *testing.T, path string) {
142+
t.Helper()
143+
144+
file, err := os.Create(path)
145+
require.NoError(t, err, "creating file %q", path)
146+
require.NoError(t, file.Close(), "closing file %q", path)
147+
}

commands/helpers/helpers_cache_archiver_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package helpers
44

55
import (
6+
"os"
67
"time"
78

89
"gocloud.dev/blob"
@@ -16,6 +17,10 @@ func NewCacheArchiverCommandForTest(file string, fileArchiverPaths []string) Cac
1617
}
1718
}
1819

20+
func GetMatches(cmd *CacheArchiverCommand) map[string]os.FileInfo {
21+
return cmd.files
22+
}
23+
1924
// SetCacheArchiverCommandMux allows integration tests to set mux
2025
func SetCacheArchiverCommandMux(cmd *CacheArchiverCommand, mux *blob.URLMux) {
2126
cmd.mux = mux

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/aws/aws-sdk-go-v2/credentials v1.17.30
2222
github.com/aws/aws-sdk-go-v2/service/s3 v1.61.0
2323
github.com/aws/aws-sdk-go-v2/service/sts v1.30.5
24-
github.com/bmatcuk/doublestar/v4 v4.4.0
24+
github.com/bmatcuk/doublestar/v4 v4.8.1
2525
github.com/creack/pty v1.1.21
2626
github.com/denisbrodbeck/machineid v1.0.1
2727
github.com/distribution/reference v0.6.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPn
150150
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
151151
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
152152
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
153-
github.com/bmatcuk/doublestar/v4 v4.4.0 h1:LmAwNwhjEbYtyVLzjcP/XeVw4nhuScHGkF/XWXnvIic=
154-
github.com/bmatcuk/doublestar/v4 v4.4.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
153+
github.com/bmatcuk/doublestar/v4 v4.8.1 h1:54Bopc5c2cAvhLRAzqOGCYHYyhcDHsFF4wWIR5wKP38=
154+
github.com/bmatcuk/doublestar/v4 v4.8.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
155155
github.com/bodgit/ntlmssp v0.0.0-20240506230425-31973bb52d9b h1:baFN6AnR0SeC194X2D292IUZcHDs4JjStpqtE70fjXE=
156156
github.com/bodgit/ntlmssp v0.0.0-20240506230425-31973bb52d9b/go.mod h1:Ram6ngyPDmP+0t6+4T2rymv0w0BS9N8Ch5vvUJccw5o=
157157
github.com/bodgit/windows v1.0.1 h1:tF7K6KOluPYygXa3Z2594zxlkbKPAOvqr97etrGNIz4=

0 commit comments

Comments
 (0)