test-file.js 9.2 KB

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