-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathassertions.rs
More file actions
133 lines (118 loc) · 4.21 KB
/
assertions.rs
File metadata and controls
133 lines (118 loc) · 4.21 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
//! Environment State Assertions
//!
//! Provides assertion utilities for verifying environment state after
//! command execution in black-box tests.
use anyhow::{Context, Result};
use serde_json::Value;
use std::fs;
use std::path::{Path, PathBuf};
/// Assertions for verifying environment state after command execution
///
/// This struct provides methods to verify that the environment was created
/// correctly and that all expected files and directories exist with the
/// correct structure and content.
pub struct EnvironmentStateAssertions {
workspace_path: PathBuf,
}
impl EnvironmentStateAssertions {
/// Create a new assertions helper for the given workspace
#[must_use]
pub fn new<P: AsRef<Path>>(workspace_path: P) -> Self {
Self {
workspace_path: workspace_path.as_ref().to_path_buf(),
}
}
/// Assert that the environment exists
///
/// Verifies that the environment state file exists at the expected location.
///
/// # Panics
///
/// Panics if the environment file does not exist.
pub fn assert_environment_exists(&self, env_name: &str) {
let env_file_path = self.environment_json_path(env_name);
assert!(
env_file_path.exists(),
"Environment file should exist at: {}",
env_file_path.display()
);
}
/// Assert that the environment is in the expected state
///
/// Verifies that the environment state matches the expected state string.
/// The state is determined by the top-level key in the environment JSON.
///
/// # Panics
///
/// Panics if the environment JSON cannot be read or if the state doesn't match.
pub fn assert_environment_state_is(&self, env_name: &str, expected_state: &str) {
let env_data = self
.read_environment_json(env_name)
.expect("Failed to read environment JSON");
// Parse the environment state structure
let state_key = env_data
.as_object()
.expect("Environment JSON should be an object")
.keys()
.next()
.expect("Environment should have a state key");
assert_eq!(
state_key, expected_state,
"Environment state should be '{expected_state}', but was '{state_key}'"
);
}
/// Assert that the data directory structure exists
///
/// Verifies that the environment's data directory and required files exist.
///
/// # Panics
///
/// Panics if the data directory or environment JSON file doesn't exist.
#[allow(dead_code)]
pub fn assert_data_directory_structure(&self, env_name: &str) {
let data_dir = self.workspace_path.join("data").join(env_name);
assert!(
data_dir.exists(),
"Data directory should exist at: {}",
data_dir.display()
);
let env_json = data_dir.join("environment.json");
assert!(
env_json.exists(),
"Environment JSON should exist at: {}",
env_json.display()
);
}
/// Assert that the trace directory exists
///
/// Verifies that the environment's traces directory exists for observability.
///
/// # Panics
///
/// Panics if the traces directory doesn't exist.
#[allow(dead_code)]
pub fn assert_trace_directory_exists(&self, env_name: &str) {
let traces_dir = self.workspace_path.join(env_name).join("traces");
assert!(
traces_dir.exists(),
"Traces directory should exist at: {}",
traces_dir.display()
);
}
fn environment_json_path(&self, env_name: &str) -> PathBuf {
self.workspace_path
.join("data")
.join(env_name)
.join("environment.json")
}
fn read_environment_json(&self, env_name: &str) -> Result<Value> {
let env_file_path = self.environment_json_path(env_name);
let content = fs::read_to_string(&env_file_path).context(format!(
"Failed to read environment file: {}",
env_file_path.display()
))?;
let json: Value =
serde_json::from_str(&content).context("Failed to parse environment JSON")?;
Ok(json)
}
}