Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | const CWD = do_get_cwd(); |
michael@0 | 2 | function checkOS(os) { |
michael@0 | 3 | const nsILocalFile_ = "nsILocalFile" + os; |
michael@0 | 4 | return nsILocalFile_ in Components.interfaces && |
michael@0 | 5 | CWD instanceof Components.interfaces[nsILocalFile_]; |
michael@0 | 6 | } |
michael@0 | 7 | |
michael@0 | 8 | const DIR_TARGET = "target"; |
michael@0 | 9 | const DIR_LINK = "link"; |
michael@0 | 10 | const DIR_LINK_LINK = "link_link"; |
michael@0 | 11 | const FILE_TARGET = "target.txt"; |
michael@0 | 12 | const FILE_LINK = "link.txt"; |
michael@0 | 13 | const FILE_LINK_LINK = "link_link.txt"; |
michael@0 | 14 | |
michael@0 | 15 | const DOES_NOT_EXIST = "doesnotexist"; |
michael@0 | 16 | const DANGLING_LINK = "dangling_link"; |
michael@0 | 17 | const LOOP_LINK = "loop_link"; |
michael@0 | 18 | |
michael@0 | 19 | const isWin = checkOS("Win"); |
michael@0 | 20 | const isMac = checkOS("Mac"); |
michael@0 | 21 | const isUnix = !(isWin || isMac); |
michael@0 | 22 | |
michael@0 | 23 | const nsIFile = Components.interfaces.nsIFile; |
michael@0 | 24 | |
michael@0 | 25 | var process; |
michael@0 | 26 | function createSymLink(from, to) { |
michael@0 | 27 | if (!process) { |
michael@0 | 28 | var ln = Components.classes["@mozilla.org/file/local;1"] |
michael@0 | 29 | .createInstance(Components.interfaces.nsILocalFile); |
michael@0 | 30 | ln.initWithPath("/bin/ln"); |
michael@0 | 31 | |
michael@0 | 32 | process = Components.classes["@mozilla.org/process/util;1"] |
michael@0 | 33 | .createInstance(Components.interfaces.nsIProcess); |
michael@0 | 34 | process.init(ln); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | const args = ["-s", from, to]; |
michael@0 | 38 | process.run(true, args, args.length); |
michael@0 | 39 | do_check_eq(process.exitValue, 0); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | function makeSymLink(from, toName, relative) { |
michael@0 | 43 | var to = from.parent; |
michael@0 | 44 | to.append(toName); |
michael@0 | 45 | |
michael@0 | 46 | if (relative) { |
michael@0 | 47 | createSymLink(from.leafName, to.path); |
michael@0 | 48 | } |
michael@0 | 49 | else { |
michael@0 | 50 | createSymLink(from.path, to.path); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | do_check_true(to.isSymlink()); |
michael@0 | 54 | |
michael@0 | 55 | print("---"); |
michael@0 | 56 | print(from.path); |
michael@0 | 57 | print(to.path); |
michael@0 | 58 | print(to.target); |
michael@0 | 59 | |
michael@0 | 60 | if (from.leafName != DOES_NOT_EXIST && from.isSymlink()) { |
michael@0 | 61 | // XXXjag wish I could set followLinks to false so we'd just get |
michael@0 | 62 | // the symlink's direct target instead of the final target. |
michael@0 | 63 | do_check_eq(from.target, to.target); |
michael@0 | 64 | } |
michael@0 | 65 | else { |
michael@0 | 66 | do_check_eq(from.path, to.target); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | return to; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | function setupTestDir(testDir, relative) { |
michael@0 | 73 | var targetDir = testDir.clone(); |
michael@0 | 74 | targetDir.append(DIR_TARGET); |
michael@0 | 75 | |
michael@0 | 76 | if (testDir.exists()) { |
michael@0 | 77 | testDir.remove(true); |
michael@0 | 78 | } |
michael@0 | 79 | do_check_true(!testDir.exists()); |
michael@0 | 80 | |
michael@0 | 81 | testDir.create(nsIFile.DIRECTORY_TYPE, 0777); |
michael@0 | 82 | |
michael@0 | 83 | targetDir.create(nsIFile.DIRECTORY_TYPE, 0777); |
michael@0 | 84 | |
michael@0 | 85 | var targetFile = testDir.clone(); |
michael@0 | 86 | targetFile.append(FILE_TARGET); |
michael@0 | 87 | targetFile.create(nsIFile.NORMAL_FILE_TYPE, 0666); |
michael@0 | 88 | |
michael@0 | 89 | var imaginary = testDir.clone(); |
michael@0 | 90 | imaginary.append(DOES_NOT_EXIST); |
michael@0 | 91 | |
michael@0 | 92 | var loop = testDir.clone(); |
michael@0 | 93 | loop.append(LOOP_LINK); |
michael@0 | 94 | |
michael@0 | 95 | var dirLink = makeSymLink(targetDir, DIR_LINK, relative); |
michael@0 | 96 | var fileLink = makeSymLink(targetFile, FILE_LINK, relative); |
michael@0 | 97 | |
michael@0 | 98 | makeSymLink(dirLink, DIR_LINK_LINK, relative); |
michael@0 | 99 | makeSymLink(fileLink, FILE_LINK_LINK, relative); |
michael@0 | 100 | |
michael@0 | 101 | makeSymLink(imaginary, DANGLING_LINK, relative); |
michael@0 | 102 | |
michael@0 | 103 | try { |
michael@0 | 104 | makeSymLink(loop, LOOP_LINK, relative); |
michael@0 | 105 | do_check_true(false); |
michael@0 | 106 | } |
michael@0 | 107 | catch (e) { |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | function createSpaces(dirs, files, links) { |
michael@0 | 112 | function longest(a, b) a.length > b.length ? a : b; |
michael@0 | 113 | return dirs.concat(files, links).reduce(longest, "").replace(/./g, " "); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | function testSymLinks(testDir, relative) { |
michael@0 | 117 | setupTestDir(testDir, relative); |
michael@0 | 118 | |
michael@0 | 119 | const dirLinks = [DIR_LINK, DIR_LINK_LINK]; |
michael@0 | 120 | const fileLinks = [FILE_LINK, FILE_LINK_LINK]; |
michael@0 | 121 | const otherLinks = [DANGLING_LINK, LOOP_LINK]; |
michael@0 | 122 | const dirs = [DIR_TARGET].concat(dirLinks); |
michael@0 | 123 | const files = [FILE_TARGET].concat(fileLinks); |
michael@0 | 124 | const links = otherLinks.concat(dirLinks, fileLinks); |
michael@0 | 125 | |
michael@0 | 126 | const spaces = createSpaces(dirs, files, links); |
michael@0 | 127 | const bools = {false: " false", true: " true "}; |
michael@0 | 128 | print(spaces + " dir file symlink"); |
michael@0 | 129 | var dirEntries = testDir.directoryEntries; |
michael@0 | 130 | while (dirEntries.hasMoreElements()) { |
michael@0 | 131 | const file = dirEntries.getNext().QueryInterface(nsIFile); |
michael@0 | 132 | const name = file.leafName; |
michael@0 | 133 | print(name + spaces.substring(name.length) + bools[file.isDirectory()] + |
michael@0 | 134 | bools[file.isFile()] + bools[file.isSymlink()]); |
michael@0 | 135 | do_check_eq(file.isDirectory(), dirs.indexOf(name) != -1); |
michael@0 | 136 | do_check_eq(file.isFile(), files.indexOf(name) != -1); |
michael@0 | 137 | do_check_eq(file.isSymlink(), links.indexOf(name) != -1); |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | function run_test() { |
michael@0 | 142 | // Skip this test on Windows |
michael@0 | 143 | if (isWin) |
michael@0 | 144 | return; |
michael@0 | 145 | |
michael@0 | 146 | var testDir = CWD; |
michael@0 | 147 | testDir.append("test_symlinks"); |
michael@0 | 148 | |
michael@0 | 149 | testSymLinks(testDir, false); |
michael@0 | 150 | testSymLinks(testDir, true); |
michael@0 | 151 | } |