Skip to content

Commit

Permalink
Use my new snapshot library (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Nov 21, 2024
1 parent d0dfd28 commit 14443ca
Show file tree
Hide file tree
Showing 19 changed files with 25 additions and 48 deletions.
27 changes: 5 additions & 22 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"io"
"math/rand/v2"
"os"
"path/filepath"
"slices"
"testing"

"github.com/FollowTheProcess/cli"
"github.com/FollowTheProcess/snapshot"
"github.com/FollowTheProcess/test"
)

Expand Down Expand Up @@ -403,7 +403,6 @@ func TestHelp(t *testing.T) {

tests := []struct {
name string // Identifier of the test case
golden string // Name of the file containing expected output
options []cli.Option // Options to apply to the command
wantErr bool // Whether we want an error
}{
Expand All @@ -413,7 +412,6 @@ func TestHelp(t *testing.T) {
cli.OverrideArgs([]string{"--help"}),
cli.Run(func(cmd *cli.Command, args []string) error { return nil }),
},
golden: "default-help.txt",
wantErr: false,
},
{
Expand All @@ -422,7 +420,6 @@ func TestHelp(t *testing.T) {
cli.OverrideArgs([]string{"-h"}),
cli.Run(func(cmd *cli.Command, args []string) error { return nil }),
},
golden: "default-help.txt",
wantErr: false,
},
{
Expand All @@ -433,7 +430,6 @@ func TestHelp(t *testing.T) {
cli.Example("Do a different thing", "test do thing --different"),
cli.Run(func(cmd *cli.Command, args []string) error { return nil }),
},
golden: "with-examples.txt",
wantErr: false,
},
{
Expand All @@ -444,7 +440,6 @@ func TestHelp(t *testing.T) {
cli.Arg("dest", "Destination to copy to", "./dest"), // This one is optional
cli.Run(func(cmd *cli.Command, args []string) error { return nil }),
},
golden: "with-named-arguments.txt",
wantErr: false,
},
{
Expand All @@ -456,7 +451,6 @@ func TestHelp(t *testing.T) {
cli.Flag(new(cli.FlagCount), "verbosity", 'v', 0, "Increase the verbosity level"),
cli.Run(func(cmd *cli.Command, args []string) error { return nil }),
},
golden: "with-verbosity-count.txt",
wantErr: false,
},
{
Expand All @@ -467,7 +461,6 @@ func TestHelp(t *testing.T) {
cli.Long("A longer, probably multiline description"),
cli.Run(func(cmd *cli.Command, args []string) error { return nil }),
},
golden: "full.txt",
wantErr: false,
},
{
Expand All @@ -476,7 +469,6 @@ func TestHelp(t *testing.T) {
cli.OverrideArgs([]string{"--help"}),
cli.Run(func(cmd *cli.Command, args []string) error { return nil }),
},
golden: "no-about.txt",
wantErr: false,
},
{
Expand All @@ -487,7 +479,6 @@ func TestHelp(t *testing.T) {
cli.Long("A longer, probably multiline description"),
cli.SubCommands(sub1, sub2),
},
golden: "subcommands.txt",
wantErr: false,
},
{
Expand All @@ -498,7 +489,6 @@ func TestHelp(t *testing.T) {
cli.Long("A longer, probably multiline description"),
cli.SubCommands(sub1, sub2, sub3),
},
golden: "subcommands-different-lengths.txt",
wantErr: false,
},
{
Expand All @@ -511,7 +501,6 @@ func TestHelp(t *testing.T) {
cli.Flag(new(bool), "delete", 'd', false, "Delete something"),
cli.Flag(new(int), "count", cli.NoShortHand, -1, "Count something"),
},
golden: "subcommands-flags.txt",
wantErr: false,
},
}
Expand All @@ -521,11 +510,11 @@ func TestHelp(t *testing.T) {
// Force no colour in tests
t.Setenv("NO_COLOR", "true")

snap := snapshot.New(t, snapshot.Update(*update))

stderr := &bytes.Buffer{}
stdout := &bytes.Buffer{}

golden := filepath.Join(test.Data(t), "TestHelp", tt.golden)

// Test specific overrides to the options in the table
options := []cli.Option{cli.Stdout(stdout), cli.Stderr(stderr)}

Expand All @@ -540,17 +529,11 @@ func TestHelp(t *testing.T) {
fmt.Printf("DEBUG\n_____\n\n%s\n", stderr.String())
}

if *update {
t.Logf("Updating %s\n", golden)
err := os.WriteFile(golden, stderr.Bytes(), os.ModePerm)
test.Ok(t, err)
}

// Should have no output to stdout
test.Equal(t, stdout.String(), "")

// --help output should be as per the golden file
test.File(t, stderr.String(), golden)
// --help output should be as per the snapshot
snap.Snap(stderr.String())
})
}
}
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module github.com/FollowTheProcess/cli

