-
Notifications
You must be signed in to change notification settings - Fork 934
test(travis-cli): migrate tests from ava to jest #910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
dc7faab
c9e4c8c
1449c6b
53ac530
db76163
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
#!/usr/bin/env node | ||
console.log(process.argv); | ||
|
||
const args = process.argv; | ||
args.shift(); // remove node | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't want to output node as arg as its just making issues, and it let me remove |
||
console.log( | ||
args.map((item, index) => { | ||
return index === 0 ? 'commitlint' : item; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this solves command test result being system depended |
||
}) | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
#!/usr/bin/env node | ||
console.log(process.argv); | ||
|
||
const args = process.argv; | ||
args.shift(); // remove node | ||
console.log( | ||
args.map((item, index) => { | ||
return index === 0 ? 'git' : item; | ||
}) | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,7 @@ async function lint(args, options) { | |
} | ||
|
||
async function log(hash) { | ||
const result = await execa('git', [ | ||
const result = await execa(GIT, [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while doing tests, i found this bug, 'git' command was called directly instead of GIT variable // Allow to override used bins for testing purposes
const GIT = process.env.TRAVIS_COMMITLINT_GIT_BIN || 'git'; |
||
'log', | ||
'-n', | ||
'1', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,218 +1,114 @@ | ||
// Disable ftb | ||
// const os = require('os'); | ||
// const {git} = require('@commitlint/test'); | ||
import test from 'ava'; | ||
import execa from 'execa'; | ||
// Disable ftb | ||
// const which = require('which'); | ||
|
||
// Disable ftb | ||
// const NODE_BIN = which.sync('node'); | ||
const BIN = require.resolve('../lib/cli.js'); | ||
|
||
// Disable ftb | ||
// const TRAVIS_COMMITLINT_BIN = require.resolve('../fixtures/commitlint'); | ||
// const TRAVIS_COMMITLINT_GIT_BIN = require.resolve('../fixtures/git'); | ||
// const TRAVIS_BRANCH = 'TRAVIS_BRANCH'; | ||
// const TRAVIS_COMMIT = 'TRAVIS_COMMIT'; | ||
import {git} from '@commitlint/test'; | ||
|
||
const bin = require.resolve('../lib/cli.js'); | ||
|
||
const TRAVIS_COMMITLINT_BIN = require.resolve('../fixtures/commitlint'); | ||
const TRAVIS_COMMITLINT_GIT_BIN = require.resolve('../fixtures/git'); | ||
|
||
const validBaseEnv = { | ||
TRAVIS: true, | ||
CI: true, | ||
TRAVIS_COMMIT: 'TRAVIS_COMMIT', | ||
TRAVIS_COMMITLINT_BIN: TRAVIS_COMMITLINT_BIN, | ||
TRAVIS_COMMITLINT_GIT_BIN: TRAVIS_COMMITLINT_GIT_BIN, | ||
TRAVIS_COMMIT_RANGE: 'TRAVIS_COMMIT_A.TRAVIS_COMMIT_B', | ||
TRAVIS_EVENT_TYPE: 'TRAVIS_EVENT_TYPE', | ||
TRAVIS_REPO_SLUG: 'TRAVIS_REPO_SLUG', | ||
TRAVIS_PULL_REQUEST_SLUG: 'TRAVIS_PULL_REQUEST_SLUG' | ||
}; | ||
|
||
const bin = async (config = {}) => { | ||
const cli = async (config = {}) => { | ||
try { | ||
return await execa(BIN, Object.assign({extendEnv: false}, config)); | ||
return await execa(bin, [], config); | ||
} catch (err) { | ||
throw new Error([err.stdout, err.stderr].join('\n')); | ||
return Promise.reject([err.stdout, err.stderr].join('\n')); | ||
} | ||
}; | ||
|
||
test('should throw when not on travis ci', async t => { | ||
test('should throw when not on travis ci', async () => { | ||
const env = { | ||
CI: false, | ||
TRAVIS: false | ||
}; | ||
|
||
await t.throwsAsync( | ||
bin({env}), | ||
/@commitlint\/travis-cli is intended to be used on Travis CI/ | ||
await expect(cli({env})).rejects.toContain( | ||
'@commitlint/travis-cli is intended to be used on Travis CI' | ||
); | ||
}); | ||
|
||
/* Test.failing( | ||
'should throw when on travis ci, but env vars are missing', | ||
async t => { | ||
const env = { | ||
TRAVIS: true, | ||
CI: true | ||
}; | ||
|
||
await t.throwsAsync(bin({env}), /TRAVIS_COMMIT, TRAVIS_BRANCH/); | ||
} | ||
); */ | ||
|
||
test('should throw when on travis ci, but TRAVIS_COMMIT is missing', async t => { | ||
test('should throw when on travis ci, but env vars are missing', async () => { | ||
const env = { | ||
TRAVIS: true, | ||
CI: true | ||
}; | ||
|
||
await t.throwsAsync(bin({env}), /TRAVIS_COMMIT/); | ||
await expect(cli({env})).rejects.toContain( | ||
'TRAVIS_COMMIT, TRAVIS_COMMIT_RANGE, TRAVIS_EVENT_TYPE, TRAVIS_REPO_SLUG, TRAVIS_PULL_REQUEST_SLUG' | ||
); | ||
}); | ||
|
||
/* Test.failing( | ||
'should throw when on travis ci, but TRAVIS_BRANCH is missing', | ||
async t => { | ||
const env = { | ||
TRAVIS: true, | ||
CI: true | ||
}; | ||
test('should call git with expected args', async () => { | ||
const cwd = await git.clone( | ||
'https://github.com/conventional-changelog/commitlint.git', | ||
['--depth=10'], | ||
__dirname, | ||
TRAVIS_COMMITLINT_GIT_BIN | ||
); | ||
|
||
await t.throwsAsync(bin({env}), /TRAVIS_BRANCH/); | ||
} | ||
); | ||
const result = await cli({ | ||
cwd, | ||
env: validBaseEnv | ||
}); | ||
const invocations = await getInvocations(result.stdout); | ||
expect(invocations.length).toBe(3); | ||
|
||
test.failing('should call git with expected args on shallow repo', async t => { | ||
if (os.platform() === 'win32') { | ||
t.pass(); | ||
return; | ||
} | ||
const [stash, branches, commilint] = invocations; | ||
|
||
const cwd = await git.clone('https://github.com/conventional-changelog/commitlint.git', [ | ||
'--depth=10' | ||
]); | ||
expect(stash).toEqual(['git', 'stash', '-k', '-u', '--quiet']); | ||
expect(branches).toEqual(['git', 'stash', 'pop', '--quiet']); | ||
expect(commilint).toEqual(['commitlint']); | ||
}); | ||
|
||
const env = { | ||
TRAVIS: true, | ||
CI: true, | ||
TRAVIS_BRANCH, | ||
TRAVIS_COMMIT, | ||
TRAVIS_COMMITLINT_BIN, | ||
test('should call git with expected args on pull_request', async () => { | ||
const cwd = await git.clone( | ||
'https://github.com/conventional-changelog/commitlint.git', | ||
['--depth=10'], | ||
__dirname, | ||
TRAVIS_COMMITLINT_GIT_BIN | ||
}; | ||
); | ||
|
||
const result = await bin({cwd, env}); | ||
const result = await cli({ | ||
cwd, | ||
env: {...validBaseEnv, TRAVIS_EVENT_TYPE: 'pull_request'} | ||
}); | ||
const invocations = await getInvocations(result.stdout); | ||
t.is(invocations.length, 7); | ||
|
||
const [ | ||
stash, | ||
branches, | ||
unshallow, | ||
checkout, | ||
back, | ||
pop, | ||
commilint | ||
] = invocations; | ||
|
||
t.deepEqual(stash, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash']); | ||
t.deepEqual(branches, [ | ||
NODE_BIN, | ||
TRAVIS_COMMITLINT_GIT_BIN, | ||
'remote', | ||
'set-branches', | ||
'origin', | ||
TRAVIS_BRANCH | ||
]); | ||
t.deepEqual(unshallow, [ | ||
NODE_BIN, | ||
TRAVIS_COMMITLINT_GIT_BIN, | ||
'fetch', | ||
'--unshallow', | ||
'--quiet' | ||
]); | ||
t.deepEqual(checkout, [ | ||
NODE_BIN, | ||
TRAVIS_COMMITLINT_GIT_BIN, | ||
'checkout', | ||
TRAVIS_BRANCH, | ||
'--quiet' | ||
]); | ||
t.deepEqual(back, [ | ||
NODE_BIN, | ||
TRAVIS_COMMITLINT_GIT_BIN, | ||
'checkout', | ||
'-', | ||
'--quiet' | ||
]); | ||
t.deepEqual(pop, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash', 'pop']); | ||
t.deepEqual(commilint, [ | ||
NODE_BIN, | ||
TRAVIS_COMMITLINT_BIN, | ||
expect(invocations.length).toBe(3); | ||
|
||
const [stash, branches, commilint] = invocations; | ||
|
||
expect(stash).toEqual(['git', 'stash', '-k', '-u', '--quiet']); | ||
expect(branches).toEqual(['git', 'stash', 'pop', '--quiet']); | ||
expect(commilint).toEqual([ | ||
'commitlint', | ||
'--from', | ||
TRAVIS_BRANCH, | ||
'TRAVIS_COMMIT_A', | ||
'--to', | ||
TRAVIS_COMMIT | ||
'TRAVIS_COMMIT_B' | ||
]); | ||
}); | ||
|
||
test.failing( | ||
'should call git with expected args on unshallow repo', | ||
async t => { | ||
if (os.platform() === 'win32') { | ||
t.pass(); | ||
return; | ||
} | ||
|
||
const cwd = await git.clone('https://github.com/conventional-changelog/commitlint.git'); | ||
|
||
const env = { | ||
TRAVIS: true, | ||
CI: true, | ||
TRAVIS_BRANCH, | ||
TRAVIS_COMMIT, | ||
TRAVIS_COMMITLINT_BIN, | ||
TRAVIS_COMMITLINT_GIT_BIN | ||
}; | ||
|
||
const result = await bin({cwd, env}); | ||
const invocations = await getInvocations(result.stdout); | ||
t.is(invocations.length, 6); | ||
|
||
const [stash, branches, checkout, back, pop, commilint] = invocations; | ||
|
||
t.deepEqual(stash, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash']); | ||
t.deepEqual(branches, [ | ||
NODE_BIN, | ||
TRAVIS_COMMITLINT_GIT_BIN, | ||
'remote', | ||
'set-branches', | ||
'origin', | ||
TRAVIS_BRANCH | ||
]); | ||
t.deepEqual(checkout, [ | ||
NODE_BIN, | ||
TRAVIS_COMMITLINT_GIT_BIN, | ||
'checkout', | ||
TRAVIS_BRANCH, | ||
'--quiet' | ||
]); | ||
t.deepEqual(back, [ | ||
NODE_BIN, | ||
TRAVIS_COMMITLINT_GIT_BIN, | ||
'checkout', | ||
'-', | ||
'--quiet' | ||
]); | ||
t.deepEqual(pop, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash', 'pop']); | ||
t.deepEqual(commilint, [ | ||
NODE_BIN, | ||
TRAVIS_COMMITLINT_BIN, | ||
'--from', | ||
TRAVIS_BRANCH, | ||
'--to', | ||
TRAVIS_COMMIT | ||
]); | ||
} | ||
); | ||
|
||
function getInvocations(stdout) { | ||
const matches = stdout.match(/[^[\]]+/g); | ||
const raw = Array.isArray(matches) ? matches : []; | ||
|
||
return raw.filter(invocation => invocation !== '\n').map(invocation => | ||
invocation | ||
.split(',') | ||
.map(fragment => fragment.trim()) | ||
.map(fragment => fragment.substring(1, fragment.length - 1)) | ||
.filter(Boolean) | ||
); | ||
return raw | ||
.filter(invocation => invocation !== '\n') | ||
.map(invocation => | ||
invocation | ||
.split(',') | ||
.map(fragment => fragment.trim()) | ||
.map(fragment => fragment.substring(1, fragment.length - 1)) | ||
.filter(Boolean) | ||
); | ||
} | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
small update here, this is a little cleaner way to get correct path for all env