Skip to content

Commit

Permalink
Merge pull request #1 from rebuy-de/initial-pr
Browse files Browse the repository at this point in the history
Initial PR
  • Loading branch information
svenwltr authored Feb 14, 2018
2 parents cf2f12a + 5432815 commit 83f49db
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
23 changes: 23 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[constraint]]
branch = "v2"
name = "gopkg.in/yaml.v2"
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
34 changes: 34 additions & 0 deletions cmdutil/exit.go
Original file line number Diff line number Diff line change
@@ -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
}
}
65 changes: 65 additions & 0 deletions testutil/golden.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 83f49db

Please sign in to comment.