diff --git a/go.mod b/go.mod index 9709a07..c154ca5 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,9 @@ go 1.20 require ( github.com/fatih/color v1.13.0 github.com/gofrs/flock v0.8.1 + github.com/liamg/memoryfs v1.6.0 github.com/mattn/go-isatty v0.0.16 github.com/platformsh/platformify v0.1.2 - github.com/spf13/afero v1.9.5 github.com/spf13/cobra v1.6.1 github.com/spf13/viper v1.15.0 github.com/stretchr/testify v1.8.1 @@ -40,6 +40,7 @@ require ( github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/shopspring/decimal v1.2.0 // indirect + github.com/spf13/afero v1.9.5 // indirect github.com/spf13/cast v1.5.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect diff --git a/go.sum b/go.sum index 181dc22..1424593 100644 --- a/go.sum +++ b/go.sum @@ -170,6 +170,8 @@ github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/liamg/memoryfs v1.6.0 h1:jAFec2HI1PgMTem5gR7UT8zi9u4BfG5jorCRlLH06W8= +github.com/liamg/memoryfs v1.6.0/go.mod h1:z7mfqXFQS8eSeBBsFjYLlxYRMRyiPktytvYCYTb3BSk= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= diff --git a/internal/file/file.go b/internal/file/file.go index 18a14ba..345d02d 100644 --- a/internal/file/file.go +++ b/internal/file/file.go @@ -6,9 +6,8 @@ import ( "encoding/hex" "fmt" "io" + "io/fs" "os" - - "github.com/spf13/afero" ) const hashExt = ".sha256" @@ -46,12 +45,12 @@ func CheckSize(filename string, size int) (bool, error) { return stat.Size() == int64(size), nil } -var testableFS = afero.NewOsFs() +var testableFS = os.DirFS("/") // CheckHash checks if a file has the given SHA256 hash. // It supports reading the file's current hash from a static file saved next to it with the hashExt extension. func CheckHash(filename, hash string) (bool, error) { - if fh, err := afero.ReadFile(testableFS, filename+hashExt); err == nil { + if fh, err := fs.ReadFile(testableFS, filename+hashExt); err == nil { return string(fh) == hash, nil } fh, err := sha256Sum(testableFS, filename) @@ -69,8 +68,8 @@ func SaveHash(filename, hash string) error { } // sha256Sum calculates the SHA256 hash of a file. -func sha256Sum(fs afero.Fs, filename string) (string, error) { - f, err := fs.Open(filename) +func sha256Sum(filesystem fs.FS, filename string) (string, error) { + f, err := filesystem.Open(filename) if err != nil { return "", err } diff --git a/internal/file/file_test.go b/internal/file/file_test.go index b10c7a5..9620f4f 100644 --- a/internal/file/file_test.go +++ b/internal/file/file_test.go @@ -4,18 +4,18 @@ import ( "os" "testing" - "github.com/spf13/afero" + "github.com/liamg/memoryfs" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestCheckHash(t *testing.T) { // Temporarily swap to a memory filesystem. - testableFS = afero.NewMemMapFs() + testableFS = memoryfs.New() defer func() { - testableFS = afero.NewOsFs() + testableFS = os.DirFS("/") }() - fs := testableFS + filesystem := testableFS.(*memoryfs.FS) mockContent := "hello world\n" mockContentHash := "a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447" @@ -54,14 +54,12 @@ func TestCheckHash(t *testing.T) { }, } - filename := "test" for _, c := range cases { - require.NoError(t, removeIfExists(fs, filename)) - require.NoError(t, removeIfExists(fs, filename+hashExt)) t.Run(c.name, func(t *testing.T) { - require.NoError(t, afero.WriteFile(fs, filename, []byte(c.content), 0o644)) + filename := c.name + require.NoError(t, filesystem.WriteFile(filename, []byte(c.content), 0o644)) if c.writeHash != "" { - require.NoError(t, afero.WriteFile(fs, filename+hashExt, []byte(c.writeHash), 0o644)) + require.NoError(t, filesystem.WriteFile(filename+hashExt, []byte(c.writeHash), 0o644)) } hashOK, err := CheckHash(filename, c.checkHash) assert.NoError(t, err) @@ -69,10 +67,3 @@ func TestCheckHash(t *testing.T) { }) } } - -func removeIfExists(fs afero.Fs, filename string) error { - if err := fs.Remove(filename); err != nil && !os.IsNotExist(err) { - return err - } - return nil -}