Skip to content

Commit

Permalink
Add some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gkze committed May 12, 2019
1 parent de35e9f commit 324264d
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 14 deletions.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
goreleaser --rm-dist
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ USAGE:
stars [global options] command [command options] [arguments...]

VERSION:
0.4.13
0.4.14

COMMANDS:
save Save all stars
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.13
0.4.14
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
8 changes: 4 additions & 4 deletions starmanager/starmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
Expand Down
14 changes: 8 additions & 6 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package utils

import (
"os"

"github.com/spf13/afero"
)

// StringInSlice checks whether a given string is in a slice
Expand All @@ -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
}
Expand Down
45 changes: 44 additions & 1 deletion utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package utils

import (
"os"
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)

Expand All @@ -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)
}
}

0 comments on commit 324264d

Please sign in to comment.