-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest-copy.browser.js
More file actions
334 lines (300 loc) · 11.1 KB
/
test-copy.browser.js
File metadata and controls
334 lines (300 loc) · 11.1 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* global expect , Filer, fs, waitForTrue, TEST_TYPE_FS_ACCESS, TEST_TYPE_FILER, TEST_TYPE_TAURI*/
function _setupTests(testTypeSrc, testTypeDst) {
function consoleLogToShell(message) {
if(window.__TAURI__) {
return window.__TAURI__.invoke("console_log", {message});
}
if(window.__ELECTRON__) {
window.electronAPI.consoleLog(message);
return Promise.resolve();
}
return Promise.resolve();
}
async function _getTestBaseDir() {
if(window.__TAURI__) {
return window.__TAURI__.path.appLocalDataDir();
}
if(window.__ELECTRON__) {
return window.electronAPI.appLocalDataDir();
}
throw new Error("No native environment detected");
}
async function _getPathForTestType(testType) {
let testPath;
switch (testType) {
case TEST_TYPE_FS_ACCESS: return window.mountTestPath;
case TEST_TYPE_FILER: return window.virtualTestPath;
case TEST_TYPE_TAURI:
testPath = fs.getTauriVirtualPath(`${await _getTestBaseDir()}test-phoenix-fs`);
consoleLogToShell("using tauri test path: "+ testPath);
return testPath;
default: throw new Error("unknown file system impl");
}
}
let srcTestPath, destTestPath;
async function _clean() {
console.log(`cleaning: `, srcTestPath);
let cleanSuccess = false;
fs.unlink(srcTestPath, ()=>{
cleanSuccess = true;
});
await waitForTrue(()=>{return cleanSuccess;},10000);
console.log(`cleaning: `, destTestPath);
cleanSuccess = false;
fs.unlink(destTestPath, ()=>{
cleanSuccess = true;
});
await waitForTrue(()=>{return cleanSuccess;},10000);
console.log(`cleaning: `, destTestPath);
cleanSuccess = false;
fs.unlink(await _getPathForTestType(testTypeSrc), ()=>{
cleanSuccess = true;
});
await waitForTrue(()=>{return cleanSuccess;},10000);
}
before(async function () {
this.timeout(15000);
if (window.__TAURI__ || window.__ELECTRON__) {
await window.waitForTrue(()=>{return window.isNodeSetup;}, 10000);
}
srcTestPath = await _getPathForTestType(testTypeSrc) + '/src';
destTestPath = await _getPathForTestType(testTypeDst)+ '/dest';
});
beforeEach(async function () {
this.timeout(15000);
// setup test folders
await _clean();
console.log(`mkdir: `, srcTestPath);
let makeSuccess = false;
let makeError = null;
fs.mkdirs(srcTestPath, 0o777 ,true, (err)=>{
makeError = err;
makeSuccess = true;
});
await waitForTrue(()=>{return makeSuccess;},10000);
if (makeError) {
console.error(`Failed to create srcTestPath: `, makeError);
}
console.log(`mkdir: `, destTestPath);
makeSuccess = false;
makeError = null;
fs.mkdirs(destTestPath, 0o777 ,true, (err)=>{
makeError = err;
makeSuccess = true;
});
await waitForTrue(()=>{return makeSuccess;},10000);
if (makeError) {
console.error(`Failed to create destTestPath: `, makeError);
}
});
afterEach(async function () {
await _clean();
});
it(`Should load Filer in browser`, function () {
expect(Filer).to.exist;
expect(Filer.fs).to.exist;
expect(Filer.Shell).to.exist;
expect(Filer.fs.name).to.equal(`local`);
});
it(`Should load clean Phoenix fs in browser`,async function () {
expect(fs).to.exist;
expect(fs.name).to.equal(`phoenixFS`);
}, 10000);
function _createFolder(path) {
return new Promise((resolve, reject)=>{
fs.mkdir(path, 0o777, (err)=>{
if(err){
reject(err);
} else {
resolve();
}
});
});
}
function _getContent() {
return `hello World`;
}
function _createFile(path) {
return new Promise((resolve, reject)=>{
fs.writeFile(path, _getContent(), `utf8`, (err)=>{
if(!err){
resolve();
} else {
reject();
}
});
});
}
async function _verifyCreatedFile(path) {
let resolveP, rejectP;
const promise = new Promise((resolve, reject) => {resolveP = resolve;rejectP=reject;});
fs.readFile(path, `utf8`, (_err, _content)=>{
if(_err){
rejectP();
}
resolveP(_content);
});
const content = await promise;
expect(content).to.eql(_getContent());
}
function _shouldExist(path) {
return new Promise((resolve)=>{
fs.stat(path, (err)=>{
if(err){
resolve(false);
} else {
resolve(true);
}
});
});
}
const TEST_FOLDERS = ["y", "z"];
const TEST_FILES = ["a.txt", "b.txt", "y/c.txt"];
async function _createTestFolderInBasePath(path, folderName) {
await _createFolder(`${path}/${folderName}`);
for(let folder of TEST_FOLDERS){
await _createFolder(`${path}/${folderName}/${folder}`);
}
for(let file of TEST_FILES){
await _createFile(`${path}/${folderName}/${file}`);
}
}
async function _verifyCopyFolder(srcPath, dstPath, expectedPath) {
let success = false;
let actualCopiedPath = null;
fs.copy(srcPath, dstPath, (err, path)=>{
if(!err){
success = true;
}
actualCopiedPath = path;
});
await waitForTrue(()=>{return success;},1000);
expect(success).to.be.true;
expectedPath = expectedPath || dstPath;
for(let folder of TEST_FOLDERS){
expect(await _shouldExist(`${expectedPath}/${folder}`)).to.be.true;
}
for(let file of TEST_FILES){
await _verifyCreatedFile(`${expectedPath}/${file}`);
}
expect(actualCopiedPath).to.equal(expectedPath);
}
it(`Should phoenix copy fail if dst is a subpath of src`, async function () {
let errored = false;
fs.copy(`/a`, `/a/b`, (err)=>{
if(err){
errored = true;
}
});
await waitForTrue(()=>{return errored;},1000);
expect(errored).to.be.true;
});
// folder copy tests
it(`Should phoenix copy folder from ${testTypeSrc} to ${testTypeDst} path`, async function () {
await _createTestFolderInBasePath(srcTestPath, `testDir4`);
let srcPath = `${srcTestPath}/testDir4`;
let dstPath = `${destTestPath}/testDir4`;
await _verifyCopyFolder(srcPath, dstPath);
});
it(`Should phoenix copy overwrite folder from ${testTypeSrc} to ${testTypeDst} path if already exist`, async function () {
await _createTestFolderInBasePath(srcTestPath, `testDir5`);
let srcPath = `${srcTestPath}/testDir5`;
let dstPath = `${destTestPath}/testDir5`;
let expectedPath = `${destTestPath}/testDir5/testDir5`;
await _createFolder(dstPath);
await _verifyCopyFolder(srcPath, dstPath, expectedPath);
});
// file copy tests
async function _verifyCopyFile(srcPath, dstPath, expectedPath) {
let success = false;
let actualCopiedPath = null;
fs.copy(srcPath, dstPath, (err, path)=>{
if(!err){
success = true;
}
actualCopiedPath = path;
});
await waitForTrue(()=>{return success;},1000);
expect(success).to.be.true;
expectedPath = expectedPath || dstPath;
expect(await _shouldExist(expectedPath)).to.be.true;
expect(actualCopiedPath).to.equal(expectedPath);
}
it(`Should phoenix copy file from ${testTypeSrc} to ${testTypeDst} path`, async function () {
let dir = `testDir10.5`;
await _createTestFolderInBasePath(srcTestPath, dir);
let srcPath = `${srcTestPath}/${dir}/a.txt`;
let dstFolder = `${destTestPath}/${dir}`;
await _createFolder(dstFolder);
let dstPath = `${dstFolder}/temp.txt`;
await _verifyCopyFile(srcPath, dstPath);
});
it(`Should phoenix copy file within same ${testTypeSrc} fs path`, async function () {
let dir = `testDir12`;
await _createTestFolderInBasePath(srcTestPath, dir);
let srcPath = `${srcTestPath}/${dir}/a.txt`;
let dstPath = `${srcTestPath}/${dir}/lols.txt`;
await _verifyCopyFile(srcPath, dstPath);
});
it(`Should phoenix fail copy file within same ${testTypeSrc} if dest file exist`, async function () {
let dir = `testDir13`;
await _createTestFolderInBasePath(srcTestPath, dir);
let srcPath = `${srcTestPath}/${dir}/a.txt`;
let dstFile = `${srcTestPath}/${dir}/jj.txt`;
await _createFile(dstFile);
let fail = false;
fs.copy(srcPath, dstFile, (err)=>{
if(err){
fail = true;
}
});
await waitForTrue(()=>{return fail;},1000);
expect(fail).to.be.true;
});
}
describe(`Browser copy tests from "filer" to "filer"`, function () {
_setupTests(TEST_TYPE_FILER, TEST_TYPE_FILER);
});
if(window.__TAURI__ || window.__ELECTRON__){
describe(`Browser copy tests from "tauri" to "tauri"`, function () {
_setupTests(TEST_TYPE_TAURI, TEST_TYPE_TAURI);
});
}
if(window.supportsFsAccessAPIs){
describe(`Browser copy tests from "fs access" to "fs access"`, function () {
if(window.__TAURI__ || window.__ELECTRON__){
it(`fs access tests are disabled in tauri/electron`, function () {});
return;
}
_setupTests(TEST_TYPE_FS_ACCESS, TEST_TYPE_FS_ACCESS);
});
}
// between filer and fs access start
if(window.supportsFsAccessAPIs){
describe(`Browser copy tests from "filer" to "fs access"`, function () {
if(window.__TAURI__ || window.__ELECTRON__){
it(`fs access tests are disabled in tauri/electron`, function () {});
return;
}
_setupTests(TEST_TYPE_FILER, TEST_TYPE_FS_ACCESS);
});
describe(`Browser copy tests from "fs access" to "filer"`, function () {
if(window.__TAURI__ || window.__ELECTRON__){
it(`fs access tests are disabled in tauri/electron`, function () {});
return;
}
_setupTests(TEST_TYPE_FS_ACCESS, TEST_TYPE_FILER);
});
}
// between filer and fs access end
// between filer and tauri start
if(window.__TAURI__ || window.__ELECTRON__){
describe(`Browser copy tests from "filer" to "tauri"`, function () {
_setupTests(TEST_TYPE_FILER, TEST_TYPE_TAURI);
});
describe(`Browser copy tests from "tauri" to "filer"`, function () {
_setupTests(TEST_TYPE_TAURI, TEST_TYPE_FILER);
});
}
// between filer and tauri end
// tauri and fs access are never enabled within the same context. So no tests for that.