Skip to content

Commit ca1dbed

Browse files
committed
fs read write tests
1 parent 14b28b9 commit ca1dbed

11 files changed

Lines changed: 130 additions & 15 deletions

File tree

LICENSE-thirdParty

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## thirparty/buffer-min.js
2+
3+
The buffer module from node.js, for the browser.
4+
5+
6+
Git: https://github.com/feross/buffer
7+
Copyright (C) Feross Aboukhadijeh, and other contributors.
8+
Originally forked from an MIT-licensed module by Romain Beauxis.
9+
MIT License
10+
11+
## thirparty/idb-min.js
12+
ISC License (ISC)
13+
Copyright (c) 2016, Jake Archibald <jaffathecake@gmail.com>
14+
15+
## thirparty/globmatch.js
16+
modified version of https://www.npmjs.com/package/minimatch
17+
ISC License (ISC)

dist/virtualfs.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/virtualfs.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"testDist": "npm run build && open-cli http://127.0.0.1:8080/test/index-dist.html",
1818
"test": "open-cli http://127.0.0.1:8080/test/index.html",
1919
"build-sw": "parcel build src/virtualfs.js --detailed-report",
20+
"build-nonopt": "parcel build src/virtualfs.js --detailed-report --no-optimize",
2021
"build": "npm run eslint && npm run build-sw",
22+
"build:dev": "npm run eslint && npm run build-nonopt",
2123
"predev": "npm run build",
2224
"serve": "http-server . -p 8080 -c-1",
2325
"bumpPatchVersion": "npm --no-git-tag-version version patch",

src/fslib_watch.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
// jshint ignore: start
20-
/*global BroadcastChannel*/
20+
/*global BroadcastChannel, globalObject*/
2121
/*eslint no-console: 0*/
2222
/*eslint strict: ["error", "global"]*/
2323

@@ -37,7 +37,7 @@ function _setupBroadcastChannel() {
3737
}
3838
if(typeof BroadcastChannel === 'undefined'){
3939
/* eslint no-console: 0 */
40-
console.warn('window.BroadcastChannel not supported. File system watch events across tabs wont be synced.');
40+
console.warn('BroadcastChannel not supported. File system watch events across tabs wont be synced.');
4141
return;
4242
}
4343
_channel = new BroadcastChannel(WATCH_EVENT_NOTIFICATION);
@@ -60,7 +60,7 @@ function _isAnIgnoredPath(path, ignoreGlobList) {
6060
}
6161

6262
function _isSameOrSubDirectory(parent, child) {
63-
return !(window.path.relative(parent, child).startsWith('..'));
63+
return !(globalObject.path.relative(parent, child).startsWith('..'));
6464
}
6565

6666
// event{ path, eventName}
@@ -97,7 +97,7 @@ function watch(path, ignoreGlobList, changeCallback, callback) {
9797
}
9898

