Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions crates/txtx-addon-kit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ hmac = "0.12.0"
pbkdf2 = { version = "0.12.2", features = ["simple"], default-features = false }
libsecp256k1 = { version = "0.7.0" }
keccak-hash = "0.11.0"
dirs = "5.0.1"
dirs = { version = "5.0.1", optional = true }
dyn-clone = "1"

[dev-dependencies]
test-case = "3.3"
hiro-system-kit = "0.3.4"

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }

[features]
default=[]
default = ["dirs"]
wasm = []

[lib]
Expand Down
16 changes: 14 additions & 2 deletions crates/txtx-addon-kit/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
#[cfg(feature = "dirs")]
use std::env;
use std::fmt::Display;
use std::path::PathBuf;
Expand Down Expand Up @@ -323,8 +324,18 @@ impl AuthorizationContext {
let path_str = input.to_string_lossy();

let loc = if let Some(stripped) = path_str.strip_prefix("~/") {
let home = PathBuf::from(get_home_dir());
FileLocation::from_path(home.join(stripped))
#[cfg(feature = "dirs")]
{
let home = PathBuf::from(get_home_dir());
FileLocation::from_path(home.join(stripped))
}
#[cfg(not(feature = "dirs"))]
{
let _ = stripped;
return Err(
"Home directory expansion (~/) is not supported in this build".to_string(),
);
}
}
// If absolute, use as-is
else if input.is_absolute() {
Expand All @@ -349,6 +360,7 @@ impl AuthorizationContext {
/// We set out snap build to set this environment variable to the real home directory,
/// because by default, snaps run in a confined environment where the home directory is not
/// the user's actual home directory.
#[cfg(feature = "dirs")]
fn get_home_dir() -> String {
if let Ok(real_home) = env::var("SNAP_REAL_HOME") {
let path_buf = PathBuf::from(real_home);
Expand Down
9 changes: 9 additions & 0 deletions crates/txtx-addon-kit/src/types/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@ fn it_rejects_invalid_keys() {
}
}

#[cfg(feature = "dirs")]
#[test_case("~/home/path", dirs::home_dir().unwrap().join("home/path").to_str().unwrap())]
fn test_auth_context_get_path_from_str_home(path_str: &str, expected: &str) {
let auth_context = AuthorizationContext::new(FileLocation::from_path(
Path::new("/workspace/txtx.yml").to_path_buf(),
));
let result = auth_context.get_file_location_from_path_buf(&PathBuf::from(path_str)).unwrap();
assert_eq!(result.to_string(), expected);
}

#[test_case("/absolute/path", "/absolute/path")]
#[test_case("./relative/path", "/workspace/./relative/path"; "current directory")]
#[test_case("../relative/path", "/workspace/../relative/path"; "parent directory")]
Expand Down
Loading