Skip to content

Commit

Permalink
Add tests for version cli
Browse files Browse the repository at this point in the history
  • Loading branch information
marcofranssen committed Oct 14, 2021
1 parent 826de43 commit c010e6d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
8 changes: 6 additions & 2 deletions cmd/slsa-provenance/cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io"
"runtime"
"strings"
"text/tabwriter"
Expand All @@ -30,11 +31,14 @@ var (
)

// Version creates an instance of *ffcli.Command to print version info
func Version() *ffcli.Command {
func Version(w io.Writer) *ffcli.Command {
var (
flagset = flag.NewFlagSet("slsa-provenance version", flag.ExitOnError)
outJSON = flagset.Bool("json", false, "print JSON instead of text")
)

flagset.SetOutput(w)

return &ffcli.Command{
Name: "version",
ShortUsage: "slsa-provenance version",
Expand All @@ -51,7 +55,7 @@ func Version() *ffcli.Command {
res = j
}

fmt.Println(res)
fmt.Fprintln(w, res)
return nil
},
}
Expand Down
56 changes: 56 additions & 0 deletions cmd/slsa-provenance/cli/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cli_test

import (
"context"
"fmt"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/philips-labs/slsa-provenance-action/cmd/slsa-provenance/cli"
)

func TestVersionCliText(t *testing.T) {
assert := assert.New(t)

sb := strings.Builder{}
cli := cli.Version(&sb)

expected := fmt.Sprintf(`GitVersion: devel
GitCommit: unknown
GitTreeState: unknown
BuildDate: unknown
GoVersion: %s
Compiler: %s
Platform: %s/%s
`, runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH)

err := cli.ParseAndRun(context.Background(), nil)
assert.NoError(err)
assert.Equal(expected, sb.String())
}

func TestVersionCliJson(t *testing.T) {
assert := assert.New(t)

sb := strings.Builder{}
cli := cli.Version(&sb)

expected := fmt.Sprintf(`{
"git_version": "devel",
"git_commit": "unknown",
"git_tree_state": "unknown",
"build_date": "unknown",
"go_version": "%s",
"compiler": "%s",
"platform": "%s/%s"
}
`, runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH)

err := cli.ParseAndRun(context.Background(), []string{"-json"})
assert.NoError(err)
assert.Equal(expected, sb.String())
}
6 changes: 4 additions & 2 deletions cmd/slsa-provenance/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ import (
func main() {
rootFlagSet := flag.NewFlagSet("slsa-provenance", flag.ExitOnError)

v := cli.Version(os.Stdout)

app := &ffcli.Command{
Name: "slsa-provenance [flags] <subcommand>",
FlagSet: rootFlagSet,
Subcommands: []*ffcli.Command{
cli.Generate(os.Stdout),
cli.Version(),
v,
},
Exec: func(ctx context.Context, args []string) error {
fmt.Println("slsa-provenance")
fmt.Println()

return cli.Version().Exec(ctx, args)
return v.Exec(ctx, args)
},
}

Expand Down

0 comments on commit c010e6d

Please sign in to comment.