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 <davidkna@users.noreply.github.com>

* update test output

---------

Co-authored-by: David Knaack <davidkna@users.noreply.github.com>
This commit is contained in:
Camron Flanders 2024-01-06 04:46:25 -06:00 committed by GitHub
parent 0d73154002
commit cec111affd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 135 additions and 7 deletions

View File

@ -366,6 +366,7 @@
"disabled": true, "disabled": true,
"format": "[$symbol$loaded/$allowed]($style) ", "format": "[$symbol$loaded/$allowed]($style) ",
"loaded_msg": "loaded", "loaded_msg": "loaded",
"not_allowed_msg": "not allowed",
"style": "bold orange", "style": "bold orange",
"symbol": "direnv ", "symbol": "direnv ",
"unloaded_msg": "not loaded" "unloaded_msg": "not loaded"
@ -2775,6 +2776,10 @@
"default": "allowed", "default": "allowed",
"type": "string" "type": "string"
}, },
"not_allowed_msg": {
"default": "not allowed",
"type": "string"
},
"denied_msg": { "denied_msg": {
"default": "denied", "default": "denied",
"type": "string" "type": "string"

View File

@ -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_files` | `['.envrc']` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this module. | | `detect_folders` | `[]` | Which folders should trigger this module. |
| `allowed_msg` | `'allowed'` | The message displayed when an rc file is allowed. | | `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. | | `denied_msg` | `'denied'` | The message displayed when an rc file is denied. |
| `loaded_msg` | `'loaded'` | The message displayed when an rc file is loaded. | | `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. | | `unloaded_msg` | `'not loaded'` | The message displayed when an rc file is not loaded. |

View File

@ -16,6 +16,7 @@ pub struct DirenvConfig<'a> {
pub detect_files: Vec<&'a str>, pub detect_files: Vec<&'a str>,
pub detect_folders: Vec<&'a str>, pub detect_folders: Vec<&'a str>,
pub allowed_msg: &'a str, pub allowed_msg: &'a str,
pub not_allowed_msg: &'a str,
pub denied_msg: &'a str, pub denied_msg: &'a str,
pub loaded_msg: &'a str, pub loaded_msg: &'a str,
pub unloaded_msg: &'a str, pub unloaded_msg: &'a str,
@ -32,6 +33,7 @@ impl<'a> Default for DirenvConfig<'a> {
detect_files: vec![".envrc"], detect_files: vec![".envrc"],
detect_folders: vec![], detect_folders: vec![],
allowed_msg: "allowed", allowed_msg: "allowed",
not_allowed_msg: "not allowed",
denied_msg: "denied", denied_msg: "denied",
loaded_msg: "loaded", loaded_msg: "loaded",
unloaded_msg: "not loaded", unloaded_msg: "not loaded",

View File

@ -45,6 +45,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
"rc_path" => Some(Ok(state.rc_path.to_string_lossy())), "rc_path" => Some(Ok(state.rc_path.to_string_lossy())),
"allowed" => Some(Ok(match state.allowed { "allowed" => Some(Ok(match state.allowed {
AllowStatus::Allowed => Cow::from(config.allowed_msg), AllowStatus::Allowed => Cow::from(config.allowed_msg),
AllowStatus::NotAllowed => Cow::from(config.not_allowed_msg),
AllowStatus::Denied => Cow::from(config.denied_msg), AllowStatus::Denied => Cow::from(config.denied_msg),
})), })),
"loaded" => state "loaded" => state
@ -109,6 +110,7 @@ impl FromStr for DirenvState {
#[derive(Debug)] #[derive(Debug)]
enum AllowStatus { enum AllowStatus {
Allowed, Allowed,
NotAllowed,
Denied, Denied,
} }
@ -117,8 +119,9 @@ impl FromStr for AllowStatus {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
match s { match s {
"true" => Ok(Self::Allowed), "0" | "true" => Ok(Self::Allowed),
"false" => Ok(Self::Denied), "1" => Ok(Self::NotAllowed),
"2" | "false" => Ok(Self::Denied),
_ => Err(Cow::from("invalid allow status")), _ => Err(Cow::from("invalid allow status")),
} }
} }
@ -148,6 +151,34 @@ mod tests {
assert_eq!(None, renderer.collect()); assert_eq!(None, renderer.collect());
} }
#[test] #[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<()> { fn folder_with_unloaded_rc_file() -> io::Result<()> {
let dir = tempfile::tempdir()?; let dir = tempfile::tempdir()?;
let rc_path = dir.path().join(".envrc"); let rc_path = dir.path().join(".envrc");
@ -163,7 +194,7 @@ mod tests {
.cmd( .cmd(
"direnv status", "direnv status",
Some(CommandOutput { 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(), stderr: String::default(),
}), }),
); );
@ -176,6 +207,31 @@ mod tests {
dir.close() dir.close()
} }
#[test] #[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<()> { fn folder_with_loaded_rc_file() -> io::Result<()> {
let dir = tempfile::tempdir()?; let dir = tempfile::tempdir()?;
let rc_path = dir.path().join(".envrc"); let rc_path = dir.path().join(".envrc");
@ -191,7 +247,7 @@ mod tests {
.cmd( .cmd(
"direnv status", "direnv status",
Some(CommandOutput { 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(), stderr: String::default(),
}), }),
); );
@ -204,6 +260,59 @@ mod tests {
dir.close() dir.close()
} }
#[test] #[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<()> { fn folder_with_loaded_and_denied_rc_file() -> io::Result<()> {
let dir = tempfile::tempdir()?; let dir = tempfile::tempdir()?;
let rc_path = dir.path().join(".envrc"); let rc_path = dir.path().join(".envrc");
@ -219,7 +328,7 @@ mod tests {
.cmd( .cmd(
"direnv status", "direnv status",
Some(CommandOutput { 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(), stderr: String::default(),
}), }),
); );
@ -245,17 +354,28 @@ No .envrc or .env loaded
No .envrc or .env found", No .envrc or .env found",
) )
} }
fn status_cmd_output_with_rc(dir: impl AsRef<Path>, loaded: bool, allowed: bool) -> String { fn status_cmd_output_with_rc(
dir: impl AsRef<Path>,
loaded: bool,
allowed: &str,
use_legacy_boolean_flags: bool,
) -> String {
let rc_path = dir.as_ref().join(".envrc"); let rc_path = dir.as_ref().join(".envrc");
let rc_path = rc_path.to_string_lossy(); 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 { let loaded = if loaded {
format!( format!(
r#"\ r#"\
Loaded RC path {rc_path} Loaded RC path {rc_path}
Loaded watch: ".envrc" - 2023-04-30T09:51:04-04:00 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 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 Loaded RC allowPath
"# "#
) )