Skip to content

Commit

Permalink
refactor: Call 'fs-extra' functions via proxies (#6683)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
zharinov and rarkins committed Jul 6, 2020
1 parent 50e36a1 commit c3fc80a
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 20 deletions.
17 changes: 8 additions & 9 deletions lib/manager/npm/post-update/index.ts
@@ -1,20 +1,19 @@
import path from 'path';
import upath from 'upath';
import { SYSTEM_INSUFFICIENT_DISK_SPACE } from '../../../constants/error-messages';
import { id as npmId } from '../../../datasource/npm';
import { logger } from '../../../logger';
import { ExternalHostError } from '../../../types/errors/external-host-error';
import { getChildProcessEnv } from '../../../util/exec/env';
import {
deleteLocalFile,
ensureDir,
outputFile,
readFile,
remove,
unlink,
writeFile,
} from 'fs-extra';
import upath from 'upath';
// eslint-disable-next-line import/no-unresolved
import { SYSTEM_INSUFFICIENT_DISK_SPACE } from '../../../constants/error-messages';
import { id as npmId } from '../../../datasource/npm';
import { logger } from '../../../logger';
import { ExternalHostError } from '../../../types/errors/external-host-error';
import { getChildProcessEnv } from '../../../util/exec/env';
import { deleteLocalFile } from '../../../util/fs';
} from '../../../util/fs';
import { branchExists, getFile, getRepoStatus } from '../../../util/git';
import * as hostRules from '../../../util/host-rules';
import { PackageFile, PostUpdateConfig, Upgrade } from '../../common';
Expand Down
8 changes: 4 additions & 4 deletions lib/manager/npm/post-update/npm.spec.ts
@@ -1,15 +1,15 @@
import { exec as _exec } from 'child_process';
import path from 'path';
import _fs from 'fs-extra';
import { envMock, mockExecAll } from '../../../../test/execUtil';
import { mocked } from '../../../../test/util';
import { BinarySource } from '../../../util/exec/common';
import * as _env from '../../../util/exec/env';
import * as _fs from '../../../util/fs/proxies';
import * as npmHelper from './npm';

jest.mock('fs-extra');
jest.mock('child_process');
jest.mock('../../../util/exec/env');
jest.mock('../../../util/fs/proxies');
jest.mock('./node-version');