go 1.23

require github.com/FollowTheProcess/test v0.17.1
require (
github.com/FollowTheProcess/snapshot v0.1.0
github.com/FollowTheProcess/test v0.17.1
)

require github.com/google/go-cmp v0.6.0 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/FollowTheProcess/snapshot v0.1.0 h1:G5tWpBXVre6cTrmN1OWZLiTnqEi9IKLiewtQQYOnQqc=
github.com/FollowTheProcess/snapshot v0.1.0/go.mod h1:sJv2oq83QK5Yj6+JVts8fQIaAJjQfsxN/WtGqX8oKIg=
github.com/FollowTheProcess/test v0.17.1 h1:j4TkMqzxvYoyAP9alaTNPgKOPUJHOBCs0z4fNJb7Kr0=
github.com/FollowTheProcess/test v0.17.1/go.mod h1:LlRdAk8bwBZ5kP10xHOcOTknNUrHU347IH7RgAm2Dgs=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
Expand Down
18 changes: 4 additions & 14 deletions internal/flag/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package flag_test
import (
goflag "flag"
"fmt"
"os"
"path/filepath"
"slices"
"testing"

"github.com/FollowTheProcess/cli/internal/flag"
"github.com/FollowTheProcess/snapshot"
"github.com/FollowTheProcess/test"
)

Expand Down Expand Up @@ -1310,7 +1309,6 @@ func TestUsage(t *testing.T) {
tests := []struct {
newSet func(t *testing.T) *flag.Set // Function to build the flag set under test
name string // Name of the test case
golden string // Name of the file containing expected output
}{
{
name: "simple",
Expand All @@ -1332,7 +1330,6 @@ func TestUsage(t *testing.T) {

return set
},
golden: "simple.txt",
},
{
name: "no shorthand",
Expand Down Expand Up @@ -1360,7 +1357,6 @@ func TestUsage(t *testing.T) {

return set
},
golden: "no-shorthand.txt",
},
{
name: "full",
Expand Down Expand Up @@ -1400,16 +1396,16 @@ func TestUsage(t *testing.T) {

return set
},
golden: "full.txt",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Force no colour in tests
t.Setenv("NO_COLOR", "true")

snap := snapshot.New(t, snapshot.Update(*update))
set := tt.newSet(t)
golden := filepath.Join(test.Data(t), "TestUsage", tt.golden)

got, err := set.Usage()
test.Ok(t, err)
Expand All @@ -1418,13 +1414,7 @@ func TestUsage(t *testing.T) {
fmt.Printf("DEBUG (%s)\n_____\n\n%s\n", tt.name, got)
}

if *update {
t.Logf("Updating %s\n", golden)
err := os.WriteFile(golden, []byte(got), os.ModePerm)
test.Ok(t, err)
}

test.File(t, got, golden)
snap.Snap(got)
})
}
}
Expand Down
File renamed without changes.
File renamed without changes.
14 changes: 3 additions & 11 deletions internal/table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"bytes"
"flag"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/FollowTheProcess/cli/internal/table"
"github.com/FollowTheProcess/snapshot"
"github.com/FollowTheProcess/test"
)

Expand All @@ -18,6 +17,7 @@ var (
)

func TestTable(t *testing.T) {
snap := snapshot.New(t, snapshot.Update(*update))
buf := &bytes.Buffer{}

tab := table.New(buf)
Expand All @@ -29,17 +29,9 @@ func TestTable(t *testing.T) {
err := tab.Flush()
test.Ok(t, err)

file := filepath.Join(test.Data(t), "table.txt")

if *debug {
fmt.Printf("DEBUG (%s)\n_____\n\n%s\n", "TestTable", buf.String())
}

if *update {
t.Logf("Updating %s\n", file)
err := os.WriteFile(file, buf.Bytes(), os.ModePerm)
test.Ok(t, err)
}

test.File(t, buf.String(), file)
snap.Snap(buf.String())
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions testdata/snapshots/TestHelp/with_no_description.snap.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
A placeholder for something cool

Usage: test [OPTIONS] ARGS...

Options:
-h --help bool Show help for test
-V --version bool Show version info for test
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 14443ca

Please sign in to comment.