Skip to content

Commit

Permalink
test python support
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP committed Jul 30, 2024
1 parent fd90dc2 commit 7a83998
Showing 1 changed file with 53 additions and 0 deletions.
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(
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);
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());
}
}

0 comments on commit 7a83998

Please sign in to comment.