Skip to content

Commit 8662784

Browse files
authored
Merge pull request #187 from pjbgf/windows-rename
v5: Ensure Chmod behaviour across BoundOS and ChrootOS
2 parents 247a741 + f387d62 commit 8662784

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ jobs:
55
test:
66
strategy:
77
matrix:
8-
go-version: [1.21.x,1.22.x,1.23.x]
8+
go-version: [1.24.x, oldstable, stable]
99
platform: [ubuntu-latest, macos-latest, windows-latest]
1010
runs-on: ${{ matrix.platform }}
1111
steps:

helper/polyfill/polyfill.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type Polyfill struct {
1313
c capabilities
1414
}
1515

16-
type capabilities struct{ tempfile, dir, symlink, chroot bool }
16+
type capabilities struct{ tempfile, dir, symlink, chroot, chmod bool }
1717

1818
// New creates a new filesystem wrapping up 'fs' the intercepts all the calls
1919
// made and errors if fs doesn't implement any of the billy interfaces.
@@ -28,6 +28,7 @@ func New(fs billy.Basic) billy.Filesystem {
2828
_, h.c.dir = h.Basic.(billy.Dir)
2929
_, h.c.symlink = h.Basic.(billy.Symlink)
3030
_, h.c.chroot = h.Basic.(billy.Chroot)
31+
_, h.c.chmod = h.Basic.(billy.Chmod)
3132
return h
3233
}
3334

@@ -87,6 +88,14 @@ func (h *Polyfill) Chroot(path string) (billy.Filesystem, error) {
8788
return h.Basic.(billy.Chroot).Chroot(path)
8889
}
8990

91+
func (h *Polyfill) Chmod(path string, mode os.FileMode) error {
92+
if !h.c.chmod {
93+
return billy.ErrNotSupported
94+
}
95+
96+
return h.Basic.(billy.Chmod).Chmod(path, mode)
97+
}
98+
9099
func (h *Polyfill) Root() string {
91100
if !h.c.chroot {
92101
return string(filepath.Separator)

helper/polyfill/polyfill_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ func (s *PolyfillSuite) TestChroot(c *C) {
5858
c.Assert(err, Equals, billy.ErrNotSupported)
5959
}
6060

61+
func (s *PolyfillSuite) TestChmod(c *C) {
62+
ch, ok := s.Helper.(billy.Chmod)
63+
c.Assert(ok, Equals, true)
64+
65+
err := ch.Chmod("", 0o444)
66+
c.Assert(err, Equals, billy.ErrNotSupported)
67+
}
68+
6169
func (s *PolyfillSuite) TestRoot(c *C) {
6270
c.Assert(s.Helper.Root(), Equals, string(filepath.Separator))
6371
}

osfs/os_bound.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ func (fs *BoundOS) TempFile(dir, prefix string) (billy.File, error) {
126126
if err != nil {
127127
return nil, err
128128
}
129+
130+
_, err = os.Stat(dir)
131+
if err != nil && os.IsNotExist(err) {
132+
err = os.MkdirAll(dir, defaultDirectoryMode)
133+
if err != nil {
134+
return nil, err
135+
}
136+
}
129137
}
130138

131139
return tempFile(dir, prefix)

osfs/os_bound_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,10 @@ func TestTempFile(t *testing.T) {
247247
g.Expect(f.Close()).ToNot(gomega.HaveOccurred())
248248

249249
f, err = fs.TempFile("/above/cwd", "prefix")
250-
g.Expect(err).To(gomega.HaveOccurred())
251-
g.Expect(err.Error()).To(gomega.ContainSubstring(fmt.Sprint(dir, filepath.FromSlash("/above/cwd/prefix"))))
252-
g.Expect(f).To(gomega.BeNil())
250+
g.Expect(err).ToNot(gomega.HaveOccurred())
251+
g.Expect(f).ToNot(gomega.BeNil())
252+
g.Expect(f.Name()).To(gomega.HavePrefix(filepath.Join(dir, "/above/cwd", "prefix")))
253+
g.Expect(f.Close()).ToNot(gomega.HaveOccurred())
253254

254255
tempDir := os.TempDir()
255256
// For windows, volume name must be removed.
@@ -258,9 +259,10 @@ func TestTempFile(t *testing.T) {
258259
}
259260

260261
f, err = fs.TempFile(tempDir, "prefix")
261-
g.Expect(err).To(gomega.HaveOccurred())
262-
g.Expect(err.Error()).To(gomega.ContainSubstring(filepath.Join(dir, tempDir, "prefix")))
263-
g.Expect(f).To(gomega.BeNil())
262+
g.Expect(err).ToNot(gomega.HaveOccurred())
263+
g.Expect(f).ToNot(gomega.BeNil())
264+
g.Expect(f.Name()).To(gomega.HavePrefix(filepath.Join(dir, tempDir, "prefix")))
265+
g.Expect(f.Close()).ToNot(gomega.HaveOccurred())
264266
}
265267

266268
func TestChroot(t *testing.T) {
@@ -1105,10 +1107,10 @@ func TestReadDir(t *testing.T) {
11051107
g.Expect(dirs).To(gomega.BeNil())
11061108
}
11071109

1108-
func TestInsideBaseDirEval(t*testing.T) {
1110+
func TestInsideBaseDirEval(t *testing.T) {
11091111
g := gomega.NewWithT(t)
11101112
fs := BoundOS{baseDir: "/"}
1111-
b, err := fs.insideBaseDirEval("a")
1113+
b, err := fs.insideBaseDirEval("a")
11121114
g.Expect(b).To(gomega.BeTrue())
11131115
g.Expect(err).To(gomega.BeNil())
11141116
}

0 commit comments

Comments
 (0)