test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*global describe, before, it*/
  2. 'use strict';
  3. var fs = require('fs');
  4. var path = require('path');
  5. var utils = require('./utils');
  6. var clean = require('./index');
  7. var expect = require('chai').expect;
  8. function noop() {}
  9. describe('gulp-clean plugin', function () {
  10. var cwd = process.cwd();
  11. before(function () {
  12. var exists = fs.existsSync('tmp');
  13. if (!exists) { fs.mkdirSync('tmp'); }
  14. });
  15. function createTree(callback) {
  16. fs.mkdir('tmp/tree/', function () {
  17. fs.mkdir('tmp/tree/leaf', function () {
  18. fs.mkdir('tmp/tree/leaf/node', function () {
  19. fs.writeFile('tmp/tree/leaf/node/leaf.js', 'console.log("leaf")', function () {
  20. fs.mkdir('tmp/tree/leftleaf', function () {
  21. fs.writeFile('tmp/tree/leftleaf/leaf1.js', 'console.log("leaf")', function () {
  22. callback();
  23. });
  24. });
  25. });
  26. });
  27. });
  28. });
  29. }
  30. it('removes a file', function (done) {
  31. var stream = clean();
  32. var content = 'testing';
  33. fs.writeFile('tmp/test.js', content, function () {
  34. stream.on('data', noop);
  35. stream.on('end', function () {
  36. fs.exists('tmp/test.js', function (exists) {
  37. expect(exists).to.be.false;
  38. done();
  39. });
  40. });
  41. stream.write(new utils.File({
  42. cwd: cwd,
  43. base: cwd + '/tmp/',
  44. path: cwd + '/tmp/test.js',
  45. contents: new Buffer(content)
  46. }));
  47. stream.end();
  48. });
  49. });
  50. it('removes a directory', function (done) {
  51. fs.mkdir('tmp/test', function () {
  52. var stream = clean();
  53. stream.on('end', function () {
  54. fs.exists('tmp/test', function (exists) {
  55. expect(exists).to.be.false;
  56. done();
  57. });
  58. });
  59. stream.write(new utils.File({
  60. cwd: cwd,
  61. base: cwd + '/tmp/',
  62. path: cwd + '/tmp/test/'
  63. }));
  64. stream.on('data', noop);
  65. stream.end();
  66. });
  67. });
  68. it('removes all from the tree', function (done) {
  69. createTree(function () {
  70. var stream = clean();
  71. stream.on('end', function () {
  72. fs.exists('tmp/tree/leaf/node/leaf.js', function (exists) {
  73. expect(exists).to.be.false;
  74. fs.exists('tmp/tree', function (exists) {
  75. expect(exists).to.be.false;
  76. done();
  77. });
  78. });
  79. });
  80. stream.on('data', noop);
  81. stream.write(new utils.File({
  82. cwd: cwd,
  83. base: cwd + '/tmp',
  84. path: cwd + '/tmp/tree/'
  85. }));
  86. stream.end();
  87. });
  88. });
  89. it('cannot remove the current working directory', function (done) {
  90. var stream = clean();
  91. stream.on('error', function () {
  92. fs.exists('.', function (exists) {
  93. expect(exists).to.be.true;
  94. });
  95. });
  96. stream.on('end', function () {
  97. fs.exists('.', function (exists) {
  98. expect(exists).to.be.true;
  99. done();
  100. });
  101. });
  102. stream.on('data', noop);
  103. stream.write(new utils.File({
  104. cwd: cwd,
  105. path: cwd
  106. }));
  107. stream.end();
  108. });
  109. it('cannot delete anything outside the current working directory', function (done) {
  110. var stream = clean();
  111. if (!fs.existsSync('../secrets')) { fs.mkdirSync('../secrets'); }
  112. stream.on('error', function () {
  113. fs.exists('../secrets', function (exists) {
  114. expect(exists).to.be.true;
  115. });
  116. });
  117. stream.on('end', function () {
  118. fs.exists('../secrets', function (exists) {
  119. expect(exists).to.be.true;
  120. done();
  121. });
  122. });
  123. stream.on('data', noop);
  124. stream.write(new utils.File({
  125. cwd: path.resolve(cwd),
  126. path: path.resolve(cwd + '/../secrets/')
  127. }));
  128. stream.end();
  129. });
  130. it('cannot delete a folder outside the current working directory', function (done) {
  131. var stream = clean();
  132. if (!fs.existsSync('../gulp-cleanTemp')) { fs.mkdirSync('../gulp-cleanTemp'); }
  133. stream.on('error', function () {
  134. fs.exists('../gulp-cleanTemp', function (exists) {
  135. expect(exists).to.be.true;
  136. });
  137. });
  138. stream.on('data', noop);
  139. stream.on('end', function () {
  140. fs.exists('../gulp-cleanTemp', function (exists) {
  141. expect(exists).to.be.true;
  142. fs.unlink('../gulp-cleanTemp', function () {
  143. done();
  144. });
  145. });
  146. });
  147. stream.write(new utils.File({
  148. cwd: path.resolve(cwd),
  149. path: path.resolve(cwd + '/../gulp-cleanTemp/')
  150. }));
  151. stream.end();
  152. });
  153. it('can delete contents outside the current working directory with option force true', function (done) {
  154. var stream = clean({force: true});
  155. if (!fs.existsSync('../gulp-cleanTemp')) { fs.mkdirSync('../gulp-cleanTemp'); }
  156. stream.on('data', noop);
  157. stream.on('end', function () {
  158. fs.exists('../gulp-cleanTemp', function (exists) {
  159. expect(exists).to.be.false;
  160. done();
  161. });
  162. });
  163. stream.write(new utils.File({
  164. cwd: path.resolve(cwd),
  165. path: path.resolve(cwd + '/../gulp-cleanTemp/')
  166. }));
  167. stream.end();
  168. });
  169. });