From ae330f928bdd8f1b89db759ba3c811272d2ae3a4 Mon Sep 17 00:00:00 2001 From: d4v1d03 Date: Thu, 15 Feb 2024 11:57:12 +0530 Subject: [PATCH] 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, {