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

View file

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

View file

@ -1,3 +1,5 @@
const tmp = require('tmp-promise');
const { initRepo } = require('./init'); const { initRepo } = require('./init');
const { ensureOnboardingPr } = require('./onboarding/pr'); const { ensureOnboardingPr } = require('./onboarding/pr');
const { handleError } = require('./error'); const { handleError } = require('./error');
@ -15,7 +17,13 @@ async function renovateRepository(repoConfig) {
logger.setMeta({ repository: config.repository }); logger.setMeta({ repository: config.repository });
logger.info('Renovating repository'); logger.info('Renovating repository');
logger.trace({ config }); logger.trace({ config });
let tmpDir;
try { 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); config = await initRepo(config);
const { res, branches, branchList, packageFiles } = await processRepo( const { res, branches, branchList, packageFiles } = await processRepo(
config config
@ -27,6 +35,9 @@ async function renovateRepository(repoConfig) {
return processResult(config, await handleError(config, err)); return processResult(config, await handleError(config, err));
} finally { } finally {
platform.cleanRepo(); platform.cleanRepo();
if (tmpDir) {
await tmpDir.cleanup();
}
logger.info('Finished repository'); logger.info('Finished repository');
} }
} }

View file

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