Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. var assert = require('assert');
  2. var fs = require('fs');
  3. var rimraf = require('rimraf');
  4. var describe = global.describe;
  5. var it = global.it;
  6. var mv = require('../');
  7. var realFsRename = fs.rename;
  8. function overrideFsRename() {
  9. // makes fs.rename return cross-device error.
  10. fs.rename = function(src, dest, cb) {
  11. setTimeout(function() {
  12. var err = new Error();
  13. err.code = 'EXDEV';
  14. cb(err);
  15. }, 10);
  16. };
  17. }
  18. function restoreFsRename() {
  19. fs.rename = realFsRename;
  20. }
  21. describe("mv", function() {
  22. it("should rename a file on the same device", function (done) {
  23. mv("test/a-file", "test/a-file-dest", function (err) {
  24. assert.ifError(err);
  25. fs.readFile("test/a-file-dest", 'utf8', function (err, contents) {
  26. assert.ifError(err);
  27. assert.strictEqual(contents, "sonic the hedgehog\n");
  28. // move it back
  29. mv("test/a-file-dest", "test/a-file", done);
  30. });
  31. });
  32. });
  33. it("should not overwrite if clobber = false", function (done) {
  34. mv("test/a-file", "test/a-folder/another-file", {clobber: false}, function (err) {
  35. assert.ok(err && err.code === 'EEXIST', "throw EEXIST");
  36. done();
  37. });
  38. });
  39. it("should not create directory structure by default", function (done) {
  40. mv("test/a-file", "test/does/not/exist/a-file-dest", function (err) {
  41. assert.strictEqual(err.code, 'ENOENT');
  42. done();
  43. });
  44. });
  45. it("should create directory structure when mkdirp option set", function (done) {
  46. mv("test/a-file", "test/does/not/exist/a-file-dest", {mkdirp: true}, function (err) {
  47. assert.ifError(err);
  48. fs.readFile("test/does/not/exist/a-file-dest", 'utf8', function (err, contents) {
  49. assert.ifError(err);
  50. assert.strictEqual(contents, "sonic the hedgehog\n");
  51. // move it back
  52. mv("test/does/not/exist/a-file-dest", "test/a-file", function(err) {
  53. assert.ifError(err);
  54. rimraf("test/does", { disableGlob: true }, done);
  55. });
  56. });
  57. });
  58. });
  59. it("should work across devices", function (done) {
  60. overrideFsRename();
  61. mv("test/a-file", "test/a-file-dest", function (err) {
  62. assert.ifError(err);
  63. fs.readFile("test/a-file-dest", 'utf8', function (err, contents) {
  64. assert.ifError(err);
  65. assert.strictEqual(contents, "sonic the hedgehog\n");
  66. // move it back
  67. mv("test/a-file-dest", "test/a-file", function(err) {
  68. restoreFsRename();
  69. done(err);
  70. });
  71. });
  72. });
  73. });
  74. it("should work across devices, even with special characters", function (done) {
  75. overrideFsRename();
  76. mv("test/a-file", "test/a-*", function (err) {
  77. assert.ifError(err);
  78. fs.readFile("test/a-*", 'utf8', function (err, contents) {
  79. assert.ifError(err);
  80. assert.strictEqual(contents, "sonic the hedgehog\n");
  81. // move it back
  82. mv("test/a-*", "test/a-file", function(err) {
  83. assert.ifError(err);
  84. fs.readFile("test/a-file", 'utf8', function (err, contents) {
  85. assert.ifError(err);
  86. assert.strictEqual(contents, "sonic the hedgehog\n");
  87. restoreFsRename();
  88. done(err);
  89. });
  90. });
  91. });
  92. });
  93. });
  94. it("should move folders", function (done) {
  95. mv("test/a-folder", "test/a-folder-dest", function (err) {
  96. assert.ifError(err);
  97. fs.readFile("test/a-folder-dest/another-file", 'utf8', function (err, contents) {
  98. assert.ifError(err);
  99. assert.strictEqual(contents, "tails\n");
  100. // move it back
  101. mv("test/a-folder-dest", "test/a-folder", done);
  102. });
  103. });
  104. });
  105. it("should move folders across devices", function (done) {
  106. overrideFsRename();
  107. mv("test/a-folder", "test/a-folder-dest", function (err) {
  108. assert.ifError(err);
  109. fs.readFile("test/a-folder-dest/another-folder/file3", 'utf8', function (err, contents) {
  110. assert.ifError(err);
  111. assert.strictEqual(contents, "knuckles\n");
  112. // move it back
  113. mv("test/a-folder-dest", "test/a-folder", function(err) {
  114. restoreFsRename();
  115. done(err);
  116. });
  117. });
  118. });
  119. });
  120. it("should move folders across devices, even with special characters", function (done) {
  121. overrideFsRename();
  122. mv("test/a-folder", "test/a-*", function (err) {
  123. assert.ifError(err);
  124. fs.readFile("test/a-*/another-folder/file3", 'utf8', function (err, contents) {
  125. assert.ifError(err);
  126. assert.strictEqual(contents, "knuckles\n");
  127. // move it back
  128. mv("test/a-*", "test/a-folder", function(err) {
  129. assert.ifError(err);
  130. fs.readFile("test/a-folder/another-folder/file3", 'utf8', function (err, contents) {
  131. assert.ifError(err);
  132. assert.strictEqual(contents, "knuckles\n");
  133. restoreFsRename();
  134. done(err);
  135. });
  136. });
  137. });
  138. });
  139. });
  140. });