Skip to main content

isothrum_runner/tool_output/
az.rs

1//! What the Azure CLI says, read: `az version --output json`'s own version.
2
3/// The Azure CLI's version, the `azure-cli` entry of the JSON `az version --output json` prints;
4/// none when the answer isn't that JSON or the entry isn't text. A `--query` would need quotes,
5/// which can't pass through `cmd.exe` (`az` is a `.cmd`).
6#[must_use]
7pub fn version(json: &str) -> Option<String> {
8    let value: serde_json::Value = serde_json::from_str(json.trim()).ok()?;
9    value.get("azure-cli")?.as_str().map(str::to_owned)
10}
11
12#[cfg(test)]
13mod tests {
14    use proptest::prelude::*;
15
16    use super::*;
17
18    #[test]
19    fn the_version_is_the_azure_cli_entry_of_its_json() {
20        assert_eq!(version(include_str!("fixtures/az/2.90.0-version.json")).as_deref(), Some("2.90.0"));
21        assert_eq!(version(include_str!("fixtures/az/2.66.0-windows-version.json")).as_deref(), Some("2.66.0"));
22        assert_eq!(version("WARNING: something\n{}"), None);
23        assert_eq!(version("{\"azure-cli\": 2.9}"), None, "a version that isn't text");
24        assert_eq!(version("not json"), None);
25        assert_eq!(version(""), None);
26    }
27
28    proptest! {
29        #[test]
30        fn only_the_azure_cli_string_is_read(
31            noise in "(?s).{0,80}",
32            v in "\\PC{0,12}",
33            extra in proptest::collection::btree_map("[a-z-]{1,12}", "\\PC{0,8}", 0..4),
34            pad in "[ \n\r\t]{0,3}",
35        ) {
36            // Any text reads without a panic.
37            let _read: Option<String> = version(&noise);
38            let mut doc = serde_json::json!(extra);
39            doc["azure-cli"] = serde_json::json!(v);
40            let text = format!("{pad}{}{pad}", serde_json::to_string_pretty(&doc).unwrap());
41            prop_assert_eq!(version(&text), Some(v));
42            doc["azure-cli"] = serde_json::json!(1);
43            prop_assert_eq!(version(&doc.to_string()), None);
44        }
45    }
46}