Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<!-- include spec files here... -->
<script type="text/javascript" src="spec/Editor-test.js"></script>
<script type="text/javascript" src="spec/NativeFileSystem-test.js"></script>
<script type="text/javascript" src="spec/LowLevelFileIO-test.js"></script>

<!-- include source files here... -->
<script type="text/javascript" src="../src/thirdparty/CodeMirror2/lib/CodeMirror.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This directory is marked write only when tests are run. Attempts to read files here should fail.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This directory is marked read only when tests are run. Attempts to write files here should fail.
1 change: 1 addition & 0 deletions test/spec/LowLevelFileIO-test-files/file_one.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
1 change: 1 addition & 0 deletions test/spec/LowLevelFileIO-test-files/file_three.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
1 change: 1 addition & 0 deletions test/spec/LowLevelFileIO-test-files/file_two.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
1 change: 1 addition & 0 deletions test/spec/LowLevelFileIO-test-files/write_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This content was generated from LowLevelFileIO-test.js
164 changes: 164 additions & 0 deletions test/spec/LowLevelFileIO-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// These are tests for the low-level file io routines in brackets-app. Make sure
// you have the latest brackets-app before running.

describe("LowLevelFileIO", function() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For boneheads like me, maybe a file header comment to make sure to update brackets-app before running these tests.


it("should have a brackets.fs namespace", function() {
expect(brackets.fs).toBeTruthy();
});

// Get window.location and remove the initial "file://" or "http://"
var baseDir = window.location.toString().substr(7);
// Remove the name of this html file
baseDir = baseDir.substr(0, baseDir.lastIndexOf("/") + 1);
baseDir += "spec/LowLevelFileIO-test-files/";

beforeEach(function() {
// Pre-test setup - set permissions on special directories

// Set read-only mode
brackets.fs.chmod(baseDir + "cant_read_here", 0222, function(err) {
expect(err).toBeFalsy();
});

// Set write-only mode
brackets.fs.chmod(baseDir + "cant_write_here", 0444, function(err) {
expect(err).toBeFalsy();
});
});

afterEach(function() {
// Restore directory permissions

// Set read-only mode
brackets.fs.chmod(baseDir + "cant_read_here", 0777, function(err) {
expect(err).toBeFalsy();
});

// Set write-only mode
brackets.fs.chmod(baseDir + "cant_write_here", 0777, function(err) {
expect(err).toBeFalsy();
});
});

describe("readdir", function() {

it("should read a directory from disk", function() {
brackets.fs.readdir(baseDir, function(err, contents) {
expect(err).toBeFalsy();

// Look for known files
expect(contents.indexOf("file_one.txt")).not.toBe(-1);
expect(contents.indexOf("file_two.txt")).not.toBe(-1);
expect(contents.indexOf("file_three.txt")).not.toBe(-1);

// Make sure '.' and '..' are omitted
expect(contents.indexOf(".")).toBe(-1);
expect(contents.indexOf("..")).toBe(-1);
});
});

it ("should return an error if the directory doesn't exist", function() {
brackets.fs.readdir("/This/directory/doesnt/exist", function(err, contents) {
expect(err).toBe(brackets.fs.ERR_NOT_FOUND);
});
});

it ("should return an error if the directory can't be read", function() {
brackets.fs.readdir(baseDir + "cant_read_here", function(err, contents) {
expect(err).toBe(brackets.fs.ERR_CANT_READ);
});
});

it ("should return an error if invalid parameters are passed", function() {
brackets.fs.readdir(42, function(err, contents) {
expect(err).toBe(brackets.fs.ERR_INVALID_PARAMS);
});
});
}); // describe("readdir")

describe("stat", function() {
it ("should return correct information for a directory", function() {
brackets.fs.stat(baseDir, function(err, stat) {
expect(err).toBeFalsy();
expect(stat.isDirectory()).toBe(true);
expect(stat.isFile()).toBe(false);
});
});

it ("should return correct information for a file", function() {
brackets.fs.stat(baseDir + "file_one.txt", function(err, stat) {
expect(err).toBeFalsy();
expect(stat.isDirectory()).toBe(false);
expect(stat.isFile()).toBe(true);
});
});

it ("should return an error if the file/directory doesn't exist", function() {
brackets.fs.stat("/This/directory/doesnt/exist", function(err, stat) {
expect(err).toBe(brackets.fs.ERR_NOT_FOUND);
});
});

it ("should return an error if incorrect parameters are passed", function() {
brackets.fs.stat(42, function(err, stat) {
expect(err).toBe(brackets.fs.ERR_INVALID_PARAMS);
});
});

}); // describe("stat")

describe("readFile", function() {
it ("should read a text file", function() {
brackets.fs.readFile(baseDir + "file_one.txt", "utf8", function(err, contents) {
expect(err).toBeFalsy();
expect(contents).toBe("Hello world");
});
});

it ("should return an error if trying to read a non-existent file", function() {
brackets.fs.readFile("/This/file/doesnt/exist.txt", "utf8", function(err, contents) {
expect(err).toBe(brackets.fs.ERR_NOT_FOUND);
});
});

it ("should return an error if trying to use an unsppported encoding", function() {
brackets.fs.readFile(baseDir + "file_one.txt", "utf16", function(err, contents) {
expect(err).toBe(brackets.fs.ERR_UNSUPPORTED_ENCODING);
});
});

it ("should return an error if called with invalid parameters", function() {
brackets.fs.readFile(42, [], function(err, contents) {
expect(err).toBe(brackets.fs.ERR_INVALID_PARAMS);
});
});
}); // describe("readFile")

describe("writeFile", function() {
var contents = "This content was generated from LowLevelFileIO-test.js";
it ("should write the entire contents of a file", function() {
brackets.fs.writeFile(baseDir + "write_test.txt", contents, "utf8", function(err) {
expect(err).toBeFalsy();

// Read contents to verify
brackets.fs.readFile(baseDir + "write_test.txt", "utf8", function(err, data) {
expect(err).toBeFalsy();
expect(data).toBe(contents);
});
});
});

it ("should return an error if the file can't be written", function() {
brackets.fs.writeFile(baseDir + "cant_write_here/write_test.txt", contents, "utf8", function(err) {
expect(err).toBe(brackets.fs.ERR_CANT_WRITE);
});
});

it ("should return an error if called with invalid parameters", function() {
brackets.fs.writeFile(42, contents, 2, function(err) {
expect(err).toBe(brackets.fs.ERR_INVALID_PARAMS);
});
});
}); // describe("writeFile")
});