Skip to content

Commit 96cac3c

Browse files
authored
Merge pull request #8 from antithesishq/fix-assertion-output
Fix defect in emitting assertions without namespacing in and add int…
2 parents f410a28 + 6e63ac1 commit 96cac3c

File tree

10 files changed

+240
-56
lines changed

10 files changed

+240
-56
lines changed

lib/src/assert/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ impl AssertionInfo {
193193
}
194194

195195
fn emit(&self) {
196-
internal::dispatch_output(&self);
196+
let json_event = json!({ "antithesis_assert": &self });
197+
internal::dispatch_output(&json_event)
197198
}
198199
}
199200

lib/src/internal/local_handler.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ use std::env;
22
use std::fs::File;
33
use std::io::{Error, Write};
44

5-
// use crate::internal::noop_handler::NoOpHandler;
6-
use crate::internal::LibHandler;
7-
8-
const LOCAL_OUTPUT: &str = "ANTITHESIS_SDK_LOCAL_OUTPUT";
5+
use crate::internal::{LibHandler, LOCAL_OUTPUT};
96

107
pub struct LocalHandler {
118
writer: File,

lib/src/internal/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const PROTOCOL_VERSION: &str = "1.0.0";
3434
// Tracks SDK releases
3535
const SDK_VERSION: &str = "0.1.2";
3636

37+
pub const LOCAL_OUTPUT: &str = "ANTITHESIS_SDK_LOCAL_OUTPUT";
38+
3739
pub(crate) static LIB_HANDLER: Lazy<Box<dyn LibHandler + Sync + Send>> = Lazy::new(|| {
3840
let handler: Box<dyn LibHandler + Sync + Send> = match VoidstarHandler::try_load() {
3941
Ok(handler) => Box::new(handler),

lib/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,5 @@ pub fn antithesis_init() {
8484

8585
pub use assert::{assert_impl, assert_raw, CatalogInfo};
8686
use once_cell::sync::Lazy;
87+
88+
pub use crate::internal::LOCAL_OUTPUT;
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
use antithesis_sdk::{antithesis_init, assert_always, LOCAL_OUTPUT};
2+
use serde_json::{json, Value};
3+
4+
mod common;
5+
use common::{AntithesisAssert, AssertType, SDKInput};
6+
7+
// Expected Output in /tmp/antithesis-assert-always-with-details.json
8+
// Note: Actual version info in antithesis_sdk can vary
9+
//
10+
// {
11+
// "antithesis_sdk": {
12+
// "language": {
13+
// "name": "Rust",
14+
// "version": "1.69.0"
15+
// },
16+
// "sdk_version": "0.1.2",
17+
// "protocol_version": "1.0.0"
18+
// }
19+
// }
20+
// {
21+
// "antithesis_assert": {
22+
// "assert_type": "always",
23+
// "condition": false,
24+
// "details": {},
25+
// "display_type": "Always",
26+
// "hit": false,
27+
// "id": "Waterproof Red",
28+
// "location": {
29+
// "begin_column": 5,
30+
// "begin_line": 23,
31+
// "class": "assert_always_with_details",
32+
// "file": "lib/tests/assert_always_with_details.rs",
33+
// "function": "assert_always_with_details::assert_always_with_details"
34+
// },
35+
// "message": "Waterproof Red",
36+
// "must_hit": true
37+
// }
38+
// }
39+
// {
40+
// "antithesis_assert": {
41+
// "assert_type": "always",
42+
// "condition": true,
43+
// "details": {
44+
// "color": "red",
45+
// "labels": [
46+
// "outdoor",
47+
// "waterproof"
48+
// ],
49+
// "width": 4
50+
// },
51+
// "display_type": "Always",
52+
// "hit": true,
53+
// "id": "Waterproof Red",
54+
// "location": {
55+
// "begin_column": 5,
56+
// "begin_line": 23,
57+
// "class": "assert_always_with_details",
58+
// "file": "lib/tests/assert_always_with_details.rs",
59+
// "function": "assert_always_with_details::assert_always_with_details"
60+
// },
61+
// "message": "Waterproof Red",
62+
// "must_hit": true
63+
// }
64+
// }
65+
66+
#[test]
67+
fn assert_always_with_details() {
68+
let output_file = "/tmp/antithesis-assert-always-with-details.json";
69+
let prev_v = common::env::set_var(LOCAL_OUTPUT, output_file);
70+
antithesis_init();
71+
let clothing_details: Value = json!({
72+
"color": "red",
73+
"width": 4,
74+
"labels": [
75+
"outdoor",
76+
"waterproof"
77+
]
78+
});
79+
let is_waterproof = true;
80+
assert_always!(is_waterproof, "Waterproof Red", &clothing_details);
81+
82+
// verify the output has landed in the expected file
83+
match common::read_jsonl_tags(output_file) {
84+
Ok(x) => {
85+
let mut did_register = false;
86+
let mut did_hit = false;
87+
for obj in x.iter() {
88+
if let SDKInput::AntithesisAssert(AntithesisAssert {
89+
assert_type,
90+
condition,
91+
display_type,
92+
hit,
93+
must_hit,
94+
id,
95+
message,
96+
location,
97+
details,
98+
}) = obj
99+
{
100+
if *hit {
101+
did_hit = true;
102+
assert_eq!(*condition, is_waterproof);
103+
assert_eq!(details, &clothing_details);
104+
} else {
105+
did_register = true;
106+
};
107+
assert_eq!(*assert_type, AssertType::Always);
108+
assert_eq!(*display_type, "Always");
109+
assert!(*must_hit);
110+
assert_eq!(message, "Waterproof Red");
111+
assert_eq!(id, message);
112+
assert!(location.begin_line > 0);
113+
assert!(location.begin_column >= 0);
114+
assert_eq!(location.class, "assert_always_with_details");
115+
assert!(location.function.ends_with("::assert_always_with_details"));
116+
assert!(location
117+
.file
118+
.ends_with("/tests/assert_always_with_details.rs"));
119+
}
120+
println!("{:?}", obj);
121+
}
122+
assert!(did_register);
123+
assert!(did_hit);
124+
}
125+
Err(e) => println!("{}", e),
126+
}
127+
common::env::restore_var(LOCAL_OUTPUT, prev_v);
128+
}

lib/tests/common/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ pub struct AntithesisSetup {
2727

2828
#[derive(Deserialize, Serialize, Debug)]
2929
pub struct Location {
30-
begin_column: i32,
31-
begin_line: i32,
32-
class: String,
33-
file: String,
34-
function: String,
30+
pub begin_column: i32,
31+
pub begin_line: i32,
32+
pub class: String,
33+
pub file: String,
34+
pub function: String,
3535
}
3636

3737
#[derive(Deserialize, Debug)]
3838
pub struct AntithesisAssert {
39-
assert_type: AssertType,
40-
condition: bool,
41-
display_type: String,
42-
hit: bool,
43-
must_hit: bool,
44-
id: String,
45-
message: String,
46-
location: Location,
47-
details: Value,
39+
pub assert_type: AssertType,
40+
pub condition: bool,
41+
pub display_type: String,
42+
pub hit: bool,
43+
pub must_hit: bool,
44+
pub id: String,
45+
pub message: String,
46+
pub location: Location,
47+
pub details: Value,
4848
}
4949

5050
#[derive(Deserialize, Debug)]
@@ -56,9 +56,9 @@ pub enum SDKInput {
5656
SendEvent { event_name: String, details: Value },
5757
}
5858

59-
#[derive(Deserialize, Debug)]
59+
#[derive(Deserialize, Debug, PartialEq)]
6060
#[serde(rename_all = "snake_case")]
61-
enum AssertType {
61+
pub enum AssertType {
6262
Always,
6363
Sometimes,
6464
Reachability,

lib/tests/sdk_info.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
1-
use antithesis_sdk::lifecycle;
1+
use antithesis_sdk::{lifecycle, LOCAL_OUTPUT};
22
use serde_json::json;
33

44
mod common;
55
use common::SDKInput;
66

7-
const LOCAL_OUTPUT: &str = "ANTITHESIS_SDK_LOCAL_OUTPUT";
7+
// Expected output in /tmp/antithesis-sdk-info.json
8+
// Note: Actual version info in antithesis_sdk can vary
9+
//
10+
// {
11+
// "antithesis_sdk": {
12+
// "language": {
13+
// "name": "Rust",
14+
// "version": "1.69.0"
15+
// },
16+
// "sdk_version": "0.1.2",
17+
// "protocol_version": "1.0.0"
18+
// }
19+
// }
20+
// {
21+
// "antithesis_setup": {
22+
// "status": "complete",
23+
// "details": {}
24+
// }
25+
// }
826

9-
// ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
10-
// File: /tmp/antithesis-lifecycle.json
11-
// ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
12-
// {"antithesis_sdk":{"language":{"name":"Rust","version":"1.77.1"},"protocol_version":"1.0.0","sdk_version":"0.1.1"}}
13-
// {"antithesis_setup":{"details":{"age":4,"name":"Tweety Bird","phones":["+1 9734970340"]},"status":"complete"}}
14-
// ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1527
#[test]
1628
fn sdk_info() {
17-
let output_file = "/tmp/antithesis-sdk.json";
29+
let output_file = "/tmp/antithesis-sdk-info.json";
1830
let prev_v = common::env::set_var(LOCAL_OUTPUT, output_file);
1931
let no_details = json!({});
2032

lib/tests/send_event.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1-
use antithesis_sdk::lifecycle;
1+
use antithesis_sdk::{lifecycle, LOCAL_OUTPUT};
22
use serde_json::json;
33

44
mod common;
55
use common::SDKInput;
66

7-
const LOCAL_OUTPUT: &str = "ANTITHESIS_SDK_LOCAL_OUTPUT";
7+
// Expected output in /tmp/antithesis-send-event.json
8+
// Note: Actual version info in antithesis_sdk can vary
9+
//
10+
// {
11+
// "antithesis_sdk": {
12+
// "language": {
13+
// "name": "Rust",
14+
// "version": "1.69.0"
15+
// },
16+
// "sdk_version": "0.1.2",
17+
// "protocol_version": "1.0.0"
18+
// }
19+
// }
20+
// {
21+
// "logging": {
22+
// "tag": "last value",
23+
// "x": 100
24+
// }
25+
// }
826

9-
// ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
10-
// File: /tmp/antithesis-lifecycle.json
11-
// ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
12-
// {"antithesis_sdk":{"language":{"name":"Rust","version":"1.77.1"},"protocol_version":"1.0.0","sdk_version":"0.1.1"}}
13-
// {"antithesis_setup":{"details":{"age":4,"name":"Tweety Bird","phones":["+1 9734970340"]},"status":"complete"}}
14-
// ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1527
#[test]
1628
fn send_event() {
1729
let output_file = "/tmp/antithesis-send-event.json";

lib/tests/setup_complete_with_details.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
1-
use antithesis_sdk::lifecycle;
1+
use antithesis_sdk::{lifecycle, LOCAL_OUTPUT};
22
use serde_json::{json, Value};
33

44
mod common;
55
use common::{AntithesisSetup, SDKInput};
66

7-
const LOCAL_OUTPUT: &str = "ANTITHESIS_SDK_LOCAL_OUTPUT";
7+
// Expected output in /tmp/antithesis-lifecycle-with-details.json
8+
// Note: Actual version info in antithesis_sdk can vary
9+
//
10+
// {
11+
// "antithesis_sdk": {
12+
// "language": {
13+
// "name": "Rust",
14+
// "version": "1.69.0"
15+
// },
16+
// "sdk_version": "0.1.2",
17+
// "protocol_version": "1.0.0"
18+
// }
19+
// }
20+
// {
21+
// "antithesis_setup": {
22+
// "status": "complete",
23+
// "details": {
24+
// "age": 4,
25+
// "name": "Tweety Bird",
26+
// "phones": [
27+
// "+1 9374970340"
28+
// ]
29+
// }
30+
// }
31+
// }
832

9-
// ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
10-
// File: /tmp/antithesis-lifecycle.json
11-
// ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
12-
// {"antithesis_sdk":{"language":{"name":"Rust","version":"1.77.1"},"protocol_version":"1.0.0","sdk_version":"0.1.1"}}
13-
// {"antithesis_setup":{"details":{"age":4,"name":"Tweety Bird","phones":["+1 9734970340"]},"status":"complete"}}
14-
// ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1533
#[test]
1634
fn setup_complete_with_details() {
1735
let output_file = "/tmp/antithesis-lifecycle-with-details.json";
@@ -20,7 +38,7 @@ fn setup_complete_with_details() {
2038
"name": "Tweety Bird",
2139
"age": 4,
2240
"phones": [
23-
"+1 9734970340"
41+
"+1 9374970340"
2442
]
2543
});
2644
lifecycle::setup_complete(&bird_value);

lib/tests/setup_complete_without_details.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
1-
use antithesis_sdk::lifecycle;
1+
use antithesis_sdk::{lifecycle, LOCAL_OUTPUT};
22
use serde_json::json;
33

44
mod common;
55
use common::{AntithesisSetup, SDKInput};
66

7-
const LOCAL_OUTPUT: &str = "ANTITHESIS_SDK_LOCAL_OUTPUT";
7+
// Expected output in /tmp/antithesis-setup-complete-without-details.json
8+
// Note: Actual version info in antithesis_sdk can vary
9+
//
10+
// {
11+
// "antithesis_sdk": {
12+
// "language": {
13+
// "name": "Rust",
14+
// "version": "1.69.0"
15+
// },
16+
// "sdk_version": "0.1.2",
17+
// "protocol_version": "1.0.0"
18+
// }
19+
// }
20+
// {
21+
// "antithesis_setup": {
22+
// "status": "complete",
23+
// "details": {}
24+
// }
25+
// }
826

9-
// ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
10-
// File: /tmp/antithesis-lifecycle.json
11-
// ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
12-
// {"antithesis_sdk":{"language":{"name":"Rust","version":"1.77.1"},"protocol_version":"1.0.0","sdk_version":"0.1.1"}}
13-
// {"antithesis_setup":{"details":{},"status":"complete"}}
14-
// ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1527
#[test]
1628
fn setup_complete_without_details() {
17-
let output_file = "/tmp/antithesis-lifecycle-withou-details.json";
29+
let output_file = "/tmp/antithesis-setup-complete-without-details.json";
1830
let prev_v = common::env::set_var(LOCAL_OUTPUT, output_file);
1931
let no_details = json!({});
2032

0 commit comments

Comments
 (0)