From 01df7536992af375a54bbedf45369a475953e372 Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Sun, 2 May 2021 23:40:08 +0200 Subject: *: use go-billy instead of os calls --- storage/filesystem/config_test.go | 7 +- storage/filesystem/dotgit/dotgit_test.go | 117 ++++++++++----------- .../dotgit/repository_filesystem_test.go | 15 +-- storage/filesystem/dotgit/writers_test.go | 37 ++----- storage/filesystem/object_test.go | 8 +- storage/filesystem/storage_test.go | 23 ++-- 6 files changed, 93 insertions(+), 114 deletions(-) (limited to 'storage') diff --git a/storage/filesystem/config_test.go b/storage/filesystem/config_test.go index c092d14..ce6a959 100644 --- a/storage/filesystem/config_test.go +++ b/storage/filesystem/config_test.go @@ -1,14 +1,13 @@ package filesystem import ( - "io/ioutil" "os" "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/config" "github.com/go-git/go-git/v5/storage/filesystem/dotgit" - - fixtures "github.com/go-git/go-git-fixtures/v4" . "gopkg.in/check.v1" ) @@ -22,7 +21,7 @@ type ConfigSuite struct { var _ = Suite(&ConfigSuite{}) func (s *ConfigSuite) SetUpTest(c *C) { - tmp, err := ioutil.TempDir("", "go-git-filestystem-config") + tmp, err := util.TempDir(osfs.Default, "", "go-git-filestystem-config") c.Assert(err, IsNil) s.dir = dotgit.New(osfs.New(tmp)) 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) diff --git a/storage/filesystem/object_test.go b/storage/filesystem/object_test.go index 036420f..22f5b0c 100644 --- a/storage/filesystem/object_test.go +++ b/storage/filesystem/object_test.go @@ -9,6 +9,8 @@ import ( "path/filepath" "testing" + "github.com/go-git/go-billy/v5" + "github.com/go-git/go-billy/v5/osfs" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/cache" "github.com/go-git/go-git/v5/storage/filesystem/dotgit" @@ -226,14 +228,14 @@ func (s *FsSuite) TestPackfileIter(c *C) { }) } -func copyFile(c *C, dstDir, dstFilename string, srcFile *os.File) { +func copyFile(c *C, dstDir, dstFilename string, srcFile billy.File) { _, err := srcFile.Seek(0, 0) c.Assert(err, IsNil) - err = os.MkdirAll(dstDir, 0750|os.ModeDir) + err = osfs.Default.MkdirAll(dstDir, 0750|os.ModeDir) c.Assert(err, IsNil) - dst, err := os.OpenFile(filepath.Join(dstDir, dstFilename), os.O_CREATE|os.O_WRONLY, 0666) + dst, err := osfs.Default.OpenFile(filepath.Join(dstDir, dstFilename), os.O_CREATE|os.O_WRONLY, 0666) c.Assert(err, IsNil) defer dst.Close() diff --git a/storage/filesystem/storage_test.go b/storage/filesystem/storage_test.go index 20eead0..096c37a 100644 --- a/storage/filesystem/storage_test.go +++ b/storage/filesystem/storage_test.go @@ -1,15 +1,16 @@ package filesystem import ( - "io/ioutil" "testing" "github.com/go-git/go-git/v5/plumbing/cache" "github.com/go-git/go-git/v5/plumbing/storer" "github.com/go-git/go-git/v5/storage/test" + "github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5/memfs" "github.com/go-git/go-billy/v5/osfs" + "github.com/go-git/go-billy/v5/util" . "gopkg.in/check.v1" ) @@ -18,13 +19,18 @@ func Test(t *testing.T) { TestingT(t) } type StorageSuite struct { test.BaseStorageSuite dir string + fs billy.Filesystem } var _ = Suite(&StorageSuite{}) func (s *StorageSuite) SetUpTest(c *C) { - s.dir = c.MkDir() - storage := NewStorage(osfs.New(s.dir), cache.NewObjectLRUDefault()) + tmp, err := util.TempDir(osfs.Default, "", "go-git-filestystem-config") + c.Assert(err, IsNil) + + s.dir = tmp + s.fs = osfs.New(s.dir) + storage := NewStorage(s.fs, cache.NewObjectLRUDefault()) setUpTest(s, c, storage) } @@ -49,7 +55,7 @@ func (s *StorageSuite) TestFilesystem(c *C) { } func (s *StorageSuite) TestNewStorageShouldNotAddAnyContentsToDir(c *C) { - fis, err := ioutil.ReadDir(s.dir) + fis, err := s.fs.ReadDir("/") c.Assert(err, IsNil) c.Assert(fis, HasLen, 0) } @@ -61,9 +67,14 @@ type StorageExclusiveSuite struct { var _ = Suite(&StorageExclusiveSuite{}) func (s *StorageExclusiveSuite) SetUpTest(c *C) { - s.dir = c.MkDir() + tmp, err := util.TempDir(osfs.Default, "", "go-git-filestystem-config") + c.Assert(err, IsNil) + + s.dir = tmp + s.fs = osfs.New(s.dir) + storage := NewStorageWithOptions( - osfs.New(s.dir), + s.fs, cache.NewObjectLRUDefault(), Options{ExclusiveAccess: true}) -- cgit