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

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

mercurial