diff --git a/pkg/commands/service/auth/service_test.go b/pkg/commands/service/auth/service_test.go index da9ecbc1e..9bc9f55e3 100644 --- a/pkg/commands/service/auth/service_test.go +++ b/pkg/commands/service/auth/service_test.go @@ -1,86 +1,66 @@ package auth_test import ( - "bytes" "context" "errors" - "io" - "strings" "testing" "github.com/fastly/go-fastly/v13/fastly" - "github.com/fastly/cli/pkg/app" - "github.com/fastly/cli/pkg/global" + root "github.com/fastly/cli/pkg/commands/service" + sub "github.com/fastly/cli/pkg/commands/service/auth" "github.com/fastly/cli/pkg/mock" "github.com/fastly/cli/pkg/testutil" ) func TestServiceAuthCreate(t *testing.T) { - args := testutil.SplitArgs - scenarios := []struct { - args []string - api mock.API - wantError string - wantOutput string - }{ + scenarios := []testutil.CLIScenario{ { - args: args("service auth create"), - wantError: "error parsing arguments: required flag --user-id not provided", + Name: "missing required flag", + Args: "", + WantError: "error parsing arguments: required flag --user-id not provided", }, { - args: args("service auth create --user-id 123 --service-id 123"), - api: mock.API{CreateServiceAuthorizationFn: createServiceAuthError}, - wantError: errTest.Error(), + Name: "api failure", + Args: "--user-id 123 --service-id 123", + API: mock.API{CreateServiceAuthorizationFn: createServiceAuthError}, + WantError: errTest.Error(), }, { - args: args("service auth create --user-id 123 --service-id 123"), - api: mock.API{CreateServiceAuthorizationFn: createServiceAuthOK}, - wantOutput: "Created service authorization 12345", + Name: "success", + Args: "--user-id 123 --service-id 123", + API: mock.API{CreateServiceAuthorizationFn: createServiceAuthOK}, + WantOutput: "Created service authorization 12345", }, } - for testcaseIdx := range scenarios { - testcase := &scenarios[testcaseIdx] - t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { - var stdout bytes.Buffer - app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { - opts := testutil.MockGlobalData(testcase.args, &stdout) - opts.APIClientFactory = mock.APIClient(testcase.api) - return opts, nil - } - err := app.Run(testcase.args, nil) - testutil.AssertErrorContains(t, err, testcase.wantError) - testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput) - }) - } + + testutil.RunCLIScenarios(t, []string{root.CommandName, sub.CommandName, "create"}, scenarios) } func TestServiceAuthList(t *testing.T) { - args := testutil.SplitArgs - scenarios := []struct { - args []string - api mock.API - wantError string - wantOutput string - }{ + scenarios := []testutil.CLIScenario{ { - args: args("service auth list --verbose --json"), - wantError: "invalid flag combination, --verbose and --json", + Name: "invalid flag combination", + Args: "--verbose --json", + WantError: "invalid flag combination, --verbose and --json", }, { - args: args("service auth list"), - api: mock.API{ListServiceAuthorizationsFn: listServiceAuthError}, - wantError: errTest.Error(), + Name: "api failure", + Args: "", + API: mock.API{ListServiceAuthorizationsFn: listServiceAuthError}, + WantError: errTest.Error(), }, { - args: args("service auth list"), - api: mock.API{ListServiceAuthorizationsFn: listServiceAuthOK}, - wantOutput: "AUTH ID USER ID SERVICE ID PERMISSION\n123 456 789 read_only\n", + Name: "success", + Args: "", + API: mock.API{ListServiceAuthorizationsFn: listServiceAuthOK}, + WantOutput: "AUTH ID USER ID SERVICE ID PERMISSION\n123 456 789 read_only\n", }, { - args: args("service auth list --json"), - api: mock.API{ListServiceAuthorizationsFn: listServiceAuthOK}, - wantOutput: `{ + Name: "success with json", + Args: "--json", + API: mock.API{ListServiceAuthorizationsFn: listServiceAuthOK}, + WantOutput: `{ "Info": { "links": {}, "meta": {} @@ -103,58 +83,45 @@ func TestServiceAuthList(t *testing.T) { }`, }, { - args: args("service auth list --verbose"), - api: mock.API{ListServiceAuthorizationsFn: listServiceAuthOK}, - wantOutput: "Fastly API endpoint: https://api.fastly.com\nFastly API token provided via config file (profile: user)\n\nAuth ID: 123\nUser ID: 456\nService ID: 789\nPermission: read_only\n", + Name: "success with verbose", + Args: "--verbose", + API: mock.API{ListServiceAuthorizationsFn: listServiceAuthOK}, + WantOutput: "Fastly API endpoint: https://api.fastly.com\nFastly API token provided via config file (profile: user)\n\nAuth ID: 123\nUser ID: 456\nService ID: 789\nPermission: read_only\n", }, } - for testcaseIdx := range scenarios { - testcase := &scenarios[testcaseIdx] - t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { - var stdout bytes.Buffer - app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { - opts := testutil.MockGlobalData(testcase.args, &stdout) - opts.APIClientFactory = mock.APIClient(testcase.api) - return opts, nil - } - err := app.Run(testcase.args, nil) - t.Log(stdout.String()) - testutil.AssertErrorContains(t, err, testcase.wantError) - testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput) - }) - } + + testutil.RunCLIScenarios(t, []string{root.CommandName, sub.CommandName, "list"}, scenarios) } func TestServiceAuthDescribe(t *testing.T) { - args := testutil.SplitArgs - scenarios := []struct { - args []string - api mock.API - wantError string - wantOutput string - }{ + scenarios := []testutil.CLIScenario{ { - args: args("service auth describe"), - wantError: "error parsing arguments: required flag --id not provided", + Name: "missing required flag", + Args: "", + WantError: "error parsing arguments: required flag --id not provided", }, { - args: args("service auth describe --id 123 --verbose --json"), - wantError: "invalid flag combination, --verbose and --json", + Name: "invalid flag combination", + Args: "--id 123 --verbose --json", + WantError: "invalid flag combination, --verbose and --json", }, { - args: args("service auth describe --id 123"), - api: mock.API{GetServiceAuthorizationFn: describeServiceAuthError}, - wantError: errTest.Error(), + Name: "api failure", + Args: "--id 123", + API: mock.API{GetServiceAuthorizationFn: describeServiceAuthError}, + WantError: errTest.Error(), }, { - args: args("service auth describe --id 123"), - api: mock.API{GetServiceAuthorizationFn: describeServiceAuthOK}, - wantOutput: "Auth ID: 12345\nUser ID: 456\nService ID: 789\nPermission: read_only\n", + Name: "success", + Args: "--id 123", + API: mock.API{GetServiceAuthorizationFn: describeServiceAuthOK}, + WantOutput: "Auth ID: 12345\nUser ID: 456\nService ID: 789\nPermission: read_only\n", }, { - args: args("service auth describe --id 123 --json"), - api: mock.API{GetServiceAuthorizationFn: describeServiceAuthOK}, - wantOutput: `{ + Name: "success with json", + Args: "--id 123 --json", + API: mock.API{GetServiceAuthorizationFn: describeServiceAuthOK}, + WantOutput: `{ "CreatedAt": null, "DeletedAt": null, "ID": "12345", @@ -169,103 +136,61 @@ func TestServiceAuthDescribe(t *testing.T) { }`, }, } - for testcaseIdx := range scenarios { - testcase := &scenarios[testcaseIdx] - t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { - var stdout bytes.Buffer - app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { - opts := testutil.MockGlobalData(testcase.args, &stdout) - opts.APIClientFactory = mock.APIClient(testcase.api) - return opts, nil - } - err := app.Run(testcase.args, nil) - t.Log(stdout.String()) - testutil.AssertErrorContains(t, err, testcase.wantError) - testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput) - }) - } + + testutil.RunCLIScenarios(t, []string{root.CommandName, sub.CommandName, "describe"}, scenarios) } func TestServiceAuthUpdate(t *testing.T) { - args := testutil.SplitArgs - scenarios := []struct { - args []string - api mock.API - wantError string - wantOutput string - }{ + scenarios := []testutil.CLIScenario{ { - args: args("service auth update --permission full"), - wantError: "error parsing arguments: required flag --id not provided", + Name: "missing id flag", + Args: "--permission full", + WantError: "error parsing arguments: required flag --id not provided", }, { - args: args("service auth update --id 123"), - wantError: "error parsing arguments: required flag --permission not provided", + Name: "missing permission flag", + Args: "--id 123", + WantError: "error parsing arguments: required flag --permission not provided", }, { - args: args("service auth update --id 123 --permission full"), - api: mock.API{UpdateServiceAuthorizationFn: updateServiceAuthError}, - wantError: errTest.Error(), + Name: "api failure", + Args: "--id 123 --permission full", + API: mock.API{UpdateServiceAuthorizationFn: updateServiceAuthError}, + WantError: errTest.Error(), }, { - args: args("service auth update --id 123 --permission full"), - api: mock.API{UpdateServiceAuthorizationFn: updateServiceAuthOK}, - wantOutput: "Updated service authorization 123", + Name: "success", + Args: "--id 123 --permission full", + API: mock.API{UpdateServiceAuthorizationFn: updateServiceAuthOK}, + WantOutput: "Updated service authorization 123", }, } - for testcaseIdx := range scenarios { - testcase := &scenarios[testcaseIdx] - t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { - var stdout bytes.Buffer - app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { - opts := testutil.MockGlobalData(testcase.args, &stdout) - opts.APIClientFactory = mock.APIClient(testcase.api) - return opts, nil - } - err := app.Run(testcase.args, nil) - testutil.AssertErrorContains(t, err, testcase.wantError) - testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput) - }) - } + + testutil.RunCLIScenarios(t, []string{root.CommandName, sub.CommandName, "update"}, scenarios) } func TestServiceAuthDelete(t *testing.T) { - args := testutil.SplitArgs - scenarios := []struct { - args []string - api mock.API - wantError string - wantOutput string - }{ + scenarios := []testutil.CLIScenario{ { - args: args("service auth delete"), - wantError: "error parsing arguments: required flag --id not provided", + Name: "missing required flag", + Args: "", + WantError: "error parsing arguments: required flag --id not provided", }, { - args: args("service auth delete --id 123"), - api: mock.API{DeleteServiceAuthorizationFn: deleteServiceAuthError}, - wantError: errTest.Error(), + Name: "api failure", + Args: "--id 123", + API: mock.API{DeleteServiceAuthorizationFn: deleteServiceAuthError}, + WantError: errTest.Error(), }, { - args: args("service auth delete --id 123"), - api: mock.API{DeleteServiceAuthorizationFn: deleteServiceAuthOK}, - wantOutput: "Deleted service authorization 123", + Name: "success", + Args: "--id 123", + API: mock.API{DeleteServiceAuthorizationFn: deleteServiceAuthOK}, + WantOutput: "Deleted service authorization 123", }, } - for testcaseIdx := range scenarios { - testcase := &scenarios[testcaseIdx] - t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { - var stdout bytes.Buffer - app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { - opts := testutil.MockGlobalData(testcase.args, &stdout) - opts.APIClientFactory = mock.APIClient(testcase.api) - return opts, nil - } - err := app.Run(testcase.args, nil) - testutil.AssertErrorContains(t, err, testcase.wantError) - testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput) - }) - } + + testutil.RunCLIScenarios(t, []string{root.CommandName, sub.CommandName, "delete"}, scenarios) } var errTest = errors.New("fixture error") diff --git a/pkg/commands/service/service_test.go b/pkg/commands/service/service_test.go index 4174c5333..b994e9997 100644 --- a/pkg/commands/service/service_test.go +++ b/pkg/commands/service/service_test.go @@ -15,6 +15,7 @@ import ( "github.com/fastly/go-fastly/v13/fastly" "github.com/fastly/cli/pkg/app" + root "github.com/fastly/cli/pkg/commands/service" "github.com/fastly/cli/pkg/global" "github.com/fastly/cli/pkg/manifest" "github.com/fastly/cli/pkg/mock" @@ -22,70 +23,53 @@ import ( ) func TestServiceCreate(t *testing.T) { - args := testutil.SplitArgs - scenarios := []struct { - args []string - api mock.API - wantError string - wantOutput string - }{ + scenarios := []testutil.CLIScenario{ { - args: args("service create --name Foo"), - api: mock.API{CreateServiceFn: createServiceOK}, - wantOutput: "Created service 12345", + Name: "success with long flag", + Args: "--name Foo", + API: mock.API{CreateServiceFn: createServiceOK}, + WantOutput: "Created service 12345", }, { - args: args("service create -n=Foo"), - api: mock.API{CreateServiceFn: createServiceOK}, - wantOutput: "Created service 12345", + Name: "success with short flag and equals", + Args: "-n=Foo", + API: mock.API{CreateServiceFn: createServiceOK}, + WantOutput: "Created service 12345", }, { - args: args("service create --name Foo --type wasm"), - api: mock.API{CreateServiceFn: createServiceOK}, - wantOutput: "Created service 12345", + Name: "success with type", + Args: "--name Foo --type wasm", + API: mock.API{CreateServiceFn: createServiceOK}, + WantOutput: "Created service 12345", }, { - args: args("service create --name Foo --type wasm --comment Hello"), - api: mock.API{CreateServiceFn: createServiceOK}, - wantOutput: "Created service 12345", + Name: "success with type and comment", + Args: "--name Foo --type wasm --comment Hello", + API: mock.API{CreateServiceFn: createServiceOK}, + WantOutput: "Created service 12345", }, { - args: args("service create -n Foo --comment Hello"), - api: mock.API{CreateServiceFn: createServiceOK}, - wantOutput: "Created service 12345", + Name: "success with short flag and comment", + Args: "-n Foo --comment Hello", + API: mock.API{CreateServiceFn: createServiceOK}, + WantOutput: "Created service 12345", }, { - args: args("service create -n Foo"), - api: mock.API{CreateServiceFn: createServiceError}, - wantError: errTest.Error(), + Name: "api failure", + Args: "-n Foo", + API: mock.API{CreateServiceFn: createServiceError}, + WantError: errTest.Error(), }, } - for testcaseIdx := range scenarios { - testcase := &scenarios[testcaseIdx] - t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { - var stdout bytes.Buffer - app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { - opts := testutil.MockGlobalData(testcase.args, &stdout) - opts.APIClientFactory = mock.APIClient(testcase.api) - return opts, nil - } - err := app.Run(testcase.args, nil) - testutil.AssertErrorContains(t, err, testcase.wantError) - testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput) - }) - } + + testutil.RunCLIScenarios(t, []string{root.CommandName, "create"}, scenarios) } func TestServiceList(t *testing.T) { - args := testutil.SplitArgs - scenarios := []struct { - args []string - api mock.API - wantError string - wantOutput string - }{ + scenarios := []testutil.CLIScenario{ { - api: mock.API{ + Name: "api failure", + API: mock.API{ GetServicesFn: func(ctx context.Context, _ *fastly.GetServicesInput) *fastly.ListPaginator[fastly.Service] { return fastly.NewPaginator[fastly.Service](ctx, &mock.HTTPClient{ Errors: []error{ @@ -95,11 +79,12 @@ func TestServiceList(t *testing.T) { }, fastly.ListOpts{}, "/example") }, }, - args: args("service list"), - wantError: testutil.Err.Error(), + Args: "", + WantError: testutil.Err.Error(), }, { - api: mock.API{ + Name: "success with pagination", + API: mock.API{ GetServicesFn: func(ctx context.Context, _ *fastly.GetServicesInput) *fastly.ListPaginator[fastly.Service] { return fastly.NewPaginator[fastly.Service](ctx, &mock.HTTPClient{ Errors: []error{nil}, @@ -132,11 +117,12 @@ func TestServiceList(t *testing.T) { }, fastly.ListOpts{}, "/example") }, }, - args: args("service list --per-page 1"), - wantOutput: listServicesShortOutput, + Args: "--per-page 1", + WantOutput: listServicesShortOutput, }, { - api: mock.API{ + Name: "success with verbose", + API: mock.API{ GetServicesFn: func(ctx context.Context, _ *fastly.GetServicesInput) *fastly.ListPaginator[fastly.Service] { return fastly.NewPaginator[fastly.Service](ctx, &mock.HTTPClient{ Errors: []error{nil}, @@ -199,196 +185,137 @@ func TestServiceList(t *testing.T) { }, fastly.ListOpts{}, "/example") }, }, - args: args("service list --verbose"), - wantOutput: listServicesVerboseOutput, + Args: "--verbose", + WantOutput: listServicesVerboseOutput, }, } - for testcaseIdx := range scenarios { - testcase := &scenarios[testcaseIdx] - t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { - var stdout bytes.Buffer - app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { - opts := testutil.MockGlobalData(testcase.args, &stdout) - opts.APIClientFactory = mock.APIClient(testcase.api) - return opts, nil - } - err := app.Run(testcase.args, nil) - testutil.AssertErrorContains(t, err, testcase.wantError) - testutil.AssertString(t, testcase.wantOutput, stdout.String()) - }) - } + + testutil.RunCLIScenarios(t, []string{root.CommandName, "list"}, scenarios) } func TestServiceDescribe(t *testing.T) { - args := testutil.SplitArgs - scenarios := []struct { - args []string - api mock.API - wantError string - wantOutput string - }{ - { - args: args("service describe"), - api: mock.API{GetServiceDetailsFn: describeServiceOK}, - wantError: "error reading service: no service ID found", - }, + scenarios := []testutil.CLIScenario{ { - args: args("service describe --service-id 123"), - api: mock.API{GetServiceDetailsFn: describeServiceOK}, - wantOutput: describeServiceShortOutput, + Name: "no service id", + Args: "", + API: mock.API{GetServiceDetailsFn: describeServiceOK}, + WantError: "error reading service: no service ID found", }, { - args: args("service describe --service-id 123 --verbose"), - api: mock.API{GetServiceDetailsFn: describeServiceOK}, - wantOutput: describeServiceVerboseOutput, + Name: "success", + Args: "--service-id 123", + API: mock.API{GetServiceDetailsFn: describeServiceOK}, + WantOutput: describeServiceShortOutput, }, { - args: args("service describe --service-id 123 -v"), - api: mock.API{GetServiceDetailsFn: describeServiceOK}, - wantOutput: describeServiceVerboseOutput, + Name: "verbose flag after args", + Args: "--service-id 123 --verbose", + API: mock.API{GetServiceDetailsFn: describeServiceOK}, + WantOutput: describeServiceVerboseOutput, }, { - args: args("service --verbose describe --service-id 123"), - api: mock.API{GetServiceDetailsFn: describeServiceOK}, - wantOutput: describeServiceVerboseOutput, + Name: "verbose short flag after args", + Args: "--service-id 123 -v", + API: mock.API{GetServiceDetailsFn: describeServiceOK}, + WantOutput: describeServiceVerboseOutput, }, { - args: args("-v service describe --service-id 123"), - api: mock.API{GetServiceDetailsFn: describeServiceOK}, - wantOutput: describeServiceVerboseOutput, - }, - { - args: args("service describe --service-id 123"), - api: mock.API{GetServiceDetailsFn: describeServiceError}, - wantError: errTest.Error(), + Name: "api failure", + Args: "--service-id 123", + API: mock.API{GetServiceDetailsFn: describeServiceError}, + WantError: errTest.Error(), }, } - for testcaseIdx := range scenarios { - testcase := &scenarios[testcaseIdx] - t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { - var stdout bytes.Buffer - app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { - opts := testutil.MockGlobalData(testcase.args, &stdout) - opts.APIClientFactory = mock.APIClient(testcase.api) - return opts, nil - } - err := app.Run(testcase.args, nil) - testutil.AssertErrorContains(t, err, testcase.wantError) - testutil.AssertString(t, testcase.wantOutput, stdout.String()) - }) - } + + testutil.RunCLIScenarios(t, []string{root.CommandName, "describe"}, scenarios) } func TestServiceSearch(t *testing.T) { - args := testutil.SplitArgs - scenarios := []struct { - args []string - api mock.API - wantError string - wantOutput string - }{ + scenarios := []testutil.CLIScenario{ { - args: args("service search"), - wantError: "error parsing arguments: required flag --name not provided", + Name: "missing required flag", + Args: "", + WantError: "error parsing arguments: required flag --name not provided", }, { - args: args("service search --name Foo"), - api: mock.API{SearchServiceFn: searchServiceOK}, - wantOutput: searchServiceShortOutput, + Name: "success", + Args: "--name Foo", + API: mock.API{SearchServiceFn: searchServiceOK}, + WantOutput: searchServiceShortOutput, }, { - args: args("service search --name Foo -v"), - api: mock.API{SearchServiceFn: searchServiceOK}, - wantOutput: searchServiceVerboseOutput, + Name: "success with verbose", + Args: "--name Foo -v", + API: mock.API{SearchServiceFn: searchServiceOK}, + WantOutput: searchServiceVerboseOutput, }, { - args: args("service search --name"), - api: mock.API{SearchServiceFn: searchServiceOK}, - wantError: "error parsing arguments: expected argument for flag '--name'", + Name: "missing flag value", + Args: "--name", + API: mock.API{SearchServiceFn: searchServiceOK}, + WantError: "error parsing arguments: expected argument for flag '--name'", }, } - for testcaseIdx := range scenarios { - testcase := &scenarios[testcaseIdx] - t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { - var stdout bytes.Buffer - app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { - opts := testutil.MockGlobalData(testcase.args, &stdout) - opts.APIClientFactory = mock.APIClient(testcase.api) - return opts, nil - } - err := app.Run(testcase.args, nil) - testutil.AssertErrorContains(t, err, testcase.wantError) - testutil.AssertString(t, testcase.wantOutput, stdout.String()) - }) - } + + testutil.RunCLIScenarios(t, []string{root.CommandName, "search"}, scenarios) } func TestServiceUpdate(t *testing.T) { - args := testutil.SplitArgs - scenarios := []struct { - args []string - api mock.API - wantError string - wantOutput string - }{ + scenarios := []testutil.CLIScenario{ { - args: args("service update"), - api: mock.API{ + Name: "no service id", + Args: "", + API: mock.API{ GetServiceFn: getServiceOK, UpdateServiceFn: updateServiceOK, }, - wantError: "error reading service: no service ID found", + WantError: "error reading service: no service ID found", }, { - args: args("service update --service-id 12345"), - api: mock.API{UpdateServiceFn: updateServiceOK}, - wantError: "error parsing arguments: must provide either --name or --comment to update service", + Name: "missing name or comment", + Args: "--service-id 12345", + API: mock.API{UpdateServiceFn: updateServiceOK}, + WantError: "error parsing arguments: must provide either --name or --comment to update service", }, { - args: args("service update --service-id 12345 --name Foo"), - api: mock.API{UpdateServiceFn: updateServiceOK}, - wantOutput: "Updated service 12345", + Name: "success with long flag", + Args: "--service-id 12345 --name Foo", + API: mock.API{UpdateServiceFn: updateServiceOK}, + WantOutput: "Updated service 12345", }, { - args: args("service update --service-id 12345 -n=Foo"), - api: mock.API{UpdateServiceFn: updateServiceOK}, - wantOutput: "Updated service 12345", + Name: "success with short flag and equals", + Args: "--service-id 12345 -n=Foo", + API: mock.API{UpdateServiceFn: updateServiceOK}, + WantOutput: "Updated service 12345", }, { - args: args("service update --service-id 12345 --name Foo"), - api: mock.API{UpdateServiceFn: updateServiceOK}, - wantOutput: "Updated service 12345", + Name: "success with long flag duplicate", + Args: "--service-id 12345 --name Foo", + API: mock.API{UpdateServiceFn: updateServiceOK}, + WantOutput: "Updated service 12345", }, { - args: args("service update --service-id 12345 --name Foo --comment Hello"), - api: mock.API{UpdateServiceFn: updateServiceOK}, - wantOutput: "Updated service 12345", + Name: "success with name and comment", + Args: "--service-id 12345 --name Foo --comment Hello", + API: mock.API{UpdateServiceFn: updateServiceOK}, + WantOutput: "Updated service 12345", }, { - args: args("service update --service-id 12345 -n Foo --comment Hello"), - api: mock.API{UpdateServiceFn: updateServiceOK}, - wantOutput: "Updated service 12345", + Name: "success with short flag and comment", + Args: "--service-id 12345 -n Foo --comment Hello", + API: mock.API{UpdateServiceFn: updateServiceOK}, + WantOutput: "Updated service 12345", }, { - args: args("service update --service-id 12345 -n Foo"), - api: mock.API{UpdateServiceFn: updateServiceError}, - wantError: errTest.Error(), + Name: "api failure", + Args: "--service-id 12345 -n Foo", + API: mock.API{UpdateServiceFn: updateServiceError}, + WantError: errTest.Error(), }, } - for testcaseIdx := range scenarios { - testcase := &scenarios[testcaseIdx] - t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { - var stdout bytes.Buffer - app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { - opts := testutil.MockGlobalData(testcase.args, &stdout) - opts.APIClientFactory = mock.APIClient(testcase.api) - return opts, nil - } - err := app.Run(testcase.args, nil) - testutil.AssertErrorContains(t, err, testcase.wantError) - testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput) - }) - } + + testutil.RunCLIScenarios(t, []string{root.CommandName, "update"}, scenarios) } func TestServiceDelete(t *testing.T) { diff --git a/pkg/commands/stats/historical_test.go b/pkg/commands/stats/historical_test.go index ba5b1a800..6119d52d6 100644 --- a/pkg/commands/stats/historical_test.go +++ b/pkg/commands/stats/historical_test.go @@ -1,59 +1,40 @@ package stats_test import ( - "bytes" "context" "encoding/json" - "io" - "strings" "testing" "github.com/fastly/go-fastly/v13/fastly" - "github.com/fastly/cli/pkg/app" - "github.com/fastly/cli/pkg/global" + root "github.com/fastly/cli/pkg/commands/stats" "github.com/fastly/cli/pkg/mock" "github.com/fastly/cli/pkg/testutil" ) func TestHistorical(t *testing.T) { - args := testutil.SplitArgs - scenarios := []struct { - args []string - api mock.API - wantError string - wantOutput string - }{ + scenarios := []testutil.CLIScenario{ { - args: args("stats historical --service-id=123"), - api: mock.API{GetStatsJSONFn: getStatsJSONOK}, - wantOutput: historicalOK, + Name: "success", + Args: "--service-id=123", + API: mock.API{GetStatsJSONFn: getStatsJSONOK}, + WantOutput: historicalOK, }, { - args: args("stats historical --service-id=123"), - api: mock.API{GetStatsJSONFn: getStatsJSONError}, - wantError: errTest.Error(), + Name: "api failure", + Args: "--service-id=123", + API: mock.API{GetStatsJSONFn: getStatsJSONError}, + WantError: errTest.Error(), }, { - args: args("stats historical --service-id=123 --format=json"), - api: mock.API{GetStatsJSONFn: getStatsJSONOK}, - wantOutput: historicalJSONOK, + Name: "success with json format", + Args: "--service-id=123 --format=json", + API: mock.API{GetStatsJSONFn: getStatsJSONOK}, + WantOutput: historicalJSONOK, }, } - for testcaseIdx := range scenarios { - testcase := &scenarios[testcaseIdx] - t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { - var stdout bytes.Buffer - app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { - opts := testutil.MockGlobalData(testcase.args, &stdout) - opts.APIClientFactory = mock.APIClient(testcase.api) - return opts, nil - } - err := app.Run(testcase.args, nil) - testutil.AssertErrorContains(t, err, testcase.wantError) - testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput) - }) - } + + testutil.RunCLIScenarios(t, []string{root.CommandName, "historical"}, scenarios) } var historicalOK = `From: Wed May 15 20:08:35 UTC 2013 diff --git a/pkg/commands/stats/regions_test.go b/pkg/commands/stats/regions_test.go index b3ac298ba..3a7eca45d 100644 --- a/pkg/commands/stats/regions_test.go +++ b/pkg/commands/stats/regions_test.go @@ -1,54 +1,34 @@ package stats_test import ( - "bytes" "context" "errors" - "io" - "strings" "testing" "github.com/fastly/go-fastly/v13/fastly" - "github.com/fastly/cli/pkg/app" - "github.com/fastly/cli/pkg/global" + root "github.com/fastly/cli/pkg/commands/stats" "github.com/fastly/cli/pkg/mock" "github.com/fastly/cli/pkg/testutil" ) func TestRegions(t *testing.T) { - args := testutil.SplitArgs - scenarios := []struct { - args []string - api mock.API - wantError string - wantOutput string - }{ + scenarios := []testutil.CLIScenario{ { - args: args("stats regions"), - api: mock.API{GetRegionsFn: getRegionsOK}, - wantOutput: "foo\nbar\nbaz\n", + Name: "success", + Args: "", + API: mock.API{GetRegionsFn: getRegionsOK}, + WantOutput: "foo\nbar\nbaz\n", }, { - args: args("stats regions"), - api: mock.API{GetRegionsFn: getRegionsError}, - wantError: errTest.Error(), + Name: "api error", + Args: "", + API: mock.API{GetRegionsFn: getRegionsError}, + WantError: errTest.Error(), }, } - for testcaseIdx := range scenarios { - testcase := &scenarios[testcaseIdx] - t.Run(strings.Join(testcase.args, " "), func(t *testing.T) { - var stdout bytes.Buffer - app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { - opts := testutil.MockGlobalData(testcase.args, &stdout) - opts.APIClientFactory = mock.APIClient(testcase.api) - return opts, nil - } - err := app.Run(testcase.args, nil) - testutil.AssertErrorContains(t, err, testcase.wantError) - testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput) - }) - } + + testutil.RunCLIScenarios(t, []string{root.CommandName, "regions"}, scenarios) } func getRegionsOK(_ context.Context) (*fastly.RegionsResponse, error) {