diff --git a/lib/clean.js b/lib/clean.js index 72dbb51..d063276 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -24,7 +24,8 @@ module.exports = async ({ dependents }, { all, dryRun }) => { let branches - const branch = await context.getTestingBranchName(parentPkgJSON.name) + const parentBranchName = await context.getParentBranchName() + const branch = await context.getTestingBranchName(parentBranchName) if (all) { branches = await github.getWibyBranches(dependentPkgInfo.owner, dependentPkgInfo.name) diff --git a/lib/context.js b/lib/context.js index 59d8a5e..a815dc2 100644 --- a/lib/context.js +++ b/lib/context.js @@ -1,9 +1,20 @@ 'use strict' +const simpleGit = require('simple-git') const fsPromises = require('fs').promises -exports.getTestingBranchName = async function getTestingBranchName (parentPackageName) { - return `wiby-${parentPackageName}` +exports.getParentBranchName = async function getParentBranchName () { + const { current } = await simpleGit().branch() + + return current +} + +exports.getTestingBranchName = function getTestingBranchName (parentBranchName) { + return `wiby-${parentBranchName}` +} + +exports.getDependencyLink = async function getDependencyLink (owner, repo, commitish) { + return `${owner}/${repo}#${commitish}` } exports.getLocalPackageJSON = async function getLocalPackageJSON () { @@ -15,7 +26,7 @@ exports.getLocalPackageJSON = async function getLocalPackageJSON () { The check should search both the dev and peer dependencies to find out if the dependency is regular, dev or peer. */ -exports.checkPackageInPackageJSON = function checkPackageInPackageJSON (dep, packageJSON) { +exports.checkDependentUsesParent = function checkDependentUsesParent (dep, packageJSON) { const dependencyKeyNames = [ 'dependencies', 'devDependencies', diff --git a/lib/result.js b/lib/result.js index 7354793..93ce1d4 100644 --- a/lib/result.js +++ b/lib/result.js @@ -30,11 +30,12 @@ module.exports = async function ({ dependents }) { const dependentPkgJSON = await github.getPackageJson(dependentPkgInfo.owner, dependentPkgInfo.name) debug(`Dependent module: ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`) - if (!context.checkPackageInPackageJSON(parentPkgJSON.name, dependentPkgJSON)) { + if (!context.checkDependentUsesParent(parentPkgJSON.name, dependentPkgJSON)) { throw new Error(`${parentPkgInfo.owner}/${parentPkgJSON.name} not found in the package.json of ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`) } - const branch = await context.getTestingBranchName(parentPkgJSON.name) + const parentBranchName = await context.getParentBranchName() + const branch = await context.getTestingBranchName(parentBranchName) let resp = await github.getChecks(dependentPkgInfo.owner, dependentPkgInfo.name, branch) if (resp.data.check_runs.length === 0) { resp = await github.getCommitStatusesForRef(dependentPkgInfo.owner, dependentPkgInfo.name, branch) diff --git a/lib/test.js b/lib/test.js index c02861b..e00f594 100644 --- a/lib/test.js +++ b/lib/test.js @@ -14,41 +14,30 @@ module.exports = async function ({ dependents }) { logger.enableLogs(testCommandNamespace) const parentPkgJSON = await context.getLocalPackageJSON() - const parentPkgInfo = gitURLParse(parentPkgJSON.repository.url) - debug(`Parent module: ${parentPkgInfo.owner}/${parentPkgJSON.name}`) + const parentRepositoryInfo = gitURLParse(parentPkgJSON.repository.url) + const parentBranchName = await context.getParentBranchName() + debug(`Parent module: ${parentRepositoryInfo.owner}/${parentPkgJSON.name}`) - const commitURL = await getCommitURL(parentPkgInfo.owner, parentPkgInfo.name) - debug('Commit URL to test:', commitURL) + const parentDependencyLink = await context.getDependencyLink(parentRepositoryInfo.owner, parentRepositoryInfo.name, parentBranchName) + debug('Commit URL to test:', parentDependencyLink) for (const { repository: url } of dependents) { - const dependentPkgInfo = gitURLParse(url) - const dependentPkgJSON = await github.getPackageJson(dependentPkgInfo.owner, dependentPkgInfo.name) - debug(`Dependent module: ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`) + const dependentRepositoryInfo = gitURLParse(url) + const dependentPkgJson = await github.getPackageJson(dependentRepositoryInfo.owner, dependentRepositoryInfo.name) + debug(`Dependent module: ${dependentRepositoryInfo.owner}/${dependentRepositoryInfo.name}`) - if (!context.checkPackageInPackageJSON(parentPkgJSON.name, dependentPkgJSON)) { - throw new Error(`${parentPkgInfo.owner}/${parentPkgJSON.name} not found in the package.json of ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`) + if (!context.checkDependentUsesParent(parentPkgJSON.name, dependentPkgJson)) { + throw new Error(`${parentRepositoryInfo.owner}/${parentPkgJSON.name} not found in the package.json of ${dependentRepositoryInfo.owner}/${dependentRepositoryInfo.name}`) } - const patchedPackageJSON = applyPatch(commitURL, parentPkgJSON.name, dependentPkgJSON) - await pushPatch(patchedPackageJSON, dependentPkgInfo.owner, dependentPkgInfo.name, parentPkgJSON.name) + const patchedPackageJSON = applyPatch(parentDependencyLink, parentPkgJSON.name, dependentPkgJson, parentPkgJSON.name) + await pushPatch(patchedPackageJSON, dependentRepositoryInfo.owner, dependentRepositoryInfo.name, parentPkgJSON.name, parentBranchName) } } -const getCommitHash = async function getCommitHash (owner, repo) { - const [headSha] = await github.getShas(owner, repo) - return headSha -} - -const getCommitURL = async function getCommitURL (owner, repo, hash) { - hash = hash || await getCommitHash(owner, repo) - - const patch = `${owner}/${repo}#${hash}` - return patch -} - const applyPatch = module.exports.applyPatch = function applyPatch (patch, module, dependentPkgJSON) { - const dependencyType = context.checkPackageInPackageJSON(module, dependentPkgJSON) + const dependencyType = context.checkDependentUsesParent(module, dependentPkgJSON) if (!dependencyType) { throw new Error('Dependency not found in package.json') } @@ -56,16 +45,16 @@ function applyPatch (patch, module, dependentPkgJSON) { return dependentPkgJSON } -async function pushPatch (packageJSON, owner, repo, dep) { - const file = JSON.stringify(packageJSON, null, 2) + '\n' // assumes package.json is using two spaces +async function pushPatch (dependentPkgJson, dependentOwner, dependentRepo, parentName, parentBranchName) { + const file = JSON.stringify(dependentPkgJson, null, 2) + '\n' // assumes package.json is using two spaces const encodedFile = Buffer.from(file).toString('base64') - const message = `wiby: update ${dep}` - const branch = await context.getTestingBranchName(dep) - - const [headSha, treeSha] = await github.getShas(owner, repo) - const blobSha = await github.createBlob(owner, repo, encodedFile) - const newTreeSha = await github.createTree(owner, repo, treeSha, blobSha) - const commitSha = await github.createCommit(owner, repo, message, newTreeSha, headSha) - await github.createBranch(owner, repo, commitSha, branch) - debug(`Changes pushed to https://github.com/${owner}/${repo}/blob/${branch}/package.json`) + const message = `wiby: update ${parentName}` + const branch = await context.getTestingBranchName(parentBranchName) + + const [headSha, treeSha] = await github.getShas(dependentOwner, dependentRepo) + const blobSha = await github.createBlob(dependentOwner, dependentRepo, encodedFile) + const newTreeSha = await github.createTree(dependentOwner, dependentRepo, treeSha, blobSha) + const commitSha = await github.createCommit(dependentOwner, dependentRepo, message, newTreeSha, headSha) + await github.createBranch(dependentOwner, dependentRepo, commitSha, branch) + debug(`Changes pushed to https://github.com/${dependentOwner}/${dependentRepo}/blob/${branch}/package.json`) } diff --git a/package.json b/package.json index cfaca88..4b9be58 100644 --- a/package.json +++ b/package.json @@ -37,12 +37,14 @@ "git-url-parse": "^11.1.2", "joi": "^17.2.1", "node-fetch": "^2.6.0", + "simple-git": "^2.35.1", "yargs": "^16.0.0" }, "devDependencies": { "nock": "^13.0.3", "standard": "^16.0.0", - "tap": "^14.10.7" + "tap": "^14.10.7", + "tmp": "^0.2.1" }, "standard": { "ignore": [ diff --git a/test/clean.js b/test/clean.js index 8580023..b63a2bc 100644 --- a/test/clean.js +++ b/test/clean.js @@ -3,32 +3,36 @@ require('dotenv').config() const tap = require('tap') const nock = require('nock') + const CONFIG = require('./fixtures/config') +const gitFixture = require('./fixtures/git') + const wiby = require('..') -tap.beforeEach(async () => { - nock.disableNetConnect() -}) +tap.test('wiby.clean()', async (tap) => { + tap.beforeEach(async () => { + nock.disableNetConnect() + gitFixture.init() + }) -tap.afterEach(async () => { - nock.cleanAll() - nock.enableNetConnect() -}) + tap.afterEach(async () => { + nock.cleanAll() + nock.enableNetConnect() + }) -tap.test('wiby.clean()', async (tap) => { tap.test('should check if the wiby branch exists', async (tap) => { nock('https://api.github.com') - .get(`/repos/wiby-test/${CONFIG.DEP_REPO}/branches/wiby-wiby`) + .get(`/repos/wiby-test/${CONFIG.DEP_REPO}/branches/wiby-running-unit-tests`) .reply(404) await wiby.clean({ dependents: [{ repository: `https://www.github.com/${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}` }] }, {}) - // implied assertion - no DELETE requests expected - we don't need to delete the missing `wiby-wiby` branch + // implied assertion - no DELETE requests expected - we don't need to delete the missing `wiby-running-unit-tests` branch }) tap.test('should rethrow when github API inaccessible during branch check', async (tap) => { nock('https://api.github.com') - .get(`/repos/wiby-test/${CONFIG.DEP_REPO}/branches/wiby-wiby`) + .get(`/repos/wiby-test/${CONFIG.DEP_REPO}/branches/wiby-running-unit-tests`) .reply(500) await tap.rejects( diff --git a/test/cli/clean.js b/test/cli/clean.js index 2538d80..869eb36 100644 --- a/test/cli/clean.js +++ b/test/cli/clean.js @@ -4,39 +4,44 @@ const tap = require('tap') const childProcess = require('child_process') const path = require('path') -const cwd = path.join(__dirname, '..', '..') -const wibyCommand = path.join(cwd, 'bin', 'wiby') +const gitFixture = require('../fixtures/git') + +const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby') const fixturesPath = path.resolve(path.join(__dirname, '..', 'fixtures')) tap.test('clean command', async (tap) => { + tap.beforeEach(async () => { + gitFixture.init() + }) + tap.test('should delete test branch in all configured test modules', async (tap) => { const result = childProcess.execSync(`${wibyCommand} clean`, { - cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/clean-command.js` } }).toString() tap.includes(result, 'Branches deleted:') - tap.includes(result, '- https://github.com/wiby-test/partial: wiby-wiby') - tap.includes(result, '- git://github.com/wiby-test/fail: wiby-wiby') - tap.includes(result, '- git+https://github.com/wiby-test/pass: wiby-wiby') + tap.includes(result, '- https://github.com/wiby-test/partial: wiby-running-unit-tests') + tap.includes(result, '- git://github.com/wiby-test/fail: wiby-running-unit-tests') + tap.includes(result, '- git+https://github.com/wiby-test/pass: wiby-running-unit-tests') }) tap.test('should delete test branch in the test module at dependent URI', async (tap) => { const result = childProcess.execSync(`${wibyCommand} clean --dependent="https://github.com/wiby-test/fakeRepo"`, { - cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/clean-command.js` } }).toString() tap.includes(result, 'Branches deleted:') - tap.includes(result, '- https://github.com/wiby-test/fakeRepo: wiby-wiby') + tap.includes(result, '- https://github.com/wiby-test/fakeRepo: wiby-running-unit-tests') }) tap.test('should delete all wiby-* branches in all configured test modules', async (tap) => { const result = childProcess.execSync(`${wibyCommand} clean --all`, { - cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/clean-command-all.js` } }).toString() @@ -48,15 +53,15 @@ tap.test('clean command', async (tap) => { tap.test('should not delete during dry-run', async (tap) => { const result = childProcess.execSync(`${wibyCommand} clean --dry-run`, { - cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/clean-command-dry.js` } }).toString() tap.includes(result, 'Branches to be deleted:') - tap.includes(result, '- https://github.com/wiby-test/partial: wiby-wiby') - tap.includes(result, '- git://github.com/wiby-test/fail: wiby-wiby') - tap.includes(result, '- git+https://github.com/wiby-test/pass: wiby-wiby') + tap.includes(result, '- https://github.com/wiby-test/partial: wiby-running-unit-tests') + tap.includes(result, '- git://github.com/wiby-test/fail: wiby-running-unit-tests') + tap.includes(result, '- git+https://github.com/wiby-test/pass: wiby-running-unit-tests') }) }) diff --git a/test/cli/result.js b/test/cli/result.js index 5ce4ab7..8afabb7 100644 --- a/test/cli/result.js +++ b/test/cli/result.js @@ -5,16 +5,21 @@ const childProcess = require('child_process') const path = require('path') const fs = require('fs') -const cwd = path.join(__dirname, '..', '..') -const wibyCommand = path.join(cwd, 'bin', 'wiby') +const gitFixture = require('../fixtures/git') + +const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby') const fixturesPath = path.resolve(path.join(__dirname, '..', 'fixtures')) const PENDING_RESULT_EXIT_CODE = 64 tap.test('result command', async (tap) => { + tap.beforeEach(async () => { + gitFixture.init() + }) + tap.test('result command should fail when config and dependent provided', async (tap) => { try { - childProcess.execSync(`${wibyCommand} result --config=.wiby.json --dependent="https://github.com/wiby-test/fakeRepo"`, { cwd: cwd }).toString() + childProcess.execSync(`${wibyCommand} result --config=.wiby.json --dependent="https://github.com/wiby-test/fakeRepo"`).toString() tap.fail() } catch (err) { tap.equal(true, err.message.includes('Arguments dependent and config are mutually exclusive')) @@ -30,8 +35,8 @@ tap.test('result command', async (tap) => { try { childProcess.execSync(`${wibyCommand} result --dependent="https://github.com/wiby-test/fakeRepo"`, { - cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive.js` } }) @@ -52,8 +57,8 @@ tap.test('result command', async (tap) => { try { childProcess.execSync(`${wibyCommand} result`, { - cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive.js` } }) @@ -65,7 +70,7 @@ tap.test('result command', async (tap) => { } }) - tap.test('result command handles empty response from github.getChecks()', tap => { + tap.test('result command handles empty response from github.getChecks()', (tap) => { const expected = fs.readFileSync( path.join(__dirname, '..', 'fixtures', 'expected-outputs', 'result', 'result-output-single-dependant.md'), 'utf-8' @@ -74,8 +79,8 @@ tap.test('result command', async (tap) => { try { childProcess.execSync(`${wibyCommand} result --dependent="https://github.com/wiby-test/fakeRepo"`, { - cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-empty-branch-checks.js` } }) @@ -98,8 +103,8 @@ tap.test('result command', async (tap) => { try { childProcess.execSync(`${wibyCommand} result --dependent="https://github.com/wiby-test/fakeRepo"`, { - cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive-checks-failed.js` } }) diff --git a/test/cli/test.js b/test/cli/test.js index 915251f..c193be3 100644 --- a/test/cli/test.js +++ b/test/cli/test.js @@ -4,14 +4,19 @@ const tap = require('tap') const childProcess = require('child_process') const path = require('path') -const cwd = path.join(__dirname, '..', '..') -const wibyCommand = path.join(cwd, 'bin', 'wiby') +const gitFixture = require('../fixtures/git') + +const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby') const fixturesPath = path.resolve(path.join(__dirname, '..', 'fixtures')) tap.test('test command', async (tap) => { + tap.beforeEach(async () => { + gitFixture.init() + }) + tap.test('test command should fail when config and dependent provided', async (tap) => { try { - childProcess.execSync(`${wibyCommand} test --config=.wiby.json --dependent="https://github.com/wiby-test/fakeRepo"`, { cwd: cwd }).toString() + childProcess.execSync(`${wibyCommand} test --config=.wiby.json --dependent="https://github.com/wiby-test/fakeRepo"`).toString() tap.fail() } catch (err) { tap.equal(true, err.message.includes('Arguments dependent and config are mutually exclusive')) @@ -20,25 +25,25 @@ tap.test('test command', async (tap) => { tap.test('test command should call test module with dependent URI', async (tap) => { const result = childProcess.execSync(`${wibyCommand} test --dependent="https://github.com/wiby-test/fakeRepo"`, { - cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/test-command-positive.js` } }).toString() - tap.includes(result, 'Changes pushed to https://github.com/wiby-test/fakeRepo/blob/wiby-wiby/package.json') + tap.includes(result, 'Changes pushed to https://github.com/wiby-test/fakeRepo/blob/wiby-running-unit-tests/package.json') }) tap.test('test command should call test module with all deps from .wiby.json', async (tap) => { const result = childProcess.execSync(`${wibyCommand} test`, { - cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/test-command-positive.js` } }).toString() - tap.includes(result, 'Changes pushed to https://github.com/wiby-test/pass/blob/wiby-wiby/package.json') - tap.includes(result, 'Changes pushed to https://github.com/wiby-test/fail/blob/wiby-wiby/package.json') - tap.includes(result, 'Changes pushed to https://github.com/wiby-test/partial/blob/wiby-wiby/package.json') + tap.includes(result, 'Changes pushed to https://github.com/wiby-test/pass/blob/wiby-running-unit-tests/package.json') + tap.includes(result, 'Changes pushed to https://github.com/wiby-test/fail/blob/wiby-running-unit-tests/package.json') + tap.includes(result, 'Changes pushed to https://github.com/wiby-test/partial/blob/wiby-running-unit-tests/package.json') }) }) diff --git a/test/cli/validate.js b/test/cli/validate.js index 923a2d0..29d8db7 100644 --- a/test/cli/validate.js +++ b/test/cli/validate.js @@ -4,12 +4,17 @@ const tap = require('tap') const childProcess = require('child_process') const path = require('path') -const cwd = path.join(__dirname, '..', '..') -const wibyCommand = path.join(cwd, 'bin', 'wiby') +const gitFixture = require('../fixtures/git') + +const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby') tap.test('validate command', async (tap) => { + tap.beforeEach(async () => { + gitFixture.init() + }) + tap.test('should pass on wiby itself', async (tap) => { - childProcess.execSync(`${wibyCommand} validate`, { cwd: cwd }) + childProcess.execSync(`${wibyCommand} validate`) tap.end() }) }) diff --git a/test/config.js b/test/config.js index e50d40a..34208d7 100644 --- a/test/config.js +++ b/test/config.js @@ -2,6 +2,9 @@ const path = require('path') const tap = require('tap') + +const gitFixture = require('./fixtures/git') + const wiby = require('..') const invalidConfigs = { @@ -17,40 +20,46 @@ const validConfigs = [ 'pass-wiby.js' ] -tap.test('should pass with the .wiby.json of the wiby itself', (tap) => { - const normalizedConfig = wiby.validate({}) - tap.strictSame(normalizedConfig, { - dependents: [ - { - repository: 'https://github.com/wiby-test/partial' - }, - { - repository: 'git://github.com/wiby-test/fail' - }, - { - repository: 'git+https://github.com/wiby-test/pass' - } - ] +tap.test('config validation', async (tap) => { + tap.beforeEach(async () => { + gitFixture.init() }) - tap.end() -}) -tap.test('Invalid configs', async (tap) => { - for (const [file, expectedError] of Object.entries(invalidConfigs)) { - tap.test(file, (tap) => { - tap.throws(() => { - wiby.validate({ config: path.join(__dirname, 'fixtures', 'config', file) }) - }, { message: expectedError }) - tap.end() + tap.test('should pass with the .wiby.json of the wiby itself', (tap) => { + const normalizedConfig = wiby.validate({}) + tap.strictSame(normalizedConfig, { + dependents: [ + { + repository: 'https://github.com/wiby-test/partial' + }, + { + repository: 'git://github.com/wiby-test/fail' + }, + { + repository: 'git+https://github.com/wiby-test/pass' + } + ] }) - } -}) + tap.end() + }) -tap.test('Valid configs', async (tap) => { - for (const file of validConfigs) { - tap.test(file, (tap) => { - wiby.validate({ config: path.join(__dirname, 'fixtures', 'config', file) }) - tap.end() - }) - } + tap.test('Invalid configs', async (tap) => { + for (const [file, expectedError] of Object.entries(invalidConfigs)) { + tap.test(file, (tap) => { + tap.throws(() => { + wiby.validate({ config: path.join(__dirname, 'fixtures', 'config', file) }) + }, { message: expectedError }) + tap.end() + }) + } + }) + + tap.test('Valid configs', async (tap) => { + for (const file of validConfigs) { + tap.test(file, (tap) => { + wiby.validate({ config: path.join(__dirname, 'fixtures', 'config', file) }) + tap.end() + }) + } + }) }) diff --git a/test/fixtures/git.js b/test/fixtures/git.js new file mode 100644 index 0000000..cbea4fb --- /dev/null +++ b/test/fixtures/git.js @@ -0,0 +1,28 @@ +'use strict' + +const childProcess = require('child_process') +const fs = require('fs') +const path = require('path') +const tmp = require('tmp') + +exports.TEST_BRANCH_NAME = 'running-unit-tests' + +exports.init = function () { + const gitRepoPath = path.join(__dirname, '..', '..') + + const { name: tmpDir } = tmp.dirSync() + process.chdir(tmpDir) + + childProcess.execSync('git init --initial-branch=running-unit-tests') + childProcess.execSync('git config user.email "wiby@example.com"') + childProcess.execSync('git config user.name "Wiby Bot"') + + for (const fn of ['package.json', '.wiby.json']) { + fs.copyFileSync(path.join(gitRepoPath, fn), path.join(tmpDir, fn)) + } + + childProcess.execSync('git add .') + childProcess.execSync('git commit -m "test" --no-gpg-sign --no-verify') + + return tmpDir +} diff --git a/test/fixtures/http/clean-command-dry.js b/test/fixtures/http/clean-command-dry.js index 3e66a37..7d2b1dd 100644 --- a/test/fixtures/http/clean-command-dry.js +++ b/test/fixtures/http/clean-command-dry.js @@ -9,7 +9,7 @@ nock.disableNetConnect() function nockRepo (nockInstance, repo) { return nockInstance - .get(`/repos/wiby-test/${repo}/branches/wiby-wiby`) + .get(`/repos/wiby-test/${repo}/branches/wiby-running-unit-tests`) .reply(200) } diff --git a/test/fixtures/http/clean-command.js b/test/fixtures/http/clean-command.js index 59d5a79..dee62b4 100644 --- a/test/fixtures/http/clean-command.js +++ b/test/fixtures/http/clean-command.js @@ -9,9 +9,9 @@ nock.disableNetConnect() function nockRepo (nockInstance, repo) { return nockInstance - .get(`/repos/wiby-test/${repo}/branches/wiby-wiby`) + .get(`/repos/wiby-test/${repo}/branches/wiby-running-unit-tests`) .reply(200) - .delete(`/repos/wiby-test/${repo}/git/refs/heads%2Fwiby-wiby`) + .delete(`/repos/wiby-test/${repo}/git/refs/heads%2Fwiby-running-unit-tests`) .reply(200) } diff --git a/test/fixtures/http/result-command-empty-branch-checks.js b/test/fixtures/http/result-command-empty-branch-checks.js index 622686c..deab11e 100644 --- a/test/fixtures/http/result-command-empty-branch-checks.js +++ b/test/fixtures/http/result-command-empty-branch-checks.js @@ -24,14 +24,14 @@ nock('https://api.github.com') } }) // get check results - .get('/repos/wiby-test/fakeRepo/commits/wiby-wiby/check-runs') + .get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs') .reply(200, { check_runs: [ // empty checks list ] }) // get checks for ref - .get('/repos/wiby-test/fakeRepo/commits/wiby-wiby/statuses') + .get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/statuses') .reply(200, { check_runs: [ { status: 'queued', name: 'fake_run' }, diff --git a/test/fixtures/http/result-command-positive-checks-failed.js b/test/fixtures/http/result-command-positive-checks-failed.js index c205489..e252c80 100644 --- a/test/fixtures/http/result-command-positive-checks-failed.js +++ b/test/fixtures/http/result-command-positive-checks-failed.js @@ -26,7 +26,7 @@ nock('https://api.github.com') } }) // get check results - .get('/repos/wiby-test/fakeRepo/commits/wiby-wiby/check-runs') + .get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs') .reply(200, { check_runs: [ { status: 'done', name: 'fake_run', conclusion: 'failure' } diff --git a/test/fixtures/http/result-command-positive.js b/test/fixtures/http/result-command-positive.js index 8e39693..a14b8d9 100644 --- a/test/fixtures/http/result-command-positive.js +++ b/test/fixtures/http/result-command-positive.js @@ -25,28 +25,28 @@ nock('https://api.github.com') } }) // get check results - .get('/repos/wiby-test/fakeRepo/commits/wiby-wiby/check-runs') + .get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs') .reply(200, { check_runs: [ { status: 'queued', name: 'fake_run' }, { status: 'done', name: 'fake_run_2', conclusion: 'fake_conclusion' } ] }) - .get('/repos/wiby-test/fail/commits/wiby-wiby/check-runs') + .get('/repos/wiby-test/fail/commits/wiby-running-unit-tests/check-runs') .reply(200, { check_runs: [ { status: 'queued', name: 'fail_run' }, { status: 'done', name: 'fail_run_2', conclusion: 'fake_conclusion' } ] }) - .get('/repos/wiby-test/pass/commits/wiby-wiby/check-runs') + .get('/repos/wiby-test/pass/commits/wiby-running-unit-tests/check-runs') .reply(200, { check_runs: [ { status: 'queued', name: 'pass_run' }, { status: 'done', name: 'pass_run_2', conclusion: 'fake_conclusion' } ] }) - .get('/repos/wiby-test/partial/commits/wiby-wiby/check-runs') + .get('/repos/wiby-test/partial/commits/wiby-running-unit-tests/check-runs') .reply(200, { check_runs: [ { status: 'queued', name: 'partial_run' }, diff --git a/test/internals/github.js b/test/internals/github.js index c8ff054..5259016 100644 --- a/test/internals/github.js +++ b/test/internals/github.js @@ -6,36 +6,38 @@ const tap = require('tap') const github = require('../../lib/github') const CONFIG = require('../fixtures/config') -tap.beforeEach(async () => { - nock.disableNetConnect() -}) +tap.test('tests for lib/github.js', async (tap) => { + tap.beforeEach(async () => { + nock.disableNetConnect() + }) -tap.afterEach(async () => { - nock.cleanAll() - nock.enableNetConnect() -}) + tap.afterEach(async () => { + nock.cleanAll() + nock.enableNetConnect() + }) -tap.test('getPackageJson() handles NotFound repository error', async tap => { - nock('https://api.github.com') - .post('/graphql') - .reply(404, { - errors: [ - { type: 'NOT_FOUND' } - ] - }) + tap.test('getPackageJson() handles NotFound repository error', async (tap) => { + nock('https://api.github.com') + .post('/graphql') + .reply(404, { + errors: [ + { type: 'NOT_FOUND' } + ] + }) - tap.rejects( - github.getPackageJson(CONFIG.DEP_ORG, CONFIG.DEP_REPO), - new Error(`Could not find GitHub repository at https://www.github.com/${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}`) - ) -}) + tap.rejects( + github.getPackageJson(CONFIG.DEP_ORG, CONFIG.DEP_REPO), + new Error(`Could not find GitHub repository at https://www.github.com/${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}`) + ) + }) -tap.test('getPackageJson() handles general error', async tap => { - nock('https://api.github.com') - .post('/graphql') - .reply(500) + tap.test('getPackageJson() handles general error', async (tap) => { + nock('https://api.github.com') + .post('/graphql') + .reply(500) - tap.rejects( - github.getPackageJson(CONFIG.DEP_ORG, CONFIG.DEP_REPO) - ) + tap.rejects( + github.getPackageJson(CONFIG.DEP_ORG, CONFIG.DEP_REPO) + ) + }) }) diff --git a/test/result.js b/test/result.js index 4143d3d..c10828a 100644 --- a/test/result.js +++ b/test/result.js @@ -3,102 +3,108 @@ require('dotenv').config() const tap = require('tap') const nock = require('nock') + const checks = require('./fixtures/checks') const CONFIG = require('./fixtures/config') -const wiby = require('..') - -tap.beforeEach(async () => { - nock.disableNetConnect() -}) +const gitFixture = require('./fixtures/git') -tap.afterEach(async () => { - nock.cleanAll() - nock.enableNetConnect() -}) - -tap.test('Test correct status returned from getResultForEachRun', tap => { - tap.equal(wiby.result.getResultForEachRun(checks).toString(), checks.expected.toString()) - tap.end() -}) +const wiby = require('..') -tap.test('result command checks package exists in dependant package.json', tap => { - nock('https://api.github.com') - // get package json - .post('/graphql') - .reply(200, { - data: { - repository: { - object: { - text: JSON.stringify({ - dependencies: { - 'other-package': '*' - } - }) +tap.test('wiby.result()', async (tap) => { + tap.beforeEach(async () => { + nock.disableNetConnect() + gitFixture.init() + }) + + tap.afterEach(async () => { + nock.cleanAll() + nock.enableNetConnect() + }) + + tap.test('Test correct status returned from getResultForEachRun', (tap) => { + tap.equal(wiby.result.getResultForEachRun(checks).toString(), checks.expected.toString()) + tap.end() + }) + + tap.test('result command checks package exists in dependant package.json', (tap) => { + nock('https://api.github.com') + // get package json + .post('/graphql') + .reply(200, { + data: { + repository: { + object: { + text: JSON.stringify({ + dependencies: { + 'other-package': '*' + } + }) + } } } - } - }) - - tap.rejects( - wiby.result({ dependents: [{ repository: `https://www.github.com/${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}` }] }), - new Error(`pkgjs/wiby not found in the package.json of ${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}`) - ) - tap.end() -}) - -tap.test('result() should return correct data object', async tap => { - // mock reall http requests with positive scenario - require('./fixtures/http/result-command-positive') - - const output = await wiby.result({ dependents: [{ repository: 'https://github.com/wiby-test/fakeRepo' }] }) - - tap.equal(output.status, 'pending') - tap.equal(output.results[0].dependent, 'wiby-test/fakeRepo') - tap.equal(output.results[0].status, 'pending') - tap.equal(output.results[0].runs.length, 2) -}) - -tap.test('getOverallStatusForAllRuns() should correctly detect overall status for runs results', async tap => { - const failedCasesPresent = [ - [null, 'failure'], - [null, 'success'], - [null, 'success'] - ] - - const unexpectedCasesPresent = [ - [null, 'unexpectedStatus'], - [null, 'success'], - [null, 'success'] - ] - - const nullCasesPresent = [ - [null, null], - [null, 'success'], - [null, 'success'] - ] - - const pendingCasesPresent = [ - [null, 'pending'], - [null, 'success'], - [null, 'success'] - ] - - const queuedCasesPresent = [ - [null, 'queued'], - [null, 'success'], - [null, 'success'] - ] - - const successCasesPresent = [ - [null, 'success'], - [null, 'success'], - [null, 'success'] - ] - - tap.equal(wiby.result.getOverallStatusForAllRuns(failedCasesPresent), 'failure') - tap.equal(wiby.result.getOverallStatusForAllRuns(unexpectedCasesPresent), 'failure') - tap.equal(wiby.result.getOverallStatusForAllRuns(nullCasesPresent), 'pending') - tap.equal(wiby.result.getOverallStatusForAllRuns(pendingCasesPresent), 'pending') - tap.equal(wiby.result.getOverallStatusForAllRuns(queuedCasesPresent), 'pending') - tap.equal(wiby.result.getOverallStatusForAllRuns(successCasesPresent), 'success') + }) + + tap.rejects( + wiby.result({ dependents: [{ repository: `https://www.github.com/${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}` }] }), + new Error(`pkgjs/wiby not found in the package.json of ${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}`) + ) + tap.end() + }) + + tap.test('result() should return correct data object', async (tap) => { + // mock reall http requests with positive scenario + require('./fixtures/http/result-command-positive') + + const output = await wiby.result({ dependents: [{ repository: 'https://github.com/wiby-test/fakeRepo' }] }) + + tap.equal(output.status, 'pending') + tap.equal(output.results[0].dependent, 'wiby-test/fakeRepo') + tap.equal(output.results[0].status, 'pending') + tap.equal(output.results[0].runs.length, 2) + }) + + tap.test('getOverallStatusForAllRuns() should correctly detect overall status for runs results', async (tap) => { + const failedCasesPresent = [ + [null, 'failure'], + [null, 'success'], + [null, 'success'] + ] + + const unexpectedCasesPresent = [ + [null, 'unexpectedStatus'], + [null, 'success'], + [null, 'success'] + ] + + const nullCasesPresent = [ + [null, null], + [null, 'success'], + [null, 'success'] + ] + + const pendingCasesPresent = [ + [null, 'pending'], + [null, 'success'], + [null, 'success'] + ] + + const queuedCasesPresent = [ + [null, 'queued'], + [null, 'success'], + [null, 'success'] + ] + + const successCasesPresent = [ + [null, 'success'], + [null, 'success'], + [null, 'success'] + ] + + tap.equal(wiby.result.getOverallStatusForAllRuns(failedCasesPresent), 'failure') + tap.equal(wiby.result.getOverallStatusForAllRuns(unexpectedCasesPresent), 'failure') + tap.equal(wiby.result.getOverallStatusForAllRuns(nullCasesPresent), 'pending') + tap.equal(wiby.result.getOverallStatusForAllRuns(pendingCasesPresent), 'pending') + tap.equal(wiby.result.getOverallStatusForAllRuns(queuedCasesPresent), 'pending') + tap.equal(wiby.result.getOverallStatusForAllRuns(successCasesPresent), 'success') + }) }) diff --git a/test/test.js b/test/test.js index 36065b5..ebfb676 100644 --- a/test/test.js +++ b/test/test.js @@ -3,91 +3,98 @@ require('dotenv').config() const tap = require('tap') const nock = require('nock') + const CONFIG = require('./fixtures/config') +const gitFixture = require('./fixtures/git') + const wiby = require('..') -tap.beforeEach(async () => { - nock.disableNetConnect() -}) +tap.test('wiby.test()', async (tap) => { + tap.beforeEach(async () => { + nock.disableNetConnect() + gitFixture.init() + }) -tap.afterEach(async () => { - nock.cleanAll() - nock.enableNetConnect() -}) + tap.afterEach(async () => { + nock.cleanAll() + nock.enableNetConnect() + }) -tap.test('Check patch applied to dependency package.json successfully', tap => { - tap.equal(JSON.stringify(wiby.test.applyPatch(CONFIG.PATCH, CONFIG.PKG_NAME_UNIT, CONFIG.PKGJSON)), JSON.stringify(CONFIG.PATCHED_PKGJSON)) - tap.end() -}) -tap.test('Check patch applied to dev dependency package.json successfully', tap => { - tap.equal(JSON.stringify(wiby.test.applyPatch(CONFIG.DEV_DEP_PATCH, CONFIG.DEV_DEP_PKG_NAME_UNIT, CONFIG.PKGJSON_DEV_DEPS)), JSON.stringify(CONFIG.PATCHED_DEV_DEPS_PKGJSON)) - tap.end() -}) + tap.test('Check patch applied to dependency package.json successfully', (tap) => { + tap.equal(JSON.stringify(wiby.test.applyPatch(CONFIG.PATCH, CONFIG.PKG_NAME_UNIT, CONFIG.PKGJSON)), JSON.stringify(CONFIG.PATCHED_PKGJSON)) + tap.end() + }) -tap.test('Check patch applied to peer dependency package.json successfully', tap => { - tap.equal(JSON.stringify(wiby.test.applyPatch(CONFIG.PEER_DEP_PATCH, CONFIG.DEV_PEER_PKG_NAME_UNIT, CONFIG.PKGJSON_PEER_DEPS)), JSON.stringify(CONFIG.PATCHED_PEER_DEPS_PKGJSON)) - tap.end() -}) + tap.test('Check patch applied to dev dependency package.json successfully', (tap) => { + tap.equal(JSON.stringify(wiby.test.applyPatch(CONFIG.DEV_DEP_PATCH, CONFIG.DEV_DEP_PKG_NAME_UNIT, CONFIG.PKGJSON_DEV_DEPS)), JSON.stringify(CONFIG.PATCHED_DEV_DEPS_PKGJSON)) + tap.end() + }) -tap.test('applyPatch() checks package exists in dependant package.json', tap => { - tap.throws( - function () { - wiby.test.applyPatch( - CONFIG.PATCH, - CONFIG.PKG_NAME_UNIT, - { - dependencies: { - 'other-package': '*' - }, - devDependencies: { - 'further-packages': '*' - }, - peerDependencies: { - 'some-plugin': '*' - }, - optionalDependencies: { - 'some-optional-dep': '*' + tap.test('Check patch applied to peer dependency package.json successfully', (tap) => { + tap.equal(JSON.stringify(wiby.test.applyPatch(CONFIG.PEER_DEP_PATCH, CONFIG.DEV_PEER_PKG_NAME_UNIT, CONFIG.PKGJSON_PEER_DEPS)), JSON.stringify(CONFIG.PATCHED_PEER_DEPS_PKGJSON)) + tap.end() + }) + + tap.test('applyPatch() checks package exists in dependant package.json', (tap) => { + tap.throws( + function () { + wiby.test.applyPatch( + CONFIG.PATCH, + CONFIG.PKG_NAME_UNIT, + { + dependencies: { + 'other-package': '*' + }, + devDependencies: { + 'further-packages': '*' + }, + peerDependencies: { + 'some-plugin': '*' + }, + optionalDependencies: { + 'some-optional-dep': '*' + } } - } - ) - }, - new Error('Dependency not found in package.json') - ) - tap.end() -}) + ) + }, + new Error('Dependency not found in package.json') + ) + tap.end() + }) -tap.test('test command checks package exists in dependant package.json', tap => { - nock('https://api.github.com') - // get package json - .post('/graphql') - .reply(200, { - data: { - repository: { - object: { - text: JSON.stringify({ - dependencies: { - 'other-package': '*' - } - }) + tap.test('test command checks package exists in dependant package.json', (tap) => { + nock('https://api.github.com') + // get package json + .post('/graphql') + .reply(200, { + data: { + repository: { + object: { + text: JSON.stringify({ + dependencies: { + 'other-package': '*' + } + }) + } } } - } - }) - .get('/repos/pkgjs/wiby/commits?per_page=1') - .reply(200, [ - { - sha: 'fake_sha', - commit: { - tree: { - sha: 'fake_sha' + }) + .get('/repos/pkgjs/wiby/commits?per_page=1') + .reply(200, [ + { + sha: 'fake_sha', + commit: { + tree: { + sha: 'fake_sha' + } } } - } - ]) + ]) - tap.rejects( - wiby.test({ dependents: [{ repository: `https://www.github.com/${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}` }] }), - new Error(`pkgjs/wiby not found in the package.json of ${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}`) - ) - tap.end() + tap.rejects( + wiby.test({ dependents: [{ repository: `https://www.github.com/${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}` }] }), + new Error(`pkgjs/wiby not found in the package.json of ${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}`) + ) + tap.end() + }) })