forked from tools/josh
1
0
Fork 0

Add /dev/null

Fixes #71.
This commit is contained in:
Florine W. Dekker 2019-11-12 01:10:53 +01:00
parent 1a9a588f8f
commit afa936bb4b
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
6 changed files with 73 additions and 14 deletions

View File

@ -1,6 +1,6 @@
{
"name": "fwdekker.com",
"version": "1.9.7",
"version": "1.10.0",
"description": "The source code of [my personal website](https://fwdekker.com/).",
"author": "Felix W. Dekker",
"repository": {

View File

@ -527,7 +527,7 @@ export class Commands {
return -1;
}
window.open(node.contents, target);
window.open(node.open("read").read(), target);
return 0;
}

View File

@ -21,6 +21,9 @@ export class FileSystem {
if (root === undefined)
this.root =
new Directory({
"dev": new Directory({
"null": new NullFile()
}),
"home": new Directory({
"felix": new Directory({
"personal": new Directory({
@ -551,18 +554,28 @@ export class File extends Node {
protected type: string = "File";
/**
* The link to the external resource.
* The contents of this file. !!Do not use this field directly!!
*/
contents: string;
_contents: string;
/**
* Constructs a new file.
*
* @param contents the contents of this file
*/
constructor(contents: string = "") {
super();
this.contents = contents;
this._contents = contents;
}
/**
* Returns the contents of this file.
*/
get contents(): string {
return this._contents;
}
@ -578,7 +591,7 @@ export class File extends Node {
case "read":
return new FileStream(this, 0);
case "write":
this.contents = "";
this._contents = "";
return new FileStream(this, 0);
}
}
@ -623,6 +636,28 @@ export class File extends Node {
}
}
/**
* A file that cannot have contents.
*/
export class NullFile extends File {
open(mode: "append" | "read" | "write"): FileStream {
return new class extends FileStream {
constructor(file: File, pointer: number) {
super(file, pointer);
}
read(count: number | undefined = undefined): string {
return "";
}
write(string: string): void {
// Do nothing
}
}(this, 0);
}
}
/**
* The mode to open a file in.
@ -671,6 +706,6 @@ export class FileStream extends Stream {
const pre = this.file.contents.slice(0, this.pointer);
const post = this.buffer.slice(string.length);
this.file.contents = pre + string + post;
this.file._contents = pre + string + post;
}
}

View File

@ -130,8 +130,8 @@ describe("directory", () => {
});
const copy = directory.copy();
(<File>directory.getNode("file")).contents = "changed";
expect((<File>copy.getNode("file")).contents).to.equal("contents");
(<File>directory.getNode("file")).open("write").write("changed");
expect((<File>copy.getNode("file")).open("read").read()).to.equal("contents");
(<Directory>directory.getNode("dir")).addNode("file2", new File());
expect((<Directory>copy.getNode("dir")).nodeCount).to.equal(0);

View File

@ -1,7 +1,7 @@
import "mocha";
import {expect} from "chai";
import {File} from "../main/js/FileSystem";
import {File, NullFile} from "../main/js/FileSystem";
describe("file", () => {
@ -45,3 +45,27 @@ describe("file", () => {
});
});
});
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("");
});
});

View File

@ -121,9 +121,9 @@ describe("file system", () => {
fileSystem.add(new Path("/src"), file, false);
fileSystem.copy(new Path("/src"), new Path("/dst"), false);
file.contents = "new";
file.open("write").write("new");
expect((<File>fileSystem.get(new Path("/dst"))).contents).to.equal("old");
expect((<File>fileSystem.get(new Path("/dst"))).open("read").read()).to.equal("old");
});
});
@ -205,9 +205,9 @@ describe("file system", () => {
fileSystem.add(new Path("/src"), file, false);
fileSystem.move(new Path("/src"), new Path("/dst"));
file.contents = "new";
file.open("write").write("new");
expect((<File>fileSystem.get(new Path("/dst"))).contents).to.equal("new");
expect((<File>fileSystem.get(new Path("/dst"))).open("read").read()).to.equal("new");
});
it("throws an error if the destination already exists", () => {