toolkit/components/osfile/tests/xpcshell/test_osfile_writeAtomic_backupTo_option.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:7753ca7a33e8
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 "use strict";
5
6 let {OS: {File, Path, Constants}} = Components.utils.import("resource://gre/modules/osfile.jsm", {});
7 Components.utils.import("resource://gre/modules/Task.jsm");
8
9 /**
10 * Remove all temporary files and back up files, including
11 * test_backupTo_option_with_tmpPath.tmp
12 * test_backupTo_option_with_tmpPath.tmp.backup
13 * test_backupTo_option_without_tmpPath.tmp
14 * test_backupTo_option_without_tmpPath.tmp.backup
15 * test_non_backupTo_option.tmp
16 * test_non_backupTo_option.tmp.backup
17 * test_backupTo_option_without_destination_file.tmp
18 * test_backupTo_option_without_destination_file.tmp.backup
19 * test_backupTo_option_with_backup_file.tmp
20 * test_backupTo_option_with_backup_file.tmp.backup
21 */
22 function clearFiles() {
23 return Task.spawn(function () {
24 let files = ["test_backupTo_option_with_tmpPath.tmp",
25 "test_backupTo_option_without_tmpPath.tmp",
26 "test_non_backupTo_option.tmp",
27 "test_backupTo_option_without_destination_file.tmp",
28 "test_backupTo_option_with_backup_file.tmp"];
29 for (let file of files) {
30 let path = Path.join(Constants.Path.tmpDir, file);
31 yield File.remove(path);
32 yield File.remove(path + ".backup");
33 }
34 });
35 }
36
37 function run_test() {
38 run_next_test();
39 }
40
41 add_task(function* init() {
42 yield clearFiles();
43 });
44
45 /**
46 * test when
47 * |backupTo| specified
48 * |tmpPath| specified
49 * destination file exists
50 * @result destination file will be backed up
51 */
52 add_task(function* test_backupTo_option_with_tmpPath() {
53 let DEFAULT_CONTENTS = "default contents" + Math.random();
54 let WRITE_CONTENTS = "abc" + Math.random();
55 let path = Path.join(Constants.Path.tmpDir,
56 "test_backupTo_option_with_tmpPath.tmp");
57 yield File.writeAtomic(path, DEFAULT_CONTENTS);
58 yield File.writeAtomic(path, WRITE_CONTENTS, { tmpPath: path + ".tmp",
59 backupTo: path + ".backup" });
60 do_check_true((yield File.exists(path + ".backup")));
61 let contents = yield File.read(path + ".backup");
62 do_check_eq(DEFAULT_CONTENTS, (new TextDecoder()).decode(contents));
63 });
64
65 /**
66 * test when
67 * |backupTo| specified
68 * |tmpPath| not specified
69 * destination file exists
70 * @result destination file will be backed up
71 */
72 add_task(function* test_backupTo_option_without_tmpPath() {
73 let DEFAULT_CONTENTS = "default contents" + Math.random();
74 let WRITE_CONTENTS = "abc" + Math.random();
75 let path = Path.join(Constants.Path.tmpDir,
76 "test_backupTo_option_without_tmpPath.tmp");
77 yield File.writeAtomic(path, DEFAULT_CONTENTS);
78 yield File.writeAtomic(path, WRITE_CONTENTS, { backupTo: path + ".backup" });
79 do_check_true((yield File.exists(path + ".backup")));
80 let contents = yield File.read(path + ".backup");
81 do_check_eq(DEFAULT_CONTENTS, (new TextDecoder()).decode(contents));
82 });
83
84 /**
85 * test when
86 * |backupTo| not specified
87 * |tmpPath| not specified
88 * destination file exists
89 * @result destination file will not be backed up
90 */
91 add_task(function* test_non_backupTo_option() {
92 let DEFAULT_CONTENTS = "default contents" + Math.random();
93 let WRITE_CONTENTS = "abc" + Math.random();
94 let path = Path.join(Constants.Path.tmpDir,
95 "test_non_backupTo_option.tmp");
96 yield File.writeAtomic(path, DEFAULT_CONTENTS);
97 yield File.writeAtomic(path, WRITE_CONTENTS);
98 do_check_false((yield File.exists(path + ".backup")));
99 });
100
101 /**
102 * test when
103 * |backupTo| specified
104 * |tmpPath| not specified
105 * destination file not exists
106 * @result no back up file exists
107 */
108 add_task(function* test_backupTo_option_without_destination_file() {
109 let DEFAULT_CONTENTS = "default contents" + Math.random();
110 let WRITE_CONTENTS = "abc" + Math.random();
111 let path = Path.join(Constants.Path.tmpDir,
112 "test_backupTo_option_without_destination_file.tmp");
113 yield File.remove(path);
114 yield File.writeAtomic(path, WRITE_CONTENTS, { backupTo: path + ".backup" });
115 do_check_false((yield File.exists(path + ".backup")));
116 });
117
118 /**
119 * test when
120 * |backupTo| specified
121 * |tmpPath| not specified
122 * backup file exists
123 * destination file exists
124 * @result destination file will be backed up
125 */
126 add_task(function* test_backupTo_option_with_backup_file() {
127 let DEFAULT_CONTENTS = "default contents" + Math.random();
128 let WRITE_CONTENTS = "abc" + Math.random();
129 let path = Path.join(Constants.Path.tmpDir,
130 "test_backupTo_option_with_backup_file.tmp");
131 yield File.writeAtomic(path, DEFAULT_CONTENTS);
132
133 yield File.writeAtomic(path + ".backup", new Uint8Array(1000));
134
135 yield File.writeAtomic(path, WRITE_CONTENTS, { backupTo: path + ".backup" });
136 do_check_true((yield File.exists(path + ".backup")));
137 let contents = yield File.read(path + ".backup");
138 do_check_eq(DEFAULT_CONTENTS, (new TextDecoder()).decode(contents));
139 });
140
141 add_task(function* cleanup() {
142 yield clearFiles();
143 });

mercurial