Skip to content

Commit f19a40c

Browse files
camino conversion
1 parent ff7e5a0 commit f19a40c

File tree

14 files changed

+117
-68
lines changed

14 files changed

+117
-68
lines changed

Cargo.lock

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/icp-cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ tokio.workspace = true
3636
assert_cmd = "2"
3737
predicates = "3"
3838
reqwest = { workspace = true }
39-
tempfile = "3"
39+
camino-tempfile = "1"
4040

4141
[lints]
4242
workspace = true

bin/icp-cli/src/env.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ pub struct Env {
1212
}
1313

1414
impl Env {
15-
pub fn new(output_format: OutputFormat, identity: Option<String>) -> Self {
15+
pub fn new(output_format: OutputFormat, dirs: IcpCliDirs, identity: Option<String>) -> Self {
1616
Self {
1717
output_format,
18-
dirs: IcpCliDirs::new(),
18+
dirs,
1919
identity,
2020
}
2121
}

bin/icp-cli/src/main.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use clap::{Parser, ValueEnum};
22
use commands::{Cmd, DispatchError};
33
use env::Env;
4-
use snafu::report;
4+
use icp_dirs::{DiscoverDirsError, IcpCliDirs};
5+
use snafu::{Snafu, report};
56

67
mod commands;
78
mod env;
@@ -26,13 +27,22 @@ enum OutputFormat {
2627

2728
#[tokio::main]
2829
#[report]
29-
async fn main() -> Result<(), DispatchError> {
30+
async fn main() -> Result<(), ProgramError> {
3031
let cli = Cli::parse();
31-
let env = Env::new(cli.output_format, cli.identity);
32+
let dirs = IcpCliDirs::new()?;
33+
let env = Env::new(cli.output_format, dirs, cli.identity);
3234
commands::dispatch(&env, cli.command).await?;
3335
Ok(())
3436
}
3537

38+
#[derive(Debug, Snafu)]
39+
pub enum ProgramError {
40+
#[snafu(transparent)]
41+
Dispatch { source: DispatchError },
42+
#[snafu(transparent)]
43+
Dirs { source: DiscoverDirsError },
44+
}
45+
3646
#[cfg(test)]
3747
mod tests {
3848
use clap::CommandFactory;

bin/icp-cli/tests/common/test_env.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
use crate::common::os::PATH_SEPARATOR;
22
use assert_cmd::Command;
3+
use camino::{Utf8Path, Utf8PathBuf};
4+
use camino_tempfile::Utf8TempDir;
35
use std::env;
46
use std::ffi::OsString;
57
use std::fs;
68
use std::fs::create_dir_all;
7-
use std::path::{Path, PathBuf};
8-
use tempfile::TempDir;
99

1010
pub struct TestEnv {
11-
home_dir: TempDir,
12-
bin_dir: PathBuf,
13-
asset_dir: PathBuf,
14-
dfx_path: Option<PathBuf>,
11+
home_dir: Utf8TempDir,
12+
bin_dir: Utf8PathBuf,
13+
asset_dir: Utf8PathBuf,
14+
dfx_path: Option<Utf8PathBuf>,
1515
os_path: OsString,
1616
}
1717

1818
impl TestEnv {
1919
pub fn new() -> Self {
20-
let home_dir = tempfile::tempdir().expect("failed to create temp home dir");
20+
let home_dir = camino_tempfile::tempdir().expect("failed to create temp home dir");
2121
let bin_dir = home_dir.path().join("bin");
2222
let asset_dir = home_dir.path().join("share");
2323
fs::create_dir(&bin_dir).expect("failed to create bin dir");
2424
fs::create_dir(&asset_dir).expect("failed to create asset dir");
2525
let os_path = Self::build_os_path(&bin_dir);
26-
eprintln!(
27-
"Test environment home directory: {}",
28-
home_dir.path().display()
29-
);
26+
eprintln!("Test environment home directory: {}", home_dir.path());
3027

3128
Self {
3229
home_dir,
@@ -39,13 +36,12 @@ impl TestEnv {
3936

4037
/// Sets up `dfx` in the test environment by copying the binary from $ICPTEST_DFX_PATH
4138
pub fn with_dfx(mut self) -> Self {
42-
let dfx_path = std::env::var_os("ICPTEST_DFX_PATH")
39+
let dfx_path = std::env::var("ICPTEST_DFX_PATH")
4340
.expect("ICPTEST_DFX_PATH must be set to use with_dfx()");
44-
let src = PathBuf::from(dfx_path);
41+
let src = Utf8PathBuf::from(dfx_path);
4542
assert!(
4643
src.exists(),
47-
"ICPTEST_DFX_PATH points to non-existent file: {}",
48-
src.display()
44+
"ICPTEST_DFX_PATH points to non-existent file: {src}",
4945
);
5046

5147
let dest = self.bin_dir.join("dfx");
@@ -55,7 +51,7 @@ impl TestEnv {
5551
{
5652
use std::os::unix::fs::PermissionsExt;
5753
let mut perms = fs::metadata(&dest)
58-
.unwrap_or_else(|e| panic!("Failed to read metadata for {}: {}", dest.display(), e))
54+
.unwrap_or_else(|e| panic!("Failed to read metadata for {dest}: {e}"))
5955
.permissions();
6056
perms.set_mode(0o500);
6157
fs::set_permissions(&dest, perms).unwrap();
@@ -65,7 +61,7 @@ impl TestEnv {
6561
self
6662
}
6763

68-
pub fn home_path(&self) -> &Path {
64+
pub fn home_path(&self) -> &Utf8Path {
6965
self.home_dir.path()
7066
}
7167

@@ -93,11 +89,11 @@ impl TestEnv {
9389
cmd.env_remove("ICP_HOME");
9490
}
9591

96-
fn build_os_path(bin_dir: &Path) -> OsString {
92+
fn build_os_path(bin_dir: &Utf8Path) -> OsString {
9793
let old_path = env::var_os("PATH").unwrap_or_default();
98-
let mut new_path = bin_dir.as_os_str().to_os_string();
94+
let mut new_path = bin_dir.as_os_str().to_owned();
9995
new_path.push(PATH_SEPARATOR);
100-
new_path.push(old_path);
96+
new_path.push(&old_path);
10197
new_path
10298
}
10399

@@ -111,11 +107,11 @@ impl TestEnv {
111107
fs::write(&networks_json_path, networks).unwrap();
112108
}
113109

114-
pub fn pkg_dir(&self) -> PathBuf {
110+
pub fn pkg_dir(&self) -> Utf8PathBuf {
115111
env!("CARGO_MANIFEST_DIR").into()
116112
}
117113

118-
pub fn make_asset(&self, name: &str) -> PathBuf {
114+
pub fn make_asset(&self, name: &str) -> Utf8PathBuf {
119115
let target = self.asset_dir.join(name);
120116
fs::copy(self.pkg_dir().join(format!("tests/assets/{name}")), &target)
121117
.expect("failed to copy asset");

bin/icp-cli/tests/identity_tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use core::str;
22
use std::io::Write;
33

4+
use camino_tempfile::NamedUtf8TempFile;
45
use common::TestEnv;
56
use ic_agent::export::Principal;
67
use predicates::{
78
ord::eq,
89
str::{PredicateStrExt, contains},
910
};
10-
use tempfile::NamedTempFile;
1111

1212
mod common;
1313

@@ -28,7 +28,7 @@ fn identity_anonymous() {
2828

2929
#[test]
3030
fn identity_import_seed() {
31-
let mut file = NamedTempFile::new().unwrap();
31+
let mut file = NamedUtf8TempFile::new().unwrap();
3232
file.write_all(b"equip will roof matter pink blind book anxiety banner elbow sun young")
3333
.unwrap();
3434
let path = file.into_temp_path();
@@ -78,7 +78,7 @@ fn identity_import_pem() {
7878
.stderr(contains("missing field `parameters`"));
7979

8080
// from encrypted
81-
let mut file = NamedTempFile::new().unwrap();
81+
let mut file = NamedUtf8TempFile::new().unwrap();
8282
file.write_all(b"swordfish").unwrap();
8383
let path = file.into_temp_path();
8484
env.icp()
@@ -118,7 +118,7 @@ fn identity_create() {
118118
.args(["identity", "principal", "--identity", "alice"])
119119
.assert()
120120
.success();
121-
let mut file = NamedTempFile::new().unwrap();
121+
let mut file = NamedUtf8TempFile::new().unwrap();
122122
file.write_all(seed.trim().as_bytes()).unwrap();
123123
let path = file.into_temp_path();
124124
env.icp()

lib/icp-dirs/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7+
camino.workspace = true
78
directories.workspace = true
9+
snafu.workspace = true
810

911
[lints]
1012
workspace = true

lib/icp-dirs/src/lib.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,56 @@
1-
use std::path::PathBuf;
2-
1+
use camino::Utf8PathBuf;
32
use directories::ProjectDirs;
3+
use snafu::{OptionExt, ResultExt, Snafu};
44

55
#[derive(Debug, Clone)]
66
pub enum IcpCliDirs {
7-
Standard(ProjectDirs),
8-
Overridden(PathBuf),
7+
Standard(Utf8ProjectDirs),
8+
Overridden(Utf8PathBuf),
99
}
1010

1111
impl IcpCliDirs {
12-
pub fn new() -> Self {
13-
if let Some(override_var) = std::env::var_os("ICP_HOME") {
14-
Self::Overridden(override_var.into())
12+
pub fn new() -> Result<Self, DiscoverDirsError> {
13+
if let Ok(override_var) = std::env::var("ICP_HOME") {
14+
Ok(Self::Overridden(override_var.into()))
1515
} else {
16-
Self::Standard(ProjectDirs::from("org.dfinity", "", "icp-cli").unwrap())
16+
Ok(Self::Standard(
17+
Utf8ProjectDirs::from_dirs(
18+
ProjectDirs::from("org.dfinity", "", "icp-cli").context(CannotFindHomeSnafu)?,
19+
)
20+
.context(NonUtf8Snafu)?,
21+
))
1722
}
1823
}
1924

20-
pub fn identity_dir(&self) -> PathBuf {
25+
pub fn identity_dir(&self) -> Utf8PathBuf {
2126
match self {
22-
Self::Standard(dirs) => dirs.data_dir().join("identity"),
27+
Self::Standard(dirs) => dirs.data_dir.join("identity"),
2328
Self::Overridden(path) => path.join("identity"),
2429
}
2530
}
2631
}
2732

28-
impl Default for IcpCliDirs {
29-
fn default() -> Self {
30-
Self::new()
33+
#[derive(Debug, Snafu)]
34+
pub enum DiscoverDirsError {
35+
#[snafu(display("user directories are non-UTF-8"))]
36+
NonUtf8 { source: camino::FromPathBufError },
37+
#[snafu(display("home directory could not be located"))]
38+
CannotFindHome,
39+
}
40+
41+
#[derive(Debug, Clone)]
42+
pub struct Utf8ProjectDirs {
43+
pub data_dir: Utf8PathBuf,
44+
pub config_dir: Utf8PathBuf,
45+
pub cache_dir: Utf8PathBuf,
46+
}
47+
48+
impl Utf8ProjectDirs {
49+
pub fn from_dirs(dirs: ProjectDirs) -> Result<Self, camino::FromPathBufError> {
50+
Ok(Self {
51+
data_dir: dirs.data_dir().to_owned().try_into()?,
52+
config_dir: dirs.config_dir().to_owned().try_into()?,
53+
cache_dir: dirs.cache_dir().to_owned().try_into()?,
54+
})
3155
}
3256
}

lib/icp-fs/src/fs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ pub struct CreateDirAllError {
88
pub source: std::io::Error,
99
}
1010

11-
pub fn create_dir_all(path: &Path) -> Result<(), CreateDirAllError> {
11+
pub fn create_dir_all(path: impl AsRef<Path>) -> Result<(), CreateDirAllError> {
12+
let path = path.as_ref();
1213
std::fs::create_dir_all(path).context(CreateDirAllSnafu { path })
1314
}
1415

lib/icp-identity/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7+
camino.workspace = true
78
directories.workspace = true
89
ed25519-consensus.workspace = true
910
elliptic-curve.workspace = true

0 commit comments

Comments
 (0)