michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const { pathFor } = require('sdk/system'); michael@0: const file = require("sdk/io/file"); michael@0: const url = require("sdk/url"); michael@0: michael@0: const byteStreams = require("sdk/io/byte-streams"); michael@0: const textStreams = require("sdk/io/text-streams"); michael@0: michael@0: const ERRORS = { michael@0: FILE_NOT_FOUND: /^path does not exist: .+$/, michael@0: NOT_A_DIRECTORY: /^path is not a directory: .+$/, michael@0: NOT_A_FILE: /^path is not a file: .+$/, michael@0: }; michael@0: michael@0: // Use profile directory to list / read / write files. michael@0: const profilePath = pathFor('ProfD'); michael@0: const fileNameInProfile = 'compatibility.ini'; michael@0: const dirNameInProfile = 'extensions'; michael@0: const filePathInProfile = file.join(profilePath, fileNameInProfile); michael@0: const dirPathInProfile = file.join(profilePath, dirNameInProfile); michael@0: michael@0: exports.testDirName = function(assert) { michael@0: assert.equal(file.dirname(dirPathInProfile), profilePath, michael@0: "file.dirname() of dir should return parent dir"); michael@0: michael@0: assert.equal(file.dirname(filePathInProfile), profilePath, michael@0: "file.dirname() of file should return its dir"); michael@0: michael@0: let dir = profilePath; michael@0: while (dir) michael@0: dir = file.dirname(dir); michael@0: michael@0: assert.equal(dir, "", michael@0: "dirname should return empty string when dir has no parent"); michael@0: }; michael@0: michael@0: exports.testBasename = function(assert) { michael@0: // Get the top-most path -- the path with no basename. E.g., on Unix-like michael@0: // systems this will be /. We'll use it below to build up some test paths. michael@0: // We have to go to this trouble because file.join() needs a legal path as a michael@0: // base case; join("foo", "bar") doesn't work unfortunately. michael@0: let topPath = profilePath; michael@0: let parentPath = file.dirname(topPath); michael@0: while (parentPath) { michael@0: topPath = parentPath; michael@0: parentPath = file.dirname(topPath); michael@0: } michael@0: michael@0: let path = topPath; michael@0: assert.equal(file.basename(path), "", michael@0: "basename should work on paths with no components"); michael@0: michael@0: path = file.join(path, "foo"); michael@0: assert.equal(file.basename(path), "foo", michael@0: "basename should work on paths with a single component"); michael@0: michael@0: path = file.join(path, "bar"); michael@0: assert.equal(file.basename(path), "bar", michael@0: "basename should work on paths with multiple components"); michael@0: }; michael@0: michael@0: exports.testList = function(assert) { michael@0: let list = file.list(profilePath); michael@0: let found = [ true for each (name in list) michael@0: if (name === fileNameInProfile) ]; michael@0: michael@0: if (found.length > 1) michael@0: assert.fail("a dir can't contain two files of the same name!"); michael@0: assert.equal(found[0], true, "file.list() should work"); michael@0: michael@0: assert.throws(function() { michael@0: file.list(filePathInProfile); michael@0: }, ERRORS.NOT_A_DIRECTORY, "file.list() on non-dir should raise error"); michael@0: michael@0: assert.throws(function() { michael@0: file.list(file.join(dirPathInProfile, "does-not-exist")); michael@0: }, ERRORS.FILE_NOT_FOUND, "file.list() on nonexistent should raise error"); michael@0: }; michael@0: michael@0: exports.testRead = function(assert) { michael@0: let contents = file.read(filePathInProfile); michael@0: assert.ok(/Compatibility/.test(contents), michael@0: "file.read() should work"); michael@0: michael@0: assert.throws(function() { michael@0: file.read(file.join(dirPathInProfile, "does-not-exists")); michael@0: }, ERRORS.FILE_NOT_FOUND, "file.read() on nonexistent file should throw"); michael@0: michael@0: assert.throws(function() { michael@0: file.read(dirPathInProfile); michael@0: }, ERRORS.NOT_A_FILE, "file.read() on dir should throw"); michael@0: }; michael@0: michael@0: exports.testJoin = function(assert) { michael@0: let baseDir = file.dirname(filePathInProfile); michael@0: michael@0: assert.equal(file.join(baseDir, fileNameInProfile), michael@0: filePathInProfile, "file.join() should work"); michael@0: }; michael@0: michael@0: exports.testOpenNonexistentForRead = function (assert) { michael@0: var filename = file.join(profilePath, 'does-not-exists'); michael@0: assert.throws(function() { michael@0: file.open(filename); michael@0: }, ERRORS.FILE_NOT_FOUND, "file.open() on nonexistent file should throw"); michael@0: michael@0: assert.throws(function() { michael@0: file.open(filename, "r"); michael@0: }, ERRORS.FILE_NOT_FOUND, "file.open('r') on nonexistent file should throw"); michael@0: michael@0: assert.throws(function() { michael@0: file.open(filename, "zz"); michael@0: }, ERRORS.FILE_NOT_FOUND, "file.open('zz') on nonexistent file should throw"); michael@0: }; michael@0: michael@0: exports.testOpenNonexistentForWrite = function (assert) { michael@0: let filename = file.join(profilePath, 'open.txt'); michael@0: michael@0: let stream = file.open(filename, "w"); michael@0: stream.close(); michael@0: michael@0: assert.ok(file.exists(filename), michael@0: "file.exists() should return true after file.open('w')"); michael@0: file.remove(filename); michael@0: assert.ok(!file.exists(filename), michael@0: "file.exists() should return false after file.remove()"); michael@0: michael@0: stream = file.open(filename, "rw"); michael@0: stream.close(); michael@0: michael@0: assert.ok(file.exists(filename), michael@0: "file.exists() should return true after file.open('rw')"); michael@0: file.remove(filename); michael@0: assert.ok(!file.exists(filename), michael@0: "file.exists() should return false after file.remove()"); michael@0: }; michael@0: michael@0: exports.testOpenDirectory = function (assert) { michael@0: let dir = dirPathInProfile; michael@0: assert.throws(function() { michael@0: file.open(dir); michael@0: }, ERRORS.NOT_A_FILE, "file.open() on directory should throw"); michael@0: michael@0: assert.throws(function() { michael@0: file.open(dir, "w"); michael@0: }, ERRORS.NOT_A_FILE, "file.open('w') on directory should throw"); michael@0: }; michael@0: michael@0: exports.testOpenTypes = function (assert) { michael@0: let filename = file.join(profilePath, 'open-types.txt'); michael@0: michael@0: michael@0: // Do the opens first to create the data file. michael@0: var stream = file.open(filename, "w"); michael@0: assert.ok(stream instanceof textStreams.TextWriter, michael@0: "open(w) should return a TextWriter"); michael@0: stream.close(); michael@0: michael@0: stream = file.open(filename, "wb"); michael@0: assert.ok(stream instanceof byteStreams.ByteWriter, michael@0: "open(wb) should return a ByteWriter"); michael@0: stream.close(); michael@0: michael@0: stream = file.open(filename); michael@0: assert.ok(stream instanceof textStreams.TextReader, michael@0: "open() should return a TextReader"); michael@0: stream.close(); michael@0: michael@0: stream = file.open(filename, "r"); michael@0: assert.ok(stream instanceof textStreams.TextReader, michael@0: "open(r) should return a TextReader"); michael@0: stream.close(); michael@0: michael@0: stream = file.open(filename, "b"); michael@0: assert.ok(stream instanceof byteStreams.ByteReader, michael@0: "open(b) should return a ByteReader"); michael@0: stream.close(); michael@0: michael@0: stream = file.open(filename, "rb"); michael@0: assert.ok(stream instanceof byteStreams.ByteReader, michael@0: "open(rb) should return a ByteReader"); michael@0: stream.close(); michael@0: michael@0: file.remove(filename); michael@0: }; michael@0: michael@0: exports.testMkpathRmdir = function (assert) { michael@0: let basePath = profilePath; michael@0: let dirs = []; michael@0: for (let i = 0; i < 3; i++) michael@0: dirs.push("test-file-dir"); michael@0: michael@0: let paths = []; michael@0: for (let i = 0; i < dirs.length; i++) { michael@0: let args = [basePath].concat(dirs.slice(0, i + 1)); michael@0: paths.unshift(file.join.apply(null, args)); michael@0: } michael@0: michael@0: for (let i = 0; i < paths.length; i++) { michael@0: assert.ok(!file.exists(paths[i]), michael@0: "Sanity check: path should not exist: " + paths[i]); michael@0: } michael@0: michael@0: file.mkpath(paths[0]); michael@0: assert.ok(file.exists(paths[0]), "mkpath should create path: " + paths[0]); michael@0: michael@0: for (let i = 0; i < paths.length; i++) { michael@0: file.rmdir(paths[i]); michael@0: assert.ok(!file.exists(paths[i]), michael@0: "rmdir should remove path: " + paths[i]); michael@0: } michael@0: }; michael@0: michael@0: exports.testMkpathTwice = function (assert) { michael@0: let dir = profilePath; michael@0: let path = file.join(dir, "test-file-dir"); michael@0: assert.ok(!file.exists(path), michael@0: "Sanity check: path should not exist: " + path); michael@0: file.mkpath(path); michael@0: assert.ok(file.exists(path), "mkpath should create path: " + path); michael@0: file.mkpath(path); michael@0: assert.ok(file.exists(path), michael@0: "After second mkpath, path should still exist: " + path); michael@0: file.rmdir(path); michael@0: assert.ok(!file.exists(path), "rmdir should remove path: " + path); michael@0: }; michael@0: michael@0: exports.testMkpathExistingNondirectory = function (assert) { michael@0: var fname = file.join(profilePath, 'conflict.txt'); michael@0: file.open(fname, "w").close(); michael@0: assert.ok(file.exists(fname), "File should exist"); michael@0: assert.throws(function() file.mkpath(fname), michael@0: /^The path already exists and is not a directory: .+$/, michael@0: "mkpath on file should raise error"); michael@0: file.remove(fname); michael@0: }; michael@0: michael@0: exports.testRmdirNondirectory = function (assert) { michael@0: var fname = file.join(profilePath, 'not-a-dir') michael@0: file.open(fname, "w").close(); michael@0: assert.ok(file.exists(fname), "File should exist"); michael@0: assert.throws(function() { michael@0: file.rmdir(fname); michael@0: }, ERRORS.NOT_A_DIRECTORY, "rmdir on file should raise error"); michael@0: file.remove(fname); michael@0: assert.ok(!file.exists(fname), "File should not exist"); michael@0: assert.throws(function () file.rmdir(fname), michael@0: ERRORS.FILE_NOT_FOUND, michael@0: "rmdir on non-existing file should raise error"); michael@0: }; michael@0: michael@0: exports.testRmdirNonempty = function (assert) { michael@0: let dir = profilePath; michael@0: let path = file.join(dir, "test-file-dir"); michael@0: assert.ok(!file.exists(path), michael@0: "Sanity check: path should not exist: " + path); michael@0: file.mkpath(path); michael@0: let filePath = file.join(path, "file"); michael@0: file.open(filePath, "w").close(); michael@0: assert.ok(file.exists(filePath), michael@0: "Sanity check: path should exist: " + filePath); michael@0: assert.throws(function () file.rmdir(path), michael@0: /^The directory is not empty: .+$/, michael@0: "rmdir on non-empty directory should raise error"); michael@0: file.remove(filePath); michael@0: file.rmdir(path); michael@0: assert.ok(!file.exists(path), "Path should not exist"); michael@0: }; michael@0: michael@0: require('sdk/test').run(exports);