|
| 1 | +#[cfg(test)] |
1 | 2 | use super::tree::{node_to_string_with_scheme, ColorScheme}; |
| 3 | +#[cfg(test)] |
2 | 4 | use crate::node::Node; |
| 5 | +#[cfg(test)] |
3 | 6 | use std::env; |
| 7 | +#[cfg(test)] |
4 | 8 | use std::fs; |
| 9 | +#[cfg(test)] |
5 | 10 | use std::io::{self, Write}; |
6 | | -use std::path::PathBuf; |
7 | 11 |
|
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> { |
11 | 16 | 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)?; |
22 | 18 |
|
23 | | -/// Returns the path to the fixture directory |
24 | | -pub fn fixtures_directory() -> PathBuf { |
25 | 19 | 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"); |
34 | 21 | let fixture_path = fixtures_dir.join(format!("{}.txt", name)); |
35 | 22 |
|
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 | + } |
39 | 38 |
|
40 | 39 | Ok(()) |
41 | 40 | } |
42 | 41 |
|
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)?; |
46 | 47 |
|
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)); |
54 | 51 |
|
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(); |
59 | 53 |
|
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 { |
63 | 61 | assert_eq!( |
64 | 62 | generated, fixture_content, |
65 | 63 | "Generated tree output doesn't match fixture file: {}", |
66 | 64 | name |
67 | 65 | ); |
68 | 66 | } |
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); |
115 | 73 | } |
116 | 74 |
|
117 | 75 | Ok(()) |
|
0 commit comments