UNPKG

3.95 kBJavaScriptView Raw
1'use strict';
2
3const chai = require(`chai`);
4const fs = require(`fs`);
5const path = require('path');
6const mocha = require(`mocha`);
7const shell = require(`shelljs`);
8const tmp = require(`tmp`);
9const nock = require('nock');
10
11const expect = chai.expect;
12
13const afterEach = mocha.afterEach;
14const before = mocha.before;
15const beforeEach = mocha.beforeEach;
16const describe = mocha.describe;
17const it = mocha.it;
18
19shell.config.silent = true;
20
21const preparations = [
22 () => {
23 shell.exec(`git init`);
24 shell.exec(`git config user.email "you@example.com"`);
25 shell.exec(`git config user.name "Your Name"`);
26 },
27 () => shell.exec(`git commit --allow-empty -m "init" --no-gpg-sign`),
28 () => shell.exec(`git tag 1.0.1`),
29 () => fs.writeFileSync(`package.json`, `{ "name": "test", "version": "1.0.0" }`),
30];
31
32const runNPreparations = n => {
33 for (let i = 0; i < n; ++i) {
34 preparations[i]();
35 }
36};
37
38describe(`npm-publish-git-tag CLI`, function () {
39 // Setting up our fake project and creating git commits takes longer than the default Mocha timeout.
40 this.timeout(20000);
41
42 before(function () {
43 // This is not actually used in these tests, since `nock` only works within the same process, while the tests below
44 // shell out to a sub process.
45 nock.disableNetConnect();
46 });
47
48 describe(`when publishing fails`, function () {
49 beforeEach(function () {
50 this.binPath = path.resolve('src/cli.js');
51 this.cwd = process.cwd();
52 this.tmpDir = tmp.dirSync();
53 process.chdir(this.tmpDir.name);
54 fs.writeFileSync(`.npmrc`, ``);
55 });
56
57 afterEach(function () {
58 process.chdir(this.cwd);
59 });
60
61 it(`returns a non-zero code when called from outside a git repo`, function () {
62 const cliRes = shell.exec(`node ${this.binPath}`);
63 expect(cliRes.code).to.be.a('number').and.not.to.equal(0);
64 expect(cliRes.stderr).to.have.string(`npm-publish-git-tag failed for the following reason`);
65 });
66
67 it(`returns a non-zero code when the current branch does not have commits`, function () {
68 runNPreparations(1);
69 const cliRes = shell.exec(`node ${this.binPath}`);
70 expect(cliRes.code).to.be.a('number').and.not.to.equal(0);
71 expect(cliRes.stderr).to.have.string(`npm-publish-git-tag failed for the following reason`);
72 });
73
74 // Our call to `latestSemverTag` returns an empty string when no valid semver tag exists.
75 // TODO: Throw an error instead, and handle that in our test case.
76 it.skip(`returns a non-zero code when there is no tag with a valid version`, function () {
77 runNPreparations(2);
78 const cliRes = shell.exec(`node ${this.binPath}`);
79 expect(cliRes.code).to.be.a('number').and.not.to.equal(0);
80 expect(cliRes.stderr).to.have.string(`npm-publish-git-tag failed for the following reason`);
81 });
82
83 it(`returns a non-zero code when the current directory does not contain package.json`, function () {
84 runNPreparations(3);
85 const cliRes = shell.exec(`node ${this.binPath}`);
86 expect(cliRes.code).to.be.a('number').and.not.to.equal(0);
87 expect(cliRes.stderr).to.have.string(`npm-publish-git-tag failed for the following reason`);
88 });
89
90 it(`returns a non-zero code when there is no environment variable NPM_TOKEN`, function () {
91 runNPreparations(4);
92
93 const oldToken = process.env.NPM_TOKEN;
94 delete process.env.NPM_TOKEN;
95
96 const cliRes = shell.exec(`node ${this.binPath}`);
97 expect(cliRes.code).to.be.a('number').and.not.to.equal(0);
98 expect(cliRes.stderr).to.have.string(`npm-publish-git-tag failed for the following reason`);
99
100 process.env.NPM_TOKEN = oldToken;
101 });
102 });
103
104 describe(`when publishing succeeds for un-scoped package`, () => {
105 it.skip(`publishes successfully when skipping token`);
106 });
107
108 describe(`when publishing succeeds for scoped package`, () => {
109 it.skip(`publishes successfully when skipping token`);
110 });
111});