diff --git a/go.mod b/go.mod index a33532d55bd17b848dbf74acf35adf31c0c295d8..165a4298eb0f2073672c0ac46be8dbb9e9e9bdeb 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.22.3 require ( github.com/spf13/cobra v1.8.0 + github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.9.0 github.com/testcontainers/testcontainers-go v0.31.0 ) @@ -47,7 +48,6 @@ require ( github.com/shirou/gopsutil/v3 v3.23.12 // indirect github.com/shoenig/go-m1cpu v0.1.6 // indirect github.com/sirupsen/logrus v1.9.3 // indirect - github.com/spf13/pflag v1.0.5 // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/numcpus v0.6.1 // indirect github.com/yusufpapurcu/wmi v1.2.3 // indirect diff --git a/snok.go b/snok.go index 7a1328aa92b0966f29266762cb975b3e2130fb37..d3c90c5643ce1f20415cdd50e72dda082c991378 100644 --- a/snok.go +++ b/snok.go @@ -16,6 +16,7 @@ import ( "unicode" "github.com/spf13/cobra" + "github.com/spf13/pflag" "github.com/stretchr/testify/require" "github.com/testcontainers/testcontainers-go" ) @@ -23,7 +24,7 @@ import ( type test struct { Name string `json:"name,omitempty"` Args []string `json:"args,omitempty"` - Input *string `json:"input,omitempty"` // Todo: add support for URIs + Input *string `json:"input,omitempty"` Output *string `json:"output,omitempty"` ExpectError bool `json:"expectError,omitempty"` } @@ -72,6 +73,7 @@ func (ct *CommandTest) Run(t *testing.T) { } func (ct *CommandTest) testCmd(t *testing.T, cmd *cobra.Command, args []string) { + ct.RootCmd.ResetFlags() args = append(args, cmd.Name()) containers, exists := cmd.Annotations["containers"] if exists { @@ -85,6 +87,12 @@ func (ct *CommandTest) testCmd(t *testing.T, cmd *cobra.Command, args []string) require.NoError(t, err, "Malformed annotation: %s", annotation) for _, test := range tests { ct.executeTest(t, test, args) + cmd.Flags().VisitAll(func(pf *pflag.Flag) { + if pf.Changed { + pf.Value.Set(pf.DefValue) + pf.Changed = false + } + }) } }) } @@ -118,7 +126,12 @@ func (ct *CommandTest) executeTest(t *testing.T, test test, args []string) { t.Run(test.Name, func(t *testing.T) { var input io.Reader if test.Input != nil { - input = strings.NewReader(*test.Input) + input = openURI(t, *test.Input) + defer func() { + if r, ok := input.(io.Closer); ok { + r.Close() + } + }() } output, err := ct.executeCmd(append(args[1:], test.Args...), input) if test.ExpectError { @@ -129,8 +142,7 @@ func (ct *CommandTest) executeTest(t *testing.T, test test, args []string) { output = strings.TrimRightFunc(output, unicode.IsSpace) if strings.HasPrefix(*test.Output, "/") && strings.HasSuffix(*test.Output, "/") { expr := (*test.Output)[1 : len(*test.Output)-1] - re, err := regexp.Compile(expr) - require.NoError(t, err, "Malformed regexp: %s", expr) + re := regexp.MustCompile(expr) require.True(t, re.MatchString(output), "Expected output to match %s, got %s", expr, output) } else { require.Equal(t, *test.Output, output, "Expected output %s, got %s", *test.Output, output) diff --git a/snok_test.go b/snok_test.go index b18c7994c509461fc0d17e9019a04bf730f0082f..e9f61643bb0e9c6425d3082b7d47ab188c107af5 100644 --- a/snok_test.go +++ b/snok_test.go @@ -103,10 +103,34 @@ var echoCmd = &cobra.Command{ "output": "dlrow olleh" }, { - "name": "Echo Input", + "name": "Echo Input (data)", "args": ["-r","The","rain"], - "input": "in Spain falls mainly on the plain", + "input": "data:,in Spain falls mainly on the plain", "output": "nialp eht no ylniam sllaf niapS ni niar ehT" + }, + { + "name": "Echo Input (base64)", + "args": ["test"], + "input": "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==", + "output": "test Hello, World!" + }, + { + "name": "Echo Input (file)", + "args": ["test"], + "input": "file:./test/hello.txt", + "output": "test Hello, World!" + }, + { + "name": "Echo Input (http)", + "args": ["test"], + "input": "https://www.example.com/", + "output": "/<html>/" + }, + { + "name": "Echo Input (ftp)", + "args": ["test"], + "input": "Hello, World!", + "output": "test Hello, World!" } ]`, },