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,
"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"

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_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. |

View file

@ -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",

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())),
"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<Self, Self::Err> {
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<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 = 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
"#
)