|
1 | | -use anyhow::{anyhow, Result}; |
| 1 | +use crate::patch::device::DeviceExt; |
| 2 | +use anyhow::{anyhow, Context, Result}; |
2 | 3 | use std::fs::File; |
3 | 4 | use std::io::Read; |
4 | 5 | use std::path::{Path, PathBuf}; |
@@ -37,6 +38,41 @@ pub fn get_patcher(test_subdir: &Path) -> Result<(Device, Hash)> { |
37 | 38 | Ok((device, yaml.clone())) |
38 | 39 | } |
39 | 40 |
|
| 41 | +/// Execute the test found in the specified res/ subdirectory. |
| 42 | +/// |
| 43 | +/// This runs the patch.yaml file in the specified subdirectory, and checks |
| 44 | +/// that the results match the expected contents found in the expected.svd file. |
| 45 | +pub fn test_expected(test_subdir: &Path) -> Result<()> { |
| 46 | + // Run the patch |
| 47 | + let (mut device, yaml) = get_patcher(test_subdir)?; |
| 48 | + device |
| 49 | + .process(&yaml, &Default::default()) |
| 50 | + .context("processing patch.yaml")?; |
| 51 | + |
| 52 | + // Load the expected contents |
| 53 | + let expected_file = res_dir().join(test_subdir.join("expected.svd")); |
| 54 | + let f = File::open(&expected_file) |
| 55 | + .with_context(|| format!("opening {}", expected_file.display()))?; |
| 56 | + let mut contents = String::new(); |
| 57 | + (&f).read_to_string(&mut contents)?; |
| 58 | + let expected = svd_parser::parse(&contents) |
| 59 | + .with_context(|| format!("parsing {}", expected_file.display()))?; |
| 60 | + |
| 61 | + if device != expected { |
| 62 | + // Include a diff of the changes in the error |
| 63 | + let config = svd_encoder::Config::default(); |
| 64 | + let dev_text = svd_encoder::encode_with_config(&device, &config)?; |
| 65 | + let expected_text = svd_encoder::encode_with_config(&expected, &config)?; |
| 66 | + let diff = similar::TextDiff::from_lines(&expected_text, &dev_text); |
| 67 | + Err(anyhow!( |
| 68 | + "patch did not produce expected results:\n{}", |
| 69 | + diff.unified_diff() |
| 70 | + )) |
| 71 | + } else { |
| 72 | + Ok(()) |
| 73 | + } |
| 74 | +} |
| 75 | + |
40 | 76 | /// Gets the absolute path of relpath from the point of view of frompath. |
41 | 77 | fn abspath(frompath: &Path, relpath: &Path) -> PathBuf { |
42 | 78 | normpath::BasePath::new(frompath) |
|
0 commit comments