Skip to content

Commit ee72ad0

Browse files
authored
tools: Remove BicepCli interface (#4116)
When we started to depend on external tools, we used a pattern of defining an interface to represent the capabilities of the tool which the rest of the system dependend on. The thought was having this interface indirection would help us with unit-testing, because we could mock different parts of the tool directly instead of depending on mocks via the external command runner. In practice, we never did this, when we test things, we just mock at the `exec.CommandRunner` layer or use something like the recorder to capture all the external tool interactions and replay them later. Since we don't have multiple implementations of the interface, let's just remove it and write things in the more direct style as we haven been doing more recently where we don't have the intermediate interface. With this change, `bicep.BicepCli` (an interface) is now replaced with `*bicep.Cli` which is logically what was returned in the past anyway, but behind the guise of the interface).
1 parent 0327ac8 commit ee72ad0

File tree

7 files changed

+37
-42
lines changed

7 files changed

+37
-42
lines changed

cli/azd/internal/scaffold/scaffold_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func TestExecInfra(t *testing.T) {
149149
}
150150

151151
ctx := context.Background()
152-
cli, err := bicep.NewBicepCli(ctx, mockinput.NewMockConsole(), exec.NewCommandRunner(nil))
152+
cli, err := bicep.NewCli(ctx, mockinput.NewMockConsole(), exec.NewCommandRunner(nil))
153153
require.NoError(t, err)
154154

155155
res, err := cli.Build(ctx, filepath.Join(dir, "main.bicep"))

cli/azd/pkg/azd/default.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (p *DefaultPlatform) IsEnabled() bool {
4646
func (p *DefaultPlatform) ConfigureContainer(container *ioc.NestedContainer) error {
4747
// Tools
4848
container.MustRegisterSingleton(terraform.NewTerraformCli)
49-
container.MustRegisterSingleton(bicep.NewBicepCli)
49+
container.MustRegisterSingleton(bicep.NewCli)
5050

5151
// Provisioning Providers
5252
provisionProviderMap := map[provisioning.ProviderKind]any{

cli/azd/pkg/infra/provisioning/bicep/bicep_provider.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type BicepProvider struct {
6767
projectPath string
6868
options Options
6969
console input.Console
70-
bicepCli bicep.BicepCli
70+
bicepCli *bicep.Cli
7171
azCli azcli.AzCli
7272
deploymentsService azapi.Deployments
7373
deploymentOperations azapi.DeploymentOperations
@@ -2194,7 +2194,7 @@ func isValueAssignableToParameterType(paramType ParameterType, value any) bool {
21942194

21952195
// NewBicepProvider creates a new instance of a Bicep Infra provider
21962196
func NewBicepProvider(
2197-
bicepCli bicep.BicepCli,
2197+
bicepCli *bicep.Cli,
21982198
azCli azcli.AzCli,
21992199
deploymentsService azapi.Deployments,
22002200
deploymentOperations azapi.DeploymentOperations,

cli/azd/pkg/infra/provisioning/bicep/bicep_provider_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func TestBicepPlanPrompt(t *testing.T) {
8686
mockContext.CommandRunner.When(func(args exec.RunArgs, command string) bool {
8787
return strings.Contains(args.Cmd, "bicep") && args.Args[0] == "--version"
8888
}).Respond(exec.RunResult{
89-
Stdout: fmt.Sprintf("Bicep CLI version %s (abcdef0123)", bicep.BicepVersion.String()),
89+
Stdout: fmt.Sprintf("Bicep CLI version %s (abcdef0123)", bicep.Version.String()),
9090
Stderr: "",
9191
})
9292

@@ -201,7 +201,7 @@ func TestPlanForResourceGroup(t *testing.T) {
201201
mockContext.CommandRunner.When(func(args exec.RunArgs, command string) bool {
202202
return strings.Contains(args.Cmd, "bicep") && strings.Contains(command, "--version")
203203
}).RespondFn(func(args exec.RunArgs) (exec.RunResult, error) {
204-
return exec.NewRunResult(0, fmt.Sprintf("Bicep CLI version %s (abcdef0123)", bicep.BicepVersion), ""), nil
204+
return exec.NewRunResult(0, fmt.Sprintf("Bicep CLI version %s (abcdef0123)", bicep.Version), ""), nil
205205
})
206206

207207
// Have `bicep build` return a ARM template that targets a resource group.
@@ -355,7 +355,7 @@ func createBicepProvider(t *testing.T, mockContext *mocks.MockContext) *BicepPro
355355
envManager := &mockenv.MockEnvManager{}
356356
envManager.On("Save", mock.Anything, mock.Anything).Return(nil)
357357

358-
bicepCli, err := bicep.NewBicepCli(*mockContext.Context, mockContext.Console, mockContext.CommandRunner)
358+
bicepCli, err := bicep.NewCli(*mockContext.Context, mockContext.Console, mockContext.CommandRunner)
359359
require.NoError(t, err)
360360
azCli := mockazcli.NewAzCliFromMockContext(mockContext)
361361
depOpService := mockazcli.NewDeploymentOperationsServiceFromMockContext(mockContext)
@@ -425,7 +425,7 @@ func prepareBicepMocks(
425425
mockContext.CommandRunner.When(func(args exec.RunArgs, command string) bool {
426426
return strings.Contains(args.Cmd, "bicep") && args.Args[0] == "--version"
427427
}).Respond(exec.RunResult{
428-
Stdout: fmt.Sprintf("Bicep CLI version %s (abcdef0123)", bicep.BicepVersion.String()),
428+
Stdout: fmt.Sprintf("Bicep CLI version %s (abcdef0123)", bicep.Version.String()),
429429
Stderr: "",
430430
})
431431

@@ -809,7 +809,7 @@ func TestFindCompletedDeployments(t *testing.T) {
809809
mockContext.CommandRunner.When(func(args exec.RunArgs, command string) bool {
810810
return strings.Contains(args.Cmd, "bicep") && strings.Contains(command, "--version")
811811
}).RespondFn(func(args exec.RunArgs) (exec.RunResult, error) {
812-
return exec.NewRunResult(0, fmt.Sprintf("Bicep CLI version %s (abcdef0123)", bicep.BicepVersion), ""), nil
812+
return exec.NewRunResult(0, fmt.Sprintf("Bicep CLI version %s (abcdef0123)", bicep.Version), ""), nil
813813
})
814814
// Have `bicep build` return a ARM template that targets a resource group.
815815
mockContext.CommandRunner.When(func(args exec.RunArgs, command string) bool {
@@ -907,11 +907,11 @@ func TestUserDefinedTypes(t *testing.T) {
907907
mockContext.CommandRunner.When(func(args exec.RunArgs, command string) bool {
908908
return strings.Contains(args.Cmd, "bicep") && args.Args[0] == "--version"
909909
}).Respond(exec.RunResult{
910-
Stdout: fmt.Sprintf("Bicep CLI version %s (abcdef0123)", bicep.BicepVersion.String()),
910+
Stdout: fmt.Sprintf("Bicep CLI version %s (abcdef0123)", bicep.Version.String()),
911911
Stderr: "",
912912
})
913913

914-
bicepCli, err := bicep.NewBicepCli(*mockContext.Context, mockContext.Console, mockContext.CommandRunner)
914+
bicepCli, err := bicep.NewCli(*mockContext.Context, mockContext.Console, mockContext.CommandRunner)
915915
require.NoError(t, err)
916916
env := environment.NewWithValues("test-env", map[string]string{})
917917

cli/azd/pkg/tools/bicep/bicep.go

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,33 @@ import (
2525
"github.com/blang/semver/v4"
2626
)
2727

28-
// BicepVersion is the minimum version of bicep that we require (and the one we fetch when we fetch bicep on behalf of a
28+
// Version is the minimum version of bicep that we require (and the one we fetch when we fetch bicep on behalf of a
2929
// user).
30-
var BicepVersion semver.Version = semver.MustParse("0.28.1")
30+
var Version semver.Version = semver.MustParse("0.28.1")
3131

32-
type BicepCli interface {
33-
Build(ctx context.Context, file string) (BuildResult, error)
34-
BuildBicepParam(ctx context.Context, file string, env []string) (BuildResult, error)
35-
}
36-
37-
// NewBicepCli creates a new BicepCli. Azd manages its own copy of the bicep CLI, stored in `$AZD_CONFIG_DIR/bin`. If
32+
// NewCli creates a new Bicep CLI. Azd manages its own copy of the bicep CLI, stored in `$AZD_CONFIG_DIR/bin`. If
3833
// bicep is not present at this location, or if it is present but is older than the minimum supported version, it is
3934
// downloaded.
40-
func NewBicepCli(
35+
func NewCli(
4136
ctx context.Context,
4237
console input.Console,
4338
commandRunner exec.CommandRunner,
44-
) (BicepCli, error) {
45-
return newBicepCliWithTransporter(ctx, console, commandRunner, http.DefaultClient)
39+
) (*Cli, error) {
40+
return newCliWithTransporter(ctx, console, commandRunner, http.DefaultClient)
4641
}
4742

48-
// newBicepCliWithTransporter is like NewBicepCli but allows providing a custom transport to use when downloading the
49-
// bicep CLI, for testing purposes.
50-
func newBicepCliWithTransporter(
43+
// newCliWithTransporter is like NewBicepCli but allows providing a custom transport to use when downloading the
44+
// Bicep CLI, for testing purposes.
45+
func newCliWithTransporter(
5146
ctx context.Context,
5247
console input.Console,
5348
commandRunner exec.CommandRunner,
5449
transporter policy.Transporter,
55-
) (BicepCli, error) {
50+
) (*Cli, error) {
5651
if override := os.Getenv("AZD_BICEP_TOOL_PATH"); override != "" {
5752
log.Printf("using external bicep tool: %s", override)
5853

59-
return &bicepCli{
54+
return &Cli{
6055
path: override,
6156
runner: commandRunner,
6257
}, nil
@@ -77,14 +72,14 @@ func newBicepCliWithTransporter(
7772

7873
if err := runStep(
7974
ctx, console, "Downloading Bicep", func() error {
80-
return downloadBicep(ctx, transporter, BicepVersion, bicepPath)
75+
return downloadBicep(ctx, transporter, Version, bicepPath)
8176
},
8277
); err != nil {
8378
return nil, fmt.Errorf("downloading bicep: %w", err)
8479
}
8580
}
8681

87-
cli := &bicepCli{
82+
cli := &Cli{
8883
path: bicepPath,
8984
runner: commandRunner,
9085
}
@@ -96,12 +91,12 @@ func newBicepCliWithTransporter(
9691

9792
log.Printf("bicep version: %s", ver)
9893

99-
if ver.LT(BicepVersion) {
100-
log.Printf("installed bicep version %s is older than %s; updating.", ver.String(), BicepVersion.String())
94+
if ver.LT(Version) {
95+
log.Printf("installed bicep version %s is older than %s; updating.", ver.String(), Version.String())
10196

10297
if err := runStep(
10398
ctx, console, "Upgrading Bicep", func() error {
104-
return downloadBicep(ctx, transporter, BicepVersion, bicepPath)
99+
return downloadBicep(ctx, transporter, Version, bicepPath)
105100
},
106101
); err != nil {
107102
return nil, fmt.Errorf("upgrading bicep: %w", err)
@@ -127,7 +122,7 @@ func runStep(ctx context.Context, console input.Console, title string, action fu
127122
return nil
128123
}
129124

130-
type bicepCli struct {
125+
type Cli struct {
131126
path string
132127
runner exec.CommandRunner
133128
}
@@ -245,7 +240,7 @@ func preferMuslBicep(stat stater) bool {
245240
return false
246241
}
247242

248-
func (cli *bicepCli) version(ctx context.Context) (semver.Version, error) {
243+
func (cli *Cli) version(ctx context.Context) (semver.Version, error) {
249244
bicepRes, err := cli.runCommand(ctx, nil, "--version")
250245
if err != nil {
251246
return semver.Version{}, err
@@ -268,7 +263,7 @@ type BuildResult struct {
268263
LintErr string
269264
}
270265

271-
func (cli *bicepCli) Build(ctx context.Context, file string) (BuildResult, error) {
266+
func (cli *Cli) Build(ctx context.Context, file string) (BuildResult, error) {
272267
args := []string{"build", file, "--stdout"}
273268
buildRes, err := cli.runCommand(ctx, nil, args...)
274269

@@ -285,7 +280,7 @@ func (cli *bicepCli) Build(ctx context.Context, file string) (BuildResult, error
285280
}, nil
286281
}
287282

288-
func (cli *bicepCli) BuildBicepParam(ctx context.Context, file string, env []string) (BuildResult, error) {
283+
func (cli *Cli) BuildBicepParam(ctx context.Context, file string, env []string) (BuildResult, error) {
289284
args := []string{"build-params", file, "--stdout"}
290285
buildRes, err := cli.runCommand(ctx, env, args...)
291286

@@ -302,7 +297,7 @@ func (cli *bicepCli) BuildBicepParam(ctx context.Context, file string, env []str
302297
}, nil
303298
}
304299

305-
func (cli *bicepCli) runCommand(ctx context.Context, env []string, args ...string) (exec.RunResult, error) {
300+
func (cli *Cli) runCommand(ctx context.Context, env []string, args ...string) (exec.RunResult, error) {
306301
runArgs := exec.NewRunArgs(cli.path, args...)
307302
if env != nil {
308303
runArgs = runArgs.WithEnv(env)

cli/azd/pkg/tools/bicep/bicep_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ func TestNewBicepCli(t *testing.T) {
3838
return strings.Contains(args.Cmd, "bicep") && len(args.Args) == 1 && args.Args[0] == "--version"
3939
}).Respond(exec.NewRunResult(
4040
0,
41-
fmt.Sprintf("Bicep CLI version %s (abcdef0123)", BicepVersion.String()),
41+
fmt.Sprintf("Bicep CLI version %s (abcdef0123)", Version.String()),
4242
"",
4343
))
4444

45-
cli, err := newBicepCliWithTransporter(
45+
cli, err := newCliWithTransporter(
4646
*mockContext.Context, mockContext.Console, mockContext.CommandRunner, mockContext.HttpClient,
4747
)
4848
require.NoError(t, err)
@@ -108,13 +108,13 @@ func TestNewBicepCliWillUpgrade(t *testing.T) {
108108
case OLD_FILE_CONTENTS:
109109
return exec.NewRunResult(0, "Bicep CLI version 0.0.1 (badbadbad1)", ""), nil
110110
case NEW_FILE_CONTENTS:
111-
return exec.NewRunResult(0, fmt.Sprintf("Bicep CLI version %s (abcdef0123)", BicepVersion.String()), ""), nil
111+
return exec.NewRunResult(0, fmt.Sprintf("Bicep CLI version %s (abcdef0123)", Version.String()), ""), nil
112112
}
113113

114114
return exec.NewRunResult(-1, "", "unexpected bicep file contents"), err
115115
})
116116

117-
cli, err := newBicepCliWithTransporter(
117+
cli, err := newCliWithTransporter(
118118
*mockContext.Context, mockContext.Console, mockContext.CommandRunner, mockContext.HttpClient,
119119
)
120120
require.NoError(t, err)

cli/azd/test/functional/aspire_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func Test_CLI_Aspire_DetectGen(t *testing.T) {
207207
_, err = cli.RunCommand(ctx, "infra", "synth")
208208
require.NoError(t, err)
209209

210-
bicepCli, err := bicep.NewBicepCli(ctx, mockinput.NewMockConsole(), exec.NewCommandRunner(nil))
210+
bicepCli, err := bicep.NewCli(ctx, mockinput.NewMockConsole(), exec.NewCommandRunner(nil))
211211
require.NoError(t, err)
212212

213213
// Validate bicep builds without errors

0 commit comments

Comments
 (0)