diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2021-05-02 23:40:08 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2021-05-02 23:40:08 +0200 |
commit | 01df7536992af375a54bbedf45369a475953e372 (patch) | |
tree | 8435050f4388f15b3a85d135a3f7d11b1ff61540 /storage/filesystem/dotgit | |
parent | 67af9d7223b0cc643025d958c592291f7475ac75 (diff) | |
download | go-git-01df7536992af375a54bbedf45369a475953e372.tar.gz |
*: use go-billy instead of os calls
Diffstat (limited to 'storage/filesystem/dotgit')
-rw-r--r-- | storage/filesystem/dotgit/dotgit_test.go | 117 | ||||
-rw-r--r-- | storage/filesystem/dotgit/repository_filesystem_test.go | 15 | ||||
-rw-r--r-- | storage/filesystem/dotgit/writers_test.go | 37 |
3 files changed, 68 insertions, 101 deletions
diff --git a/storage/filesystem/dotgit/dotgit_test.go b/storage/filesystem/dotgit/dotgit_test.go index 237605f..4c2ae94 100644 --- a/storage/filesystem/dotgit/dotgit_test.go +++ b/storage/filesystem/dotgit/dotgit_test.go @@ -11,10 +11,10 @@ import ( "testing" "github.com/go-git/go-billy/v5" - "github.com/go-git/go-git/v5/plumbing" - "github.com/go-git/go-billy/v5/osfs" + "github.com/go-git/go-billy/v5/util" fixtures "github.com/go-git/go-git-fixtures/v4" + "github.com/go-git/go-git/v5/plumbing" . "gopkg.in/check.v1" ) @@ -26,15 +26,30 @@ type SuiteDotGit struct { var _ = Suite(&SuiteDotGit{}) +func (s *SuiteDotGit) TemporalFilesystem() (fs billy.Filesystem, clean func()) { + fs = osfs.New(os.TempDir()) + path, err := util.TempDir(fs, "", "") + if err != nil { + panic(err) + } + + fs, err = fs.Chroot(path) + if err != nil { + panic(err) + } + + return fs, func() { + util.RemoveAll(fs, path) + } +} + func (s *SuiteDotGit) TestInitialize(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(fs) - err = dir.Initialize() + err := dir.Initialize() c.Assert(err, IsNil) _, err = fs.Stat(fs.Join("objects", "info")) @@ -51,22 +66,18 @@ func (s *SuiteDotGit) TestInitialize(c *C) { } func (s *SuiteDotGit) TestSetRefs(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(fs) testSetRefs(c, dir) } func (s *SuiteDotGit) TestSetRefsNorwfs(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(&norwfs{fs}) testSetRefs(c, dir) @@ -220,7 +231,7 @@ func (s *SuiteDotGit) TestRemoveRefFromPackedRefs(c *C) { err := dir.RemoveRef(name) c.Assert(err, IsNil) - b, err := ioutil.ReadFile(filepath.Join(fs.Root(), packedRefsPath)) + b, err := util.ReadFile(fs, packedRefsPath) c.Assert(err, IsNil) c.Assert(string(b), Equals, ""+ @@ -255,7 +266,7 @@ func (s *SuiteDotGit) TestRemoveRefFromReferenceFileAndPackedRefs(c *C) { err = dir.RemoveRef(name) c.Assert(err, IsNil) - b, err := ioutil.ReadFile(filepath.Join(fs.Root(), packedRefsPath)) + b, err := util.ReadFile(fs, packedRefsPath) c.Assert(err, IsNil) c.Assert(string(b), Equals, ""+ @@ -274,15 +285,14 @@ func (s *SuiteDotGit) TestRemoveRefNonExistent(c *C) { fs := fixtures.Basic().ByTag(".git").One().DotGit() dir := New(fs) - packedRefs := filepath.Join(fs.Root(), packedRefsPath) - before, err := ioutil.ReadFile(packedRefs) + before, err := util.ReadFile(fs, packedRefsPath) c.Assert(err, IsNil) name := plumbing.ReferenceName("refs/heads/nonexistent") err = dir.RemoveRef(name) c.Assert(err, IsNil) - after, err := ioutil.ReadFile(packedRefs) + after, err := util.ReadFile(fs, packedRefsPath) c.Assert(err, IsNil) c.Assert(string(before), Equals, string(after)) @@ -292,17 +302,16 @@ func (s *SuiteDotGit) TestRemoveRefInvalidPackedRefs(c *C) { fs := fixtures.Basic().ByTag(".git").One().DotGit() dir := New(fs) - packedRefs := filepath.Join(fs.Root(), packedRefsPath) brokenContent := "BROKEN STUFF REALLY BROKEN" - err := ioutil.WriteFile(packedRefs, []byte(brokenContent), os.FileMode(0755)) + err := util.WriteFile(fs, packedRefsPath, []byte(brokenContent), os.FileMode(0755)) c.Assert(err, IsNil) name := plumbing.ReferenceName("refs/heads/nonexistent") err = dir.RemoveRef(name) c.Assert(err, NotNil) - after, err := ioutil.ReadFile(filepath.Join(fs.Root(), packedRefsPath)) + after, err := util.ReadFile(fs, packedRefsPath) c.Assert(err, IsNil) c.Assert(brokenContent, Equals, string(after)) @@ -312,17 +321,16 @@ func (s *SuiteDotGit) TestRemoveRefInvalidPackedRefs2(c *C) { fs := fixtures.Basic().ByTag(".git").One().DotGit() dir := New(fs) - packedRefs := filepath.Join(fs.Root(), packedRefsPath) brokenContent := strings.Repeat("a", bufio.MaxScanTokenSize*2) - err := ioutil.WriteFile(packedRefs, []byte(brokenContent), os.FileMode(0755)) + err := util.WriteFile(fs, packedRefsPath, []byte(brokenContent), os.FileMode(0755)) c.Assert(err, IsNil) name := plumbing.ReferenceName("refs/heads/nonexistent") err = dir.RemoveRef(name) c.Assert(err, NotNil) - after, err := ioutil.ReadFile(filepath.Join(fs.Root(), packedRefsPath)) + after, err := util.ReadFile(fs, packedRefsPath) c.Assert(err, IsNil) c.Assert(brokenContent, Equals, string(after)) @@ -351,11 +359,9 @@ func (s *SuiteDotGit) TestConfig(c *C) { } func (s *SuiteDotGit) TestConfigWriteAndConfig(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(fs) f, err := dir.ConfigWriter() @@ -383,11 +389,9 @@ func (s *SuiteDotGit) TestIndex(c *C) { } func (s *SuiteDotGit) TestIndexWriteAndIndex(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(fs) f, err := dir.IndexWriter() @@ -415,11 +419,9 @@ func (s *SuiteDotGit) TestShallow(c *C) { } func (s *SuiteDotGit) TestShallowWriteAndShallow(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(fs) f, err := dir.ShallowWriter() @@ -562,11 +564,9 @@ func (s *SuiteDotGit) TestObjectPackNotFound(c *C) { } func (s *SuiteDotGit) TestNewObject(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(fs) w, err := dir.NewObject() c.Assert(err, IsNil) @@ -627,11 +627,9 @@ func testObjectsWithPrefix(c *C, fs billy.Filesystem, dir *DotGit) { } func (s *SuiteDotGit) TestObjectsNoFolder(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(fs) hash, err := dir.Objects() c.Assert(err, IsNil) @@ -722,14 +720,12 @@ func (s *SuiteDotGit) TestSubmodules(c *C) { } func (s *SuiteDotGit) TestPackRefs(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() - fs := osfs.New(tmp) dir := New(fs) - err = dir.SetRef(plumbing.NewReferenceFromStrings( + err := dir.SetRef(plumbing.NewReferenceFromStrings( "refs/heads/foo", "e8d3ffab552895c19b9fcf7aa264d277cde33881", ), nil) @@ -794,20 +790,16 @@ func (s *SuiteDotGit) TestPackRefs(c *C) { } func (s *SuiteDotGit) TestAlternates(c *C) { - tmp, err := ioutil.TempDir("", "dot-git") - c.Assert(err, IsNil) - defer os.RemoveAll(tmp) - - // Create a new billy fs. - fs := osfs.New(tmp) + fs, clean := s.TemporalFilesystem() + defer clean() // Create a new dotgit object and initialize. dir := New(fs) - err = dir.Initialize() + err := dir.Initialize() c.Assert(err, IsNil) // Create alternates file. - altpath := filepath.Join("objects", "info", "alternates") + altpath := fs.Join("objects", "info", "alternates") f, err := fs.Create(altpath) c.Assert(err, IsNil) @@ -832,16 +824,17 @@ func (s *SuiteDotGit) TestAlternates(c *C) { // For relative path: // /some/absolute/path/to/dot-git -> /some/absolute/path - pathx := strings.Split(tmp, string(filepath.Separator)) + pathx := strings.Split(fs.Root(), string(filepath.Separator)) pathx = pathx[:len(pathx)-2] // Use string.Join() to avoid malformed absolutepath on windows // C:Users\\User\\... instead of C:\\Users\\appveyor\\... . resolvedPath := strings.Join(pathx, string(filepath.Separator)) // Append the alternate path to the resolvedPath - expectedPath := filepath.Join(string(filepath.Separator), resolvedPath, "rep2", ".git") + expectedPath := fs.Join(string(filepath.Separator), resolvedPath, "rep2", ".git") if runtime.GOOS == "windows" { - expectedPath = filepath.Join(resolvedPath, "rep2", ".git") + expectedPath = fs.Join(resolvedPath, "rep2", ".git") } + c.Assert(dotgits[1].fs.Root(), Equals, expectedPath) } diff --git a/storage/filesystem/dotgit/repository_filesystem_test.go b/storage/filesystem/dotgit/repository_filesystem_test.go index 880ec0d..022bde7 100644 --- a/storage/filesystem/dotgit/repository_filesystem_test.go +++ b/storage/filesystem/dotgit/repository_filesystem_test.go @@ -1,25 +1,16 @@ package dotgit import ( - "io/ioutil" - "log" "os" - "github.com/go-git/go-billy/v5/osfs" - . "gopkg.in/check.v1" ) func (s *SuiteDotGit) TestRepositoryFilesystem(c *C) { - dir, err := ioutil.TempDir("", "repository_filesystem") - if err != nil { - log.Fatal(err) - } - defer os.RemoveAll(dir) - - fs := osfs.New(dir) + fs, clean := s.TemporalFilesystem() + defer clean() - err = fs.MkdirAll("dotGit", 0777) + err := fs.MkdirAll("dotGit", 0777) c.Assert(err, IsNil) dotGitFs, err := fs.Chroot("dotGit") c.Assert(err, IsNil) diff --git a/storage/filesystem/dotgit/writers_test.go b/storage/filesystem/dotgit/writers_test.go index 7147aec..a2517cc 100644 --- a/storage/filesystem/dotgit/writers_test.go +++ b/storage/filesystem/dotgit/writers_test.go @@ -3,16 +3,15 @@ package dotgit import ( "fmt" "io" - "io/ioutil" - "log" "os" "strconv" + "github.com/go-git/go-billy/v5/osfs" + "github.com/go-git/go-billy/v5/util" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/format/idxfile" "github.com/go-git/go-git/v5/plumbing/format/packfile" - "github.com/go-git/go-billy/v5/osfs" fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" ) @@ -20,14 +19,9 @@ import ( func (s *SuiteDotGit) TestNewObjectPack(c *C) { f := fixtures.Basic().One() - dir, err := ioutil.TempDir("", "example") - if err != nil { - log.Fatal(err) - } + fs, clean := s.TemporalFilesystem() + defer clean() - defer os.RemoveAll(dir) - - fs := osfs.New(dir) dot := New(fs) w, err := dot.NewObjectPack() @@ -65,14 +59,9 @@ func (s *SuiteDotGit) TestNewObjectPack(c *C) { } func (s *SuiteDotGit) TestNewObjectPackUnused(c *C) { - dir, err := ioutil.TempDir("", "example") - if err != nil { - log.Fatal(err) - } + fs, clean := s.TemporalFilesystem() + defer clean() - defer os.RemoveAll(dir) - - fs := osfs.New(dir) dot := New(fs) w, err := dot.NewObjectPack() @@ -93,10 +82,10 @@ func (s *SuiteDotGit) TestNewObjectPackUnused(c *C) { } func (s *SuiteDotGit) TestSyncedReader(c *C) { - tmpw, err := ioutil.TempFile("", "example") + tmpw, err := util.TempFile(osfs.Default, "", "example") c.Assert(err, IsNil) - tmpr, err := os.Open(tmpw.Name()) + tmpr, err := osfs.Default.Open(tmpw.Name()) c.Assert(err, IsNil) defer func() { @@ -137,14 +126,8 @@ func (s *SuiteDotGit) TestSyncedReader(c *C) { } func (s *SuiteDotGit) TestPackWriterUnusedNotify(c *C) { - dir, err := ioutil.TempDir("", "example") - if err != nil { - c.Assert(err, IsNil) - } - - defer os.RemoveAll(dir) - - fs := osfs.New(dir) + fs, clean := s.TemporalFilesystem() + defer clean() w, err := newPackWrite(fs) c.Assert(err, IsNil) |