Skip to content

Commit

Permalink
replace exit_code test fixture with noop test fixture when tests rely…
Browse files Browse the repository at this point in the history
… on success results

The exit code function does not return valid JSON which means it will fail with a non-zero exit code.
Instead, use a new noop test fixture that does return valid JSON for tests that expect a successful result.
  • Loading branch information
jacobsteves committed Dec 17, 2024
1 parent 9284dcf commit 41d5220
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 16 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
"tests/fixtures/exit_code",
"tests/fixtures/log_truncation_function",
"tests/fixtures/exports",
"tests/fixtures/noop",
]

[package]
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Example Functions used as test fixtures.

**Rust examples:**
```
cargo wasi build --profile=wasm -p exit_code -p exports -p log_truncation_function &&
cp target/wasm32-wasi/wasm/{exit_code.wasm,exports.wasm,log_truncation_function.wasm} tests/fixtures/build
cargo wasi build --profile=wasm -p exit_code -p exports -p log_truncation_function -p noop &&
cp target/wasm32-wasi/wasm/{exit_code.wasm,exports.wasm,log_truncation_function.wasm,noop.wasm} tests/fixtures/build
```

**JS examples:**
Expand Down
Binary file modified tests/fixtures/build/exit_code.wasm
Binary file not shown.
Binary file modified tests/fixtures/build/exports.wasm
Binary file not shown.
Binary file modified tests/fixtures/build/log_truncation_function.wasm
Binary file not shown.
Binary file added tests/fixtures/build/noop.wasm
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/fixtures/noop/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "noop"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
nanoserde = "0.1.37"
9 changes: 9 additions & 0 deletions tests/fixtures/noop/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::io;
use std::io::Write;

fn main() -> std::io::Result<()> {
let input_string = io::read_to_string(io::stdin())?;
std::io::stdout().write_all(input_string.as_bytes())?;
std::io::stdout().flush()?;
Ok(())
}
29 changes: 15 additions & 14 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ mod tests {
#[test]
fn run() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("function-runner")?;
let input_file = temp_input(json!({"exit_code": 0}))?;
let input_file = temp_input(json!({"count": 0}))?;

cmd.args(["--function", "tests/fixtures/build/exit_code.wasm"])
cmd.args(["--function", "tests/fixtures/build/noop.wasm"])
.arg("--input")
.arg(input_file.as_os_str());
cmd.assert().success();
Expand Down Expand Up @@ -100,9 +100,9 @@ mod tests {
#[test]
fn run_json() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("function-runner")?;
let input_file = temp_input(json!({"exit_code": 0}))?;
let input_file = temp_input(json!({"count": 0}))?;

cmd.args(["--function", "tests/fixtures/build/exit_code.wasm"])
cmd.args(["--function", "tests/fixtures/build/noop.wasm"])
.arg("--json")
.arg("--input")
.arg(input_file.as_os_str());
Expand Down Expand Up @@ -146,8 +146,7 @@ mod tests {
fn profile_writes_file() -> Result<(), Box<dyn std::error::Error>> {
let (mut cmd, temp) = profile_base_cmd_in_temp_dir()?;
cmd.arg("--profile").assert().success();
temp.child("exit_code.perf")
.assert(predicate::path::exists());
temp.child("noop.perf").assert(predicate::path::exists());

Ok(())
}
Expand All @@ -167,8 +166,7 @@ mod tests {
cmd.args(["--profile-frequency", "80000"])
.assert()
.success();
temp.child("exit_code.perf")
.assert(predicate::path::exists());
temp.child("noop.perf").assert(predicate::path::exists());

Ok(())
}
Expand All @@ -183,10 +181,13 @@ mod tests {
.arg(input_file.as_os_str());

cmd.assert()
.success()
.failure()
.stdout(contains("Key not found code"))
.stdout(contains("Invalid Output"))
.stdout(contains("JSON Error"))
.stderr(contains(
"Error: The Function execution failed. Review the logs for more information.",
))
.stderr(contains(""));

Ok(())
Expand Down Expand Up @@ -246,11 +247,11 @@ mod tests {
let cwd = std::env::current_dir()?;
let temp = assert_fs::TempDir::new()?;
let input_file = temp.child("input.json");
input_file.write_str(json!({"exit_code": 0}).to_string().as_str())?;
input_file.write_str(json!({"count": 0}).to_string().as_str())?;

cmd.current_dir(temp.path())
.arg("--function")
.arg(cwd.join("tests/fixtures/build/exit_code.wasm"))
.arg(cwd.join("tests/fixtures/build/noop.wasm"))
.arg("--input")
.arg(input_file.as_os_str());

Expand All @@ -276,7 +277,7 @@ mod tests {
]
}}))?;

cmd.args(["--function", "tests/fixtures/build/exit_code.wasm"])
cmd.args(["--function", "tests/fixtures/build/noop.wasm"])
.arg("--input")
.arg(input_file.as_os_str());
cmd.assert().success();
Expand All @@ -300,7 +301,7 @@ mod tests {
]
}}))?;

cmd.args(["--function", "tests/fixtures/build/exit_code.wasm"])
cmd.args(["--function", "tests/fixtures/build/noop.wasm"])
.arg("--input")
.arg(input_file.as_os_str())
.arg("--schema-path")
Expand Down Expand Up @@ -328,7 +329,7 @@ mod tests {
});
let input_file = temp_input(json_data)?;

cmd.args(["--function", "tests/fixtures/build/exit_code.wasm"])
cmd.args(["--function", "tests/fixtures/build/noop.wasm"])
.arg("--input")
.arg(input_file.as_os_str())
.arg("--schema-path")
Expand Down

0 comments on commit 41d5220

Please sign in to comment.