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