Skip to content

Commit

Permalink
test(git): enhance commit signing tests (#30396)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
sisp and viceice authored Jul 26, 2024
1 parent d439f84 commit e5f4a1e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
46 changes: 34 additions & 12 deletions lib/util/git/private-key.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { mocked } from '../../../test/util';
import os from 'node:os';
import { any, mockDeep } from 'jest-mock-extended';
import upath from 'upath';
import { mockedExtended } from '../../../test/util';
import * as exec_ from '../exec';
import { configSigningKey, writePrivateKey } from './private-key';
import { setPrivateKey } from '.';
Expand All @@ -10,9 +13,9 @@ jest.mock('fs-extra', () =>
>('../../../test/fixtures')
.fsExtra(),
);
jest.mock('../exec');
jest.mock('../exec', () => mockDeep());

const exec = mocked(exec_);
const exec = mockedExtended(exec_);

describe('util/git/private-key', () => {
describe('writePrivateKey()', () => {
Expand All @@ -23,21 +26,40 @@ describe('util/git/private-key', () => {

it('throws error if failing', async () => {
setPrivateKey('some-key');
exec.exec.mockRejectedValueOnce({
stderr: `something wrong`,
stdout: '',
});
exec.exec.calledWith(any()).mockResolvedValue({ stdout: '', stderr: '' });
exec.exec
.calledWith(
`gpg --import ${upath.join(os.tmpdir() + '/git-private-gpg.key')}`,
)
.mockRejectedValueOnce({
stderr: `something wrong`,
stdout: '',
});
await expect(writePrivateKey()).rejects.toThrow();
});

it('imports the private key', async () => {
const publicKey = 'BADC0FFEE';
const repoDir = '/tmp/some-repo';
exec.exec.calledWith(any()).mockResolvedValue({ stdout: '', stderr: '' });
exec.exec
.calledWith(
`gpg --import ${upath.join(os.tmpdir() + '/git-private-gpg.key')}`,
)
.mockResolvedValueOnce({
stderr: `gpg: key ${publicKey}: secret key imported\nfoo\n`,
stdout: '',
});
setPrivateKey('some-key');
exec.exec.mockResolvedValueOnce({
stderr: `gpg: key BADC0FFEE: secret key imported\nfoo\n`,
stdout: '',
});
await expect(writePrivateKey()).resolves.not.toThrow();
await expect(configSigningKey('/tmp/some-repo')).resolves.not.toThrow();
await expect(configSigningKey(repoDir)).resolves.not.toThrow();
expect(exec.exec).toHaveBeenCalledWith(
`git config user.signingkey ${publicKey}`,
{ cwd: repoDir },
);
expect(exec.exec).toHaveBeenCalledWith('git config commit.gpgsign true', {
cwd: repoDir,
});
});

it('does not import the key again', async () => {
Expand Down
9 changes: 9 additions & 0 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import crypto from 'node:crypto';
import { expect, jest } from '@jest/globals';
import type { DeepMockProxy } from 'jest-mock-extended';
import type { Plugin } from 'pretty-format';
import upath from 'upath';
import type { RenovateConfig } from '../lib/config/types';
Expand All @@ -20,6 +21,14 @@ export function mocked<T extends object>(module: T): jest.Mocked<T> {
return jest.mocked(module);
}

/**
* Simple wrapper for getting mocked version of a module
* @param module module which is mocked by `jest-mock-extended.mockDeep`
*/
export function mockedExtended<T extends object>(module: T): DeepMockProxy<T> {
return module as DeepMockProxy<T>;
}

/**
* Simple wrapper for getting mocked version of a function
* @param func function which is mocked by `jest.mock`
Expand Down

0 comments on commit e5f4a1e

Please sign in to comment.