Skip to content

Commit

Permalink
file: remove afero, add memoryfs
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Aug 23, 2023
1 parent f19ce9d commit 1209f44
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
11 changes: 5 additions & 6 deletions internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (
"encoding/hex"
"fmt"
"io"
"io/fs"
"os"

"github.com/spf13/afero"
)

const hashExt = ".sha256"
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
23 changes: 7 additions & 16 deletions internal/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -54,25 +54,16 @@ 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)
assert.Equal(t, !c.shouldFail, hashOK)
})
}
}

func removeIfExists(fs afero.Fs, filename string) error {
if err := fs.Remove(filename); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}

0 comments on commit 1209f44

Please sign in to comment.