1 | 'use strict';
|
2 |
|
3 | const chai = require(`chai`);
|
4 | const fs = require(`fs`);
|
5 | const path = require('path');
|
6 | const mocha = require(`mocha`);
|
7 | const shell = require(`shelljs`);
|
8 | const tmp = require(`tmp`);
|
9 | const nock = require('nock');
|
10 |
|
11 | const expect = chai.expect;
|
12 |
|
13 | const afterEach = mocha.afterEach;
|
14 | const before = mocha.before;
|
15 | const beforeEach = mocha.beforeEach;
|
16 | const describe = mocha.describe;
|
17 | const it = mocha.it;
|
18 |
|
19 | shell.config.silent = true;
|
20 |
|
21 | const 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 |
|
32 | const runNPreparations = n => {
|
33 | for (let i = 0; i < n; ++i) {
|
34 | preparations[i]();
|
35 | }
|
36 | };
|
37 |
|
38 | describe(`npm-publish-git-tag CLI`, function () {
|
39 |
|
40 | this.timeout(20000);
|
41 |
|
42 | before(function () {
|
43 |
|
44 |
|
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 |
|
75 |
|
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 | });
|