Skip to content

Commit ad6572e

Browse files
committed
Make tests more portable to allow them to run under cargo stress
1 parent bcceb23 commit ad6572e

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

crates/common/axum_tls/src/files.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ mod tests {
114114
use assert_matches::assert_matches;
115115
use axum::routing::get;
116116
use axum::Router;
117+
use camino::Utf8PathBuf;
117118
use std::io::Cursor;
118119

119120
mod read_trust_store {
@@ -199,27 +200,28 @@ mod tests {
199200
}
200201

201202
fn copy_test_file_to(test_file: &str, path: impl AsRef<Path>) -> io::Result<u64> {
202-
std::fs::copy(format!("./test_data/{test_file}"), path)
203+
let dir = env!("CARGO_MANIFEST_DIR");
204+
std::fs::copy(format!("{dir}/test_data/{test_file}"), path)
203205
}
204206
}
205207

206208
#[test]
207209
fn load_pkey_fails_when_given_x509_certificate() {
210+
let dir = env!("CARGO_MANIFEST_DIR");
211+
let path = Utf8PathBuf::from(format!("{dir}/test_data/ec.crt"));
208212
assert_eq!(
209-
load_pkey(Utf8Path::new("./test_data/ec.crt"))
210-
.unwrap_err()
211-
.to_string(),
212-
"expected private key in \"./test_data/ec.crt\", found an X509 certificate"
213+
load_pkey(&path).unwrap_err().to_string(),
214+
format!("expected private key in {path:?}, found an X509 certificate")
213215
);
214216
}
215217

216218
#[test]
217219
fn load_pkey_fails_when_given_certificate_revocation_list() {
220+
let dir = env!("CARGO_MANIFEST_DIR");
221+
let path = Utf8PathBuf::from(format!("{dir}/test_data/demo.crl"));
218222
assert_eq!(
219-
load_pkey(Utf8Path::new("./test_data/demo.crl"))
220-
.unwrap_err()
221-
.to_string(),
222-
"expected private key in \"./test_data/demo.crl\", found a CRL"
223+
load_pkey(&path).unwrap_err().to_string(),
224+
format!("expected private key in {path:?}, found a CRL")
223225
);
224226
}
225227

@@ -288,7 +290,8 @@ mod tests {
288290
}
289291

290292
fn test_data(file_name: &str) -> String {
291-
std::fs::read_to_string(format!("./test_data/{file_name}"))
293+
let dir = env!("CARGO_MANIFEST_DIR");
294+
std::fs::read_to_string(format!("{dir}/test_data/{file_name}"))
292295
.with_context(|| format!("opening file {file_name} from test_data"))
293296
.unwrap()
294297
}

crates/core/plugin_sm/tests/plugin.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#[cfg(test)]
22
mod tests {
3-
43
use certificate::CloudRootCerts;
54
use plugin_sm::plugin::deserialize_module_info;
65
use plugin_sm::plugin::sm_path;
@@ -9,7 +8,6 @@ mod tests {
98
use std::io::Write;
109
use std::path::Path;
1110
use std::path::PathBuf;
12-
use std::str::FromStr;
1311
use tedge_api::SoftwareError;
1412
use tedge_api::SoftwareModule;
1513
use tedge_config::SudoCommandBuilder;
@@ -194,13 +192,9 @@ mod tests {
194192
}
195193

196194
fn get_dummy_plugin_path() -> PathBuf {
197-
// Return a path to a dummy plugin in target directory.
198-
let package_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
199-
200195
// To get the plugin binary path we need to find the `target` directory which is 3 levels above the `Cargo.toml` file of the package
201196
// CARGO_MANIFEST_DIR == ./thin-edge.io/crates/core/plugin_sm
202-
let dummy_plugin_path = PathBuf::from_str(package_dir.as_str())
203-
.unwrap()
197+
let dummy_plugin_path = Path::new(env!("CARGO_MANIFEST_DIR"))
204198
.parent() //./thin-edge.io/crates/core/
205199
.unwrap()
206200
.parent() // ./thin-edge.io/crates/

0 commit comments

Comments
 (0)