Skip to content

Commit 4b7d6fa

Browse files
hemarinaCopilot
andauthored
Phase 4-6: Improve test coverage to 65% and modernize test infrastructure (#7862)
* phase 4 * phase 5 * Phase 6 * address copilot feedback * address feedback and fix pipeline failure * Resolve merge conflict * Align prompt tests with post-merge behavior Remove tests that asserted features removed in main (auto-select subscription/location, no-prompt 'resource not found' code paths) and reshape resource-prompt no-prompt tests to exercise the real ErrNoResourcesFound path via empty selectors + AllowNewResource=false. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix the gap created by merge main for phase 6 --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent be4e633 commit 4b7d6fa

275 files changed

Lines changed: 13358 additions & 2054 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cli/azd/cmd/auth_login_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package cmd
55

66
import (
7-
"context"
87
"errors"
98
"testing"
109

@@ -47,7 +46,7 @@ func TestRunningOnCodespacesBrowser(t *testing.T) {
4746
for _, tt := range tests {
4847
t.Run(tt.name, func(t *testing.T) {
4948
t.Parallel()
50-
mockContext := mocks.NewMockContext(context.Background())
49+
mockContext := mocks.NewMockContext(t.Context())
5150

5251
if tt.runErr != nil {
5352
mockContext.CommandRunner.When(func(args exec.RunArgs, commandName string) bool {
@@ -100,7 +99,7 @@ func TestParseUseDeviceCode(t *testing.T) {
10099
for _, tt := range tests {
101100
t.Run(tt.name, func(t *testing.T) {
102101
t.Parallel()
103-
mockContext := mocks.NewMockContext(context.Background())
102+
mockContext := mocks.NewMockContext(t.Context())
104103

105104
// Mock code --status to return non-browser env
106105
mockContext.CommandRunner.When(func(args exec.RunArgs, commandName string) bool {

cli/azd/cmd/auth_token_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestAuthToken(t *testing.T) {
5555
cloud.AzurePublic(),
5656
)
5757

58-
_, err := a.Run(context.Background())
58+
_, err := a.Run(t.Context())
5959
require.NoError(t, err)
6060
require.True(t, wasCalled, "GetToken was not called on the credential")
6161

@@ -127,7 +127,7 @@ func TestAuthTokenSysEnv(t *testing.T) {
127127
cloud.AzurePublic(),
128128
)
129129

130-
_, err := a.Run(context.Background())
130+
_, err := a.Run(t.Context())
131131
require.NoError(t, err)
132132

133133
var res contracts.AuthTokenResult
@@ -175,7 +175,7 @@ func TestAuthTokenSysEnvError(t *testing.T) {
175175
cloud.AzurePublic(),
176176
)
177177

178-
_, err := a.Run(context.Background())
178+
_, err := a.Run(t.Context())
179179
require.ErrorContains(
180180
t,
181181
err,
@@ -219,7 +219,7 @@ func TestAuthTokenAzdEnvError(t *testing.T) {
219219
cloud.AzurePublic(),
220220
)
221221

222-
_, err := a.Run(context.Background())
222+
_, err := a.Run(t.Context())
223223
require.ErrorContains(
224224
t,
225225
err,
@@ -260,7 +260,7 @@ func TestAuthTokenAzdEnv(t *testing.T) {
260260
cloud.AzurePublic(),
261261
)
262262

263-
_, err := a.Run(context.Background())
263+
_, err := a.Run(t.Context())
264264
require.NoError(t, err)
265265

266266
var res contracts.AuthTokenResult
@@ -301,7 +301,7 @@ func TestAuthTokenAzdEnvWithEmpty(t *testing.T) {
301301
cloud.AzurePublic(),
302302
)
303303

304-
_, err := a.Run(context.Background())
304+
_, err := a.Run(t.Context())
305305
require.NoError(t, err)
306306

307307
var res contracts.AuthTokenResult
@@ -338,7 +338,7 @@ func TestAuthTokenCustomScopes(t *testing.T) {
338338
cloud.AzurePublic(),
339339
)
340340

341-
_, err := a.Run(context.Background())
341+
_, err := a.Run(t.Context())
342342
require.NoError(t, err)
343343
require.True(t, wasCalled, "GetToken was not called on the credential")
344344
}
@@ -360,7 +360,7 @@ func TestAuthTokenFailure(t *testing.T) {
360360
cloud.AzurePublic(),
361361
)
362362

363-
_, err := a.Run(context.Background())
363+
_, err := a.Run(t.Context())
364364
require.ErrorContains(t, err, "could not fetch token")
365365
}
366366

cli/azd/cmd/cobra_builder_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func Test_BuildAndRunSimpleCommand(t *testing.T) {
4747

4848
// Disable args processing from os:args
4949
cmd.SetArgs([]string{})
50-
err = cmd.ExecuteContext(context.Background())
50+
err = cmd.ExecuteContext(t.Context())
5151

5252
require.NoError(t, err)
5353
require.True(t, ran)
@@ -70,7 +70,7 @@ func Test_BuildAndRunSimpleAction(t *testing.T) {
7070
require.NoError(t, err)
7171

7272
cmd.SetArgs([]string{"-r"})
73-
err = cmd.ExecuteContext(context.Background())
73+
err = cmd.ExecuteContext(t.Context())
7474

7575
require.NoError(t, err)
7676
}
@@ -94,7 +94,7 @@ func Test_BuildAndRunSimpleActionWithMiddleware(t *testing.T) {
9494
actionRan := false
9595
middlewareRan := false
9696

97-
ctx := context.Background()
97+
ctx := t.Context()
9898
ctx = context.WithValue(ctx, actionName, &actionRan)
9999
ctx = context.WithValue(ctx, middlewareAName, &middlewareRan)
100100

@@ -129,7 +129,7 @@ func Test_BuildAndRunActionWithNestedMiddleware(t *testing.T) {
129129
middlewareARan := false
130130
middlewareBRan := false
131131

132-
ctx := context.Background()
132+
ctx := t.Context()
133133
ctx = context.WithValue(ctx, actionName, &actionRan)
134134
ctx = context.WithValue(ctx, middlewareAName, &middlewareARan)
135135
ctx = context.WithValue(ctx, middlewareBName, &middlewareBRan)
@@ -172,7 +172,7 @@ func Test_BuildAndRunActionWithNestedAndConditionalMiddleware(t *testing.T) {
172172
middlewareARan := false
173173
middlewareBRan := false
174174

175-
ctx := context.Background()
175+
ctx := t.Context()
176176
ctx = context.WithValue(ctx, actionName, &actionRan)
177177
ctx = context.WithValue(ctx, middlewareAName, &middlewareARan)
178178
ctx = context.WithValue(ctx, middlewareBName, &middlewareBRan)
@@ -229,7 +229,7 @@ func Test_BuildCommandsWithAutomaticHelpAndOutputFlags(t *testing.T) {
229229
func Test_RunDocsFlow(t *testing.T) {
230230
t.Parallel()
231231
container := ioc.NewNestedContainer(nil)
232-
testCtx := mocks.NewMockContext(context.Background())
232+
testCtx := mocks.NewMockContext(t.Context())
233233
container.MustRegisterSingleton(func() input.Console {
234234
return testCtx.Console
235235
})
@@ -264,7 +264,7 @@ func Test_RunDocsFlow(t *testing.T) {
264264
func Test_RunDocsAndHelpFlow(t *testing.T) {
265265
t.Parallel()
266266
container := ioc.NewNestedContainer(nil)
267-
testCtx := mocks.NewMockContext(context.Background())
267+
testCtx := mocks.NewMockContext(t.Context())
268268
container.MustRegisterSingleton(func() input.Console {
269269
return testCtx.Console
270270
})

cli/azd/cmd/config_options_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package cmd
55

66
import (
77
"bytes"
8-
"context"
98
"encoding/json"
109
"testing"
1110

@@ -19,7 +18,7 @@ import (
1918
func TestConfigOptionsAction_JSON(t *testing.T) {
2019
t.Parallel()
2120
buf := &bytes.Buffer{}
22-
mockContext := mocks.NewMockContext(context.Background())
21+
mockContext := mocks.NewMockContext(t.Context())
2322
console := mockContext.Console
2423

2524
// Create a temporary config file
@@ -95,7 +94,7 @@ func TestConfigOptionsAction_JSON(t *testing.T) {
9594
func TestConfigOptionsAction_Table(t *testing.T) {
9695
t.Parallel()
9796
buf := &bytes.Buffer{}
98-
mockContext := mocks.NewMockContext(context.Background())
97+
mockContext := mocks.NewMockContext(t.Context())
9998
console := mockContext.Console
10099

101100
tempDir := t.TempDir()
@@ -133,7 +132,7 @@ func TestConfigOptionsAction_Table(t *testing.T) {
133132
func TestConfigOptionsAction_DefaultFormat(t *testing.T) {
134133
t.Parallel()
135134
buf := &bytes.Buffer{}
136-
mockContext := mocks.NewMockContext(context.Background())
135+
mockContext := mocks.NewMockContext(t.Context())
137136
console := mockContext.Console
138137

139138
tempDir := t.TempDir()

cli/azd/cmd/config_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package cmd
55

66
import (
77
"bytes"
8-
"context"
98
"encoding/json"
109
"path/filepath"
1110
"testing"
@@ -65,7 +64,7 @@ func TestConfigShowAction_NoneFormat(t *testing.T) {
6564

6665
func TestConfigListAction_DelegatesAndShowsWarning(t *testing.T) {
6766
buf := &bytes.Buffer{}
68-
mockContext := mocks.NewMockContext(context.Background())
67+
mockContext := mocks.NewMockContext(t.Context())
6968
userConfigManager := newTestUserConfigManager(t)
7069

7170
showAction := &configShowAction{
@@ -165,7 +164,7 @@ func TestConfigUnsetAction(t *testing.T) {
165164
}
166165

167166
func TestConfigResetAction_WithForce(t *testing.T) {
168-
mockContext := mocks.NewMockContext(context.Background())
167+
mockContext := mocks.NewMockContext(t.Context())
169168
userConfigManager := newTestUserConfigManager(t)
170169

171170
// Set a value first
@@ -196,7 +195,7 @@ func TestConfigResetAction_WithForce(t *testing.T) {
196195
}
197196

198197
func TestConfigResetAction_UserDeclines(t *testing.T) {
199-
mockContext := mocks.NewMockContext(context.Background())
198+
mockContext := mocks.NewMockContext(t.Context())
200199
userConfigManager := newTestUserConfigManager(t)
201200

202201
mockContext.Console.WhenConfirm(func(options input.ConsoleOptions) bool {
@@ -231,7 +230,7 @@ func TestConfigResetAction_UserDeclines(t *testing.T) {
231230
}
232231

233232
func TestConfigListAlphaAction_Run(t *testing.T) {
234-
mockContext := mocks.NewMockContext(context.Background())
233+
mockContext := mocks.NewMockContext(t.Context())
235234

236235
featureManager := alpha.NewFeaturesManagerWithConfig(config.NewEmptyConfig())
237236
action := newConfigListAlphaAction(featureManager, mockContext.Console, []string{})

cli/azd/cmd/container_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
func Test_Lazy_Project_Config_Resolution(t *testing.T) {
2222
t.Parallel()
23-
ctx := context.Background()
23+
ctx := t.Context()
2424
container := ioc.NewNestedContainer(nil)
2525
ioc.RegisterInstance(container, ctx)
2626

@@ -90,7 +90,7 @@ func Test_Lazy_Project_Config_Resolution(t *testing.T) {
9090

9191
func Test_Lazy_AzdContext_Resolution(t *testing.T) {
9292
t.Parallel()
93-
ctx := context.Background()
93+
ctx := t.Context()
9494
container := ioc.NewNestedContainer(nil)
9595
ioc.RegisterInstance(container, ctx)
9696

@@ -200,7 +200,7 @@ func Test_workflowCmdAdapter_ContextPropagation(t *testing.T) {
200200

201201
// In production, main.go wraps with context.WithoutCancel.
202202
// Simulate this by using a non-cancellable context.
203-
ctx := context.WithoutCancel(context.Background())
203+
ctx := context.WithoutCancel(t.Context())
204204
err := adapter.ExecuteContext(ctx, []string{"sub"})
205205
require.NoError(t, err)
206206
require.Len(t, receivedContexts, 1, "Execution should have received context")
@@ -257,7 +257,7 @@ func Test_workflowCmdAdapter_ContextPropagation(t *testing.T) {
257257
adapter := &workflowCmdAdapter{newCommand: newCommand}
258258

259259
// In production, main.go wraps with context.WithoutCancel.
260-
ctx := context.WithoutCancel(context.Background())
260+
ctx := context.WithoutCancel(t.Context())
261261
err := adapter.ExecuteContext(ctx, []string{"parent", "child"})
262262
require.NoError(t, err)
263263
require.Len(t, receivedContexts, 1)
@@ -300,7 +300,7 @@ func Test_workflowCmdAdapter_ContextPropagation(t *testing.T) {
300300
}
301301

302302
adapter := &workflowCmdAdapter{newCommand: newCommand}
303-
ctx := context.WithoutCancel(context.Background())
303+
ctx := context.WithoutCancel(t.Context())
304304

305305
err := adapter.ExecuteContext(ctx, []string{"test"})
306306
require.NoError(t, err)
@@ -357,7 +357,7 @@ func Test_workflowCmdAdapter_ContextPropagation(t *testing.T) {
357357
globalArgs: globalArgs,
358358
}
359359

360-
err := adapter.ExecuteContext(context.WithoutCancel(context.Background()), []string{"package", "--all"})
360+
err := adapter.ExecuteContext(context.WithoutCancel(t.Context()), []string{"package", "--all"})
361361
require.NoError(t, err)
362362
require.True(t, debugEnabled, "global --debug flag should still be parsed on the rebuilt tree")
363363
require.Empty(t, capturedPositionalArgs,
@@ -368,7 +368,7 @@ func Test_workflowCmdAdapter_ContextPropagation(t *testing.T) {
368368
// Verify that building a real command tree via NewRootCmd preserves
369369
// the full middleware chain (debug, ux, telemetry, error, loginGuard, etc.)
370370
container := ioc.NewNestedContainer(nil)
371-
ctx := context.WithoutCancel(context.Background())
371+
ctx := context.WithoutCancel(t.Context())
372372
ioc.RegisterInstance(container, ctx)
373373
ioc.RegisterInstance(container, &internal.GlobalCommandOptions{})
374374

@@ -437,7 +437,7 @@ func Test_workflowCmdAdapter_ContextPropagation(t *testing.T) {
437437
}
438438

439439
adapter := &workflowCmdAdapter{newCommand: newCommand}
440-
ctx := context.WithoutCancel(context.Background())
440+
ctx := context.WithoutCancel(t.Context())
441441

442442
// Execute "provision" through the adapter (simulates workflow step)
443443
err := adapter.ExecuteContext(ctx, []string{"provision"})
@@ -476,7 +476,7 @@ func Test_workflowCmdAdapter_ContextPropagation(t *testing.T) {
476476
}
477477

478478
adapter := &workflowCmdAdapter{newCommand: newCommand}
479-
ctx := context.WithoutCancel(context.Background())
479+
ctx := context.WithoutCancel(t.Context())
480480

481481
// Simulate the default "up" workflow steps
482482
steps := [][]string{
@@ -511,7 +511,7 @@ func Test_NewRootCmd_ReregistrationReplacesProjectConfig(t *testing.T) {
511511
// 6. Use newRootCmdWithoutRegistration instead, validate handler is preserved (proving the fix)
512512

513513
container := ioc.NewNestedContainer(nil)
514-
ctx := context.WithoutCancel(context.Background())
514+
ctx := context.WithoutCancel(t.Context())
515515
ioc.RegisterInstance(container, ctx)
516516
ioc.RegisterInstance(container, &internal.GlobalCommandOptions{})
517517

cli/azd/cmd/deeper_coverage3_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ func Test_EnvSetSecretAction_UsesResourceTenantForKeyVaultAndPrincipalId(t *test
10081008
"secret-value",
10091009
).Return(nil)
10101010

1011-
mockContext := mocks.NewMockContext(context.Background())
1011+
mockContext := mocks.NewMockContext(t.Context())
10121012
userProfileService := azapi.NewUserProfileService(
10131013
&mocks.MockMultiTenantCredentialProvider{
10141014
TokenMap: map[string]mocks.MockCredentials{

0 commit comments

Comments
 (0)