1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-file.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,275 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +const { pathFor } = require('sdk/system'); 1.11 +const file = require("sdk/io/file"); 1.12 +const url = require("sdk/url"); 1.13 + 1.14 +const byteStreams = require("sdk/io/byte-streams"); 1.15 +const textStreams = require("sdk/io/text-streams"); 1.16 + 1.17 +const ERRORS = { 1.18 + FILE_NOT_FOUND: /^path does not exist: .+$/, 1.19 + NOT_A_DIRECTORY: /^path is not a directory: .+$/, 1.20 + NOT_A_FILE: /^path is not a file: .+$/, 1.21 +}; 1.22 + 1.23 +// Use profile directory to list / read / write files. 1.24 +const profilePath = pathFor('ProfD'); 1.25 +const fileNameInProfile = 'compatibility.ini'; 1.26 +const dirNameInProfile = 'extensions'; 1.27 +const filePathInProfile = file.join(profilePath, fileNameInProfile); 1.28 +const dirPathInProfile = file.join(profilePath, dirNameInProfile); 1.29 + 1.30 +exports.testDirName = function(assert) { 1.31 + assert.equal(file.dirname(dirPathInProfile), profilePath, 1.32 + "file.dirname() of dir should return parent dir"); 1.33 + 1.34 + assert.equal(file.dirname(filePathInProfile), profilePath, 1.35 + "file.dirname() of file should return its dir"); 1.36 + 1.37 + let dir = profilePath; 1.38 + while (dir) 1.39 + dir = file.dirname(dir); 1.40 + 1.41 + assert.equal(dir, "", 1.42 + "dirname should return empty string when dir has no parent"); 1.43 +}; 1.44 + 1.45 +exports.testBasename = function(assert) { 1.46 + // Get the top-most path -- the path with no basename. E.g., on Unix-like 1.47 + // systems this will be /. We'll use it below to build up some test paths. 1.48 + // We have to go to this trouble because file.join() needs a legal path as a 1.49 + // base case; join("foo", "bar") doesn't work unfortunately. 1.50 + let topPath = profilePath; 1.51 + let parentPath = file.dirname(topPath); 1.52 + while (parentPath) { 1.53 + topPath = parentPath; 1.54 + parentPath = file.dirname(topPath); 1.55 + } 1.56 + 1.57 + let path = topPath; 1.58 + assert.equal(file.basename(path), "", 1.59 + "basename should work on paths with no components"); 1.60 + 1.61 + path = file.join(path, "foo"); 1.62 + assert.equal(file.basename(path), "foo", 1.63 + "basename should work on paths with a single component"); 1.64 + 1.65 + path = file.join(path, "bar"); 1.66 + assert.equal(file.basename(path), "bar", 1.67 + "basename should work on paths with multiple components"); 1.68 +}; 1.69 + 1.70 +exports.testList = function(assert) { 1.71 + let list = file.list(profilePath); 1.72 + let found = [ true for each (name in list) 1.73 + if (name === fileNameInProfile) ]; 1.74 + 1.75 + if (found.length > 1) 1.76 + assert.fail("a dir can't contain two files of the same name!"); 1.77 + assert.equal(found[0], true, "file.list() should work"); 1.78 + 1.79 + assert.throws(function() { 1.80 + file.list(filePathInProfile); 1.81 + }, ERRORS.NOT_A_DIRECTORY, "file.list() on non-dir should raise error"); 1.82 + 1.83 + assert.throws(function() { 1.84 + file.list(file.join(dirPathInProfile, "does-not-exist")); 1.85 + }, ERRORS.FILE_NOT_FOUND, "file.list() on nonexistent should raise error"); 1.86 +}; 1.87 + 1.88 +exports.testRead = function(assert) { 1.89 + let contents = file.read(filePathInProfile); 1.90 + assert.ok(/Compatibility/.test(contents), 1.91 + "file.read() should work"); 1.92 + 1.93 + assert.throws(function() { 1.94 + file.read(file.join(dirPathInProfile, "does-not-exists")); 1.95 + }, ERRORS.FILE_NOT_FOUND, "file.read() on nonexistent file should throw"); 1.96 + 1.97 + assert.throws(function() { 1.98 + file.read(dirPathInProfile); 1.99 + }, ERRORS.NOT_A_FILE, "file.read() on dir should throw"); 1.100 +}; 1.101 + 1.102 +exports.testJoin = function(assert) { 1.103 + let baseDir = file.dirname(filePathInProfile); 1.104 + 1.105 + assert.equal(file.join(baseDir, fileNameInProfile), 1.106 + filePathInProfile, "file.join() should work"); 1.107 +}; 1.108 + 1.109 +exports.testOpenNonexistentForRead = function (assert) { 1.110 + var filename = file.join(profilePath, 'does-not-exists'); 1.111 + assert.throws(function() { 1.112 + file.open(filename); 1.113 + }, ERRORS.FILE_NOT_FOUND, "file.open() on nonexistent file should throw"); 1.114 + 1.115 + assert.throws(function() { 1.116 + file.open(filename, "r"); 1.117 + }, ERRORS.FILE_NOT_FOUND, "file.open('r') on nonexistent file should throw"); 1.118 + 1.119 + assert.throws(function() { 1.120 + file.open(filename, "zz"); 1.121 + }, ERRORS.FILE_NOT_FOUND, "file.open('zz') on nonexistent file should throw"); 1.122 +}; 1.123 + 1.124 +exports.testOpenNonexistentForWrite = function (assert) { 1.125 + let filename = file.join(profilePath, 'open.txt'); 1.126 + 1.127 + let stream = file.open(filename, "w"); 1.128 + stream.close(); 1.129 + 1.130 + assert.ok(file.exists(filename), 1.131 + "file.exists() should return true after file.open('w')"); 1.132 + file.remove(filename); 1.133 + assert.ok(!file.exists(filename), 1.134 + "file.exists() should return false after file.remove()"); 1.135 + 1.136 + stream = file.open(filename, "rw"); 1.137 + stream.close(); 1.138 + 1.139 + assert.ok(file.exists(filename), 1.140 + "file.exists() should return true after file.open('rw')"); 1.141 + file.remove(filename); 1.142 + assert.ok(!file.exists(filename), 1.143 + "file.exists() should return false after file.remove()"); 1.144 +}; 1.145 + 1.146 +exports.testOpenDirectory = function (assert) { 1.147 + let dir = dirPathInProfile; 1.148 + assert.throws(function() { 1.149 + file.open(dir); 1.150 + }, ERRORS.NOT_A_FILE, "file.open() on directory should throw"); 1.151 + 1.152 + assert.throws(function() { 1.153 + file.open(dir, "w"); 1.154 + }, ERRORS.NOT_A_FILE, "file.open('w') on directory should throw"); 1.155 +}; 1.156 + 1.157 +exports.testOpenTypes = function (assert) { 1.158 + let filename = file.join(profilePath, 'open-types.txt'); 1.159 + 1.160 + 1.161 + // Do the opens first to create the data file. 1.162 + var stream = file.open(filename, "w"); 1.163 + assert.ok(stream instanceof textStreams.TextWriter, 1.164 + "open(w) should return a TextWriter"); 1.165 + stream.close(); 1.166 + 1.167 + stream = file.open(filename, "wb"); 1.168 + assert.ok(stream instanceof byteStreams.ByteWriter, 1.169 + "open(wb) should return a ByteWriter"); 1.170 + stream.close(); 1.171 + 1.172 + stream = file.open(filename); 1.173 + assert.ok(stream instanceof textStreams.TextReader, 1.174 + "open() should return a TextReader"); 1.175 + stream.close(); 1.176 + 1.177 + stream = file.open(filename, "r"); 1.178 + assert.ok(stream instanceof textStreams.TextReader, 1.179 + "open(r) should return a TextReader"); 1.180 + stream.close(); 1.181 + 1.182 + stream = file.open(filename, "b"); 1.183 + assert.ok(stream instanceof byteStreams.ByteReader, 1.184 + "open(b) should return a ByteReader"); 1.185 + stream.close(); 1.186 + 1.187 + stream = file.open(filename, "rb"); 1.188 + assert.ok(stream instanceof byteStreams.ByteReader, 1.189 + "open(rb) should return a ByteReader"); 1.190 + stream.close(); 1.191 + 1.192 + file.remove(filename); 1.193 +}; 1.194 + 1.195 +exports.testMkpathRmdir = function (assert) { 1.196 + let basePath = profilePath; 1.197 + let dirs = []; 1.198 + for (let i = 0; i < 3; i++) 1.199 + dirs.push("test-file-dir"); 1.200 + 1.201 + let paths = []; 1.202 + for (let i = 0; i < dirs.length; i++) { 1.203 + let args = [basePath].concat(dirs.slice(0, i + 1)); 1.204 + paths.unshift(file.join.apply(null, args)); 1.205 + } 1.206 + 1.207 + for (let i = 0; i < paths.length; i++) { 1.208 + assert.ok(!file.exists(paths[i]), 1.209 + "Sanity check: path should not exist: " + paths[i]); 1.210 + } 1.211 + 1.212 + file.mkpath(paths[0]); 1.213 + assert.ok(file.exists(paths[0]), "mkpath should create path: " + paths[0]); 1.214 + 1.215 + for (let i = 0; i < paths.length; i++) { 1.216 + file.rmdir(paths[i]); 1.217 + assert.ok(!file.exists(paths[i]), 1.218 + "rmdir should remove path: " + paths[i]); 1.219 + } 1.220 +}; 1.221 + 1.222 +exports.testMkpathTwice = function (assert) { 1.223 + let dir = profilePath; 1.224 + let path = file.join(dir, "test-file-dir"); 1.225 + assert.ok(!file.exists(path), 1.226 + "Sanity check: path should not exist: " + path); 1.227 + file.mkpath(path); 1.228 + assert.ok(file.exists(path), "mkpath should create path: " + path); 1.229 + file.mkpath(path); 1.230 + assert.ok(file.exists(path), 1.231 + "After second mkpath, path should still exist: " + path); 1.232 + file.rmdir(path); 1.233 + assert.ok(!file.exists(path), "rmdir should remove path: " + path); 1.234 +}; 1.235 + 1.236 +exports.testMkpathExistingNondirectory = function (assert) { 1.237 + var fname = file.join(profilePath, 'conflict.txt'); 1.238 + file.open(fname, "w").close(); 1.239 + assert.ok(file.exists(fname), "File should exist"); 1.240 + assert.throws(function() file.mkpath(fname), 1.241 + /^The path already exists and is not a directory: .+$/, 1.242 + "mkpath on file should raise error"); 1.243 + file.remove(fname); 1.244 +}; 1.245 + 1.246 +exports.testRmdirNondirectory = function (assert) { 1.247 + var fname = file.join(profilePath, 'not-a-dir') 1.248 + file.open(fname, "w").close(); 1.249 + assert.ok(file.exists(fname), "File should exist"); 1.250 + assert.throws(function() { 1.251 + file.rmdir(fname); 1.252 + }, ERRORS.NOT_A_DIRECTORY, "rmdir on file should raise error"); 1.253 + file.remove(fname); 1.254 + assert.ok(!file.exists(fname), "File should not exist"); 1.255 + assert.throws(function () file.rmdir(fname), 1.256 + ERRORS.FILE_NOT_FOUND, 1.257 + "rmdir on non-existing file should raise error"); 1.258 +}; 1.259 + 1.260 +exports.testRmdirNonempty = function (assert) { 1.261 + let dir = profilePath; 1.262 + let path = file.join(dir, "test-file-dir"); 1.263 + assert.ok(!file.exists(path), 1.264 + "Sanity check: path should not exist: " + path); 1.265 + file.mkpath(path); 1.266 + let filePath = file.join(path, "file"); 1.267 + file.open(filePath, "w").close(); 1.268 + assert.ok(file.exists(filePath), 1.269 + "Sanity check: path should exist: " + filePath); 1.270 + assert.throws(function () file.rmdir(path), 1.271 + /^The directory is not empty: .+$/, 1.272 + "rmdir on non-empty directory should raise error"); 1.273 + file.remove(filePath); 1.274 + file.rmdir(path); 1.275 + assert.ok(!file.exists(path), "Path should not exist"); 1.276 +}; 1.277 + 1.278 +require('sdk/test').run(exports);