diff --git a/command_test.go b/command_test.go index 4ba8aaa..bc3932d 100644 --- a/command_test.go +++ b/command_test.go @@ -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" ) @@ -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 }{ @@ -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, }, { @@ -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, }, { @@ -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, }, { @@ -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, }, { @@ -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, }, { @@ -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, }, { @@ -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, }, { @@ -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, }, { @@ -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, }, { @@ -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, }, } @@ -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)} @@ -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()) }) } } diff --git a/go.mod b/go.mod index 390ad6e..9168a22 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 8167952..ce1d89f 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/flag/set_test.go b/internal/flag/set_test.go index ee34719..c43713b 100644 --- a/internal/flag/set_test.go +++ b/internal/flag/set_test.go @@ -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" ) @@ -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", @@ -1332,7 +1330,6 @@ func TestUsage(t *testing.T) { return set }, - golden: "simple.txt", }, { name: "no shorthand", @@ -1360,7 +1357,6 @@ func TestUsage(t *testing.T) { return set }, - golden: "no-shorthand.txt", }, { name: "full", @@ -1400,7 +1396,6 @@ func TestUsage(t *testing.T) { return set }, - golden: "full.txt", }, } @@ -1408,8 +1403,9 @@ func TestUsage(t *testing.T) { 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) @@ -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) }) } } diff --git a/internal/flag/testdata/TestUsage/full.txt b/internal/flag/testdata/snapshots/TestUsage/full.snap.txt old mode 100755 new mode 100644 similarity index 100% rename from internal/flag/testdata/TestUsage/full.txt rename to internal/flag/testdata/snapshots/TestUsage/full.snap.txt diff --git a/internal/flag/testdata/TestUsage/no-shorthand.txt b/internal/flag/testdata/snapshots/TestUsage/no_shorthand.snap.txt similarity index 100% rename from internal/flag/testdata/TestUsage/no-shorthand.txt rename to internal/flag/testdata/snapshots/TestUsage/no_shorthand.snap.txt diff --git a/internal/flag/testdata/TestUsage/simple.txt b/internal/flag/testdata/snapshots/TestUsage/simple.snap.txt similarity index 100% rename from internal/flag/testdata/TestUsage/simple.txt rename to internal/flag/testdata/snapshots/TestUsage/simple.snap.txt diff --git a/internal/table/table_test.go b/internal/table/table_test.go index a0bed1e..c7e6990 100644 --- a/internal/table/table_test.go +++ b/internal/table/table_test.go @@ -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" ) @@ -18,6 +17,7 @@ var ( ) func TestTable(t *testing.T) { + snap := snapshot.New(t, snapshot.Update(*update)) buf := &bytes.Buffer{} tab := table.New(buf) @@ -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()) } diff --git a/internal/table/testdata/table.txt b/internal/table/testdata/snapshots/TestTable.snap.txt similarity index 100% rename from internal/table/testdata/table.txt rename to internal/table/testdata/snapshots/TestTable.snap.txt diff --git a/testdata/TestHelp/default-help.txt b/testdata/snapshots/TestHelp/default.short.snap.txt similarity index 100% rename from testdata/TestHelp/default-help.txt rename to testdata/snapshots/TestHelp/default.short.snap.txt diff --git a/testdata/TestHelp/no-about.txt b/testdata/snapshots/TestHelp/default_long.snap.txt similarity index 100% rename from testdata/TestHelp/no-about.txt rename to testdata/snapshots/TestHelp/default_long.snap.txt diff --git a/testdata/TestHelp/subcommands-different-lengths.txt b/testdata/snapshots/TestHelp/subcommands_different_lengths.snap.txt old mode 100755 new mode 100644 similarity index 100% rename from testdata/TestHelp/subcommands-different-lengths.txt rename to testdata/snapshots/TestHelp/subcommands_different_lengths.snap.txt diff --git a/testdata/TestHelp/with-examples.txt b/testdata/snapshots/TestHelp/with_examples.snap.txt similarity index 100% rename from testdata/TestHelp/with-examples.txt rename to testdata/snapshots/TestHelp/with_examples.snap.txt diff --git a/testdata/TestHelp/full.txt b/testdata/snapshots/TestHelp/with_full_description.snap.txt similarity index 100% rename from testdata/TestHelp/full.txt rename to testdata/snapshots/TestHelp/with_full_description.snap.txt diff --git a/testdata/TestHelp/with-named-arguments.txt b/testdata/snapshots/TestHelp/with_named_arguments.snap.txt similarity index 100% rename from testdata/TestHelp/with-named-arguments.txt rename to testdata/snapshots/TestHelp/with_named_arguments.snap.txt diff --git a/testdata/snapshots/TestHelp/with_no_description.snap.txt b/testdata/snapshots/TestHelp/with_no_description.snap.txt new file mode 100644 index 0000000..2dd88a7 --- /dev/null +++ b/testdata/snapshots/TestHelp/with_no_description.snap.txt @@ -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 diff --git a/testdata/TestHelp/subcommands.txt b/testdata/snapshots/TestHelp/with_subcommands.snap.txt similarity index 100% rename from testdata/TestHelp/subcommands.txt rename to testdata/snapshots/TestHelp/with_subcommands.snap.txt diff --git a/testdata/TestHelp/subcommands-flags.txt b/testdata/snapshots/TestHelp/with_subcommands_and_flags.snap.txt similarity index 100% rename from testdata/TestHelp/subcommands-flags.txt rename to testdata/snapshots/TestHelp/with_subcommands_and_flags.snap.txt diff --git a/testdata/TestHelp/with-verbosity-count.txt b/testdata/snapshots/TestHelp/with_verbosity_count.snap.txt old mode 100755 new mode 100644 similarity index 100% rename from testdata/TestHelp/with-verbosity-count.txt rename to testdata/snapshots/TestHelp/with_verbosity_count.snap.txt