Debian Codegraph @

subplot/sq-subplot.rs

raw
1 // Rust support for running sq-subplot.md scenarios.
2
3 use subplotlib::file::SubplotDataFile;
4 use subplotlib::steplibrary::datadir::Datadir;
5 use subplotlib::steplibrary::runcmd::Runcmd;
6
7 use std::collections::HashMap;
8 use std::path::{Path, PathBuf};
9
10 #[step]
11 #[context(Runcmd)]
12 #[context(Datadir)]
13 fn install_sq(context: &ScenarioContext) {
14 // The SQ_DIR variable can be set to test an installed sq rather
15 // than the one built from the source tree.
16 if let Some(bindir) = std::env::var_os("SQ_DIR") {
17 println!("Found SQ_DIR environment variable, using that");
18 context.with_mut(
19 |rc: &mut Runcmd| {
20 rc.prepend_to_path(bindir);
21 Ok(())
22 },
23 false,
24 )?;
25 } else {
26 let target_exe = env!("CARGO_BIN_EXE_sq");
27 let target_path = Path::new(target_exe);
28 let target_path = target_path.parent().ok_or("No parent?")?;
29
30 context.with_mut(
31 |context: &mut Runcmd| {
32 context.prepend_to_path(target_path);
33 Ok(())
34 },
35 false,
36 )?;
37 }
38
39 // Create a state directory and set SEQUOIA_HOME so that sq will
40 // use it.
41 let home = PathBuf::from(".sequoia-home");
42 let mut absolute_home = Default::default();
43 context.with_mut(
44 |datadir: &mut Datadir| {
45 datadir.create_dir_all(&home)?;
46 absolute_home = datadir.base_path().join(home);
47 Ok(())
48 }, false)?;
49 context.with_mut(
50 |runcmd: &mut Runcmd| {
51 runcmd.setenv("SEQUOIA_HOME", absolute_home);
52 Ok(())
53 }, false)?;
54 }
55
56 /// Remember values between steps.
57 #[derive(Default, Debug, Clone)]
58 struct Memory {
59 map: HashMap<String, String>,
60 }
61
62 impl ContextElement for Memory {
63 fn scenario_starts(&mut self) -> StepResult {
64 self.map.clear();
65 Ok(())
66 }
67 }
68
69 impl Memory {
70 /// Remember a key, value pair.
71 pub fn remember(&mut self, key: &str, value: &str) {
72 eprintln!("remember {}={:?}", key, value);
73 self.map.insert(key.into(), value.into());
74 }
75
76 /// Retrieve the value for a key. Panics if key hasn't been set.
77 pub fn get(&self, key: &str) -> &str {
78 eprintln!("recall {}: {:?}", key, self.map.get(key));
79 self.map.get(key).unwrap()
80 }
81 }
82
83 #[step]
84 #[context(Memory)]
85 #[context(Runcmd)]
86 fn remember_fingerprint_in_variable(context: &ScenarioContext, name: &str) {
87 let stdout = context.with(|runcmd: &Runcmd| Ok(runcmd.stdout_as_string()), false)?;
88 const PAT: &str = "Fingerprint: ";
89 if let Some(i) = stdout.find(PAT) {
90 let s = &stdout[i + PAT.len()..];
91 if let Some(j) = s.find('\n') {
92 let fpr = &s[..j];
93 context.with_mut(|memory: &mut Memory| Ok(memory.remember(name, fpr)), false)?;
94 } else {
95 panic!("stdout didn't include newline after {:?}", PAT);
96 }
97 } else {
98 panic!("STDOUT didn't include {:?}", PAT);
99 }
100 }
101
102 #[cfg(all(unix, not(unix)))] // Bottom.
103 #[step]
104 #[context(Memory)]
105 #[context(Runcmd)]
106 fn stdout_matches_json_template(context: &ScenarioContext, file: SubplotDataFile) {
107 let memory = context.with(|memory: &Memory| Ok(memory.clone()), false)?;
108 let template = String::from_utf8_lossy(file.data());
109 let wanted = expand_from_memory(&template, &memory);
110 eprintln!("parsing JSON");
111 let wanted: serde_json::Value = serde_json::from_str(&wanted)?;
112 eprintln!("matches JSON template: wanted: {:#?}", wanted);
113
114 let stdout = context.with(|runcmd: &Runcmd| Ok(runcmd.stdout_as_string()), false)?;
115 let actual: serde_json::Value = serde_json::from_str(&stdout)?;
116 eprintln!("matches JSON template: actual: {:#?}", actual);
117
118 assert_eq!(actual, wanted);
119 }
120
121 #[allow(unused)]
122 fn expand_from_memory(mut s: &str, memory: &Memory) -> String {
123 let mut result = String::new();
124 while !s.is_empty() {
125 let before = s;
126 if let Some(i) = s.find("${") {
127 result.push_str(&s[..i]);
128 s = &s[i..];
129 if let Some(j) = s.find("}") {
130 let name = &s[2..j];
131 result.push_str(memory.get(name));
132 s = &s[j+1..];
133 } else {
134 result.push_str(&s[..2]);
135 s = &s[2..];
136 }
137 } else {
138 result.push_str(s);
139 s = "";
140 }
141 assert!(s != before);
142 }
143 result
144 }