1
- import { mocked } from '../../../test/util' ;
1
+ import os from 'node:os' ;
2
+ import { any , mockDeep } from 'jest-mock-extended' ;
3
+ import upath from 'upath' ;
4
+ import { mockedExtended } from '../../../test/util' ;
2
5
import * as exec_ from '../exec' ;
3
6
import { configSigningKey , writePrivateKey } from './private-key' ;
4
7
import { setPrivateKey } from '.' ;
@@ -10,9 +13,9 @@ jest.mock('fs-extra', () =>
10
13
> ( '../../../test/fixtures' )
11
14
. fsExtra ( ) ,
12
15
) ;
13
- jest . mock ( '../exec' ) ;
16
+ jest . mock ( '../exec' , ( ) => mockDeep ( ) ) ;
14
17
15
- const exec = mocked ( exec_ ) ;
18
+ const exec = mockedExtended ( exec_ ) ;
16
19
17
20
describe ( 'util/git/private-key' , ( ) => {
18
21
describe ( 'writePrivateKey()' , ( ) => {
@@ -23,21 +26,40 @@ describe('util/git/private-key', () => {
23
26
24
27
it ( 'throws error if failing' , async ( ) => {
25
28
setPrivateKey ( 'some-key' ) ;
26
- exec . exec . mockRejectedValueOnce ( {
27
- stderr : `something wrong` ,
28
- stdout : '' ,
29
- } ) ;
29
+ exec . exec . calledWith ( any ( ) ) . mockResolvedValue ( { stdout : '' , stderr : '' } ) ;
30
+ exec . exec
31
+ . calledWith (
32
+ `gpg --import ${ upath . join ( os . tmpdir ( ) + '/git-private-gpg.key' ) } ` ,
33
+ )
34
+ . mockRejectedValueOnce ( {
35
+ stderr : `something wrong` ,
36
+ stdout : '' ,
37
+ } ) ;
30
38
await expect ( writePrivateKey ( ) ) . rejects . toThrow ( ) ;
31
39
} ) ;
32
40
33
41
it ( 'imports the private key' , async ( ) => {
42
+ const publicKey = 'BADC0FFEE' ;
43
+ const repoDir = '/tmp/some-repo' ;
44
+ exec . exec . calledWith ( any ( ) ) . mockResolvedValue ( { stdout : '' , stderr : '' } ) ;
45
+ exec . exec
46
+ . calledWith (
47
+ `gpg --import ${ upath . join ( os . tmpdir ( ) + '/git-private-gpg.key' ) } ` ,
48
+ )
49
+ . mockResolvedValueOnce ( {
50
+ stderr : `gpg: key ${ publicKey } : secret key imported\nfoo\n` ,
51
+ stdout : '' ,
52
+ } ) ;
34
53
setPrivateKey ( 'some-key' ) ;
35
- exec . exec . mockResolvedValueOnce ( {
36
- stderr : `gpg: key BADC0FFEE: secret key imported\nfoo\n` ,
37
- stdout : '' ,
38
- } ) ;
39
54
await expect ( writePrivateKey ( ) ) . resolves . not . toThrow ( ) ;
40
- await expect ( configSigningKey ( '/tmp/some-repo' ) ) . resolves . not . toThrow ( ) ;
55
+ await expect ( configSigningKey ( repoDir ) ) . resolves . not . toThrow ( ) ;
56
+ expect ( exec . exec ) . toHaveBeenCalledWith (
57
+ `git config user.signingkey ${ publicKey } ` ,
58
+ { cwd : repoDir } ,
59
+ ) ;
60
+ expect ( exec . exec ) . toHaveBeenCalledWith ( 'git config commit.gpgsign true' , {
61
+ cwd : repoDir ,
62
+ } ) ;
41
63
} ) ;
42
64
43
65
it ( 'does not import the key again' , async ( ) => {
0 commit comments