Use OS.File for file reads in Zotero.File.get(Binary)ContentsAsync()
This is the recommended approach (since NetUtil can still do some main-thread I/O for files) and avoids warnings in the console. For getContentsAsync(), also sends nsIURIs and string URIs to Zotero.HTTP.request(), which should be used instead. This makes getBinaryContentsAsync() much slower (due to the conversion from an array of bytes to a binary string), but it's only used in tests. For one test that compares two large files, use MD5 instead.
This commit is contained in:
parent
12da3f00dc
commit
d857a06661
3 changed files with 130 additions and 91 deletions
|
@ -38,8 +38,43 @@ describe("Zotero.File", function () {
|
|||
assert.lengthOf(contents, 3);
|
||||
assert.equal(contents, "A\uFFFDB");
|
||||
})
|
||||
|
||||
it("should respect maxLength", function* () {
|
||||
var contents = yield Zotero.File.getContentsAsync(
|
||||
OS.Path.join(getTestDataDirectory().path, "test.txt"),
|
||||
false,
|
||||
6
|
||||
);
|
||||
assert.lengthOf(contents, 6);
|
||||
assert.equal(contents, "Zotero");
|
||||
});
|
||||
})
|
||||
|
||||
describe("#getBinaryContentsAsync()", function () {
|
||||
var magicPNG = ["89", "50", "4e", "47", "0d", "0a", "1a", "0a"].map(x => parseInt(x, 16));
|
||||
|
||||
it("should return a binary string", function* () {
|
||||
var contents = yield Zotero.File.getBinaryContentsAsync(
|
||||
OS.Path.join(getTestDataDirectory().path, "test.png")
|
||||
);
|
||||
assert.isAbove(contents.length, magicPNG.length);
|
||||
for (let i = 0; i < magicPNG.length; i++) {
|
||||
assert.equal(magicPNG[i], contents.charCodeAt(i));
|
||||
}
|
||||
});
|
||||
|
||||
it("should respect maxLength", function* () {
|
||||
var contents = yield Zotero.File.getBinaryContentsAsync(
|
||||
OS.Path.join(getTestDataDirectory().path, "test.png"),
|
||||
magicPNG.length
|
||||
);
|
||||
assert.lengthOf(contents, magicPNG.length)
|
||||
for (let i = 0; i < contents.length; i++) {
|
||||
assert.equal(magicPNG[i], contents.charCodeAt(i));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("#copyDirectory()", function () {
|
||||
it("should copy all files within a directory", function* () {
|
||||
var tmpDir = Zotero.getTempDirectory().path;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue