From a18863a64a67153aae14a72d97b13630faa62254 Mon Sep 17 00:00:00 2001 From: Sven Walter Date: Mon, 12 Feb 2018 23:21:48 +0100 Subject: [PATCH 1/4] add cmdutil --- cmdutil/exit.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 cmdutil/exit.go diff --git a/cmdutil/exit.go b/cmdutil/exit.go new file mode 100644 index 0000000..6c194ff --- /dev/null +++ b/cmdutil/exit.go @@ -0,0 +1,34 @@ +package cmdutil + +import "os" + +type exitCode struct { + code int +} + +// Exit causes the current program to exit with the given status code. On the +// contrary to os.Exit, it respects defer statements. It requires the +// HandleExit function to be deferred in top of the main function. +// +// Internally this is done by throwing a panic with the ExitCode type, which +// gets recovered in the HandleExit function. +func Exit(code int) { + panic(exitCode{code: code}) +} + +// HandleExit recovers from Exit calls and terminates the current program with +// a proper exit code. It should get deferred at the beginning of the main +// function. +// +// func main() { +// defer cmdutil.HandleExit() +// run() // this function might call Exit anytime +// } +func HandleExit() { + if e := recover(); e != nil { + if exit, ok := e.(exitCode); ok == true { + os.Exit(exit.code) + } + panic(e) // not an Exit, bubble up + } +} From 025c9adb2ae987909a8dd26117a57b5fa3f06f69 Mon Sep 17 00:00:00 2001 From: Sven Walter Date: Mon, 12 Feb 2018 23:21:54 +0100 Subject: [PATCH 2/4] add testutil --- testutil/golden.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 testutil/golden.go diff --git a/testutil/golden.go b/testutil/golden.go new file mode 100644 index 0000000..8a579a4 --- /dev/null +++ b/testutil/golden.go @@ -0,0 +1,65 @@ +package testutil + +import ( + "encoding/json" + "flag" + "io/ioutil" + "os" + "testing" + + yaml "gopkg.in/yaml.v2" +) + +var ( + updateGolden = flag.Bool("update-golden", false, + "update the golden file instead of comparing it") +) + +// AssertGolden tests, if the content of filename matches given data. On +// missmatch the test fails. When setting the `-update-golden` flag to the +// test, if will update the file which can be compared via a VCS diff. +func AssertGolden(t *testing.T, filename string, data []byte) { + if *updateGolden { + err := ioutil.WriteFile(filename, data, os.FileMode(0644)) + if err != nil { + t.Error(err) + return + } + } + + golden, err := ioutil.ReadFile(filename) + if err != nil { + t.Error(err) + return + } + + if string(golden) != string(data) { + t.Errorf("Generated file '%s' doesn't match golden file. Update with '-update-golden'.", filename) + } +} + +// AssertGoldenYAML works like AssertGolden, but converts the data to YAML file. +func AssertGoldenYAML(t *testing.T, filename string, data interface{}) { + generated, err := yaml.Marshal(data) + if err != nil { + t.Error(err) + return + } + + generated = append(generated, '\n') + + AssertGolden(t, filename, generated) +} + +// AssertGoldenJSON works like AssertGolden, but converts the data to JSON file. +func AssertGoldenJSON(t *testing.T, filename string, data interface{}) { + generated, err := json.MarshalIndent(data, "", " ") + if err != nil { + t.Error(err) + return + } + + generated = append(generated, '\n') + + AssertGolden(t, filename, generated) +} From 78dfffc58023df3327aae99847c4b4179c01c6eb Mon Sep 17 00:00:00 2001 From: Sven Walter Date: Tue, 13 Feb 2018 17:38:40 +0100 Subject: [PATCH 3/4] update README --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 460409a..71629ca 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,9 @@ # rebuy-go-sdk + +[![GoDoc](https://godoc.org/github.com/rebuy-de/rebuy-go-sdk?status.svg)](https://godoc.org/github.com/rebuy-de/rebuy-go-sdk) + Library for our Golang projects + +> **Development Status** *rebuy-go-sdk* is designed for internal use. Since it +> uses [Semantic Versioning](https://semver.org/) it is safe to use, but expect +> big changes between major version updates. From 54328155b22600ef8a5ad8967ff74e479620bb54 Mon Sep 17 00:00:00 2001 From: Sven Walter Date: Tue, 13 Feb 2018 18:20:34 +0100 Subject: [PATCH 4/4] add Travis configuration --- .gitignore | 1 + .travis.yml | 23 +++++++++++++++++++++++ Gopkg.lock | 15 +++++++++++++++ Gopkg.toml | 3 +++ 4 files changed, 42 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61ead86 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..7d36e3b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,23 @@ +language: go + +go: + - 1.9 + - master + +go_import_path: github.com/rebuy-de/rebuy-go-sdk + +before_install: +- GOFILES=$(find . -iname '*.go' -type f | grep -v /vendor/) +- GOPKGS=$(go list ./...) +- curl -L -s https://github.com/golang/dep/releases/download/v0.4.1/dep-linux-amd64 -o $GOPATH/bin/dep +- chmod +x $GOPATH/bin/dep +- go get github.com/golang/lint/golint + +install: +- dep ensure + +script: +- gofmt -l $GOFILES +- go build $GOPKGS +- go test -v $GOPKGS +- golint -set_exit_status $GOPKGS diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..88c1561 --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,15 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + branch = "v2" + name = "gopkg.in/yaml.v2" + packages = ["."] + revision = "d670f9405373e636a5a2765eea47fac0c9bc91a4" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "c39e9119cc91080f9178c39214d6ca06156205351dec2523319554ee3669537e" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..af5215d --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,3 @@ +[[constraint]] + branch = "v2" + name = "gopkg.in/yaml.v2"