Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | const { pathFor, platform } = require("sdk/system"); |
michael@0 | 8 | const fs = require("sdk/io/fs"); |
michael@0 | 9 | const url = require("sdk/url"); |
michael@0 | 10 | const path = require("sdk/fs/path"); |
michael@0 | 11 | const { defer } = require("sdk/core/promise"); |
michael@0 | 12 | const { Buffer } = require("sdk/io/buffer"); |
michael@0 | 13 | const { is } = require("sdk/system/xul-app"); |
michael@0 | 14 | |
michael@0 | 15 | // Use profile directory to list / read / write files. |
michael@0 | 16 | const profilePath = pathFor("ProfD"); |
michael@0 | 17 | const fileNameInProfile = "compatibility.ini"; |
michael@0 | 18 | const dirNameInProfile = "extensions"; |
michael@0 | 19 | const filePathInProfile = path.join(profilePath, fileNameInProfile); |
michael@0 | 20 | const dirPathInProfile = path.join(profilePath, dirNameInProfile); |
michael@0 | 21 | const mkdirPath = path.join(profilePath, "sdk-fixture-mkdir"); |
michael@0 | 22 | const writePath = path.join(profilePath, "sdk-fixture-writeFile"); |
michael@0 | 23 | const unlinkPath = path.join(profilePath, "sdk-fixture-unlink"); |
michael@0 | 24 | const truncatePath = path.join(profilePath, "sdk-fixture-truncate"); |
michael@0 | 25 | const renameFromPath = path.join(profilePath, "sdk-fixture-rename-from"); |
michael@0 | 26 | const renameToPath = path.join(profilePath, "sdk-fixture-rename-to"); |
michael@0 | 27 | const chmodPath = path.join(profilePath, "sdk-fixture-chmod"); |
michael@0 | 28 | |
michael@0 | 29 | const profileEntries = [ |
michael@0 | 30 | "compatibility.ini", |
michael@0 | 31 | "extensions", |
michael@0 | 32 | "prefs.js" |
michael@0 | 33 | // There are likely to be a lot more files but we can"t really |
michael@0 | 34 | // on consistent list so we limit to this. |
michael@0 | 35 | ]; |
michael@0 | 36 | |
michael@0 | 37 | const isWindows = platform.indexOf('win') === 0; |
michael@0 | 38 | |
michael@0 | 39 | exports["test readdir"] = function(assert, end) { |
michael@0 | 40 | var async = false; |
michael@0 | 41 | fs.readdir(profilePath, function(error, entries) { |
michael@0 | 42 | assert.ok(async, "readdir is async"); |
michael@0 | 43 | assert.ok(!error, "there is no error when reading directory"); |
michael@0 | 44 | assert.ok(profileEntries.length <= entries.length, |
michael@0 | 45 | "got at least number of entries we expect"); |
michael@0 | 46 | assert.ok(profileEntries.every(function(entry) { |
michael@0 | 47 | return entries.indexOf(entry) >= 0; |
michael@0 | 48 | }), "all profiles are present"); |
michael@0 | 49 | end(); |
michael@0 | 50 | }); |
michael@0 | 51 | |
michael@0 | 52 | async = true; |
michael@0 | 53 | }; |
michael@0 | 54 | |
michael@0 | 55 | exports["test readdir error"] = function(assert, end) { |
michael@0 | 56 | var async = false; |
michael@0 | 57 | var path = profilePath + "-does-not-exists"; |
michael@0 | 58 | fs.readdir(path, function(error, entries) { |
michael@0 | 59 | assert.ok(async, "readdir is async"); |
michael@0 | 60 | assert.equal(error.message, "ENOENT, readdir " + path); |
michael@0 | 61 | assert.equal(error.code, "ENOENT", "error has a code"); |
michael@0 | 62 | assert.equal(error.path, path, "error has a path"); |
michael@0 | 63 | assert.equal(error.errno, 34, "error has a errno"); |
michael@0 | 64 | end(); |
michael@0 | 65 | }); |
michael@0 | 66 | |
michael@0 | 67 | async = true; |
michael@0 | 68 | }; |
michael@0 | 69 | |
michael@0 | 70 | exports["test readdirSync"] = function(assert) { |
michael@0 | 71 | var async = false; |
michael@0 | 72 | var entries = fs.readdirSync(profilePath); |
michael@0 | 73 | assert.ok(profileEntries.length <= entries.length, |
michael@0 | 74 | "got at least number of entries we expect"); |
michael@0 | 75 | assert.ok(profileEntries.every(function(entry) { |
michael@0 | 76 | return entries.indexOf(entry) >= 0; |
michael@0 | 77 | }), "all profiles are present"); |
michael@0 | 78 | }; |
michael@0 | 79 | |
michael@0 | 80 | exports["test readdirSync error"] = function(assert) { |
michael@0 | 81 | var async = false; |
michael@0 | 82 | var path = profilePath + "-does-not-exists"; |
michael@0 | 83 | try { |
michael@0 | 84 | fs.readdirSync(path); |
michael@0 | 85 | assert.fail(Error("No error was thrown")); |
michael@0 | 86 | } catch (error) { |
michael@0 | 87 | assert.equal(error.message, "ENOENT, readdir " + path); |
michael@0 | 88 | assert.equal(error.code, "ENOENT", "error has a code"); |
michael@0 | 89 | assert.equal(error.path, path, "error has a path"); |
michael@0 | 90 | assert.equal(error.errno, 34, "error has a errno"); |
michael@0 | 91 | } |
michael@0 | 92 | }; |
michael@0 | 93 | |
michael@0 | 94 | exports["test readFile"] = function(assert, end) { |
michael@0 | 95 | let async = false; |
michael@0 | 96 | fs.readFile(filePathInProfile, function(error, content) { |
michael@0 | 97 | assert.ok(async, "readFile is async"); |
michael@0 | 98 | assert.ok(!error, "error is falsy"); |
michael@0 | 99 | |
michael@0 | 100 | assert.ok(Buffer.isBuffer(content), "readFile returns buffer"); |
michael@0 | 101 | assert.ok(typeof(content.length) === "number", "buffer has length"); |
michael@0 | 102 | assert.ok(content.toString().indexOf("[Compatibility]") >= 0, |
michael@0 | 103 | "content contains expected data"); |
michael@0 | 104 | end(); |
michael@0 | 105 | }); |
michael@0 | 106 | async = true; |
michael@0 | 107 | }; |
michael@0 | 108 | |
michael@0 | 109 | exports["test readFile error"] = function(assert, end) { |
michael@0 | 110 | let async = false; |
michael@0 | 111 | let path = filePathInProfile + "-does-not-exists"; |
michael@0 | 112 | fs.readFile(path, function(error, content) { |
michael@0 | 113 | assert.ok(async, "readFile is async"); |
michael@0 | 114 | assert.equal(error.message, "ENOENT, open " + path); |
michael@0 | 115 | assert.equal(error.code, "ENOENT", "error has a code"); |
michael@0 | 116 | assert.equal(error.path, path, "error has a path"); |
michael@0 | 117 | assert.equal(error.errno, 34, "error has a errno"); |
michael@0 | 118 | |
michael@0 | 119 | end(); |
michael@0 | 120 | }); |
michael@0 | 121 | async = true; |
michael@0 | 122 | }; |
michael@0 | 123 | |
michael@0 | 124 | exports["test readFileSync not implemented"] = function(assert) { |
michael@0 | 125 | let buffer = fs.readFileSync(filePathInProfile); |
michael@0 | 126 | assert.ok(buffer.toString().indexOf("[Compatibility]") >= 0, |
michael@0 | 127 | "read content"); |
michael@0 | 128 | }; |
michael@0 | 129 | |
michael@0 | 130 | exports["test fs.stat file"] = function(assert, end) { |
michael@0 | 131 | let async = false; |
michael@0 | 132 | let path = filePathInProfile; |
michael@0 | 133 | fs.stat(path, function(error, stat) { |
michael@0 | 134 | assert.ok(async, "fs.stat is async"); |
michael@0 | 135 | assert.ok(!error, "error is falsy"); |
michael@0 | 136 | assert.ok(!stat.isDirectory(), "not a dir"); |
michael@0 | 137 | assert.ok(stat.isFile(), "is a file"); |
michael@0 | 138 | assert.ok(!stat.isSymbolicLink(), "isn't a symlink"); |
michael@0 | 139 | assert.ok(typeof(stat.size) === "number", "size is a number"); |
michael@0 | 140 | assert.ok(stat.exists === true, "file exists"); |
michael@0 | 141 | assert.ok(typeof(stat.isBlockDevice()) === "boolean"); |
michael@0 | 142 | assert.ok(typeof(stat.isCharacterDevice()) === "boolean"); |
michael@0 | 143 | assert.ok(typeof(stat.isFIFO()) === "boolean"); |
michael@0 | 144 | assert.ok(typeof(stat.isSocket()) === "boolean"); |
michael@0 | 145 | assert.ok(typeof(stat.hidden) === "boolean"); |
michael@0 | 146 | assert.ok(typeof(stat.writable) === "boolean") |
michael@0 | 147 | assert.ok(stat.readable === true); |
michael@0 | 148 | |
michael@0 | 149 | end(); |
michael@0 | 150 | }); |
michael@0 | 151 | async = true; |
michael@0 | 152 | }; |
michael@0 | 153 | |
michael@0 | 154 | exports["test fs.stat dir"] = function(assert, end) { |
michael@0 | 155 | let async = false; |
michael@0 | 156 | let path = dirPathInProfile; |
michael@0 | 157 | fs.stat(path, function(error, stat) { |
michael@0 | 158 | assert.ok(async, "fs.stat is async"); |
michael@0 | 159 | assert.ok(!error, "error is falsy"); |
michael@0 | 160 | assert.ok(stat.isDirectory(), "is a dir"); |
michael@0 | 161 | assert.ok(!stat.isFile(), "not a file"); |
michael@0 | 162 | assert.ok(!stat.isSymbolicLink(), "isn't a symlink"); |
michael@0 | 163 | assert.ok(typeof(stat.size) === "number", "size is a number"); |
michael@0 | 164 | assert.ok(stat.exists === true, "file exists"); |
michael@0 | 165 | assert.ok(typeof(stat.isBlockDevice()) === "boolean"); |
michael@0 | 166 | assert.ok(typeof(stat.isCharacterDevice()) === "boolean"); |
michael@0 | 167 | assert.ok(typeof(stat.isFIFO()) === "boolean"); |
michael@0 | 168 | assert.ok(typeof(stat.isSocket()) === "boolean"); |
michael@0 | 169 | assert.ok(typeof(stat.hidden) === "boolean"); |
michael@0 | 170 | assert.ok(typeof(stat.writable) === "boolean") |
michael@0 | 171 | assert.ok(typeof(stat.readable) === "boolean"); |
michael@0 | 172 | |
michael@0 | 173 | end(); |
michael@0 | 174 | }); |
michael@0 | 175 | async = true; |
michael@0 | 176 | }; |
michael@0 | 177 | |
michael@0 | 178 | exports["test fs.stat error"] = function(assert, end) { |
michael@0 | 179 | let async = false; |
michael@0 | 180 | let path = filePathInProfile + "-does-not-exists"; |
michael@0 | 181 | fs.stat(path, function(error, stat) { |
michael@0 | 182 | assert.ok(async, "fs.stat is async"); |
michael@0 | 183 | assert.equal(error.message, "ENOENT, stat " + path); |
michael@0 | 184 | assert.equal(error.code, "ENOENT", "error has a code"); |
michael@0 | 185 | assert.equal(error.path, path, "error has a path"); |
michael@0 | 186 | assert.equal(error.errno, 34, "error has a errno"); |
michael@0 | 187 | |
michael@0 | 188 | end(); |
michael@0 | 189 | }); |
michael@0 | 190 | async = true; |
michael@0 | 191 | }; |
michael@0 | 192 | |
michael@0 | 193 | exports["test fs.exists NO"] = function(assert, end) { |
michael@0 | 194 | let async = false |
michael@0 | 195 | let path = filePathInProfile + "-does-not-exists"; |
michael@0 | 196 | fs.exists(path, function(error, value) { |
michael@0 | 197 | assert.ok(async, "fs.exists is async"); |
michael@0 | 198 | assert.ok(!error, "error is falsy"); |
michael@0 | 199 | assert.ok(!value, "file does not exists"); |
michael@0 | 200 | end(); |
michael@0 | 201 | }); |
michael@0 | 202 | async = true; |
michael@0 | 203 | }; |
michael@0 | 204 | |
michael@0 | 205 | exports["test fs.exists YES"] = function(assert, end) { |
michael@0 | 206 | let async = false |
michael@0 | 207 | let path = filePathInProfile |
michael@0 | 208 | fs.exists(path, function(error, value) { |
michael@0 | 209 | assert.ok(async, "fs.exists is async"); |
michael@0 | 210 | assert.ok(!error, "error is falsy"); |
michael@0 | 211 | assert.ok(value, "file exists"); |
michael@0 | 212 | end(); |
michael@0 | 213 | }); |
michael@0 | 214 | async = true; |
michael@0 | 215 | }; |
michael@0 | 216 | |
michael@0 | 217 | exports["test fs.exists NO"] = function(assert, end) { |
michael@0 | 218 | let async = false |
michael@0 | 219 | let path = filePathInProfile + "-does-not-exists"; |
michael@0 | 220 | fs.exists(path, function(error, value) { |
michael@0 | 221 | assert.ok(async, "fs.exists is async"); |
michael@0 | 222 | assert.ok(!error, "error is falsy"); |
michael@0 | 223 | assert.ok(!value, "file does not exists"); |
michael@0 | 224 | end(); |
michael@0 | 225 | }); |
michael@0 | 226 | async = true; |
michael@0 | 227 | }; |
michael@0 | 228 | |
michael@0 | 229 | exports["test fs.existsSync"] = function(assert) { |
michael@0 | 230 | let path = filePathInProfile |
michael@0 | 231 | assert.equal(fs.existsSync(path), true, "exists"); |
michael@0 | 232 | assert.equal(fs.existsSync(path + "-does-not-exists"), false, "exists"); |
michael@0 | 233 | }; |
michael@0 | 234 | |
michael@0 | 235 | exports["test fs.mkdirSync fs.rmdirSync"] = function(assert) { |
michael@0 | 236 | let path = mkdirPath; |
michael@0 | 237 | |
michael@0 | 238 | assert.equal(fs.existsSync(path), false, "does not exists"); |
michael@0 | 239 | fs.mkdirSync(path); |
michael@0 | 240 | assert.equal(fs.existsSync(path), true, "dir was created"); |
michael@0 | 241 | try { |
michael@0 | 242 | fs.mkdirSync(path); |
michael@0 | 243 | assert.fail(Error("mkdir on existing should throw")); |
michael@0 | 244 | } catch (error) { |
michael@0 | 245 | assert.equal(error.message, "EEXIST, mkdir " + path); |
michael@0 | 246 | assert.equal(error.code, "EEXIST", "error has a code"); |
michael@0 | 247 | assert.equal(error.path, path, "error has a path"); |
michael@0 | 248 | assert.equal(error.errno, 47, "error has a errno"); |
michael@0 | 249 | } |
michael@0 | 250 | fs.rmdirSync(path); |
michael@0 | 251 | assert.equal(fs.existsSync(path), false, "dir was removed"); |
michael@0 | 252 | }; |
michael@0 | 253 | |
michael@0 | 254 | exports["test fs.mkdir"] = function(assert, end) { |
michael@0 | 255 | let path = mkdirPath; |
michael@0 | 256 | |
michael@0 | 257 | if (!fs.existsSync(path)) { |
michael@0 | 258 | let async = false; |
michael@0 | 259 | fs.mkdir(path, function(error) { |
michael@0 | 260 | assert.ok(async, "mkdir is async"); |
michael@0 | 261 | assert.ok(!error, "no error"); |
michael@0 | 262 | assert.equal(fs.existsSync(path), true, "dir was created"); |
michael@0 | 263 | fs.rmdirSync(path); |
michael@0 | 264 | assert.equal(fs.existsSync(path), false, "dir was removed"); |
michael@0 | 265 | end(); |
michael@0 | 266 | }); |
michael@0 | 267 | async = true; |
michael@0 | 268 | } |
michael@0 | 269 | }; |
michael@0 | 270 | |
michael@0 | 271 | exports["test fs.mkdir error"] = function(assert, end) { |
michael@0 | 272 | let path = mkdirPath; |
michael@0 | 273 | |
michael@0 | 274 | if (!fs.existsSync(path)) { |
michael@0 | 275 | fs.mkdirSync(path); |
michael@0 | 276 | let async = false; |
michael@0 | 277 | fs.mkdir(path, function(error) { |
michael@0 | 278 | assert.ok(async, "mkdir is async"); |
michael@0 | 279 | assert.equal(error.message, "EEXIST, mkdir " + path); |
michael@0 | 280 | assert.equal(error.code, "EEXIST", "error has a code"); |
michael@0 | 281 | assert.equal(error.path, path, "error has a path"); |
michael@0 | 282 | assert.equal(error.errno, 47, "error has a errno"); |
michael@0 | 283 | fs.rmdirSync(path); |
michael@0 | 284 | assert.equal(fs.existsSync(path), false, "dir was removed"); |
michael@0 | 285 | end(); |
michael@0 | 286 | }); |
michael@0 | 287 | async = true; |
michael@0 | 288 | } |
michael@0 | 289 | }; |
michael@0 | 290 | |
michael@0 | 291 | exports["test fs.rmdir"] = function(assert, end) { |
michael@0 | 292 | let path = mkdirPath; |
michael@0 | 293 | |
michael@0 | 294 | if (!fs.existsSync(path)) { |
michael@0 | 295 | fs.mkdirSync(path); |
michael@0 | 296 | assert.equal(fs.existsSync(path), true, "dir exists"); |
michael@0 | 297 | let async = false; |
michael@0 | 298 | fs.rmdir(path, function(error) { |
michael@0 | 299 | assert.ok(async, "mkdir is async"); |
michael@0 | 300 | assert.ok(!error, "no error"); |
michael@0 | 301 | assert.equal(fs.existsSync(path), false, "dir was removed"); |
michael@0 | 302 | end(); |
michael@0 | 303 | }); |
michael@0 | 304 | async = true; |
michael@0 | 305 | } |
michael@0 | 306 | }; |
michael@0 | 307 | |
michael@0 | 308 | |
michael@0 | 309 | exports["test fs.rmdir error"] = function(assert, end) { |
michael@0 | 310 | let path = mkdirPath; |
michael@0 | 311 | |
michael@0 | 312 | if (!fs.existsSync(path)) { |
michael@0 | 313 | assert.equal(fs.existsSync(path), false, "dir doesn't exists"); |
michael@0 | 314 | let async = false; |
michael@0 | 315 | fs.rmdir(path, function(error) { |
michael@0 | 316 | assert.ok(async, "mkdir is async"); |
michael@0 | 317 | assert.equal(error.message, "ENOENT, remove " + path); |
michael@0 | 318 | assert.equal(error.code, "ENOENT", "error has a code"); |
michael@0 | 319 | assert.equal(error.path, path, "error has a path"); |
michael@0 | 320 | assert.equal(error.errno, 34, "error has a errno"); |
michael@0 | 321 | assert.equal(fs.existsSync(path), false, "dir is removed"); |
michael@0 | 322 | end(); |
michael@0 | 323 | }); |
michael@0 | 324 | async = true; |
michael@0 | 325 | } |
michael@0 | 326 | }; |
michael@0 | 327 | |
michael@0 | 328 | exports["test fs.truncateSync fs.unlinkSync"] = function(assert) { |
michael@0 | 329 | let path = truncatePath; |
michael@0 | 330 | |
michael@0 | 331 | assert.equal(fs.existsSync(path), false, "does not exists"); |
michael@0 | 332 | fs.truncateSync(path); |
michael@0 | 333 | assert.equal(fs.existsSync(path), true, "file was created"); |
michael@0 | 334 | fs.truncateSync(path); |
michael@0 | 335 | fs.unlinkSync(path); |
michael@0 | 336 | assert.equal(fs.existsSync(path), false, "file was removed"); |
michael@0 | 337 | }; |
michael@0 | 338 | |
michael@0 | 339 | |
michael@0 | 340 | exports["test fs.truncate"] = function(assert, end) { |
michael@0 | 341 | let path = truncatePath; |
michael@0 | 342 | if (!fs.existsSync(path)) { |
michael@0 | 343 | let async = false; |
michael@0 | 344 | fs.truncate(path, 0, function(error) { |
michael@0 | 345 | assert.ok(async, "truncate is async"); |
michael@0 | 346 | assert.ok(!error, "no error"); |
michael@0 | 347 | assert.equal(fs.existsSync(path), true, "file was created"); |
michael@0 | 348 | fs.unlinkSync(path); |
michael@0 | 349 | assert.equal(fs.existsSync(path), false, "file was removed"); |
michael@0 | 350 | end(); |
michael@0 | 351 | }) |
michael@0 | 352 | async = true; |
michael@0 | 353 | } |
michael@0 | 354 | }; |
michael@0 | 355 | |
michael@0 | 356 | exports["test fs.unlink"] = function(assert, end) { |
michael@0 | 357 | let path = unlinkPath; |
michael@0 | 358 | let async = false; |
michael@0 | 359 | assert.ok(!fs.existsSync(path), "file doesn't exists yet"); |
michael@0 | 360 | fs.truncateSync(path, 0); |
michael@0 | 361 | assert.ok(fs.existsSync(path), "file was created"); |
michael@0 | 362 | fs.unlink(path, function(error) { |
michael@0 | 363 | assert.ok(async, "fs.unlink is async"); |
michael@0 | 364 | assert.ok(!error, "error is falsy"); |
michael@0 | 365 | assert.ok(!fs.existsSync(path), "file was removed"); |
michael@0 | 366 | end(); |
michael@0 | 367 | }); |
michael@0 | 368 | async = true; |
michael@0 | 369 | }; |
michael@0 | 370 | |
michael@0 | 371 | exports["test fs.unlink error"] = function(assert, end) { |
michael@0 | 372 | let path = unlinkPath; |
michael@0 | 373 | let async = false; |
michael@0 | 374 | assert.ok(!fs.existsSync(path), "file doesn't exists yet"); |
michael@0 | 375 | fs.unlink(path, function(error) { |
michael@0 | 376 | assert.ok(async, "fs.unlink is async"); |
michael@0 | 377 | assert.equal(error.message, "ENOENT, remove " + path); |
michael@0 | 378 | assert.equal(error.code, "ENOENT", "error has a code"); |
michael@0 | 379 | assert.equal(error.path, path, "error has a path"); |
michael@0 | 380 | assert.equal(error.errno, 34, "error has a errno"); |
michael@0 | 381 | end(); |
michael@0 | 382 | }); |
michael@0 | 383 | async = true; |
michael@0 | 384 | }; |
michael@0 | 385 | |
michael@0 | 386 | exports["test fs.rename"] = function(assert, end) { |
michael@0 | 387 | let fromPath = renameFromPath; |
michael@0 | 388 | let toPath = renameToPath; |
michael@0 | 389 | |
michael@0 | 390 | fs.truncateSync(fromPath); |
michael@0 | 391 | assert.ok(fs.existsSync(fromPath), "source file exists"); |
michael@0 | 392 | assert.ok(!fs.existsSync(toPath), "destination doesn't exists"); |
michael@0 | 393 | var async = false; |
michael@0 | 394 | fs.rename(fromPath, toPath, function(error) { |
michael@0 | 395 | assert.ok(async, "fs.rename is async"); |
michael@0 | 396 | assert.ok(!error, "error is falsy"); |
michael@0 | 397 | assert.ok(!fs.existsSync(fromPath), "source path no longer exists"); |
michael@0 | 398 | assert.ok(fs.existsSync(toPath), "destination file exists"); |
michael@0 | 399 | fs.unlinkSync(toPath); |
michael@0 | 400 | assert.ok(!fs.existsSync(toPath), "cleaned up properly"); |
michael@0 | 401 | end(); |
michael@0 | 402 | }); |
michael@0 | 403 | async = true; |
michael@0 | 404 | }; |
michael@0 | 405 | |
michael@0 | 406 | exports["test fs.rename (missing source file)"] = function(assert, end) { |
michael@0 | 407 | let fromPath = renameFromPath; |
michael@0 | 408 | let toPath = renameToPath; |
michael@0 | 409 | |
michael@0 | 410 | assert.ok(!fs.existsSync(fromPath), "source file doesn't exists"); |
michael@0 | 411 | assert.ok(!fs.existsSync(toPath), "destination doesn't exists"); |
michael@0 | 412 | var async = false; |
michael@0 | 413 | fs.rename(fromPath, toPath, function(error) { |
michael@0 | 414 | assert.ok(async, "fs.rename is async"); |
michael@0 | 415 | assert.equal(error.message, "ENOENT, rename " + fromPath); |
michael@0 | 416 | assert.equal(error.code, "ENOENT", "error has a code"); |
michael@0 | 417 | assert.equal(error.path, fromPath, "error has a path"); |
michael@0 | 418 | assert.equal(error.errno, 34, "error has a errno"); |
michael@0 | 419 | end(); |
michael@0 | 420 | }); |
michael@0 | 421 | async = true; |
michael@0 | 422 | }; |
michael@0 | 423 | |
michael@0 | 424 | exports["test fs.rename (existing target file)"] = function(assert, end) { |
michael@0 | 425 | let fromPath = renameFromPath; |
michael@0 | 426 | let toPath = renameToPath; |
michael@0 | 427 | |
michael@0 | 428 | fs.truncateSync(fromPath); |
michael@0 | 429 | fs.truncateSync(toPath); |
michael@0 | 430 | assert.ok(fs.existsSync(fromPath), "source file exists"); |
michael@0 | 431 | assert.ok(fs.existsSync(toPath), "destination file exists"); |
michael@0 | 432 | var async = false; |
michael@0 | 433 | fs.rename(fromPath, toPath, function(error) { |
michael@0 | 434 | assert.ok(async, "fs.rename is async"); |
michael@0 | 435 | assert.ok(!error, "error is falsy"); |
michael@0 | 436 | assert.ok(!fs.existsSync(fromPath), "source path no longer exists"); |
michael@0 | 437 | assert.ok(fs.existsSync(toPath), "destination file exists"); |
michael@0 | 438 | fs.unlinkSync(toPath); |
michael@0 | 439 | assert.ok(!fs.existsSync(toPath), "cleaned up properly"); |
michael@0 | 440 | end(); |
michael@0 | 441 | }); |
michael@0 | 442 | async = true; |
michael@0 | 443 | }; |
michael@0 | 444 | |
michael@0 | 445 | exports["test fs.writeFile"] = function(assert, end) { |
michael@0 | 446 | let path = writePath; |
michael@0 | 447 | let content = ["hello world", |
michael@0 | 448 | "this is some text"].join("\n"); |
michael@0 | 449 | |
michael@0 | 450 | var async = false; |
michael@0 | 451 | fs.writeFile(path, content, function(error) { |
michael@0 | 452 | assert.ok(async, "fs write is async"); |
michael@0 | 453 | assert.ok(!error, "error is falsy"); |
michael@0 | 454 | assert.ok(fs.existsSync(path), "file was created"); |
michael@0 | 455 | assert.equal(fs.readFileSync(path).toString(), |
michael@0 | 456 | content, |
michael@0 | 457 | "contet was written"); |
michael@0 | 458 | fs.unlinkSync(path); |
michael@0 | 459 | assert.ok(!fs.exists(path), "file was removed"); |
michael@0 | 460 | |
michael@0 | 461 | end(); |
michael@0 | 462 | }); |
michael@0 | 463 | async = true; |
michael@0 | 464 | }; |
michael@0 | 465 | |
michael@0 | 466 | exports["test fs.writeFile (with large files)"] = function(assert, end) { |
michael@0 | 467 | let path = writePath; |
michael@0 | 468 | let content = ""; |
michael@0 | 469 | |
michael@0 | 470 | for (var i = 0; i < 100000; i++) { |
michael@0 | 471 | content += "buffer\n"; |
michael@0 | 472 | } |
michael@0 | 473 | |
michael@0 | 474 | var async = false; |
michael@0 | 475 | fs.writeFile(path, content, function(error) { |
michael@0 | 476 | assert.ok(async, "fs write is async"); |
michael@0 | 477 | assert.ok(!error, "error is falsy"); |
michael@0 | 478 | assert.ok(fs.existsSync(path), "file was created"); |
michael@0 | 479 | assert.equal(fs.readFileSync(path).toString(), |
michael@0 | 480 | content, |
michael@0 | 481 | "contet was written"); |
michael@0 | 482 | fs.unlinkSync(path); |
michael@0 | 483 | assert.ok(!fs.exists(path), "file was removed"); |
michael@0 | 484 | |
michael@0 | 485 | end(); |
michael@0 | 486 | }); |
michael@0 | 487 | async = true; |
michael@0 | 488 | }; |
michael@0 | 489 | |
michael@0 | 490 | exports["test fs.writeFile error"] = function (assert, done) { |
michael@0 | 491 | try { |
michael@0 | 492 | fs.writeFile({}, 'content', function (err) { |
michael@0 | 493 | assert.fail('Error thrown from TypeError should not be caught'); |
michael@0 | 494 | }); |
michael@0 | 495 | } catch (e) { |
michael@0 | 496 | assert.ok(e, |
michael@0 | 497 | 'writeFile with a non-string error should not be caught'); |
michael@0 | 498 | assert.equal(e.name, 'TypeError', 'error should be TypeError'); |
michael@0 | 499 | } |
michael@0 | 500 | fs.writeFile('not/a/valid/path', 'content', function (err) { |
michael@0 | 501 | assert.ok(err, 'error caught and handled in callback'); |
michael@0 | 502 | done(); |
michael@0 | 503 | }); |
michael@0 | 504 | }; |
michael@0 | 505 | |
michael@0 | 506 | exports["test fs.chmod"] = function (assert, done) { |
michael@0 | 507 | let content = ["hej från sverige"]; |
michael@0 | 508 | |
michael@0 | 509 | fs.writeFile(chmodPath, content, function (err) { |
michael@0 | 510 | testPerm("0755")() |
michael@0 | 511 | .then(testPerm("0777")) |
michael@0 | 512 | .then(testPerm("0666")) |
michael@0 | 513 | .then(testPerm(parseInt("0511", 8))) |
michael@0 | 514 | .then(testPerm(parseInt("0200", 8))) |
michael@0 | 515 | .then(testPerm("0040")) |
michael@0 | 516 | .then(testPerm("0000")) |
michael@0 | 517 | .then(testPermSync(parseInt("0777", 8))) |
michael@0 | 518 | .then(testPermSync(parseInt("0666", 8))) |
michael@0 | 519 | .then(testPermSync("0511")) |
michael@0 | 520 | .then(testPermSync("0200")) |
michael@0 | 521 | .then(testPermSync("0040")) |
michael@0 | 522 | .then(testPermSync("0000")) |
michael@0 | 523 | .then(() => { |
michael@0 | 524 | assert.pass("Successful chmod passes"); |
michael@0 | 525 | }, assert.fail) |
michael@0 | 526 | // Test invalid paths |
michael@0 | 527 | .then(() => chmod("not-a-valid-file", parseInt("0755", 8))) |
michael@0 | 528 | .then(assert.fail, (err) => { |
michael@0 | 529 | checkPermError(err, "not-a-valid-file"); |
michael@0 | 530 | }) |
michael@0 | 531 | .then(() => chmod("not-a-valid-file", parseInt("0755", 8), "sync")) |
michael@0 | 532 | .then(assert.fail, (err) => { |
michael@0 | 533 | checkPermError(err, "not-a-valid-file"); |
michael@0 | 534 | }) |
michael@0 | 535 | // Test invalid files |
michael@0 | 536 | .then(() => chmod("resource://not-a-real-file", parseInt("0755", 8))) |
michael@0 | 537 | .then(assert.fail, (err) => { |
michael@0 | 538 | checkPermError(err, "resource://not-a-real-file"); |
michael@0 | 539 | }) |
michael@0 | 540 | .then(() => chmod("resource://not-a-real-file", parseInt("0755", 8), 'sync')) |
michael@0 | 541 | .then(assert.fail, (err) => { |
michael@0 | 542 | checkPermError(err, "resource://not-a-real-file"); |
michael@0 | 543 | }) |
michael@0 | 544 | .then(done, assert.fail); |
michael@0 | 545 | }); |
michael@0 | 546 | |
michael@0 | 547 | function checkPermError (err, path) { |
michael@0 | 548 | assert.equal(err.message, "ENOENT, chmod " + path); |
michael@0 | 549 | assert.equal(err.code, "ENOENT", "error has a code"); |
michael@0 | 550 | assert.equal(err.path, path, "error has a path"); |
michael@0 | 551 | assert.equal(err.errno, 34, "error has a errno"); |
michael@0 | 552 | } |
michael@0 | 553 | |
michael@0 | 554 | function chmod (path, mode, sync) { |
michael@0 | 555 | let { promise, resolve, reject } = defer(); |
michael@0 | 556 | if (!sync) { |
michael@0 | 557 | fs.chmod(path, mode, (err) => { |
michael@0 | 558 | if (err) reject(err); |
michael@0 | 559 | else resolve(); |
michael@0 | 560 | }); |
michael@0 | 561 | } else { |
michael@0 | 562 | fs.chmodSync(path, mode); |
michael@0 | 563 | resolve(); |
michael@0 | 564 | } |
michael@0 | 565 | return promise; |
michael@0 | 566 | } |
michael@0 | 567 | |
michael@0 | 568 | function testPerm (mode, sync) { |
michael@0 | 569 | return function () { |
michael@0 | 570 | return chmod(chmodPath, mode, sync) |
michael@0 | 571 | .then(() => getPerm(chmodPath)) |
michael@0 | 572 | .then(perm => { |
michael@0 | 573 | let nMode = normalizeMode(mode); |
michael@0 | 574 | if (isWindows) |
michael@0 | 575 | assert.equal(perm, nMode, |
michael@0 | 576 | "mode correctly set to " + mode + " (" + nMode + " on windows)"); |
michael@0 | 577 | else |
michael@0 | 578 | assert.equal(perm, nMode, "mode correctly set to " + nMode); |
michael@0 | 579 | }); |
michael@0 | 580 | }; |
michael@0 | 581 | } |
michael@0 | 582 | |
michael@0 | 583 | function testPermSync (mode) { |
michael@0 | 584 | return testPerm(mode, true); |
michael@0 | 585 | } |
michael@0 | 586 | |
michael@0 | 587 | function getPerm (path) { |
michael@0 | 588 | let { promise, resolve, reject } = defer(); |
michael@0 | 589 | fs.stat(path, function (err, stat) { |
michael@0 | 590 | if (err) reject(err); |
michael@0 | 591 | else resolve(stat.mode); |
michael@0 | 592 | }); |
michael@0 | 593 | return promise; |
michael@0 | 594 | } |
michael@0 | 595 | |
michael@0 | 596 | /* |
michael@0 | 597 | * Converts a unix mode `0755` into a Windows version of unix permissions |
michael@0 | 598 | */ |
michael@0 | 599 | function normalizeMode (mode) { |
michael@0 | 600 | if (typeof mode === "string") |
michael@0 | 601 | mode = parseInt(mode, 8); |
michael@0 | 602 | |
michael@0 | 603 | if (!isWindows) |
michael@0 | 604 | return mode; |
michael@0 | 605 | |
michael@0 | 606 | var ANY_READ = parseInt("0444", 8); |
michael@0 | 607 | var ANY_WRITE = parseInt("0222", 8); |
michael@0 | 608 | var winMode = 0; |
michael@0 | 609 | |
michael@0 | 610 | // On Windows, if WRITE is true, then READ is also true |
michael@0 | 611 | if (mode & ANY_WRITE) |
michael@0 | 612 | winMode |= ANY_WRITE | ANY_READ; |
michael@0 | 613 | // Minimum permissions are READ for Windows |
michael@0 | 614 | else |
michael@0 | 615 | winMode |= ANY_READ; |
michael@0 | 616 | |
michael@0 | 617 | return winMode; |
michael@0 | 618 | } |
michael@0 | 619 | }; |
michael@0 | 620 | |
michael@0 | 621 | require("test").run(exports); |