Skip to content

Commit 45e3b40

Browse files
committed
test: unit tests for VFS.unlinkResolves,unlinkAsync,readFileResolves,readFileAsync,writeFileAsync
1 parent 6e0e2a1 commit 45e3b40

File tree

1 file changed

+154
-1
lines changed

1 file changed

+154
-1
lines changed

test/spec/LowLevelFileIO-test.js

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
*/
2121

22-
/*global describe, it, expect, beforeEach, afterEach, awaitsFor, awaitsForDone*/
22+
/*global describe, it, expect, beforeEach, afterEach, awaitsFor, awaitsForDone, fail*/
2323

2424
define(function (require, exports, module) {
2525

@@ -641,6 +641,159 @@ define(function (require, exports, module) {
641641
});
642642
});
643643

644+
describe("VFS Async APIs", function () {
645+
646+
var contents = "This content was generated from LowLevelFileIO-test.js for VFS async APIs";
647+
648+
describe("writeFileAsync", function () {
649+
650+
it("should write file content asynchronously", async function () {
651+
var testFile = baseDir + "/vfs_write_test.txt";
652+
653+
try {
654+
await Phoenix.VFS.writeFileAsync(testFile, contents, UTF8);
655+
656+
var readFileCB = readFileSpy();
657+
brackets.fs.readFile(testFile, UTF8, readFileCB);
658+
659+
await awaitsFor(function () { return readFileCB.wasCalled; }, 1000);
660+
661+
expect(readFileCB.error).toBeFalsy();
662+
expect(readFileCB.content).toBe(contents);
663+
} catch (error) {
664+
fail("writeFileAsync should not reject: " + error);
665+
}
666+
});
667+
668+
it("should reject when writing to invalid path", async function () {
669+
try {
670+
await Phoenix.VFS.writeFileAsync("/invalid/path/file.txt", contents, UTF8);
671+
fail("writeFileAsync should have rejected for invalid path");
672+
} catch (error) {
673+
expect(error).toBeTruthy();
674+
}
675+
});
676+
677+
}); // describe("writeFileAsync")
678+
679+
describe("readFileAsync", function () {
680+
681+
beforeEach(async function () {
682+
var writeFileCB = errSpy();
683+
brackets.fs.writeFile(testDir + "/vfs_read_test.txt", contents, UTF8, writeFileCB);
684+
await awaitsFor(function () { return writeFileCB.wasCalled; }, 1000);
685+
expect(writeFileCB.error).toBeFalsy();
686+
});
687+
688+
it("should read file content asynchronously", async function () {
689+
try {
690+
var data = await Phoenix.VFS.readFileAsync(testDir + "/vfs_read_test.txt", UTF8);
691+
expect(data).toBe(contents);
692+
} catch (error) {
693+
fail("readFileAsync should not reject: " + error);
694+
}
695+
});
696+
697+
it("should reject when reading non-existent file", async function () {
698+
try {
699+
await Phoenix.VFS.readFileAsync("/non/existent/file.txt", UTF8);
700+
fail("readFileAsync should have rejected for non-existent file");
701+
} catch (error) {
702+
expect(error.code).toBe(brackets.fs.ERR_CODES.ENOENT);
703+
}
704+
});
705+
706+
}); // describe("readFileAsync")
707+
708+
describe("readFileResolves", function () {
709+
710+
beforeEach(async function () {
711+
var writeFileCB = errSpy();
712+
brackets.fs.writeFile(testDir + "/vfs_read_resolves_test.txt", contents, UTF8, writeFileCB);
713+
await awaitsFor(function () { return writeFileCB.wasCalled; }, 1000);
714+
expect(writeFileCB.error).toBeFalsy();
715+
});
716+
717+
it("should resolve with data when reading existing file", async function () {
718+
var result = await Phoenix.VFS.readFileResolves(testDir + "/vfs_read_resolves_test.txt", UTF8);
719+
expect(result.error).toBeUndefined();
720+
expect(result.data).toBe(contents);
721+
});
722+
723+
it("should resolve with error when reading non-existent file", async function () {
724+
var result = await Phoenix.VFS.readFileResolves("/non/existent/file.txt", UTF8);
725+
expect(result.error).toBeTruthy();
726+
expect(result.error.code).toBe(brackets.fs.ERR_CODES.ENOENT);
727+
expect(result.data).toBeUndefined();
728+
});
729+
730+
}); // describe("readFileResolves")
731+
732+
describe("unlinkAsync", function () {
733+
734+
it("should delete file asynchronously", async function () {
735+
var testFile = baseDir + "/vfs_unlink_test.txt";
736+
737+
var writeFileCB = errSpy();
738+
brackets.fs.writeFile(testFile, contents, UTF8, writeFileCB);
739+
await awaitsFor(function () { return writeFileCB.wasCalled; }, 1000);
740+
expect(writeFileCB.error).toBeFalsy();
741+
742+
try {
743+
await Phoenix.VFS.unlinkAsync(testFile);
744+
745+
var statCB = statSpy();
746+
brackets.fs.stat(testFile, statCB);
747+
await awaitsFor(function () { return statCB.wasCalled; }, 1000);
748+
749+
expect(statCB.error.code).toBe(brackets.fs.ERR_CODES.ENOENT);
750+
} catch (error) {
751+
fail("unlinkAsync should not reject: " + error);
752+
}
753+
});
754+
755+
it("should reject when deleting non-existent file", async function () {
756+
try {
757+
await Phoenix.VFS.unlinkAsync("/non/existent/file.txt");
758+
fail("unlinkAsync should have rejected for non-existent file");
759+
} catch (error) {
760+
expect(error.code).toBe(brackets.fs.ERR_CODES.ENOENT);
761+
}
762+
});
763+
764+
}); // describe("unlinkAsync")
765+
766+
describe("unlinkResolves", function () {
767+
768+
it("should resolve with empty object when deleting existing file", async function () {
769+
var testFile = baseDir + "/vfs_unlink_resolves_test.txt";
770+
771+
var writeFileCB = errSpy();
772+
brackets.fs.writeFile(testFile, contents, UTF8, writeFileCB);
773+
await awaitsFor(function () { return writeFileCB.wasCalled; }, 1000);
774+
expect(writeFileCB.error).toBeFalsy();
775+
776+
var result = await Phoenix.VFS.unlinkResolves(testFile);
777+
expect(result.error).toBeUndefined();
778+
expect(Object.keys(result).length).toBe(0);
779+
780+
var statCB = statSpy();
781+
brackets.fs.stat(testFile, statCB);
782+
await awaitsFor(function () { return statCB.wasCalled; }, 1000);
783+
784+
expect(statCB.error.code).toBe(brackets.fs.ERR_CODES.ENOENT);
785+
});
786+
787+
it("should resolve with error when deleting non-existent file", async function () {
788+
var result = await Phoenix.VFS.unlinkResolves("/non/existent/file.txt");
789+
expect(result.error).toBeTruthy();
790+
expect(result.error.code).toBe(brackets.fs.ERR_CODES.ENOENT);
791+
});
792+
793+
}); // describe("unlinkResolves")
794+
795+
}); // describe("VFS Async APIs")
796+
644797
describe("specialDirectories", function () {
645798
it("should have an application support directory", async function () {
646799
// these tests are here as these are absolute unchanging dir convention used by phoenix.

0 commit comments

Comments
 (0)