-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support output to file, including append
- Loading branch information
1 parent
e6b9b80
commit 542b5f8
Showing
10 changed files
with
126 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,25 @@ | ||
import { BufferedOutput } from "./buffered_output" | ||
//import { IFileSystem } from "../file_system" | ||
import { IFileSystem } from "../file_system" | ||
|
||
export class FileOutput extends BufferedOutput { | ||
//constructor(fs: IFileSystem, path: string, append: boolean) { | ||
constructor(path: string, append: boolean) { | ||
constructor(readonly fileSystem: IFileSystem, readonly path: string, readonly append: boolean) { | ||
super() | ||
//this.fs = fs | ||
this.path = path | ||
this.append = append | ||
} | ||
|
||
console.log(this.path) | ||
override async flush(): Promise<void> { | ||
const { FS } = this.fileSystem | ||
let content = this.data.join("") | ||
|
||
if (this.append) { | ||
throw Error("FileOutput in append mode not implemented") | ||
try { | ||
const prevContent = FS.readFile(this.path, { "encoding": "utf8" }) | ||
content = prevContent + content | ||
} catch (e) { | ||
// If file does not exist, fallback to write (non-append) behaviour. | ||
} | ||
} | ||
} | ||
|
||
override async flush(): Promise<void> { | ||
//const all_data = this.data.join() | ||
//this.fs.write(this.path, all_data) | ||
FS.writeFile(this.path, content) | ||
this.clear() | ||
} | ||
|
||
//private readonly fs: IFileSystem | ||
private readonly path: string | ||
private readonly append: boolean // or replace | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { shell_setup_simple } from "../shell_setup" | ||
|
||
describe("grep command", () => { | ||
it("should write to stdout", async () => { | ||
const { shell, output } = await shell_setup_simple() | ||
await shell._runCommands("grep cond file2") | ||
expect(output.text).toEqual("Second line\r\n") | ||
}) | ||
|
||
it("should support ^ and $", async () => { | ||
const { shell, output, FS } = await shell_setup_simple() | ||
const line0 = " hello" | ||
const line1 = "hello " | ||
FS.writeFile("file3", line0 + "\n" + line1) | ||
|
||
await shell._runCommands("grep hello file3") | ||
expect(output.text).toEqual(line0 + "\r\n" + line1 + "\r\n") | ||
output.clear() | ||
|
||
await shell._runCommands("grep ^hello file3") | ||
expect(output.text).toEqual(line1 + "\r\n") | ||
output.clear() | ||
|
||
await shell._runCommands("grep hello$ file3") | ||
expect(output.text).toEqual(line0 + "\r\n") | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters