Skip to content

Commit

Permalink
feat: Add --raw-logs option for forc-test (#6528)
Browse files Browse the repository at this point in the history
## Description

Closes #6015

- Adds the `--raw-logs` option
- Removes the `decode` option as this is now the default for `--logs`
- Shortens `--pretty-print` to `--pretty` 

## Checklist

- [x] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
sdankel authored Sep 13, 2024
1 parent 7b07216 commit 7ab1565
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 22 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,19 @@ jobs:
env:
RUST_BACKTRACE: full
run: cargo nextest run --locked --release -p sway-lsp --no-capture --profile ci --config-file sway-lsp/tests/nextest.toml
cargo-test-forc:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.RUST_VERSION }}
- uses: Swatinem/rust-cache@v2
- name: Run forc tests separately
env:
RUST_BACKTRACE: full
run: cargo test --locked --release -p forc -- --nocapture
cargo-test-workspace:
runs-on: ubuntu-latest
steps:
Expand All @@ -561,7 +574,7 @@ jobs:
toolchain: ${{ env.RUST_VERSION }}
- uses: Swatinem/rust-cache@v2
- name: Run tests
run: cargo test --locked --release --workspace --exclude forc-debug --exclude sway-lsp --exclude forc-client
run: cargo test --locked --release --workspace --exclude forc-debug --exclude sway-lsp --exclude forc-client --exclude forc
cargo-unused-deps-check:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 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 forc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ uwu = ["uwuify"]

[dev-dependencies]
completest-pty = "0.5.0"
rexpect = "0.5"
41 changes: 20 additions & 21 deletions forc/src/cli/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,15 @@ pub struct Command {
#[derive(Parser, Debug, Clone)]
#[clap(after_help = help())]
pub struct TestPrintOpts {
#[clap(long = "pretty-print", short = 'r')]
#[clap(long = "pretty")]
/// Pretty-print the logs emitted from tests.
pub pretty_print: bool,
/// Print `Log` and `LogData` receipts for tests.
#[clap(long = "logs", short = 'l')]
pub print_logs: bool,
/// Decode logs and show decoded log information in human readable format alongside the raw
/// logs.
#[clap(long = "decode", short = 'd')]
pub decode_logs: bool,
/// Print the raw logs for tests.
#[clap(long)]
pub raw_logs: bool,
}

pub(crate) fn exec(cmd: Command) -> ForcResult<()> {
Expand Down Expand Up @@ -150,26 +149,26 @@ fn print_tested_pkg(pkg: &TestedPackage, test_print_opts: &TestPrintOpts) -> For
);

// If logs are enabled, print them.
let logs = &test.logs;
if test_print_opts.print_logs {
let logs = &test.logs;
if test_print_opts.decode_logs {
for log in logs {
if let Receipt::LogData {
rb,
data: Some(data),
..
} = log
{
let decoded_log_data =
decode_log_data(&rb.to_string(), data, &pkg.built.program_abi)?;
let var_value = decoded_log_data.value;
info!("Decoded log value: {}, log rb: {}", var_value, rb);
}
for log in logs {
if let Receipt::LogData {
rb,
data: Some(data),
..
} = log
{
let decoded_log_data =
decode_log_data(&rb.to_string(), data, &pkg.built.program_abi)?;
let var_value = decoded_log_data.value;
info!("Decoded log value: {}, log rb: {}", var_value, rb);
}
info!("Raw logs:");
}
}

if test_print_opts.raw_logs {
let formatted_logs = format_log_receipts(logs, test_print_opts.pretty_print)?;
info!("{}", formatted_logs);
info!("Raw logs:\n{}", formatted_logs);
}

// If the test is failing, save the test result for printing the details later on.
Expand Down
84 changes: 84 additions & 0 deletions forc/tests/cli_integration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::path::PathBuf;

use rexpect::spawn;

const TIMEOUT_MS: u64 = 300000;

fn test_fixtures_path() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("fixtures")
.canonicalize()
.unwrap()
}

#[test]
fn test_forc_test_decoded_logs() -> Result<(), rexpect::error::Error> {
// Spawn the forc binary using cargo run
let project_dir = test_fixtures_path().join("test_contract");
let mut process = spawn(
&format!(
"cargo run --bin forc -- test --logs --path {}",
project_dir.to_string_lossy()
),
Some(TIMEOUT_MS),
)?;

// Assert that the output is correct
process.exp_string(" test test_log_4")?;
process.exp_string("Decoded log value: 4, log rb: 1515152261580153489")?;
process.exp_string(" test test_log_2")?;
process.exp_string("Decoded log value: 2, log rb: 1515152261580153489")?;

process.process.exit()?;
Ok(())
}

#[test]
fn test_forc_test_raw_logs() -> Result<(), rexpect::error::Error> {
// Spawn the forc binary using cargo run
let project_dir = test_fixtures_path().join("test_contract");
let mut process = spawn(
&format!(
"cargo run --bin forc -- test --raw-logs --path {}",
project_dir.to_string_lossy()
),
Some(TIMEOUT_MS),
)?;

// Assert that the output is correct
process.exp_string(" test test_log_4")?;
process.exp_string("Raw logs:")?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12652,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.exp_string(" test test_log_2")?;
process.exp_string("Raw logs:")?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12652,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;

process.process.exit()?;
Ok(())
}

#[test]
fn test_forc_test_both_logs() -> Result<(), rexpect::error::Error> {
// Spawn the forc binary using cargo run
let project_dir = test_fixtures_path().join("test_contract");
let mut process = spawn(
&format!(
"cargo run --bin forc -- test --logs --raw-logs --path {}",
project_dir.to_string_lossy()
),
Some(TIMEOUT_MS),
)?;

// Assert that the output is correct
process.exp_string(" test test_log_4")?;
process.exp_string("Decoded log value: 4, log rb: 1515152261580153489")?;
process.exp_string("Raw logs:")?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12652,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.exp_string(" test test_log_2")?;
process.exp_string("Decoded log value: 2, log rb: 1515152261580153489")?;
process.exp_string("Raw logs:")?;
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12652,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
process.process.exit()?;
Ok(())
}
2 changes: 2 additions & 0 deletions forc/tests/fixtures/test_contract/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
13 changes: 13 additions & 0 deletions forc/tests/fixtures/test_contract/Forc.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = "core"
source = "path+from-root-4D4735C41181917E"

[[package]]
name = "std"
source = "path+from-root-4D4735C41181917E"
dependencies = ["core"]

[[package]]
name = "test_contract"
source = "member"
dependencies = ["std"]
8 changes: 8 additions & 0 deletions forc/tests/fixtures/test_contract/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "test_contract"

[dependencies]
std = { path = "../../../../sway-lib-std/" }
23 changes: 23 additions & 0 deletions forc/tests/fixtures/test_contract/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
contract;

abi MyContract {
fn test_function() -> bool;
}

impl MyContract for Contract {
fn test_function() -> bool {
true
}
}

#[test]
fn test_log_4() {
log(4);
assert(1 == 1)
}

#[test]
fn test_log_2() {
log(2);
assert(1 == 1)
}

0 comments on commit 7ab1565

Please sign in to comment.