Skip to content

Commit

Permalink
new: Add variants to affectedFiles option. (#488)
Browse files Browse the repository at this point in the history
* Add config.

* Implement.

* Add tests.

* Fix tests.

* Fix windows tests.
  • Loading branch information
milesj committed Dec 19, 2022
1 parent 4f997aa commit 3eafb1b
Show file tree
Hide file tree
Showing 17 changed files with 503 additions and 229 deletions.
43 changes: 37 additions & 6 deletions crates/cli/tests/run_node_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,7 @@ mod affected_files {
let output = assert.output();

assert!(predicate::str::contains("Args: .\n").eval(&output));
assert!(predicate::str::contains("Env: .\n").eval(&output));
}

#[test]
Expand All @@ -1297,14 +1298,40 @@ mod affected_files {
let output = assert.output();

if cfg!(windows) {
assert!(predicate::str::contains("Args: .\\input1.js .\\input2.js").eval(&output));
assert!(predicate::str::contains("Args: .\\input1.js .\\input2.js\n").eval(&output));
assert!(predicate::str::contains("Env: .\\input1.js,.\\input2.js\n").eval(&output));
} else {
assert!(predicate::str::contains("Args: ./input1.js ./input2.js").eval(&output));
assert!(predicate::str::contains("Args: ./input1.js ./input2.js\n").eval(&output));
assert!(predicate::str::contains("Env: ./input1.js,./input2.js\n").eval(&output));
}
}

#[test]
fn sets_env_var() {
fn sets_args_only() {
let sandbox = node_sandbox();

sandbox.create_file("base/input1.js", "");
sandbox.create_file("base/input2.js", "");

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run")
.arg("node:affectedFilesArgs")
.arg("--affected");
});

let output = assert.output();

if cfg!(windows) {
assert!(predicate::str::contains("Args: .\\input1.js .\\input2.js\n").eval(&output));
assert!(predicate::str::contains("Env: \n").eval(&output));
} else {
assert!(predicate::str::contains("Args: ./input1.js ./input2.js\n").eval(&output));
assert!(predicate::str::contains("Env: \n").eval(&output));
}
}

#[test]
fn sets_env_var_only() {
let sandbox = node_sandbox();

sandbox.create_file("base/input1.js", "");
Expand All @@ -1318,8 +1345,12 @@ mod affected_files {

let output = assert.output();

assert!(
predicate::str::contains("MOON_AFFECTED_FILES=./input1.js,./input2.js").eval(&output)
);
if cfg!(windows) {
assert!(predicate::str::contains("Args: \n").eval(&output));
assert!(predicate::str::contains("Env: .\\input1.js,.\\input2.js\n").eval(&output));
} else {
assert!(predicate::str::contains("Args: \n").eval(&output));
assert!(predicate::str::contains("Env: ./input1.js,./input2.js\n").eval(&output));
}
}
}
28 changes: 23 additions & 5 deletions crates/cli/tests/run_system_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ mod unix {
let output = assert.output();

assert!(predicate::str::contains("Args: .\n").eval(&output));
assert!(predicate::str::contains("Env: .\n").eval(&output));
}

#[test]
Expand All @@ -281,10 +282,29 @@ mod unix {
let output = assert.output();

assert!(predicate::str::contains("Args: ./input1.txt ./input2.txt").eval(&output));
assert!(predicate::str::contains("Env: ./input1.txt,./input2.txt").eval(&output));
}

#[test]
fn sets_env_var() {
fn sets_args_only() {
let sandbox = system_sandbox();

sandbox.create_file("unix/input1.txt", "");
sandbox.create_file("unix/input2.txt", "");

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run")
.arg("unix:affectedFilesArgs")
.arg("--affected");
});
let output = assert.output();

assert!(predicate::str::contains("Args: ./input1.txt ./input2.txt\n").eval(&output));
assert!(predicate::str::contains("Env: \n").eval(&output));
}

#[test]
fn sets_env_var_only() {
let sandbox = system_sandbox();

sandbox.create_file("unix/input1.txt", "");
Expand All @@ -297,10 +317,8 @@ mod unix {
});
let output = assert.output();

