From e5f4a1e0658b3dc0bb37b48c9011440517db304c Mon Sep 17 00:00:00 2001 From: Sigurd Spieckermann <2206639+sisp@users.noreply.github.com> Date: Fri, 26 Jul 2024 12:35:49 +0200 Subject: [PATCH] test(git): enhance commit signing tests (#30396) Co-authored-by: Michael Kriese <michael.kriese@visualon.de> --- lib/util/git/private-key.spec.ts | 46 +++++++++++++++++++++++--------- test/util.ts | 9 +++++++ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/lib/util/git/private-key.spec.ts b/lib/util/git/private-key.spec.ts index 36fb4ba75e..f13c5d40a7 100644 --- a/lib/util/git/private-key.spec.ts +++ b/lib/util/git/private-key.spec.ts @@ -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 '.'; @@ -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()', () => { @@ -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 () => { diff --git a/test/util.ts b/test/util.ts index ee4add4661..406322b914 100644 --- a/test/util.ts +++ b/test/util.ts @@ -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'; @@ -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` -- GitLab