From 324264d8d4d769f91448091e1f95c7b9da5914b6 Mon Sep 17 00:00:00 2001 From: gkze Date: Sun, 12 May 2019 12:23:45 -0700 Subject: [PATCH] Add some more tests --- Makefile | 11 +++++++++- README.md | 2 +- VERSION | 2 +- go.mod | 1 + go.sum | 2 ++ starmanager/starmanager.go | 8 +++---- utils/utils.go | 14 +++++++----- utils/utils_test.go | 45 +++++++++++++++++++++++++++++++++++++- 8 files changed, 71 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index e9439e2..fe64589 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,26 @@ +# Display this help message +.PHONY: help +help: + @awk '/^.PHONY:/ && (a ~ /#/) {gsub(/.PHONY: /, "", $$0); gsub(/# /, "", a); printf "\033[0;32m%-10s\033[0m%s\n", $$0, a}{a=$$0}' $(MAKEFILE_LIST) + +# Check code for errors .PHONY: check check: go vet ./... +# Run unit tests .PHONY: test test: check go test -v -race ./... +# Compile into executable binary .PHONY: build build: test CGO_ENABLED=0 go build -o stars -ldflags "-X main.Version=$(shell cat VERSION)" ./cmd +# Do a release. VERSION needs to be bumped manually .PHONY: release release: git tag v$(shell cat VERSION) git push origin master - goreleaser --rm-dist \ No newline at end of file + goreleaser --rm-dist diff --git a/README.md b/README.md index 89aa42b..daa391e 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ USAGE: stars [global options] command [command options] [arguments...] VERSION: - 0.4.13 + 0.4.14 COMMANDS: save Save all stars diff --git a/VERSION b/VERSION index bf357b6..489c893 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.4.13 \ No newline at end of file +0.4.14 diff --git a/go.mod b/go.mod index 9229f8a..f4ecb74 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/google/go-github/v25 v25.0.1 github.com/jdxcode/netrc v0.0.0-20190329161231-b36f1c51d91d github.com/kr/pretty v0.1.0 // indirect + github.com/spf13/afero v1.2.2 github.com/stretchr/testify v1.3.0 github.com/urfave/cli v1.20.0 github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect diff --git a/go.sum b/go.sum index 8bb5948..f48c13e 100644 --- a/go.sum +++ b/go.sum @@ -28,6 +28,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc= +github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= diff --git a/starmanager/starmanager.go b/starmanager/starmanager.go index 49d6206..61b0eb9 100644 --- a/starmanager/starmanager.go +++ b/starmanager/starmanager.go @@ -14,13 +14,13 @@ import ( "sync" "time" - "golang.org/x/oauth2" - "github.com/asdine/storm" "github.com/asdine/storm/q" - "github.com/gkze/stars/utils" "github.com/gkze/stars/auth" + "github.com/gkze/stars/utils" "github.com/google/go-github/v25/github" + "github.com/spf13/afero" + "golang.org/x/oauth2" ) // GITHUB - the GitHub API host @@ -90,7 +90,7 @@ func New() (*StarManager, error) { } for _, p := range toCreate { - err := utils.CreateIfNotExists(p.path, p.mode) + err := utils.CreateIfNotExists(p.path, p.mode, afero.NewOsFs()) if err != nil { log.Printf("An error occurred while attempting to create %s: %v", p.path, err.Error()) } diff --git a/utils/utils.go b/utils/utils.go index 64b54fe..df7aba3 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -2,6 +2,8 @@ package utils import ( "os" + + "github.com/spf13/afero" ) // StringInSlice checks whether a given string is in a slice @@ -15,19 +17,19 @@ func StringInSlice(s string, sl []string) bool { return false } -// CreateIfNotExists examines a path and if it is not present, creates the passed file type for the -// given path. -func CreateIfNotExists(path string, mode os.FileMode) error { - _, err := os.Stat(path) +// CreateIfNotExists examines a path and if it is not present, creates the +// passed file type for the given path +func CreateIfNotExists(path string, mode os.FileMode, fs afero.Fs) error { + _, err := fs.Stat(path) if err != nil && os.IsNotExist(err) { switch mode { case os.ModeDir: - err := os.MkdirAll(path, 0755) + err := fs.MkdirAll(path, 0755) if err != nil { return err } case 0: - _, err := os.Create(path) + _, err := fs.Create(path) if err != nil { return err } diff --git a/utils/utils_test.go b/utils/utils_test.go index 061529f..3a2015f 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -1,8 +1,10 @@ package utils import ( + "os" "testing" + "github.com/spf13/afero" "github.com/stretchr/testify/assert" ) @@ -29,6 +31,47 @@ func TestStringInSlice(t *testing.T) { } } +func doTestCreateIfNotExists( + t *testing.T, + path string, + mode os.FileMode, + exists bool, +) { + fs := afero.NewMemMapFs() + + err := CreateIfNotExists(path, mode, fs) + assert.NoError(t, err) + + fi, err := fs.Stat(path) + assert.NoError(t, err) + + switch mode { + case os.ModeDir: + assert.True(t, fi.Mode().IsDir()) + case 0: + assert.True(t, fi.Mode().IsRegular()) + } +} + func TestCreateIfNotExists(t *testing.T) { - t.SkipNow() + testCases := []struct { + path string + mode os.FileMode + exists bool + }{ + { + path: "/tmp/nonexistenttestfile", + mode: 0, + exists: false, + }, + { + path: "/tmp/nonexistenttestdir", + mode: os.ModeDir, + exists: false, + }, + } + + for _, tc := range testCases { + doTestCreateIfNotExists(t, tc.path, tc.mode, tc.exists) + } }