move.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const { expect } = require("chai");
  2. const fs = require('fs');
  3. module.exports = {
  4. name: 'move',
  5. do: async t => {
  6. // setup conditions for tests
  7. await t.mkdir('dir_with_contents');
  8. await t.write('dir_with_contents/a.txt', 'move test\n');
  9. await t.write('dir_with_contents/b.txt', 'move test\n');
  10. await t.write('dir_with_contents/c.txt', 'move test\n');
  11. await t.mkdir('dir_with_contents/q');
  12. await t.mkdir('dir_with_contents/w');
  13. await t.mkdir('dir_with_contents/e');
  14. await t.mkdir('dir_no_contents');
  15. await t.write('just_a_file.txt', 'move test\n');
  16. await t.case('move file', async () => {
  17. await t.move('just_a_file.txt', 'just_a_file_moved.txt');
  18. const moved = await t.stat('just_a_file_moved.txt');
  19. let threw = false;
  20. try {
  21. await t.stat('just_a_file.txt');
  22. } catch (e) {
  23. expect(e.response.status).equal(404);
  24. threw = true;
  25. }
  26. expect(threw).true;
  27. expect(moved.name).equal('just_a_file_moved.txt');
  28. });
  29. await t.case('move file to existing file', async () => {
  30. await t.write('just_a_file.txt', 'move test\n');
  31. let threw = false;
  32. try {
  33. await t.move('just_a_file.txt', 'dir_with_contents/a.txt');
  34. } catch (e) {
  35. expect(e.response.status).equal(409);
  36. threw = true;
  37. }
  38. expect(threw).true;
  39. });
  40. /*
  41. await t.case('move file to existing directory', async () => {
  42. await t.move('just_a_file.txt', 'dir_with_contents');
  43. const moved = await t.stat('dir_with_contents/just_a_file.txt');
  44. let threw = false;
  45. try {
  46. await t.stat('just_a_file.txt');
  47. } catch (e) {
  48. expect(e.response.status).equal(404);
  49. threw = true;
  50. }
  51. expect(threw).true;
  52. expect(moved.name).equal('just_a_file.txt');
  53. });
  54. */
  55. await t.case('move directory', async () => {
  56. await t.move('dir_no_contents', 'dir_no_contents_moved');
  57. const moved = await t.stat('dir_no_contents_moved');
  58. let threw = false;
  59. try {
  60. await t.stat('dir_no_contents');
  61. } catch (e) {
  62. expect(e.response.status).equal(404);
  63. threw = true;
  64. }
  65. expect(threw).true;
  66. expect(moved.name).equal('dir_no_contents_moved');
  67. });
  68. await t.case('move file and create parents', async () => {
  69. await t.write('just_a_file.txt', 'move test\n', { overwrite: true });
  70. const res = await t.move(
  71. 'just_a_file.txt',
  72. 'dir_with_contents/q/w/e/just_a_file.txt',
  73. { create_missing_parents: true }
  74. );
  75. expect(res.parent_dirs_created).length(2);
  76. const moved = await t.stat('dir_with_contents/q/w/e/just_a_file.txt');
  77. let threw = false;
  78. try {
  79. await t.stat('just_a_file.txt');
  80. } catch (e) {
  81. expect(e.response.status).equal(404);
  82. threw = true;
  83. }
  84. expect(threw).true;
  85. expect(moved.name).equal('just_a_file.txt');
  86. });
  87. }
  88. };