From 852ce6e6e52e6552e9f323015d60780118de788d Mon Sep 17 00:00:00 2001 From: anderspree Date: Wed, 9 May 2018 14:41:14 +0200 Subject: [PATCH] fix(create-version-branch): adapt to new structure of files object * tests(create-version-branch): update to ne files object structure * tests(create-version-branch): fix tests to test in-range behaviour * tests(create-version-branch): update tests * fix(create-version-branch): adapt to new structure of files object --- jobs/create-version-branch.js | 14 +- test/jobs/create-version-branch.js | 198 +++++++++++++++++++++-------- 2 files changed, 157 insertions(+), 55 deletions(-) diff --git a/jobs/create-version-branch.js b/jobs/create-version-branch.js index e8d9270f..d19eee4a 100644 --- a/jobs/create-version-branch.js +++ b/jobs/create-version-branch.js @@ -62,12 +62,16 @@ module.exports = async function ( // // See this issue for details: https://github.com/greenkeeperio/greenkeeper/issues/506 - const moduleLockFiles = ['npm-shrinkwrap.json'] - const projectLockFiles = ['package-lock.json', 'yarn.lock'] - const hasModuleLockFile = _.some(_.pick(repository.files, moduleLockFiles)) - const hasProjectLockFile = _.some(_.pick(repository.files, projectLockFiles)) - const usesGreenkeeperLockfile = _.some(_.pick(repository.packages['package.json'].devDependencies, 'greenkeeper-lockfile')) + function isTrue (x) { + if (typeof x === 'object') { + return !!x.length + } + return x + } + const hasModuleLockFile = isTrue(repository.files['npm-shrinkwrap.json']) + const hasProjectLockFile = isTrue(repository.files['package-lock.json']) || isTrue(repository.files['yarn.lock']) + const usesGreenkeeperLockfile = _.some(_.pick(repository.packages['package.json'].devDependencies, 'greenkeeper-lockfile')) // Bail if it’s in range and the repo uses shrinkwrap if (satisfies && hasModuleLockFile) { log.info('exited: dependency satisfies semver & repository has a module lockfile (shrinkwrap type)') diff --git a/test/jobs/create-version-branch.js b/test/jobs/create-version-branch.js index 334d7315..321e1a45 100644 --- a/test/jobs/create-version-branch.js +++ b/test/jobs/create-version-branch.js @@ -55,6 +55,12 @@ describe('create version brach', () => { _id: '1', accountId: '123', fullName: 'finnp/test', + files: { + 'package.json': ['package.json'], + 'package-lock.json': [], + 'npm-shrinkwrap.json': [], + 'yarn.lock': [] + }, packages: { 'package.json': { greenkeeper: { @@ -183,6 +189,12 @@ describe('create version brach', () => { accountId: '124', fullName: 'finnp/testtest', private: true, + files: { + 'package.json': true, + 'package-lock.json': false, + 'npm-shrinkwrap.json': false, + 'yarn.lock': false + }, packages: { 'package.json': { greenkeeper: { @@ -313,6 +325,12 @@ describe('create version brach', () => { accountId: '124gke', fullName: 'finnp/testtest', private: true, + files: { + 'package.json': ['package.json'], + 'package-lock.json': [], + 'npm-shrinkwrap.json': [], + 'yarn.lock': [] + }, packages: { 'package.json': { greenkeeper: { @@ -442,6 +460,12 @@ describe('create version brach', () => { accountId: '125', fullName: 'finnp/testtest', private: true, + files: { + 'package.json': ['package.json'], + 'package-lock.json': [], + 'npm-shrinkwrap.json': [], + 'yarn.lock': [] + }, packages: { 'package.json': { greenkeeper: { @@ -517,6 +541,12 @@ describe('create version brach', () => { _id: '43', accountId: '126', fullName: 'finnp/test2', + files: { + 'package.json': ['package.json'], + 'package-lock.json': [], + 'npm-shrinkwrap.json': [], + 'yarn.lock': [] + }, packages: { 'package.json': { greenkeeper: { @@ -621,6 +651,12 @@ describe('create version brach', () => { _id: '44', accountId: '127', fullName: 'finnp/test', + files: { + 'package.json': ['package.json'], + 'package-lock.json': [], + 'npm-shrinkwrap.json': [], + 'yarn.lock': [] + }, packages: { 'package.json': { greenkeeper: { @@ -701,6 +737,12 @@ describe('create version brach', () => { _id: '45', accountId: '123', fullName: 'finnp/test', + files: { + 'package.json': ['package.json'], + 'package-lock.json': [], + 'npm-shrinkwrap.json': [], + 'yarn.lock': [] + }, packages: { 'package.json': { greenkeeper: { @@ -734,6 +776,12 @@ describe('create version brach', () => { _id: '51', accountId: '123', fullName: 'treasure-data/td-js-sdk', + files: { + 'package.json': ['package.json'], + 'package-lock.json': [], + 'npm-shrinkwrap.json': [], + 'yarn.lock': [] + }, packages: { 'package.json': { greenkeeper: { @@ -820,10 +868,10 @@ describe('create version brach', () => { accountId: '2323', fullName: 'espy/test', files: { - 'package.json': true, - 'package-lock.json': false, - 'npm-shrinkwrap.json': true, - 'yarn.lock': false + 'package.json': ['package.json'], + 'package-lock.json': [], + 'npm-shrinkwrap.json': ['npm-shrinkwrap.json'], + 'yarn.lock': [] }, packages: { 'package.json': {} @@ -856,10 +904,10 @@ describe('create version brach', () => { accountId: '2323', fullName: 'espy/test', files: { - 'package.json': true, - 'package-lock.json': true, - 'npm-shrinkwrap.json': false, - 'yarn.lock': false + 'package.json': ['package.json'], + 'package-lock.json': ['package-lock.json'], + 'npm-shrinkwrap.json': [], + 'yarn.lock': [] }, packages: { 'package.json': {} @@ -892,10 +940,10 @@ describe('create version brach', () => { accountId: '2323', fullName: 'espy/test', files: { - 'package.json': true, - 'package-lock.json': true, - 'npm-shrinkwrap.json': false, - 'yarn.lock': false + 'package.json': ['package.json'], + 'package-lock.json': ['package-lock.json'], + 'npm-shrinkwrap.json': [], + 'yarn.lock': [] }, packages: { 'package.json': { @@ -935,10 +983,10 @@ describe('create version brach', () => { accountId: '2323', fullName: 'espy/test', files: { - 'package.json': true, - 'package-lock.json': true, - 'npm-shrinkwrap.json': false, - 'yarn.lock': false + 'package.json': ['package.json'], + 'package-lock.json': ['package-lock.json'], + 'npm-shrinkwrap.json': [], + 'yarn.lock': [] }, packages: { 'package.json': { @@ -948,7 +996,7 @@ describe('create version brach', () => { } } }) - expect.assertions(5) + expect.assertions(4) const githubMock = nock('https://api.github.com') .post('/installations/40/access_tokens') @@ -959,34 +1007,10 @@ describe('create version brach', () => { .get('/rate_limit') .optionally() .reply(200, {}) - .post('/repos/espy/test/pulls') - .reply(200, () => { - // pull request created - expect(true).toBeTruthy() - return { - id: 321, - number: 66, - state: 'open' - } - }) .get('/repos/espy/test') .reply(200, { default_branch: 'master' }) - .post( - '/repos/espy/test/issues/66/labels', - body => body[0] === 'greenkeeper' - ) - .reply(201, () => { - return {} - }) - .post( - '/repos/espy/test/statuses/1234abcd', - ({ state }) => state === 'success' - ) - .reply(201, () => { - return {} - }) jest.mock('../../lib/get-infos', () => () => { return { @@ -1012,26 +1036,100 @@ describe('create version brach', () => { type: 'devDependencies', distTag: 'latest', distTags: { - latest: '2.0.0' + latest: '1.1.0' }, oldVersion: '^1.0.0', oldVersionResolved: '1.0.0', versions: { '1.0.0': {}, - '2.0.0': {} + '1.1.0': {} } }) - githubMock.done() // no new job scheduled expect(newJob).toBeFalsy() const branch = await repositories.get('50:branch:1234abcd') - const pr = await repositories.get('50:pr:321') + expect(branch).toBeTruthy() + await expect(repositories.get('50:pr:321')).rejects.toThrow('missing') + expect(githubMock.isDone()).toBeTruthy() + }) - expect(branch.processed).toBeTruthy() + test('runs if in range, has project lockfile, has gk-lockfile with old files object format', async () => { + const { repositories } = await dbs() + await repositories.put({ + _id: '86', + accountId: '2323', + fullName: 'johnlocke/test', + files: { + 'package.json': true, + 'package-lock.json': true, + 'npm-shrinkwrap.json': false, + 'yarn.lock': false + }, + packages: { + 'package.json': { + devDependencies: { + 'greenkeeper-lockfile': '1.1.1' + } + } + } + }) + expect.assertions(4) - expect(pr.number).toBe(66) - expect(pr.state).toEqual('open') + const githubMock = nock('https://api.github.com') + .post('/installations/40/access_tokens') + .optionally() + .reply(200, { + token: 'secret' + }) + .get('/rate_limit') + .optionally() + .reply(200, {}) + .get('/repos/johnlocke/test') + .reply(200, { + default_branch: 'master' + }) + + jest.mock('../../lib/get-infos', () => () => { + return { + dependencyLink: '[]()', + release: 'the release', + diffCommits: 'commits...' + } + }) + + jest.mock('../../lib/get-diff-commits', () => () => ({ + html_url: 'https://github.com/lkjlsgfj/', + total_commits: 0, + behind_by: 0, + commits: [] + })) + jest.mock('../../lib/create-branch', () => ({ transform }) => '1234abcd') + const createVersionBranch = require('../../jobs/create-version-branch') + + const newJob = await createVersionBranch({ + dependency: '@finnpauls/dep', + accountId: '2323', + repositoryId: '86', + type: 'devDependencies', + distTag: 'latest', + distTags: { + latest: '1.1.0' + }, + oldVersion: '^1.0.0', + oldVersionResolved: '1.0.0', + versions: { + '1.0.0': {}, + '1.1.0': {} + } + }) + + // no new job scheduled + expect(newJob).toBeFalsy() + const branch = await repositories.get('86:branch:1234abcd') + expect(branch).toBeTruthy() + await expect(repositories.get('86:pr:321')).rejects.toThrow('missing') + expect(githubMock.isDone()).toBeTruthy() }) }) @@ -1041,7 +1139,7 @@ afterAll(async () => { await Promise.all([ removeIfExists(installations, '123', '124', '124gke', '125', '126', '127', '2323'), removeIfExists(payments, '124', '125'), - removeIfExists(repositories, '41', '42', '43', '44', '45', '46', '47', '48', '49', '50'), - removeIfExists(repositories, '41:branch:1234abcd', '41:pr:321', '42:branch:1234abcd', '43:branch:1234abcd', '50:branch:1234abcd', '50:pr:321') + removeIfExists(repositories, '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '86'), + removeIfExists(repositories, '41:branch:1234abcd', '41:pr:321', '42:branch:1234abcd', '43:branch:1234abcd', '50:branch:1234abcd', '50:pr:321', '86:branch:1234abcd') ]) })