1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 | const chai = require(`chai`);
|
6 | const chaiAsPromised = require(`chai-as-promised`);
|
7 | const fs = require(`fs`);
|
8 | const mocha = require(`mocha`);
|
9 | const shell = require(`shelljs`);
|
10 | const sinon = require(`sinon`);
|
11 | const sinonChai = require(`sinon-chai`);
|
12 | const nock = require('nock');
|
13 | const tmp = require(`tmp`);
|
14 |
|
15 | const npmPublishGitTag = require(`./index`).npmPublishGitTag;
|
16 |
|
17 | chai.use(chaiAsPromised);
|
18 | chai.use(sinonChai);
|
19 | const expect = chai.expect;
|
20 |
|
21 | const afterEach = mocha.after;
|
22 | const before = mocha.before;
|
23 | const beforeEach = mocha.beforeEach;
|
24 | const describe = mocha.describe;
|
25 | const it = mocha.it;
|
26 |
|
27 | describe(`npm-publish-git-tag`, function () {
|
28 |
|
29 | this.timeout(20000);
|
30 |
|
31 | before(function () {
|
32 | nock.disableNetConnect();
|
33 | });
|
34 |
|
35 | beforeEach(function () {
|
36 |
|
37 |
|
38 | this.cwd = process.cwd();
|
39 | this.tmpDir = tmp.dirSync();
|
40 | process.chdir(this.tmpDir.name);
|
41 |
|
42 | this.oldToken = process.env.NPM_TOKEN;
|
43 | process.env.NPM_TOKEN = `token`;
|
44 |
|
45 |
|
46 | fs.writeFileSync(`package.json`, `{ "name": "test", "version": "0.0.0" }`);
|
47 |
|
48 |
|
49 | fs.writeFileSync(`.npmrc`, ``);
|
50 |
|
51 |
|
52 | shell.config.silent = true;
|
53 |
|
54 |
|
55 | shell.exec(`git init`);
|
56 | shell.exec(`git config user.email "you@example.com"`);
|
57 | shell.exec(`git config user.name "Your Name"`);
|
58 | shell.exec(`git commit --allow-empty -m "init" --no-gpg-sign`);
|
59 |
|
60 |
|
61 |
|
62 | this.execStub = sinon.stub();
|
63 | this.execStub.rejects();
|
64 |
|
65 | this.wrapped = options => npmPublishGitTag({exec: this.execStub})(options);
|
66 | });
|
67 |
|
68 | afterEach(function () {
|
69 | process.env.NPM_TOKEN = this.oldToken;
|
70 | process.chdir(this.cwd);
|
71 | });
|
72 |
|
73 | describe(`existing tag`, function () {
|
74 | beforeEach(function () {
|
75 | shell.exec(`git tag 1.0.0`);
|
76 | });
|
77 |
|
78 | it(`writes last git tag to 'package.json'`, function () {
|
79 | this.execStub.withArgs(`npm publish`).resolves();
|
80 |
|
81 | return expect(this.wrapped({})).to.be.fulfilled
|
82 | .then(() => {
|
83 | const packageContent = JSON.parse(fs.readFileSync(`package.json`));
|
84 | expect(packageContent.name).to.equal(`test`);
|
85 | expect(packageContent.version).to.equal(`1.0.0`);
|
86 | expect(this.execStub).to.have.been.calledOnce;
|
87 | });
|
88 | });
|
89 |
|
90 | it(`augments '.npmrc' with authentication placeholder`, function () {
|
91 | this.execStub.withArgs(`npm publish`).resolves();
|
92 |
|
93 | return expect(this.wrapped({})).to.be.fulfilled
|
94 | .then(() => {
|
95 | const npmrcContent = fs.readFileSync(`.npmrc`);
|
96 | expect(npmrcContent.toString()).to.equal(`\n//registry.npmjs.org/:_authToken=\${NPM_TOKEN}\n`);
|
97 | expect(this.execStub).to.have.been.calledOnce;
|
98 | });
|
99 | });
|
100 |
|
101 | describe(`with a trailing commit`, function () {
|
102 | beforeEach(function () {
|
103 | shell.exec(`git commit --allow-empty -m "feat(index): add enhancement" --no-gpg-sign`);
|
104 | });
|
105 |
|
106 | it(`writes last git tag to 'package.json'`, function () {
|
107 | this.execStub.withArgs(`npm publish`).resolves();
|
108 |
|
109 | return expect(this.wrapped({})).to.be.fulfilled
|
110 | .then(() => {
|
111 | const packageContent = JSON.parse(fs.readFileSync(`package.json`));
|
112 | expect(packageContent.name).to.equal(`test`);
|
113 | expect(packageContent.version).to.equal(`1.0.0`);
|
114 | expect(this.execStub).to.have.been.calledOnce;
|
115 | });
|
116 | });
|
117 | });
|
118 |
|
119 | describe(`with a second tag`, function () {
|
120 | beforeEach(function () {
|
121 | shell.exec(`git commit --allow-empty -m "feat(index): add enhancement" --no-gpg-sign`);
|
122 | shell.exec(`git tag 1.1.0`);
|
123 | });
|
124 |
|
125 | it(`writes last git tag to 'package.json'`, function () {
|
126 | this.execStub.withArgs(`npm publish`).resolves();
|
127 |
|
128 | return expect(this.wrapped({})).to.be.fulfilled
|
129 | .then(() => {
|
130 | const packageContent = JSON.parse(fs.readFileSync(`package.json`));
|
131 | expect(packageContent.name).to.equal(`test`);
|
132 | expect(packageContent.version).to.equal(`1.1.0`);
|
133 | expect(this.execStub).to.have.been.calledOnce;
|
134 | });
|
135 | });
|
136 | });
|
137 |
|
138 | it(`can set access level for package'`, function () {
|
139 | this.execStub.withArgs(`npm publish --access restricted`).resolves();
|
140 |
|
141 | return expect(this.wrapped({access: `restricted`})).to.be.fulfilled
|
142 | .then(() => {
|
143 | expect(this.execStub).to.have.been.calledOnce;
|
144 | });
|
145 | });
|
146 |
|
147 | it(`does not augment '.npmrc' with authentication placeholder when skipping token authentication`, function () {
|
148 | this.execStub.withArgs(`npm publish`).resolves();
|
149 |
|
150 | return expect(this.wrapped({skipToken: true})).to.be.fulfilled
|
151 | .then(() => {
|
152 | const npmrcContent = fs.readFileSync(`.npmrc`);
|
153 | expect(npmrcContent.toString()).to.equal(``);
|
154 | expect(this.execStub).to.have.been.calledOnce;
|
155 | });
|
156 | });
|
157 | });
|
158 |
|
159 | describe(`publishing patches and minor versions off of a branch`, function () {
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | beforeEach(function () {
|
174 | shell.exec(`git tag 1.0.1`);
|
175 | shell.exec(`git commit --allow-empty -m "feat(index): major change\n\nBREAKING CHANGE: change" --no-gpg-sign`);
|
176 |
|
177 |
|
178 | shell.exec(`git tag 2.0.0`);
|
179 |
|
180 |
|
181 |
|
182 | shell.exec(`git checkout -b fix/package 1.0.1`);
|
183 |
|
184 |
|
185 | shell.exec(`git commit --allow-empty -m "fix(index): remove bug" --no-gpg-sign`);
|
186 | shell.exec(`git tag 1.0.2`);
|
187 | });
|
188 |
|
189 | it(`should publish patch version using latest tag on the current branch`, function () {
|
190 | this.execStub.withArgs(`npm publish`).resolves();
|
191 |
|
192 | return expect(this.wrapped({})).to.be.fulfilled
|
193 | .then(() => {
|
194 | const packageContent = JSON.parse(fs.readFileSync(`package.json`));
|
195 | expect(packageContent.name).to.equal(`test`);
|
196 | expect(packageContent.version).to.equal(`1.0.2`);
|
197 | expect(this.execStub).to.have.been.calledOnce;
|
198 | });
|
199 | });
|
200 | });
|
201 | });
|