Skip to content

Commit

Permalink
test: move bad args check for config apply to sepparate test
Browse files Browse the repository at this point in the history
  • Loading branch information
dodokek authored and lowitea committed Mar 5, 2025
1 parent 67ed7f7 commit 90a0818
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 26 deletions.
12 changes: 7 additions & 5 deletions src/commands/config/apply.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{bail, Context, Result};
use anyhow::{Context, Result};
use derive_builder::Builder;
use log::info;
use serde::Deserialize;
Expand All @@ -8,7 +8,7 @@ use std::{
env, fs,
io::{BufRead, BufReader, Read, Write},
path::{Path, PathBuf},
process::{Command, Stdio},
process::{self, Command, Stdio},
};

const WISE_PIKE: &str = r"
Expand Down Expand Up @@ -166,8 +166,6 @@ pub fn cmd(params: &Params) -> Result<()> {
return Ok(());
}

info!("Applying plugin config in each plugin");

let root_dir = env::current_dir()?.join(&params.plugin_path);

let cargo_toml_path = root_dir.join("Cargo.toml");
Expand All @@ -182,8 +180,10 @@ pub fn cmd(params: &Params) -> Result<()> {

if let Some(workspace) = parsed_toml.get("workspace") {
if params.config_path.to_str().unwrap() != "plugin_config.yaml" {
bail!(WISE_PIKE);
println!("{WISE_PIKE}");
process::exit(1);
}
info!("Applying plugin config in each plugin");

if let Some(members) = workspace.get("members") {
if let Some(members_array) = members.as_array() {
Expand All @@ -208,6 +208,8 @@ pub fn cmd(params: &Params) -> Result<()> {
return Ok(());
}

info!("Applying plugin config");

apply_plugin_config(params, "./")?;

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/custom_assets_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
// The path is calculated from plugin directory

let params = build::ParamsBuilder::default()
.custom_assets(vec!["topology.toml"])
.custom_assets(vec!["../topology.toml"])
.build()
.unwrap();
build::main(&params);
Expand Down
86 changes: 66 additions & 20 deletions tests/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use helpers::{exec_pike, get_picodata_table, run_cluster, CmdArguments, PLUGIN_D
use std::{
collections::BTreeMap,
fs,
io::{BufRead, BufReader},
path::Path,
process::Command,
process::{Command, Stdio},
time::{Duration, Instant},
vec,
};
Expand Down Expand Up @@ -213,10 +214,53 @@ fn test_workspace_config_apply() {

assert!(is_cluster_valid, "Failed to apply config for one plugin");

assert!(exec_pike(
vec!["stop"],
TESTS_DIR,
&vec![
"--data-dir".to_string(),
"./tmp".to_string(),
"--plugin-path".to_string(),
"./workspace_plugin".to_string()
],
)
.unwrap()
.success());
}

#[test]
fn test_plugin_apply_wrong_cmd_combination() {
let tests_dir = Path::new(TESTS_DIR);
let workspace_path = tests_dir.join("workspace_plugin");

// Cleaning up metadata from past run
if workspace_path.exists() {
fs::remove_dir_all(&workspace_path).unwrap();
}

assert!(exec_pike(
vec!["plugin", "new", "workspace_plugin"],
tests_dir,
&vec!["--workspace".to_string()]
)
.unwrap()
.success());

assert!(exec_pike(
vec!["plugin", "add", "sub_plugin"],
tests_dir,
&vec![
"--plugin-path".to_string(),
"./workspace_plugin".to_string()
]
)
.unwrap()
.success());

// Test uncle Pike wise advice's
// Forced to call Command manually instead of exec_pike to read output
let root_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let wrong_plugin_path_cmd = Command::new(format!("{root_dir}/target/debug/cargo-pike"))
let mut wrong_plugin_path_cmd = Command::new(format!("{root_dir}/target/debug/cargo-pike"))
.args([
"pike",
"config",
Expand All @@ -227,25 +271,27 @@ fn test_workspace_config_apply() {
"./workspace_plugin",
])
.current_dir(TESTS_DIR)
.output()
.unwrap();
.stdout(Stdio::piped()) // Capture stdout
.stderr(Stdio::piped()) // Capture stderr
.spawn()
.expect("Failed to start command");
wrong_plugin_path_cmd.wait().unwrap();

let mut good_output = false;
if let Some(stdout) = wrong_plugin_path_cmd.stdout.take() {
let reader = BufReader::new(stdout);
for line in reader.lines() {
if line
.unwrap()
.contains("You are trying to apply config from")
{
good_output = true;
}
}
}

let stdout = String::from_utf8_lossy(&wrong_plugin_path_cmd.stdout);
assert!(
stdout.contains("You are trying to apply config from"),
"Failed to handle case with invalid command line arguments combination"
good_output,
"Failed to handle case with invalid command line arguments combination line",
);

assert!(exec_pike(
vec!["stop"],
TESTS_DIR,
&vec![
"--data-dir".to_string(),
"./tmp".to_string(),
"--plugin-path".to_string(),
"./workspace_plugin".to_string()
],
)
.unwrap()
.success());
}

0 comments on commit 90a0818

Please sign in to comment.