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