Skip to content

Commit

Permalink
Add Coveralls support and increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bcandrea committed Feb 15, 2016
1 parent 2c55e47 commit c6af998
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 17 deletions.
13 changes: 11 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
language: go

sudo: false
go:
- 1.4.2
- 1.5
- '1.5'
- tip
install:
- go get golang.org/x/tools/cmd/cover
- go get github.com/mattn/goveralls
script:
- go test -v -covermode=count -coverprofile=coverage.out
- $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN
env:
global:
secure: OqpRE8HgudoTmdRJpaesrmfLMkfpBr86aDCzPk6OHFKcib6xBHiX2ghWhVuJVlaf4S7b3FQbMAKPxjiVhDMt/eUoRmyIrfKd1z6MMlFv965pg+PnCZDiWIb5AfQnZIav6xAn6kHhXP/3RiT9Ok+n9Z256VIoWTX/bZ/NZlPeuWitUbh/cZP/4XydoKdDQc2iSpoVbVXSyqsyOEt5fJ6m3V0HVueIzNBYYjoZZoN9/IeA4AFnBmZi2WBGfuUJ57JyKjPF++lCNOJwONza3Nllzic6WId/SZ1kwKB5AkbR2fv+AS02SZUmMF3fRgxfMnYAAtRwqSwq44eKwTW3xDxrOXhnCEgmKOxoCg8KV49GLvdTBaTYH9WZUC5DjhWv9pvOci7hrnc/wsh3fajHDMIPON3qECGe4P0cZfEaJuIJxjZHqDQT5SWMq0eLAcMox6zueCgTYeTYRKgjrG0yQFPDnuFru3ocvF2tWmqsZcIWUMYNyUHtjpXnC6tjsPGU+hqXM0DY6L43bdlWRlsCxxBwbbYNbC/OX/FJufTSSRY7QXbn8/+0eXVTVCN63iD7p5Obx749ubZ3oKUHYKvZo1vJxIXrN7EAHI95iC9GeGr8IwDu9WoMyYI3UX6HLFRhPwNKMS5tAZpfEZO0gQV9aHNM3ucXwLFZ2l936hvfHxiPSpE=
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# gover - a version detector for Go packages

[![Build Status](https://travis-ci.org/bcandrea/gover.svg?branch=master)](https://travis-ci.org/bcandrea/gover)
[![Coverage Status](https://coveralls.io/repos/github/bcandrea/gover/badge.svg?branch=master)](https://coveralls.io/github/bcandrea/gover?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/bcandrea/gover)](https://goreportcard.com/report/github.com/bcandrea/gover)

A simple tool that outputs the value of the constant (or variable) `Version`
Expand Down
1 change: 1 addition & 0 deletions data/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data
34 changes: 21 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"go/ast"
"go/parser"
"go/token"
"io"
"os"
"path/filepath"
"strings"
)

// Version constant for gover.
const Version = "0.1.1"
const Version = "0.1.2"

// packageDir returns the actual directory on the file system where a package is located.
// It accepts a standard package name (e.g. github.com/bcandrea/gover) or a
Expand Down Expand Up @@ -108,23 +109,30 @@ func GetVersion(pkg string) (string, error) {
return versionFromAST(tree)
}

func main() {
showVersion := flag.Bool("v", false, "display version")
flag.Parse()
// run implements the behaviour of the gover command line application.
func run(args []string, outW, errW io.Writer) int {
cmdLine := flag.NewFlagSet(args[0], flag.ExitOnError)
showVersion := cmdLine.Bool("v", false, "display version")
cmdLine.Parse(args[1:])

if *showVersion {
fmt.Println("gover version", Version)
os.Exit(0)
fmt.Fprintln(outW, "gover version", Version)
return 0
}

if len(flag.Args()) < 1 {
fmt.Fprintln(os.Stderr, "usage: gover <package>")
os.Exit(1)
if len(cmdLine.Args()) < 1 {
fmt.Fprintln(errW, "usage: gover <package>")
return 1
}
ver, err := GetVersion(flag.Arg(0))
ver, err := GetVersion(cmdLine.Arg(0))
if err != nil {
fmt.Fprintln(os.Stderr, "gover:", err)
os.Exit(2)
fmt.Fprintln(errW, "gover:", err)
return 2
}
fmt.Println(ver)
fmt.Fprintln(outW, ver)
return 0
}

func main() {
os.Exit(run(os.Args, os.Stdout, os.Stderr))
}
71 changes: 70 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import "testing"
import (
"bytes"
"fmt"
"testing"
)

func TestGetVersion(t *testing.T) {
v, err := GetVersion("github.com/bcandrea/gover/test")
Expand Down Expand Up @@ -40,6 +44,13 @@ func TestGetVersionInFunc(t *testing.T) {
}
}

func TestGetVersionNotLiteral(t *testing.T) {
_, err := GetVersion("github.com/bcandrea/gover/nostring")
if err == nil {
t.Error("Versions should be basic literals")
}
}

func TestGetVersionInt(t *testing.T) {
v, err := GetVersion("github.com/bcandrea/gover/testint")
if err != nil {
Expand Down Expand Up @@ -77,3 +88,61 @@ func TestRelativePath(t *testing.T) {
t.Errorf("Expected version %s, got %s", expected, got)
}
}

func TestGetVersionWithNoPackage(t *testing.T) {
_, err := GetVersion("github.com/bcandrea/gover/data")
if err == nil {
t.Error("Trying to get the version from a package that does not contain go files should return an error")
}
}

func TestGetVersionOfFile(t *testing.T) {
_, err := GetVersion("./test/version.go")
if err == nil {
t.Error("Trying to get the version from a .go file should return an error")
}
}

func TestRun(t *testing.T) {
var outW, errW bytes.Buffer
args := []string{"gover", "-v"}

if expected, got := 0, run(args, &outW, &errW); expected != got {
t.Errorf("`gover -v' should exit with %d, got %d", expected, got)
}

if expected, got := fmt.Sprintf("gover version %s\n", Version), outW.String(); expected != got {
t.Errorf("`gover -v' should return [%s], got [%s]", expected, got)
}
}

func TestRunOnPackage(t *testing.T) {
var outW, errW bytes.Buffer
args := []string{"gover", "github.com/bcandrea/gover/test"}

if expected, got := 0, run(args, &outW, &errW); expected != got {
t.Errorf("expected exit code %d, got %d", expected, got)
}

if expected, got := "0.4.2\n", outW.String(); expected != got {
t.Errorf("version should be [%s], got [%s]", expected, got)
}
}

func TestRunWithNoArgs(t *testing.T) {
var outW, errW bytes.Buffer
args := []string{"gover"}

if expected, got := 1, run(args, &outW, &errW); expected != got {
t.Errorf("`gover' should exit with %d, got %d", expected, got)
}
}

func TestRunWithError(t *testing.T) {
var outW, errW bytes.Buffer
args := []string{"gover", "github.com/bcandrea/thispackagedoesnotexist"}

if expected, got := 2, run(args, &outW, &errW); expected != got {
t.Errorf("expected exit code %d, got %d", expected, got)
}
}
4 changes: 4 additions & 0 deletions nostring/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package nostring

// Version is an array.
var Version = []string{"1", "0"}

0 comments on commit c6af998

Please sign in to comment.