From ae330f928bdd8f1b89db759ba3c811272d2ae3a4 Mon Sep 17 00:00:00 2001 From: d4v1d03 Date: Thu, 15 Feb 2024 11:57:12 +0530 Subject: [PATCH 01/11] fixed tests and added kpm_update Signed-off-by: d4v1d03 --- kclvm/driver/src/kpm_metadata.rs | 3 +- kclvm/driver/src/kpm_update.rs | 56 ++++++++++++++++++++++++++++++++ kclvm/driver/src/lib.rs | 1 + kclvm/driver/src/tests.rs | 33 +++++++++++++++++++ kclvm/tools/src/vet/tests.rs | 2 +- 5 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 kclvm/driver/src/kpm_update.rs diff --git a/kclvm/driver/src/kpm_metadata.rs b/kclvm/driver/src/kpm_metadata.rs index 996d404fa..5f489beda 100644 --- a/kclvm/driver/src/kpm_metadata.rs +++ b/kclvm/driver/src/kpm_metadata.rs @@ -3,6 +3,7 @@ use kclvm_parser::LoadProgramOptions; use serde::{Deserialize, Serialize}; use std::{collections::HashMap, env, iter, path::PathBuf, process::Command}; + const MANIFEST_FILE: &str = "kcl.mod"; /// [`fill_pkg_maps_for_k_file`] will call `kpm metadata` to obtain the metadata @@ -182,4 +183,4 @@ fn probe(path: PathBuf) -> Option { iter::once(path) .chain(with_extension) .find(|it| it.is_file()) -} +} \ No newline at end of file diff --git a/kclvm/driver/src/kpm_update.rs b/kclvm/driver/src/kpm_update.rs new file mode 100644 index 000000000..d791f17a3 --- /dev/null +++ b/kclvm/driver/src/kpm_update.rs @@ -0,0 +1,56 @@ +use anyhow::{bail, Result}; +use std::{path::PathBuf, process::Command}; +use crate::kpm_metadata::get_path_for_executable; + + +const MANIFEST_FILE: &str = "kcl.mod"; + + +pub(crate) fn update_kcl_module(manifest_path: PathBuf) -> Result<()> {match lookup_the_nearest_file_dir(manifest_path.clone(), MANIFEST_FILE) { + Some(mod_dir) => { + match Command::new(kcl()) + .arg("mod") + .arg("update") + .current_dir(mod_dir) + .output() + { + Ok(output) => { + if !output.status.success() { + bail!( + "update failed with error: {}", + String::from_utf8_lossy(&output.stderr) + ); + } + Ok(()) + } + Err(err) => bail!("update failed with error: {}", err), + } + } + None => bail!("Manifest file '{}' not found in directory hierarchy", MANIFEST_FILE), + } +} +pub fn kcl() -> PathBuf { + get_path_for_executable("kcl") +} +pub fn kpm() -> PathBuf { + get_path_for_executable("kpm") +} + +pub(crate) fn lookup_the_nearest_file_dir( + from: PathBuf, + the_nearest_file: &str, +) -> Option { + let mut current_dir = from; + + loop { + let found_path = current_dir.join(the_nearest_file); + if found_path.is_file() { + return current_dir.canonicalize().ok(); + } + + match current_dir.parent() { + Some(parent) => current_dir = parent.to_path_buf(), + None => return None, + } + } +} \ No newline at end of file diff --git a/kclvm/driver/src/lib.rs b/kclvm/driver/src/lib.rs index a454f65d2..9a2f15e55 100644 --- a/kclvm/driver/src/lib.rs +++ b/kclvm/driver/src/lib.rs @@ -1,6 +1,7 @@ use anyhow::Result; pub mod arguments; pub mod kpm_metadata; +pub mod kpm_update; pub const DEFAULT_PROJECT_FILE: &str = "project.yaml"; #[cfg(test)] diff --git a/kclvm/driver/src/tests.rs b/kclvm/driver/src/tests.rs index 2d218f393..1c3406217 100644 --- a/kclvm/driver/src/tests.rs +++ b/kclvm/driver/src/tests.rs @@ -1,5 +1,6 @@ use std::path::{Path, PathBuf}; use std::{env, fs, panic}; +use super::*; use kclvm_config::modfile::get_vendor_home; use kclvm_config::settings::KeyValuePair; @@ -9,6 +10,7 @@ use walkdir::WalkDir; use crate::arguments::parse_key_value_pair; use crate::kpm_metadata::{fetch_metadata, fill_pkg_maps_for_k_file, lookup_the_nearest_file_dir}; use crate::{canonicalize_input_files, expand_input_files, get_pkg_list}; +use crate::kpm_update::update_kcl_module; #[test] fn test_canonicalize_input_files() { @@ -379,3 +381,34 @@ fn test_get_pkg_list() { 3 ); } + +#[cfg(test)] + // Define a mock structure to simulate the behavior of Command::output + struct MockCommand { + output: Result, + } + // Define a mock structure to represent the output of Command::output + struct MockCommandOutput { + status: std::process::ExitStatus, + stderr: Vec, + } + + #[test] + fn test_update_kcl_module_success() { + let manifest_path = PathBuf::from("path/to/manifest"); + let result = update_kcl_module( manifest_path); + assert!(result.is_ok()); + } + + #[test] + fn test_update_kcl_module_failure() { + + let manifest_path = PathBuf::from("path/to/manifest"); + fn mock_command_new_failing(_command: &str) -> MockCommand { + MockCommand { + output: Err(std::io::Error::new(std::io::ErrorKind::Other, "Command failed")), + } + } + let result = update_kcl_module( manifest_path); + assert!(result.is_err()); + } \ No newline at end of file diff --git a/kclvm/tools/src/vet/tests.rs b/kclvm/tools/src/vet/tests.rs index 0623a8c4e..b7b51b597 100644 --- a/kclvm/tools/src/vet/tests.rs +++ b/kclvm/tools/src/vet/tests.rs @@ -616,7 +616,7 @@ mod test_validater { } /// Deal with windows filepath -fn deal_windows_filepath(filepath: String, transform: F) -> String +fn deal_windows_filepath(filepath: String, _transform: F) -> String where F: FnOnce(String) -> String, { From ae47cd3daf14c6ec3c9a48e488bea56cc1de87be Mon Sep 17 00:00:00 2001 From: d4v1d03 Date: Thu, 15 Feb 2024 12:09:31 +0530 Subject: [PATCH 02/11] code formatting made better Signed-off-by: d4v1d03 --- kclvm/driver/src/kpm_metadata.rs | 3 +- kclvm/driver/src/kpm_update.rs | 11 ++++---- kclvm/driver/src/tests.rs | 48 ++++++++++++++++---------------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/kclvm/driver/src/kpm_metadata.rs b/kclvm/driver/src/kpm_metadata.rs index 5f489beda..996d404fa 100644 --- a/kclvm/driver/src/kpm_metadata.rs +++ b/kclvm/driver/src/kpm_metadata.rs @@ -3,7 +3,6 @@ use kclvm_parser::LoadProgramOptions; use serde::{Deserialize, Serialize}; use std::{collections::HashMap, env, iter, path::PathBuf, process::Command}; - const MANIFEST_FILE: &str = "kcl.mod"; /// [`fill_pkg_maps_for_k_file`] will call `kpm metadata` to obtain the metadata @@ -183,4 +182,4 @@ fn probe(path: PathBuf) -> Option { iter::once(path) .chain(with_extension) .find(|it| it.is_file()) -} \ No newline at end of file +} diff --git a/kclvm/driver/src/kpm_update.rs b/kclvm/driver/src/kpm_update.rs index d791f17a3..56e673f0c 100644 --- a/kclvm/driver/src/kpm_update.rs +++ b/kclvm/driver/src/kpm_update.rs @@ -2,11 +2,10 @@ use anyhow::{bail, Result}; use std::{path::PathBuf, process::Command}; use crate::kpm_metadata::get_path_for_executable; - const MANIFEST_FILE: &str = "kcl.mod"; - -pub(crate) fn update_kcl_module(manifest_path: PathBuf) -> Result<()> {match lookup_the_nearest_file_dir(manifest_path.clone(), MANIFEST_FILE) { +pub(crate) fn update_kcl_module(manifest_path: PathBuf) -> Result<()> { + match lookup_the_nearest_file_dir(manifest_path.clone(), MANIFEST_FILE) { Some(mod_dir) => { match Command::new(kcl()) .arg("mod") @@ -26,7 +25,10 @@ pub(crate) fn update_kcl_module(manifest_path: PathBuf) -> Result<()> {match loo Err(err) => bail!("update failed with error: {}", err), } } - None => bail!("Manifest file '{}' not found in directory hierarchy", MANIFEST_FILE), + None => bail!( + "Manifest file '{}' not found in directory hierarchy", + MANIFEST_FILE + ), } } pub fn kcl() -> PathBuf { @@ -47,7 +49,6 @@ pub(crate) fn lookup_the_nearest_file_dir( if found_path.is_file() { return current_dir.canonicalize().ok(); } - match current_dir.parent() { Some(parent) => current_dir = parent.to_path_buf(), None => return None, diff --git a/kclvm/driver/src/tests.rs b/kclvm/driver/src/tests.rs index 1c3406217..fe08f347f 100644 --- a/kclvm/driver/src/tests.rs +++ b/kclvm/driver/src/tests.rs @@ -1,6 +1,6 @@ +use super::*; use std::path::{Path, PathBuf}; use std::{env, fs, panic}; -use super::*; use kclvm_config::modfile::get_vendor_home; use kclvm_config::settings::KeyValuePair; @@ -383,32 +383,32 @@ fn test_get_pkg_list() { } #[cfg(test)] - // Define a mock structure to simulate the behavior of Command::output - struct MockCommand { - output: Result, - } - // Define a mock structure to represent the output of Command::output - struct MockCommandOutput { - status: std::process::ExitStatus, - stderr: Vec, - } +// Define a mock structure to simulate the behavior of Command::output + struct MockCommand { + output: Result, +} +// Define a mock structure to represent the output of Command::output +struct MockCommandOutput { + status: std::process::ExitStatus, + stderr: Vec, + } - #[test] - fn test_update_kcl_module_success() { - let manifest_path = PathBuf::from("path/to/manifest"); - let result = update_kcl_module( manifest_path); - assert!(result.is_ok()); - } +#[test] +fn test_update_kcl_module_success() { + let manifest_path = PathBuf::from("path/to/manifest"); + let result = update_kcl_module( manifest_path); + assert!(result.is_ok() + ); +} - #[test] - fn test_update_kcl_module_failure() { - - let manifest_path = PathBuf::from("path/to/manifest"); - fn mock_command_new_failing(_command: &str) -> MockCommand { +#[test] +fn test_update_kcl_module_failure() { + let manifest_path = PathBuf::from("path/to/manifest"); + fn mock_command_new_failing(_command: &str) -> MockCommand { MockCommand { output: Err(std::io::Error::new(std::io::ErrorKind::Other, "Command failed")), } } - let result = update_kcl_module( manifest_path); - assert!(result.is_err()); - } \ No newline at end of file + let result = update_kcl_module( manifest_path); + assert!(result.is_err()); +} \ No newline at end of file From 40229cc3633c3bdc6379e2d67045712b0f5fc9ed Mon Sep 17 00:00:00 2001 From: d4v1d03 Date: Thu, 15 Feb 2024 19:01:16 +0530 Subject: [PATCH 03/11] make fmt done Signed-off-by: d4v1d03 --- kclvm/driver/src/kpm_update.rs | 6 +++--- kclvm/driver/src/tests.rs | 30 ++++++++++++++++-------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/kclvm/driver/src/kpm_update.rs b/kclvm/driver/src/kpm_update.rs index 56e673f0c..5fb384a42 100644 --- a/kclvm/driver/src/kpm_update.rs +++ b/kclvm/driver/src/kpm_update.rs @@ -1,6 +1,6 @@ +use crate::kpm_metadata::get_path_for_executable; use anyhow::{bail, Result}; use std::{path::PathBuf, process::Command}; -use crate::kpm_metadata::get_path_for_executable; const MANIFEST_FILE: &str = "kcl.mod"; @@ -26,7 +26,7 @@ pub(crate) fn update_kcl_module(manifest_path: PathBuf) -> Result<()> { } } None => bail!( - "Manifest file '{}' not found in directory hierarchy", + "Manifest file '{}' not found in directory hierarchy", MANIFEST_FILE ), } @@ -54,4 +54,4 @@ pub(crate) fn lookup_the_nearest_file_dir( None => return None, } } -} \ No newline at end of file +} diff --git a/kclvm/driver/src/tests.rs b/kclvm/driver/src/tests.rs index fe08f347f..7aa14acdf 100644 --- a/kclvm/driver/src/tests.rs +++ b/kclvm/driver/src/tests.rs @@ -9,8 +9,8 @@ use walkdir::WalkDir; use crate::arguments::parse_key_value_pair; use crate::kpm_metadata::{fetch_metadata, fill_pkg_maps_for_k_file, lookup_the_nearest_file_dir}; -use crate::{canonicalize_input_files, expand_input_files, get_pkg_list}; use crate::kpm_update::update_kcl_module; +use crate::{canonicalize_input_files, expand_input_files, get_pkg_list}; #[test] fn test_canonicalize_input_files() { @@ -384,31 +384,33 @@ fn test_get_pkg_list() { #[cfg(test)] // Define a mock structure to simulate the behavior of Command::output - struct MockCommand { - output: Result, +struct MockCommand { + output: Result, } // Define a mock structure to represent the output of Command::output struct MockCommandOutput { - status: std::process::ExitStatus, - stderr: Vec, - } + status: std::process::ExitStatus, + stderr: Vec, +} #[test] fn test_update_kcl_module_success() { let manifest_path = PathBuf::from("path/to/manifest"); - let result = update_kcl_module( manifest_path); - assert!(result.is_ok() - ); + let result = update_kcl_module(manifest_path); + assert!(result.is_ok()); } #[test] fn test_update_kcl_module_failure() { let manifest_path = PathBuf::from("path/to/manifest"); fn mock_command_new_failing(_command: &str) -> MockCommand { - MockCommand { - output: Err(std::io::Error::new(std::io::ErrorKind::Other, "Command failed")), - } + MockCommand { + output: Err(std::io::Error::new( + std::io::ErrorKind::Other, + "Command failed", + )), } - let result = update_kcl_module( manifest_path); + } + let result = update_kcl_module(manifest_path); assert!(result.is_err()); -} \ No newline at end of file +} From 78991e35250a054ff933e0e25aecbec3b84dbbf7 Mon Sep 17 00:00:00 2001 From: d4v1d03 Date: Tue, 20 Feb 2024 06:15:27 +0530 Subject: [PATCH 04/11] removed kpm() Signed-off-by: d4v1d03 --- kclvm/driver/src/kpm_update.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/kclvm/driver/src/kpm_update.rs b/kclvm/driver/src/kpm_update.rs index 5fb384a42..7835bf018 100644 --- a/kclvm/driver/src/kpm_update.rs +++ b/kclvm/driver/src/kpm_update.rs @@ -34,9 +34,6 @@ pub(crate) fn update_kcl_module(manifest_path: PathBuf) -> Result<()> { pub fn kcl() -> PathBuf { get_path_for_executable("kcl") } -pub fn kpm() -> PathBuf { - get_path_for_executable("kpm") -} pub(crate) fn lookup_the_nearest_file_dir( from: PathBuf, From ee4550488e79e63ab9fc9aabc085ab1a882a82a1 Mon Sep 17 00:00:00 2001 From: d4v1d03 Date: Wed, 21 Feb 2024 00:45:32 +0530 Subject: [PATCH 05/11] tweaks Signed-off-by: d4v1d03 --- kclvm/driver/src/tests.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/kclvm/driver/src/tests.rs b/kclvm/driver/src/tests.rs index 7aa14acdf..90faaa4de 100644 --- a/kclvm/driver/src/tests.rs +++ b/kclvm/driver/src/tests.rs @@ -393,13 +393,6 @@ struct MockCommandOutput { stderr: Vec, } -#[test] -fn test_update_kcl_module_success() { - let manifest_path = PathBuf::from("path/to/manifest"); - let result = update_kcl_module(manifest_path); - assert!(result.is_ok()); -} - #[test] fn test_update_kcl_module_failure() { let manifest_path = PathBuf::from("path/to/manifest"); @@ -414,3 +407,12 @@ fn test_update_kcl_module_failure() { let result = update_kcl_module(manifest_path); assert!(result.is_err()); } + +#[test] +fn test_update_kcl_module_success() { + let manifest_path = PathBuf::from("path/to/manifest"); + let result = update_kcl_module(manifest_path); + assert!(result.is_ok()); +} + + From 21519b85cbe4b42940472cd2f80280fa361981b2 Mon Sep 17 00:00:00 2001 From: d4v1d03 Date: Wed, 21 Feb 2024 13:11:06 +0530 Subject: [PATCH 06/11] re Signed-off-by: d4v1d03 --- kclvm/driver/src/tests.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/kclvm/driver/src/tests.rs b/kclvm/driver/src/tests.rs index 90faaa4de..bb5f1220d 100644 --- a/kclvm/driver/src/tests.rs +++ b/kclvm/driver/src/tests.rs @@ -414,5 +414,3 @@ fn test_update_kcl_module_success() { let result = update_kcl_module(manifest_path); assert!(result.is_ok()); } - - From 26d14b434c783cb2949067b3b6c8566a23368be3 Mon Sep 17 00:00:00 2001 From: d4v1d03 Date: Wed, 21 Feb 2024 13:48:14 +0530 Subject: [PATCH 07/11] removed unnecessary test that required test environment setup Signed-off-by: d4v1d03 --- kclvm/driver/src/tests.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/kclvm/driver/src/tests.rs b/kclvm/driver/src/tests.rs index bb5f1220d..279f1cb3c 100644 --- a/kclvm/driver/src/tests.rs +++ b/kclvm/driver/src/tests.rs @@ -407,10 +407,3 @@ fn test_update_kcl_module_failure() { let result = update_kcl_module(manifest_path); assert!(result.is_err()); } - -#[test] -fn test_update_kcl_module_success() { - let manifest_path = PathBuf::from("path/to/manifest"); - let result = update_kcl_module(manifest_path); - assert!(result.is_ok()); -} From 62c3dfc43daa942da668d6d9973ecb45c1a871a3 Mon Sep 17 00:00:00 2001 From: d4v1d03 Date: Wed, 21 Feb 2024 14:19:00 +0530 Subject: [PATCH 08/11] fixed the failing test Signed-off-by: d4v1d03 --- kclvm/driver/src/tests.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kclvm/driver/src/tests.rs b/kclvm/driver/src/tests.rs index 279f1cb3c..63f180e28 100644 --- a/kclvm/driver/src/tests.rs +++ b/kclvm/driver/src/tests.rs @@ -407,3 +407,11 @@ fn test_update_kcl_module_failure() { let result = update_kcl_module(manifest_path); assert!(result.is_err()); } + +#[test] +fn test_update_kcl_module_success() { + let manifest_path = PathBuf::from("./src/test_data/expand_file_pattern/kcl1"); + let result = update_kcl_module(manifest_path); + println!("{:?}", result); + assert!(result.is_ok()); +} From 73e1c11b5e1c0f014db70a529e14e7260c49b98e Mon Sep 17 00:00:00 2001 From: d4v1d03 Date: Wed, 21 Feb 2024 14:43:28 +0530 Subject: [PATCH 09/11] removed Signed-off-by: d4v1d03 --- kclvm/driver/src/tests.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kclvm/driver/src/tests.rs b/kclvm/driver/src/tests.rs index 279f1cb3c..c804af414 100644 --- a/kclvm/driver/src/tests.rs +++ b/kclvm/driver/src/tests.rs @@ -407,3 +407,5 @@ fn test_update_kcl_module_failure() { let result = update_kcl_module(manifest_path); assert!(result.is_err()); } + + From 93a05fc88687b8ea470a5117c82007c724b3c510 Mon Sep 17 00:00:00 2001 From: d4v1d03 Date: Wed, 21 Feb 2024 15:09:10 +0530 Subject: [PATCH 10/11] re Signed-off-by: d4v1d03 --- kclvm/driver/src/tests.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/kclvm/driver/src/tests.rs b/kclvm/driver/src/tests.rs index 90b719909..279f1cb3c 100644 --- a/kclvm/driver/src/tests.rs +++ b/kclvm/driver/src/tests.rs @@ -407,10 +407,3 @@ fn test_update_kcl_module_failure() { let result = update_kcl_module(manifest_path); assert!(result.is_err()); } - -#[test] -fn test_update_kcl_module_success() { - let manifest_path = PathBuf::from("./src/test_data/expand_file_pattern/kcl1"); - let result = update_kcl_module(manifest_path); - assert!(result.is_ok()); -} From 719e300b07890562cbccde75a9b990ebbbcaa810 Mon Sep 17 00:00:00 2001 From: d4v1d03 Date: Wed, 21 Feb 2024 15:53:39 +0530 Subject: [PATCH 11/11] fix Signed-off-by: d4v1d03 --- kclvm/tools/src/vet/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kclvm/tools/src/vet/tests.rs b/kclvm/tools/src/vet/tests.rs index b7b51b597..0623a8c4e 100644 --- a/kclvm/tools/src/vet/tests.rs +++ b/kclvm/tools/src/vet/tests.rs @@ -616,7 +616,7 @@ mod test_validater { } /// Deal with windows filepath -fn deal_windows_filepath(filepath: String, _transform: F) -> String +fn deal_windows_filepath(filepath: String, transform: F) -> String where F: FnOnce(String) -> String, {