Skip to content

Commit

Permalink
fixed tests and added kpm_update
Browse files Browse the repository at this point in the history
Signed-off-by: d4v1d03 <[email protected]>
  • Loading branch information
d4v1d03 committed Feb 15, 2024
1 parent 80f6849 commit ae330f9
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
3 changes: 2 additions & 1 deletion kclvm/driver/src/kpm_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -182,4 +183,4 @@ fn probe(path: PathBuf) -> Option<PathBuf> {
iter::once(path)
.chain(with_extension)
.find(|it| it.is_file())
}
}
56 changes: 56 additions & 0 deletions kclvm/driver/src/kpm_update.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf> {
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,
}
}
}
1 change: 1 addition & 0 deletions kclvm/driver/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
33 changes: 33 additions & 0 deletions kclvm/driver/src/tests.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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<MockCommandOutput, std::io::Error>,
}
// Define a mock structure to represent the output of Command::output
struct MockCommandOutput {
status: std::process::ExitStatus,
stderr: Vec<u8>,
}

#[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());
}
2 changes: 1 addition & 1 deletion kclvm/tools/src/vet/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ mod test_validater {
}

/// Deal with windows filepath
fn deal_windows_filepath<F>(filepath: String, transform: F) -> String
fn deal_windows_filepath<F>(filepath: String, _transform: F) -> String
where
F: FnOnce(String) -> String,
{
Expand Down

0 comments on commit ae330f9

Please sign in to comment.