|
| 1 | +use crate::utils::{check_go_version, make_path_absolute}; |
| 2 | +use anyhow::{Result, anyhow}; |
| 3 | +use std::{ |
| 4 | + path::{Path, PathBuf}, |
| 5 | + process::Command, |
| 6 | +}; |
| 7 | + |
| 8 | +/// Compiles a Go application to a wasm module with `go test -c`. |
| 9 | +/// |
| 10 | +/// If the module is not going to be adapted to the component model, |
| 11 | +/// set the `only_wasip1` arg to true. |
| 12 | +pub fn build_test_module( |
| 13 | + path: &Path, |
| 14 | + output_dir: Option<&PathBuf>, |
| 15 | + go_path: Option<&PathBuf>, |
| 16 | + only_wasip1: bool, |
| 17 | +) -> Result<PathBuf> { |
| 18 | + let go = match &go_path { |
| 19 | + Some(p) => make_path_absolute(p)?, |
| 20 | + None => PathBuf::from("go"), |
| 21 | + }; |
| 22 | + |
| 23 | + check_go_version(&go)?; |
| 24 | + |
| 25 | + let test_wasm_path = { |
| 26 | + // The directory in which the test component will be placed |
| 27 | + let test_dir = match output_dir { |
| 28 | + Some(p) => make_path_absolute(p)?, |
| 29 | + None => std::env::current_dir()?, |
| 30 | + }; |
| 31 | + |
| 32 | + test_dir.join(get_test_filename(path)) |
| 33 | + }; |
| 34 | + |
| 35 | + // Ensuring the newly compiled wasm file overwrites any previously-existing wasm file |
| 36 | + if test_wasm_path.exists() { |
| 37 | + std::fs::remove_file(&test_wasm_path)?; |
| 38 | + } |
| 39 | + |
| 40 | + if let Some(dir) = output_dir { |
| 41 | + std::fs::create_dir_all(dir)?; |
| 42 | + } |
| 43 | + |
| 44 | + // The -buildmode flag mutes the unit test output, so it is ommitted |
| 45 | + let module_args = [ |
| 46 | + "test", |
| 47 | + "-c", |
| 48 | + "-ldflags=-checklinkname=0", |
| 49 | + "-o", |
| 50 | + test_wasm_path |
| 51 | + .to_str() |
| 52 | + .expect("the combined paths of 'output-dir' and 'pkg' are not valid unicode"), |
| 53 | + path.to_str().expect("pkg path is not valid unicode"), |
| 54 | + ]; |
| 55 | + |
| 56 | + // TODO: for when we figure out how wasip2 tests are to be run |
| 57 | + #[allow(unused_variables)] |
| 58 | + let component_args = [ |
| 59 | + "test", |
| 60 | + "-c", |
| 61 | + "-buildmode=c-shared", |
| 62 | + "-ldflags=-checklinkname=0", |
| 63 | + "-o", |
| 64 | + test_wasm_path |
| 65 | + .to_str() |
| 66 | + .expect("the combined paths of 'output-dir' and 'pkg' are not valid unicode"), |
| 67 | + path.to_str().expect("pkg path is not valid unicode"), |
| 68 | + ]; |
| 69 | + |
| 70 | + let output = if only_wasip1 { |
| 71 | + Command::new(&go) |
| 72 | + .args(module_args) |
| 73 | + .env("GOOS", "wasip1") |
| 74 | + .env("GOARCH", "wasm") |
| 75 | + .output()? |
| 76 | + } else { |
| 77 | + unimplemented!(); |
| 78 | + |
| 79 | + // TODO: for when we figure out how wasip2 tests are to be run |
| 80 | + #[allow(unreachable_code)] |
| 81 | + Command::new(&go) |
| 82 | + .args(component_args) |
| 83 | + .env("GOOS", "wasip1") |
| 84 | + .env("GOARCH", "wasm") |
| 85 | + .output()? |
| 86 | + }; |
| 87 | + |
| 88 | + if !output.status.success() { |
| 89 | + return Err(anyhow!( |
| 90 | + "'go test -c' command failed: {}", |
| 91 | + String::from_utf8_lossy(&output.stderr) |
| 92 | + )); |
| 93 | + } |
| 94 | + |
| 95 | + Ok(test_wasm_path) |
| 96 | +} |
| 97 | + |
| 98 | +// Format the test filename based on the package path (see unit tests for more details). |
| 99 | +pub fn get_test_filename(path: &Path) -> String { |
| 100 | + let components: Vec<&str> = path |
| 101 | + .components() |
| 102 | + .filter_map(|c| match c { |
| 103 | + // Filter out the `/` and `.` |
| 104 | + std::path::Component::Normal(s) => s.to_str(), |
| 105 | + _ => None, |
| 106 | + }) |
| 107 | + .collect(); |
| 108 | + |
| 109 | + let tail = if components.len() >= 2 { |
| 110 | + &components[components.len() - 2..] |
| 111 | + } else { |
| 112 | + &components[..] |
| 113 | + }; |
| 114 | + |
| 115 | + format!("test_{}.wasm", tail.join("_")) |
| 116 | +} |
| 117 | + |
| 118 | +#[cfg(test)] |
| 119 | +mod tests { |
| 120 | + use super::*; |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn test_get_test_filename() { |
| 124 | + let tests = [ |
| 125 | + ("./foo/bar/baz", "test_bar_baz.wasm"), |
| 126 | + ("./foo/bar", "test_foo_bar.wasm"), |
| 127 | + ("./bar", "test_bar.wasm"), |
| 128 | + ("/usr/bin/foo/bar/baz", "test_bar_baz.wasm"), |
| 129 | + ]; |
| 130 | + |
| 131 | + for (input, expected) in tests.iter() { |
| 132 | + let input_string = input.to_string(); |
| 133 | + let actual = get_test_filename(&PathBuf::from(input_string)); |
| 134 | + assert_eq!(actual, expected.to_string()); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments