Debian Codegraph @

osfs/os_chroot_test.go

raw symbols SCIP
1 //go:build !wasm
2 // +build !wasm
3
4 package osfs
5
6 import (
7 "io/ioutil"
8 "os"
9 "path/filepath"
10 "runtime"
11 "testing"
12
13 "github.com/go-git/go-billy/v5"
14 "github.com/go-git/go-billy/v5/test"
15
16 . "gopkg.in/check.v1"
17 )
18
19 func Test(t *testing.T) { TestingT(t) }
20
21 type ChrootOSSuite struct {
22 test.FilesystemSuite
23 path string
24 }
25
26 var _ = Suite(&ChrootOSSuite{})
27
28 func (s *ChrootOSSuite) SetUpTest(c *C) {
29 s.path, _ = ioutil.TempDir(os.TempDir(), "go-billy-osfs-test")
30 if runtime.GOOS == "plan9" {
31 // On Plan 9, permission mode of newly created files
32 // or directories are based on the permission mode of
33 // the containing directory (see http://man.cat-v.org/plan_9/5/open).
34 // Since TestOpenFileWithModes and TestStat creates files directly
35 // in the temporary directory, we need to make it more permissive.
36 c.Assert(os.Chmod(s.path, 0777), IsNil)
37 }
38 s.FilesystemSuite = test.NewFilesystemSuite(newChrootOS(s.path))
39 }
40
41 func (s *ChrootOSSuite) TearDownTest(c *C) {
42 err := os.RemoveAll(s.path)
43 c.Assert(err, IsNil)
44 }
45
46 func (s *ChrootOSSuite) TestOpenDoesNotCreateDir(c *C) {
47 _, err := s.FS.Open("dir/non-existent")
48 c.Assert(err, NotNil)
49
50 _, err = os.Stat(filepath.Join(s.path, "dir"))
51 c.Assert(os.IsNotExist(err), Equals, true)
52 }
53
54 func (s *ChrootOSSuite) TestEmptyBaseChrootPreservesAbsolutePath(c *C) {
55 dir := c.MkDir()
56 c.Assert(os.Mkdir(filepath.Join(dir, ".git"), 0755), IsNil)
57
58 fs, err := newChrootOS("").Chroot(dir)
59 c.Assert(err, IsNil)
60 c.Assert(fs.Root(), Equals, dir)
61
62 fi, err := fs.Stat(".git")
63 c.Assert(err, IsNil)
64 c.Assert(fi.IsDir(), Equals, true)
65 }
66
67 func (s *ChrootOSSuite) TestSymlinkedRoot(c *C) {
68 root := c.MkDir()
69 realRoot := filepath.Join(root, "real")
70 linkRoot := filepath.Join(root, "link")
71
72 c.Assert(os.Mkdir(realRoot, 0755), IsNil)
73 if err := os.Symlink(realRoot, linkRoot); err != nil {
74 c.Skip("symlink creation is not supported")
75 }
76
77 fs := newChrootOS(linkRoot)
78
79 fi, err := fs.Stat("/")
80 c.Assert(err, IsNil)
81 c.Assert(fi.IsDir(), Equals, true)
82
83 _, err = fs.Stat(".git")
84 c.Assert(os.IsNotExist(err), Equals, true)
85 c.Assert(err, Not(Equals), billy.ErrCrossedBoundary)
86 }
87
88 func (s *ChrootOSSuite) TestCapabilities(c *C) {
89 _, ok := s.FS.(billy.Capable)
90 c.Assert(ok, Equals, true)
91
92 caps := billy.Capabilities(s.FS)
93 c.Assert(caps, Equals, billy.AllCapabilities)
94 }