assert!(
predicate::str::contains("MOON_AFFECTED_FILES=./input1.txt,./input2.txt")
.eval(&output)
);
assert!(predicate::str::contains("Args: \n").eval(&output));
assert!(predicate::str::contains("Env: ./input1.txt,./input2.txt\n").eval(&output));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: crates/cli/tests/run_system_test.rs
assertion_line: 236
assertion_line: 233
expression: "fs::read_to_string(sandbox.path().join(format!(\".moon/cache/hashes/{}.json\",\n state.hash))).unwrap()"
---
[
Expand All @@ -12,13 +12,14 @@ expression: "fs::read_to_string(sandbox.path().join(format!(\".moon/cache/hashes
"deps": {},
"envVars": {},
"inputs": {
"unix/affectedFiles.sh": "4cc0d80d6a9d4dd55481467de96476618ef131ae",
"unix/args.sh": "ac54a54b387de2281f14259a6e974688f6465c57",
"unix/cwd.sh": "9a5ea47dd8809abadf1c61817ffe6be07883af0d",
"unix/envVars.sh": "63cd12999af89a6823e908f32f2a08bf7f27182e",
"unix/envVarsMoon.sh": "357c556827eaccb9fc7cc96fe882c073068b112f",
"unix/exitNonZero.sh": "73cb4653d441d323178c51ea1ba9c4d82e169a91",
"unix/exitZero.sh": "1ad12d878a9eac999c1bfd3b033158e22f147918",
"unix/moon.yml": "a0b293ab4cc21ae2241b36f3afaec39a89ce47c0",
"unix/moon.yml": "111d048aee3d1783f3d32cee5dc49f90b2b3ea12",
"unix/outputs.sh": "9eb7ff5d58f831c346d39e41f98aac3f937a7e1d",
"unix/standard.sh": "5d6a286c841c122f2dcc0df70cbf7cc17629d8c0"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
source: crates/cli/tests/run_system_test.rs
assertion_line: 33
expression: get_assert_output(&assert)
assertion_line: 51
expression: assert.output()
---
▪▪▪▪ unix:ls
affectedFiles.sh
args.sh
cwd.sh
envVars.sh
Expand Down
44 changes: 33 additions & 11 deletions crates/core/config/src/project/task_options.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,53 @@
use crate::validators::validate_child_relative_path;
use crate::{errors::create_validation_error, validators::validate_child_relative_path};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use validator::{Validate, ValidationError};

fn validate_env_file(file: &TaskOptionEnvFile) -> Result<(), ValidationError> {
if let TaskOptionEnvFile::File(path) = file {
validate_child_relative_path("envFile", path)?;
fn validate_affected_files(file: &TaskOptionAffectedFilesConfig) -> Result<(), ValidationError> {
if let TaskOptionAffectedFilesConfig::Value(value) = file {
if value != "args" && value != "env" {
return Err(create_validation_error(
"invalid_value",
"options.affectedFiles",
"expected `args`, `env`, or a boolean",
));
}
}

Ok(())
}

fn validate_env_file(file: &TaskOptionEnvFileConfig) -> Result<(), ValidationError> {
if let TaskOptionEnvFileConfig::File(path) = file {
validate_child_relative_path("options.envFile", path)?;
}

Ok(())
}

#[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(untagged, expecting = "expected `args`, `env`, or a boolean")]
pub enum TaskOptionAffectedFilesConfig {
Enabled(bool),
Value(String),
}

#[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(
untagged,
expecting = "expected a boolean or a relative file system path"
)]
pub enum TaskOptionEnvFile {
pub enum TaskOptionEnvFileConfig {
Enabled(bool),
File(String),
}

impl TaskOptionEnvFile {
impl TaskOptionEnvFileConfig {
pub fn to_option(&self) -> Option<String> {
match self {
TaskOptionEnvFile::Enabled(true) => Some(".env".to_owned()),
TaskOptionEnvFile::Enabled(false) => None,
TaskOptionEnvFile::File(path) => Some(path.to_owned()),
TaskOptionEnvFileConfig::Enabled(true) => Some(".env".to_owned()),
TaskOptionEnvFileConfig::Enabled(false) => None,
TaskOptionEnvFileConfig::File(path) => Some(path.to_owned()),
}
}
}
Expand Down Expand Up @@ -54,12 +75,13 @@ pub enum TaskOutputStyle {
#[schemars(default)]
#[serde(default, rename_all = "camelCase")]
pub struct TaskOptionsConfig {
pub affected_files: Option<bool>,
#[validate(custom = "validate_affected_files")]
pub affected_files: Option<TaskOptionAffectedFilesConfig>,

pub cache: Option<bool>,

#[validate(custom = "validate_env_file")]
pub env_file: Option<TaskOptionEnvFile>,
pub env_file: Option<TaskOptionEnvFileConfig>,

pub merge_args: Option<TaskMergeStrategy>,

Expand Down
119 changes: 119 additions & 0 deletions crates/core/config/tests/tasks_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,8 @@ platform: whatisthis
}

mod options {
use super::*;

#[test]
#[should_panic(
expected = "invalid type: found unsigned int `123`, expected struct TaskOptionsConfig for key \"default.options\""
Expand Down Expand Up @@ -690,4 +692,121 @@ options:
// Ok(())
// });
// }

mod affected_files {

#[test]
#[should_panic(
expected = "expected `args`, `env`, or a boolean for key \"default.options.affectedFiles\""
)]
fn invalid_type() {
figment::Jail::expect_with(|jail| {
jail.create_file(
super::CONFIG_FILENAME,
r#"
command: foo
options:
affectedFiles: 123
"#,
)?;

super::load_jailed_config()?;

Ok(())
});
}

#[test]
#[should_panic(
expected = "expected `args`, `env`, or a boolean for key \"default.options.affectedFiles\""
)]
fn invalid_value() {
figment::Jail::expect_with(|jail| {
jail.create_file(
super::CONFIG_FILENAME,
r#"
command: foo
options:
affectedFiles: unknown
"#,
)?;

super::load_jailed_config()?;

Ok(())
});
}

#[test]
fn supports_bool_true() {
figment::Jail::expect_with(|jail| {
jail.create_file(
super::CONFIG_FILENAME,
r#"
command: foo
options:
affectedFiles: true
"#,
)?;

super::load_jailed_config()?;

Ok(())
});
}

#[test]
fn supports_bool_false() {
figment::Jail::expect_with(|jail| {
jail.create_file(
super::CONFIG_FILENAME,
r#"
command: foo
options:
affectedFiles: false
"#,
)?;

super::load_jailed_config()?;

Ok(())
});
}

#[test]
fn supports_args() {
figment::Jail::expect_with(|jail| {
jail.create_file(
super::CONFIG_FILENAME,
r#"
command: foo
options:
affectedFiles: args
"#,
)?;

super::load_jailed_config()?;

Ok(())
});
}

#[test]
fn supports_env() {
figment::Jail::expect_with(|jail| {
jail.create_file(
super::CONFIG_FILENAME,
r#"
command: foo
options:
affectedFiles: env
"#,
)?;

super::load_jailed_config()?;

Ok(())
});
}
}
}
Loading

0 comments on commit 3eafb1b

Please sign in to comment.