const exec: jest.Mock<typeof _exec> = _exec as any;
Expand Down Expand Up @@ -64,7 +64,7 @@ describe('generateLockFile', () => {
});
it('performs npm-shrinkwrap.json updates', async () => {
const execSnapshots = mockExecAll(exec);
fs.pathExists.mockImplementationOnce(() => true);
fs.pathExists.mockResolvedValueOnce(true);
fs.move = jest.fn();
fs.readFile = jest.fn(() => 'package-lock-contents') as never;
const skipInstalls = true;
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('generateLockFile', () => {
});
it('performs npm-shrinkwrap.json updates (no package-lock.json)', async () => {
const execSnapshots = mockExecAll(exec);
fs.pathExists.mockImplementationOnce(() => false);
fs.pathExists.mockResolvedValueOnce(false);
fs.move = jest.fn();
fs.readFile = jest.fn((_, _1) => 'package-lock-contents') as never;
const skipInstalls = true;
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/npm/post-update/npm.ts
@@ -1,10 +1,10 @@
import { move, pathExists, readFile, remove } from 'fs-extra';
import { validRange } from 'semver';
import { quote } from 'shlex';
import { join } from 'upath';
import { SYSTEM_INSUFFICIENT_DISK_SPACE } from '../../../constants/error-messages';
import { logger } from '../../../logger';
import { ExecOptions, exec } from '../../../util/exec';
import { move, pathExists, readFile, remove } from '../../../util/fs';
import { PostUpdateConfig, Upgrade } from '../../common';
import { getNodeConstraint } from './node-version';

Expand Down
4 changes: 2 additions & 2 deletions lib/manager/npm/post-update/pnpm.spec.ts
@@ -1,14 +1,14 @@
import { exec as _exec } from 'child_process';
import _fs from 'fs-extra';
import { envMock, mockExecAll } from '../../../../test/execUtil';
import { mocked } from '../../../../test/util';
import * as _env from '../../../util/exec/env';
import * as _fs from '../../../util/fs/proxies';
import { PostUpdateConfig } from '../../common';
import * as _pnpmHelper from './pnpm';

jest.mock('fs-extra');
jest.mock('child_process');
jest.mock('../../../util/exec/env');
jest.mock('../../../util/fs/proxies');
jest.mock('./node-version');

const exec: jest.Mock<typeof _exec> = _exec as any;
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/npm/post-update/pnpm.ts
@@ -1,9 +1,9 @@
import { readFile, remove } from 'fs-extra';
import { validRange } from 'semver';
import { quote } from 'shlex';
import { join } from 'upath';
import { logger } from '../../../logger';
import { ExecOptions, exec } from '../../../util/exec';
import { readFile, remove } from '../../../util/fs';
import { PostUpdateConfig, Upgrade } from '../../common';
import { getNodeConstraint } from './node-version';

Expand Down
2 changes: 1 addition & 1 deletion lib/manager/npm/post-update/yarn.ts
@@ -1,5 +1,4 @@
import is from '@sindresorhus/is';
import { readFile, remove } from 'fs-extra';
import { validRange } from 'semver';
import { quote } from 'shlex';
import { join } from 'upath';
Expand All @@ -8,6 +7,7 @@ import { id as npmId } from '../../../datasource/npm';
import { logger } from '../../../logger';
import { ExternalHostError } from '../../../types/errors/external-host-error';
import { ExecOptions, exec } from '../../../util/exec';
import { readFile, remove } from '../../../util/fs';
import { PostUpdateConfig, Upgrade } from '../../common';
import { getNodeConstraint } from './node-version';

Expand Down
2 changes: 1 addition & 1 deletion lib/manager/nuget/extract.ts
@@ -1,11 +1,11 @@
import * as path from 'path';
import findUp from 'find-up';
import { readFile } from 'fs-extra';
import { XmlDocument } from 'xmldoc';
import * as datasourceNuget from '../../datasource/nuget';
import { logger } from '../../logger';
import { SkipReason } from '../../types';
import { clone } from '../../util/clone';
import { readFile } from '../../util/fs';
import { get } from '../../versioning';
import * as semverVersioning from '../../versioning/semver';
import { ExtractConfig, PackageDependency, PackageFile } from '../common';
Expand Down
2 changes: 2 additions & 0 deletions lib/util/fs/index.ts
Expand Up @@ -3,6 +3,8 @@ import { join, parse } from 'upath';
import { RenovateConfig } from '../../config/common';
import { logger } from '../../logger';

export * from './proxies';

let localDir = '';
let cacheDir = '';

Expand Down
72 changes: 72 additions & 0 deletions lib/util/fs/proxies.ts
@@ -0,0 +1,72 @@
import * as fs from 'fs-extra';
import { MoveOptions, WriteFileOptions } from 'fs-extra';

// istanbul ignore next
export function stat(path: string | Buffer): Promise<fs.Stats> {
return fs.stat(path);
}

// istanbul ignore next
export function chmod(
path: string | Buffer,
mode: string | number
): Promise<void> {
return fs.chmod(path, mode);
}

export async function readFile(fileName: string): Promise<Buffer>;
export async function readFile(
fileName: string,
encoding: 'utf8'
): Promise<string>;
export async function readFile(
fileName: string,
encoding?: string
): Promise<string | Buffer> {
return fs.readFile(fileName, encoding);
}

// istanbul ignore next
export async function writeFile(
fileName: string,
fileContent: string
): Promise<void> {
return fs.writeFile(fileName, fileContent);
}

// istanbul ignore next
export async function outputFile(
file: string,
data: any,
options?: WriteFileOptions | string
): Promise<void> {
return fs.outputFile(file, data, options);
}

export async function remove(dir: string): Promise<void> {
return fs.remove(dir);
}

// istanbul ignore next
export async function unlink(path: string | Buffer): Promise<void> {
return fs.unlink(path);
}

// istanbul ignore next
export async function exists(path: string): Promise<boolean> {
return fs.pathExists(path);
}

// istanbul ignore next
export async function pathExists(path: string): Promise<boolean> {
return fs.pathExists(path);
}

// istanbul ignore next
export function move(
src: string,
dest: string,
options?: MoveOptions
): Promise<void> {
return fs.move(src, dest, options);
}
2 changes: 1 addition & 1 deletion lib/workers/branch/lock-files/index.spec.ts
@@ -1,4 +1,3 @@
import _fs from 'fs-extra';
import { git, mocked } from '../../../../test/util';
import { getConfig } from '../../../config/defaults';
import { PostUpdateConfig } from '../../../manager/common';
Expand All @@ -7,6 +6,7 @@ import * as _lerna from '../../../manager/npm/post-update/lerna';
import * as _npm from '../../../manager/npm/post-update/npm';
import * as _pnpm from '../../../manager/npm/post-update/pnpm';
import * as _yarn from '../../../manager/npm/post-update/yarn';
import * as _fs from '../../../util/fs/proxies';
import * as _hostRules from '../../../util/host-rules';

const defaultConfig = getConfig();
Expand Down

0 comments on commit c3fc80a

Please sign in to comment.