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