Skip to content

Commit

Permalink
fix(create-version-branch): adapt to new structure of files object
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Realtin authored May 9, 2018
1 parent 018c847 commit 852ce6e
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 55 deletions.
14 changes: 9 additions & 5 deletions jobs/create-version-branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)')
Expand Down
198 changes: 148 additions & 50 deletions test/jobs/create-version-branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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': {}
Expand Down Expand Up @@ -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': {}
Expand Down Expand Up @@ -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': {
Expand Down Expand Up @@ -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': {
Expand All @@ -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')
Expand All @@ -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 {
Expand All @@ -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()
})
})

Expand All @@ -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')
])
})

0 comments on commit 852ce6e

Please sign in to comment.