Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for condition_script_runner_args expansion #1157

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ toml = "^0.8"
[dev-dependencies]
cfg-if = "^1.0.0"
expect-test = "^1"
tempfile = "^3.10.1"

[target.'cfg(windows)'.dependencies]
nu-ansi-term = "^0.50"
Expand Down
96 changes: 94 additions & 2 deletions src/lib/runner_test.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::env::VarError;
use std::fs;

use super::*;
use crate::test;
use crate::types::{
ConditionScriptValue, ConfigSection, CrateInfo, EnvFile, RunTaskDetails, ScriptValue,
TaskCondition,
ConditionScriptValue, ConfigSection, CrateInfo, EnvFile, InstallCrate, InstallCrateInfo,
RunTaskDetails, ScriptValue, TaskCondition, TestArg,
};
use cfg_if::cfg_if;
use git_info::types::GitInfo;
use rust_info::types::RustInfo;
use tempfile::tempdir;

#[cfg(target_os = "linux")]
use crate::types::WatchOptions;
Expand Down Expand Up @@ -961,6 +963,96 @@ fn run_task_failed_condition_script_doesnt_change_env() {
assert_eq!(std::env::var(NEW_ENV_VAR), Err(VarError::NotPresent));
}

#[test]
#[ignore]
fn run_task_rust_script_with_args_and_rust_condition_script_with_args() {
let dummy_path = "dummy/path";

// Create temporary directory to store outputs of condition script and script
let output_dir = tempdir().unwrap();
let condition_script_output_file = output_dir.path().join("condition-script-output.txt");
let script_output_file = output_dir.path().join("script-output.txt");

let flow_info = FlowInfo {
config: Config::default(),
task: "test".to_string(),
env_info: EnvInfo {
rust_info: RustInfo::new(),
crate_info: CrateInfo::new(),
git_info: GitInfo::new(),
ci_info: ci_info::get(),
},
disable_workspace: false,
disable_on_error: false,
allow_private: false,
skip_init_end_tasks: false,
skip_tasks_pattern: None,
cli_arguments: None,
};

let step = Step {
name: "test".to_string(),
config: Task {
install_crate: Some(InstallCrate::CrateInfo(InstallCrateInfo {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

install_crate here doesnt do what i intended here since its run after the condition script is validated. I should just remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sagiegurari is there a better way to ensure that rust-script 0.35.0 is available for this test?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can create multiple tasks with dependencies.
so one task will do install and another task depending on the first, will do the condition.

crate_name: "rust-script".to_string(),
rustup_component_name: None,
binary: "rust-script".to_string(),
test_arg: TestArg {
inner: vec!["--version".to_string()],
},
// rust-script 0.35.0 introduced a fix required for this test to work: https://github.com/fornwall/rust-script/issues/135
min_version: Some("0.35.0".to_string()),
version: None,
install_command: None,
force: None,
})),
condition_script_runner_args: Some(
["--base-path", &dummy_path]
.iter()
.map(ToString::to_string)
.collect(),
),
condition_script: Some(ConditionScriptValue::SingleLine(format!(
r##"#!@rust
#![allow(unused_doc_comments)]

let base_path = std::env::var("RUST_SCRIPT_BASE_PATH").expect("RUST_SCRIPT_BASE_PATH should always be set by rust-script");
std::fs::write(r#"{}"#, base_path)?
"##,
condition_script_output_file.to_str().unwrap()
))),
script_runner_args: Some(
["--base-path", &dummy_path]
.iter()
.map(ToString::to_string)
.collect(),
),
script: Some(ScriptValue::SingleLine(format!(
r##"#!@rust
#![allow(unused_doc_comments)]

let base_path = std::env::var("RUST_SCRIPT_BASE_PATH").expect("RUST_SCRIPT_BASE_PATH should always be set by rust-script");
std::fs::write(r#"{}"#, base_path)?
"##,
script_output_file.to_str().unwrap()
))),
..Default::default()
},
};

run_task(&flow_info, Rc::new(RefCell::new(FlowState::new())), &step).unwrap();

// Check that condition_script_args are expanded
let condition_script_output = fs::read_to_string(&condition_script_output_file)
.expect("condition_script should have created this file");
assert_eq!(condition_script_output, dummy_path);

// Check that script_args are expanded
let script_output =
fs::read_to_string(&script_output_file).expect("script should have created this file");
assert_eq!(script_output, dummy_path);
}

#[test]
#[ignore]
fn should_watch_none_and_env_not_set() {
Expand Down
Loading