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