Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[do not merge] test python support #452

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions wasi/src/test/java/com/dylibso/chicory/wasi/WasiPreview1Test.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
package com.dylibso.chicory.wasi;

import static java.nio.file.Files.copy;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.dylibso.chicory.log.Logger;
import com.dylibso.chicory.log.SystemLogger;
import com.dylibso.chicory.runtime.HostImports;
import com.dylibso.chicory.runtime.Module;
import com.dylibso.chicory.wasm.types.Value;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;

import org.junit.jupiter.api.Test;

public class WasiPreview1Test {
Expand Down Expand Up @@ -77,4 +88,46 @@ public void shouldRunTinyGoModule() {

assertEquals(result.asInt(), 42);
}

@Test
public void shouldRunPythonModule() throws Exception {
// implementation of this tutorial:
var fakeStdout = new MockPrintStream();
var fakeStderr = new MockPrintStream();
FileSystem fs =
Jimfs.newFileSystem(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add an <optional>true</optional> dependency on Jimfs and add a utility method for this?

Configuration.unix().toBuilder().setAttributeViews("unix").build());
// --mapdir /::$PWD \
Path inputFolder = fs.getPath("/");

// cannot get inline python to run
// but works when loading a file ...
// -- -c "import sys; from pprint import pprint as pp; \
// pp(sys.path); pp(sys.platform)"
var script = "print(\"hello python!\")";

FileWriter fileWriter = new FileWriter(new File("/tmp/try-python-wasm").toPath().resolve("test.py").toFile());
PrintWriter printWriter = new PrintWriter(fileWriter);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: you can use Files.writeString()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

printWriter.print(script);
printWriter.flush();
printWriter.close();

Files.copyDirectory(new File("/tmp/try-python-wasm").toPath(), inputFolder);

var wasiOpts =
WasiOptions.builder()
.withDirectory(inputFolder.toString(), inputFolder)
.withArguments(List.of("-c", "test.py"))
.withStdout(fakeStdout)
.withStderr(fakeStderr)
.build();
var wasi = new WasiPreview1(this.logger, wasiOpts);
var imports = new HostImports(wasi.toHostFunctions());
var file = new File("/tmp/try-python-wasm/bin/python-3.11.1.wasm");

var module = Module.builder(file).withHostImports(imports).build();
module.instantiate();

assertEquals("hello python!\n", fakeStdout.output());
}
}
Loading