refactor: detectPackageFiles returns packageFiles not config (#1092)

This commit is contained in:
Rhys Arkins 2017-11-03 09:25:18 +01:00 committed by GitHub
parent 9c1b7d78cd
commit 9769f5a5db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 62 deletions

View file

@ -17,36 +17,32 @@ module.exports = {
getUpdatedPackageFiles, getUpdatedPackageFiles,
}; };
async function detectPackageFiles(input) { async function detectPackageFiles(config) {
const config = { ...input };
const { logger } = config; const { logger } = config;
let packageFiles = [];
const fileList = (await config.api.getFileList()).filter( const fileList = (await config.api.getFileList()).filter(
file => file =>
!config.ignorePaths.some( !config.ignorePaths.some(
ignorePath => file.includes(ignorePath) || minimatch(file, ignorePath) ignorePath => file.includes(ignorePath) || minimatch(file, ignorePath)
) )
); );
logger.debug({ config }, 'detectPackageFiles'); logger.trace({ config }, 'detectPackageFiles');
config.types = {};
const packageJsonFiles = await npmDetect.detectPackageFiles(config, fileList); const packageJsonFiles = await npmDetect.detectPackageFiles(config, fileList);
if (packageJsonFiles.length) { if (packageJsonFiles.length) {
logger.info({ packageJsonFiles }, 'Found package.json files'); logger.info({ packageJsonFiles }, 'Found package.json files');
config.packageFiles = config.packageFiles.concat(packageJsonFiles); packageFiles = packageFiles.concat(packageJsonFiles);
config.types.npm = true;
} }
const meteorFiles = await meteorDetect.detectPackageFiles(config, fileList); const meteorFiles = await meteorDetect.detectPackageFiles(config, fileList);
if (meteorFiles.length) { if (meteorFiles.length) {
logger.info({ packageJsonFiles }, 'Found meteor files'); logger.info({ packageJsonFiles }, 'Found meteor files');
config.packageFiles = config.packageFiles.concat(meteorFiles); packageFiles = packageFiles.concat(meteorFiles);
config.types.meteor = true;
} }
const dockerFiles = await dockerDetect.detectPackageFiles(config, fileList); const dockerFiles = await dockerDetect.detectPackageFiles(config, fileList);
if (dockerFiles.length) { if (dockerFiles.length) {
logger.info({ dockerFiles }, 'Found Dockerfiles'); logger.info({ dockerFiles }, 'Found Dockerfiles');
config.packageFiles = config.packageFiles.concat(dockerFiles); packageFiles = packageFiles.concat(dockerFiles);
config.types.docker = true;
} }
return config; return packageFiles;
} }
async function getPackageUpdates(config) { async function getPackageUpdates(config) {

View file

@ -75,7 +75,7 @@ async function renovateRepository(repoConfig, token) {
// Detect package files in default branch if not manually provisioned // Detect package files in default branch if not manually provisioned
if (config.packageFiles.length === 0) { if (config.packageFiles.length === 0) {
logger.debug('Detecting package files'); logger.debug('Detecting package files');
config = await manager.detectPackageFiles(config); config.packageFiles = await manager.detectPackageFiles(config);
// If we can't detect any package.json then return // If we can't detect any package.json then return
if (config.packageFiles.length === 0) { if (config.packageFiles.length === 0) {
logger.info('Cannot detect package files'); logger.info('Cannot detect package files');

View file

@ -10,10 +10,10 @@ module.exports = {
}; };
async function createOnboardingBranch(inputConfig) { async function createOnboardingBranch(inputConfig) {
let config = { ...inputConfig }; const config = { ...inputConfig };
const { logger } = config; const { logger } = config;
logger.debug('Creating onboarding branch'); logger.debug('Creating onboarding branch');
config = await manager.detectPackageFiles(config); config.packageFiles = await manager.detectPackageFiles(config);
if (config.packageFiles.length === 0) { if (config.packageFiles.length === 0) {
throw new Error('no package files'); throw new Error('no package files');
} }

View file

@ -22,12 +22,11 @@ Array [
exports[`manager detectPackageFiles(config) ignores node modules 1`] = ` exports[`manager detectPackageFiles(config) ignores node modules 1`] = `
Array [ Array [
"package.json", "package.json",
"not_node_mldules/backend/package.json",
] ]
`; `;
exports[`manager detectPackageFiles(config) ignores node modules 2`] = `undefined`; exports[`manager detectPackageFiles(config) ignores node modules 2`] = `undefined`;
exports[`manager detectPackageFiles(config) ignores node modules 3`] = `Array []`; exports[`manager detectPackageFiles(config) ignores node modules 3`] = `undefined`;
exports[`manager detectPackageFiles(config) skips meteor package files with no json 1`] = `Array []`; exports[`manager detectPackageFiles(config) skips meteor package files with no json 1`] = `Array []`;

View file

@ -28,8 +28,8 @@ describe('manager', () => {
'backend/package.json', 'backend/package.json',
]); ]);
const res = await manager.detectPackageFiles(config); const res = await manager.detectPackageFiles(config);
expect(res.packageFiles).toMatchSnapshot(); expect(res).toMatchSnapshot();
expect(res.packageFiles).toHaveLength(2); expect(res).toHaveLength(2);
}); });
it('finds meteor package files', async () => { it('finds meteor package files', async () => {
config.meteor.enabled = true; config.meteor.enabled = true;
@ -38,8 +38,8 @@ describe('manager', () => {
]); // meteor ]); // meteor
config.api.getFileContent.mockReturnValueOnce('Npm.depends( {} )'); config.api.getFileContent.mockReturnValueOnce('Npm.depends( {} )');
const res = await manager.detectPackageFiles(config); const res = await manager.detectPackageFiles(config);
expect(res.packageFiles).toMatchSnapshot(); expect(res).toMatchSnapshot();
expect(res.packageFiles).toHaveLength(1); expect(res).toHaveLength(1);
}); });
it('skips meteor package files with no json', async () => { it('skips meteor package files with no json', async () => {
config.meteor.enabled = true; config.meteor.enabled = true;
@ -48,8 +48,8 @@ describe('manager', () => {
]); // meteor ]); // meteor
config.api.getFileContent.mockReturnValueOnce('Npm.depends(packages)'); config.api.getFileContent.mockReturnValueOnce('Npm.depends(packages)');
const res = await manager.detectPackageFiles(config); const res = await manager.detectPackageFiles(config);
expect(res.packageFiles).toMatchSnapshot(); expect(res).toMatchSnapshot();
expect(res.packageFiles).toHaveLength(0); expect(res).toHaveLength(0);
}); });
it('finds Dockerfiles', async () => { it('finds Dockerfiles', async () => {
config.api.getFileList.mockReturnValueOnce([ config.api.getFileList.mockReturnValueOnce([
@ -63,18 +63,17 @@ describe('manager', () => {
'ARG foo\nFROM something\nRUN something' 'ARG foo\nFROM something\nRUN something'
); );
const res = await manager.detectPackageFiles(config); const res = await manager.detectPackageFiles(config);
expect(res.packageFiles).toMatchSnapshot(); expect(res).toMatchSnapshot();
expect(res.packageFiles).toHaveLength(1); expect(res).toHaveLength(1);
}); });
it('ignores node modules', async () => { it('ignores node modules', async () => {
config.api.getFileList.mockReturnValueOnce([ config.api.getFileList.mockReturnValueOnce([
'package.json', 'package.json',
'node_modules/backend/package.json', 'node_modules/backend/package.json',
'not_node_mldules/backend/package.json',
]); ]);
const res = await manager.detectPackageFiles(config); const res = await manager.detectPackageFiles(config);
expect(res.packageFiles).toMatchSnapshot(); expect(res).toMatchSnapshot();
expect(res.packageFiles).toHaveLength(2); expect(res).toHaveLength(1);
expect(res.foundIgnoredPaths).toMatchSnapshot(); expect(res.foundIgnoredPaths).toMatchSnapshot();
expect(res.warnings).toMatchSnapshot(); expect(res.warnings).toMatchSnapshot();
}); });

View file

@ -79,46 +79,31 @@ describe('workers/repository', () => {
it('does not skip repository if its a configured fork', async () => { it('does not skip repository if its a configured fork', async () => {
config.isFork = true; config.isFork = true;
config.renovateFork = true; config.renovateFork = true;
manager.detectPackageFiles.mockImplementationOnce(input => ({ manager.detectPackageFiles.mockReturnValueOnce([]);
...input,
...{ packageFiles: [] },
}));
await repositoryWorker.renovateRepository(config); await repositoryWorker.renovateRepository(config);
}); });
it('sets custom base branch', async () => { it('sets custom base branch', async () => {
config.baseBranch = 'some-branch'; config.baseBranch = 'some-branch';
config.api.branchExists.mockReturnValueOnce(true); config.api.branchExists.mockReturnValueOnce(true);
manager.detectPackageFiles.mockImplementationOnce(input => ({ manager.detectPackageFiles.mockReturnValueOnce([]);
...input,
...{ packageFiles: [] },
}));
await repositoryWorker.renovateRepository(config); await repositoryWorker.renovateRepository(config);
expect(config.api.setBaseBranch.mock.calls).toHaveLength(1); expect(config.api.setBaseBranch.mock.calls).toHaveLength(1);
}); });
it('errors when missing custom base branch', async () => { it('errors when missing custom base branch', async () => {
config.baseBranch = 'some-branch'; config.baseBranch = 'some-branch';
config.api.branchExists.mockReturnValueOnce(false); config.api.branchExists.mockReturnValueOnce(false);
manager.detectPackageFiles.mockImplementationOnce(input => ({ manager.detectPackageFiles.mockReturnValueOnce([]);
...input,
...{ packageFiles: [] },
}));
await repositoryWorker.renovateRepository(config); await repositoryWorker.renovateRepository(config);
expect(config.api.setBaseBranch.mock.calls).toHaveLength(0); expect(config.api.setBaseBranch.mock.calls).toHaveLength(0);
}); });
it('skips repository if no package.json', async () => { it('skips repository if no package.json', async () => {
manager.detectPackageFiles.mockImplementationOnce(input => ({ manager.detectPackageFiles.mockReturnValueOnce([]);
...input,
...{ packageFiles: [] },
}));
await repositoryWorker.renovateRepository(config); await repositoryWorker.renovateRepository(config);
expect(apis.resolvePackageFiles.mock.calls.length).toBe(0); expect(apis.resolvePackageFiles.mock.calls.length).toBe(0);
expect(config.logger.error.mock.calls.length).toBe(0); expect(config.logger.error.mock.calls.length).toBe(0);
}); });
it('does not skip repository if package.json', async () => { it('does not skip repository if package.json', async () => {
manager.detectPackageFiles.mockImplementationOnce(input => ({ manager.detectPackageFiles.mockReturnValueOnce(['package.json']);
...input,
...{ packageFiles: ['package.json'] },
}));
config.api.getFileJson = jest.fn(() => ({ a: 1 })); config.api.getFileJson = jest.fn(() => ({ a: 1 }));
apis.mergeRenovateJson.mockImplementationOnce(input => ({ apis.mergeRenovateJson.mockImplementationOnce(input => ({
...input, ...input,
@ -138,15 +123,9 @@ describe('workers/repository', () => {
expect(config.logger.error.mock.calls.length).toBe(0); expect(config.logger.error.mock.calls.length).toBe(0);
}); });
it('uses onboarding custom baseBranch', async () => { it('uses onboarding custom baseBranch', async () => {
manager.detectPackageFiles.mockImplementationOnce(input => ({ manager.detectPackageFiles.mockReturnValueOnce(['package.json']);
...input,
...{ packageFiles: ['package.json'] },
}));
config.api.getFileJson = jest.fn(() => ({ a: 1 })); config.api.getFileJson = jest.fn(() => ({ a: 1 }));
apis.mergeRenovateJson.mockImplementationOnce(input => ({ manager.detectPackageFiles.mockReturnValueOnce(['package.json']);
...input,
...{ packageFiles: ['package.json'] },
}));
apis.mergeRenovateJson.mockImplementationOnce(input => ({ apis.mergeRenovateJson.mockImplementationOnce(input => ({
...input, ...input,
...{ packageFiles: ['package.json'], baseBranch: 'next' }, ...{ packageFiles: ['package.json'], baseBranch: 'next' },
@ -162,10 +141,7 @@ describe('workers/repository', () => {
expect(config.logger.error.mock.calls.length).toBe(0); expect(config.logger.error.mock.calls.length).toBe(0);
}); });
it('errors onboarding custom baseBranch', async () => { it('errors onboarding custom baseBranch', async () => {
manager.detectPackageFiles.mockImplementationOnce(input => ({ manager.detectPackageFiles.mockReturnValueOnce(['package.json']);
...input,
...{ packageFiles: ['package.json'] },
}));
config.api.getFileJson = jest.fn(() => ({ a: 1 })); config.api.getFileJson = jest.fn(() => ({ a: 1 }));
apis.mergeRenovateJson.mockImplementationOnce(input => ({ apis.mergeRenovateJson.mockImplementationOnce(input => ({
...input, ...input,

View file

@ -248,9 +248,7 @@ describe('lib/workers/repository/onboarding', () => {
expect(config.api.commitFilesToBranch.mock.calls[0]).toMatchSnapshot(); expect(config.api.commitFilesToBranch.mock.calls[0]).toMatchSnapshot();
}); });
it('throws if no packageFiles', async () => { it('throws if no packageFiles', async () => {
manager.detectPackageFiles = jest.fn(input => ({ manager.detectPackageFiles = jest.fn(() => []);
...input,
}));
let e; let e;
try { try {
await onboarding.getOnboardingStatus(config); await onboarding.getOnboardingStatus(config);