forked from wasmerio/wasmer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wasmerio#368 from wasmerio/michael/sdk-38-providin…
…g-stdin-up-front-seems-to-always-read-the-same-bytes Pick up wasmerio#4355 to fix stdin
- Loading branch information
Showing
7 changed files
with
73 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
|
@@ -42,6 +42,22 @@ describe("Wasmer.spawn", function () { | |
expect(output.stderr.length).to.equal(0); | ||
}); | ||
|
||
it("Can pass stdin to a dumb echo program", async () => { | ||
const pkg = await Wasmer.fromRegistry( | ||
"christoph/[email protected]", | ||
); | ||
const instance = await pkg.commands["stdinout-loop"].run({ | ||
stdin: "Hello\nWorld!\n", | ||
}); | ||
|
||
const output = await instance.wait(); | ||
|
||
console.log({...output}); | ||
expect(output.ok).to.be.true; | ||
expect(output.code).to.equal(0); | ||
expect(output.stderr).to.equal("Hello\n\nWorld!\n\n"); | ||
}); | ||
|
||
it("Can communicate with a dumb echo program", async () => { | ||
// First, start our program in the background | ||
const pkg = await Wasmer.fromRegistry( | ||
|
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,6 +1,5 @@ | ||
import { expect } from "@esm-bundle/chai"; | ||
import { | ||
Runtime, | ||
runWasix, | ||
wat2wasm, | ||
Wasmer, | ||
|
@@ -9,17 +8,12 @@ import { | |
Directory, | ||
} from ".."; | ||
|
||
let runtime: Runtime; | ||
const decoder = new TextDecoder("utf-8"); | ||
const encoder = new TextEncoder(); | ||
|
||
const initialized = (async () => { | ||
await init(); | ||
initializeLogger("warn"); | ||
runtime = new Runtime(); | ||
})(); | ||
|
||
describe("run", function () { | ||
describe.skip("run", function () { | ||
this.timeout("60s").beforeAll(async () => await initialized); | ||
|
||
it("can execute a noop program", async () => { | ||
|
@@ -32,7 +26,7 @@ describe("run", function () { | |
const wasm = wat2wasm(noop); | ||
const module = await WebAssembly.compile(wasm); | ||
|
||
const instance = await runWasix(module, { program: "noop", runtime }); | ||
const instance = await runWasix(module, { program: "noop" }); | ||
const output = await instance.wait(); | ||
|
||
expect(output.ok).to.be.true; | ||
|
@@ -42,12 +36,10 @@ describe("run", function () { | |
it("can start quickjs", async () => { | ||
const pkg = await Wasmer.fromRegistry("saghul/[email protected]"); | ||
const quickjs = pkg.commands["quickjs"].binary(); | ||
const module = await WebAssembly.compile(quickjs); | ||
|
||
const instance = await runWasix(module, { | ||
const instance = await runWasix(quickjs, { | ||
program: "quickjs", | ||
args: ["--eval", "console.log('Hello, World!')"], | ||
runtime, | ||
}); | ||
const output = await instance.wait(); | ||
|
||
|
@@ -62,16 +54,14 @@ describe("run", function () { | |
await dir.writeFile("/file.txt", ""); | ||
const pkg = await Wasmer.fromRegistry("saghul/[email protected]"); | ||
const quickjs = pkg.commands["quickjs"].binary(); | ||
const module = await WebAssembly.compile(quickjs); | ||
|
||
const instance = await runWasix(module, { | ||
const instance = await runWasix(quickjs, { | ||
program: "quickjs", | ||
args: [ | ||
"--std", | ||
"--eval", | ||
`[dirs] = os.readdir("/"); console.log(dirs.join("\\n"))`, | ||
], | ||
runtime, | ||
mount: { | ||
"/mount": dir, | ||
}, | ||
|
@@ -86,19 +76,17 @@ describe("run", function () { | |
|
||
it("can read files", async () => { | ||
const tmp = new Directory(); | ||
await tmp.writeFile("/file.txt", encoder.encode("Hello, World!")); | ||
await tmp.writeFile("/file.txt", "Hello, World!"); | ||
const pkg = await Wasmer.fromRegistry("saghul/[email protected]"); | ||
const quickjs = pkg.commands["quickjs"].binary(); | ||
const module = await WebAssembly.compile(quickjs); | ||
|
||
const instance = await runWasix(module, { | ||
const instance = await runWasix(quickjs, { | ||
program: "quickjs", | ||
args: [ | ||
"--std", | ||
"--eval", | ||
`console.log(std.open('/tmp/file.txt', "r").readAsString())`, | ||
], | ||
runtime, | ||
mount: { | ||
"/tmp": tmp, | ||
}, | ||
|
@@ -114,16 +102,14 @@ describe("run", function () { | |
it("can read files mounted using DirectoryInit", async () => { | ||
const pkg = await Wasmer.fromRegistry("saghul/[email protected]"); | ||
const quickjs = pkg.commands["quickjs"].binary(); | ||
const module = await WebAssembly.compile(quickjs); | ||
|
||
const instance = await runWasix(module, { | ||
const instance = await runWasix(quickjs, { | ||
program: "quickjs", | ||
args: [ | ||
"--std", | ||
"--eval", | ||
`console.log(std.open('/tmp/file.txt', "r").readAsString())`, | ||
], | ||
runtime, | ||
mount: { | ||
"/tmp": { | ||
"file.txt": "Hello, World!", | ||
|
@@ -142,17 +128,15 @@ describe("run", function () { | |
const dir = new Directory(); | ||
const pkg = await Wasmer.fromRegistry("saghul/[email protected]"); | ||
const quickjs = pkg.commands["quickjs"].binary(); | ||
const module = await WebAssembly.compile(quickjs); | ||
const script = ` | ||
const f = std.open('/mount/file.txt', 'w'); | ||
f.puts('Hello, World!'); | ||
f.close(); | ||
`; | ||
|
||
const instance = await runWasix(module, { | ||
const instance = await runWasix(quickjs, { | ||
program: "quickjs", | ||
args: ["--std", "--eval", script], | ||
runtime, | ||
mount: { | ||
"/mount": dir, | ||
}, | ||
|
@@ -162,4 +146,20 @@ describe("run", function () { | |
expect(output.ok).to.be.true; | ||
expect(await dir.readTextFile("/file.txt")).to.equal("Hello, World!"); | ||
}); | ||
}); | ||
|
||
it("can accept strings as stdin", async () => { | ||
const pkg = await Wasmer.fromRegistry("saghul/[email protected]"); | ||
const quickjs = pkg.commands["quickjs"].binary(); | ||
|
||
const instance = await runWasix(quickjs, { | ||
program: "quickjs", | ||
args: ["--interactive", "--std"], | ||
stdin: "console.log('Hello, World!');\nstd.exit(42)\n", | ||
}); | ||
const output = await instance.wait(); | ||
|
||
expect(output.code).to.equal(42); | ||
expect(output.stdout).to.contain("Hello, World!\n"); | ||
expect(output.stderr).to.be.empty; | ||
}); | ||
}) |