Skip to content

Commit 4339218

Browse files
committed
fix: native mkdir not working with mode args
1 parent ac43743 commit 4339218

File tree

5 files changed

+120
-11
lines changed

5 files changed

+120
-11
lines changed

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.

src/fslib_native.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,24 @@ async function _listDir(path, handle, options, callback) {
5050
}
5151
}
5252

53+
// never throws
54+
async function _subDirectoryExists(parentDirHandle, dirName) {
55+
try {
56+
await parentDirHandle.getDirectoryHandle(dirName);
57+
return true;
58+
} catch (e) {
59+
return false;
60+
}
61+
}
5362

54-
async function _mkdir(paretDirHandle, dirName, callback) {
63+
async function _mkdir(parentDirHandle, dirName, callback) {
5564
try {
56-
let childDirHandle = await paretDirHandle.getDirectoryHandle(dirName, { create: true });
65+
let alreadyExists = await _subDirectoryExists(parentDirHandle, dirName);
66+
if(alreadyExists){
67+
callback(new Errors.EEXIST(`Folder ${dirName} already exists`));
68+
return ;
69+
}
70+
let childDirHandle = await parentDirHandle.getDirectoryHandle(dirName, { create: true });
5771
if(callback){
5872
callback(null);
5973
}
@@ -68,7 +82,7 @@ async function _mkdir(paretDirHandle, dirName, callback) {
6882

6983

7084
function mkdir(path, mode, callback) {
71-
if (arguments.length < 4) {
85+
if (arguments.length < 3) {
7286
callback = mode;
7387
}
7488

test/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
function mountNative() {
2727
fs.mountNativeFolder((err, mountTestPath)=>{
2828
if(!mountTestPath[0]) return;
29-
window.mountTestPath = mountTestPath[0];
3029
localStorage.setItem('mountTestPath', mountTestPath[0]);
3130
document.getElementById('openFolderButton').style = "display:none";
31+
window.mountTestPath = `${mountTestPath[0]}/test`;
3232
mocha.run();
3333
});
3434
}
@@ -38,18 +38,19 @@
3838
mountNative();
3939
return;
4040
}
41+
window.virtualTestPath = '/test';
4142
fs.readdir(mountTestPath, (err, contents)=>{
4243
console.log("Checking if any mounted paths exists: ",err, contents);
4344
if(err){
4445
mountNative();
4546
return;
4647
}
47-
window.mountTestPath = mountTestPath;
48+
window.mountTestPath = `${mountTestPath}/test`;
4849
document.getElementById('openFolderButton').style = "display:none";
4950
mocha.run();
5051
});
5152
}
5253
</script>
53-
<button id="openFolderButton" onclick="openFolderAndRunTests()">Open any blank folder to start tests</button>
54+
<button id="openFolderButton" onclick="openFolderAndRunTests()">Open any blank folder to start tests. Warning - folder contents will be deleted!!!!</button>
5455
</body>
5556
</html>

test/test.browser.js

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,35 @@ describe('Browser main tests', function () {
2727
expect(Filer.fs.name).to.equal('local');
2828
});
2929

30-
it('Should load Phoenix fs in browser', function () {
30+
it('Should load Phoenix fs in browser',async function () {
3131
expect(fs).to.exist;
3232
expect(fs.name).to.equal('phoenixFS');
33+
// setup test folders
34+
console.log('cleaning: ', window.virtualTestPath);
35+
let cleanSuccess = false;
36+
fs.unlink(window.virtualTestPath, ()=>{
37+
cleanSuccess = true;
38+
});
39+
await waitForTrue(()=>{return cleanSuccess;},10000);
40+
console.log('cleaning: ', window.mountTestPath);
41+
cleanSuccess = false;
42+
fs.unlink(window.mountTestPath, ()=>{
43+
cleanSuccess = true;
44+
});
45+
await waitForTrue(()=>{return cleanSuccess;},10000);
46+
47+
console.log('mkdir: ', window.virtualTestPath);
48+
cleanSuccess = false;
49+
fs.mkdirs(window.virtualTestPath, 777 ,true, ()=>{
50+
cleanSuccess = true;
51+
});
52+
await waitForTrue(()=>{return cleanSuccess;},10000);
53+
console.log('mkdir: ', window.mountTestPath);
54+
cleanSuccess = false;
55+
fs.mkdirs(window.mountTestPath, 777 ,true,()=>{
56+
cleanSuccess = true;
57+
});
58+
await waitForTrue(()=>{return cleanSuccess;},10000);
3359
});
3460

3561
it('Should phoenix native write in browser', async function () {
@@ -39,7 +65,7 @@ describe('Browser main tests', function () {
3965
writeSuccess = true;
4066
}
4167
});
42-
await waitForTrue(()=>{return writeSuccess;},1000);
68+
await waitForTrue(()=>{return writeSuccess;},10000);
4369
expect(writeSuccess).to.be.true;
4470
});
4571

@@ -64,7 +90,7 @@ describe('Browser main tests', function () {
6490
});
6591
await waitForTrue(()=>{return readSuccess;},1000);
6692
expect(readSuccess).to.be.true;
67-
expect(contentsRead.length).to.be.above(1);
93+
expect(contentsRead.length).to.equal(1);
6894
});
6995

7096
it('Should phoenix native read dir with withFileTypes', async function () {
@@ -90,4 +116,72 @@ describe('Browser main tests', function () {
90116
await waitForTrue(()=>{return delSuccess;},1000);
91117
expect(delSuccess).to.be.true;
92118
});
119+
120+
it('Should phoenix mkdir(path,cb) in browser if it doesnt exist', async function () {
121+
// mount fs
122+
let createSuccess = false;
123+
fs.mkdir(`${window.mountTestPath}/testDir`, (err)=>{
124+
if(!err){
125+
createSuccess = true;
126+
}
127+
});
128+
await waitForTrue(()=>{return createSuccess;},1000);
129+
expect(createSuccess).to.be.true;
130+
// virtual fs
131+
createSuccess = false;
132+
fs.mkdir(`${window.virtualTestPath}/testDir`, (err)=>{
133+
if(!err){
134+
createSuccess = true;
135+
}
136+
});
137+
await waitForTrue(()=>{return createSuccess;},1000);
138+
expect(createSuccess).to.be.true;
139+
});
140+
141+
it('Should phoenix mount:mkdir(path,mode, cb) in browser if it doesnt exist', async function () {
142+
// mount fs
143+
let createSuccess = false;
144+
fs.mkdir(`${window.mountTestPath}/testDir1`, 777, (err)=>{
145+
if(!err){
146+
createSuccess = true;
147+
}
148+
});
149+
await waitForTrue(()=>{return createSuccess;},1000);
150+
expect(createSuccess).to.be.true;
151+
});
152+
it('Should phoenix virtual:mkdir(path,mode, cb) in browser if it doesnt exist', async function () {
153+
// virtual fs
154+
let createSuccess = false;
155+
fs.mkdir(`${window.virtualTestPath}/testDir1`, 777, (err)=>{
156+
if(!err){
157+
createSuccess = true;
158+
}
159+
});
160+
await waitForTrue(()=>{return createSuccess;},1000);
161+
expect(createSuccess).to.be.true;
162+
});
163+
164+
it('Should phoenix fail mount:mkdir(path,mode, cb) if already exists', async function () {
165+
// mount fs
166+
let failed = false;
167+
fs.mkdir(`${window.mountTestPath}/testDir1`, 777, (err)=>{
168+
if(err){
169+
failed = true;
170+
}
171+
});
172+
await waitForTrue(()=>{return failed;},1000);
173+
expect(failed).to.be.true;
174+
});
175+
176+
it('Should phoenix fail virtual:mkdir(path,mode, cb) if already exists', async function () {
177+
// virtual fs
178+
let failed = false;
179+
fs.mkdir(`${window.virtualTestPath}/testDir1`, 777, (err)=>{
180+
if(err){
181+
failed = true;
182+
}
183+
});
184+
await waitForTrue(()=>{return failed;},1000);
185+
expect(failed).to.be.true;
186+
});
93187
});

0 commit comments

Comments
 (0)