Skip to content

Commit cefbd78

Browse files
OttoAllmendingerllm-git
andcommitted
feat(wasm-utxo): refactor fixtures handling and tree rendering
Simplified the fixture handling code and tree rendering utilities. Moved non-test-related code behind #[cfg(test)] attributes to reduce binary size. Removed unused color schemes and unnecessary public exports. Issue: BTC-2652 Co-authored-by: llm-git <[email protected]>
1 parent f222325 commit cefbd78

File tree

7 files changed

+58
-148
lines changed

7 files changed

+58
-148
lines changed

packages/wasm-utxo/cli/src/format/fixtures.rs

Lines changed: 48 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,75 @@
1+
#[cfg(test)]
12
use super::tree::{node_to_string_with_scheme, ColorScheme};
3+
#[cfg(test)]
24
use crate::node::Node;
5+
#[cfg(test)]
36
use std::env;
7+
#[cfg(test)]
48
use std::fs;
9+
#[cfg(test)]
510
use std::io::{self, Write};
6-
use std::path::PathBuf;
711

8-
/// Generate a tree representation of a Node without colors
9-
pub fn generate_tree_text(node: &Node) -> Result<String, io::Error> {
10-
// Use the no-color scheme for consistent fixture output
12+
/// Ensure the generated tree output matches the fixture file
13+
/// If the fixture doesn't exist, it will be created
14+
#[cfg(test)]
15+
pub fn assert_tree_matches_fixture(node: &Node, name: &str) -> Result<(), io::Error> {
1116
let no_color_scheme = ColorScheme::no_color();
12-
node_to_string_with_scheme(node, &no_color_scheme)
13-
}
14-
15-
/// Generate a tree representation of a Node with a specific color scheme
16-
pub fn generate_tree_text_with_scheme(
17-
node: &Node,
18-
color_scheme: &ColorScheme,
19-
) -> Result<String, io::Error> {
20-
node_to_string_with_scheme(node, color_scheme)
21-
}
17+
let generated = node_to_string_with_scheme(node, &no_color_scheme)?;
2218

23-
/// Returns the path to the fixture directory
24-
pub fn fixtures_directory() -> PathBuf {
2519
let project_dir = env::current_dir().expect("Failed to get current directory");
26-
project_dir.join("test").join("fixtures")
27-
}
28-
29-
/// Write tree output to a fixture file
30-
pub fn write_fixture(name: &str, content: &str) -> Result<(), io::Error> {
31-
let fixtures_dir = fixtures_directory();
32-
fs::create_dir_all(&fixtures_dir)?;
33-
20+
let fixtures_dir = project_dir.join("test").join("fixtures");
3421
let fixture_path = fixtures_dir.join(format!("{}.txt", name));
3522

36-
// Write the content to the file
37-
let mut file = fs::File::create(&fixture_path)?;
38-
file.write_all(content.as_bytes())?;
23+
if fixture_path.exists() {
24+
let fixture_content = fs::read_to_string(&fixture_path)?;
25+
// Compare the generated output to the fixture
26+
assert_eq!(
27+
generated, fixture_content,
28+
"Generated tree output doesn't match fixture file: {}",
29+
name
30+
);
31+
} else {
32+
// Create the fixture if it doesn't exist
33+
fs::create_dir_all(&fixtures_dir)?;
34+
let mut file = fs::File::create(&fixture_path)?;
35+
file.write_all(generated.as_bytes())?;
36+
println!("Created new fixture: {}.txt", name);
37+
}
3938

4039
Ok(())
4140
}
4241

43-
/// Read the content of a fixture file if it exists
44-
pub fn read_fixture(name: &str) -> Result<Option<String>, io::Error> {
45-
let fixture_path = fixtures_directory().join(format!("{}.txt", name));
42+
/// Assert tree matches fixture, updating if needed or requested
43+
#[cfg(test)]
44+
pub fn assert_or_update_fixture(node: &Node, name: &str) -> Result<(), io::Error> {
45+
let no_color_scheme = ColorScheme::no_color();
46+
let generated = node_to_string_with_scheme(node, &no_color_scheme)?;
4647

47-
if fixture_path.exists() {
48-
let content = fs::read_to_string(&fixture_path)?;
49-
Ok(Some(content))
50-
} else {
51-
Ok(None)
52-
}
53-
}
48+
let project_dir = env::current_dir().expect("Failed to get current directory");
49+
let fixtures_dir = project_dir.join("test").join("fixtures");
50+
let fixture_path = fixtures_dir.join(format!("{}.txt", name));
5451

55-
/// Ensure the generated tree output matches the fixture file
56-
/// If the fixture doesn't exist, it will be created
57-
pub fn assert_tree_matches_fixture(node: &Node, name: &str) -> Result<(), io::Error> {
58-
let generated = generate_tree_text(node)?;
52+
let update_fixtures = env::var("UPDATE_FIXTURES").is_ok();
5953

60-
match read_fixture(name)? {
61-
Some(fixture_content) => {
62-
// Compare the generated output to the fixture
54+
if fixture_path.exists() {
55+
let fixture_content = fs::read_to_string(&fixture_path)?;
56+
if update_fixtures || generated != fixture_content {
57+
let mut file = fs::File::create(&fixture_path)?;
58+
file.write_all(generated.as_bytes())?;
59+
println!("Updated fixture: {}.txt", name);
60+
} else {
6361
assert_eq!(
6462
generated, fixture_content,
6563
"Generated tree output doesn't match fixture file: {}",
6664
name
6765
);
6866
}
69-
None => {
70-
// Create the fixture if it doesn't exist
71-
write_fixture(name, &generated)?;
72-
println!("Created new fixture: {}.txt", name);
73-
}
74-
}
75-
76-
Ok(())
77-
}
78-
79-
/// Force update of a fixture file with new content
80-
pub fn update_fixture(node: &Node, name: &str) -> Result<(), io::Error> {
81-
let generated = generate_tree_text(node)?;
82-
write_fixture(name, &generated)
83-
}
84-
85-
// Environment variable to force fixture updates
86-
const UPDATE_FIXTURES_ENV: &str = "UPDATE_FIXTURES";
87-
88-
/// Check if fixtures should be updated
89-
pub fn should_update_fixtures() -> bool {
90-
env::var(UPDATE_FIXTURES_ENV).is_ok()
91-
}
92-
93-
/// Assert tree matches fixture, updating if needed or requested
94-
pub fn assert_or_update_fixture(node: &Node, name: &str) -> Result<(), io::Error> {
95-
let generated = generate_tree_text(node)?;
96-
97-
match read_fixture(name)? {
98-
Some(fixture_content) => {
99-
if should_update_fixtures() || generated != fixture_content {
100-
write_fixture(name, &generated)?;
101-
println!("Updated fixture: {}.txt", name);
102-
} else {
103-
assert_eq!(
104-
generated, fixture_content,
105-
"Generated tree output doesn't match fixture file: {}",
106-
name
107-
);
108-
}
109-
}
110-
None => {
111-
// Create the fixture if it doesn't exist
112-
write_fixture(name, &generated)?;
113-
println!("Created new fixture: {}.txt", name);
114-
}
67+
} else {
68+
// Create the fixture if it doesn't exist
69+
fs::create_dir_all(&fixtures_dir)?;
70+
let mut file = fs::File::create(&fixture_path)?;
71+
file.write_all(generated.as_bytes())?;
72+
println!("Created new fixture: {}.txt", name);
11573
}
11674

11775
Ok(())

packages/wasm-utxo/cli/src/format/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,4 @@ pub mod fixtures;
33
mod tests;
44
mod tree;
55

6-
pub use tree::{
7-
add_node_to_tree, add_node_to_tree_with_scheme, format_primitive_for_tree, node_to_string,
8-
node_to_string_with_scheme, render_tree, render_tree_with_scheme, ColorScheme,
9-
};
6+
pub use tree::{render_tree_with_scheme, ColorScheme};

packages/wasm-utxo/cli/src/format/tree.rs

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::node::{Node, Primitive};
22
use colored::*;
3-
use ptree::{print_tree, TreeBuilder};
3+
use ptree::print_tree;
4+
#[cfg(test)]
5+
use ptree::TreeBuilder;
46
use std::borrow::Cow;
57
use std::io;
68

@@ -46,30 +48,6 @@ impl ColorScheme {
4648
}
4749
}
4850

49-
/// High contrast color scheme for better visibility
50-
pub fn high_contrast() -> Self {
51-
Self {
52-
label_style: |s| s.bold().bright_white().to_string(),
53-
value_style: |s| s.to_string(),
54-
buffer_style: |s| s.bright_cyan().to_string(),
55-
numeric_style: |s| s.bright_yellow().to_string(),
56-
string_style: |s| s.bright_green().to_string(),
57-
boolean_style: |s| s.bright_magenta().to_string(),
58-
}
59-
}
60-
61-
/// Minimal color scheme with subtle styling
62-
pub fn minimal() -> Self {
63-
Self {
64-
label_style: |s| s.dimmed().to_string(),
65-
value_style: |s| s.to_string(),
66-
buffer_style: |s| s.blue().to_string(),
67-
numeric_style: |s| s.to_string(),
68-
string_style: |s| s.to_string(),
69-
boolean_style: |s| s.to_string(),
70-
}
71-
}
72-
7351
/// Apply appropriate styling to a primitive value based on its type
7452
pub fn style_primitive(&self, primitive: &Primitive, formatted_value: &str) -> String {
7553
match primitive {
@@ -149,7 +127,8 @@ pub fn format_primitive_for_tree(primitive: &Primitive) -> String {
149127
}
150128

151129
/// Render a Node tree to a string with the specified color scheme
152-
pub fn node_to_string_with_scheme(
130+
#[cfg(test)]
131+
pub(super) fn node_to_string_with_scheme(
153132
node: &Node,
154133
color_scheme: &ColorScheme,
155134
) -> Result<String, io::Error> {
@@ -173,17 +152,9 @@ pub fn node_to_string_with_scheme(
173152
Ok(String::from_utf8_lossy(&output).to_string())
174153
}
175154

176-
/// Render a Node tree to a string (using default color scheme for backward compatibility)
177-
pub fn node_to_string(node: &Node) -> Result<String, io::Error> {
178-
node_to_string_with_scheme(node, &ColorScheme::default())
179-
}
180-
181155
/// Helper function to add a node and its children to a tree with color scheme
182-
pub fn add_node_to_tree_with_scheme(
183-
tree: &mut TreeBuilder,
184-
node: &Node,
185-
color_scheme: &ColorScheme,
186-
) {
156+
#[cfg(test)]
157+
fn add_node_to_tree_with_scheme(tree: &mut TreeBuilder, node: &Node, color_scheme: &ColorScheme) {
187158
let styled_label = (color_scheme.label_style)(&node.label);
188159
let value_str = format_primitive_for_tree(&node.value);
189160
let styled_value = color_scheme.style_primitive(&node.value, &value_str);
@@ -197,18 +168,8 @@ pub fn add_node_to_tree_with_scheme(
197168
tree.end_child();
198169
}
199170

200-
/// Helper function to add a node and its children to a tree (using default color scheme for backward compatibility)
201-
pub fn add_node_to_tree(tree: &mut TreeBuilder, node: &Node) {
202-
add_node_to_tree_with_scheme(tree, node, &ColorScheme::default());
203-
}
204-
205171
/// Render a Node tree to the terminal with the specified color scheme
206172
pub fn render_tree_with_scheme(node: &Node, color_scheme: &ColorScheme) -> Result<(), io::Error> {
207173
let tree_item = NodeTreeItem { node, color_scheme };
208174
print_tree(&tree_item)
209175
}
210-
211-
/// Render a Node tree to the terminal (using default color scheme for backward compatibility)
212-
pub fn render_tree(node: &Node) -> Result<(), io::Error> {
213-
render_tree_with_scheme(node, &ColorScheme::default())
214-
}

packages/wasm-utxo/cli/src/node.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ impl Node {
8282
}
8383
}
8484

85+
#[cfg(test)]
8586
pub fn with_children(label: impl Into<String>, value: Primitive, children: Vec<Node>) -> Self {
8687
Self {
8788
label: label.into(),
@@ -94,15 +95,11 @@ impl Node {
9495
self.children.push(child);
9596
}
9697

97-
pub fn with_child(mut self, child: Node) -> Self {
98-
self.children.push(child);
99-
self
100-
}
101-
10298
pub fn extend(&mut self, nodes: impl IntoIterator<Item = Node>) {
10399
self.children.extend(nodes);
104100
}
105101

102+
#[cfg(test)]
106103
pub fn child_count(&self) -> usize {
107104
self.children.len()
108105
}

packages/wasm-utxo/cli/src/psbt/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,3 @@ pub fn handle_command(command: PsbtCommand) -> Result<()> {
2727
} => parse::handle_parse_command(path, no_color, raw),
2828
}
2929
}
30-

packages/wasm-utxo/cli/src/psbt/parse.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,3 @@ pub fn handle_parse_command(path: PathBuf, no_color: bool, raw: bool) -> Result<
3030

3131
Ok(())
3232
}
33-

packages/wasm-utxo/cli/src/tx/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,3 @@ pub fn handle_command(command: TxCommand) -> Result<()> {
2020
TxCommand::Parse { path, no_color } => parse::handle_parse_command(path, no_color),
2121
}
2222
}
23-

0 commit comments

Comments
 (0)