2019-11-12 00:49:05 +01:00
|
|
|
import {expect} from "chai";
|
2020-03-24 20:48:57 +01:00
|
|
|
import "mocha";
|
2019-11-12 00:49:05 +01:00
|
|
|
|
2019-11-14 23:21:06 +01:00
|
|
|
import {File, NullFile, Path} from "../main/js/FileSystem";
|
2019-11-12 00:49:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
describe("file", () => {
|
|
|
|
describe("open", () => {
|
|
|
|
describe("append", () => {
|
|
|
|
it("returns a stream that reads nothing", () => {
|
|
|
|
expect(new File("contents").open("append").read()).to.equal("");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns a stream that writes to the end of the file", () => {
|
|
|
|
const file = new File("old");
|
|
|
|
|
|
|
|
file.open("append").write("_new");
|
|
|
|
|
|
|
|
expect(file.contents).to.equal("old_new");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("read", () => {
|
|
|
|
it("returns a stream that reads from the start of the file", () => {
|
|
|
|
expect(new File("contents").open("read").read()).to.equal("contents");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("write", () => {
|
|
|
|
it("empties the file", () => {
|
|
|
|
const file = new File("contents");
|
|
|
|
|
|
|
|
file.open("write");
|
|
|
|
|
|
|
|
expect(file.contents).to.equal("");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns a stream that overwrites the file", () => {
|
|
|
|
const file = new File("old");
|
|
|
|
|
|
|
|
file.open("write").write("new");
|
|
|
|
|
|
|
|
expect(file.contents).to.equal("new");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-11-14 23:21:06 +01:00
|
|
|
|
|
|
|
describe("nameString", () => {
|
|
|
|
it("returns the name if no type is known", () => {
|
|
|
|
expect(new File("contents").nameString("file", new Path("/file"))).to.equal("file");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("uses the file extension to determine the value", () => {
|
|
|
|
expect(new File("contents").nameString("file.txt", new Path("/file")))
|
2022-11-26 14:43:33 +01:00
|
|
|
.to.equal(`<a class="file-link" onclick="execute('cat /file')">file.txt</a>`);
|
2019-11-14 23:21:06 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("uses the mime type if no type is known", () => {
|
|
|
|
expect(new File("contents", "txt").nameString("file", new Path("/file")))
|
2022-11-26 14:43:33 +01:00
|
|
|
.to.equal(`<a class="file-link" onclick="execute('cat /file')">file</a>`);
|
2019-11-14 23:21:06 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("overrides the file extension with the mime type", () => {
|
|
|
|
expect(new File("link", "lnk").nameString("file.txt", new Path("/file")))
|
2022-11-26 14:43:33 +01:00
|
|
|
.to.equal(`<a href="link" class="file-link" onclick="execute('open /file'); return false">file.txt</a>`);
|
2019-11-14 23:21:06 +01:00
|
|
|
});
|
|
|
|
});
|
2019-11-12 00:49:05 +01:00
|
|
|
});
|
2019-11-12 01:10:53 +01:00
|
|
|
|
|
|
|
describe("null file", () => {
|
|
|
|
let file: NullFile;
|
|
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
file = new NullFile();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("has empty contents", () => {
|
|
|
|
expect(file.contents).to.equal("");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("is empty after writing to it", () => {
|
|
|
|
file.open("write").write("contents");
|
|
|
|
|
|
|
|
expect(file.contents).to.equal("");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("is empty when reading from a stream", () => {
|
|
|
|
expect(file.open("read").read()).to.equal("");
|
|
|
|
});
|
|
|
|
});
|