6
6
import * as assert from 'assert'
7
7
import * as fs from 'fs-extra'
8
8
import * as path from 'path'
9
+ import * as sinon from 'sinon'
9
10
10
11
import { Uri } from 'vscode'
11
12
import { loadSharedConfigFiles } from '../../../shared/credentials/credentialsFile'
@@ -23,9 +24,7 @@ describe('UserCredentialsUtils', function () {
23
24
} )
24
25
25
26
afterEach ( async function ( ) {
26
- const env = process . env as EnvironmentVariables
27
- delete env . AWS_SHARED_CREDENTIALS_FILE
28
- delete env . AWS_CONFIG_FILE
27
+ sinon . restore ( )
29
28
} )
30
29
31
30
after ( async function ( ) {
@@ -37,30 +36,30 @@ describe('UserCredentialsUtils', function () {
37
36
const credentialsFilename = path . join ( tempFolder , 'credentials-both-exist-test' )
38
37
const configFilename = path . join ( tempFolder , 'config-both-exist-test' )
39
38
40
- const env = process . env as EnvironmentVariables
41
- env . AWS_SHARED_CREDENTIALS_FILE = credentialsFilename
42
- env . AWS_CONFIG_FILE = configFilename
39
+ sinon . stub ( process , 'env' ) . value ( {
40
+ AWS_SHARED_CREDENTIALS_FILE : credentialsFilename ,
41
+ AWS_CONFIG_FILE : configFilename ,
42
+ } as EnvironmentVariables )
43
43
44
44
createCredentialsFile ( credentialsFilename , [ 'default' ] )
45
45
createCredentialsFile ( configFilename , [ 'default' ] )
46
46
47
47
const foundFiles : string [ ] = await UserCredentialsUtils . findExistingCredentialsFilenames ( )
48
- assert ( foundFiles )
49
48
assert . strictEqual ( foundFiles . length , 2 )
50
49
} )
51
50
52
51
it ( 'returns credentials file if it exists and config file does not exist' , async function ( ) {
53
52
const credentialsFilename = path . join ( tempFolder , 'credentials-exist-test' )
54
53
const configFilename = path . join ( tempFolder , 'config-not-exist-test' )
55
54
56
- const env = process . env as EnvironmentVariables
57
- env . AWS_SHARED_CREDENTIALS_FILE = credentialsFilename
58
- env . AWS_CONFIG_FILE = configFilename
55
+ sinon . stub ( process , 'env' ) . value ( {
56
+ AWS_SHARED_CREDENTIALS_FILE : credentialsFilename ,
57
+ AWS_CONFIG_FILE : configFilename ,
58
+ } as EnvironmentVariables )
59
59
60
60
createCredentialsFile ( credentialsFilename , [ 'default' ] )
61
61
62
62
const foundFiles : string [ ] = await UserCredentialsUtils . findExistingCredentialsFilenames ( )
63
- assert ( foundFiles )
64
63
assert . strictEqual ( foundFiles . length , 1 )
65
64
assert . strictEqual ( foundFiles [ 0 ] , credentialsFilename )
66
65
} )
@@ -69,14 +68,14 @@ describe('UserCredentialsUtils', function () {
69
68
const credentialsFilename = path . join ( tempFolder , 'credentials-not-exist-test' )
70
69
const configFilename = path . join ( tempFolder , 'config-exist-test' )
71
70
72
- const env = process . env as EnvironmentVariables
73
- env . AWS_SHARED_CREDENTIALS_FILE = credentialsFilename
74
- env . AWS_CONFIG_FILE = configFilename
71
+ sinon . stub ( process , 'env' ) . value ( {
72
+ AWS_SHARED_CREDENTIALS_FILE : credentialsFilename ,
73
+ AWS_CONFIG_FILE : configFilename ,
74
+ } as EnvironmentVariables )
75
75
76
76
createCredentialsFile ( configFilename , [ 'default' ] )
77
77
78
78
const foundFiles : string [ ] = await UserCredentialsUtils . findExistingCredentialsFilenames ( )
79
- assert ( foundFiles )
80
79
assert . strictEqual ( foundFiles . length , 1 )
81
80
assert . strictEqual ( foundFiles [ 0 ] , configFilename )
82
81
} )
@@ -85,12 +84,28 @@ describe('UserCredentialsUtils', function () {
85
84
const credentialsFilename = path . join ( tempFolder , 'credentials-not-exist-test' )
86
85
const configFilename = path . join ( tempFolder , 'config-not-exist-test' )
87
86
88
- const env = process . env as EnvironmentVariables
89
- env . AWS_SHARED_CREDENTIALS_FILE = credentialsFilename
90
- env . AWS_CONFIG_FILE = configFilename
87
+ sinon . stub ( process , 'env' ) . value ( {
88
+ AWS_SHARED_CREDENTIALS_FILE : credentialsFilename ,
89
+ AWS_CONFIG_FILE : configFilename ,
90
+ } as EnvironmentVariables )
91
91
92
92
const foundFiles : string [ ] = await UserCredentialsUtils . findExistingCredentialsFilenames ( )
93
- assert ( foundFiles )
93
+ assert . strictEqual ( foundFiles . length , 0 )
94
+ } )
95
+
96
+ it ( 'returns empty result if files dont contain credentials' , async function ( ) {
97
+ const credentialsFilename = path . join ( tempFolder , 'credentials-both-exist-but-no-credentials-test' )
98
+ const configFilename = path . join ( tempFolder , 'config-both-exist-but-no-credentials-test' )
99
+
100
+ sinon . stub ( process , 'env' ) . value ( {
101
+ AWS_SHARED_CREDENTIALS_FILE : credentialsFilename ,
102
+ AWS_CONFIG_FILE : configFilename ,
103
+ } as EnvironmentVariables )
104
+
105
+ createCredentialsFile ( credentialsFilename , [ ] )
106
+ createCredentialsFile ( configFilename , [ ] )
107
+
108
+ const foundFiles : string [ ] = await UserCredentialsUtils . findExistingCredentialsFilenames ( true )
94
109
assert . strictEqual ( foundFiles . length , 0 )
95
110
} )
96
111
} )
@@ -100,8 +115,9 @@ describe('UserCredentialsUtils', function () {
100
115
const credentialsFilename = path . join ( tempFolder , 'credentials-generation-test' )
101
116
const profileName = 'someRandomProfileName'
102
117
103
- const env = process . env as EnvironmentVariables
104
- env . AWS_SHARED_CREDENTIALS_FILE = credentialsFilename
118
+ sinon . stub ( process , 'env' ) . value ( {
119
+ AWS_SHARED_CREDENTIALS_FILE : credentialsFilename ,
120
+ } as EnvironmentVariables )
105
121
const creds = {
106
122
accessKey : '123' ,
107
123
profileName : profileName ,
0 commit comments