Skip to content

Commit

Permalink
Move main cmd to ./cmd
Browse files Browse the repository at this point in the history
So that it can be run as a library.
  • Loading branch information
dnephin committed Oct 10, 2020
1 parent 915824d commit 243e57f
Show file tree
Hide file tree
Showing 25 changed files with 55 additions and 46 deletions.
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ builds:
- amd64
- arm64
env: [CGO_ENABLED=0]
ldflags: ["-s -w -X gotest.tools/gotestsum/cmd.version={{.Version}}"]

checksum:
name_template: '{{ .ProjectName }}-{{ .Version }}-checksums.txt'
2 changes: 1 addition & 1 deletion flags.go → cmd/flags.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"encoding/csv"
Expand Down
2 changes: 1 addition & 1 deletion flags_test.go → cmd/flags_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion handler.go → cmd/handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"fmt"
Expand Down
6 changes: 3 additions & 3 deletions handler_test.go → cmd/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"bytes"
Expand Down Expand Up @@ -35,7 +35,7 @@ func TestPostRunHook(t *testing.T) {

func newExecFromTestData(t *testing.T) *testjson.Execution {
t.Helper()
f, err := os.Open("testjson/testdata/go-test-json.out")
f, err := os.Open("../testjson/testdata/go-test-json.out")
assert.NilError(t, err)
defer f.Close() // nolint: errcheck

Expand All @@ -58,7 +58,7 @@ func TestEventHandler_Event_WithMissingActionFail(t *testing.T) {
errBuf := new(bytes.Buffer)
format := testjson.NewEventFormatter(errBuf, "testname")

source := golden.Get(t, "../testjson/testdata/go-test-json-missing-test-fail.out")
source := golden.Get(t, "../../testjson/testdata/go-test-json-missing-test-fail.out")
cfg := testjson.ScanConfig{
Stdout: bytes.NewReader(source),
Handler: &eventHandler{jsonFile: buf, formatter: format},
Expand Down
File renamed without changes.
38 changes: 5 additions & 33 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"context"
Expand All @@ -12,41 +12,13 @@ import (
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/spf13/pflag"
"gotest.tools/gotestsum/cmd"
"gotest.tools/gotestsum/cmd/tool"
"gotest.tools/gotestsum/log"
"gotest.tools/gotestsum/testjson"
)

var version = "master"

func main() {
err := route(os.Args)
switch err.(type) {
case nil:
return
case *exec.ExitError:
// go test should already report the error to stderr, exit with
// the same status code
os.Exit(exitCodeWithDefault(err))
default:
log.Error(err.Error())
os.Exit(3)
}
}

func route(args []string) error {
name := args[0]
next, rest := cmd.Next(args[1:])
switch next {
case "tool":
return tool.Run(name+" "+next, rest)
default:
return runMain(name, args[1:])
}
}
var version = "dev"

func runMain(name string, args []string) error {
func Run(name string, args []string) error {
flags, opts := setupFlags(name)
switch err := flags.Parse(args); {
case err == pflag.ErrHelp:
Expand Down Expand Up @@ -378,9 +350,9 @@ func startGoTest(ctx context.Context, args []string) (proc, error) {
return p, nil
}

// GetExitCode returns the ExitStatus of a process from the error returned by
// ExitCodeWithDefault returns the ExitStatus of a process from the error returned by
// exec.Run(). If the exit status is not available an error is returned.
func exitCodeWithDefault(err error) int {
func ExitCodeWithDefault(err error) int {
if err == nil {
return 0
}
Expand Down
6 changes: 3 additions & 3 deletions main_e2e_test.go → cmd/main_e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"bytes"
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestE2E_RerunFails(t *testing.T) {
args: []string{
"-f=testname",
"--rerun-fails=2",
"--packages=./testjson/internal/broken",
"--packages=../testjson/internal/broken",
"--", "-count=1", "-tags=stubpkg",
},
expectedErr: "rerun aborted because previous run had errors",
Expand Down Expand Up @@ -182,7 +182,7 @@ func compileBinary(t *testing.T) string {
assert.NilError(t, err)

path := filepath.Join(tmpDir, "gotestsum")
result := icmd.RunCommand("go", "build", "-o", path, ".")
result := icmd.RunCommand("go", "build", "-o", path, "..")
result.Assert(t, icmd.Success)
return path
})
Expand Down
2 changes: 1 addition & 1 deletion main_test.go → cmd/main_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"bytes"
Expand Down
4 changes: 2 additions & 2 deletions rerunfails.go → cmd/rerunfails.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"context"
Expand Down Expand Up @@ -89,7 +89,7 @@ func hasErrors(err error, exec *testjson.Execution) error {
case len(exec.Errors()) > 0:
return fmt.Errorf("rerun aborted because previous run had errors")
// Exit code 0 and 1 are expected.
case exitCodeWithDefault(err) > 1:
case ExitCodeWithDefault(err) > 1:
return fmt.Errorf("unexpected go test exit code: %v", err)
default:
return nil
Expand Down
2 changes: 1 addition & 1 deletion rerunfails_test.go → cmd/rerunfails_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"bytes"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"os"
"os/exec"

"gotest.tools/gotestsum/cmd"
"gotest.tools/gotestsum/cmd/tool"
"gotest.tools/gotestsum/log"
)

func main() {
err := route(os.Args)
switch err.(type) {
case nil:
return
case *exec.ExitError:
// go test should already report the error to stderr, exit with
// the same status code
os.Exit(cmd.ExitCodeWithDefault(err))
default:
log.Error(err.Error())
os.Exit(3)
}
}

func route(args []string) error {
name := args[0]
next, rest := cmd.Next(args[1:])
switch next {
case "tool":
return tool.Run(name+" "+next, rest)
default:
return cmd.Run(name, args[1:])
}
}

0 comments on commit 243e57f

Please sign in to comment.