refactor(composer): reuse localDir for lockfile generation

This commit is contained in:
Rhys Arkins 2018-09-10 15:58:24 +02:00
parent 6bd41d3541
commit 2ce75cdbf1
4 changed files with 32 additions and 13 deletions

View file

@ -1,6 +1,5 @@
const { exec } = require('child-process-promise');
const fs = require('fs-extra');
const tmp = require('tmp-promise');
const upath = require('upath');
const endpoints = require('../../util/endpoints');
@ -12,7 +11,8 @@ module.exports = {
async function getLockFile(
packageFileName,
updatedDeps,
newPackageFileContent
newPackageFileContent,
config
) {
logger.debug(`composer.getLockFile(${packageFileName})`);
const lockFileName = packageFileName.replace(/\.json$/, '.lock');
@ -21,19 +21,18 @@ async function getLockFile(
logger.debug('No composer.lock found');
return null;
}
const tmpDir = await tmp.dir({ unsafeCleanup: true });
const cwd = upath.join(tmpDir.path, upath.dirname(packageFileName));
const cwd = upath.join(config.localDir, upath.dirname(packageFileName));
let stdout;
let stderr;
try {
const localPackageFileName = upath.join(tmpDir.path, packageFileName);
const localPackageFileName = upath.join(config.localDir, packageFileName);
const newPackageFileParsed = JSON.parse(newPackageFileContent);
delete newPackageFileParsed.scripts;
await fs.outputFile(
localPackageFileName,
JSON.stringify(newPackageFileParsed)
);
const localLockFileName = upath.join(tmpDir.path, lockFileName);
const localLockFileName = upath.join(config.localDir, lockFileName);
await fs.outputFile(localLockFileName, existingLockFileContent);
const credentials = endpoints.find({
platform: 'github',
@ -84,7 +83,5 @@ async function getLockFile(
'Failed to generate composer.lock'
);
return null;
} finally {
tmpDir.cleanup();
}
}

View file

@ -61,7 +61,8 @@ async function getUpdatedPackageFiles(config) {
const updatedLockFile = await getLockFile(
packageFile.name,
updatedDeps,
packageFile.contents
packageFile.contents,
config
);
if (updatedLockFile) {
updatedLockFiles.push(updatedLockFile);

View file

@ -1,3 +1,5 @@
const tmp = require('tmp-promise');
const { initRepo } = require('./init');
const { ensureOnboardingPr } = require('./onboarding/pr');
const { handleError } = require('./error');
@ -15,7 +17,13 @@ async function renovateRepository(repoConfig) {
logger.setMeta({ repository: config.repository });
logger.info('Renovating repository');
logger.trace({ config });
let tmpDir;
try {
if (!config.localDir) {
// Use an ephemeral directory if none configured
tmpDir = await tmp.dir({ unsafeCleanup: true });
config.localDir = tmpDir.path;
}
config = await initRepo(config);
const { res, branches, branchList, packageFiles } = await processRepo(
config
@ -27,6 +35,9 @@ async function renovateRepository(repoConfig) {
return processResult(config, await handleError(config, err));
} finally {
platform.cleanRepo();
if (tmpDir) {
await tmpDir.cleanup();
}
logger.info('Finished repository');
}
}

View file

@ -5,12 +5,18 @@ const fs = require('fs-extra');
const { exec } = require('child-process-promise');
const composer = require('../../../lib/manager/composer/lock-file');
const config = {
localDir: '/tmp/github/some/repo',
};
describe('.getLockFile()', () => {
beforeEach(() => {
jest.resetAllMocks();
});
it('returns if no composer.lock found', async () => {
expect(await composer.getLockFile('composer.json', [], '{}')).toBeNull();
expect(
await composer.getLockFile('composer.json', [], '{}', config)
).toBeNull();
});
it('returns null if unchanged', async () => {
platform.getFile.mockReturnValueOnce('Current composer.lock');
@ -19,7 +25,9 @@ describe('.getLockFile()', () => {
stderror: '',
});
fs.readFile = jest.fn(() => 'Current composer.lock');
expect(await composer.getLockFile('composer.json', [], '{}')).toBeNull();
expect(
await composer.getLockFile('composer.json', [], '{}', config)
).toBeNull();
});
it('returns updated composer.lock', async () => {
platform.getFile.mockReturnValueOnce('Current composer.lock');
@ -29,7 +37,7 @@ describe('.getLockFile()', () => {
});
fs.readFile = jest.fn(() => 'New composer.lock');
expect(
await composer.getLockFile('composer.json', [], '{}')
await composer.getLockFile('composer.json', [], '{}', config)
).not.toBeNull();
});
it('catches errors', async () => {
@ -37,6 +45,8 @@ describe('.getLockFile()', () => {
fs.outputFile = jest.fn(() => {
throw new Error('not found');
});
expect(await composer.getLockFile('composer.json', [], '{}')).toBeNull();
expect(
await composer.getLockFile('composer.json', [], '{}', config)
).toBeNull();
});
});