Skip to content

Commit

Permalink
fix: native mkdir not working with mode args
Browse files Browse the repository at this point in the history
  • Loading branch information
abose committed Jun 19, 2022
1 parent ac43743 commit 4339218
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 11 deletions.
2 changes: 1 addition & 1 deletion dist/virtualfs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/virtualfs.js.map

Large diffs are not rendered by default.

20 changes: 17 additions & 3 deletions src/fslib_native.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,24 @@ async function _listDir(path, handle, options, callback) {
}
}

// never throws
async function _subDirectoryExists(parentDirHandle, dirName) {
try {
await parentDirHandle.getDirectoryHandle(dirName);
return true;
} catch (e) {
return false;
}
}

async function _mkdir(paretDirHandle, dirName, callback) {
async function _mkdir(parentDirHandle, dirName, callback) {
try {
let childDirHandle = await paretDirHandle.getDirectoryHandle(dirName, { create: true });
let alreadyExists = await _subDirectoryExists(parentDirHandle, dirName);
if(alreadyExists){
callback(new Errors.EEXIST(`Folder ${dirName} already exists`));
return ;
}
let childDirHandle = await parentDirHandle.getDirectoryHandle(dirName, { create: true });
if(callback){
callback(null);
}
Expand All @@ -68,7 +82,7 @@ async function _mkdir(paretDirHandle, dirName, callback) {


function mkdir(path, mode, callback) {
if (arguments.length < 4) {
if (arguments.length < 3) {
callback = mode;
}

Expand Down
7 changes: 4 additions & 3 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
function mountNative() {
fs.mountNativeFolder((err, mountTestPath)=>{
if(!mountTestPath[0]) return;
window.mountTestPath = mountTestPath[0];
localStorage.setItem('mountTestPath', mountTestPath[0]);
document.getElementById('openFolderButton').style = "display:none";
window.mountTestPath = `${mountTestPath[0]}/test`;
mocha.run();
});
}
Expand All @@ -38,18 +38,19 @@
mountNative();
return;
}
window.virtualTestPath = '/test';
fs.readdir(mountTestPath, (err, contents)=>{
console.log("Checking if any mounted paths exists: ",err, contents);
if(err){
mountNative();
return;
}
window.mountTestPath = mountTestPath;
window.mountTestPath = `${mountTestPath}/test`;
document.getElementById('openFolderButton').style = "display:none";
mocha.run();
});
}
</script>
<button id="openFolderButton" onclick="openFolderAndRunTests()">Open any blank folder to start tests</button>
<button id="openFolderButton" onclick="openFolderAndRunTests()">Open any blank folder to start tests. Warning - folder contents will be deleted!!!!</button>
</body>
</html>
100 changes: 97 additions & 3 deletions test/test.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,35 @@ describe('Browser main tests', function () {
expect(Filer.fs.name).to.equal('local');
});

it('Should load Phoenix fs in browser', function () {
it('Should load Phoenix fs in browser',async function () {
expect(fs).to.exist;
expect(fs.name).to.equal('phoenixFS');
// setup test folders
console.log('cleaning: ', window.virtualTestPath);
let cleanSuccess = false;
fs.unlink(window.virtualTestPath, ()=>{
cleanSuccess = true;
});
await waitForTrue(()=>{return cleanSuccess;},10000);
console.log('cleaning: ', window.mountTestPath);
cleanSuccess = false;
fs.unlink(window.mountTestPath, ()=>{
cleanSuccess = true;
});
await waitForTrue(()=>{return cleanSuccess;},10000);

console.log('mkdir: ', window.virtualTestPath);
cleanSuccess = false;
fs.mkdirs(window.virtualTestPath, 777 ,true, ()=>{
cleanSuccess = true;
});
await waitForTrue(()=>{return cleanSuccess;},10000);
console.log('mkdir: ', window.mountTestPath);
cleanSuccess = false;
fs.mkdirs(window.mountTestPath, 777 ,true,()=>{
cleanSuccess = true;
});
await waitForTrue(()=>{return cleanSuccess;},10000);
});

it('Should phoenix native write in browser', async function () {
Expand All @@ -39,7 +65,7 @@ describe('Browser main tests', function () {
writeSuccess = true;
}
});
await waitForTrue(()=>{return writeSuccess;},1000);
await waitForTrue(()=>{return writeSuccess;},10000);
expect(writeSuccess).to.be.true;
});

Expand All @@ -64,7 +90,7 @@ describe('Browser main tests', function () {
});
await waitForTrue(()=>{return readSuccess;},1000);
expect(readSuccess).to.be.true;
expect(contentsRead.length).to.be.above(1);
expect(contentsRead.length).to.equal(1);
});

it('Should phoenix native read dir with withFileTypes', async function () {
Expand All @@ -90,4 +116,72 @@ describe('Browser main tests', function () {
await waitForTrue(()=>{return delSuccess;},1000);
expect(delSuccess).to.be.true;
});

it('Should phoenix mkdir(path,cb) in browser if it doesnt exist', async function () {
// mount fs
let createSuccess = false;
fs.mkdir(`${window.mountTestPath}/testDir`, (err)=>{
if(!err){
createSuccess = true;
}
});
await waitForTrue(()=>{return createSuccess;},1000);
expect(createSuccess).to.be.true;
// virtual fs
createSuccess = false;
fs.mkdir(`${window.virtualTestPath}/testDir`, (err)=>{
if(!err){
createSuccess = true;
}
});
await waitForTrue(()=>{return createSuccess;},1000);
expect(createSuccess).to.be.true;
});

it('Should phoenix mount:mkdir(path,mode, cb) in browser if it doesnt exist', async function () {
// mount fs
let createSuccess = false;
fs.mkdir(`${window.mountTestPath}/testDir1`, 777, (err)=>{
if(!err){
createSuccess = true;
}
});
await waitForTrue(()=>{return createSuccess;},1000);
expect(createSuccess).to.be.true;
});
it('Should phoenix virtual:mkdir(path,mode, cb) in browser if it doesnt exist', async function () {
// virtual fs
let createSuccess = false;
fs.mkdir(`${window.virtualTestPath}/testDir1`, 777, (err)=>{
if(!err){
createSuccess = true;
}
});
await waitForTrue(()=>{return createSuccess;},1000);
expect(createSuccess).to.be.true;
});

it('Should phoenix fail mount:mkdir(path,mode, cb) if already exists', async function () {
// mount fs
let failed = false;
fs.mkdir(`${window.mountTestPath}/testDir1`, 777, (err)=>{
if(err){
failed = true;
}
});
await waitForTrue(()=>{return failed;},1000);
expect(failed).to.be.true;
});

it('Should phoenix fail virtual:mkdir(path,mode, cb) if already exists', async function () {
// virtual fs
let failed = false;
fs.mkdir(`${window.virtualTestPath}/testDir1`, 777, (err)=>{
if(err){
failed = true;
}
});
await waitForTrue(()=>{return failed;},1000);
expect(failed).to.be.true;
});
});

0 comments on commit 4339218

Please sign in to comment.