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