Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ jobs:
${{ runner.os }}-go-cache
- name: Run gofmt
run: gofmt -d -e . 2>&1 | tee outfile && test -z "$(cat outfile)" && rm outfile
- name: Run staticcheck
- name: Run golangci-lint
if: matrix.go-version == 'stable'
uses: dominikh/staticcheck-action@v1.4.0
uses: golangci/golangci-lint-action@v8
with:
version: "latest"
install-go: false
cache-key: ${{ matrix.go }}

version: v2.1
- name: Run go vet
run: |
go vet ./...
Expand Down
36 changes: 24 additions & 12 deletions cmd/godog/internal/cmd_root.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package internal

import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"log"

"github.com/cucumber/godog/internal/flags"
)
Expand All @@ -23,7 +25,10 @@ and contain buildable go source.`,
RunE: runRootCmd,
}

bindRootCmdFlags(rootCmd.Flags())
err := bindRootCmdFlags(rootCmd.Flags())
if err != nil {
log.Println(err)
}

return rootCmd
}
Expand All @@ -44,22 +49,29 @@ func runRootCmd(cmd *cobra.Command, args []string) error {
return runCmdRunFunc(cmd, args)
}

func bindRootCmdFlags(flagSet *pflag.FlagSet) {
func bindRootCmdFlags(flagSet *pflag.FlagSet) error {
flagSet.StringVarP(&output, "output", "o", "", "compiles the test runner to the named file")
flagSet.BoolVar(&version, "version", false, "show current version")

flags.BindRunCmdFlags("", flagSet, &opts)

// Since using the root command directly is deprecated.
// All flags will be hidden
flagSet.MarkHidden("output")
flagSet.MarkHidden("version")
flagSet.MarkHidden("no-colors")
flagSet.MarkHidden("concurrency")
flagSet.MarkHidden("tags")
flagSet.MarkHidden("format")
flagSet.MarkHidden("definitions")
flagSet.MarkHidden("stop-on-failure")
flagSet.MarkHidden("strict")
flagSet.MarkHidden("random")
for _, name := range []string{
"output",
"version",
"no-colors",
"concurrency",
"tags",
"format",
"definitions",
"stop-on-failure",
"strict",
"random",
} {
if err := flagSet.MarkHidden(name); err != nil {
return fmt.Errorf("failed to hide deprecated root command flag %q: %w", name, err)
}
}
return nil
}
8 changes: 7 additions & 1 deletion cmd/godog/internal/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -69,7 +70,12 @@ func buildAndRunGodog(args []string) (err error) {
return err
}

defer os.Remove(bin)
defer func(name string) {
err = os.Remove(name)
if err != nil {
log.Printf("failed to remove temporary build file: %v", err)
}
}(bin)

return runGodog(bin, args)
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/godog/internal/cmd_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"fmt"
"log"
"os"

"github.com/spf13/cobra"
Expand All @@ -22,5 +23,7 @@ func CreateVersionCmd() cobra.Command {
}

func versionCmdRunFunc(cmd *cobra.Command, args []string) {
fmt.Fprintln(os.Stdout, "Godog version is:", godog.Version)
if _, err := fmt.Fprintln(os.Stdout, "Godog version is:", godog.Version); err != nil {
log.Fatalf("failed to print Godog version: %v", err)
}
}
5 changes: 4 additions & 1 deletion colors/no_colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ loop:
break loop
}
if c1 != 0x1b {
fmt.Fprint(w.out, string(c1))
_, err = fmt.Fprint(w.out, string(c1))
if err != nil {
break loop
}
continue
}
c2, _, err := er.ReadRune()
Expand Down
21 changes: 16 additions & 5 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"io"
"log"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -207,15 +208,25 @@ func usage(set *flag.FlagSet, w io.Writer) func() {
}

// --- GENERAL ---
fmt.Fprintln(w, colors.Yellow("Usage:"))
fmt.Fprintf(w, s(2)+"go test [options]\n\n")
if _, err := fmt.Fprintln(w, colors.Yellow("Usage:")); err != nil {
log.Fatal(err)
}
if _, err := fmt.Fprintf(w, s(2)+"go test [options]\n\n"); err != nil {
log.Fatal(err)
}

// --- OPTIONS ---
fmt.Fprintln(w, colors.Yellow("Options:"))
if _, err := fmt.Fprintln(w, colors.Yellow("Options:")); err != nil {
log.Fatal(err)
}
for _, f := range list {
fmt.Fprintln(w, opt(f.name(), f.descr))
if _, err := fmt.Fprintln(w, opt(f.name(), f.descr)); err != nil {
log.Fatal(err)
}
}
if _, err := fmt.Fprintln(w, ""); err != nil {
log.Fatal(err)
}
fmt.Fprintln(w, "")
}
}

Expand Down
5 changes: 4 additions & 1 deletion flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func TestBindFlagsShouldRespectFlagOverrides(t *testing.T) {

BindFlags("optOverrides.", &flagSet, &opts)

flagSet.Parse([]string{
err := flagSet.Parse([]string{
"--optOverrides.format=junit",
"--optOverrides.tags=test2",
"--optOverrides.concurrency=3",
Expand All @@ -176,6 +176,9 @@ func TestBindFlagsShouldRespectFlagOverrides(t *testing.T) {
"--optOverrides.no-colors=false",
"--optOverrides.random=2",
})
if err != nil {
t.Fatalf("failed to parse flags: %v", err)
}

if opts.Format != "junit" {
t.Fatalf("expected Format: junit, but it was: %s", opts.Format)
Expand Down
14 changes: 11 additions & 3 deletions fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type storageFormatter interface {
// suite name and io.Writer to record output
type FormatterFunc = formatters.FormatterFunc

func printStepDefinitions(steps []*models.StepDefinition, w io.Writer) {
func printStepDefinitions(steps []*models.StepDefinition, w io.Writer) error {
var longest int
for _, def := range steps {
n := utf8.RuneCountInString(def.Expr.String())
Expand All @@ -65,14 +65,22 @@ func printStepDefinitions(steps []*models.StepDefinition, w io.Writer) {
n := utf8.RuneCountInString(def.Expr.String())
location := internal_fmt.DefinitionID(def)
spaces := strings.Repeat(" ", longest-n)
fmt.Fprintln(w,
_, err := fmt.Fprintln(w,
colors.Yellow(def.Expr.String())+spaces,
colors.Bold(colors.Black)("# "+location))
if err != nil {
return fmt.Errorf("failed to print step definition: %w", err)
}
}

if len(steps) == 0 {
fmt.Fprintln(w, "there were no contexts registered, could not find any step definition..")
_, err := fmt.Fprintln(w, "there were no contexts registered, could not find any step definition..")
if err != nil {
return fmt.Errorf("failed to print no any step definition message: %w", err)
}
}

return nil
}

// NewBaseFmt creates a new base formatter.
Expand Down
24 changes: 19 additions & 5 deletions internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -115,7 +116,12 @@ func Build(bin string) error {
if err != nil {
return err
}
defer os.Remove(pathTemp)
defer func(name string) {
err = os.Remove(name)
if err != nil {
log.Printf("failed to remove temporary file %s: %s", name, err)
}
}(pathTemp)
}

workdir := ""
Expand All @@ -137,7 +143,12 @@ func Build(bin string) error {
if err != nil {
return fmt.Errorf("failed to compile tested package: %s, reason: %v, output: %s", abs, err, string(testOutput))
}
defer os.Remove(temp)
defer func(name string) {
err = os.Remove(name)
if err != nil {
log.Printf("failed to remove temporary file %s: %v", name, err)
}
}(temp)

// extract go-build temporary directory as our workdir
linesOut := strings.Split(strings.TrimSpace(string(testOutput)), "\n")
Expand Down Expand Up @@ -170,7 +181,12 @@ func Build(bin string) error {
return fmt.Errorf("expected WORK dir: %s to be directory", workdir)
}
testdir = filepath.Join(workdir, "b001")
defer os.RemoveAll(workdir)
defer func(path string) {
err := os.RemoveAll(path)
if err != nil {
log.Printf("failed to remove files in %s: %v", path, err)
}
}(workdir)

// replace _testmain.go file with our own
testmain := filepath.Join(testdir, "_testmain.go")
Expand Down Expand Up @@ -373,8 +389,6 @@ func buildTestMain(pkg *build.Package) ([]byte, error) {

importPath = parseImport(pkg.ImportPath, pkg.Root)
name = pkg.Name
} else {
name = "main"
}

data := struct {
Expand Down
6 changes: 5 additions & 1 deletion internal/builder/builder_go113_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ func testWithVendoredGodogAndMod(t *testing.T) {
builderTC := builderTestCase{}

gopath := filepath.Join(os.TempDir(), t.Name(), "_gpc")
defer os.RemoveAll(gopath)
defer func() {
if err := os.RemoveAll(gopath); err != nil {
t.Fatal(err)
}
}()

builderTC.dir = filepath.Join(gopath, "src", "godogs")
builderTC.files = map[string]string{
Expand Down
6 changes: 5 additions & 1 deletion internal/builder/builder_go_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ func testInsideGopath(t *testing.T) {
builderTC := builderTestCase{}

gopath := filepath.Join(os.TempDir(), t.Name(), "_gp")
defer os.RemoveAll(gopath)
defer func() {
if err := os.RemoveAll(gopath); err != nil {
t.Fatal(err)
}
}()

builderTC.dir = filepath.Join(gopath, "src", "godogs")
builderTC.files = map[string]string{
Expand Down
Loading
Loading