Skip to content

Commit 26429e7

Browse files
authored
Update appcmdtesting to use functional options (#3820)
1 parent 33b7f61 commit 26429e7

File tree

8 files changed

+743
-714
lines changed

8 files changed

+743
-714
lines changed

private/buf/cmd/buf/buf_test.go

Lines changed: 98 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
3838
"github.com/bufbuild/buf/private/bufpkg/bufimage"
3939
imagev1 "github.com/bufbuild/buf/private/gen/proto/go/buf/alpha/image/v1"
40-
"github.com/bufbuild/buf/private/pkg/app/appcmd"
4140
"github.com/bufbuild/buf/private/pkg/app/appcmd/appcmdtesting"
4241
"github.com/bufbuild/buf/private/pkg/osext"
4342
"github.com/bufbuild/buf/private/pkg/protoencoding"
@@ -3050,20 +3049,20 @@ testdata/check_plugins/current/proto/common/v1/common.proto:8:5:Field name "comm
30503049
require.NotPanics(
30513050
t,
30523051
func() {
3053-
appcmdtesting.RunCommandExitCodeStderrContains(
3052+
appcmdtesting.Run(
30543053
t,
3055-
func(use string) *appcmd.Command { return NewRootCommand(use) },
3056-
1,
3057-
[]string{
3054+
NewRootCommand,
3055+
appcmdtesting.WithEnv(internaltesting.NewEnvFunc(t)),
3056+
appcmdtesting.WithExpectedExitCode(1),
3057+
appcmdtesting.WithExpectedStderrPartials(
30583058
`panic: this panic is intentional`,
30593059
`Failure: plugin "buf-plugin-panic" failed: Exited with code 2: exit status 2`,
3060-
},
3061-
internaltesting.NewEnvFunc(t),
3062-
nil,
3063-
"lint",
3064-
filepath.Join("testdata", "check_plugins", "current"),
3065-
"--config",
3066-
`{
3060+
),
3061+
appcmdtesting.WithArgs(
3062+
"lint",
3063+
filepath.Join("testdata", "check_plugins", "current"),
3064+
"--config",
3065+
`{
30673066
"version":"v2",
30683067
"modules": [
30693068
{"path": "testdata/check_plugins/current/proto"},
@@ -3076,6 +3075,7 @@ testdata/check_plugins/current/proto/common/v1/common.proto:8:5:Field name "comm
30763075
{"plugin": "buf-plugin-panic"}
30773076
]
30783077
}`,
3078+
),
30793079
)
30803080
},
30813081
)
@@ -4468,59 +4468,63 @@ func testModInit(t *testing.T, expectedData string, document bool, name string,
44684468
}
44694469

44704470
func testRunStdout(t *testing.T, stdin io.Reader, expectedExitCode int, expectedStdout string, args ...string) {
4471-
appcmdtesting.RunCommandExitCodeStdout(
4471+
appcmdtesting.Run(
44724472
t,
4473-
func(use string) *appcmd.Command { return NewRootCommand(use) },
4474-
expectedExitCode,
4475-
expectedStdout,
4476-
internaltesting.NewEnvFunc(t),
4477-
stdin,
4478-
args...,
4473+
NewRootCommand,
4474+
appcmdtesting.WithEnv(internaltesting.NewEnvFunc(t)),
4475+
appcmdtesting.WithStdin(stdin),
4476+
appcmdtesting.WithExpectedExitCode(expectedExitCode),
4477+
appcmdtesting.WithExpectedStdout(expectedStdout),
4478+
appcmdtesting.WithArgs(args...),
44794479
)
44804480
}
44814481

44824482
func testRunStderr(t *testing.T, stdin io.Reader, expectedExitCode int, expectedStderr string, args ...string) {
4483-
appcmdtesting.RunCommandExitCodeStderr(
4483+
appcmdtesting.Run(
44844484
t,
4485-
func(use string) *appcmd.Command { return NewRootCommand(use) },
4486-
expectedExitCode,
4487-
expectedStderr,
4488-
internaltesting.NewEnvFunc(t),
4489-
stdin,
4490-
args...,
4485+
NewRootCommand,
4486+
appcmdtesting.WithEnv(internaltesting.NewEnvFunc(t)),
4487+
appcmdtesting.WithStdin(stdin),
4488+
appcmdtesting.WithExpectedExitCode(expectedExitCode),
4489+
appcmdtesting.WithExpectedStderr(expectedStderr),
4490+
appcmdtesting.WithArgs(args...),
44914491
)
44924492
}
44934493

44944494
func testRunStdoutStderrNoWarn(t *testing.T, stdin io.Reader, expectedExitCode int, expectedStdout string, expectedStderr string, args ...string) {
4495-
appcmdtesting.RunCommandExitCodeStdoutStderr(
4496-
t,
4497-
func(use string) *appcmd.Command { return NewRootCommand(use) },
4498-
expectedExitCode,
4499-
expectedStdout,
4500-
expectedStderr,
4501-
internaltesting.NewEnvFunc(t),
4502-
stdin,
4503-
// we do not want warnings to be part of our stderr test calculation
4504-
append(
4505-
args,
4506-
"--no-warn",
4507-
)...,
4495+
appcmdtesting.Run(
4496+
t,
4497+
NewRootCommand,
4498+
appcmdtesting.WithEnv(internaltesting.NewEnvFunc(t)),
4499+
appcmdtesting.WithStdin(stdin),
4500+
appcmdtesting.WithExpectedExitCode(expectedExitCode),
4501+
appcmdtesting.WithExpectedStdout(expectedStdout),
4502+
appcmdtesting.WithExpectedStderr(expectedStderr),
4503+
appcmdtesting.WithArgs(
4504+
// we do not want warnings to be part of our stderr test calculation
4505+
append(
4506+
args,
4507+
"--no-warn",
4508+
)...,
4509+
),
45084510
)
45094511
}
45104512

45114513
func testRunStderrContainsNoWarn(t *testing.T, stdin io.Reader, expectedExitCode int, expectedStderrPartials []string, args ...string) {
4512-
appcmdtesting.RunCommandExitCodeStderrContains(
4513-
t,
4514-
func(use string) *appcmd.Command { return NewRootCommand(use) },
4515-
expectedExitCode,
4516-
expectedStderrPartials,
4517-
internaltesting.NewEnvFunc(t),
4518-
stdin,
4519-
// we do not want warnings to be part of our stderr test calculation
4520-
append(
4521-
args,
4522-
"--no-warn",
4523-
)...,
4514+
appcmdtesting.Run(
4515+
t,
4516+
NewRootCommand,
4517+
appcmdtesting.WithEnv(internaltesting.NewEnvFunc(t)),
4518+
appcmdtesting.WithStdin(stdin),
4519+
appcmdtesting.WithExpectedExitCode(expectedExitCode),
4520+
appcmdtesting.WithExpectedStderrPartials(expectedStderrPartials...),
4521+
appcmdtesting.WithArgs(
4522+
// we do not want warnings to be part of our stderr test calculation
4523+
append(
4524+
args,
4525+
"--no-warn",
4526+
)...,
4527+
),
45244528
)
45254529
}
45264530

@@ -4545,44 +4549,42 @@ func testRun(
45454549
stdout io.Writer,
45464550
args ...string,
45474551
) {
4548-
stderr := bytes.NewBuffer(nil)
4549-
appcmdtesting.RunCommandExitCode(
4552+
appcmdtesting.Run(
45504553
t,
4551-
func(use string) *appcmd.Command { return NewRootCommand(use) },
4552-
expectedExitCode,
4553-
internaltesting.NewEnvFunc(t),
4554-
stdin,
4555-
stdout,
4556-
stderr,
4557-
args...,
4554+
NewRootCommand,
4555+
appcmdtesting.WithEnv(internaltesting.NewEnvFunc(t)),
4556+
appcmdtesting.WithStdin(stdin),
4557+
appcmdtesting.WithStdout(stdout),
4558+
appcmdtesting.WithExpectedExitCode(expectedExitCode),
4559+
appcmdtesting.WithArgs(args...),
45584560
)
45594561
}
45604562

45614563
func getRuleIDsFromLsBreaking(t *testing.T, fileVersion string, useIDs []string, exceptIDs []string) []string {
45624564
t.Helper()
4563-
var stdout bytes.Buffer
4564-
appcmdtesting.RunCommandExitCode(
4565-
t,
4566-
func(use string) *appcmd.Command { return NewRootCommand(use) },
4567-
0,
4568-
internaltesting.NewEnvFunc(t),
4569-
nil,
4570-
&stdout,
4571-
nil,
4572-
"config",
4573-
"ls-breaking-rules",
4574-
"--format=json",
4575-
"--configured-only",
4576-
"--config",
4577-
fmt.Sprintf(
4578-
`{ "version": %q, "breaking": { "use": %s, "except": %s } }`,
4579-
fileVersion,
4580-
"["+strings.Join(slicesext.Map(useIDs, func(s string) string { return strconv.Quote(s) }), ",")+"]",
4581-
"["+strings.Join(slicesext.Map(exceptIDs, func(s string) string { return strconv.Quote(s) }), ",")+"]",
4565+
stdout := bytes.NewBuffer(nil)
4566+
appcmdtesting.Run(
4567+
t,
4568+
NewRootCommand,
4569+
appcmdtesting.WithEnv(internaltesting.NewEnvFunc(t)),
4570+
appcmdtesting.WithStdout(stdout),
4571+
appcmdtesting.WithExpectedExitCode(0),
4572+
appcmdtesting.WithArgs(
4573+
"config",
4574+
"ls-breaking-rules",
4575+
"--format=json",
4576+
"--configured-only",
4577+
"--config",
4578+
fmt.Sprintf(
4579+
`{ "version": %q, "breaking": { "use": %s, "except": %s } }`,
4580+
fileVersion,
4581+
"["+strings.Join(slicesext.Map(useIDs, func(s string) string { return strconv.Quote(s) }), ",")+"]",
4582+
"["+strings.Join(slicesext.Map(exceptIDs, func(s string) string { return strconv.Quote(s) }), ",")+"]",
4583+
),
45824584
),
45834585
)
45844586
var ids []string
4585-
decoder := json.NewDecoder(&stdout)
4587+
decoder := json.NewDecoder(stdout)
45864588
type entry struct {
45874589
ID string
45884590
}
@@ -4617,21 +4619,22 @@ func testLsRuleOutputJSON(
46174619
t.Errorf("invalid rule type %v", ruleType)
46184620
t.FailNow()
46194621
}
4620-
appcmdtesting.RunCommandExitCode(
4621-
t,
4622-
func(use string) *appcmd.Command { return NewRootCommand(use) },
4623-
0,
4624-
internaltesting.NewEnvFunc(t),
4625-
nil,
4626-
stdout,
4627-
stderr,
4628-
"config",
4629-
command,
4630-
"--configured-only",
4631-
"--config",
4632-
config,
4633-
"--format",
4634-
"json",
4622+
appcmdtesting.Run(
4623+
t,
4624+
NewRootCommand,
4625+
appcmdtesting.WithEnv(internaltesting.NewEnvFunc(t)),
4626+
appcmdtesting.WithStdout(stdout),
4627+
appcmdtesting.WithStderr(stderr),
4628+
appcmdtesting.WithExpectedExitCode(0),
4629+
appcmdtesting.WithArgs(
4630+
"config",
4631+
command,
4632+
"--configured-only",
4633+
"--config",
4634+
config,
4635+
"--format",
4636+
"json",
4637+
),
46354638
)
46364639
outputRules :=
46374640
slicesext.Map(

0 commit comments

Comments
 (0)