// SPDX-License-Identifier: Apache-2.0 // Copyright Scienting // Source: https://git.scient.ing/education/coursebank //! Fixtures shared by the 2.1 integration tests: the examples in //! `docs/design/2.1/examples/`, copied into a scratch directory so a test may //! change them. #![allow(dead_code)] use std::path::{Path, PathBuf}; /// `docs/design/2.1/`. pub fn design() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("docs/design/2.1") } /// A fresh, empty directory under the system temp dir. pub fn scratch(tag: &str) -> PathBuf { let dir = std::env::temp_dir().join(format!("coursebank-2-1-{tag}-{}", std::process::id())); let _ = std::fs::remove_dir_all(&dir); std::fs::create_dir_all(&dir).unwrap(); dir } /// Copies a directory tree. pub fn copy_tree(from: &Path, to: &Path) { std::fs::create_dir_all(to).unwrap(); for entry in std::fs::read_dir(from).unwrap() { let path = entry.unwrap().path(); let target = to.join(path.file_name().unwrap()); if path.is_dir() { copy_tree(&path, &target); } else { std::fs::copy(&path, &target).unwrap(); } } } /// The 2.1 example course, copied so a test may change it. pub fn v21_course(tag: &str) -> PathBuf { let root = scratch(tag); copy_tree(&design().join("examples/v21"), &root); let _ = std::fs::remove_file(root.join("lecture-page-preview.md")); root } /// Replaces the first occurrence of `from` with `to` in a file of a course. pub fn edit(root: &Path, relative: &str, from: &str, to: &str) { let path = root.join(relative); let text = std::fs::read_to_string(&path).unwrap(); assert!(text.contains(from), "{relative} does not contain {from:?}"); std::fs::write(&path, text.replacen(from, to, 1)).unwrap(); }