Skip to content

Commit

Permalink
Fix up some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Apr 17, 2020
1 parent 6d6f59b commit 24b1c04
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
10 changes: 7 additions & 3 deletions test/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,24 +257,28 @@ test.serial('should reset files', async t => {

test.serial('should roll back when cancelled', async t => {
sh.exec('git init');
sh.exec(`git remote add origin foo`);
const version = '1.2.3';
gitAdd(`{"version":"${version}"}`, 'package.json', 'Add package.json');
const options = { git: { requireCleanWorkingDir: true, commit: true, tag: true, tagName: 'v${version}' } };
const gitClient = factory(Git, { options });
const exec = sinon.spy(gitClient.shell, 'execFormattedCommand');
sh.exec(`git tag ${version}`);
gitAdd('line', 'file', 'Add file');

await gitClient.init();

sh.exec('npm --no-git-tag-version version patch');

const exec = sinon.spy(gitClient.shell, 'execFormattedCommand');
gitClient.bump('1.2.4');
await gitClient.beforeRelease();
await gitClient.stage('package.json');
await gitClient.commit({ message: 'Add this' });
await gitClient.tag();
await gitClient.rollbackOnce();

t.is(exec.args[5][0], 'git tag --delete v1.2.4');
t.is(exec.args[6][0], 'git reset --hard HEAD~1');
t.is(exec.args[10][0], 'git tag --delete v1.2.4');
t.is(exec.args[11][0], 'git reset --hard HEAD~1');
});

test.serial('should not touch existing history when rolling back', async t => {
Expand Down
18 changes: 8 additions & 10 deletions test/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ test('should release and upload assets', async t => {

interceptAuthentication();
interceptCollaborator();
interceptDraft({
body: { tag_name: '2.0.2', name: 'Release 2.0.2', body: 'Custom notes', prerelease: false, draft: true }
});
interceptPublish({ body: { draft: false, tag_name: '2.0.2' } });
interceptDraft({ body: { tag_name: '2.0.2', name: 'Release 2.0.2', body: 'Custom notes' } });
interceptPublish({ body: { tag_name: '2.0.2' } });
interceptAsset({ body: asset });

await runTasks(github);
Expand All @@ -73,8 +71,8 @@ test('should release to enterprise host', async t => {
const remote = { api: 'https://github.example.org/api/v3', host: 'github.example.org' };
interceptAuthentication(remote);
interceptCollaborator(remote);
interceptDraft(Object.assign({ body: { tag_name: '1.0.1', name: '', prerelease: false, draft: true } }, remote));
interceptPublish(Object.assign({ body: { draft: false, tag_name: '1.0.1' } }, remote));
interceptDraft(Object.assign({ body: { tag_name: '1.0.1' } }, remote));
interceptPublish(Object.assign({ body: { tag_name: '1.0.1' } }, remote));

await runTasks(github);

Expand All @@ -87,8 +85,8 @@ test('should release to alternative host and proxy', async t => {
const remote = { api: 'https://my-custom-host.org/api/v3', host: 'my-custom-host.org' };
interceptAuthentication(remote);
interceptCollaborator(remote);
interceptDraft(Object.assign({ body: { tag_name: '1.0.1', name: '', prerelease: false, draft: true } }, remote));
interceptPublish(Object.assign({ body: { draft: false, tag_name: '1.0.1' } }, remote));
interceptDraft(Object.assign({ body: { tag_name: '1.0.1' } }, remote));
interceptPublish(Object.assign({ body: { tag_name: '1.0.1' } }, remote));
const options = {
github: {
tokenRef,
Expand All @@ -110,8 +108,8 @@ test('should release to alternative host and proxy', async t => {

test('should release to git.pushRepo', async t => {
const remote = { api: 'https://my-custom-host.org/api/v3', host: 'my-custom-host.org' };
interceptDraft(Object.assign({ body: { tag_name: '1.0.1', name: '', prerelease: false, draft: true } }, remote));
interceptPublish(Object.assign({ body: { draft: false, tag_name: '1.0.1' } }, remote));
interceptDraft(Object.assign({ body: { tag_name: '1.0.1' } }, remote));
interceptPublish(Object.assign({ body: { tag_name: '1.0.1' } }, remote));
const options = { git: { pushRepo: 'upstream' }, github: { tokenRef, skipChecks: true } };
const github = factory(GitHub, { options });
const exec = sinon.stub(github.shell, 'exec').callThrough();
Expand Down
11 changes: 5 additions & 6 deletions test/stub/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ const interceptDraft = ({
host = 'github.com',
owner = 'user',
project = 'repo',
body: requestBody
body: { tag_name, name = '', body = null, prerelease = false, draft = true }
} = {}) =>
nock(api)
.post(`/repos/${owner}/${project}/releases`, requestBody)
.post(`/repos/${owner}/${project}/releases`, { tag_name, name, body, prerelease, draft })
.reply(() => {
const id = 1;
const { tag_name, name, body, prerelease, draft } = requestBody;
const responseBody = {
id,
tag_name,
Expand All @@ -42,13 +41,13 @@ const interceptPublish = ({
api = 'https://api.github.com',
owner = 'user',
project = 'repo',
body = {}
body: { tag_name, draft = false }
} = {}) =>
nock(api)
.patch(`/repos/${owner}/${project}/releases/1`, body)
.patch(`/repos/${owner}/${project}/releases/1`, { tag_name, draft })
.reply(200, {
draft: false,
html_url: `https://${host}/${owner}/${project}/releases/tag/${body.tag_name}`
html_url: `https://${host}/${owner}/${project}/releases/tag/${tag_name}`
});

const interceptAsset = ({
Expand Down
21 changes: 16 additions & 5 deletions test/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ test.serial('should release all the things (pre-release, github, gitlab)', async
const project = path.basename(bare);
const pkgName = path.basename(target);
const owner = path.basename(path.dirname(bare));
const url = `https://gitlab.com/${owner}/${project}`;
gitAdd(`{"name":"${pkgName}","version":"1.0.0"}`, 'package.json', 'Add package.json');
sh.exec('git tag v1.0.0');
const sha = gitAdd('line', 'file', 'More file');
Expand Down Expand Up @@ -231,7 +232,7 @@ test.serial('should release all the things (pre-release, github, gitlab)', async
links: [
{
name: 'file',
url: `https://gitlab.com/${owner}/${project}/uploads/7e8bec1fe27cc46a4bc6a91b9e82a07c/file`
url: `${url}/uploads/7e8bec1fe27cc46a4bc6a91b9e82a07c/file`
}
]
}
Expand All @@ -241,7 +242,11 @@ test.serial('should release all the things (pre-release, github, gitlab)', async
const container = getContainer({
increment: 'minor',
preRelease: 'alpha',
git: { changelog: 'git log --pretty=format:%h ${latestTag}...HEAD' },
git: {
changelog: 'git log --pretty=format:%h ${latestTag}...HEAD',
commitMessage: 'Release ${version} for ${name} (from ${latestVersion})',
tagAnnotation: '${repo.remote} ${repo.owner} ${repo.repository} ${repo.project}'
},
github: {
release: true,
pushRepo: `https://github.com/${owner}/${project}`,
Expand All @@ -250,7 +255,7 @@ test.serial('should release all the things (pre-release, github, gitlab)', async
},
gitlab: {
release: true,
pushRepo: `https://gitlab.com/${owner}/${project}`,
pushRepo: url,
releaseNotes: 'echo Notes for ${name}: ${changelog}',
assets: ['file']
},
Expand All @@ -270,8 +275,14 @@ test.serial('should release all the things (pre-release, github, gitlab)', async
'npm publish . --tag alpha'
]);

const { stdout } = sh.exec('git describe --tags --abbrev=0');
t.is(stdout.trim(), 'v1.1.0-alpha.0');
const { stdout: commitMessage } = sh.exec('git log --oneline --format=%B -n 1 HEAD');
t.is(commitMessage.trim(), `Release 1.1.0-alpha.0 for ${pkgName} (from 1.0.0)`);

const { stdout: tagName } = sh.exec('git describe --tags --abbrev=0');
t.is(tagName.trim(), 'v1.1.0-alpha.0');

const { stdout: tagAnnotation } = sh.exec("git for-each-ref refs/tags/v1.1.0-alpha.0 --format='%(contents)'");
t.is(tagAnnotation.trim(), `${bare} ${owner} ${owner}/${project} ${project}`);

t.true(log.obtrusive.firstCall.args[0].endsWith(`release ${pkgName} (1.0.0...1.1.0-alpha.0)`));
t.true(log.log.firstCall.args[0].endsWith(`https://github.com/${owner}/${project}/releases/tag/v1.1.0-alpha.0`));
Expand Down

0 comments on commit 24b1c04

Please sign in to comment.