From cec111affdaf0a52f72c398f8307cf7e19c7dd8d Mon Sep 17 00:00:00 2001 From: Camron Flanders Date: Sat, 6 Jan 2024 04:46:25 -0600 Subject: [PATCH] fix(direnv): update to work with direnv v2.33 (#5657) * update AllowStatus to work with direnv 2.33 direnv now returns int enum instead of boolean, https://github.com/direnv/direnv/pull/1158 * update schema * maybe fixed the schema now * Whoops, I inverted the flags somehow * have coffee, fix mistaken understanding * undo changes to tranlations * Update docs/config/README.md * Update src/modules/direnv.rs Co-authored-by: David Knaack * update test output --------- Co-authored-by: David Knaack --- .github/config-schema.json | 5 ++ docs/config/README.md | 1 + src/configs/direnv.rs | 2 + src/modules/direnv.rs | 134 +++++++++++++++++++++++++++++++++++-- 4 files changed, 135 insertions(+), 7 deletions(-) diff --git a/.github/config-schema.json b/.github/config-schema.json index 914693c3..e2bade18 100644 --- a/.github/config-schema.json +++ b/.github/config-schema.json @@ -366,6 +366,7 @@ "disabled": true, "format": "[$symbol$loaded/$allowed]($style) ", "loaded_msg": "loaded", + "not_allowed_msg": "not allowed", "style": "bold orange", "symbol": "direnv ", "unloaded_msg": "not loaded" @@ -2775,6 +2776,10 @@ "default": "allowed", "type": "string" }, + "not_allowed_msg": { + "default": "not allowed", + "type": "string" + }, "denied_msg": { "default": "denied", "type": "string" diff --git a/docs/config/README.md b/docs/config/README.md index 046ad01d..786ab81c 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1225,6 +1225,7 @@ The `direnv` module shows the status of the current rc file if one is present. T | `detect_files` | `['.envrc']` | Which filenames should trigger this module. | | `detect_folders` | `[]` | Which folders should trigger this module. | | `allowed_msg` | `'allowed'` | The message displayed when an rc file is allowed. | +| `not_allowed_msg` | `'not allowed'` | The message displayed when an rc file is not_allowed. | | `denied_msg` | `'denied'` | The message displayed when an rc file is denied. | | `loaded_msg` | `'loaded'` | The message displayed when an rc file is loaded. | | `unloaded_msg` | `'not loaded'` | The message displayed when an rc file is not loaded. | diff --git a/src/configs/direnv.rs b/src/configs/direnv.rs index 5a58d795..5ca1e185 100755 --- a/src/configs/direnv.rs +++ b/src/configs/direnv.rs @@ -16,6 +16,7 @@ pub struct DirenvConfig<'a> { pub detect_files: Vec<&'a str>, pub detect_folders: Vec<&'a str>, pub allowed_msg: &'a str, + pub not_allowed_msg: &'a str, pub denied_msg: &'a str, pub loaded_msg: &'a str, pub unloaded_msg: &'a str, @@ -32,6 +33,7 @@ impl<'a> Default for DirenvConfig<'a> { detect_files: vec![".envrc"], detect_folders: vec![], allowed_msg: "allowed", + not_allowed_msg: "not allowed", denied_msg: "denied", loaded_msg: "loaded", unloaded_msg: "not loaded", diff --git a/src/modules/direnv.rs b/src/modules/direnv.rs index 6378cb96..4816a7eb 100644 --- a/src/modules/direnv.rs +++ b/src/modules/direnv.rs @@ -45,6 +45,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { "rc_path" => Some(Ok(state.rc_path.to_string_lossy())), "allowed" => Some(Ok(match state.allowed { AllowStatus::Allowed => Cow::from(config.allowed_msg), + AllowStatus::NotAllowed => Cow::from(config.not_allowed_msg), AllowStatus::Denied => Cow::from(config.denied_msg), })), "loaded" => state @@ -109,6 +110,7 @@ impl FromStr for DirenvState { #[derive(Debug)] enum AllowStatus { Allowed, + NotAllowed, Denied, } @@ -117,8 +119,9 @@ impl FromStr for AllowStatus { fn from_str(s: &str) -> Result { match s { - "true" => Ok(Self::Allowed), - "false" => Ok(Self::Denied), + "0" | "true" => Ok(Self::Allowed), + "1" => Ok(Self::NotAllowed), + "2" | "false" => Ok(Self::Denied), _ => Err(Cow::from("invalid allow status")), } } @@ -148,6 +151,34 @@ mod tests { assert_eq!(None, renderer.collect()); } #[test] + fn folder_with_unloaded_rc_file_pre_2_33() -> io::Result<()> { + let dir = tempfile::tempdir()?; + let rc_path = dir.path().join(".envrc"); + + std::fs::File::create(&rc_path)?.sync_all()?; + + let renderer = ModuleRenderer::new("direnv") + .config(toml::toml! { + [direnv] + disabled = false + }) + .path(dir.path()) + .cmd( + "direnv status", + Some(CommandOutput { + stdout: status_cmd_output_with_rc(dir.path(), false, "0", true), + stderr: String::default(), + }), + ); + + assert_eq!( + Some(format!("direnv not loaded/allowed ")), + renderer.collect() + ); + + dir.close() + } + #[test] fn folder_with_unloaded_rc_file() -> io::Result<()> { let dir = tempfile::tempdir()?; let rc_path = dir.path().join(".envrc"); @@ -163,7 +194,7 @@ mod tests { .cmd( "direnv status", Some(CommandOutput { - stdout: status_cmd_output_with_rc(dir.path(), false, true), + stdout: status_cmd_output_with_rc(dir.path(), false, "0", false), stderr: String::default(), }), ); @@ -176,6 +207,31 @@ mod tests { dir.close() } #[test] + fn folder_with_loaded_rc_file_pre_2_33() -> io::Result<()> { + let dir = tempfile::tempdir()?; + let rc_path = dir.path().join(".envrc"); + + std::fs::File::create(&rc_path)?.sync_all()?; + + let renderer = ModuleRenderer::new("direnv") + .config(toml::toml! { + [direnv] + disabled = false + }) + .path(dir.path()) + .cmd( + "direnv status", + Some(CommandOutput { + stdout: status_cmd_output_with_rc(dir.path(), true, "0", true), + stderr: String::default(), + }), + ); + + assert_eq!(Some(format!("direnv loaded/allowed ")), renderer.collect()); + + dir.close() + } + #[test] fn folder_with_loaded_rc_file() -> io::Result<()> { let dir = tempfile::tempdir()?; let rc_path = dir.path().join(".envrc"); @@ -191,7 +247,7 @@ mod tests { .cmd( "direnv status", Some(CommandOutput { - stdout: status_cmd_output_with_rc(dir.path(), true, true), + stdout: status_cmd_output_with_rc(dir.path(), true, "0", false), stderr: String::default(), }), ); @@ -204,6 +260,59 @@ mod tests { dir.close() } #[test] + fn folder_with_loaded_and_denied_rc_file_pre_2_33() -> io::Result<()> { + let dir = tempfile::tempdir()?; + let rc_path = dir.path().join(".envrc"); + + std::fs::File::create(&rc_path)?.sync_all()?; + + let renderer = ModuleRenderer::new("direnv") + .config(toml::toml! { + [direnv] + disabled = false + }) + .path(dir.path()) + .cmd( + "direnv status", + Some(CommandOutput { + stdout: status_cmd_output_with_rc(dir.path(), true, "2", true), + stderr: String::default(), + }), + ); + + assert_eq!(Some(format!("direnv loaded/denied ")), renderer.collect()); + + dir.close() + } + #[test] + fn folder_with_loaded_and_not_allowed_rc_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + let rc_path = dir.path().join(".envrc"); + + std::fs::File::create(&rc_path)?.sync_all()?; + + let renderer = ModuleRenderer::new("direnv") + .config(toml::toml! { + [direnv] + disabled = false + }) + .path(dir.path()) + .cmd( + "direnv status", + Some(CommandOutput { + stdout: status_cmd_output_with_rc(dir.path(), true, "1", false), + stderr: String::default(), + }), + ); + + assert_eq!( + Some(format!("direnv loaded/not allowed ")), + renderer.collect() + ); + + dir.close() + } + #[test] fn folder_with_loaded_and_denied_rc_file() -> io::Result<()> { let dir = tempfile::tempdir()?; let rc_path = dir.path().join(".envrc"); @@ -219,7 +328,7 @@ mod tests { .cmd( "direnv status", Some(CommandOutput { - stdout: status_cmd_output_with_rc(dir.path(), true, false), + stdout: status_cmd_output_with_rc(dir.path(), true, "2", false), stderr: String::default(), }), ); @@ -245,17 +354,28 @@ No .envrc or .env loaded No .envrc or .env found", ) } - fn status_cmd_output_with_rc(dir: impl AsRef, loaded: bool, allowed: bool) -> String { + fn status_cmd_output_with_rc( + dir: impl AsRef, + loaded: bool, + allowed: &str, + use_legacy_boolean_flags: bool, + ) -> String { let rc_path = dir.as_ref().join(".envrc"); let rc_path = rc_path.to_string_lossy(); + let allowed_value = match (use_legacy_boolean_flags, allowed) { + (true, "0") => "true", + (true, ..) => "false", + (false, val) => val, + }; + let loaded = if loaded { format!( r#"\ Loaded RC path {rc_path} Loaded watch: ".envrc" - 2023-04-30T09:51:04-04:00 Loaded watch: "../.local/share/direnv/allow/abcd" - 2023-04-30T09:52:58-04:00 - Loaded RC allowed false + Loaded RC allowed {allowed_value} Loaded RC allowPath "# )