test(gradle-wrapper): add mock test (#6233)

This commit is contained in:
Michael Kriese 2020-05-15 13:30:36 +02:00 committed by GitHub
parent 081ee55dea
commit ca71eddbdb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 535 additions and 256 deletions

View file

@ -0,0 +1,67 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`manager/gradle-wrapper/artifacts gradlew failed 1`] = `
Array [
Object {
"cmd": "<gradlew> wrapper --gradle-version 5.6.4",
"options": Object {
"cwd": "/root/project/lib/manager/gradle-wrapper/__fixtures__/testFiles",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
"HTTPS_PROXY": "https://example.com",
"HTTP_PROXY": "http://example.com",
"LANG": "en_US.UTF-8",
"LC_ALL": "en_US",
"NO_PROXY": "localhost",
"PATH": "/tmp/path",
},
"timeout": 900000,
},
},
]
`;
exports[`manager/gradle-wrapper/artifacts replaces existing value 1`] = `
Array [
Object {
"cmd": "<gradlew> wrapper --gradle-distribution-url https://services.gradle.org/distributions/gradle-6.3-bin.zip",
"options": Object {
"cwd": "/root/project/lib/manager/gradle-wrapper/__fixtures__/testFiles",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
"HTTPS_PROXY": "https://example.com",
"HTTP_PROXY": "http://example.com",
"LANG": "en_US.UTF-8",
"LC_ALL": "en_US",
"NO_PROXY": "localhost",
"PATH": "/tmp/path",
},
"timeout": 900000,
},
},
]
`;
exports[`manager/gradle-wrapper/artifacts updates distributionSha256Sum 1`] = `
Array [
Object {
"cmd": "<gradlew> wrapper --gradle-distribution-url https://services.gradle.org/distributions/gradle-6.3-bin.zip --gradle-distribution-sha256-sum 038794feef1f4745c6347107b6726279d1c824f3fc634b60f86ace1e9fbd1768",
"options": Object {
"cwd": "/root/project/lib/manager/gradle-wrapper/__fixtures__/testFiles",
"encoding": "utf-8",
"env": Object {
"HOME": "/home/user",
"HTTPS_PROXY": "https://example.com",
"HTTP_PROXY": "http://example.com",
"LANG": "en_US.UTF-8",
"LC_ALL": "en_US",
"NO_PROXY": "localhost",
"PATH": "/tmp/path",
},
"timeout": 900000,
},
},
]
`;

View file

