copy.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. var fs = require('fs'),
  2. path = require('path'),
  3. vfs = require('../lib/fs'),
  4. TEST_DIR = path.join(__dirname, 'test-dir');
  5. module.exports = {
  6. setUp : function(done) {
  7. fs.mkdirSync(TEST_DIR);
  8. done();
  9. },
  10. tearDown : function(done) {
  11. fs.rmdirSync(TEST_DIR);
  12. done();
  13. },
  14. 'should copy file' : function(test) {
  15. var sourceFile = path.join(TEST_DIR, 'source-file'),
  16. targetFile = path.join(TEST_DIR, 'target-file');
  17. fs.writeFileSync(sourceFile, 'source');
  18. vfs.copy(sourceFile, targetFile)
  19. .then(function() {
  20. test.equal(fs.readFileSync(targetFile, 'utf8'), 'source');
  21. })
  22. .always(function() {
  23. fs.unlinkSync(sourceFile);
  24. fs.unlinkSync(targetFile);
  25. test.done();
  26. });
  27. },
  28. 'should copy file if target exists' : function(test) {
  29. var sourceFile = path.join(TEST_DIR, 'source-file'),
  30. targetFile = path.join(TEST_DIR, 'target-file');
  31. fs.writeFileSync(sourceFile, 'source');
  32. fs.writeFileSync(targetFile, 'target');
  33. vfs.copy(sourceFile, targetFile)
  34. .then(function() {
  35. test.equal(fs.readFileSync(targetFile, 'utf8'), 'source');
  36. })
  37. .always(function() {
  38. fs.unlinkSync(sourceFile);
  39. fs.unlinkSync(targetFile);
  40. test.done();
  41. });
  42. },
  43. 'should not copy file if source is directory' : function(test) {
  44. var sourceDir = path.join(TEST_DIR, 'source-dir'),
  45. targetFile = path.join(TEST_DIR, 'target-file');
  46. fs.mkdirSync(sourceDir);
  47. vfs.copy(sourceDir, targetFile)
  48. .then(
  49. function() {
  50. test.ok(false);
  51. },
  52. function(err) {
  53. test.equal(err.code, 'EISDIR');
  54. })
  55. .always(function() {
  56. fs.rmdirSync(sourceDir);
  57. test.done();
  58. });
  59. },
  60. 'should not copy file if target is directory' : function(test) {
  61. var sourceFile = path.join(TEST_DIR, 'source-file'),
  62. targetDir = path.join(TEST_DIR, 'target-dir');
  63. fs.writeFileSync(sourceFile, 'source');
  64. fs.mkdirSync(targetDir);
  65. vfs.copy(sourceFile, targetDir)
  66. .then(
  67. function() {
  68. test.ok(false);
  69. },
  70. function(err) {
  71. test.equal(err.code, 'EISDIR');
  72. })
  73. .always(function() {
  74. fs.unlinkSync(sourceFile);
  75. fs.rmdirSync(targetDir);
  76. test.done();
  77. });
  78. },
  79. 'should not copy if target not exists' : function(test) {
  80. var sourceFile = path.join(TEST_DIR, 'source-file'),
  81. targetFile = path.join(TEST_DIR, 'target-file');
  82. vfs.copy(sourceFile, targetFile)
  83. .then(
  84. function() {
  85. test.ok(false);
  86. },
  87. function(err) {
  88. test.equal(err.code, 'ENOENT');
  89. })
  90. .always(function() {
  91. test.done();
  92. });
  93. }
  94. };