diff options
author | Alex Solomes <alex.solomes@softwareag.com> | 2022-06-09 17:09:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-09 17:09:06 +0100 |
commit | c95fe8e202f470e79123bf4c992f779df249ec42 (patch) | |
tree | 848540c0275a70b143440edfdacea0533e28d8ac /plugins | |
parent | b91cc0205399fef3b73e4a41e56b02fd8cfc7d87 (diff) |
Testing utility: TempTedgeDir POC (#1148)
* temp tedge dir poc
Signed-off-by: initard <solo@softwareag.com>
* renaming crate and removing TedgeChildTempDir
- renamed the crate to tedge_test_utils
- removed TedgeChildTempDir as TempTedgeDir already had the same
functions
Signed-off-by: Alex Solomes <alex.solomes@softwareag.com>
* changing agent.rs tests to use TempTedgeDir
- changing agent.rs to use TempTedgeDir
- un-ignoring a test to check if agent restart creates a file in the
right place
Signed-off-by: Alex Solomes <alex.solomes@softwareag.com>
* changing tedge_mapper tests to use TempTedgeDir
Signed-off-by: Alex Solomes <alex.solomes@softwareag.com>
* changing tedge_config tests to use TempTedgeDir
Signed-off-by: Alex Solomes <alex.solomes@softwareag.com>
* changing c8y_configuration_plugin tests to use TempTedgeDir
Signed-off-by: Alex Solomes <alex.solomes@softwareag.com>
* changing tedge_apama_plugin tests to use TempTedgeDir
Signed-off-by: Alex Solomes <alex.solomes@softwareag.com>
* changing logged_command tests to use TempTedgeDir
Signed-off-by: Alex Solomes <alex.solomes@softwareag.com>
* adding another method to TempTedgeDir
Signed-off-by: initard <solo@softwareag.com>
Co-authored-by: initard <solo@softwareag.com>
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/c8y_configuration_plugin/Cargo.toml | 2 | ||||
-rw-r--r-- | plugins/c8y_configuration_plugin/src/config.rs | 19 | ||||
-rw-r--r-- | plugins/c8y_configuration_plugin/src/main.rs | 3 | ||||
-rw-r--r-- | plugins/tedge_apama_plugin/Cargo.toml | 2 | ||||
-rw-r--r-- | plugins/tedge_apama_plugin/src/main.rs | 39 |
5 files changed, 24 insertions, 41 deletions
diff --git a/plugins/c8y_configuration_plugin/Cargo.toml b/plugins/c8y_configuration_plugin/Cargo.toml index 20a27927..171a73e3 100644 --- a/plugins/c8y_configuration_plugin/Cargo.toml +++ b/plugins/c8y_configuration_plugin/Cargo.toml @@ -37,6 +37,6 @@ mockall = "0.11" mockito = "0.31" mqtt_tests = { path = "../../crates/tests/mqtt_tests" } serial_test = "0.6" -tempfile = "3.3" +tedge_test_utils = { path = "../../crates/tests/tedge_test_utils" } test-case = "2.0" toml = "0.5" diff --git a/plugins/c8y_configuration_plugin/src/config.rs b/plugins/c8y_configuration_plugin/src/config.rs index 892d157a..d738bda2 100644 --- a/plugins/c8y_configuration_plugin/src/config.rs +++ b/plugins/c8y_configuration_plugin/src/config.rs @@ -182,9 +182,7 @@ impl PluginConfig { #[cfg(test)] mod tests { use super::*; - use std::io::Write; - use std::path::PathBuf; - use tempfile::TempDir; + use tedge_test_utils::fs::TempTedgeDir; use test_case::test_case; const PLUGIN_CONFIG_FILE: &str = "c8y-configuration-plugin.toml"; @@ -397,8 +395,8 @@ mod tests { file_content: &str, expected_config: PluginConfig, ) -> anyhow::Result<()> { - let (_dir, config_root_path) = create_temp_plugin_config(file_content)?; - let tmp_path_to_plugin_config = config_root_path.join(PLUGIN_CONFIG_FILE); + let dir = create_temp_plugin_config(file_content)?; + let tmp_path_to_plugin_config = dir.path().join(PLUGIN_CONFIG_FILE); let tmp_path_to_plugin_config_str = tmp_path_to_plugin_config.as_path().display().to_string(); @@ -470,12 +468,9 @@ mod tests { } // Need to return TempDir, otherwise the dir will be deleted when this function ends. - fn create_temp_plugin_config(content: &str) -> std::io::Result<(TempDir, PathBuf)> { - let temp_dir = TempDir::new()?; - let config_root = temp_dir.path().to_path_buf(); - let config_file_path = config_root.join(PLUGIN_CONFIG_FILE); - let mut file = fs::File::create(config_file_path.as_path())?; - file.write_all(content.as_bytes())?; - Ok((temp_dir, config_root)) + fn create_temp_plugin_config(content: &str) -> std::io::Result<TempTedgeDir> { + let temp_dir = TempTedgeDir::new(); + temp_dir.file(PLUGIN_CONFIG_FILE).with_raw_content(content); + Ok(temp_dir) } } diff --git a/plugins/c8y_configuration_plugin/src/main.rs b/plugins/c8y_configuration_plugin/src/main.rs index 6f1d7592..eb4b8272 100644 --- a/plugins/c8y_configuration_plugin/src/main.rs +++ b/plugins/c8y_configuration_plugin/src/main.rs @@ -250,6 +250,7 @@ mod tests { use c8y_api::http_proxy::MockC8YHttpProxy; use mockall::predicate; use std::{path::Path, time::Duration}; + use tedge_test_utils::fs::TempTedgeDir; const TEST_TIMEOUT_MS: Duration = Duration::from_millis(5000); @@ -272,7 +273,7 @@ mod tests { ) .return_once(|_path, _type| Ok("http://server/some/test/config/url".to_string())); - let tmp_dir = tempfile::tempdir()?; + let tmp_dir = TempTedgeDir::new(); // Run the plugin's runtime logic in an async task tokio::spawn(async move { diff --git a/plugins/tedge_apama_plugin/Cargo.toml b/plugins/tedge_apama_plugin/Cargo.toml index 6fbaa306..26f77498 100644 --- a/plugins/tedge_apama_plugin/Cargo.toml +++ b/plugins/tedge_apama_plugin/Cargo.toml @@ -19,4 +19,4 @@ zip = { version = "0.5", default-features = false, features = ["deflate"] } roxmltree = "0.14" [dev-dependencies] -tempfile = "3.2" +tedge_test_utils = { path = "../../crates/tests/tedge_test_utils" } diff --git a/plugins/tedge_apama_plugin/src/main.rs b/plugins/tedge_apama_plugin/src/main.rs index 7ec18a8d..6f3ae40c 100644 --- a/plugins/tedge_apama_plugin/src/main.rs +++ b/plugins/tedge_apama_plugin/src/main.rs @@ -326,51 +326,38 @@ fn main() { #[cfg(test)] mod tests { - use std::fs::{self, File}; - use tempfile::TempDir; + use tedge_test_utils::fs::TempTedgeDir; use crate::get_project_name; #[test] fn get_project_name_project_descriptor_xml() { - let temp_dir = TempDir::new().unwrap(); - let project_dir_path = temp_dir.path(); - let project_descriptor_path = project_dir_path.join(".project"); - - fs::write( - project_descriptor_path.as_path(), - r#"<projectDescription><name>quickstart-project</name></projectDescription>"#, - ) - .expect("Failed to create project descriptor xml file"); - - assert_eq!(get_project_name(project_dir_path), "quickstart-project"); + let temp_dir = TempTedgeDir::new(); + let content = r#"<projectDescription><name>quickstart-project</name></projectDescription>"#; + temp_dir.file(".project").with_raw_content(content); + assert_eq!(get_project_name(temp_dir.path()), "quickstart-project"); } #[test] fn get_project_name_empty_project() { - let temp_dir = TempDir::new().unwrap(); - let project_dir_path = temp_dir.path(); - let project_descriptor_path = project_dir_path.join(".project"); - File::create(project_descriptor_path.as_path()) - .expect("Failed to create empty project descriptor xml file"); - + let temp_dir = TempTedgeDir::new(); + temp_dir.file(".project"); assert_eq!(get_project_name(temp_dir.path()), "unnamed"); } #[test] fn get_project_name_empty_project_descriptor() { - let temp_dir = TempDir::new().unwrap(); + let temp_dir = TempTedgeDir::new(); assert_eq!(get_project_name(temp_dir.path()), "unnamed"); } #[test] fn get_project_name_invalid_project_descriptor() { - let temp_dir = TempDir::new().unwrap(); - let project_dir_path = temp_dir.path(); - let project_descriptor_path = project_dir_path.join(".project"); - - fs::write(project_descriptor_path.clone(), "not an xml").unwrap(); - assert_eq!(get_project_name(project_dir_path), "unnamed"); + let temp_dir = TempTedgeDir::new(); + temp_dir + .file(".project") + .with_raw_content("not an xml content"); + assert_eq!(get_project_name(temp_dir.path()), "unnamed"); } } |