Skip to content

Commit

Permalink
test(git): Decouple 'gitfs/fs' and 'gitfs/git' in tests (#6661)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Jul 3, 2020
1 parent ae0fb69 commit 679176f
Show file tree
Hide file tree
Showing 26 changed files with 322 additions and 336 deletions.
66 changes: 32 additions & 34 deletions lib/manager/bundler/artifacts.spec.ts
Expand Up @@ -2,17 +2,15 @@ import { exec as _exec } from 'child_process';
import Git from 'simple-git/promise';
import { join } from 'upath';
import { envMock, mockExecAll } from '../../../test/execUtil';
import { mocked, platform } from '../../../test/util';
import { fs, mocked, platform } from '../../../test/util';
import * as _datasource from '../../datasource';
import { setUtilConfig } from '../../util';
import { BinarySource } from '../../util/exec/common';
import * as docker from '../../util/exec/docker';
import * as _env from '../../util/exec/env';
import * as _gitfs from '../../util/gitfs';
import * as _bundlerHostRules from './host-rules';
import { updateArtifacts } from '.';

const gitfs: jest.Mocked<typeof _gitfs> = _gitfs as any;
const exec: jest.Mock<typeof _exec> = _exec as any;
const env = mocked(_env);
const datasource = mocked(_datasource);
Expand All @@ -22,7 +20,7 @@ jest.mock('fs-extra');
jest.mock('child_process');
jest.mock('../../../lib/util/exec/env');
jest.mock('../../../lib/datasource');
jest.mock('../../../lib/util/gitfs');
jest.mock('../../../lib/util/gitfs/fs');
jest.mock('../../../lib/util/host-rules');
jest.mock('./host-rules');

Expand Down Expand Up @@ -59,13 +57,13 @@ describe('bundler.updateArtifacts()', () => {
).toBeNull();
});
it('returns null if Gemfile.lock was not changed', async () => {
gitfs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
gitfs.writeLocalFile.mockResolvedValueOnce(null as never);
fs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
fs.writeLocalFile.mockResolvedValueOnce(null as never);
const execSnapshots = mockExecAll(exec);
platform.getRepoStatus.mockResolvedValueOnce({
modified: [],
} as Git.StatusResult);
gitfs.readLocalFile.mockResolvedValueOnce('Updated Gemfile.lock' as any);
fs.readLocalFile.mockResolvedValueOnce('Updated Gemfile.lock' as any);
expect(
await updateArtifacts({
packageFileName: 'Gemfile',
Expand All @@ -77,14 +75,14 @@ describe('bundler.updateArtifacts()', () => {
expect(execSnapshots).toMatchSnapshot();
});
it('works for default binarySource', async () => {
gitfs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
gitfs.writeLocalFile.mockResolvedValueOnce(null as never);
gitfs.readLocalFile.mockResolvedValueOnce(null);
fs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
fs.writeLocalFile.mockResolvedValueOnce(null as never);
fs.readLocalFile.mockResolvedValueOnce(null);
const execSnapshots = mockExecAll(exec);
platform.getRepoStatus.mockResolvedValueOnce({
modified: ['Gemfile.lock'],
} as Git.StatusResult);
gitfs.readLocalFile.mockResolvedValueOnce('Updated Gemfile.lock' as any);
fs.readLocalFile.mockResolvedValueOnce('Updated Gemfile.lock' as any);
expect(
await updateArtifacts({
packageFileName: 'Gemfile',
Expand All @@ -96,14 +94,14 @@ describe('bundler.updateArtifacts()', () => {
expect(execSnapshots).toMatchSnapshot();
});
it('works explicit global binarySource', async () => {
gitfs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
gitfs.writeLocalFile.mockResolvedValueOnce(null as never);
gitfs.readLocalFile.mockResolvedValueOnce(null);
fs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
fs.writeLocalFile.mockResolvedValueOnce(null as never);
fs.readLocalFile.mockResolvedValueOnce(null);
const execSnapshots = mockExecAll(exec);
platform.getRepoStatus.mockResolvedValueOnce({
modified: ['Gemfile.lock'],
} as Git.StatusResult);
gitfs.readLocalFile.mockResolvedValueOnce('Updated Gemfile.lock' as any);
fs.readLocalFile.mockResolvedValueOnce('Updated Gemfile.lock' as any);
expect(
await updateArtifacts({
packageFileName: 'Gemfile',
Expand All @@ -123,9 +121,9 @@ describe('bundler.updateArtifacts()', () => {
await setUtilConfig({ ...config, binarySource: BinarySource.Docker });
});
it('.ruby-version', async () => {
gitfs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
gitfs.writeLocalFile.mockResolvedValueOnce(null as never);
gitfs.readLocalFile.mockResolvedValueOnce('1.2.0');
fs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
fs.writeLocalFile.mockResolvedValueOnce(null as never);
fs.readLocalFile.mockResolvedValueOnce('1.2.0');
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [
{ version: '1.0.0' },
Expand All @@ -137,7 +135,7 @@ describe('bundler.updateArtifacts()', () => {
platform.getRepoStatus.mockResolvedValueOnce({
modified: ['Gemfile.lock'],
} as Git.StatusResult);
gitfs.readLocalFile.mockResolvedValueOnce('Updated Gemfile.lock' as any);
fs.readLocalFile.mockResolvedValueOnce('Updated Gemfile.lock' as any);
expect(
await updateArtifacts({
packageFileName: 'Gemfile',
Expand All @@ -152,8 +150,8 @@ describe('bundler.updateArtifacts()', () => {
expect(execSnapshots).toMatchSnapshot();
});
it('compatibility options', async () => {
gitfs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
gitfs.writeLocalFile.mockResolvedValueOnce(null as never);
fs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
fs.writeLocalFile.mockResolvedValueOnce(null as never);
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [
{ version: '1.0.0' },
Expand All @@ -165,7 +163,7 @@ describe('bundler.updateArtifacts()', () => {
platform.getRepoStatus.mockResolvedValueOnce({
modified: ['Gemfile.lock'],
} as Git.StatusResult);
gitfs.readLocalFile.mockResolvedValueOnce('Updated Gemfile.lock' as any);
fs.readLocalFile.mockResolvedValueOnce('Updated Gemfile.lock' as any);
expect(
await updateArtifacts({
packageFileName: 'Gemfile',
Expand All @@ -185,8 +183,8 @@ describe('bundler.updateArtifacts()', () => {
expect(execSnapshots).toMatchSnapshot();
});
it('invalid compatibility options', async () => {
gitfs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
gitfs.writeLocalFile.mockResolvedValueOnce(null as never);
fs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
fs.writeLocalFile.mockResolvedValueOnce(null as never);
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [
{ version: '1.0.0' },
Expand All @@ -198,7 +196,7 @@ describe('bundler.updateArtifacts()', () => {
platform.getRepoStatus.mockResolvedValueOnce({
modified: ['Gemfile.lock'],
} as Git.StatusResult);
gitfs.readLocalFile.mockResolvedValueOnce('Updated Gemfile.lock' as any);
fs.readLocalFile.mockResolvedValueOnce('Updated Gemfile.lock' as any);
expect(
await updateArtifacts({
packageFileName: 'Gemfile',
Expand All @@ -219,9 +217,9 @@ describe('bundler.updateArtifacts()', () => {
});

it('injects bundler host configuration environment variables', async () => {
gitfs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
gitfs.writeLocalFile.mockResolvedValueOnce(null as never);
gitfs.readLocalFile.mockResolvedValueOnce('1.2.0');
fs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
fs.writeLocalFile.mockResolvedValueOnce(null as never);
fs.readLocalFile.mockResolvedValueOnce('1.2.0');
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [
{ version: '1.0.0' },
Expand All @@ -245,7 +243,7 @@ describe('bundler.updateArtifacts()', () => {
platform.getRepoStatus.mockResolvedValueOnce({
modified: ['Gemfile.lock'],
} as Git.StatusResult);
gitfs.readLocalFile.mockResolvedValueOnce('Updated Gemfile.lock' as any);
fs.readLocalFile.mockResolvedValueOnce('Updated Gemfile.lock' as any);
expect(
await updateArtifacts({
packageFileName: 'Gemfile',
Expand All @@ -264,8 +262,8 @@ describe('bundler.updateArtifacts()', () => {
const execError = new Error();
(execError as any).stdout = ' foo was resolved to';
(execError as any).stderr = '';
gitfs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
gitfs.writeLocalFile.mockResolvedValueOnce(null as never);
fs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
fs.writeLocalFile.mockResolvedValueOnce(null as never);
const execSnapshots = mockExecAll(exec, execError);
platform.getRepoStatus.mockResolvedValueOnce({
modified: ['Gemfile.lock'],
Expand All @@ -284,13 +282,13 @@ describe('bundler.updateArtifacts()', () => {
expect(execSnapshots).toMatchSnapshot();
});
it('performs lockFileMaintenance', async () => {
gitfs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
gitfs.writeLocalFile.mockResolvedValueOnce(null as never);
fs.readLocalFile.mockResolvedValueOnce('Current Gemfile.lock');
fs.writeLocalFile.mockResolvedValueOnce(null as never);
const execSnapshots = mockExecAll(exec);
platform.getRepoStatus.mockResolvedValueOnce({
modified: ['Gemfile.lock'],
} as Git.StatusResult);
gitfs.readLocalFile.mockResolvedValueOnce('Updated Gemfile.lock' as any);
fs.readLocalFile.mockResolvedValueOnce('Updated Gemfile.lock' as any);
expect(
await updateArtifacts({
packageFileName: 'Gemfile',
Expand Down
18 changes: 8 additions & 10 deletions lib/manager/bundler/extract.spec.ts
@@ -1,11 +1,9 @@
import { readFileSync } from 'fs';
import * as _gitfs from '../../util/gitfs';
import { fs } from '../../../test/util';
import { isValid } from '../../versioning/ruby';
import { extractPackageFile } from './extract';

jest.mock('../../util/gitfs');

const gitfs: any = _gitfs;
jest.mock('../../util/gitfs/fs');

const railsGemfile = readFileSync(
'lib/manager/bundler/__fixtures__/Gemfile.rails',
Expand Down Expand Up @@ -74,7 +72,7 @@ describe('lib/manager/bundler/extract', () => {
expect(await extractPackageFile('nothing here', 'Gemfile')).toBeNull();
});
it('parses rails Gemfile', async () => {
gitfs.readLocalFile.mockReturnValueOnce(railsGemfileLock);
fs.readLocalFile.mockResolvedValueOnce(railsGemfileLock);
const res = await extractPackageFile(railsGemfile, 'Gemfile');
expect(res).toMatchSnapshot();
// couple of dependency of ruby rails are not present in the lock file. Filter out those before processing
Expand All @@ -98,7 +96,7 @@ describe('lib/manager/bundler/extract', () => {
validateGems(sourceGroupGemfile, res);
});
it('parse webpacker Gemfile', async () => {
gitfs.readLocalFile.mockReturnValueOnce(webPackerGemfileLock);
fs.readLocalFile.mockResolvedValueOnce(webPackerGemfileLock);
const res = await extractPackageFile(webPackerGemfile, 'Gemfile');
expect(res).toMatchSnapshot();
expect(
Expand All @@ -112,7 +110,7 @@ describe('lib/manager/bundler/extract', () => {
validateGems(webPackerGemfile, res);
});
it('parse mastodon Gemfile', async () => {
gitfs.readLocalFile.mockReturnValueOnce(mastodonGemfileLock);
fs.readLocalFile.mockResolvedValueOnce(mastodonGemfileLock);
const res = await extractPackageFile(mastodonGemfile, 'Gemfile');
expect(res).toMatchSnapshot();
expect(
Expand All @@ -130,7 +128,7 @@ describe('lib/manager/bundler/extract', () => {
validateGems(mastodonGemfile, res);
});
it('parse Ruby CI Gemfile', async () => {
gitfs.readLocalFile.mockReturnValueOnce(rubyCIGemfileLock);
fs.readLocalFile.mockResolvedValueOnce(rubyCIGemfileLock);
const res = await extractPackageFile(rubyCIGemfile, 'Gemfile');
expect(res).toMatchSnapshot();
expect(
Expand All @@ -145,7 +143,7 @@ describe('lib/manager/bundler/extract', () => {
});
});
it('parse Gitlab Foss Gemfile', async () => {
gitfs.readLocalFile.mockReturnValueOnce(gitlabFossGemfileLock);
fs.readLocalFile.mockResolvedValueOnce(gitlabFossGemfileLock);
const res = await extractPackageFile(gitlabFossGemfile, 'Gemfile');
expect(res).toMatchSnapshot();
expect(
Expand All @@ -160,7 +158,7 @@ describe('lib/manager/bundler/extract', () => {
});

it('parse source blocks with spaces in Gemfile', async () => {
gitfs.readLocalFile.mockReturnValueOnce(sourceBlockWithNewLinesGemfileLock);
fs.readLocalFile.mockResolvedValueOnce(sourceBlockWithNewLinesGemfileLock);
const res = await extractPackageFile(
sourceBlockWithNewLinesGemfile,
'Gemfile'
Expand Down

0 comments on commit 679176f

Please sign in to comment.