Skip to content

Commit

Permalink
Version flag (#28)
Browse files Browse the repository at this point in the history
* cli version

* Fix linter
  • Loading branch information
hedhyw authored Jun 8, 2022
1 parent 4bd677e commit 349439a
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 15 deletions.
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
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
./bin/golangci-lint run
.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

Expand Down
5 changes: 4 additions & 1 deletion cmd/gherkingen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions cmd/gherkingen/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"os"
"testing"
)

func TestMainVersion(_ *testing.T) {
os.Args = append(os.Args, "-version")
main()
}
16 changes: 16 additions & 0 deletions internal/app/app.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 <flag> is provided
Then version is printed
Examples:
| <flag> |
| --version |
| -version |

Scenario: User specifies a file, but the file is not found
When inexistent <template> is provided
And <feature> is provided
Then the user receives an error
Examples:
| <feature> | <template> |
| app.feature | not_found |
17 changes: 12 additions & 5 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
)

// Run the application.
func Run(arguments []string, out io.Writer) (err error) {
func Run(arguments []string, out io.Writer, version string) (err error) {
flag.CommandLine.Init(flag.CommandLine.Name(), flag.ContinueOnError)
flag.CommandLine.SetOutput(out)

Expand All @@ -37,12 +37,12 @@ func Run(arguments []string, out io.Writer) (err error) {
false,
"The same calls to the generator always produces the same output",
)
help := flag.Bool(
helpCmd := flag.Bool(
"help",
false,
"print usage",
)
list := flag.Bool(
listCmd := flag.Bool(
"list",
false,
"list internal templates",
Expand All @@ -52,6 +52,11 @@ func Run(arguments []string, out io.Writer) (err error) {
"generated_test",
"name of the generated package",
)
versionCmd := flag.Bool(
"version",
false,
"print version",
)
if err = flag.CommandLine.Parse(arguments); err != nil {
return err
}
Expand All @@ -70,9 +75,11 @@ func Run(arguments []string, out io.Writer) (err error) {
}

switch {
case *list:
case *versionCmd:
return runVersion(out, version)
case *listCmd:
return runListTemplates(out)
case *help, inputFile == "":
case *helpCmd, inputFile == "":
return runHelp()
default:
return runGenerator(
Expand Down
53 changes: 52 additions & 1 deletion internal/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/hedhyw/gherkingen/pkg/v1/bdd"
)

const testVersion = "0.0.1"

func TestApplicationCommandLineTool(t *testing.T) {
f := bdd.NewFeature(t, "Application command line tool")

Expand Down Expand Up @@ -174,6 +176,55 @@ func TestApplicationCommandLineTool(t *testing.T) {
runApp(t, arguments, false)
})
})

f.Scenario("User wants to know version", func(_ *testing.T, f *bdd.Feature) {
type testCase struct {
Flag string `field:"<flag>"`
}

testCases := map[string]testCase{
"--version": {"--version"},
"-version": {"-version"},
}

f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
arguments := []string{}

f.When("<flag> is provided", func() {
arguments = append(arguments, tc.Flag)
})
f.Then("version is printed", func() {
out := runApp(t, arguments, true)
if !strings.Contains(out, testVersion) {
t.Fatalf("expected %q in %q", testVersion, out)
}
})
})
})

f.Scenario("User specifies a file, but the file is not found", func(_ *testing.T, f *bdd.Feature) {
type testCase struct {
Feature string `field:"<feature>"`
Template string `field:"<template>"`
}

testCases := map[string]testCase{
"app.feature_not_found": {"app.feature", "not_found"},
}

f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
arguments := []string{}
f.When("inexistent <template> is provided", func() {
arguments = append(arguments, "-template", tc.Template)
})
f.And("<feature> is provided", func() {
arguments = append(arguments, tc.Feature)
})
f.Then("the user receives an error", func() {
runApp(t, arguments, false)
})
})
})
}

func runApp(tb testing.TB, arguments []string, ok bool) string {
Expand All @@ -182,7 +233,7 @@ func runApp(tb testing.TB, arguments []string, ok bool) string {
flag.CommandLine = flag.NewFlagSet("", flag.PanicOnError)

var buf bytes.Buffer
err := app.Run(arguments, &buf)
err := app.Run(arguments, &buf, testVersion)

if ok == (err != nil) {
tb.Errorf("Assertion failed, ok: %t, err: %s", ok, err)
Expand Down
12 changes: 12 additions & 0 deletions internal/app/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package app

import (
"fmt"
"io"
)

func runVersion(w io.Writer, version string) error {
fmt.Fprintln(w, "github.com/hedhyw/gherkingen@"+version)

return nil
}
14 changes: 14 additions & 0 deletions internal/generator/golang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ func TestGenerateGo(t *testing.T) {
}
}

func TestGenerateGoFormattingFailed(t *testing.T) {
t.Parallel()

_, err := generator.Generate(model.GenerateArgs{
Format: model.FormatGo,
InputSource: exampleFeature,
TemplateSource: []byte("-"),
PackageName: "generated_test",
})
if err == nil {
t.Fatal(err)
}
}

func TestGenerateAssetTemplatesShouldNotFail(t *testing.T) {
t.Parallel()

Expand Down
14 changes: 14 additions & 0 deletions internal/generator/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,17 @@ func TestGenerateRaw(t *testing.T) {
})
}
}

func TestGenerateRaw_failed(t *testing.T) {
t.Parallel()

_, err := generator.Generate(model.GenerateArgs{
Format: model.FormatRaw,
InputSource: exampleFeature,
TemplateSource: []byte("{{"),
PackageName: "generated_test.go",
})
if err == nil {
t.Fatal(err)
}
}
10 changes: 5 additions & 5 deletions pkg/v1/bdd/bdd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestBDDTestCases(t *testing.T) {

var gotSum int

f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
f.TestCases(testCases, func(_ *testing.T, _ *bdd.Feature, tc testCase) {
gotSum += tc.Inc
})

Expand All @@ -42,19 +42,19 @@ func TestBDD(t *testing.T) {
called++
}

f.Rule("rule", func(t *testing.T, f *bdd.Feature) {
f.Background("background", func(t *testing.T, f *bdd.Feature) {
f.Rule("rule", func(_ *testing.T, f *bdd.Feature) {
f.Background("background", func(_ *testing.T, f *bdd.Feature) {
f.Then("then", inc)
})

f.Scenario("simple", func(t *testing.T, f *bdd.Feature) {
f.Scenario("simple", func(_ *testing.T, f *bdd.Feature) {
tc := struct {
Fn string `field:"<field>"`
}{
Fn: "FUNC",
}

f.Example("example", func(t *testing.T, f *bdd.Feature) {
f.Example("example", func(_ *testing.T, f *bdd.Feature) {
f.TestCase("testCase", tc, func(t *testing.T, f *bdd.Feature) {
f.Given("given <field> called", inc)
f.But("but <field> called", inc)
Expand Down

0 comments on commit 349439a

Please sign in to comment.