phantomjs/test/fs-spec-01.js

220 lines
6.6 KiB
JavaScript
Raw Normal View History

describe("Basic Files API (read, write, remove, ...)", function() {
var FILENAME = "temp-01.test",
FILENAME_COPY = FILENAME + ".copy",
FILENAME_MOVED = FILENAME + ".moved",
FILENAME_EMPTY = FILENAME + ".empty",
FILENAME_ENC = FILENAME + ".enc",
Filesystem module should allow reading and writing binary files. CommonJS proposal: http://wiki.commonjs.org/wiki/Filesystem/A. It's called "raw". http://code.google.com/p/phantomjs/issues/detail?id=400 Squashed commit of the following: commit dd5fab4778bb7b67f1eca26a07d430aadd458c6e Author: Milian Wolff <milian.wolff@kdab.com> Date: Thu Feb 23 16:19:21 2012 +0100 the "mode" string is now properly parsed, and not only the first char evaluated. This allows us to do fancy things like fs.open(file, "rw+"); // read/write/append Furthermore .read() is adapted such that it will always return the full file contents, no matter where we have seeked to before (i.e. by passing + we seek to the end, hence read() would always return an empty string). To open a binary file, pass "b" in the mode string to fs.open, e.g.: fs.open(file, "rb"); // read binary fs.open(file, "wb"); // write binary fs.open(file, "rwb+"); // read/write binary, append alternatively, one can use these shortcuts: fs.write(file, contents, "b"); // write binary fs.read(file, "b"); // read binary Unit tests are extended and the echoToFile.js example fixed (it did not close the file, which lead to the contents never getting written on-disk since flush() is never called). Also note that the FileSystem::open method was cleaned up and at least one memory leak (if QFile* could not open) was fixed. The code should now also be more C++-like. commit 41139951138491459accefab22d48eba7b0b9900 Author: Milian Wolff <milian.wolff@kdab.com> Date: Wed Feb 15 16:39:23 2012 +0100 use QString instead of QByteArray for raw binary data QByteArray is simply unusable in JavaScript, since functions like e.g. window.btoa expect a string. Also there is no sane way to create a byte array in javascript, as ArrayBuffer e.g. is not supported by QScript (at least there is no conversion in place). If we use QString and some custom read/write code this all works as expected though, we can use window.btoa to base64 encode binary data and we can create random binary data using String.fromCharCode also adds a unit test commit e45673486ef27daf916902153217f9e5001b68c9 Author: Milian Wolff <milian.wolff@kdab.com> Date: Wed Feb 15 14:39:15 2012 +0100 make it possible to read/write raw/binary files this adds File::readRaw and File::writeRaw functions, as well as 'shimmed' versions FS::readRaw and FS::writeRaw these functions directly use QFile and QByteArray instead of QTextStream and QString, making it possible to read and write binary data, e.g. images and such.
2012-02-29 19:53:12 +04:00
FILENAME_BIN = FILENAME + ".bin",
ABSENT = "absent-01.test";
it("should be able to create and write a file", function() {
try{
var f = fs.open(FILENAME, "w");
f.write("hello");
f.writeLine("");
f.writeLine("world");
f.close();
} catch (e) { }
expect(fs.exists(FILENAME)).toBeTruthy();
});
it("should be able to create (touch) an empty file", function() {
expect(fs.exists(FILENAME_EMPTY)).toBeFalsy();
fs.touch(FILENAME_EMPTY);
expect(fs.exists(FILENAME_EMPTY)).toBeTruthy();
expect(fs.size(FILENAME_EMPTY)).toEqual(0);
});
it("should be able to read content from a file", function() {
var content = "";
try{
var f = fs.open(FILENAME, "r");
content = f.read();
f.close();
} catch (e) { }
expect(content).toEqual("hello\nworld\n");
});
it("should be able to read specific number of bytes from a specific position in a file", function() {
var content = "";
try{
var f = fs.open(FILENAME, "r");
f.seek(3);
content = f.read(5);
f.close();
} catch (e) { }
expect(content).toEqual("lo\nwo");
});
Filesystem module should allow reading and writing binary files. CommonJS proposal: http://wiki.commonjs.org/wiki/Filesystem/A. It's called "raw". http://code.google.com/p/phantomjs/issues/detail?id=400 Squashed commit of the following: commit dd5fab4778bb7b67f1eca26a07d430aadd458c6e Author: Milian Wolff <milian.wolff@kdab.com> Date: Thu Feb 23 16:19:21 2012 +0100 the "mode" string is now properly parsed, and not only the first char evaluated. This allows us to do fancy things like fs.open(file, "rw+"); // read/write/append Furthermore .read() is adapted such that it will always return the full file contents, no matter where we have seeked to before (i.e. by passing + we seek to the end, hence read() would always return an empty string). To open a binary file, pass "b" in the mode string to fs.open, e.g.: fs.open(file, "rb"); // read binary fs.open(file, "wb"); // write binary fs.open(file, "rwb+"); // read/write binary, append alternatively, one can use these shortcuts: fs.write(file, contents, "b"); // write binary fs.read(file, "b"); // read binary Unit tests are extended and the echoToFile.js example fixed (it did not close the file, which lead to the contents never getting written on-disk since flush() is never called). Also note that the FileSystem::open method was cleaned up and at least one memory leak (if QFile* could not open) was fixed. The code should now also be more C++-like. commit 41139951138491459accefab22d48eba7b0b9900 Author: Milian Wolff <milian.wolff@kdab.com> Date: Wed Feb 15 16:39:23 2012 +0100 use QString instead of QByteArray for raw binary data QByteArray is simply unusable in JavaScript, since functions like e.g. window.btoa expect a string. Also there is no sane way to create a byte array in javascript, as ArrayBuffer e.g. is not supported by QScript (at least there is no conversion in place). If we use QString and some custom read/write code this all works as expected though, we can use window.btoa to base64 encode binary data and we can create random binary data using String.fromCharCode also adds a unit test commit e45673486ef27daf916902153217f9e5001b68c9 Author: Milian Wolff <milian.wolff@kdab.com> Date: Wed Feb 15 14:39:15 2012 +0100 make it possible to read/write raw/binary files this adds File::readRaw and File::writeRaw functions, as well as 'shimmed' versions FS::readRaw and FS::writeRaw these functions directly use QFile and QByteArray instead of QTextStream and QString, making it possible to read and write binary data, e.g. images and such.
2012-02-29 19:53:12 +04:00
it("should be able to read/write/append content from a file", function() {
var content = "";
try{
var f = fs.open(FILENAME, "rw+");
f.writeLine("asdf");
content = f.read();
f.close();
} catch (e) { }
expect(content).toEqual("hello\nworld\nasdf\n");
});
it("should be able to get the encoding (default: UTF-8)", function() {
var encoding = "";
try {
var f = fs.open(FILENAME, "r");
encoding = f.getEncoding();
f.close();
} catch (e) {
console.log(e);
}
expect(encoding).toEqual("UTF-8");
});
it("should be able to set the encoding via options", function() {
var encoding = "";
try {
var f = fs.open(FILENAME, {
charset: "UTF-8"
, mode: "r"
});
encoding = f.getEncoding();
f.close();
} catch (e) {
console.log(e);
}
expect(encoding).toEqual("UTF-8");
try {
var f = fs.open(FILENAME, {
charset: "SJIS"
, mode: "r"
});
encoding = f.getEncoding();
f.close();
} catch (e) {
console.log(e);
}
expect(encoding).toEqual("Shift_JIS");
});
it("should be able to change the encoding", function() {
var encoding = "";
try {
var f = fs.open(FILENAME, {
charset: "UTF-8"
, mode: "r"
});
f.setEncoding("utf8");
encoding = f.getEncoding();
f.close();
} catch (e) {
console.log(e);
}
expect(encoding).toEqual("UTF-8");
try {
var f = fs.open(FILENAME, {
charset: "SJIS"
, mode: "r"
});
f.setEncoding("eucjp");
encoding = f.getEncoding();
f.close();
} catch (e) {
console.log(e);
}
expect(encoding).toEqual("EUC-JP");
});
it("should be able to copy a file", function() {
expect(fs.exists(FILENAME_COPY)).toBeFalsy();
fs.copy(FILENAME, FILENAME_COPY);
expect(fs.exists(FILENAME_COPY)).toBeTruthy();
expect(fs.read(FILENAME)).toEqual(fs.read(FILENAME_COPY));
});
it("should be able to move a file", function() {
expect(fs.exists(FILENAME)).toBeTruthy();
var contentBeforeMove = fs.read(FILENAME);
fs.move(FILENAME, FILENAME_MOVED);
expect(fs.exists(FILENAME)).toBeFalsy();
expect(fs.exists(FILENAME_MOVED)).toBeTruthy();
expect(fs.read(FILENAME_MOVED)).toEqual(contentBeforeMove);
});
it("should be able to remove a (moved) file", function() {
expect(fs.exists(FILENAME_MOVED)).toBeTruthy();
fs.remove(FILENAME_MOVED);
expect(fs.exists(FILENAME_MOVED)).toBeFalsy();
});
it("should be able to remove a (copied) file", function() {
expect(fs.exists(FILENAME_COPY)).toBeTruthy();
fs.remove(FILENAME_COPY);
expect(fs.exists(FILENAME_COPY)).toBeFalsy();
});
it("should be able to remove an empty file", function() {
expect(fs.exists(FILENAME_EMPTY)).toBeTruthy();
fs.remove(FILENAME_EMPTY);
expect(fs.exists(FILENAME_EMPTY)).toBeFalsy();
});
it("should throw an exception when trying to open for read a non existing file", function(){
expect(function(){
fs.open(ABSENT, "r");
}).toThrow("Unable to open file '"+ ABSENT +"'");
});
it("should throw an exception when trying to copy a non existing file", function() {
expect(function(){
fs.copy(ABSENT, FILENAME_COPY);
}).toThrow("Unable to copy file '" + ABSENT + "' at '" + FILENAME_COPY + "'");
});
it("should be read/write utf8 text by default", function() {
var content, output = "ÄABCÖ";
try {
var f = fs.open(FILENAME_ENC, "w");
f.write(output);
f.close();
f = fs.open(FILENAME_ENC, "r");
content = f.read();
f.close();
fs.remove(FILENAME_ENC);
} catch (e) { }
expect(content).toEqual(output);
});
Filesystem module should allow reading and writing binary files. CommonJS proposal: http://wiki.commonjs.org/wiki/Filesystem/A. It's called "raw". http://code.google.com/p/phantomjs/issues/detail?id=400 Squashed commit of the following: commit dd5fab4778bb7b67f1eca26a07d430aadd458c6e Author: Milian Wolff <milian.wolff@kdab.com> Date: Thu Feb 23 16:19:21 2012 +0100 the "mode" string is now properly parsed, and not only the first char evaluated. This allows us to do fancy things like fs.open(file, "rw+"); // read/write/append Furthermore .read() is adapted such that it will always return the full file contents, no matter where we have seeked to before (i.e. by passing + we seek to the end, hence read() would always return an empty string). To open a binary file, pass "b" in the mode string to fs.open, e.g.: fs.open(file, "rb"); // read binary fs.open(file, "wb"); // write binary fs.open(file, "rwb+"); // read/write binary, append alternatively, one can use these shortcuts: fs.write(file, contents, "b"); // write binary fs.read(file, "b"); // read binary Unit tests are extended and the echoToFile.js example fixed (it did not close the file, which lead to the contents never getting written on-disk since flush() is never called). Also note that the FileSystem::open method was cleaned up and at least one memory leak (if QFile* could not open) was fixed. The code should now also be more C++-like. commit 41139951138491459accefab22d48eba7b0b9900 Author: Milian Wolff <milian.wolff@kdab.com> Date: Wed Feb 15 16:39:23 2012 +0100 use QString instead of QByteArray for raw binary data QByteArray is simply unusable in JavaScript, since functions like e.g. window.btoa expect a string. Also there is no sane way to create a byte array in javascript, as ArrayBuffer e.g. is not supported by QScript (at least there is no conversion in place). If we use QString and some custom read/write code this all works as expected though, we can use window.btoa to base64 encode binary data and we can create random binary data using String.fromCharCode also adds a unit test commit e45673486ef27daf916902153217f9e5001b68c9 Author: Milian Wolff <milian.wolff@kdab.com> Date: Wed Feb 15 14:39:15 2012 +0100 make it possible to read/write raw/binary files this adds File::readRaw and File::writeRaw functions, as well as 'shimmed' versions FS::readRaw and FS::writeRaw these functions directly use QFile and QByteArray instead of QTextStream and QString, making it possible to read and write binary data, e.g. images and such.
2012-02-29 19:53:12 +04:00
it("should be read/write binary data", function() {
var content, output = String.fromCharCode(0, 1, 2, 3, 4, 5);
try {
var f = fs.open(FILENAME_BIN, "wb");
f.write(output);
f.close();
f = fs.open(FILENAME_BIN, "rb");
content = f.read();
f.close();
fs.remove(FILENAME_BIN);
} catch (e) { }
expect(content).toEqual(output);
});
it("should be read/write binary data (shortcuts)", function() {
var content, output = String.fromCharCode(0, 1, 2, 3, 4, 5);
try {
fs.write(FILENAME_BIN, output, "b");
content = fs.read(FILENAME_BIN, "b");
fs.remove(FILENAME_BIN);
} catch (e) { }
expect(content).toEqual(output);
});
});