Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Aug 14, 2023
1 parent 894e621 commit f896c74
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 25 deletions.
6 changes: 5 additions & 1 deletion .moon/tasks/node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ tasks:
- '--color'
- '--ext'
- '.js,.ts,.tsx'
- '--fix'
- '--ignore-path'
- '@in(5)'
- '--exit-on-fatal-error'
Expand All @@ -71,6 +70,11 @@ tasks:
options:
affectedFiles: true

lint-fix:
extends: 'lint'
args: '--fix'
local: true

test:
command: 'jest'
args:
Expand Down
4 changes: 2 additions & 2 deletions nextgen/config/src/inherited_tasks_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::language_platform::{LanguageType, PlatformType};
use crate::project::{validate_deps, TaskConfig};
use crate::project_config::ProjectType;
use crate::project_config::{validate_tasks, ProjectType};
use crate::shapes::InputPath;
use moon_common::cacheable;
use moon_common::{consts, Id};
Expand Down Expand Up @@ -49,7 +49,7 @@ cacheable!(
#[setting(merge = merge::append_vec)]
pub implicit_inputs: Vec<InputPath>,

#[setting(nested, merge = merge::merge_btreemap)]
#[setting(nested, merge = merge::merge_btreemap, validate = validate_tasks)]
pub tasks: BTreeMap<Id, TaskConfig>,
}
);
Expand Down
2 changes: 1 addition & 1 deletion nextgen/config/src/project_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn validate_tasks<D, C>(
if let Some(extends_from) = &config.extends {
if !tasks.contains_key(extends_from) {
return Err(ValidateError::new(format!(
"task {} is extending a non-existent task {}",
"task {} is extending an unknown task {}",
id, extends_from
)));
}
Expand Down
2 changes: 1 addition & 1 deletion nextgen/config/tests/project_config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ tasks:
}

#[test]
#[should_panic(expected = "task extender is extending a non-existent task unknown")]
#[should_panic(expected = "task extender is extending an unknown task unknown")]
fn errors_if_extending_unknown_task() {
test_load_config(
CONFIG_PROJECT_FILENAME,
Expand Down
2 changes: 1 addition & 1 deletion nextgen/config/tests/task_config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod task_config {

#[test]
#[should_panic(
expected = "unknown field `unknown`, expected one of `command`, `args`, `deps`, `env`, `inputs`, `local`, `outputs`, `options`, `platform`, `type`"
expected = "unknown field `unknown`, expected one of `extends`, `command`, `args`, `deps`, `env`, `inputs`, `local`, `outputs`, `options`, `platform`, `type`"
)]
fn error_unknown_field() {
test_parse_config("unknown: 123", |code| TaskConfig::parse(code));
Expand Down
54 changes: 35 additions & 19 deletions nextgen/task-builder/src/tasks_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,7 @@ impl<'proj> TasksBuilder<'proj> {
trace!(target = target.as_str(), "Building task");

let mut task = Task::default();
let mut configs = vec![];

if let Some(config) = self.global_tasks.get(id) {
configs.push(*config);
}

if let Some(config) = self.local_tasks.get(id) {
configs.push(*config);
}
let configs = self.get_config_inherit_chain(id);

// Determine command and args before building options and the task,
// as we need to figure out if we're running in local mode or not.
Expand All @@ -219,7 +211,9 @@ impl<'proj> TasksBuilder<'proj> {
}
}

trace!(target = target.as_str(), "Marking task as local");
if is_local {
trace!(target = target.as_str(), "Marking task as local");
}

task.options = self.build_task_options(id, is_local)?;
task.flags.local = is_local;
Expand Down Expand Up @@ -370,15 +364,11 @@ impl<'proj> TasksBuilder<'proj> {
..TaskOptions::default()
};

let mut configs = vec![];

if let Some(config) = self.global_tasks.get(id) {
configs.push(&config.options);
}

if let Some(config) = self.local_tasks.get(id) {
configs.push(&config.options);
}
let configs = self
.get_config_inherit_chain(id)
.iter()
.map(|cfg| &cfg.options)
.collect::<Vec<_>>();

for config in configs {
if let Some(affected_files) = &config.affected_files {
Expand Down Expand Up @@ -536,6 +526,32 @@ impl<'proj> TasksBuilder<'proj> {
Ok((command, args))
}

fn get_config_inherit_chain(&self, id: &Id) -> Vec<&TaskConfig> {
let mut configs = vec![];

if let Some(config) = self.global_tasks.get(id) {
if let Some(extends_from) = &config.extends {
if let Some(extends_config) = self.global_tasks.get(extends_from) {
configs.push(*extends_config);
}
}

configs.push(*config);
}

if let Some(config) = self.local_tasks.get(id) {
if let Some(extends_from) = &config.extends {
if let Some(extends_config) = self.local_tasks.get(extends_from) {
configs.push(*extends_config);
}
}

configs.push(*config);
}

configs
}

fn merge_map<K, V>(
&self,
base: FxHashMap<K, V>,
Expand Down
48 changes: 48 additions & 0 deletions nextgen/task-builder/tests/__fixtures__/builder/extends/moon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
tags: [extends]

tasks:
base:
command: lint ./src
local: true
inputs:
- '**/*'

extend-args:
extends: base
args: --fix
options:
mergeArgs: prepend

extend-inputs:
extends: base
inputs:
- 'src/**/*'
options:
mergeInputs: replace

extend-options:
extends: base
options:
retryCount: 3
runInCI: true

extend-local:
extends: base
local: false

# Tests global inheritance

local-base:
inputs:
- 'local-base'
options:
cache: true
retryCount: 3

extender:
extends: 'local-base'
args: '-qux'
inputs:
- 'local-extender'
options:
mergeArgs: 'prepend'
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
tasks:
global-base:
command: 'global-base'
local: true
inputs:
- 'global-base'

extender:
extends: 'global-base'
args: '--foo --bar -z'
inputs:
- 'global-extender'
78 changes: 78 additions & 0 deletions nextgen/task-builder/tests/tasks_builder_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1120,4 +1120,82 @@ mod tasks_builder {
);
}
}

mod extending {
use super::*;

#[tokio::test]
async fn handles_args() {
let sandbox = create_sandbox("builder");
let tasks = build_tasks(sandbox.path(), "extends/moon.yml").await;
let task = tasks.get("extend-args").unwrap();

assert_eq!(task.command, "lint");
assert_eq!(task.args, vec!["--fix", "./src"]);
}

#[tokio::test]
async fn handles_inputs() {
let sandbox = create_sandbox("builder");
let tasks = build_tasks(sandbox.path(), "extends/moon.yml").await;
let task = tasks.get("extend-inputs").unwrap();

assert_eq!(
task.inputs,
vec![
InputPath::ProjectGlob("src/**/*".into()),
InputPath::WorkspaceGlob(".moon/*.yml".into()),
]
);
}

#[tokio::test]
async fn handles_options() {
let sandbox = create_sandbox("builder");
let tasks = build_tasks(sandbox.path(), "extends/moon.yml").await;
let task = tasks.get("extend-options").unwrap();

assert!(!task.options.cache);
assert!(task.options.run_in_ci);
assert!(task.options.persistent);
assert_eq!(task.options.retry_count, 3);
}

#[tokio::test]
async fn handles_local() {
let sandbox = create_sandbox("builder");
let tasks = build_tasks(sandbox.path(), "extends/moon.yml").await;
let task = tasks.get("extend-local").unwrap();

assert!(task.options.cache);
assert!(task.options.run_in_ci);
assert!(!task.options.persistent);
}

#[tokio::test]
async fn inherits_and_merges_globals_extend_chain() {
let sandbox = create_sandbox("builder");
let tasks = build_tasks(sandbox.path(), "extends/moon.yml").await;
let task = tasks.get("extender").unwrap();

assert_eq!(task.command, "global-base");
assert_eq!(task.args, vec!["-qux", "--foo", "--bar", "-z"]);
assert_eq!(
task.inputs,
vec![
InputPath::ProjectFile("global-base".into()),
InputPath::ProjectFile("global-extender".into()),
InputPath::ProjectFile("local-base".into()),
InputPath::ProjectFile("local-extender".into()),
InputPath::WorkspaceGlob(".moon/*.yml".into()),
InputPath::WorkspaceFile(".moon/tasks/tag-extends.yml".into()),
]
);

assert!(task.options.cache);
assert!(!task.options.run_in_ci);
assert!(task.options.persistent);
assert_eq!(task.options.retry_count, 3);
}
}
}

0 comments on commit f896c74

Please sign in to comment.