diff --git a/kclvm/driver/src/kpm_metadata.rs b/kclvm/driver/src/kpm_metadata.rs index ed1abdc39..694eb7f6f 100644 --- a/kclvm/driver/src/kpm_metadata.rs +++ b/kclvm/driver/src/kpm_metadata.rs @@ -1,4 +1,4 @@ -use crate::{get_path_for_executable, kcl, lookup_the_nearest_file_dir}; +use crate::lib::{get_path_for_executable, kcl, lookup_the_nearest_file_dir}; use anyhow::{bail, Ok, Result}; use kclvm_parser::LoadProgramOptions; use serde::{Deserialize, Serialize}; diff --git a/kclvm/driver/src/kpm_update.rs b/kclvm/driver/src/kpm_update.rs index f6caf4f5e..d1ffcc02a 100644 --- a/kclvm/driver/src/kpm_update.rs +++ b/kclvm/driver/src/kpm_update.rs @@ -1,6 +1,6 @@ use anyhow::{bail, Result}; use std::{path::PathBuf, process::Command}; -use crate::{probe, lookup_the_nearest_file_dir, kcl}; +use crate::lib::{probe, lookup_the_nearest_file_dir, kcl}; const MANIFEST_FILE: &str = "kcl.mod"; diff --git a/kclvm/driver/src/test_data/kpm_update/kcl.mod b/kclvm/driver/src/test_data/kpm_update/kcl.mod new file mode 100644 index 000000000..d3ed2fe74 --- /dev/null +++ b/kclvm/driver/src/test_data/kpm_update/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "kpm_update" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +k8s = "1.29" diff --git a/kclvm/driver/src/test_data/kpm_update/subdir/main.k b/kclvm/driver/src/test_data/kpm_update/subdir/main.k new file mode 100644 index 000000000..e69de29bb diff --git a/kclvm/driver/src/tests.rs b/kclvm/driver/src/tests.rs index 2d218f393..3f3a358fd 100644 --- a/kclvm/driver/src/tests.rs +++ b/kclvm/driver/src/tests.rs @@ -7,8 +7,10 @@ use kclvm_parser::LoadProgramOptions; 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::kpm_metadata::{fetch_metadata}; +use crate::kpm_update::{update_kcl_module}; use crate::{canonicalize_input_files, expand_input_files, get_pkg_list}; +use crate::lib::{fill_pkg_maps_for_k_file, lookup_the_nearest_file_dir}; #[test] fn test_canonicalize_input_files() { @@ -379,3 +381,45 @@ fn test_get_pkg_list() { 3 ); } + +#[test] +fn test_lookup_the_nearest_file_dir() { + let path = PathBuf::from(".") + .join("src") + .join("test_data") + .join("kpm_update"); + let result = lookup_the_nearest_file_dir(path.clone(), "kcl.mod"); + assert!(result.is_some()); + assert_eq!( + result.unwrap().display().to_string(), + path.canonicalize().unwrap().display().to_string() + ); + + let main_path = path.join("subdir").join("main.k"); + let result = lookup_the_nearest_file_dir(main_path, "kcl.mod"); + assert!(result.is_some()); + assert_eq!( + result.unwrap().display().to_string(), + path.canonicalize().unwrap().display().to_string() + ); +} + +fn test_fetch_mod_metadata() { + let path = PathBuf::from(".") + .join("src") + .join("test_data") + .join("kpm_update"); + + let update_mod = update_kcl_module(path.clone()); + // Show more information when the test fails. + println!("{:?}", update_mod); + assert!(!update_mod.is_err()); + let pkgs = metadata.unwrap().packages.clone(); + assert_eq!(pkgs.len(), 1); +} + +#[test] +fn test_update_module() { + test_fetch_mod_metadata(); + println!("test_fetch_metadata() passed"); +} \ No newline at end of file