From 6d379cec4f4bdb8e814de63a937265cee0efedbd Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Sat, 19 Nov 2016 11:08:35 -0500 Subject: [PATCH] Adding gometalinter to CI checks Signed-off-by: Dave Henderson --- circle.yml | 8 ++++-- context_test.go | 2 +- data.go | 25 +++++++++++++---- data_test.go | 71 ++++++++++++++++-------------------------------- main.go | 5 +++- main_test.go | 27 +++++++----------- typeconv.go | 40 +++++++++++++-------------- typeconv_test.go | 60 ++++++++++++++++------------------------ 8 files changed, 108 insertions(+), 130 deletions(-) diff --git a/circle.yml b/circle.yml index d5261c3fa..899d75698 100644 --- a/circle.yml +++ b/circle.yml @@ -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 diff --git a/context_test.go b/context_test.go index 4e339533c..b4a60ff3c 100644 --- a/context_test.go +++ b/context_test.go @@ -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") } diff --git a/data.go b/data.go index 0f966c84e..b67442011 100644 --- a/data.go +++ b/data.go @@ -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)) @@ -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 } @@ -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 diff --git a/data_test.go b/data_test.go index d2d6610c8..0f63a5840 100644 --- a/data_test.go +++ b/data_test.go @@ -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) { diff --git a/main.go b/main.go index 3d46fa34a..e31e882cd 100644 --- a/main.go +++ b/main.go @@ -91,5 +91,8 @@ func main() { }, } - app.Run(os.Args) + err := app.Run(os.Args) + if err != nil { + log.Fatal(err) + } } diff --git a/main_test.go b/main_test.go index 814d5afd8..01af00dc8 100644 --- a/main_test.go +++ b/main_test.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "net/http/httptest" "os" "strings" "testing" @@ -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"}}`)) } diff --git a/typeconv.go b/typeconv.go index b5a23d55e..86c8e7f24 100644 --- a/typeconv.go +++ b/typeconv.go @@ -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 diff --git a/typeconv_test.go b/typeconv_test.go index 997f89de0..d5f5637d2 100644 --- a/typeconv_test.go +++ b/typeconv_test.go @@ -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) {