@@ -163,6 +163,75 @@ describe('accounts', function () {
163163 } )
164164 } )
165165
166+ describe ( 'set()' , function ( ) {
167+ describe ( 'with credentialStore and no account name' , function ( ) {
168+ let writeLoginStateStub : sinon . SinonStub
169+
170+ beforeEach ( function ( ) {
171+ sinon . stub ( AccountsModule , 'getStorageConfig' ) . returns ( { credentialStore : 'keychain' as any , useNetrc : false } )
172+ writeLoginStateStub = sinon . stub ( AccountsModule , 'writeLoginState' ) . resolves ( )
173+ } )
174+
175+ it ( 'calls writeLoginState with the dataDir and account username' , async function ( ) {
176+ const account = { username : 'user@example.com' }
177+ await AccountsModule . set ( account , '/data/heroku' )
178+
179+ expect ( writeLoginStateStub . calledOnce ) . to . be . true
180+ expect ( writeLoginStateStub . firstCall . args [ 0 ] ) . to . equal ( '/data/heroku' )
181+ expect ( writeLoginStateStub . firstCall . args [ 1 ] ) . to . equal ( 'user@example.com' )
182+ } )
183+
184+ it ( 'does not call writeLoginState when account has a name' , async function ( ) {
185+ const account = { name : 'my-account' , username : 'user@example.com' }
186+ await AccountsModule . set ( account , '/data/heroku' )
187+
188+ expect ( writeLoginStateStub . called ) . to . be . false
189+ } )
190+ } )
191+
192+ describe ( 'with useNetrc and account name' , function ( ) {
193+ let fakeNetrc : { machines : Record < string , { login : string , password : string } > , save : sinon . SinonStub }
194+
195+ function setNetrc ( value : typeof fakeNetrc | undefined ) {
196+ ( AccountsModule as unknown as { netrc : typeof fakeNetrc | undefined } ) . netrc = value
197+ }
198+
199+ beforeEach ( function ( ) {
200+ sinon . stub ( AccountsModule , 'getStorageConfig' ) . returns ( { credentialStore : null , useNetrc : true } )
201+ fakeNetrc = { machines : { } , save : sinon . stub ( ) . resolves ( ) }
202+ setNetrc ( fakeNetrc )
203+ fsReadFileStub . withArgs ( sinon . match ( / m y - a c c o u n t $ / ) , 'utf8' )
204+ . returns ( 'username: user@example.com\npassword: secret\n' )
205+ } )
206+
207+ afterEach ( function ( ) {
208+ setNetrc ( null as unknown as typeof fakeNetrc )
209+ } )
210+
211+ it ( 'writes credentials to api.heroku.com and git.heroku.com machines' , async function ( ) {
212+ const account = { name : 'my-account' , username : 'user@example.com' }
213+ await AccountsModule . set ( account , '/data/heroku' )
214+
215+ expect ( fakeNetrc . machines [ 'api.heroku.com' ] ) . to . deep . equal ( { login : 'user@example.com' , password : 'secret' } )
216+ expect ( fakeNetrc . machines [ 'git.heroku.com' ] ) . to . deep . equal ( { login : 'user@example.com' , password : 'secret' } )
217+ } )
218+
219+ it ( 'saves the netrc file' , async function ( ) {
220+ const account = { name : 'my-account' , username : 'user@example.com' }
221+ await AccountsModule . set ( account , '/data/heroku' )
222+
223+ expect ( fakeNetrc . save . calledOnce ) . to . be . true
224+ } )
225+
226+ it ( 'does not update netrc when account has no name' , async function ( ) {
227+ const account = { username : 'user@example.com' }
228+ await AccountsModule . set ( account , '/data/heroku' )
229+
230+ expect ( fakeNetrc . save . called ) . to . be . false
231+ } )
232+ } )
233+ } )
234+
166235 describe ( 'remove' , function ( ) {
167236 let unlinkStub : sinon . SinonStub
168237 let osHomeStub : sinon . SinonStub
0 commit comments