9999
function _triggerEvent(path, eventType) {
100-
let pathLib = window.path;
100+
let pathLib = globalObject.path;
101101
path = pathLib.normalize(path);
102102
let event = {
103103
event: eventType,

src/thirdparty/buffer-min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/virtualfs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*global globalObject, virtualfs*/
22

33
const Filer = require('filer');
4+
const buffer = require('./thirdparty/buffer-min');
45

56
let virtualGlobalObject = {};
67
let env = 'browser';
@@ -27,6 +28,7 @@ setupGlobalObject();
2728

2829
const urlParams = new URLSearchParams(location.search);
2930
globalObject.Filer = Filer;
31+
globalObject.buffer = buffer;
3032
globalObject.virtualfs = {
3133
urlParams : urlParams,
3234
debugMode : urlParams.get('debug'),

test/index.html

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,37 @@
1919

2020
<script src="../dist/virtualfs.js"></script>
2121
<script src="testInit.js"></script>
22-
<script src="test.worker.js"></script>
2322
<script src="test.browser.js"></script>
23+
<script src="test.worker.js"></script>
2424
<script class="mocha-exec">
25-
mocha.run();
25+
function openFolderAndRunTests() {
26+
function mountNative() {
27+
fs.mountNativeFolder((err, mountTestPath)=>{
28+
if(!mountTestPath[0]) return;
29+
window.mountTestPath = mountTestPath[0];
30+
localStorage.setItem('mountTestPath', mountTestPath[0]);
31+
document.getElementById('openFolderButton').style = "display:none";
32+
mocha.run();
33+
});
34+
}
35+
36+
let mountTestPath = localStorage.getItem('mountTestPath');
37+
if(!mountTestPath) {
38+
mountNative();
39+
return;
40+
}
41+
fs.readdir(mountTestPath, (err, contents)=>{
42+
console.log("Checking if any mounted paths exists: ",err, contents);
43+
if(err){
44+
mountNative();
45+
return;
46+
}
47+
window.mountTestPath = mountTestPath;
48+
document.getElementById('openFolderButton').style = "display:none";
49+
mocha.run();
50+
});
51+
}
2652
</script>
53+
<button id="openFolderButton" onclick="openFolderAndRunTests()">Open any blank folder to start tests</button>
2754
</body>
2855
</html>

test/test.browser.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
/* global expect , Filer, fs*/
22

33
describe('Browser main tests', function () {
4+
async function waitForTrue(checkFn, timeoutMs) {
5+
let startTime = Date.now();
6+
return new Promise((resolve)=>{
7+
let interVal;
8+
function checkMessage() {
9+
if(checkFn() === true){
10+
resolve(true);
11+
clearInterval(interVal);
12+
}
13+
let elapsedTime = Date.now() - startTime;
14+
if(elapsedTime > timeoutMs){
15+
resolve(false);
16+
clearInterval(interVal);
17+
}
18+
}
19+
interVal = setInterval(checkMessage, 10);
20+
});
21+
}
22+
423
it('Should load Filer in browser', function () {
524
expect(Filer).to.exist;
625
expect(Filer.fs).to.exist;
@@ -12,4 +31,14 @@ describe('Browser main tests', function () {
1231
expect(fs).to.exist;
1332
expect(fs.name).to.equal('phoenixFS');
1433
});
34+
35+
it('Should phoenix native read write in browser', async function () {
36+
let writeSuccess = false;
37+
fs.writeFile(`${window.mountTestPath}/browserWrite.txt`, 'hello World', 'utf8', (err)=>{
38+
if(!err){
39+
writeSuccess = true;
40+
}
41+
});
42+
waitForTrue(()=>{return writeSuccess;},1);
43+
});
1544
});

test/test.worker.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
1-
/* global expect */
1+
/* global expect,fs */
22

33
describe('web worker tests', function () {
4-
let worker = new Worker('worker-task.js?debug=true');
5-
console.log(worker);
4+
let worker;
65
let messageFromWorker = null;
7-
worker.onmessage= function (event) {
8-
console.log('From Worker:', event);
9-
messageFromWorker = event.data;
10-
};
6+
async function _requestWritePerm() {
7+
return new Promise((resolve, reject)=>{
8+
fs.writeFile(`${window.mountTestPath}/forTestPermissionOnFolder.txt`, 'hello World', 'utf8', (err)=>{
9+
if(err){
10+
console.log(err);
11+
reject(err);
12+
} else {
13+
resolve();
14+
}
15+
});
16+
});
17+
}
18+
before(async function () {
19+
await _requestWritePerm();
20+
worker = new Worker(`worker-task.js?debug=true&mountTestPath=${window.mountTestPath}`);
21+
console.log(worker);
22+
worker.onmessage= function (event) {
23+
console.log('From Worker:', event);
24+
messageFromWorker = event.data;
25+
};
26+
});
1127

1228
async function waitForWorkerMessage(message, timeoutMs) {
1329
let startTime = Date.now();
@@ -41,4 +57,11 @@ describe('web worker tests', function () {
4157
let status = await waitForWorkerMessage('phoenixFsCheck.ok', 1000);
4258
expect(status).to.be.true;
4359
});
60+
61+
it('Should phoenix native read write in worker', async function () {
62+
messageFromWorker = null;
63+
worker.postMessage('RWMountCheck');
64+
let status = await waitForWorkerMessage('RWMountCheck.ok', 1000);
65+
expect(status).to.be.true;
66+
});
4467
});

0 commit comments

Comments
 (0)