addon-sdk/source/test/test-file.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 } = require('sdk/system');
michael@0 8 const file = require("sdk/io/file");
michael@0 9 const url = require("sdk/url");
michael@0 10
michael@0 11 const byteStreams = require("sdk/io/byte-streams");
michael@0 12 const textStreams = require("sdk/io/text-streams");
michael@0 13
michael@0 14 const ERRORS = {
michael@0 15 FILE_NOT_FOUND: /^path does not exist: .+$/,
michael@0 16 NOT_A_DIRECTORY: /^path is not a directory: .+$/,
michael@0 17 NOT_A_FILE: /^path is not a file: .+$/,
michael@0 18 };
michael@0 19
michael@0 20 // Use profile directory to list / read / write files.
michael@0 21 const profilePath = pathFor('ProfD');
michael@0 22 const fileNameInProfile = 'compatibility.ini';
michael@0 23 const dirNameInProfile = 'extensions';
michael@0 24 const filePathInProfile = file.join(profilePath, fileNameInProfile);
michael@0 25 const dirPathInProfile = file.join(profilePath, dirNameInProfile);
michael@0 26
michael@0 27 exports.testDirName = function(assert) {
michael@0 28 assert.equal(file.dirname(dirPathInProfile), profilePath,
michael@0 29 "file.dirname() of dir should return parent dir");
michael@0 30
michael@0 31 assert.equal(file.dirname(filePathInProfile), profilePath,
michael@0 32 "file.dirname() of file should return its dir");
michael@0 33
michael@0 34 let dir = profilePath;
michael@0 35 while (dir)
michael@0 36 dir = file.dirname(dir);
michael@0 37
michael@0 38 assert.equal(dir, "",
michael@0 39 "dirname should return empty string when dir has no parent");
michael@0 40 };
michael@0 41
michael@0 42 exports.testBasename = function(assert) {
michael@0 43 // Get the top-most path -- the path with no basename. E.g., on Unix-like
michael@0 44 // systems this will be /. We'll use it below to build up some test paths.
michael@0 45 // We have to go to this trouble because file.join() needs a legal path as a
michael@0 46 // base case; join("foo", "bar") doesn't work unfortunately.
michael@0 47 let topPath = profilePath;
michael@0 48 let parentPath = file.dirname(topPath);
michael@0 49 while (parentPath) {
michael@0 50 topPath = parentPath;
michael@0 51 parentPath = file.dirname(topPath);
michael@0 52 }
michael@0 53
michael@0 54 let path = topPath;
michael@0 55 assert.equal(file.basename(path), "",
michael@0 56 "basename should work on paths with no components");
michael@0 57
michael@0 58 path = file.join(path, "foo");
michael@0 59 assert.equal(file.basename(path), "foo",
michael@0 60 "basename should work on paths with a single component");
michael@0 61
michael@0 62 path = file.join(path, "bar");
michael@0 63 assert.equal(file.basename(path), "bar",
michael@0 64 "basename should work on paths with multiple components");
michael@0 65 };
michael@0 66
michael@0 67 exports.testList = function(assert) {
michael@0 68 let list = file.list(profilePath);
michael@0 69 let found = [ true for each (name in list)
michael@0 70 if (name === fileNameInProfile) ];
michael@0 71
michael@0 72 if (found.length > 1)
michael@0 73 assert.fail("a dir can't contain two files of the same name!");
michael@0 74 assert.equal(found[0], true, "file.list() should work");
michael@0 75
michael@0 76 assert.throws(function() {
michael@0 77 file.list(filePathInProfile);
michael@0 78 }, ERRORS.NOT_A_DIRECTORY, "file.list() on non-dir should raise error");
michael@0 79
michael@0 80 assert.throws(function() {
michael@0 81 file.list(file.join(dirPathInProfile, "does-not-exist"));
michael@0 82 }, ERRORS.FILE_NOT_FOUND, "file.list() on nonexistent should raise error");
michael@0 83 };
michael@0 84
michael@0 85 exports.testRead = function(assert) {
michael@0 86 let contents = file.read(filePathInProfile);
michael@0 87 assert.ok(/Compatibility/.test(contents),
michael@0 88 "file.read() should work");
michael@0 89
michael@0 90 assert.throws(function() {
michael@0 91 file.read(file.join(dirPathInProfile, "does-not-exists"));
michael@0 92 }, ERRORS.FILE_NOT_FOUND, "file.read() on nonexistent file should throw");
michael@0 93
michael@0 94 assert.throws(function() {
michael@0 95 file.read(dirPathInProfile);
michael@0 96 }, ERRORS.NOT_A_FILE, "file.read() on dir should throw");
michael@0 97 };
michael@0 98
michael@0 99 exports.testJoin = function(assert) {
michael@0 100 let baseDir = file.dirname(filePathInProfile);
michael@0 101
michael@0 102 assert.equal(file.join(baseDir, fileNameInProfile),
michael@0 103 filePathInProfile, "file.join() should work");
michael@0 104 };
michael@0 105
michael@0 106 exports.testOpenNonexistentForRead = function (assert) {
michael@0 107 var filename = file.join(profilePath, 'does-not-exists');
michael@0 108 assert.throws(function() {
michael@0 109 file.open(filename);
michael@0 110 }, ERRORS.FILE_NOT_FOUND, "file.open() on nonexistent file should throw");
michael@0 111
michael@0 112 assert.throws(function() {
michael@0 113 file.open(filename, "r");
michael@0 114 }, ERRORS.FILE_NOT_FOUND, "file.open('r') on nonexistent file should throw");
michael@0 115
michael@0 116 assert.throws(function() {
michael@0 117 file.open(filename, "zz");
michael@0 118 }, ERRORS.FILE_NOT_FOUND, "file.open('zz') on nonexistent file should throw");
michael@0 119 };
michael@0 120
michael@0 121 exports.testOpenNonexistentForWrite = function (assert) {
michael@0 122 let filename = file.join(profilePath, 'open.txt');
michael@0 123
michael@0 124 let stream = file.open(filename, "w");
michael@0 125 stream.close();
michael@0 126
michael@0 127 assert.ok(file.exists(filename),
michael@0 128 "file.exists() should return true after file.open('w')");
michael@0 129 file.remove(filename);
michael@0 130 assert.ok(!file.exists(filename),
michael@0 131 "file.exists() should return false after file.remove()");
michael@0 132
michael@0 133 stream = file.open(filename, "rw");
michael@0 134 stream.close();
michael@0 135
michael@0 136 assert.ok(file.exists(filename),
michael@0 137 "file.exists() should return true after file.open('rw')");
michael@0 138 file.remove(filename);
michael@0 139 assert.ok(!file.exists(filename),
michael@0 140 "file.exists() should return false after file.remove()");
michael@0 141 };
michael@0 142
michael@0 143 exports.testOpenDirectory = function (assert) {
michael@0 144 let dir = dirPathInProfile;
michael@0 145 assert.throws(function() {
michael@0 146 file.open(dir);
michael@0 147 }, ERRORS.NOT_A_FILE, "file.open() on directory should throw");
michael@0 148
michael@0 149 assert.throws(function() {
michael@0 150 file.open(dir, "w");
michael@0 151 }, ERRORS.NOT_A_FILE, "file.open('w') on directory should throw");
michael@0 152 };
michael@0 153
michael@0 154 exports.testOpenTypes = function (assert) {
michael@0 155 let filename = file.join(profilePath, 'open-types.txt');
michael@0 156
michael@0 157
michael@0 158 // Do the opens first to create the data file.
michael@0 159 var stream = file.open(filename, "w");
michael@0 160 assert.ok(stream instanceof textStreams.TextWriter,
michael@0 161 "open(w) should return a TextWriter");
michael@0 162 stream.close();
michael@0 163
michael@0 164 stream = file.open(filename, "wb");
michael@0 165 assert.ok(stream instanceof byteStreams.ByteWriter,
michael@0 166 "open(wb) should return a ByteWriter");
michael@0 167 stream.close();
michael@0 168
michael@0 169 stream = file.open(filename);
michael@0 170 assert.ok(stream instanceof textStreams.TextReader,
michael@0 171 "open() should return a TextReader");
michael@0 172 stream.close();
michael@0 173
michael@0 174 stream = file.open(filename, "r");
michael@0 175 assert.ok(stream instanceof textStreams.TextReader,
michael@0 176 "open(r) should return a TextReader");
michael@0 177 stream.close();
michael@0 178
michael@0 179 stream = file.open(filename, "b");
michael@0 180 assert.ok(stream instanceof byteStreams.ByteReader,
michael@0 181 "open(b) should return a ByteReader");
michael@0 182 stream.close();
michael@0 183
michael@0 184 stream = file.open(filename, "rb");
michael@0 185 assert.ok(stream instanceof byteStreams.ByteReader,
michael@0 186 "open(rb) should return a ByteReader");
michael@0 187 stream.close();
michael@0 188
michael@0 189 file.remove(filename);
michael@0 190 };
michael@0 191
michael@0 192 exports.testMkpathRmdir = function (assert) {
michael@0 193 let basePath = profilePath;
michael@0 194 let dirs = [];
michael@0 195 for (let i = 0; i < 3; i++)
michael@0 196 dirs.push("test-file-dir");
michael@0 197
michael@0 198 let paths = [];
michael@0 199 for (let i = 0; i < dirs.length; i++) {
michael@0 200 let args = [basePath].concat(dirs.slice(0, i + 1));
michael@0 201 paths.unshift(file.join.apply(null, args));
michael@0 202 }
michael@0 203
michael@0 204 for (let i = 0; i < paths.length; i++) {
michael@0 205 assert.ok(!file.exists(paths[i]),
michael@0 206 "Sanity check: path should not exist: " + paths[i]);
michael@0 207 }
michael@0 208
michael@0 209 file.mkpath(paths[0]);
michael@0 210 assert.ok(file.exists(paths[0]), "mkpath should create path: " + paths[0]);
michael@0 211
michael@0 212 for (let i = 0; i < paths.length; i++) {
michael@0 213 file.rmdir(paths[i]);
michael@0 214 assert.ok(!file.exists(paths[i]),
michael@0 215 "rmdir should remove path: " + paths[i]);
michael@0 216 }
michael@0 217 };
michael@0 218
michael@0 219 exports.testMkpathTwice = function (assert) {
michael@0 220 let dir = profilePath;
michael@0 221 let path = file.join(dir, "test-file-dir");
michael@0 222 assert.ok(!file.exists(path),
michael@0 223 "Sanity check: path should not exist: " + path);
michael@0 224 file.mkpath(path);
michael@0 225 assert.ok(file.exists(path), "mkpath should create path: " + path);
michael@0 226 file.mkpath(path);
michael@0 227 assert.ok(file.exists(path),
michael@0 228 "After second mkpath, path should still exist: " + path);
michael@0 229 file.rmdir(path);
michael@0 230 assert.ok(!file.exists(path), "rmdir should remove path: " + path);
michael@0 231 };
michael@0 232
michael@0 233 exports.testMkpathExistingNondirectory = function (assert) {
michael@0 234 var fname = file.join(profilePath, 'conflict.txt');
michael@0 235 file.open(fname, "w").close();
michael@0 236 assert.ok(file.exists(fname), "File should exist");
michael@0 237 assert.throws(function() file.mkpath(fname),
michael@0 238 /^The path already exists and is not a directory: .+$/,
michael@0 239 "mkpath on file should raise error");
michael@0 240 file.remove(fname);
michael@0 241 };
michael@0 242
michael@0 243 exports.testRmdirNondirectory = function (assert) {
michael@0 244 var fname = file.join(profilePath, 'not-a-dir')
michael@0 245 file.open(fname, "w").close();
michael@0 246 assert.ok(file.exists(fname), "File should exist");
michael@0 247 assert.throws(function() {
michael@0 248 file.rmdir(fname);
michael@0 249 }, ERRORS.NOT_A_DIRECTORY, "rmdir on file should raise error");
michael@0 250 file.remove(fname);
michael@0 251 assert.ok(!file.exists(fname), "File should not exist");
michael@0 252 assert.throws(function () file.rmdir(fname),
michael@0 253 ERRORS.FILE_NOT_FOUND,
michael@0 254 "rmdir on non-existing file should raise error");
michael@0 255 };
michael@0 256
michael@0 257 exports.testRmdirNonempty = function (assert) {
michael@0 258 let dir = profilePath;
michael@0 259 let path = file.join(dir, "test-file-dir");
michael@0 260 assert.ok(!file.exists(path),
michael@0 261 "Sanity check: path should not exist: " + path);
michael@0 262 file.mkpath(path);
michael@0 263 let filePath = file.join(path, "file");
michael@0 264 file.open(filePath, "w").close();
michael@0 265 assert.ok(file.exists(filePath),
michael@0 266 "Sanity check: path should exist: " + filePath);
michael@0 267 assert.throws(function () file.rmdir(path),
michael@0 268 /^The directory is not empty: .+$/,
michael@0 269 "rmdir on non-empty directory should raise error");
michael@0 270 file.remove(filePath);
michael@0 271 file.rmdir(path);
michael@0 272 assert.ok(!file.exists(path), "Path should not exist");
michael@0 273 };
michael@0 274
michael@0 275 require('sdk/test').run(exports);

mercurial