@ -0,0 +1,289 @@
import { resolve } from 'path';
import { readFile, readFileSync } from 'fs-extra';
import Git from 'simple-git/promise';
import * as httpMock from '../../../test/httpMock';
import { getName, partial, platform } from '../../../test/util';
import { setUtilConfig } from '../../util';
import * as runCache from '../../util/cache/run';
import { ifSystemSupportsGradle } from '../gradle/__testutil__/gradle';
import * as dcUpdate from '.';
const fixtures = resolve(__dirname, './__fixtures__');
const config = {
localDir: resolve(fixtures, './testFiles'),
toVersion: '5.6.4',
};
function readString(...paths: string[]): Promise<string> {
return readFile(resolve(fixtures, ...paths), 'utf8');
}
function readBinSync(...paths: string[]): Buffer {
return readFileSync(resolve(fixtures, ...paths));
}
function compareFile(file: string, path: string) {
expect(readBinSync(`./testFiles/${file}`)).toEqual(
readBinSync(`./${path}/${file}`)
);
}
describe(getName(__filename), () => {
ifSystemSupportsGradle(6).describe('real tests', () => {
jest.setTimeout(60 * 1000);
beforeEach(async () => {
jest.resetAllMocks();
await setUtilConfig(config);
httpMock.setup();
runCache.clear();
});
afterEach(async () => {
await Git(fixtures).checkout(['HEAD', '--', '.']);
httpMock.reset();
});
it('replaces existing value', async () => {
platform.getRepoStatus.mockResolvedValue({
modified: [
'gradle/wrapper/gradle-wrapper.properties',
'gradle/wrapper/gradle-wrapper.jar',
'gradlew',
'gradlew.bat',
],
} as Git.StatusResult);
const res = await dcUpdate.updateArtifacts({
packageFileName: 'gradle/wrapper/gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: await readString(
`./expectedFiles/gradle/wrapper/gradle-wrapper.properties`
),
config: { ...config, toVersion: '6.3' },
});
expect(res).toEqual(
[
'gradle/wrapper/gradle-wrapper.properties',
'gradle/wrapper/gradle-wrapper.jar',
'gradlew',
'gradlew.bat',
].map((fileProjectPath) => {
return {
file: {
name: fileProjectPath,
contents: readBinSync(`./testFiles/${fileProjectPath}`),
},
};
})
);
[
'gradle/wrapper/gradle-wrapper.properties',
'gradle/wrapper/gradle-wrapper.jar',
'gradlew',
'gradlew.bat',
].forEach((file) => {
compareFile(file, 'expectedFiles');
});
});
it('updates from version', async () => {
platform.getRepoStatus.mockResolvedValueOnce(
partial<Git.StatusResult>({
modified: ['gradle/wrapper/gradle-wrapper.properties'],
})
);
const result = await dcUpdate.updateArtifacts({
packageFileName: 'gradle/wrapper/gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: ``,
config: { ...config, toVersion: '6.3' },
});
expect(result).toHaveLength(1);
expect(result[0].artifactError).toBeUndefined();
compareFile('gradle/wrapper/gradle-wrapper.properties', 'expectedFiles');
});
it('up to date', async () => {
platform.getRepoStatus.mockResolvedValue({
modified: [],
} as Git.StatusResult);
const res = await dcUpdate.updateArtifacts({
packageFileName: 'gradle/wrapper/gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: await readString(
`./testFiles/gradle/wrapper/gradle-wrapper.properties`
),
config,
});
expect(res).toEqual([]);
// 5.6.4 => 5.6.4 (updates execs)
// 6.3 => (5.6.4) (downgrades execs)
// looks like a bug in Gradle
['gradle/wrapper/gradle-wrapper.properties'].forEach((file) => {
compareFile(file, 'testFiles-copy');
});
});
it('getRepoStatus fails', async () => {
platform.getRepoStatus.mockImplementation(() => {
throw new Error('failed');
});
const res = await dcUpdate.updateArtifacts({
packageFileName: 'gradle/wrapper/gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: await readString(
`./testFiles/gradle/wrapper/gradle-wrapper.properties`
),
config,
});
expect(res[0].artifactError.lockFile).toEqual(
'gradle/wrapper/gradle-wrapper.properties'
);
expect(res[0].artifactError.stderr).toEqual('failed');
// 5.6.4 => 5.6.4 (updates execs) - unexpected behavior (looks like a bug in Gradle)
['gradle/wrapper/gradle-wrapper.properties'].forEach((file) => {
compareFile(file, 'testFiles-copy');
});
});
it('gradlew failed', async () => {
const cfg = { ...config, localDir: resolve(fixtures, './wrongCmd') };
await setUtilConfig(cfg);
const res = await dcUpdate.updateArtifacts({
packageFileName: 'gradle/wrapper/gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: await readString(
`./testFiles/gradle/wrapper/gradle-wrapper.properties`
),
config: cfg,
});
expect(res[0].artifactError.lockFile).toEqual(
'gradle/wrapper/gradle-wrapper.properties'
);
expect(res[0].artifactError.stderr).not.toBeNull();
expect(res[0].artifactError.stderr).not.toEqual('');
// 5.6.4 => 5.6.4 (updates execs) - unexpected behavior (looks like a bug in Gradle)
['gradle/wrapper/gradle-wrapper.properties'].forEach((file) => {
compareFile(file, 'testFiles-copy');
});
});
it('gradlew not found', async () => {
const res = await dcUpdate.updateArtifacts({
packageFileName: 'gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: undefined,
config: {
localDir: 'some-dir',
},
});
expect(res).toBeNull();
});
it('updates distributionSha256Sum', async () => {
httpMock
.scope('https://services.gradle.org')
.get('/distributions/gradle-6.3-bin.zip.sha256')
.reply(
200,
'038794feef1f4745c6347107b6726279d1c824f3fc634b60f86ace1e9fbd1768'
);
platform.getRepoStatus.mockResolvedValueOnce(
partial<Git.StatusResult>({
modified: ['gradle/wrapper/gradle-wrapper.properties'],
})
);
const newContent = await readString(`./gradle-wrapper-sum.properties`);
const result = await dcUpdate.updateArtifacts({
packageFileName: 'gradle/wrapper/gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: newContent.replace(
'038794feef1f4745c6347107b6726279d1c824f3fc634b60f86ace1e9fbd1768',
'1f3067073041bc44554d0efe5d402a33bc3d3c93cc39ab684f308586d732a80d'
),
config: {
...config,
toVersion: '6.3',
currentValue: '5.6.4',
},
});
expect(result).toHaveLength(1);
expect(result[0].artifactError).toBeUndefined();
expect(
await readString(
config.localDir,
`./gradle/wrapper/gradle-wrapper.properties`
)
).toEqual(newContent);
expect(httpMock.getTrace()).toEqual([
{
headers: {
'accept-encoding': 'gzip, deflate',
host: 'services.gradle.org',
'user-agent': 'https://github.com/renovatebot/renovate',
},
method: 'GET',
url:
'https://services.gradle.org/distributions/gradle-6.3-bin.zip.sha256',
},
]);
});
it('distributionSha256Sum 404', async () => {
httpMock
.scope('https://services.gradle.org')
.get('/distributions/gradle-6.3-bin.zip.sha256')
.reply(404);
const result = await dcUpdate.updateArtifacts({
packageFileName: 'gradle/wrapper/gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: `distributionSha256Sum=336b6898b491f6334502d8074a6b8c2d73ed83b92123106bd4bf837f04111043\ndistributionUrl=https\\://services.gradle.org/distributions/gradle-6.3-bin.zip`,
config,
});
expect(result).toEqual([
{
artifactError: {
lockFile: 'gradle/wrapper/gradle-wrapper.properties',
stderr: 'Response code 404 (Not Found)',
},
},
]);
expect(httpMock.getTrace()).toEqual([
{
headers: {
'accept-encoding': 'gzip, deflate',
host: 'services.gradle.org',
'user-agent': 'https://github.com/renovatebot/renovate',
},
method: 'GET',
url:
'https://services.gradle.org/distributions/gradle-6.3-bin.zip.sha256',
},
]);
});
});
});

