1mod assertions;
2mod marked_text;
3
4pub use assertions::*;
5pub use marked_text::*;
6
7use git2;
8use std::ffi::OsStr;
9use std::path::{Path, PathBuf};
10use tempfile::TempDir;
11
12pub struct TempTree {
13 _temp_dir: TempDir,
14 path: PathBuf,
15}
16
17impl TempTree {
18 pub fn new(tree: serde_json::Value) -> Self {
19 let dir = TempDir::new().unwrap();
20 let path = std::fs::canonicalize(dir.path()).unwrap();
21 write_tree(path.as_path(), tree);
22
23 Self {
24 _temp_dir: dir,
25 path,
26 }
27 }
28
29 pub fn path(&self) -> &Path {
30 self.path.as_path()
31 }
32}
33
34fn write_tree(path: &Path, tree: serde_json::Value) {
35 use serde_json::Value;
36 use std::fs;
37
38 if let Value::Object(map) = tree {
39 for (name, contents) in map {
40 let mut path = PathBuf::from(path);
41 path.push(name);
42 match contents {
43 Value::Object(_) => {
44 fs::create_dir(&path).unwrap();
45
46 #[cfg(not(target_family = "wasm"))]
47 if path.file_name() == Some(OsStr::new(".git")) {
48 git2::Repository::init(path.parent().unwrap()).unwrap();
49 }
50
51 write_tree(&path, contents);
52 }
53 Value::Null => {
54 fs::create_dir(&path).unwrap();
55 }
56 Value::String(contents) => {
57 fs::write(&path, contents).unwrap();
58 }
59 _ => {
60 panic!("JSON object must contain only objects, strings, or null");
61 }
62 }
63 }
64 } else {
65 panic!("You must pass a JSON object to this helper")
66 }
67}
68
69pub fn sample_text(rows: usize, cols: usize, start_char: char) -> String {
70 let mut text = String::new();
71 for row in 0..rows {
72 let c: char = (start_char as u32 + row as u32) as u8 as char;
73 let mut line = c.to_string().repeat(cols);
74 if row < rows - 1 {
75 line.push('\n');
76 }
77 text += &line;
78 }
79 text
80}