file.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Utilities: A classic collection of JavaScript utilities
  3. * Copyright 2112 Matthew Eernisse ([email protected])
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. var assert = require('assert')
  19. , fs = require('fs')
  20. , path = require('path')
  21. , file = require('../lib/file')
  22. , existsSync = fs.existsSync || path.existsSync
  23. , tests;
  24. tests = {
  25. 'before': function () {
  26. process.chdir('./test');
  27. }
  28. , 'after': function () {
  29. process.chdir('../');
  30. }
  31. , 'test mkdirP': function () {
  32. var expected = [
  33. ['foo']
  34. , ['foo', 'bar']
  35. , ['foo', 'bar', 'baz']
  36. , ['foo', 'bar', 'baz', 'qux']
  37. ]
  38. , res;
  39. file.mkdirP('foo/bar/baz/qux');
  40. res = file.readdirR('foo');
  41. for (var i = 0, ii = res.length; i < ii; i++) {
  42. assert.equal(path.join.apply(path, expected[i]), res[i]);
  43. }
  44. file.rmRf('foo', {silent: true});
  45. }
  46. , 'test rmRf': function () {
  47. file.mkdirP('foo/bar/baz/qux', {silent: true});
  48. file.rmRf('foo/bar', {silent: true});
  49. res = file.readdirR('foo');
  50. assert.equal(1, res.length);
  51. assert.equal('foo', res[0]);
  52. fs.rmdirSync('foo');
  53. }
  54. , 'test cpR with same name and different directory': function () {
  55. file.mkdirP('foo', {silent: true});
  56. fs.writeFileSync('foo/bar.txt', 'w00t');
  57. file.cpR('foo', 'bar', {silent: true});
  58. assert.ok(existsSync('bar/bar.txt'));
  59. file.rmRf('foo', {silent: true});
  60. file.rmRf('bar', {silent: true});
  61. }
  62. , 'test cpR with same to and from will throw': function () {
  63. assert.throws(function () {
  64. file.cpR('foo.txt', 'foo.txt', {silent: true});
  65. });
  66. }
  67. , 'test cpR rename via copy in directory': function () {
  68. file.mkdirP('foo', {silent: true});
  69. fs.writeFileSync('foo/bar.txt', 'w00t');
  70. file.cpR('foo/bar.txt', 'foo/baz.txt', {silent: true});
  71. assert.ok(existsSync('foo/baz.txt'));
  72. file.rmRf('foo', {silent: true});
  73. }
  74. , 'test cpR rename via copy in base': function () {
  75. fs.writeFileSync('bar.txt', 'w00t');
  76. file.cpR('bar.txt', 'baz.txt', {silent: true});
  77. assert.ok(existsSync('baz.txt'));
  78. file.rmRf('bar.txt', {silent: true});
  79. file.rmRf('baz.txt', {silent: true});
  80. }
  81. , 'test readdirR': function () {
  82. var expected = [
  83. ['foo']
  84. , ['foo', 'bar']
  85. , ['foo', 'bar', 'baz']
  86. , ['foo', 'bar', 'baz', 'qux']
  87. ]
  88. , res;
  89. file.mkdirP('foo/bar/baz/qux', {silent: true});
  90. res = file.readdirR('foo');
  91. for (var i = 0, ii = res.length; i < ii; i++) {
  92. assert.equal(path.join.apply(path, expected[i]), res[i]);
  93. }
  94. file.rmRf('foo', {silent: true});
  95. }
  96. , 'test isAbsolute with Unix absolute path': function () {
  97. var p = '/foo/bar/baz';
  98. assert.equal('/', file.isAbsolute(p));
  99. }
  100. , 'test isAbsolute with Unix relative path': function () {
  101. var p = 'foo/bar/baz';
  102. assert.equal(false, file.isAbsolute(p));
  103. }
  104. , 'test isAbsolute with Win absolute path': function () {
  105. var p = 'C:\\foo\\bar\\baz';
  106. assert.equal('C:\\', file.isAbsolute(p));
  107. }
  108. , 'test isAbsolute with Win relative path': function () {
  109. var p = 'foo\\bar\\baz';
  110. assert.equal(false, file.isAbsolute(p));
  111. }
  112. , 'test absolutize with Unix absolute path': function () {
  113. var expected = '/foo/bar/baz'
  114. , actual = file.absolutize('/foo/bar/baz');
  115. assert.equal(expected, actual);
  116. }
  117. , 'test absolutize with Win absolute path': function () {
  118. var expected = 'C:\\foo\\bar\\baz'
  119. , actual = file.absolutize('C:\\foo\\bar\\baz');
  120. assert.equal(expected, actual);
  121. }
  122. , 'test absolutize with relative path': function () {
  123. var expected = process.cwd()
  124. , actual = '';
  125. // We can't just create two different tests here
  126. // because file.absolutize uses process.cwd()
  127. // to get absolute path which is platform
  128. // specific
  129. if (process.platform === 'win32') {
  130. expected += '\\foo\\bar\\baz'
  131. actual = file.absolutize('foo\\bar\\baz')
  132. }
  133. else {
  134. expected += '/foo/bar/baz'
  135. actual = file.absolutize('foo/bar/baz');
  136. }
  137. assert.equal(expected, actual);
  138. }
  139. , 'test basedir with Unix absolute path': function () {
  140. var p = '/foo/bar/baz';
  141. assert.equal('/foo/bar', file.basedir(p));
  142. }
  143. , 'test basedir with Win absolute path': function () {
  144. var p = 'C:\\foo\\bar\\baz';
  145. assert.equal('C:\\foo\\bar', file.basedir(p));
  146. }
  147. , 'test basedir with Unix root path': function () {
  148. var p = '/';
  149. assert.equal('/', file.basedir(p));
  150. }
  151. , 'test basedir with Unix absolute path and double-asterisk': function () {
  152. var p = '/**/foo/bar/baz';
  153. assert.equal('/', file.basedir(p));
  154. }
  155. , 'test basedir with leading double-asterisk': function () {
  156. var p = '**/foo';
  157. assert.equal('.', file.basedir(p));
  158. }
  159. , 'test basedir with leading asterisk': function () {
  160. var p = '*.js';
  161. assert.equal('.', file.basedir(p));
  162. }
  163. , 'test basedir with leading dot-slash and double-asterisk': function () {
  164. var p = './**/foo';
  165. assert.equal('.', file.basedir(p));
  166. }
  167. , 'test basedir with leading dirname and double-asterisk': function () {
  168. var p = 'a/**/*.js';
  169. assert.equal('a', file.basedir(p));
  170. }
  171. , 'test basedir with leading dot-dot-slash and double-asterisk': function () {
  172. var p = '../../test/**/*.js';
  173. assert.equal('../../test', file.basedir(p));
  174. }
  175. , 'test basedir with single-asterisk in dirname': function () {
  176. var p = 'a/test*/file';
  177. assert.equal('a', file.basedir(p));
  178. }
  179. , 'test basedir with single filename': function () {
  180. var p = 'filename';
  181. assert.equal('.', file.basedir(p));
  182. }
  183. , 'test basedir with empty path': function () {
  184. var p = '';
  185. assert.equal('.', file.basedir(p));
  186. assert.equal('.', file.basedir());
  187. }
  188. };
  189. module.exports = tests;