|
| 1 | +//! Regenerates the SynBioDex/SBOL-Converter reference conversions under |
| 2 | +//! `tests/fixtures/cross-impl-sbolconverter/expected/` by running the pinned |
| 3 | +//! reference converter in Docker. For each SBOL 2 fixture it records the |
| 4 | +//! reference SBOL 3 output (`<stem>.sbolconverter.to-sbol3.nt`); for each |
| 5 | +//! SBOL 3 fixture it records the reference SBOL 2 output |
| 6 | +//! (`<stem>.sbolconverter.to-sbol2.rdf`). Contributors run this locally after |
| 7 | +//! a reference version bump; CI never invokes it (CI only diffs the committed |
| 8 | +//! references via the `cross_impl_reference` integration test). |
| 9 | +//! |
| 10 | +//! Usage: |
| 11 | +//! |
| 12 | +//! docker build -t sbolconverter-pinned tests/fixtures/cross-impl-sbolconverter/ |
| 13 | +//! cargo run -p sbol-convert --bin regenerate-cross-impl-reference |
| 14 | +//! |
| 15 | +//! Failure modes are fail-loud: missing Docker, image not built, or a |
| 16 | +//! non-zero exit from the container surface as an error and a non-zero exit. |
| 17 | +
|
| 18 | +use std::io::Write; |
| 19 | +use std::path::{Path, PathBuf}; |
| 20 | +use std::process::{Command, ExitCode, Stdio}; |
| 21 | + |
| 22 | +const DOCKER_IMAGE: &str = "sbolconverter-pinned"; |
| 23 | + |
| 24 | +/// SBOL 2 fixtures whose reference SBOL 3 conversion is recorded. Paths are |
| 25 | +/// relative to `tests/fixtures/sbol2/`. |
| 26 | +const SBOL2_FIXTURES: &[(&str, &str)] = &[ |
| 27 | + ( |
| 28 | + "component_definition_output", |
| 29 | + "SBOLTestSuite/SBOL2/ComponentDefinitionOutput.xml", |
| 30 | + ), |
| 31 | + ( |
| 32 | + "module_definition_output", |
| 33 | + "SBOLTestSuite/SBOL2/ModuleDefinitionOutput.xml", |
| 34 | + ), |
| 35 | + ( |
| 36 | + "sequence_constraint_output", |
| 37 | + "SBOLTestSuite/SBOL2/SequenceConstraintOutput.xml", |
| 38 | + ), |
| 39 | + ( |
| 40 | + "repression_model", |
| 41 | + "SBOLTestSuite/SBOL2/RepressionModel.xml", |
| 42 | + ), |
| 43 | +]; |
| 44 | + |
| 45 | +/// SBOL 3 fixtures whose reference SBOL 2 conversion is recorded. Paths are |
| 46 | +/// relative to `tests/fixtures/sbol3/`. |
| 47 | +const SBOL3_FIXTURES: &[(&str, &str)] = &[ |
| 48 | + ( |
| 49 | + "bba_f2620", |
| 50 | + "SBOLTestSuite/SBOL3/BBa_F2620_PoPSReceiver/BBa_F2620_PoPSReceiver.ttl", |
| 51 | + ), |
| 52 | + ( |
| 53 | + "model", |
| 54 | + "SBOLTestSuite/SBOL3/entity/model/model.ttl", |
| 55 | + ), |
| 56 | +]; |
| 57 | + |
| 58 | +fn main() -> ExitCode { |
| 59 | + if let Err(err) = check_docker_available() { |
| 60 | + eprintln!("docker is required to regenerate reference conversions: {err}"); |
| 61 | + return ExitCode::FAILURE; |
| 62 | + } |
| 63 | + if let Err(err) = check_image_present() { |
| 64 | + eprintln!( |
| 65 | + "image `{DOCKER_IMAGE}` not found: {err}\n\ |
| 66 | + build it: docker build -t {DOCKER_IMAGE} tests/fixtures/cross-impl-sbolconverter/" |
| 67 | + ); |
| 68 | + return ExitCode::FAILURE; |
| 69 | + } |
| 70 | + |
| 71 | + let expected = fixture_root().join("cross-impl-sbolconverter/expected"); |
| 72 | + if let Err(err) = std::fs::create_dir_all(&expected) { |
| 73 | + eprintln!("cannot create {}: {err}", expected.display()); |
| 74 | + return ExitCode::FAILURE; |
| 75 | + } |
| 76 | + |
| 77 | + let sbol2_root = fixture_root().join("sbol2"); |
| 78 | + for (stem, rel) in SBOL2_FIXTURES { |
| 79 | + let src = sbol2_root.join(rel); |
| 80 | + let out = expected.join(format!("{stem}.sbolconverter.to-sbol3.nt")); |
| 81 | + match run_reference(&src, "to-sbol3", "nt", &out) { |
| 82 | + Ok(()) => eprintln!("regenerated {stem}.sbolconverter.to-sbol3.nt"), |
| 83 | + Err(err) => { |
| 84 | + eprintln!("FAILED {stem} (to-sbol3): {err}"); |
| 85 | + return ExitCode::FAILURE; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + let sbol3_root = fixture_root().join("sbol3"); |
| 91 | + for (stem, rel) in SBOL3_FIXTURES { |
| 92 | + let src = sbol3_root.join(rel); |
| 93 | + if !src.exists() { |
| 94 | + eprintln!("skip {stem}: SBOL 3 fixture not present ({})", src.display()); |
| 95 | + continue; |
| 96 | + } |
| 97 | + let out = expected.join(format!("{stem}.sbolconverter.to-sbol2.rdf")); |
| 98 | + match run_reference(&src, "to-sbol2", "rdf", &out) { |
| 99 | + Ok(()) => eprintln!("regenerated {stem}.sbolconverter.to-sbol2.rdf"), |
| 100 | + Err(err) => { |
| 101 | + eprintln!("FAILED {stem} (to-sbol2): {err}"); |
| 102 | + return ExitCode::FAILURE; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + ExitCode::SUCCESS |
| 108 | +} |
| 109 | + |
| 110 | +fn run_reference(source: &Path, direction: &str, format: &str, output: &Path) -> std::io::Result<()> { |
| 111 | + let parent = source |
| 112 | + .parent() |
| 113 | + .ok_or_else(|| std::io::Error::other("source has no parent"))?; |
| 114 | + let file_name = source |
| 115 | + .file_name() |
| 116 | + .ok_or_else(|| std::io::Error::other("source has no file name"))?; |
| 117 | + let mount = format!("{}:/work:ro", parent.display()); |
| 118 | + let container_input = format!("/work/{}", Path::new(file_name).display()); |
| 119 | + |
| 120 | + let result = Command::new("docker") |
| 121 | + .args([ |
| 122 | + "run", |
| 123 | + "--rm", |
| 124 | + "-v", |
| 125 | + &mount, |
| 126 | + DOCKER_IMAGE, |
| 127 | + &container_input, |
| 128 | + direction, |
| 129 | + format, |
| 130 | + ]) |
| 131 | + .stdout(Stdio::piped()) |
| 132 | + .stderr(Stdio::inherit()) |
| 133 | + .output()?; |
| 134 | + if !result.status.success() { |
| 135 | + return Err(std::io::Error::other(format!( |
| 136 | + "reference container exited with status {:?}", |
| 137 | + result.status |
| 138 | + ))); |
| 139 | + } |
| 140 | + |
| 141 | + let mut handle = std::fs::File::create(output)?; |
| 142 | + handle.write_all(&result.stdout)?; |
| 143 | + if !result.stdout.ends_with(b"\n") { |
| 144 | + handle.write_all(b"\n")?; |
| 145 | + } |
| 146 | + Ok(()) |
| 147 | +} |
| 148 | + |
| 149 | +fn check_docker_available() -> std::io::Result<()> { |
| 150 | + let status = Command::new("docker").arg("info").output()?; |
| 151 | + if status.status.success() { |
| 152 | + Ok(()) |
| 153 | + } else { |
| 154 | + Err(std::io::Error::other("`docker info` failed")) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +fn check_image_present() -> std::io::Result<()> { |
| 159 | + let status = Command::new("docker") |
| 160 | + .args(["image", "inspect", DOCKER_IMAGE]) |
| 161 | + .output()?; |
| 162 | + if status.status.success() { |
| 163 | + Ok(()) |
| 164 | + } else { |
| 165 | + Err(std::io::Error::other("image not present")) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +fn fixture_root() -> PathBuf { |
| 170 | + let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
| 171 | + path.pop(); |
| 172 | + path.pop(); |
| 173 | + path.push("tests/fixtures"); |
| 174 | + path |
| 175 | +} |
0 commit comments