Skip to content

Commit

Permalink
Merge pull request #78 from hairyhenderson/add-gometalinter
Browse files Browse the repository at this point in the history
Adding gometalinter to CI checks
  • Loading branch information
hairyhenderson authored Nov 19, 2016
2 parents c160666 + 6d379ce commit 1949b92
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 130 deletions.
8 changes: 6 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
test:
dependencies:
pre:
- go get -u github.com/jstemmer/go-junit-report
- hash go-junit-report || go get -u github.com/jstemmer/go-junit-report
- hash gometalinter || (go get -u github.com/alecthomas/gometalinter && gometalinter --install)

test:
override:
- |
set -e -o pipefail
go test -v -race ./... | go-junit-report > $CIRCLE_TEST_REPORTS/report.xml
- gometalinter --deadline 20s --disable gotype --enable gofmt --enable goimports --enable misspell --enable unused
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ func TestEnvMapifiesEnvironment(t *testing.T) {
func TestEnvGetsUpdatedEnvironment(t *testing.T) {
c := &Context{}
assert.Empty(t, c.Env()["FOO"])
os.Setenv("FOO", "foo")
assert.NoError(t, os.Setenv("FOO", "foo"))
assert.Equal(t, c.Env()["FOO"], "foo")
}
25 changes: 20 additions & 5 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ import (

func init() {
// Add some types we want to be able to handle which can be missing by default
mime.AddExtensionType(".json", "application/json")
mime.AddExtensionType(".yml", "application/yaml")
mime.AddExtensionType(".yaml", "application/yaml")
err := mime.AddExtensionType(".json", "application/json")
if err != nil {
log.Fatal(err)
}
err = mime.AddExtensionType(".yml", "application/yaml")
if err != nil {
log.Fatal(err)
}
err = mime.AddExtensionType(".yaml", "application/yaml")
if err != nil {
log.Fatal(err)
}

sourceReaders = make(map[string]func(*Source, ...string) ([]byte, error))

Expand Down Expand Up @@ -219,7 +228,10 @@ func readHTTP(source *Source, args ...string) ([]byte, error) {
return nil, err
}
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return nil, err
}
err = res.Body.Close()
if err != nil {
return nil, err
}
Expand All @@ -241,8 +253,11 @@ func readHTTP(source *Source, args ...string) ([]byte, error) {
func readVault(source *Source, args ...string) ([]byte, error) {
if source.VC == nil {
source.VC = vault.NewClient()
source.VC.Login()
err := source.VC.Login()
addCleanupHook(source.VC.RevokeToken)
if err != nil {
return nil, err
}
}

p := source.URL.Path
Expand Down
71 changes: 24 additions & 47 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,55 +73,32 @@ func TestParseSourceWithAlias(t *testing.T) {
}

func TestDatasource(t *testing.T) {
fs := memfs.Create()
fs.Mkdir("/tmp", 0777)
f, _ := vfs.Create(fs, "/tmp/foo.json")
f.Write([]byte(`{"hello":"world"}`))

sources := make(map[string]*Source)
sources["foo"] = &Source{
Alias: "foo",
URL: &url.URL{
Scheme: "file",
Path: "/tmp/foo.json",
},
Ext: "json",
Type: "application/json",
FS: fs,
}
data := &Data{
Sources: sources,
test := func(ext, mime, contents string) {
fname := "foo." + ext
fs := memfs.Create()
_ = fs.Mkdir("/tmp", 0777)
f, _ := vfs.Create(fs, "/tmp/"+fname)
_, _ = f.Write([]byte(contents))

sources := map[string]*Source{
"foo": {
Alias: "foo",
URL: &url.URL{Scheme: "file", Path: "/tmp/" + fname},
Ext: ext,
Type: mime,
FS: fs,
},
}
data := &Data{
Sources: sources,
}
expected := map[string]interface{}{"hello": "world"}
actual := data.Datasource("foo")
assert.Equal(t, expected["hello"], actual["hello"])
}
expected := make(map[string]interface{})
expected["hello"] = "world"
actual := data.Datasource("foo")
assert.Equal(t, expected["hello"], actual["hello"])
}

func TestYAMLDatasource(t *testing.T) {
fs := memfs.Create()
fs.Mkdir("/tmp", 0777)
f, _ := vfs.Create(fs, "/tmp/foo.yml")
f.Write([]byte(`hello: world`))

sources := make(map[string]*Source)
sources["foo"] = &Source{
Alias: "foo",
URL: &url.URL{
Scheme: "file",
Path: "/tmp/foo.yml",
},
Ext: "yml",
Type: "application/yaml",
FS: fs,
}
data := &Data{
Sources: sources,
}
expected := make(map[string]interface{})
expected["hello"] = "world"
actual := data.Datasource("foo")
assert.Equal(t, expected["hello"], actual["hello"])
test("json", "application/json", `{"hello":"world"}`)
test("yml", "application/yaml", `hello: world`)
}

func setupHTTP(code int, mimetype string, body string) (*httptest.Server, *http.Client) {
Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,8 @@ func main() {
},
}

app.Run(os.Args)
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
27 changes: 10 additions & 17 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"net/http/httptest"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -46,28 +47,20 @@ func TestBoolTemplates(t *testing.T) {
assert.Equal(t, "false", testTemplate(g, `{{bool ""}}`))
}

func TestEc2MetaTemplates_MissingKey(t *testing.T) {
server, ec2meta := aws.MockServer(404, "")
defer server.Close()
g := &Gomplate{
funcMap: template.FuncMap{
"ec2meta": ec2meta.Meta,
},
func TestEc2MetaTemplates(t *testing.T) {
createGomplate := func(status int, body string) (*Gomplate, *httptest.Server) {
server, ec2meta := aws.MockServer(status, body)
return &Gomplate{template.FuncMap{"ec2meta": ec2meta.Meta}}, server
}

g, s := createGomplate(404, "")
defer s.Close()
assert.Equal(t, "", testTemplate(g, `{{ec2meta "foo"}}`))
assert.Equal(t, "default", testTemplate(g, `{{ec2meta "foo" "default"}}`))
}

func TestEc2MetaTemplates_ValidKey(t *testing.T) {
server, ec2meta := aws.MockServer(200, "i-1234")
defer server.Close()
g := &Gomplate{
funcMap: template.FuncMap{
"ec2meta": ec2meta.Meta,
},
}

s.Close()
g, s = createGomplate(200, "i-1234")
defer s.Close()
assert.Equal(t, "i-1234", testTemplate(g, `{{ec2meta "instance-id"}}`))
assert.Equal(t, "i-1234", testTemplate(g, `{{ec2meta "instance-id" "default"}}`))
}
Expand Down
40 changes: 20 additions & 20 deletions typeconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,44 @@ func (t *TypeConv) Bool(in string) bool {
return false
}

// JSON - Unmarshal a JSON Object
func (t *TypeConv) JSON(in string) map[string]interface{} {
obj := make(map[string]interface{})
err := json.Unmarshal([]byte(in), &obj)
func unmarshalObj(obj map[string]interface{}, in string, f func([]byte, interface{}) error) map[string]interface{} {
err := f([]byte(in), &obj)
if err != nil {
log.Fatalf("Unable to unmarshal JSON object %s: %v", in, err)
log.Fatalf("Unable to unmarshal object %s: %v", in, err)
}
return obj
}

// JSONArray - Unmarshal a JSON Array
func (t *TypeConv) JSONArray(in string) []interface{} {
obj := make([]interface{}, 1)
err := json.Unmarshal([]byte(in), &obj)
func unmarshalArray(obj []interface{}, in string, f func([]byte, interface{}) error) []interface{} {
err := f([]byte(in), &obj)
if err != nil {
log.Fatalf("Unable to unmarshal JSON array %s: %v", in, err)
log.Fatalf("Unable to unmarshal array %s: %v", in, err)
}
return obj
}

// JSON - Unmarshal a JSON Object
func (t *TypeConv) JSON(in string) map[string]interface{} {
obj := make(map[string]interface{})
return unmarshalObj(obj, in, json.Unmarshal)
}

// JSONArray - Unmarshal a JSON Array
func (t *TypeConv) JSONArray(in string) []interface{} {
obj := make([]interface{}, 1)
return unmarshalArray(obj, in, json.Unmarshal)
}

// YAML - Unmarshal a YAML Object
func (t *TypeConv) YAML(in string) map[string]interface{} {
obj := make(map[string]interface{})
err := yaml.Unmarshal([]byte(in), &obj)
if err != nil {
log.Fatalf("Unable to unmarshal YAML object %s: %v", in, err)
}
return obj
return unmarshalObj(obj, in, yaml.Unmarshal)
}

// YAMLArray - Unmarshal a YAML Array
func (t *TypeConv) YAMLArray(in string) []interface{} {
obj := make([]interface{}, 1)
err := yaml.Unmarshal([]byte(in), &obj)
if err != nil {
log.Fatalf("Unable to unmarshal YAML array %s: %v", in, err)
}
return obj
return unmarshalArray(obj, in, yaml.Unmarshal)
}

// Slice creates a slice from a bunch of arguments
Expand Down
60 changes: 23 additions & 37 deletions typeconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,54 +23,40 @@ func TestBool(t *testing.T) {
assert.True(t, ty.Bool("1"))
}

func TestJSON(t *testing.T) {
func TestUnmarshalObj(t *testing.T) {
ty := new(TypeConv)
expected := make(map[string]interface{})
expected["foo"] = "bar"
expected["one"] = 1.0
expected["true"] = true
expected := map[string]interface{}{
"foo": "bar",
"one": 1.0,
"true": true,
}

actual := ty.JSON(`{"foo":"bar","one":1.0,"true":true}`)
assert.Equal(t, expected["foo"], actual["foo"])
assert.Equal(t, expected["one"], actual["one"])
assert.Equal(t, expected["true"], actual["true"])
}

func TestJSONArray(t *testing.T) {
ty := new(TypeConv)

expected := []string{"foo", "bar"}
actual := ty.JSONArray(`["foo","bar"]`)
assert.Equal(t, expected[0], actual[0])
assert.Equal(t, expected[1], actual[1])
}

func TestYAML(t *testing.T) {
ty := new(TypeConv)
expected := make(map[string]interface{})
expected["foo"] = "bar"
expected["one"] = 1.0
expected["true"] = true

actual := ty.YAML(`foo: bar
test := func(actual map[string]interface{}) {
assert.Equal(t, expected["foo"], actual["foo"])
assert.Equal(t, expected["one"], actual["one"])
assert.Equal(t, expected["true"], actual["true"])
}
test(ty.JSON(`{"foo":"bar","one":1.0,"true":true}`))
test(ty.YAML(`foo: bar
one: 1.0
true: true
`)
assert.Equal(t, expected["foo"], actual["foo"])
assert.Equal(t, expected["one"], actual["one"])
assert.Equal(t, expected["true"], actual["true"])
`))
}

func TestYAMLArray(t *testing.T) {
func TestUnmarshalArray(t *testing.T) {
ty := new(TypeConv)

expected := []string{"foo", "bar"}
actual := ty.YAMLArray(`

test := func(actual []interface{}) {
assert.Equal(t, expected[0], actual[0])
assert.Equal(t, expected[1], actual[1])
}
test(ty.JSONArray(`["foo","bar"]`))
test(ty.YAMLArray(`
- foo
- bar
`)
assert.Equal(t, expected[0], actual[0])
assert.Equal(t, expected[1], actual[1])
`))
}

func TestSlice(t *testing.T) {
Expand Down

0 comments on commit 1949b92

Please sign in to comment.