main.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. var fs = require('fs');
  2. var gutil = require('gulp-util');
  3. var should = require('should');
  4. var bump = require('../index');
  5. require('mocha');
  6. var fixtureFile = fs.readFileSync('test/fixtures/package.json');
  7. describe('gulp-bump', function() {
  8. describe('gulp-bump - JSON comparison fixtures', function() {
  9. it('should bump patch version by default', function(done) {
  10. var fakeFile = new gutil.File({
  11. contents: new Buffer('{ "version": "0.0.1" }')
  12. });
  13. var bumpS = bump();
  14. bumpS.once('data', function(newFile) {
  15. should.exist(newFile);
  16. should.exist(newFile.contents);
  17. JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2');
  18. return done();
  19. });
  20. bumpS.write(fakeFile);
  21. bumpS.end();
  22. });
  23. it('should bump patch version as default and a key=appversion', function(done) {
  24. var fakeFile = new gutil.File({
  25. contents: new Buffer('{ "appversion": "0.0.1" }')
  26. });
  27. var bumpS = bump({key: 'appversion'});
  28. bumpS.once('data', function(newFile) {
  29. should.exist(newFile);
  30. should.exist(newFile.contents);
  31. JSON.parse(newFile.contents.toString()).appversion.should.equal('0.0.2');
  32. return done();
  33. });
  34. bumpS.write(fakeFile);
  35. bumpS.end();
  36. });
  37. it('should ignore invalid type and use type=patch', function(done) {
  38. var fakeFile = new gutil.File({
  39. contents: new Buffer('{ "version": "0.0.1" }')
  40. });
  41. var bumpS = bump({type: 'invalidType'});
  42. bumpS.once('data', function(newFile) {
  43. should.exist(newFile);
  44. should.exist(newFile.contents);
  45. JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2');
  46. return done();
  47. });
  48. bumpS.write(fakeFile);
  49. bumpS.end();
  50. });
  51. it('should set the correct version when supplied', function(done) {
  52. var fakeFile = new gutil.File({
  53. contents: new Buffer('{ "version": "0.0.1" }')
  54. });
  55. var bumpS = bump({version: '0.0.2'});
  56. bumpS.once('data', function(newFile) {
  57. should.exist(newFile);
  58. should.exist(newFile.contents);
  59. JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2');
  60. return done();
  61. });
  62. bumpS.write(fakeFile);
  63. bumpS.end();
  64. });
  65. it('should set the correct version when supplied even if key did not exist', function(done) {
  66. var fakeFile = new gutil.File({
  67. contents: new Buffer('{}')
  68. });
  69. var bumpS = bump({version: '0.0.2'});
  70. bumpS.once('data', function(newFile) {
  71. should.exist(newFile);
  72. should.exist(newFile.contents);
  73. JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2');
  74. return done();
  75. });
  76. bumpS.write(fakeFile);
  77. bumpS.end();
  78. });
  79. it('should bump prerelease version', function(done) {
  80. var fakeFile = new gutil.File({
  81. contents: new Buffer('{ "version": "0.0.1-0"}')
  82. });
  83. var bumpS = bump({type: 'prerelease'});
  84. bumpS.once('data', function(newFile) {
  85. should.exist(newFile);
  86. should.exist(newFile.contents);
  87. JSON.parse(newFile.contents.toString()).version.should.equal('0.0.1-1');
  88. return done();
  89. });
  90. bumpS.write(fakeFile);
  91. bumpS.end();
  92. });
  93. });
  94. describe('Test failure cases cases in gulp-bump', function() {
  95. it('should fail when not detect a valid semver version', function(done) {
  96. var file = 'some-dir/dummyfile.js';
  97. var fakeFile = new gutil.File({
  98. path: file,
  99. contents: new Buffer('{ "version": "0.A.1" }')
  100. });
  101. var bumpS = bump();
  102. bumpS.on('error', function(e) {
  103. should.exist(e);
  104. e.message.should.equal('Detected invalid semver version');
  105. e.fileName.should.containEql(file);
  106. return done();
  107. });
  108. bumpS.write(fakeFile);
  109. bumpS.end();
  110. });
  111. it('should fail when not detect a valid semver version and wrong key', function(done) {
  112. var file = 'some-dir/dummyfile.js';
  113. var fakeFile = new gutil.File({
  114. path: file,
  115. contents: new Buffer('{ "version": "0.0.1" }')
  116. });
  117. var bumpS = bump({key: 'appversion'});
  118. bumpS.on('error', function(e) {
  119. should.exist(e);
  120. e.message.should.containEql('Detected invalid semver appversion');
  121. e.fileName.should.containEql(file);
  122. return done();
  123. });
  124. bumpS.write(fakeFile);
  125. bumpS.end();
  126. });
  127. it('should fail when supplied with an invalid JSON', function(done) {
  128. var file = 'some-dir/dummyfile.js';
  129. var fakeFile = new gutil.File({
  130. path: file,
  131. contents: new Buffer('{ invalid json oh no!!!}')
  132. });
  133. var bumpS = bump();
  134. bumpS.on('error', function(e) {
  135. should.exist(e);
  136. e.name.should.equal('Error');
  137. e.message.should.containEql('Problem parsing JSON file');
  138. e.fileName.should.containEql(file);
  139. return done();
  140. });
  141. bumpS.write(fakeFile);
  142. bumpS.end();
  143. });
  144. it('should fallback to defaults when supplied with invalid semver version', function(done) {
  145. var fakeFile = new gutil.File({
  146. contents: new Buffer('{ "version": "0.0.1" }')
  147. });
  148. var bumpS = bump({version: '0.A.2'});
  149. bumpS.once('data', function(newFile) {
  150. should.exist(newFile);
  151. should.exist(newFile.contents);
  152. JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2');
  153. return done();
  154. });
  155. bumpS.write(fakeFile);
  156. bumpS.end();
  157. });
  158. });
  159. describe('gulp-bump - JSON File fixtures', function() {
  160. it('should bump minor by default', function(done) {
  161. var fakeFile = new gutil.File({
  162. base: 'test/',
  163. cwd: 'test/',
  164. path: 'test/fixtures/package.json',
  165. contents: fixtureFile
  166. });
  167. var bumpS = bump();
  168. bumpS.once('data', function(newFile) {
  169. should.exist(newFile);
  170. should.exist(newFile.path);
  171. should.exist(newFile.contents);
  172. String(newFile.contents).should.equal(fs.readFileSync('test/expected/default.json', 'utf8'));
  173. done();
  174. });
  175. bumpS.write(fakeFile);
  176. });
  177. it('should bump major if options.bump = major', function(done) {
  178. var fakeFile = new gutil.File({
  179. base: 'test/',
  180. cwd: 'test/',
  181. path: 'test/fixtures/package.json',
  182. contents: fixtureFile
  183. });
  184. var bumpS = bump({type: 'major'});
  185. bumpS.once('data', function(newFile) {
  186. should.exist(newFile);
  187. should.exist(newFile.path);
  188. should.exist(newFile.contents);
  189. String(newFile.contents).should.equal(fs.readFileSync('test/expected/major.json', 'utf8'));
  190. done();
  191. });
  192. bumpS.write(fakeFile);
  193. });
  194. it('should bump minor if options.bump = minor', function(done) {
  195. var fakeFile = new gutil.File({
  196. base: 'test/',
  197. cwd: 'test/',
  198. path: 'test/fixtures/package.json',
  199. contents: fixtureFile
  200. });
  201. var bumpS = bump({type: 'minor'});
  202. bumpS.once('data', function(newFile) {
  203. should.exist(newFile);
  204. should.exist(newFile.path);
  205. should.exist(newFile.contents);
  206. String(newFile.contents).should.equal(fs.readFileSync('test/expected/minor.json', 'utf8'));
  207. done();
  208. });
  209. bumpS.write(fakeFile);
  210. });
  211. it('should set version to value specified by options.version', function(done) {
  212. var fakeFile = new gutil.File({
  213. base: 'test/',
  214. cwd: 'test/',
  215. path: 'test/fixtures/package.json',
  216. contents: fixtureFile
  217. });
  218. var bumpS = bump({version: '1.0.0'});
  219. bumpS.once('data', function(newFile) {
  220. should.exist(newFile);
  221. should.exist(newFile.path);
  222. should.exist(newFile.contents);
  223. String(newFile.contents).should.equal(fs.readFileSync('test/expected/version.json', 'utf8'));
  224. done();
  225. });
  226. bumpS.write(fakeFile);
  227. });
  228. it('should set the key to a custom version', function(done) {
  229. var fakeFile = new gutil.File({
  230. base: 'test/',
  231. cwd: 'test/',
  232. path: 'test/fixtures/key.json',
  233. contents: fs.readFileSync('test/fixtures/key.json')
  234. });
  235. var bumpS = bump({key: 'appversion'});
  236. bumpS.once('data', function(newFile) {
  237. should.exist(newFile);
  238. should.exist(newFile.path);
  239. should.exist(newFile.contents);
  240. String(newFile.contents).should.equal(fs.readFileSync('test/expected/key.json', 'utf8'));
  241. done();
  242. });
  243. bumpS.write(fakeFile);
  244. });
  245. });
  246. describe('gulp-bump - Whitespace preserving', function() {
  247. var fixtureObj = { version: '1.0.0' };
  248. var expectedObj = { version: '1.0.1' };
  249. var createFile = function (tabType) {
  250. return new gutil.File({
  251. base: 'test/',
  252. cwd: 'test/',
  253. path: 'test/fixtures/package.json',
  254. contents: new Buffer(JSON.stringify(fixtureObj, null, tabType))
  255. });
  256. };
  257. it('should preserve tab whitespace settings', function (done) {
  258. var fakeFile = createFile('\t');
  259. var bumpS = bump();
  260. bumpS.once('data', function(newFile) {
  261. String(newFile.contents).should.equal(JSON.stringify(expectedObj, null, '\t'));
  262. done();
  263. });
  264. bumpS.write(fakeFile);
  265. });
  266. it('should preserve spaces whitespace settings', function (done) {
  267. var fakeFile = createFile(3);
  268. var bumpS = bump();
  269. bumpS.once('data', function(newFile) {
  270. String(newFile.contents).should.equal(JSON.stringify(expectedObj, null, 3));
  271. done();
  272. });
  273. bumpS.write(fakeFile);
  274. });
  275. it('should override whitespace if indent defined', function (done) {
  276. var fakeFile = createFile(3);
  277. var bumpS = bump({ indent: 2 });
  278. bumpS.once('data', function(newFile) {
  279. String(newFile.contents).should.equal(JSON.stringify(expectedObj, null, 2));
  280. done();
  281. });
  282. bumpS.write(fakeFile);
  283. });
  284. it('should preserve whitespace at end', function (done) {
  285. var fakeFile = new gutil.File({
  286. base: 'test/',
  287. cwd: 'test/',
  288. path: 'test/fixtures/package.json',
  289. contents: new Buffer(JSON.stringify(fixtureObj, null, 2) + '\n')
  290. });
  291. var bumpS = bump();
  292. bumpS.once('data', function(newFile) {
  293. String(newFile.contents.slice(-1)).should.equal('\n');
  294. done();
  295. });
  296. bumpS.write(fakeFile);
  297. });
  298. it('should not add new line to file', function (done) {
  299. var fakeFile = new gutil.File({
  300. base: 'test/',
  301. cwd: 'test/',
  302. path: 'test/fixtures/package.json',
  303. contents: new Buffer(JSON.stringify(fixtureObj, null, 2))
  304. });
  305. var bumpS = bump();
  306. bumpS.once('data', function(newFile) {
  307. String(newFile.contents.slice(-1)).should.not.equal('\n');
  308. done();
  309. });
  310. bumpS.write(fakeFile);
  311. });
  312. });
  313. });