From 349439aca33acd76e92d32bf9c82b9accc271615 Mon Sep 17 00:00:00 2001 From: Maksym Kryvchun Date: Wed, 8 Jun 2022 19:18:19 +0300 Subject: [PATCH] Version flag (#28) * cli version * Fix linter --- Makefile | 15 +++++++-- cmd/gherkingen/main.go | 5 ++- cmd/gherkingen/main_test.go | 11 +++++++ internal/app/app.feature | 16 ++++++++++ internal/app/app.go | 17 +++++++--- internal/app/app_test.go | 53 ++++++++++++++++++++++++++++++- internal/app/version.go | 12 +++++++ internal/generator/golang_test.go | 14 ++++++++ internal/generator/raw_test.go | 14 ++++++++ pkg/v1/bdd/bdd_test.go | 10 +++--- 10 files changed, 152 insertions(+), 15 deletions(-) create mode 100644 cmd/gherkingen/main_test.go create mode 100644 internal/app/version.go diff --git a/Makefile b/Makefile index 5490055..ac2875f 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,14 @@ GOLANG_CI_LINT_VER:=v1.45.0 OUT_BIN?=${PWD}/bin/gherkingen -COVER_PACKAGES=${go list ./... | grep -Ev 'examples' | tr '\n' ','} +COVER_PACKAGES=${shell go list ./... | grep -Ev 'examples|model' | tr '\n' ','} +VERSION=${shell git describe --tags} build: - go build -o ${OUT_BIN} cmd/gherkingen/main.go + @echo "building ${VERSION}" + go build \ + -o ${OUT_BIN} \ + --ldflags "-s -w -X main.version=${VERSION}" \ + cmd/gherkingen/main.go .PHONY: build lint: bin/golangci-lint @@ -11,7 +16,11 @@ lint: bin/golangci-lint .PHONY: lint test: - go test -coverpkg=github.com/hedhyw/gherkingen/internal/app -covermode=count -coverprofile=coverage.out ./... + go test \ + -coverpkg=${COVER_PACKAGES} \ + -covermode=count \ + -coverprofile=coverage.out \ + ./... go tool cover -func=coverage.out .PHONY: test diff --git a/cmd/gherkingen/main.go b/cmd/gherkingen/main.go index 6c49793..f0ecc98 100644 --- a/cmd/gherkingen/main.go +++ b/cmd/gherkingen/main.go @@ -6,8 +6,11 @@ import ( "github.com/hedhyw/gherkingen/internal/app" ) +// Version will be set on build. +var version = "unknown" + func main() { - if err := app.Run(os.Args[1:], os.Stdout); err != nil { + if err := app.Run(os.Args[1:], os.Stdout, version); err != nil { // nolint: forbidigo // Command-line-tool. println(err.Error()) os.Exit(1) diff --git a/cmd/gherkingen/main_test.go b/cmd/gherkingen/main_test.go new file mode 100644 index 0000000..8d36647 --- /dev/null +++ b/cmd/gherkingen/main_test.go @@ -0,0 +1,11 @@ +package main + +import ( + "os" + "testing" +) + +func TestMainVersion(_ *testing.T) { + os.Args = append(os.Args, "-version") + main() +} diff --git a/internal/app/app.feature b/internal/app/app.feature index de3d6ce..1b42b3a 100644 --- a/internal/app/app.feature +++ b/internal/app/app.feature @@ -54,3 +54,19 @@ Feature: Application command line tool Scenario: User gives an invalid flag When flag -invalid is provided Then a generation failed + + Scenario: User wants to know version + When is provided + Then version is printed + Examples: + | | + | --version | + | -version | + + Scenario: User specifies a file, but the file is not found + When inexistent