From 7d06bebe2e835456bffdbfc334312e100c7220af Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@keylocation.sg> Date: Wed, 25 Oct 2017 06:00:07 +0200 Subject: [PATCH] refactor: push detect dockerfiles to manager (#1043) * refactor platform * refactor detect package files * fix * refactor npm detect * refactor meteor detect * refactor: move detect package files completely to manager * update snapshots --- lib/manager/docker/detect.js | 21 +++++ lib/manager/index.js | 34 ++++++++ lib/manager/meteor/detect.js | 18 ++++ lib/manager/npm/detect.js | 15 ++++ lib/platform/github.js | 11 +-- lib/platform/gitlab.js | 11 +-- lib/workers/repository/apis.js | 84 ------------------- lib/workers/repository/index.js | 3 +- lib/workers/repository/onboarding.js | 3 +- test/manager/__snapshots__/index.spec.js.snap | 32 +++++++ test/manager/index.spec.js | 75 +++++++++++++++++ .../__snapshots__/github.spec.js.snap | 17 ++-- .../__snapshots__/gitlab.spec.js.snap | 17 ++-- test/platform/github.spec.js | 12 ++- test/platform/gitlab.spec.js | 12 +-- .../__snapshots__/apis.spec.js.snap | 31 ------- test/workers/repository/apis.spec.js | 78 ----------------- test/workers/repository/index.spec.js | 19 +++-- test/workers/repository/onboarding.spec.js | 14 ++-- 19 files changed, 248 insertions(+), 259 deletions(-) create mode 100644 lib/manager/docker/detect.js create mode 100644 lib/manager/meteor/detect.js create mode 100644 lib/manager/npm/detect.js create mode 100644 test/manager/__snapshots__/index.spec.js.snap create mode 100644 test/manager/index.spec.js diff --git a/lib/manager/docker/detect.js b/lib/manager/docker/detect.js new file mode 100644 index 0000000000..7818f7de06 --- /dev/null +++ b/lib/manager/docker/detect.js @@ -0,0 +1,21 @@ +module.exports = { + detectPackageFiles, +}; + +async function detectPackageFiles(config, fileList) { + const packageFiles = []; + if (config.docker.enabled) { + for (const file of fileList) { + if (file === 'Dockerfile' || file.endsWith('/Dockerfile')) { + const content = await config.api.getFileContent(file); + const strippedComment = content.replace(/^(#.*?\n)+/, ''); + // This means we skip ones with ARG for now + const fromMatch = strippedComment.startsWith('FROM '); + if (fromMatch) { + packageFiles.push(file); + } + } + } + } + return packageFiles; +} diff --git a/lib/manager/index.js b/lib/manager/index.js index 4c0f2cfe5d..746735b524 100644 --- a/lib/manager/index.js +++ b/lib/manager/index.js @@ -1,10 +1,44 @@ const docker = require('./docker/package'); const npm = require('./npm/package'); +const dockerDetect = require('./docker/detect'); +const meteorDetect = require('./meteor/detect'); +const npmDetect = require('./npm/detect'); + module.exports = { + detectPackageFiles, getPackageUpdates, }; +async function detectPackageFiles(input) { + const config = { ...input }; + const { logger } = config; + const fileList = (await config.api.getFileList()).filter( + file => !config.ignorePaths.some(ignorePath => file.includes(ignorePath)) + ); + logger.debug({ config }, 'detectPackageFiles'); + config.types = {}; + const packageJsonFiles = await npmDetect.detectPackageFiles(config, fileList); + if (packageJsonFiles.length) { + logger.info({ packageJsonFiles }, 'Found package.json files'); + config.packageFiles = config.packageFiles.concat(packageJsonFiles); + config.types.npm = true; + } + const meteorFiles = await meteorDetect.detectPackageFiles(config, fileList); + if (meteorFiles.length) { + logger.info({ packageJsonFiles }, 'Found meteor files'); + config.packageFiles = config.packageFiles.concat(meteorFiles); + config.types.meteor = true; + } + const dockerFiles = await dockerDetect.detectPackageFiles(config, fileList); + if (dockerFiles.length) { + logger.info({ dockerFiles }, 'Found Dockerfiles'); + config.packageFiles = config.packageFiles.concat(dockerFiles); + config.types.docker = true; + } + return config; +} + async function getPackageUpdates(config) { if (config.packageFile.endsWith('Dockerfile')) { return docker.getPackageUpdates(config); diff --git a/lib/manager/meteor/detect.js b/lib/manager/meteor/detect.js new file mode 100644 index 0000000000..80a3d7f19b --- /dev/null +++ b/lib/manager/meteor/detect.js @@ -0,0 +1,18 @@ +module.exports = { + detectPackageFiles, +}; + +async function detectPackageFiles(config, fileList) { + const packageFiles = []; + if (config.meteor.enabled) { + for (const file of fileList) { + if (file === 'package.js' || file.endsWith('/package.js')) { + const content = await config.api.getFileContent(file); + if (content && content.replace(/\s/g, '').includes('Npm.depends({')) { + packageFiles.push(file); + } + } + } + } + return packageFiles; +} diff --git a/lib/manager/npm/detect.js b/lib/manager/npm/detect.js new file mode 100644 index 0000000000..8ee0934324 --- /dev/null +++ b/lib/manager/npm/detect.js @@ -0,0 +1,15 @@ +module.exports = { + detectPackageFiles, +}; + +async function detectPackageFiles(config, fileList) { + const packageFiles = []; + if (config.npm.enabled) { + for (const file of fileList) { + if (file === 'package.json' || file.endsWith('/package.json')) { + packageFiles.push(file); + } + } + } + return packageFiles; +} diff --git a/lib/platform/github.js b/lib/platform/github.js index f78f2e7508..973b1e220f 100644 --- a/lib/platform/github.js +++ b/lib/platform/github.js @@ -13,7 +13,7 @@ module.exports = { initRepo, setBaseBranch, // Search - findFilePaths, + getFileList, // Branch branchExists, getAllRenovateBranches, @@ -236,7 +236,7 @@ async function setBaseBranch(branchName) { // Search // Get full file list -async function getFileList(branchName) { +async function getFileList(branchName = config.baseBranch) { if (config.fileList) { return config.fileList; } @@ -266,13 +266,6 @@ async function getFileList(branchName) { return config.fileList; } -// Return all files in the repository matching the filename -async function findFilePaths(fileName, branchName = config.baseBranch) { - return (await getFileList(branchName)).filter(fullFilePath => - fullFilePath.endsWith(fileName) - ); -} - // Branch // Returns true if branch exists, otherwise false diff --git a/lib/platform/gitlab.js b/lib/platform/gitlab.js index f395b27afb..8c3cd44072 100644 --- a/lib/platform/gitlab.js +++ b/lib/platform/gitlab.js @@ -8,7 +8,7 @@ module.exports = { initRepo, setBaseBranch, // Search - findFilePaths, + getFileList, // Branch branchExists, createBranch, @@ -117,7 +117,7 @@ async function setBaseBranch(branchName) { // Search // Get full file list -async function getFileList(branchName) { +async function getFileList(branchName = config.baseBranch) { if (config.fileList) { return config.fileList; } @@ -137,13 +137,6 @@ async function getFileList(branchName) { return config.fileList; } -// Return all files in the repository matching the filename -async function findFilePaths(fileName, branchName = config.baseBranch) { - return (await getFileList(branchName)).filter(fullFilePath => - fullFilePath.endsWith(fileName) - ); -} - // Branch // Returns true if branch exists, otherwise false diff --git a/lib/workers/repository/apis.js b/lib/workers/repository/apis.js index d55812a71c..3bb7cd8870 100644 --- a/lib/workers/repository/apis.js +++ b/lib/workers/repository/apis.js @@ -17,7 +17,6 @@ module.exports = { getNpmrc, initApis, mergeRenovateJson, - detectPackageFiles, resolvePackageFiles, migrateAndValidate, }; @@ -227,89 +226,6 @@ async function mergeRenovateJson(config, branchName) { return returnConfig; } -async function detectPackageFiles(input) { - const config = { ...input }; - const { logger } = config; - function filterIgnorePaths(packageFiles, ignorePaths) { - logger.debug('Checking ignorePaths'); - return packageFiles.filter(packageFile => { - logger.trace(`Checking ${packageFile}`); - if ( - ignorePaths.some(ignorePath => { - logger.trace(` ..against ${ignorePath}`); - return packageFile.includes(ignorePath); - }) - ) { - logger.trace('Filtered out'); - return false; - } - logger.trace('Included'); - return true; - }); - } - logger.debug({ config }, 'detectPackageFiles'); - config.types = {}; - if (config.npm.enabled) { - config.packageFiles = filterIgnorePaths( - await config.api.findFilePaths('package.json'), - config.ignorePaths - ); - logger.debug( - { packageFiles: config.packageFiles }, - `Found ${config.packageFiles.length} package.json file(s)` - ); - if (config.packageFiles.length) { - config.types.npm = true; - } - } - if (config.meteor.enabled) { - logger.debug('Detecting meteor package.js files'); - const allPackageJs = filterIgnorePaths( - await config.api.findFilePaths('package.js'), - config.ignorePaths - ); - const meteorPackageFiles = []; - for (const mFile of allPackageJs) { - const packageJsContent = await config.api.getFileContent(mFile); - if ( - packageJsContent && - packageJsContent.replace(/\s/g, '').includes('Npm.depends({') - ) { - meteorPackageFiles.push(mFile); - } - } - if (meteorPackageFiles.length) { - logger.info( - { count: meteorPackageFiles.length }, - `Found meteor package files` - ); - config.types.meteor = true; - config.packageFiles = config.packageFiles.concat(meteorPackageFiles); - } - } - if (config.docker.enabled) { - logger.debug('Detecting Dockerfiles'); - const files = filterIgnorePaths( - await config.api.findFilePaths('Dockerfile'), - config.ignorePaths - ); - for (const file of files) { - const content = await config.api.getFileContent(file); - const strippedComment = content.replace(/^(#.*?\n)+/, ''); - // This means we skip ones with ARG for now - const fromMatch = strippedComment.startsWith('FROM '); - if (fromMatch) { - config.packageFiles.push(file); - config.types.docker = true; - } - } - if (config.types.docker) { - logger.info(`Found Dockerfiles`); - } - } - return config; -} - async function resolvePackageFiles(inputConfig) { const config = { ...inputConfig }; const { logger } = config; diff --git a/lib/workers/repository/index.js b/lib/workers/repository/index.js index 526fb6b541..fa068fe5b6 100644 --- a/lib/workers/repository/index.js +++ b/lib/workers/repository/index.js @@ -1,5 +1,6 @@ const convertHrTime = require('convert-hrtime'); const tmp = require('tmp-promise'); +const manager = require('../../manager'); // Workers const branchWorker = require('../branch'); // children @@ -68,7 +69,7 @@ async function renovateRepository(repoConfig, token) { // Detect package files in default branch if not manually provisioned if (config.packageFiles.length === 0) { logger.debug('Detecting package files'); - config = await apis.detectPackageFiles(config); + config = await manager.detectPackageFiles(config); // If we can't detect any package.json then return if (config.packageFiles.length === 0) { logger.info('Cannot detect package files'); diff --git a/lib/workers/repository/onboarding.js b/lib/workers/repository/onboarding.js index 21eff58aa5..2ac29a06bb 100644 --- a/lib/workers/repository/onboarding.js +++ b/lib/workers/repository/onboarding.js @@ -1,4 +1,5 @@ const apis = require('./apis'); +const manager = require('../../manager'); const onboardPrTitle = 'Configure Renovate'; @@ -26,7 +27,7 @@ async function createOnboardingBranch(inputConfig) { let config = { ...inputConfig }; const { logger } = config; logger.debug('Creating onboarding branch'); - config = await apis.detectPackageFiles(config); + config = await manager.detectPackageFiles(config); if (config.packageFiles.length === 0) { throw new Error('no package files'); } diff --git a/test/manager/__snapshots__/index.spec.js.snap b/test/manager/__snapshots__/index.spec.js.snap new file mode 100644 index 0000000000..f40065dc21 --- /dev/null +++ b/test/manager/__snapshots__/index.spec.js.snap @@ -0,0 +1,32 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`manager detectPackageFiles(config) adds package files to object 1`] = ` +Array [ + "package.json", + "backend/package.json", +] +`; + +exports[`manager detectPackageFiles(config) finds Dockerfiles 1`] = ` +Array [ + "Dockerfile", +] +`; + +exports[`manager detectPackageFiles(config) finds meteor package files 1`] = ` +Array [ + "modules/something/package.js", +] +`; + +exports[`manager detectPackageFiles(config) ignores node modules 1`] = ` +Array [ + "package.json", +] +`; + +exports[`manager detectPackageFiles(config) ignores node modules 2`] = `undefined`; + +exports[`manager detectPackageFiles(config) ignores node modules 3`] = `Array []`; + +exports[`manager detectPackageFiles(config) skips meteor package files with no json 1`] = `Array []`; diff --git a/test/manager/index.spec.js b/test/manager/index.spec.js new file mode 100644 index 0000000000..94d1ed5830 --- /dev/null +++ b/test/manager/index.spec.js @@ -0,0 +1,75 @@ +const logger = require('../_fixtures/logger'); +const defaultConfig = require('../../lib/config/defaults').getConfig(); +const manager = require('../../lib/manager'); + +describe('manager', () => { + describe('detectPackageFiles(config)', () => { + let config; + beforeEach(() => { + config = { + ...defaultConfig, + api: { + getFileList: jest.fn(() => []), + getFileContent: jest.fn(), + }, + logger, + warnings: [], + }; + }); + it('adds package files to object', async () => { + config.api.getFileList.mockReturnValueOnce([ + 'package.json', + 'backend/package.json', + ]); + const res = await manager.detectPackageFiles(config); + expect(res.packageFiles).toMatchSnapshot(); + expect(res.packageFiles).toHaveLength(2); + }); + it('finds meteor package files', async () => { + config.meteor.enabled = true; + config.api.getFileList.mockReturnValueOnce([ + 'modules/something/package.js', + ]); // meteor + config.api.getFileContent.mockReturnValueOnce('Npm.depends( {} )'); + const res = await manager.detectPackageFiles(config); + expect(res.packageFiles).toMatchSnapshot(); + expect(res.packageFiles).toHaveLength(1); + }); + it('skips meteor package files with no json', async () => { + config.meteor.enabled = true; + config.api.getFileList.mockReturnValueOnce([ + 'modules/something/package.js', + ]); // meteor + config.api.getFileContent.mockReturnValueOnce('Npm.depends(packages)'); + const res = await manager.detectPackageFiles(config); + expect(res.packageFiles).toMatchSnapshot(); + expect(res.packageFiles).toHaveLength(0); + }); + it('finds Dockerfiles', async () => { + config.api.getFileList.mockReturnValueOnce([ + 'Dockerfile', + 'other/Dockerfile', + ]); + config.api.getFileContent.mockReturnValueOnce( + '### comment\nFROM something\nRUN something' + ); + config.api.getFileContent.mockReturnValueOnce( + 'ARG foo\nFROM something\nRUN something' + ); + const res = await manager.detectPackageFiles(config); + expect(res.packageFiles).toMatchSnapshot(); + expect(res.packageFiles).toHaveLength(1); + }); + it('ignores node modules', async () => { + config.api.getFileList.mockReturnValueOnce([ + 'package.json', + 'node_modules/backend/package.json', + ]); + const res = await manager.detectPackageFiles(config); + expect(res.packageFiles).toMatchSnapshot(); + expect(res.packageFiles).toHaveLength(1); + expect(res.foundIgnoredPaths).toMatchSnapshot(); + expect(res.warnings).toMatchSnapshot(); + }); + }); +}); diff --git a/test/platform/__snapshots__/github.spec.js.snap b/test/platform/__snapshots__/github.spec.js.snap index dc6cd2505b..f863af2c82 100644 --- a/test/platform/__snapshots__/github.spec.js.snap +++ b/test/platform/__snapshots__/github.spec.js.snap @@ -465,14 +465,6 @@ content", ] `; -exports[`platform/github findFilePaths(fileName) should return the files matching the fileName 1`] = ` -Array [ - "package.json", - "src/app/package.json", - "src/otherapp/package.json", -] -`; - exports[`platform/github getAllRenovateBranches() should return all renovate branches 1`] = ` Array [ "renovate/a", @@ -689,6 +681,15 @@ Object { } `; +exports[`platform/github getFileList should return the files matching the fileName 1`] = ` +Array [ + "package.json", + "some-dir/package.json.some-thing-else", + "src/app/package.json", + "src/otherapp/package.json", +] +`; + exports[`platform/github getInstallationRepositories should return an array of repositories 1`] = ` Array [ Array [ diff --git a/test/platform/__snapshots__/gitlab.spec.js.snap b/test/platform/__snapshots__/gitlab.spec.js.snap index 0bd6371145..a3ef2a1e75 100644 --- a/test/platform/__snapshots__/gitlab.spec.js.snap +++ b/test/platform/__snapshots__/gitlab.spec.js.snap @@ -193,14 +193,6 @@ Array [ ] `; -exports[`platform/gitlab findFilePaths(fileName) should return the files matching the fileName 1`] = ` -Array [ - "package.json", - "src/app/package.json", - "src/otherapp/package.json", -] -`; - exports[`platform/gitlab getBranch returns a branch 1`] = `"foo"`; exports[`platform/gitlab getBranchLastCommitTime should return a Date 1`] = `2012-09-20T08:50:22.000Z`; @@ -288,6 +280,15 @@ Object { } `; +exports[`platform/gitlab getFileList should return the files matching the fileName 1`] = ` +Array [ + "package.json", + "some-dir/package.json.some-thing-else", + "src/app/package.json", + "src/otherapp/package.json", +] +`; + exports[`platform/gitlab getPr(prNo) returns the PR 1`] = ` Object { "body": "a merge request", diff --git a/test/platform/github.spec.js b/test/platform/github.spec.js index d798f1f1bd..74297af5e3 100644 --- a/test/platform/github.spec.js +++ b/test/platform/github.spec.js @@ -475,11 +475,9 @@ describe('platform/github', () => { get.mockImplementationOnce(() => { throw new Error('some error'); }); - const files = await github.findFilePaths('someething'); + const files = await github.getFileList(); expect(files).toEqual([]); }); - }); - describe('findFilePaths(fileName)', () => { it('warns if truncated result', async () => { await initRepo('some/repo', 'token'); get.mockImplementationOnce(() => ({ @@ -488,7 +486,7 @@ describe('platform/github', () => { tree: [], }, })); - const files = await github.findFilePaths('package.json'); + const files = await github.getFileList(); expect(files.length).toBe(0); }); it('caches the result', async () => { @@ -499,9 +497,9 @@ describe('platform/github', () => { tree: [], }, })); - let files = await github.findFilePaths('package.json'); + let files = await github.getFileList(); expect(files.length).toBe(0); - files = await github.findFilePaths('package.js'); + files = await github.getFileList(); expect(files.length).toBe(0); }); it('should return the files matching the fileName', async () => { @@ -520,7 +518,7 @@ describe('platform/github', () => { ], }, })); - const files = await github.findFilePaths('package.json'); + const files = await github.getFileList(); expect(files).toMatchSnapshot(); }); }); diff --git a/test/platform/gitlab.spec.js b/test/platform/gitlab.spec.js index 775e955195..f2f346e5bf 100644 --- a/test/platform/gitlab.spec.js +++ b/test/platform/gitlab.spec.js @@ -171,13 +171,13 @@ describe('platform/gitlab', () => { expect(get.mock.calls).toMatchSnapshot(); }); }); - describe('findFilePaths(fileName)', () => { + describe('getFileList', () => { it('returns empty array if error', async () => { await initRepo('some/repo', 'token'); get.mockImplementationOnce(() => { throw new Error('some error'); }); - const files = await gitlab.findFilePaths('someething'); + const files = await gitlab.getFileList(); expect(files).toEqual([]); }); it('warns if truncated result', async () => { @@ -185,7 +185,7 @@ describe('platform/gitlab', () => { get.mockImplementationOnce(() => ({ body: [], })); - const files = await gitlab.findFilePaths('package.json'); + const files = await gitlab.getFileList(); expect(files.length).toBe(0); }); it('caches the result', async () => { @@ -193,9 +193,9 @@ describe('platform/gitlab', () => { get.mockImplementationOnce(() => ({ body: [], })); - let files = await gitlab.findFilePaths('package.json'); + let files = await gitlab.getFileList(); expect(files.length).toBe(0); - files = await gitlab.findFilePaths('package.js'); + files = await gitlab.getFileList(); expect(files.length).toBe(0); }); it('should return the files matching the fileName', async () => { @@ -212,7 +212,7 @@ describe('platform/gitlab', () => { { type: 'blob', path: 'src/otherapp/package.json' }, ], })); - const files = await gitlab.findFilePaths('package.json'); + const files = await gitlab.getFileList(); expect(files).toMatchSnapshot(); }); }); diff --git a/test/workers/repository/__snapshots__/apis.spec.js.snap b/test/workers/repository/__snapshots__/apis.spec.js.snap index dce7c2e091..ede1b66dc1 100644 --- a/test/workers/repository/__snapshots__/apis.spec.js.snap +++ b/test/workers/repository/__snapshots__/apis.spec.js.snap @@ -30,37 +30,6 @@ Array [ exports[`workers/repository/apis checkMonorepos skips if no lerna packages 1`] = `Array []`; -exports[`workers/repository/apis detectPackageFiles(config) adds package files to object 1`] = ` -Array [ - "package.json", - "backend/package.json", -] -`; - -exports[`workers/repository/apis detectPackageFiles(config) finds Dockerfiles 1`] = ` -Array [ - "Dockerfile", -] -`; - -exports[`workers/repository/apis detectPackageFiles(config) finds meteor package files 1`] = ` -Array [ - "modules/something/package.js", -] -`; - -exports[`workers/repository/apis detectPackageFiles(config) ignores node modules 1`] = ` -Array [ - "package.json", -] -`; - -exports[`workers/repository/apis detectPackageFiles(config) ignores node modules 2`] = `undefined`; - -exports[`workers/repository/apis detectPackageFiles(config) ignores node modules 3`] = `Array []`; - -exports[`workers/repository/apis detectPackageFiles(config) skips meteor package files with no json 1`] = `Array []`; - exports[`workers/repository/apis initApis(config) throws if unknown platform 1`] = `"Unknown platform: foo"`; exports[`workers/repository/apis mergeRenovateJson(config) returns error in config if renovate.json cannot be parsed 1`] = ` diff --git a/test/workers/repository/apis.spec.js b/test/workers/repository/apis.spec.js index 04c837baab..180bd9c5df 100644 --- a/test/workers/repository/apis.spec.js +++ b/test/workers/repository/apis.spec.js @@ -252,84 +252,6 @@ describe('workers/repository/apis', () => { expect(e).toBeDefined(); }); }); - describe('detectPackageFiles(config)', () => { - let config; - beforeEach(() => { - config = { - ...defaultConfig, - api: { - findFilePaths: jest.fn(), - getFileContent: jest.fn(), - }, - logger, - warnings: [], - }; - config.api.findFilePaths.mockReturnValue([]); - }); - it('adds package files to object', async () => { - config.api.findFilePaths.mockReturnValueOnce([ - 'package.json', - 'backend/package.json', - ]); - const res = await apis.detectPackageFiles(config); - expect(res.packageFiles).toMatchSnapshot(); - expect(res.packageFiles).toHaveLength(2); - }); - it('finds meteor package files', async () => { - config.meteor.enabled = true; - config.api.findFilePaths.mockReturnValueOnce([]); // package.json - config.api.findFilePaths.mockReturnValueOnce([ - 'modules/something/package.js', - ]); // meteor - config.api.findFilePaths.mockReturnValueOnce([]); // Dockerfile - config.api.getFileContent.mockReturnValueOnce('Npm.depends( {} )'); - const res = await apis.detectPackageFiles(config); - expect(res.packageFiles).toMatchSnapshot(); - expect(res.packageFiles).toHaveLength(1); - }); - it('skips meteor package files with no json', async () => { - config.meteor.enabled = true; - config.api.findFilePaths.mockReturnValueOnce([]); // package.json - config.api.findFilePaths.mockReturnValueOnce([ - 'modules/something/package.js', - ]); // meteor - config.api.findFilePaths.mockReturnValueOnce([]); // Dockerfile - config.api.getFileContent.mockReturnValueOnce('Npm.depends(packages)'); - const res = await apis.detectPackageFiles(config); - expect(res.packageFiles).toMatchSnapshot(); - expect(res.packageFiles).toHaveLength(0); - }); - it('finds Dockerfiles', async () => { - config.api.findFilePaths.mockReturnValueOnce([]); - config.api.findFilePaths.mockReturnValueOnce([]); - config.api.findFilePaths.mockReturnValueOnce([ - 'Dockerfile', - 'other/Dockerfile', - ]); - config.api.getFileContent.mockReturnValueOnce( - '### comment\nFROM something\nRUN something' - ); - config.api.getFileContent.mockReturnValueOnce( - 'ARG foo\nFROM something\nRUN something' - ); - const res = await apis.detectPackageFiles(config); - expect(res.packageFiles).toMatchSnapshot(); - expect(res.packageFiles).toHaveLength(1); - }); - it('ignores node modules', async () => { - config.api.findFilePaths.mockReturnValueOnce([ - 'package.json', - 'node_modules/backend/package.json', - ]); - config.api.findFilePaths.mockReturnValueOnce([]); - config.api.findFilePaths.mockReturnValueOnce([]); - const res = await apis.detectPackageFiles(config); - expect(res.packageFiles).toMatchSnapshot(); - expect(res.packageFiles).toHaveLength(1); - expect(res.foundIgnoredPaths).toMatchSnapshot(); - expect(res.warnings).toMatchSnapshot(); - }); - }); describe('resolvePackageFiles', () => { let config; beforeEach(() => { diff --git a/test/workers/repository/index.spec.js b/test/workers/repository/index.spec.js index 9f0b158def..bed7d6c8cd 100644 --- a/test/workers/repository/index.spec.js +++ b/test/workers/repository/index.spec.js @@ -2,6 +2,7 @@ const repositoryWorker = require('../../../lib/workers/repository/index'); const branchWorker = require('../../../lib/workers/branch'); const apis = require('../../../lib/workers/repository/apis'); +const manager = require('../../../lib/manager'); const onboarding = require('../../../lib/workers/repository/onboarding'); const upgrades = require('../../../lib/workers/repository/upgrades'); @@ -45,7 +46,7 @@ describe('workers/repository', () => { jest.resetAllMocks(); apis.initApis = jest.fn(input => input); apis.mergeRenovateJson = jest.fn(input => input); - apis.detectPackageFiles = jest.fn(); + manager.detectPackageFiles = jest.fn(); apis.resolvePackageFiles = jest.fn(input => input); apis.checkMonorepos = jest.fn(input => input); onboarding.getOnboardingStatus = jest.fn(input => input); @@ -67,18 +68,18 @@ describe('workers/repository', () => { it('skips repository if config is disabled', async () => { config.enabled = false; await repositoryWorker.renovateRepository(config); - expect(apis.detectPackageFiles.mock.calls.length).toBe(0); + expect(manager.detectPackageFiles.mock.calls.length).toBe(0); }); it('skips repository if its unconfigured fork', async () => { config.isFork = true; config.renovateJsonPresent = false; await repositoryWorker.renovateRepository(config); - expect(apis.detectPackageFiles.mock.calls.length).toBe(0); + expect(manager.detectPackageFiles.mock.calls.length).toBe(0); }); it('sets custom base branch', async () => { config.baseBranch = 'some-branch'; config.api.branchExists.mockReturnValueOnce(true); - apis.detectPackageFiles.mockImplementationOnce(input => ({ + manager.detectPackageFiles.mockImplementationOnce(input => ({ ...input, ...{ packageFiles: [] }, })); @@ -88,7 +89,7 @@ describe('workers/repository', () => { it('errors when missing custom base branch', async () => { config.baseBranch = 'some-branch'; config.api.branchExists.mockReturnValueOnce(false); - apis.detectPackageFiles.mockImplementationOnce(input => ({ + manager.detectPackageFiles.mockImplementationOnce(input => ({ ...input, ...{ packageFiles: [] }, })); @@ -96,7 +97,7 @@ describe('workers/repository', () => { expect(config.api.setBaseBranch.mock.calls).toHaveLength(0); }); it('skips repository if no package.json', async () => { - apis.detectPackageFiles.mockImplementationOnce(input => ({ + manager.detectPackageFiles.mockImplementationOnce(input => ({ ...input, ...{ packageFiles: [] }, })); @@ -105,7 +106,7 @@ describe('workers/repository', () => { expect(config.logger.error.mock.calls.length).toBe(0); }); it('does not skip repository if package.json', async () => { - apis.detectPackageFiles.mockImplementationOnce(input => ({ + manager.detectPackageFiles.mockImplementationOnce(input => ({ ...input, ...{ packageFiles: ['package.json'] }, })); @@ -128,7 +129,7 @@ describe('workers/repository', () => { expect(config.logger.error.mock.calls.length).toBe(0); }); it('uses onboarding custom baseBranch', async () => { - apis.detectPackageFiles.mockImplementationOnce(input => ({ + manager.detectPackageFiles.mockImplementationOnce(input => ({ ...input, ...{ packageFiles: ['package.json'] }, })); @@ -152,7 +153,7 @@ describe('workers/repository', () => { expect(config.logger.error.mock.calls.length).toBe(0); }); it('errors onboarding custom baseBranch', async () => { - apis.detectPackageFiles.mockImplementationOnce(input => ({ + manager.detectPackageFiles.mockImplementationOnce(input => ({ ...input, ...{ packageFiles: ['package.json'] }, })); diff --git a/test/workers/repository/onboarding.spec.js b/test/workers/repository/onboarding.spec.js index 89087848e6..fd620a2f6a 100644 --- a/test/workers/repository/onboarding.spec.js +++ b/test/workers/repository/onboarding.spec.js @@ -1,5 +1,5 @@ const onboarding = require('../../../lib/workers/repository/onboarding'); -const apis = require('../../../lib/workers/repository/apis'); +const manager = require('../../../lib/manager'); const logger = require('../../_fixtures/logger'); const defaultConfig = require('../../../lib/config/defaults').getConfig(); @@ -226,7 +226,7 @@ describe('lib/workers/repository/onboarding', () => { config.api = { commitFilesToBranch: jest.fn(), createPr: jest.fn(() => ({ displayNumber: 1 })), - findFilePaths: jest.fn(() => []), + getFileList: jest.fn(() => []), findPr: jest.fn(), getFileContent: jest.fn(), getFileJson: jest.fn(() => ({})), @@ -267,8 +267,7 @@ describe('lib/workers/repository/onboarding', () => { expect(config.api.commitFilesToBranch.mock.calls.length).toBe(0); }); it('commits files and returns false if no pr', async () => { - config.api.findFilePaths.mockReturnValueOnce(['package.json']); - config.api.findFilePaths.mockReturnValue([]); + config.api.getFileList.mockReturnValueOnce(['package.json']); const res = await onboarding.getOnboardingStatus(config); expect(res.repoIsOnboarded).toEqual(false); expect(config.api.findPr.mock.calls.length).toBe(1); @@ -276,8 +275,7 @@ describe('lib/workers/repository/onboarding', () => { expect(config.api.commitFilesToBranch.mock.calls[0]).toMatchSnapshot(); }); it('pins private repos', async () => { - config.api.findFilePaths.mockReturnValueOnce(['package.json']); - config.api.findFilePaths.mockReturnValue([]); + config.api.getFileList.mockReturnValueOnce(['package.json']); onboarding.isRepoPrivate.mockReturnValueOnce(true); const res = await onboarding.getOnboardingStatus(config); expect(res.repoIsOnboarded).toEqual(false); @@ -286,7 +284,7 @@ describe('lib/workers/repository/onboarding', () => { expect(config.api.commitFilesToBranch.mock.calls[0]).toMatchSnapshot(); }); it('uses base + docker', async () => { - apis.detectPackageFiles = jest.fn(input => ({ + manager.detectPackageFiles = jest.fn(input => ({ ...input, packageFiles: [{}, {}], types: { @@ -300,7 +298,7 @@ describe('lib/workers/repository/onboarding', () => { expect(config.api.commitFilesToBranch.mock.calls[0]).toMatchSnapshot(); }); it('throws if no packageFiles', async () => { - apis.detectPackageFiles = jest.fn(input => ({ + manager.detectPackageFiles = jest.fn(input => ({ ...input, })); let e; -- GitLab