Skip to content

Commit a486bac

Browse files
authored
test: stub AreCredsFromEnvVars in app init to speed tests by 70s (#2465)
🤦 Previously, we were calling sessions.AreCredsFromEnvVars in our tests which would timeout after 10s: https://github.com/aws/copilot-cli/blob/f2f0ee317e01b9da85ab72ecd64481c10658cd05/internal/pkg/aws/sessions/sessions.go#L28 Since the test TestInitAppOpts_Ask has 7 testcases it would take 70s to test the cli package. With this one weird trick™️ and replacing the functional call with a fake, we speed up our tests by 70s. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 8236f9b commit a486bac

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

internal/pkg/cli/app_init.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ type initAppVars struct {
4141
type initAppOpts struct {
4242
initAppVars
4343

44-
identity identityService
45-
store applicationStore
46-
route53 domainHostedZoneGetter
47-
ws wsAppManager
48-
cfn appDeployer
49-
prompt prompter
50-
prog progress
44+
identity identityService
45+
store applicationStore
46+
route53 domainHostedZoneGetter
47+
ws wsAppManager
48+
cfn appDeployer
49+
prompt prompter
50+
prog progress
51+
isSessionFromEnvVars func() (bool, error)
5152

5253
cachedHostedZoneID string
5354
}
@@ -75,6 +76,9 @@ func newInitAppOpts(vars initAppVars) (*initAppOpts, error) {
7576
cfn: cloudformation.New(sess),
7677
prompt: prompt.New(),
7778
prog: termprogress.NewSpinner(log.DiagnosticWriter),
79+
isSessionFromEnvVars: func() (bool, error) {
80+
return sessions.AreCredsFromEnvVars(sess)
81+
},
7882
}, nil
7983
}
8084

@@ -100,11 +104,7 @@ func (o *initAppOpts) Validate() error {
100104

101105
// Ask prompts the user for any required arguments that they didn't provide.
102106
func (o *initAppOpts) Ask() error {
103-
sess, err := sessions.NewProvider().Default()
104-
if err != nil {
105-
return fmt.Errorf("get default session: %w", err)
106-
}
107-
if ok, _ := sessions.AreCredsFromEnvVars(sess); ok { // Ignore the error, we do not want to crash for a warning.
107+
if ok, _ := o.isSessionFromEnvVars(); ok { // Ignore the error, we do not want to crash for a warning.
108108
log.Warningln(`Looks like you're creating an application using credentials set by environment variables.
109109
Copilot will store your application metadata in this account.
110110
We recommend using credentials from named profiles. To learn more:

internal/pkg/cli/app_init_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ func TestInitAppOpts_Ask(t *testing.T) {
266266
store: mocks.NewMockstore(ctrl),
267267
ws: mocks.NewMockwsAppManager(ctrl),
268268
prompt: mocks.NewMockprompter(ctrl),
269+
isSessionFromEnvVars: func() (bool, error) {
270+
return false, nil
271+
},
269272
}
270273
tc.expect(opts)
271274

internal/pkg/cli/init.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ func newInitOpts(vars initVars) (*initOpts, error) {
116116
identity: id,
117117
cfn: deployer,
118118
prog: spin,
119+
isSessionFromEnvVars: func() (bool, error) {
120+
return sessions.AreCredsFromEnvVars(defaultSess)
121+
},
119122
}
120123
initEnvCmd := &initEnvOpts{
121124
initEnvVars: initEnvVars{

0 commit comments

Comments
 (0)