diff --git a/.editorconfig b/.editorconfig index 9912e0f..6369aed 100644 --- a/.editorconfig +++ b/.editorconfig @@ -9,6 +9,6 @@ trim_trailing_whitespace = true insert_final_newline = true charset = utf-8 -[*.go] +[{*.go,Makefile}] indent_style = tab indent_size = 4 diff --git a/.travis.yml b/.travis.yml index 8fa6db3..0fa6ba3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,11 @@ language: go go: 1.7.4 -install: - - go get github.com/Masterminds/glide - - go get github.com/pierrre/gotestcover - - go get github.com/mattn/goveralls - - go get golang.org/x/tools/cmd/cover - - glide install -script: gotestcover -race -coverprofile=coverage.out $(glide novendor) +install: make setup +script: make ci after_success: + - go get github.com/mattn/goveralls - goveralls -coverprofile=coverage.out -service=travis-ci -repotoken="$COVERALLS_TOKEN" - - test -n "$TRAVIS_TAG" && gem install fpm && go get github.com/goreleaser/goreleaser && goreleaser + - go get github.com/goreleaser/goreleaser + - test -n "$TRAVIS_TAG" && goreleaser notifications: email: false diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d1e20a4 --- /dev/null +++ b/Makefile @@ -0,0 +1,44 @@ +SOURCE_FILES?=$$(glide novendor) +TEST_PATTERN?=. +TEST_OPTIONS?=-race + +setup: ## Install all the build and lint dependencies + @go get -u github.com/alecthomas/gometalinter + @go get -u github.com/pierrre/gotestcover + @go get -u golang.org/x/tools/cmd/cover + @go get -u github.com/Masterminds/glide + @glide install + @gometalinter --install + +test: ## Run all the tests + @gotestcover $(TEST_OPTIONS) -coverprofile=coverage.out $(SOURCE_FILES) -run $(TEST_PATTERN) -timeout=30s + +fmt: ## gofmt and goimports all go files + find . -name '*.go' -not -wholename './vendor/*' | while read -r file; do gofmt -w -s "$$file"; goimports -w "$$file"; done + +lint: ## Run all the linters + @gometalinter --vendor --disable-all \ + --enable=deadcode \ + --enable=ineffassign \ + --enable=gosimple \ + --enable=staticcheck \ + --enable=gofmt \ + --enable=goimports \ + --enable=dupl \ + --enable=misspell \ + --enable=errcheck \ + --enable=vet \ + --enable=vetshadow \ + --deadline=1m \ + ./... + +ci: lint test ## Run all the tests and code checks + +build: ## Build a beta version of releaser + @go build + +# Absolutely awesome: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html +help: + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' + +.DEFAULT_GOAL := build diff --git a/antibody.go b/antibody.go index 1ddaacc..81579b3 100644 --- a/antibody.go +++ b/antibody.go @@ -40,17 +40,17 @@ func (a *Antibody) Bundle() (result string, err error) { g.Go(func() error { l = strings.TrimSpace(l) if l != "" && l[0] != '#' { - s, err := bundle.New(a.Home, l).Get() + s, berr := bundle.New(a.Home, l).Get() lock.Lock() shs = append(shs, indexedLine{index, s}) lock.Unlock() - return err + return berr } return nil }) } - if err := scanner.Err(); err != nil { - return result, err + if err = scanner.Err(); err != nil { + return } err = g.Wait() return shs.String(), err diff --git a/antibody_test.go b/antibody_test.go index b1907ef..b71c153 100644 --- a/antibody_test.go +++ b/antibody_test.go @@ -14,7 +14,6 @@ import ( func TestAntibody(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) bundles := []string{ "# comments also are allowed", "caarlos0/ports kind:path # comment at the end of the line", @@ -42,7 +41,6 @@ func TestAntibody(t *testing.T) { func TestAntibodyError(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) bundles := bytes.NewBufferString("invalid-repo") sh, err := antibody.New(home, bundles).Bundle() assert.Error(err) @@ -54,7 +52,7 @@ func TestHome(t *testing.T) { } func TestHomeFromEnvironmentVariable(t *testing.T) { - os.Setenv("ANTIBODY_HOME", "/tmp") + assert.NoError(t, os.Setenv("ANTIBODY_HOME", "/tmp")) assert.Equal(t, "/tmp", antibody.Home()) } diff --git a/bundle/bundle_test.go b/bundle/bundle_test.go index fdd3ad3..e02f963 100644 --- a/bundle/bundle_test.go +++ b/bundle/bundle_test.go @@ -9,19 +9,41 @@ import ( "github.com/stretchr/testify/assert" ) -func TestZshGitBundle(t *testing.T) { - assert := assert.New(t) - home := home() - defer os.RemoveAll(home) - result, err := bundle.New(home, "caarlos0/jvm").Get() - assert.Contains(result, "jvm.plugin.zsh") - assert.NoError(err) +func TestSuccessfullGitBundles(t *testing.T) { + table := []struct { + line, result string + }{ + { + "caarlos0/jvm", + "jvm.plugin.zsh", + }, + { + "caarlos0/jvm kind:path", + "export PATH=\"", + }, + { + "caarlos0/jvm kind:path branch:gh-pages", + "export PATH=\"", + }, + { + "caarlos0/jvm kind:dummy", + "", + }, + } + for _, row := range table { + t.Run(row.line, func(t *testing.T) { + assert := assert.New(t) + home := home() + result, err := bundle.New(home, row.line).Get() + assert.Contains(result, row.result) + assert.NoError(err) + }) + } } func TestZshInvalidGitBundle(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) _, err := bundle.New(home, "doesnt exist").Get() assert.Error(err) } @@ -29,7 +51,6 @@ func TestZshInvalidGitBundle(t *testing.T) { func TestZshLocalBundle(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) assert.NoError(ioutil.WriteFile(home+"/a.sh", []byte("echo 9"), 0644)) result, err := bundle.New(home, home).Get() assert.Contains(result, "a.sh") @@ -39,7 +60,6 @@ func TestZshLocalBundle(t *testing.T) { func TestZshInvalidLocalBundle(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) _, err := bundle.New(home, "/asduhasd/asdasda").Get() assert.Error(err) } @@ -47,48 +67,19 @@ func TestZshInvalidLocalBundle(t *testing.T) { func TestPathInvalidLocalBundle(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) _, err := bundle.New(home, "/asduhasd/asdasda kind:path").Get() assert.Error(err) } -func TestPathGitBundle(t *testing.T) { - assert := assert.New(t) - home := home() - defer os.RemoveAll(home) - result, err := bundle.New(home, "caarlos0/jvm kind:path").Get() - assert.Contains(result, "export PATH=\"") - assert.NoError(err) -} - func TestPathLocalBundle(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) assert.NoError(ioutil.WriteFile(home+"whatever.sh", []byte(""), 0644)) result, err := bundle.New(home, home+" kind:path").Get() assert.Equal("export PATH=\""+home+":$PATH\"", result) assert.NoError(err) } -func TestPathGitBundleWithBranch(t *testing.T) { - assert := assert.New(t) - home := home() - defer os.RemoveAll(home) - result, err := bundle.New(home, "caarlos0/jvm kind:path branch:gh-pages").Get() - assert.Contains(result, "export PATH=\"") - assert.NoError(err) -} - -func TestPathDummyBundle(t *testing.T) { - assert := assert.New(t) - home := home() - defer os.RemoveAll(home) - result, err := bundle.New(home, "caarlos0/jvm kind:dummy").Get() - assert.Empty(result) - assert.NoError(err) -} - func home() string { home, err := ioutil.TempDir(os.TempDir(), "antibody") if err != nil { diff --git a/bundle/path.go b/bundle/path.go index e378097..93fc83b 100644 --- a/bundle/path.go +++ b/bundle/path.go @@ -7,7 +7,7 @@ type pathBundle struct { } func (bundle pathBundle) Get() (result string, err error) { - if err := bundle.Project.Download(); err != nil { + if err = bundle.Project.Download(); err != nil { return result, err } return "export PATH=\"" + bundle.Project.Folder() + ":$PATH\"", err diff --git a/bundle/zsh.go b/bundle/zsh.go index f4754ac..e7bfbc7 100644 --- a/bundle/zsh.go +++ b/bundle/zsh.go @@ -14,7 +14,7 @@ type zshBundle struct { var zshGlobs = []string{"*.plugin.zsh", "*.zsh", "*.sh", "*.zsh-theme"} func (bundle zshBundle) Get() (result string, err error) { - if err := bundle.Project.Download(); err != nil { + if err = bundle.Project.Download(); err != nil { return result, err } for _, glob := range zshGlobs { diff --git a/cmd/antibody/main.go b/cmd/antibody/main.go index 71bf0b3..f8f9791 100644 --- a/cmd/antibody/main.go +++ b/cmd/antibody/main.go @@ -22,5 +22,5 @@ func main() { } app.Version = version app.Author = "Carlos Alexandro Becker (caarlos0@gmail.com)" - app.Run(os.Args) + _ = app.Run(os.Args) } diff --git a/project/git.go b/project/git.go index 4c89866..02faf03 100644 --- a/project/git.go +++ b/project/git.go @@ -19,8 +19,10 @@ type gitProject struct { // NewClonedGit is a git project that was already cloned, so, only Update // will work here. func NewClonedGit(home, folderName string) Project { - version := "master" - version, _ = branch(folderName) + version, err := branch(folderName) + if err != nil { + version = "master" + } url := folder.ToURL(folderName) return gitProject{ folder: filepath.Join(home, folderName), diff --git a/project/git_test.go b/project/git_test.go index 6eba2b0..479e3d2 100644 --- a/project/git_test.go +++ b/project/git_test.go @@ -23,7 +23,6 @@ func TestDownloadAllKinds(t *testing.T) { } for _, url := range urls { home := home() - defer os.RemoveAll(home) assert.NoError( project.NewGit(home, url).Download(), "Repo "+url+" failed to download", @@ -34,23 +33,12 @@ func TestDownloadAllKinds(t *testing.T) { func TestDownloadAnotherBranch(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) assert.NoError(project.NewGit(home, "caarlos0/jvm branch:gh-pages").Download()) } -func TestDownloadAndUpdate(t *testing.T) { - assert := assert.New(t) - home := home() - defer os.RemoveAll(home) - repo := project.NewGit(home, "caarlos0/ports") - assert.NoError(repo.Download()) - assert.NoError(repo.Update()) -} - func TestUpdateNonExistentLocalRepo(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) repo := project.NewGit(home, "caarlos0/ports") assert.Error(repo.Update()) } @@ -58,7 +46,6 @@ func TestUpdateNonExistentLocalRepo(t *testing.T) { func TestDownloadNonExistenRepo(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) repo := project.NewGit(home, "doesn-not-exist-really branch:also-nope") assert.Error(repo.Download()) } @@ -66,16 +53,15 @@ func TestDownloadNonExistenRepo(t *testing.T) { func TestDownloadMultipleTimes(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) repo := project.NewGit(home, "caarlos0/ports") assert.NoError(repo.Download()) assert.NoError(repo.Download()) + assert.NoError(repo.Update()) } func TestDownloadFolderNaming(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) repo := project.NewGit(home, "caarlos0/ports") assert.Equal( home+"/https-COLON--SLASH--SLASH-github.com-SLASH-caarlos0-SLASH-ports", diff --git a/project/project_test.go b/project/project_test.go index 29e6fe2..26676ee 100644 --- a/project/project_test.go +++ b/project/project_test.go @@ -12,7 +12,6 @@ import ( func TestList(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) assert.NoError(project.New(home, "caarlos0/jvm branch:gh-pages").Download()) list, err := project.List(home) assert.NoError(err) @@ -22,7 +21,6 @@ func TestList(t *testing.T) { func TestListEmptyFolder(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) list, err := project.List(home) assert.NoError(err) assert.Len(list, 0) @@ -38,8 +36,7 @@ func TestListNonExistentFolder(t *testing.T) { func TestUpdate(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) - repo := project.New(home, "caarlos0/jvm") + repo := project.New(home, "caarlos0/ports") assert.NoError(repo.Download()) assert.NoError(repo.Update()) } @@ -47,7 +44,6 @@ func TestUpdate(t *testing.T) { func TestUpdateHome(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) assert.NoError(project.New(home, "caarlos0/jvm").Download()) assert.NoError(project.New(home, "caarlos0/ports").Download()) assert.NoError(project.New(home, "/tmp").Download()) @@ -61,9 +57,8 @@ func TestUpdateNonExistentHome(t *testing.T) { func TestUpdateHomeWithNoGitProjects(t *testing.T) { assert := assert.New(t) home := home() - defer os.RemoveAll(home) repo := project.New(home, "caarlos0/jvm") assert.NoError(repo.Download()) - os.RemoveAll(filepath.Join(repo.Folder(), ".git")) + assert.NoError(os.RemoveAll(filepath.Join(repo.Folder(), ".git"))) assert.Error(project.Update(home)) }