summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorCamron Flanders <me@camronflanders.com>2024-01-06 04:46:25 -0600
committerGitHub <noreply@github.com>2024-01-06 11:46:25 +0100
commitcec111affdaf0a52f72c398f8307cf7e19c7dd8d (patch)
treed6a40dedac0b9b99cc2a0458ff7b88bbaaec1ade /src
parent0d73154002a3f8a7ee55a2578cf8caf4d7325782 (diff)
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>
Diffstat (limited to 'src')
-rwxr-xr-xsrc/configs/direnv.rs2
-rw-r--r--src/modules/direnv.rs134
2 files changed, 129 insertions, 7 deletions
diff --git a/src/configs/direnv.rs b/src/configs/direnv.rs
index 5a58d795c..5ca1e1853 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 6378cb96e..4816a7ebb 100644
--- a/src/modules/direnv.rs
+++ b/src/modules/direnv.rs
@@ -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
"#
)