toolkit/components/osfile/tests/xpcshell/test_unique.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 "use strict";
michael@0 2
michael@0 3 Components.utils.import("resource://gre/modules/osfile.jsm");
michael@0 4 Components.utils.import("resource://gre/modules/Task.jsm");
michael@0 5
michael@0 6 function run_test() {
michael@0 7 do_get_profile();
michael@0 8 run_next_test();
michael@0 9 }
michael@0 10
michael@0 11 function testFiles(filename) {
michael@0 12 return Task.spawn(function() {
michael@0 13 const MAX_TRIES = 10;
michael@0 14 let profileDir = OS.Constants.Path.profileDir;
michael@0 15 let path = OS.Path.join(profileDir, filename);
michael@0 16
michael@0 17 // Ensure that openUnique() uses the file name if there is no file with that name already.
michael@0 18 let openedFile = yield OS.File.openUnique(path);
michael@0 19 do_print("\nCreate new file: " + openedFile.path);
michael@0 20 yield openedFile.file.close();
michael@0 21 let exists = yield OS.File.exists(openedFile.path);
michael@0 22 do_check_true(exists);
michael@0 23 do_check_eq(path, openedFile.path);
michael@0 24 let fileInfo = yield OS.File.stat(openedFile.path);
michael@0 25 do_check_true(fileInfo.size == 0);
michael@0 26
michael@0 27 // Ensure that openUnique() creates a new file name using a HEX number, as the original name is already taken.
michael@0 28 openedFile = yield OS.File.openUnique(path);
michael@0 29 do_print("\nCreate unique HEX file: " + openedFile.path);
michael@0 30 yield openedFile.file.close();
michael@0 31 exists = yield OS.File.exists(openedFile.path);
michael@0 32 do_check_true(exists);
michael@0 33 let fileInfo = yield OS.File.stat(openedFile.path);
michael@0 34 do_check_true(fileInfo.size == 0);
michael@0 35
michael@0 36 // Ensure that openUnique() generates different file names each time, using the HEX number algorithm
michael@0 37 let filenames = new Set();
michael@0 38 for (let i=0; i < MAX_TRIES; i++) {
michael@0 39 openedFile = yield OS.File.openUnique(path);
michael@0 40 yield openedFile.file.close();
michael@0 41 filenames.add(openedFile.path);
michael@0 42 }
michael@0 43
michael@0 44 do_check_eq(filenames.size, MAX_TRIES);
michael@0 45
michael@0 46 // Ensure that openUnique() creates a new human readable file name using, as the original name is already taken.
michael@0 47 openedFile = yield OS.File.openUnique(path, {humanReadable : true});
michael@0 48 do_print("\nCreate unique Human Readable file: " + openedFile.path);
michael@0 49 yield openedFile.file.close();
michael@0 50 exists = yield OS.File.exists(openedFile.path);
michael@0 51 do_check_true(exists);
michael@0 52 let fileInfo = yield OS.File.stat(openedFile.path);
michael@0 53 do_check_true(fileInfo.size == 0);
michael@0 54
michael@0 55 // Ensure that openUnique() generates different human readable file names each time
michael@0 56 filenames = new Set();
michael@0 57 for (let i=0; i < MAX_TRIES; i++) {
michael@0 58 openedFile = yield OS.File.openUnique(path, {humanReadable : true});
michael@0 59 yield openedFile.file.close();
michael@0 60 filenames.add(openedFile.path);
michael@0 61 }
michael@0 62
michael@0 63 do_check_eq(filenames.size, MAX_TRIES);
michael@0 64
michael@0 65 let exn;
michael@0 66 try {
michael@0 67 for (let i=0; i < 100; i++) {
michael@0 68 openedFile = yield OS.File.openUnique(path, {humanReadable : true});
michael@0 69 yield openedFile.file.close();
michael@0 70 }
michael@0 71 } catch (ex) {
michael@0 72 exn = ex;
michael@0 73 }
michael@0 74
michael@0 75 do_print("Ensure that this raises the correct error");
michael@0 76 do_check_true(!!exn);
michael@0 77 do_check_true(exn instanceof OS.File.Error);
michael@0 78 do_check_true(exn.becauseExists);
michael@0 79 });
michael@0 80 }
michael@0 81
michael@0 82 add_task(function test_unique() {
michael@0 83 OS.Shared.DEBUG = true;
michael@0 84 // Tests files with extension
michael@0 85 yield testFiles("dummy_unique_file.txt");
michael@0 86 // Tests files with no extension
michael@0 87 yield testFiles("dummy_unique_file_no_ext");
michael@0 88 });

mercurial