PathException when cannot copy
1 import dshould; 2 auto cdir = std.file.getcwd; 3 Path root = createTempPath(); 4 scope(exit) { 5 std.file.chdir(cdir); 6 root.remove(); 7 } 8 9 auto test_c_file = root.join("test-create.txt"); 10 11 // Create test file to copy 12 test_c_file.exists.should.be(false); 13 test_c_file.writeFile("Hello World"); 14 test_c_file.exists.should.be(true); 15 16 // Test copy file when dest dir does not exists 17 test_c_file.copyTo( 18 root.join("test-copy-dst", "test.txt") 19 ).should.throwA!(std.file.FileException); 20 21 // Test copy file where dest dir exists and dest name specified 22 root.join("test-copy-dst").exists().should.be(false); 23 root.join("test-copy-dst").mkdir(); 24 root.join("test-copy-dst").exists().should.be(true); 25 root.join("test-copy-dst", "test.txt").exists.should.be(false); 26 test_c_file.copyTo(root.join("test-copy-dst", "test.txt")); 27 root.join("test-copy-dst", "test.txt").exists.should.be(true); 28 29 // Try to copy file when it is already exists in dest folder 30 test_c_file.copyTo( 31 root.join("test-copy-dst", "test.txt") 32 ).should.throwA!PathException; 33 34 // Try to copy file, when only dirname specified 35 root.join("test-copy-dst", "test-create.txt").exists.should.be(false); 36 test_c_file.copyTo(root.join("test-copy-dst")); 37 root.join("test-copy-dst", "test-create.txt").exists.should.be(true); 38 39 // Try to copy empty directory with its content 40 root.join("test-copy-dir-empty").mkdir; 41 root.join("test-copy-dir-empty").exists.should.be(true); 42 root.join("test-copy-dir-empty-cpy").exists.should.be(false); 43 root.join("test-copy-dir-empty").copyTo( 44 root.join("test-copy-dir-empty-cpy")); 45 root.join("test-copy-dir-empty").exists.should.be(true); 46 root.join("test-copy-dir-empty-cpy").exists.should.be(true); 47 48 // Create test dir with content to test copying non-empty directory 49 root.join("test-dir").mkdir(); 50 root.join("test-dir", "f1.txt").writeFile("f1"); 51 root.join("test-dir", "d2").mkdir(); 52 root.join("test-dir", "d2", "f2.txt").writeFile("f2"); 53 54 // Test that test-dir content created 55 root.join("test-dir").exists.should.be(true); 56 root.join("test-dir").isDir.should.be(true); 57 root.join("test-dir", "f1.txt").exists.should.be(true); 58 root.join("test-dir", "f1.txt").isFile.should.be(true); 59 root.join("test-dir", "d2").exists.should.be(true); 60 root.join("test-dir", "d2").isDir.should.be(true); 61 root.join("test-dir", "d2", "f2.txt").exists.should.be(true); 62 root.join("test-dir", "d2", "f2.txt").isFile.should.be(true); 63 64 // Copy non-empty dir to unexisting location 65 root.join("test-dir-cpy-1").exists.should.be(false); 66 root.join("test-dir").copyTo(root.join("test-dir-cpy-1")); 67 68 // Test that dir copied successfully 69 root.join("test-dir-cpy-1").exists.should.be(true); 70 root.join("test-dir-cpy-1").isDir.should.be(true); 71 root.join("test-dir-cpy-1", "f1.txt").exists.should.be(true); 72 root.join("test-dir-cpy-1", "f1.txt").isFile.should.be(true); 73 root.join("test-dir-cpy-1", "d2").exists.should.be(true); 74 root.join("test-dir-cpy-1", "d2").isDir.should.be(true); 75 root.join("test-dir-cpy-1", "d2", "f2.txt").exists.should.be(true); 76 root.join("test-dir-cpy-1", "d2", "f2.txt").isFile.should.be(true); 77 78 // Copy non-empty dir to existing location 79 root.join("test-dir-cpy-2").exists.should.be(false); 80 root.join("test-dir-cpy-2").mkdir; 81 root.join("test-dir-cpy-2").exists.should.be(true); 82 83 // Copy directory to already existing dir 84 root.join("test-dir").copyTo(root.join("test-dir-cpy-2")); 85 86 // Test that dir copied successfully 87 root.join("test-dir-cpy-2", "test-dir").exists.should.be(true); 88 root.join("test-dir-cpy-2", "test-dir").isDir.should.be(true); 89 root.join("test-dir-cpy-2", "test-dir", "f1.txt").exists.should.be(true); 90 root.join("test-dir-cpy-2", "test-dir", "f1.txt").isFile.should.be(true); 91 root.join("test-dir-cpy-2", "test-dir", "d2").exists.should.be(true); 92 root.join("test-dir-cpy-2", "test-dir", "d2").isDir.should.be(true); 93 root.join("test-dir-cpy-2", "test-dir", "d2", "f2.txt").exists.should.be(true); 94 root.join("test-dir-cpy-2", "test-dir", "d2", "f2.txt").isFile.should.be(true); 95 96 // Try again to copy non-empty dir to already existing dir 97 // where dir with same base name already exists 98 root.join("test-dir").copyTo(root.join("test-dir-cpy-2")).should.throwA!PathException; 99 100 101 // Change dir to our temp directory and test copying using 102 // relative paths 103 root.chdir; 104 105 // Copy content using relative paths 106 root.join("test-dir-cpy-3").exists.should.be(false); 107 Path("test-dir-cpy-3").exists.should.be(false); 108 Path("test-dir").copyTo("test-dir-cpy-3"); 109 110 // Test that content was copied in right way 111 root.join("test-dir-cpy-3").exists.should.be(true); 112 root.join("test-dir-cpy-3").isDir.should.be(true); 113 root.join("test-dir-cpy-3", "f1.txt").exists.should.be(true); 114 root.join("test-dir-cpy-3", "f1.txt").isFile.should.be(true); 115 root.join("test-dir-cpy-3", "d2").exists.should.be(true); 116 root.join("test-dir-cpy-3", "d2").isDir.should.be(true); 117 root.join("test-dir-cpy-3", "d2", "f2.txt").exists.should.be(true); 118 root.join("test-dir-cpy-3", "d2", "f2.txt").isFile.should.be(true); 119 120 // Try to copy to already existing file 121 root.join("test-dir-cpy-4").writeFile("Test"); 122 123 // Expect error 124 root.join("test-dir").copyTo("test-dir-cpy-4").should.throwA!PathException; 125 126 version(Posix) { 127 // Prepare test dir in user's home directory 128 Path home_tmp = createTempPath("~", "tmp-d-test"); 129 scope(exit) home_tmp.remove(); 130 131 // Test if home_tmp created in right way and ensure that 132 // dest for copy dir does not exists 133 home_tmp.parent.toString.should.equal(std.path.expandTilde("~")); 134 home_tmp.isAbsolute.should.be(true); 135 home_tmp.join("test-dir").exists.should.be(false); 136 137 // Copy test-dir to home_tmp 138 import std.algorithm: startsWith; 139 auto home_tmp_rel = home_tmp.baseName; 140 string home_tmp_tilde = "~/%s".format(home_tmp_rel); 141 home_tmp_tilde.startsWith("~/tmp-d-test").should.be(true); 142 root.join("test-dir").copyTo(home_tmp_tilde); 143 144 // Test that content was copied in right way 145 home_tmp.join("test-dir").exists.should.be(true); 146 home_tmp.join("test-dir").isDir.should.be(true); 147 home_tmp.join("test-dir", "f1.txt").exists.should.be(true); 148 home_tmp.join("test-dir", "f1.txt").isFile.should.be(true); 149 home_tmp.join("test-dir", "d2").exists.should.be(true); 150 home_tmp.join("test-dir", "d2").isDir.should.be(true); 151 home_tmp.join("test-dir", "d2", "f2.txt").exists.should.be(true); 152 home_tmp.join("test-dir", "d2", "f2.txt").isFile.should.be(true); 153 } 154 155 156
Copy file or directory to destination If source is a file, then copyFileTo will be use to copy it. If source is a directory, then more complex logic will be applied: - if dest already exists and it is not dir, then exception will be raised. - if dest already exists and it is dir, then source dir will be copied inseide that dir with it's name - if dest does not exists, then current directory will be copied to dest path.
Note, that work with symlinks have to be improved. Not tested yet.