View file

@ -1,297 +1,204 @@
import { exec as _exec } from 'child_process';
import { resolve } from 'path';
import { readFile, readFileSync } from 'fs-extra';
import { readFile } from 'fs-extra';
import Git from 'simple-git/promise';
import { envMock, mockExecAll } from '../../../test/execUtil';
import * as httpMock from '../../../test/httpMock';
import {
bufferSerializer,
addReplacingSerializer,
env,
fs,
getName,
partial,
platform,
} from '../../../test/util';
import { setUtilConfig } from '../../util';
import * as runCache from '../../util/cache/run';
import { ifSystemSupportsGradle } from '../gradle/__testutil__/gradle';
import { BinarySource } from '../../util/exec/common';
import { resetPrefetchedImages } from '../../util/exec/docker';
import * as dcUpdate from '.';
jest.mock('child_process');
jest.mock('../../util/fs');
jest.mock('../../util/exec/env');
const exec: jest.Mock<typeof _exec> = _exec as any;
const fixtures = resolve(__dirname, './__fixtures__');
const config = {
localDir: resolve(fixtures, './testFiles'),
toVersion: '5.6.4',
};
const dockerConfig = { ...config, binarySource: BinarySource.Docker };
jest.mock('../../platform');
expect.addSnapshotSerializer(bufferSerializer());
addReplacingSerializer('gradlew.bat', '<gradlew>');
addReplacingSerializer('./gradlew', '<gradlew>');
function readString(...paths: string[]): Promise<string> {
return readFile(resolve(fixtures, ...paths), 'utf8');
}
function readBinSync(...paths: string[]): Buffer {
return readFileSync(resolve(fixtures, ...paths));
}
function compareFile(file: string, path: string) {
expect(readBinSync(`./testFiles/${file}`)).toEqual(
readBinSync(`./${path}/${file}`)
);
}
describe(getName(__filename), () => {
ifSystemSupportsGradle(6).describe('real tests', () => {
jest.setTimeout(60 * 1000);
beforeEach(async () => {
jest.resetAllMocks();
httpMock.setup();
beforeEach(async () => {
jest.resetAllMocks();
await setUtilConfig(config);
httpMock.setup();
runCache.clear();
env.getChildProcessEnv.mockReturnValue({
...envMock.basic,
LANG: 'en_US.UTF-8',
LC_ALL: 'en_US',
});
afterEach(async () => {
await Git(fixtures)?.checkout(['HEAD', '--', '.']);
httpMock.reset();
});
await setUtilConfig(config);
resetPrefetchedImages();
it('replaces existing value', async () => {
platform.getRepoStatus.mockResolvedValue({
modified: [
'gradle/wrapper/gradle-wrapper.properties',
'gradle/wrapper/gradle-wrapper.jar',
'gradlew',
'gradlew.bat',
],
} as Git.StatusResult);
fs.readLocalFile.mockResolvedValue('test');
});
const res = await dcUpdate.updateArtifacts({
packageFileName: 'gradle/wrapper/gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: await readString(
`./expectedFiles/gradle/wrapper/gradle-wrapper.properties`
),
config: { ...config, toVersion: '6.3' },
});
afterEach(() => {
httpMock.reset();
});
expect(res).toEqual(
[
'gradle/wrapper/gradle-wrapper.properties',
'gradle/wrapper/gradle-wrapper.jar',
'gradlew',
'gradlew.bat',
].map((fileProjectPath) => {
return {
file: {
name: fileProjectPath,
contents: readBinSync(`./testFiles/${fileProjectPath}`),
},
};
})
);
[
it('replaces existing value', async () => {
platform.getRepoStatus.mockResolvedValue({
modified: [
'gradle/wrapper/gradle-wrapper.properties',
'gradle/wrapper/gradle-wrapper.jar',
'gradlew',
'gradlew.bat',
].forEach((file) => {
compareFile(file, 'expectedFiles');
});
],
} as Git.StatusResult);
const execSnapshots = mockExecAll(exec);
const res = await dcUpdate.updateArtifacts({
packageFileName: 'gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: await readString(
`./expectedFiles/gradle/wrapper/gradle-wrapper.properties`
),
config: { ...config, toVersion: '6.3' },
});
it('updates from version', async () => {
platform.getRepoStatus.mockResolvedValueOnce(
partial<Git.StatusResult>({
modified: ['gradle/wrapper/gradle-wrapper.properties'],
})
);
expect(res).toEqual(
[
'gradle/wrapper/gradle-wrapper.properties',
'gradlew',
'gradlew.bat',
].map((fileProjectPath) => {
return {
file: {
name: fileProjectPath,
contents: 'test',
},
};
})
);
expect(execSnapshots).toMatchSnapshot();
});
const result = await dcUpdate.updateArtifacts({
packageFileName: 'gradle/wrapper/gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: ``,
config: { ...config, toVersion: '6.3' },
});
expect(result).toHaveLength(1);
expect(result[0].artifactError).toBeUndefined();
compareFile('gradle/wrapper/gradle-wrapper.properties', 'expectedFiles');
it('gradlew not found', async () => {
const res = await dcUpdate.updateArtifacts({
packageFileName: 'gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: undefined,
config: {
localDir: 'some-dir',
},
});
it('up to date', async () => {
platform.getRepoStatus.mockResolvedValue({
expect(res).toBeNull();
});
it('gradlew failed', async () => {
const execSnapshots = mockExecAll(exec, new Error('failed'));
platform.getRepoStatus.mockResolvedValueOnce(
partial<Git.StatusResult>({
modified: [],
} as Git.StatusResult);
const res = await dcUpdate.updateArtifacts({
packageFileName: 'gradle/wrapper/gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: await readString(
`./testFiles/gradle/wrapper/gradle-wrapper.properties`
),
config,
});
expect(res).toEqual([]);
// 5.6.4 => 5.6.4 (updates execs)
// 6.3 => (5.6.4) (downgrades execs)
// looks like a bug in Gradle
['gradle/wrapper/gradle-wrapper.properties'].forEach((file) => {
compareFile(file, 'testFiles-copy');
});
})
);
const res = await dcUpdate.updateArtifacts({
packageFileName: 'gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: '',
config,
});
it('getRepoStatus fails', async () => {
platform.getRepoStatus.mockImplementation(() => {
throw new Error('failed');
});
expect(execSnapshots).toMatchSnapshot();
expect(res).toEqual([]);
});
const res = await dcUpdate.updateArtifacts({
packageFileName: 'gradle/wrapper/gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: await readString(
`./testFiles/gradle/wrapper/gradle-wrapper.properties`
),
config,
});
expect(res[0].artifactError.lockFile).toEqual(
'gradle/wrapper/gradle-wrapper.properties'
it.only('updates distributionSha256Sum', async () => {
httpMock
.scope('https://services.gradle.org')
.get('/distributions/gradle-6.3-bin.zip.sha256')
.reply(
200,
'038794feef1f4745c6347107b6726279d1c824f3fc634b60f86ace1e9fbd1768'
);
expect(res[0].artifactError.stderr).toEqual('failed');
// 5.6.4 => 5.6.4 (updates execs) - unexpected behavior (looks like a bug in Gradle)
['gradle/wrapper/gradle-wrapper.properties'].forEach((file) => {
compareFile(file, 'testFiles-copy');
});
platform.getRepoStatus.mockResolvedValueOnce(
partial<Git.StatusResult>({
modified: ['gradle/wrapper/gradle-wrapper.properties'],
})
);
const execSnapshots = mockExecAll(exec);
const result = await dcUpdate.updateArtifacts({
packageFileName: 'gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: `distributionSha256Sum=336b6898b491f6334502d8074a6b8c2d73ed83b92123106bd4bf837f04111043\ndistributionUrl=https\\://services.gradle.org/distributions/gradle-6.3-bin.zip`,
config: dockerConfig,
});
it('gradlew failed', async () => {
const cfg = { ...config, localDir: resolve(fixtures, './wrongCmd') };
expect(result).toHaveLength(1);
expect(result[0].artifactError).toBeUndefined();
await setUtilConfig(cfg);
const res = await dcUpdate.updateArtifacts({
packageFileName: 'gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: await readString(
`./testFiles/gradle/wrapper/gradle-wrapper.properties`
),
config: cfg,
});
expect(execSnapshots).toMatchSnapshot();
expect(httpMock.getTrace()).toEqual([
{
headers: {
'accept-encoding': 'gzip, deflate',
host: 'services.gradle.org',
'user-agent': 'https://github.com/renovatebot/renovate',
},
method: 'GET',
url:
'https://services.gradle.org/distributions/gradle-6.3-bin.zip.sha256',
},
]);
});
expect(res[0].artifactError.lockFile).toEqual(
'gradle-wrapper.properties'
);
expect(res[0].artifactError.stderr).not.toBeNull();
expect(res[0].artifactError.stderr).not.toEqual('');
it('distributionSha256Sum 404', async () => {
httpMock
.scope('https://services.gradle.org')
.get('/distributions/gradle-6.3-bin.zip.sha256')
.reply(404);
// 5.6.4 => 5.6.4 (updates execs) - unexpected behavior (looks like a bug in Gradle)
['gradle/wrapper/gradle-wrapper.properties'].forEach((file) => {
compareFile(file, 'testFiles-copy');
});
const result = await dcUpdate.updateArtifacts({
packageFileName: 'gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: `distributionSha256Sum=336b6898b491f6334502d8074a6b8c2d73ed83b92123106bd4bf837f04111043\ndistributionUrl=https\\://services.gradle.org/distributions/gradle-6.3-bin.zip`,
config,
});
it('gradlew not found', async () => {
const res = await dcUpdate.updateArtifacts({
packageFileName: 'gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: undefined,
config: {
localDir: 'some-dir',
expect(result).toEqual([
{
artifactError: {
lockFile: 'gradle-wrapper.properties',
stderr: 'Response code 404 (Not Found)',
},
});
expect(res).toBeNull();
});
it('updates distributionSha256Sum', async () => {
httpMock
.scope('https://services.gradle.org')
.get('/distributions/gradle-6.3-bin.zip.sha256')
.reply(
200,
'038794feef1f4745c6347107b6726279d1c824f3fc634b60f86ace1e9fbd1768'
);
platform.getRepoStatus.mockResolvedValueOnce(
partial<Git.StatusResult>({
modified: ['gradle/wrapper/gradle-wrapper.properties'],
})
);
const newContent = await readString(`./gradle-wrapper-sum.properties`);
const result = await dcUpdate.updateArtifacts({
packageFileName: 'gradle/wrapper/gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: newContent.replace(
'038794feef1f4745c6347107b6726279d1c824f3fc634b60f86ace1e9fbd1768',
'1f3067073041bc44554d0efe5d402a33bc3d3c93cc39ab684f308586d732a80d'
),
config: {
...config,
toVersion: '6.3',
currentValue: '5.6.4',
},
]);
expect(httpMock.getTrace()).toEqual([
{
headers: {
'accept-encoding': 'gzip, deflate',
host: 'services.gradle.org',
'user-agent': 'https://github.com/renovatebot/renovate',
},
});
expect(result).toHaveLength(1);
expect(result[0].artifactError).toBeUndefined();
expect(
await readString(
config.localDir,
`./gradle/wrapper/gradle-wrapper.properties`
)
).toEqual(newContent);
expect(httpMock.getTrace()).toEqual([
{
headers: {
'accept-encoding': 'gzip, deflate',
host: 'services.gradle.org',
'user-agent': 'https://github.com/renovatebot/renovate',
},
method: 'GET',
url:
'https://services.gradle.org/distributions/gradle-6.3-bin.zip.sha256',
},
]);
});
it('distributionSha256Sum 404', async () => {
httpMock
.scope('https://services.gradle.org')
.get('/distributions/gradle-6.3-bin.zip.sha256')
.reply(404);
const result = await dcUpdate.updateArtifacts({
packageFileName: 'gradle/wrapper/gradle-wrapper.properties',
updatedDeps: [],
newPackageFileContent: `distributionSha256Sum=336b6898b491f6334502d8074a6b8c2d73ed83b92123106bd4bf837f04111043\ndistributionUrl=https\\://services.gradle.org/distributions/gradle-6.3-bin.zip`,
config,
});
expect(result).toEqual([
{
artifactError: {
lockFile: 'gradle/wrapper/gradle-wrapper.properties',
stderr: 'Response code 404 (Not Found)',
},
},
]);
expect(httpMock.getTrace()).toEqual([
{
headers: {
'accept-encoding': 'gzip, deflate',
host: 'services.gradle.org',
'user-agent': 'https://github.com/renovatebot/renovate',
},
method: 'GET',
url:
'https://services.gradle.org/distributions/gradle-6.3-bin.zip.sha256',
},
]);
});
method: 'GET',
url:
'https://services.gradle.org/distributions/gradle-6.3-bin.zip.sha256',
},
]);
});
});

View file

@ -1,4 +1,3 @@
/* istanbul ignore file */
import { resolve } from 'path';
import * as fs from 'fs-extra';
import Git from 'simple-git/promise';

View file

@ -184,6 +184,7 @@
"@babel/plugin-syntax-dynamic-import": "7.8.3",
"@babel/preset-env": "7.9.6",
"@babel/preset-typescript": "7.9.0",
"@jest/globals": "26.0.1",
"@jest/reporters": "26.0.1",
"@jest/test-result": "26.0.1",
"@semantic-release/exec": "5.0.0",

View file

@ -1,4 +1,7 @@
import { exec as _exec } from 'child_process';
import is from '@sindresorhus/is';
import traverse from 'traverse';
import { toUnix } from 'upath';
import { ExecOptions } from '../lib/util/exec';
type CallOptions = ExecOptions | null | undefined;
@ -18,15 +21,15 @@ export function execSnapshot(cmd: string, options?: CallOptions): ExecSnapshot {
options,
};
const str = JSON.stringify(snapshot, (k, v) => (v === undefined ? null : v));
const cwd = toUnix(process.cwd());
const cwd = process.cwd().replace(/\\(\w)/g, '/$1');
return JSON.parse(
str
.replace(/\\(\w)/g, '/$1')
.split(cwd)
.join('/root/project')
);
// eslint-disable-next-line array-callback-return
return traverse(snapshot).map(function fixup(v) {
if (is.string(v)) {
const val = v.replace(/\\(\w)/g, '/$1').replace(cwd, '/root/project');
this.update(val);
}
});
}
const defaultExecResult = { stdout: '', stderr: '' };

View file

@ -1,9 +1,12 @@
import crypto from 'crypto';
import { expect, jest } from '@jest/globals';
import * as upath from 'upath';
import { RenovateConfig as _RenovateConfig } from '../lib/config';
import { getConfig } from '../lib/config/defaults';
import { platform as _platform } from '../lib/platform';
import * as _env from '../lib/util/exec/env';
import * as _fs from '../lib/util/fs';
import * as _hostRules from '../lib/util/host-rules';
/**
* Simple wrapper for getting mocked version of a module
@ -26,7 +29,7 @@ export function mocked<T>(module: T): jest.Mocked<T> {
*/
export function mockPartial(moduleName: string, overrides?: object): unknown {
const absolutePath = upath.join(module.parent.filename, '../', moduleName);
const originalModule = jest.requireActual(absolutePath);
const originalModule = jest.requireActual(absolutePath) as any;
return {
__esModule: true,
...originalModule,
@ -44,6 +47,8 @@ export function partial<T>(obj: Partial<T>): T {
export const fs = mocked(_fs);
export const platform = mocked(_platform);
export const env = mocked(_env);
export const hostRules = mocked(_hostRules);
// Required because of isolatedModules
export type RenovateConfig = _RenovateConfig;
@ -75,14 +80,22 @@ export const replacingSerializer = (
},
});
export function addReplacingSerializer(from: string, to: string): void {
expect.addSnapshotSerializer(replacingSerializer(from, to));
}
function toHash(buf: Buffer): string {
return crypto.createHash('sha256').update(buf).digest('hex');
}
export const bufferSerializer = (): jest.SnapshotSerializerPlugin => ({
const bufferSerializer: jest.SnapshotSerializerPlugin = {
test: (value) => Buffer.isBuffer(value),
serialize: (val, config, indent, depth, refs, printer) => {
const replaced = toHash(val);
return printer(replaced, config, indent, depth, refs);
},
});
};
export function addBufferSerializer(): void {
expect.addSnapshotSerializer(bufferSerializer);
}

View file

@ -946,7 +946,7 @@
jest-mock "^26.0.1"
jest-util "^26.0.1"
"@jest/globals@^26.0.1":
"@jest/globals@26.0.1", "@jest/globals@^26.0.1":
version "26.0.1"
resolved "https://registry.yarnpkg.com/@jest/globals/-/globals-26.0.1.tgz#3f67b508a7ce62b6e6efc536f3d18ec9deb19a9c"
integrity sha512-iuucxOYB7BRCvT+TYBzUqUNuxFX1hqaR6G6IcGgEqkJ5x4htNKo1r7jk1ji9Zj8ZMiMw0oB5NaA7k5Tx6MVssA==