Skip to content

Commit

Permalink
tests: Use threadsafe.Buffer in centralized test runner. (#1288)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpfleming authored Aug 14, 2024
1 parent fc70ca5 commit fc9549b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions pkg/commands/compute/metadata_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package compute_test

import (
"bytes"
"os"
"path/filepath"
"testing"
Expand All @@ -13,6 +12,7 @@ import (
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/revision"
"github.com/fastly/cli/pkg/testutil"
"github.com/fastly/cli/pkg/threadsafe"
)

func TestMetadata(t *testing.T) {
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestMetadata(t *testing.T) {
},
},
WantOutput: "SUCCESS: configuration updated",
Validator: func(t *testing.T, _ *testutil.TestScenario, opts *global.Data, _ bytes.Buffer) {
Validator: func(t *testing.T, _ *testutil.TestScenario, opts *global.Data, _ *threadsafe.Buffer) {
data, err := os.ReadFile(opts.ConfigPath)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestMetadata(t *testing.T) {
},
},
WantOutput: "SUCCESS: configuration updated",
Validator: func(t *testing.T, _ *testutil.TestScenario, opts *global.Data, _ bytes.Buffer) {
Validator: func(t *testing.T, _ *testutil.TestScenario, opts *global.Data, _ *threadsafe.Buffer) {
data, err := os.ReadFile(opts.ConfigPath)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestMetadata(t *testing.T) {
"INFO: We will enable all metadata except for the specified `--disable-*` flags",
"SUCCESS: configuration updated",
},
Validator: func(t *testing.T, _ *testutil.TestScenario, opts *global.Data, _ bytes.Buffer) {
Validator: func(t *testing.T, _ *testutil.TestScenario, opts *global.Data, _ *threadsafe.Buffer) {
data, err := os.ReadFile(opts.ConfigPath)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestMetadata(t *testing.T) {
"INFO: We will disable all metadata except for the specified `--enable-*` flags",
"SUCCESS: configuration updated",
},
Validator: func(t *testing.T, _ *testutil.TestScenario, opts *global.Data, _ bytes.Buffer) {
Validator: func(t *testing.T, _ *testutil.TestScenario, opts *global.Data, _ *threadsafe.Buffer) {
data, err := os.ReadFile(opts.ConfigPath)
if err != nil {
t.Error(err)
Expand Down
12 changes: 6 additions & 6 deletions pkg/commands/sso/sso_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sso_test

import (
"bytes"
"errors"
"testing"
"time"
Expand All @@ -13,6 +12,7 @@ import (
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/mock"
"github.com/fastly/cli/pkg/testutil"
"github.com/fastly/cli/pkg/threadsafe"
)

func TestSSO(t *testing.T) {
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestSSO(t *testing.T) {
"We need to open your browser to authenticate you.",
"Session token (persisted to your local configuration): 123",
},
Validator: func(t *testing.T, scenario *testutil.TestScenario, opts *global.Data, stdout bytes.Buffer) {
Validator: func(t *testing.T, scenario *testutil.TestScenario, opts *global.Data, stdout *threadsafe.Buffer) {
const expectedToken = "123"
userProfile := opts.Config.Profiles["user"]
if userProfile.Token != expectedToken {
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestSSO(t *testing.T) {
"We need to open your browser to authenticate you.",
"Session token (persisted to your local configuration): 123",
},
Validator: func(t *testing.T, scenario *testutil.TestScenario, opts *global.Data, stdout bytes.Buffer) {
Validator: func(t *testing.T, scenario *testutil.TestScenario, opts *global.Data, stdout *threadsafe.Buffer) {
const expectedToken = "123"
userProfile := opts.Config.Profiles["test_user"]
if userProfile.Token != expectedToken {
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestSSO(t *testing.T) {
// "is not a Fastly SSO (Single Sign-On) generated token",
"{Latitude:1 Longtitude:2 X:3 Y:4}",
},
Validator: func(t *testing.T, scenario *testutil.TestScenario, opts *global.Data, stdout bytes.Buffer) {
Validator: func(t *testing.T, scenario *testutil.TestScenario, opts *global.Data, stdout *threadsafe.Buffer) {
const expectedToken = "mock-token"
userProfile := opts.Config.Profiles["user"]
if userProfile.Token != expectedToken {
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestSSO(t *testing.T) {
},
WantOutput: "Your access token has expired and so has your refresh token.",
DontWantOutput: "{Latitude:1 Longtitude:2 X:3 Y:4}",
Validator: func(t *testing.T, scenario *testutil.TestScenario, opts *global.Data, stdout bytes.Buffer) {
Validator: func(t *testing.T, scenario *testutil.TestScenario, opts *global.Data, stdout *threadsafe.Buffer) {
const expectedToken = "mock-token"
userProfile := opts.Config.Profiles["user"]
if userProfile.Token != expectedToken {
Expand Down Expand Up @@ -283,7 +283,7 @@ func TestSSO(t *testing.T) {
"Session token (persisted to your local configuration): 123",
"{Latitude:1 Longtitude:2 X:3 Y:4}",
},
Validator: func(t *testing.T, scenario *testutil.TestScenario, opts *global.Data, stdout bytes.Buffer) {
Validator: func(t *testing.T, scenario *testutil.TestScenario, opts *global.Data, stdout *threadsafe.Buffer) {
const expectedToken = "123"
userProfile := opts.Config.Profiles["user"]
if userProfile.Token != expectedToken {
Expand Down
8 changes: 4 additions & 4 deletions pkg/testutil/scenarios.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package testutil

import (
"bytes"
"fmt"
"github.com/fastly/cli/pkg/app"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/mock"
"github.com/fastly/cli/pkg/threadsafe"
"io"
"os"
"slices"
Expand Down Expand Up @@ -54,7 +54,7 @@ type TestScenario struct {
Stdin []string
// Validator function can perform additional validation on the results
// of the scenario
Validator func(t *testing.T, scenario *TestScenario, opts *global.Data, stdout bytes.Buffer)
Validator func(t *testing.T, scenario *TestScenario, opts *global.Data, stdout *threadsafe.Buffer)
// WantError will cause the scenario to fail if this string
// does not appear in an Error
WantError string
Expand Down Expand Up @@ -95,7 +95,7 @@ func RunScenario(t *testing.T, command []string, scenario TestScenario) {
err error
fullargs []string
rootdir string
stdout bytes.Buffer
stdout threadsafe.Buffer
)

if len(scenario.Arg) > 0 {
Expand Down Expand Up @@ -233,7 +233,7 @@ func RunScenario(t *testing.T, command []string, scenario TestScenario) {
}

if scenario.Validator != nil {
scenario.Validator(t, &scenario, opts, stdout)
scenario.Validator(t, &scenario, opts, &stdout)
}
})
}
Expand Down

0 comments on commit fc9549b

Please sign in to comment.