diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml new file mode 100644 index 0000000..25442a3 --- /dev/null +++ b/.github/workflows/testing.yml @@ -0,0 +1,76 @@ +name: Testing + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + release: + types: + - created + +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v2 + - name: golangci-lint + uses: golangci/golangci-lint-action@v1 + with: + version: v1.27 + + test: + name: Test + runs-on: ubuntu-latest + needs: [lint] + + services: + postgres: + image: postgres:9.6-alpine + ports: + - "5432" + env: + POSTGRES_USER: test + POSTGRES_PASSWORD: test + POSTGRES_DB: test + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + mysql: + image: mysql:5.7 + ports: + - "3306" + env: + MYSQL_ROOT_PASSWORD: test + MYSQL_DATABASE: test + MYSQL_USER: test + MYSQL_PASSWORD: test + options: >- + --health-cmd "mysqladmin ping -h 127.0.0.1 -u $$MYSQL_USER --password=$$MYSQL_PASSWORD" + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + steps: + - name: Set up Go + uses: actions/setup-go@v2 + - name: Check out code + uses: actions/checkout@v2 + - name: Run tests + if: success() + run: go test -cover ./... -coverprofile=coverage.txt -covermode=atomic + env: + TEST_POSTGRES: postgres://test:test@localhost:${{ job.services.postgres.ports[5432] }}/?sslmode=disable + TEST_MYSQL: root:test@tcp(localhost:${{ job.services.mysql.ports[3306] }})/?charset=utf8 + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v1 + if: success() + with: + file: ./coverage.txt + fail_ci_if_error: false diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..ad50dd7 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,15 @@ +# See https://golangci-lint.run/usage/configuration/#config-file for more information +run: + timeout: 5m +linters: + disable-all: true + enable: + - gofmt + - golint + - goimports + fast: false +linters-settings: + gofmt: + simplify: false +issues: + exclude-use-default: false diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 2b6d978..0000000 --- a/.travis.yml +++ /dev/null @@ -1,41 +0,0 @@ -sudo: required -language: go - -go: - - "1.13" - - "1.14" - - "stable" - -services: - - mysql - - postgresql - -addons: - postgresql: "9.6" - -env: - - TEST_POSTGRES="postgres://postgres@localhost:5432?sslmode=disable" - TEST_MYSQL="root@tcp(localhost:3306)/" - -install: - - go get -u github.com/go-playground/overalls - - go get -u github.com/goreleaser/goreleaser - - make deps - -script: - - make test - -after_success: - - $GOPATH/bin/overalls -project=github.com/hellofresh/klepto -covermode=count - - if [ "$TRAVIS_SECURE_ENV_VARS" == "true" ]; then bash <(curl -s https://codecov.io/bash) -f overalls.coverprofile; fi - -# calls goreleaser -deploy: - - provider: script - # run the clean up as tests install some extra-tools required for coverage and linting - skip_cleanup: false - script: curl -sL http://git.io/goreleaser | bash - on: - tags: true - # build only on stable go version - go: "stable" diff --git a/Makefile b/Makefile index a6bb17a..ca5ac26 100644 --- a/Makefile +++ b/Makefile @@ -3,55 +3,15 @@ OK_COLOR=\033[32;01m ERROR_COLOR=\033[31;01m WARN_COLOR=\033[33;01m -# Space separated patterns of packages to skip in list, test, format. -IGNORED_PACKAGES := /vendor/ +.PHONY: all test build -.PHONY: all clean deps build - -all: clean deps build - -deps: - @echo "$(OK_COLOR)==> Installing dependencies$(NO_COLOR)" - @go get -u golang.org/x/lint/golint - @go mod vendor +all: test build # Builds the project build: @echo "$(OK_COLOR)==> Building... $(NO_COLOR)" @goreleaser --snapshot --rm-dist --skip-validate -test: lint format vet +test: @echo "$(OK_COLOR)==> Running tests$(NO_COLOR)" @CGO_ENABLED=0 go test -cover ./... -coverprofile=coverage.txt -covermode=atomic - -lint: - @echo "$(OK_COLOR)==> Checking code style with 'golint' tool$(NO_COLOR)" - @go list ./... | xargs -n 1 golint -set_exit_status - -format: - @echo "$(OK_COLOR)==> Checking code formating with 'gofmt' tool$(NO_COLOR)" - @gofmt -l -s cmd pkg | grep ".*\.go"; if [ "$$?" = "0" ]; then exit 1; fi - -vet: - @echo "$(OK_COLOR)==> Checking code correctness with 'go vet' tool$(NO_COLOR)" - @go vet ./... - -test-docker: - docker-compose up -d - @TEST_POSTGRES="postgres://hello:fresh@localhost:8050/klepto?sslmode=disable" \ - TEST_MYSQL="root:hellofresh@tcp(localhost:8052)/" \ - /bin/sh -c "./build/test.sh $(allpackages)" - -# Cleans our project: deletes binaries -clean: - @echo "$(OK_COLOR)==> Cleaning project$(NO_COLOR)" - @go clean - @rm -rf dist - -# cd into the GOPATH to workaround ./... not following symlinks -_allpackages = $(shell ( go list ./... 2>&1 1>&3 | \ - grep -v -e "^$$" $(addprefix -e ,$(IGNORED_PACKAGES)) 1>&2 ) 3>&1 | \ - grep -v -e "^$$" $(addprefix -e ,$(IGNORED_PACKAGES))) - -# memoize allpackages, so that it's executed only once and only if used -allpackages = $(if $(__allpackages),,$(eval __allpackages := $$(_allpackages)))$(__allpackages) diff --git a/features/mysql_test.go b/features/mysql_test.go index b3a29bd..ce76bc0 100644 --- a/features/mysql_test.go +++ b/features/mysql_test.go @@ -39,11 +39,17 @@ func (s *MysqlTestSuite) TestExample() { rdr, err := reader.Connect(reader.ConnOpts{DSN: readDSN, Timeout: s.timeout}) s.Require().NoError(err, "Unable to create reader") - defer rdr.Close() + defer func() { + err := rdr.Close() + s.Assert().NoError(err) + }() dmp, err := dumper.NewDumper(dumper.ConnOpts{DSN: dumpDSN}, rdr) s.Require().NoError(err, "Unable to create dumper") - defer dmp.Close() + defer func() { + err := dmp.Close() + s.Assert().NoError(err) + }() done := make(chan struct{}) defer close(done) @@ -75,7 +81,8 @@ func (s *MysqlTestSuite) TearDownSuite() { s.dropDatabase(db) } - s.rootConnection.Close() + err := s.rootConnection.Close() + s.Assert().NoError(err) } func (s *MysqlTestSuite) createDatabase(name string) string { @@ -102,7 +109,10 @@ func (s *MysqlTestSuite) loadFixture(dsn string, file string) { s.Require().NoError(err, "Unable to load fixture file") conn, err := sql.Open("mysql", dsn) - defer conn.Close() + defer func() { + err := conn.Close() + s.Assert().NoError(err) + }() s.Require().NoError(err, "Unable to open db connection to load fixture") _, err = conn.Exec(string(data)) @@ -112,11 +122,17 @@ func (s *MysqlTestSuite) loadFixture(dsn string, file string) { func (s *MysqlTestSuite) assertDatabaseAreTheSame(expectedDSN string, dumpDSN string) { sourceConn, err := sql.Open("mysql", expectedDSN) s.Require().NoError(err, "Unable to connect to source db") - defer sourceConn.Close() + defer func() { + err := sourceConn.Close() + s.Assert().NoError(err) + }() targetConn, err := sql.Open("mysql", dumpDSN) s.Require().NoError(err, "Unable to connect to target db") - defer targetConn.Close() + defer func() { + err := targetConn.Close() + s.Assert().NoError(err) + }() tables := s.fetchTableRowCount(sourceConn) s.Require().Equal(tables, s.fetchTableRowCount(targetConn)) @@ -142,7 +158,7 @@ func (s *MysqlTestSuite) fetchTableRowCount(db *sql.DB) []tableInfo { s.Require().NoError(err, "Unable to fetch table info") defer tableRows.Close() - tables := []tableInfo{} + var tables []tableInfo for tableRows.Next() { table := tableInfo{} @@ -163,11 +179,17 @@ func (s *MysqlTestSuite) compareTable(source *sql.DB, target *sql.DB, table stri expectedRows, err := source.Query(query) assert.NoError(err, "Unable to query source table") - defer expectedRows.Close() + defer func() { + err := expectedRows.Close() + s.Assert().NoError(err) + }() rows, err := target.Query(query) assert.NoError(err, "Unable to query target table") - defer rows.Close() + defer func() { + err := rows.Close() + s.Assert().NoError(err) + }() for expectedRows.Next() { assert.True(rows.Next(), "target row mismatch") diff --git a/go.mod b/go.mod index 5a64172..3ad3a5a 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/hellofresh/klepto -go 1.13 +go 1.14 require ( github.com/BurntSushi/toml v0.3.1 diff --git a/go.sum b/go.sum index 1be43ca..65e0116 100644 --- a/go.sum +++ b/go.sum @@ -171,6 +171,7 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 h1:XQyxROzUlZH+WIQwySDgnISgOivlhjIEwaQaJEJrrN0= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -207,6 +208,7 @@ golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384 h1:TFlARGu6Czu1z7q93HTxcP1P+/ZFC/IKythI5RzrnRg= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=