Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Dec 13, 2024
1 parent 5bec5dc commit af84e3a
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 41 deletions.
4 changes: 4 additions & 0 deletions crates/action-graph/src/action_graph_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,10 @@ impl<'app> ActionGraphBuilder<'app> {
let mut inserted_nodes = vec![];
let mut initial_targets = vec![];

if let Some(affected) = &mut self.affected {
affected.set_ci_check(reqs.ci_check);
}

// Track the qualified as an initial target
for locator in reqs.target_locators.clone() {
initial_targets.push(match locator {
Expand Down
10 changes: 5 additions & 5 deletions crates/action-graph/tests/action_graph_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use moon_action_context::TargetState;
use moon_action_graph::*;
use moon_common::path::WorkspaceRelativePathBuf;
use moon_common::Id;
use moon_config::{PlatformType, TaskArgs, TaskDependencyConfig};
use moon_config::{PlatformType, TaskArgs, TaskDependencyConfig, TaskOptionRunInCI};
use moon_platform::*;
use moon_task::{Target, TargetLocator, Task};
use moon_test_utils2::generate_workspace_graph;
Expand Down Expand Up @@ -1027,7 +1027,7 @@ mod action_graph {
let project = container.workspace_graph.get_project("bar").unwrap();

let mut task = create_task("build", "bar");
task.options.run_in_ci = true;
task.options.run_in_ci = TaskOptionRunInCI::Enabled(true);

builder
.run_task(
Expand Down Expand Up @@ -1121,7 +1121,7 @@ mod action_graph {
let project = container.workspace_graph.get_project("bar").unwrap();

let mut task = create_task("build", "bar");
task.options.run_in_ci = false;
task.options.run_in_ci = TaskOptionRunInCI::Enabled(false);

builder
.run_task(
Expand Down Expand Up @@ -1158,7 +1158,7 @@ mod action_graph {
let project = container.workspace_graph.get_project("bar").unwrap();

let mut task = create_task("build", "bar");
task.options.run_in_ci = false;
task.options.run_in_ci = TaskOptionRunInCI::Enabled(false);

builder
.run_task(
Expand Down Expand Up @@ -1188,7 +1188,7 @@ mod action_graph {
let project = container.workspace_graph.get_project("bar").unwrap();

let mut task = create_task("build", "bar");
task.options.run_in_ci = false;
task.options.run_in_ci = TaskOptionRunInCI::Enabled(false);

builder
.run_task(
Expand Down
24 changes: 17 additions & 7 deletions crates/affected/src/affected_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use std::fmt;
use tracing::{debug, trace};

pub struct AffectedTracker<'app> {
ci: bool,

workspace_graph: &'app WorkspaceGraph,
touched_files: &'app FxHashSet<WorkspaceRelativePathBuf>,

Expand Down Expand Up @@ -38,6 +40,7 @@ impl<'app> AffectedTracker<'app> {
tasks: FxHashMap::default(),
task_downstream: DownstreamScope::None,
task_upstream: UpstreamScope::Deep,
ci: false,
}
}

Expand Down Expand Up @@ -82,6 +85,11 @@ impl<'app> AffectedTracker<'app> {
affected
}

pub fn set_ci_check(&mut self, ci: bool) -> &mut Self {
self.ci = ci;
self
}

pub fn with_project_scopes(
&mut self,
upstream_scope: UpstreamScope,
Expand Down Expand Up @@ -322,13 +330,15 @@ impl<'app> AffectedTracker<'app> {
return Ok(Some(AffectedBy::AlreadyMarked));
}

match &task.options.run_in_ci {
TaskOptionRunInCI::Always => {
return Ok(Some(AffectedBy::AlwaysAffected));
}
TaskOptionRunInCI::Enabled(false) => return Ok(None),
_ => {}
};
if self.ci {
match &task.options.run_in_ci {
TaskOptionRunInCI::Always => {
return Ok(Some(AffectedBy::AlwaysAffected));
}
TaskOptionRunInCI::Enabled(false) => return Ok(None),
_ => {}
};
}

// inputs: []
if task.state.empty_inputs {
Expand Down
13 changes: 13 additions & 0 deletions crates/affected/tests/__fixtures__/tasks/ci/moon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
tasks:
enabled:
options:
runInCI: true
disabled:
options:
runInCI: false
affected:
options:
runInCI: affected
always:
options:
runInCI: always
106 changes: 106 additions & 0 deletions crates/affected/tests/affected_tracker_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,4 +956,110 @@ mod affected_tasks {
);
}
}

mod ci {
use super::*;

#[tokio::test]
async fn when_ci_tracks_for_true() {
let workspace_graph = generate_workspace_graph("tasks").await;
let touched_files = FxHashSet::from_iter(["ci/file.txt".into()]);

let mut tracker = AffectedTracker::new(&workspace_graph, &touched_files);
tracker.set_ci_check(true);
tracker
.track_tasks_by_target(&[Target::parse("ci:enabled").unwrap()])
.unwrap();
let affected = tracker.build();

assert_eq!(
affected.tasks,
FxHashMap::from_iter([(
Target::parse("ci:enabled").unwrap(),
create_state_from_file("ci/file.txt")
)])
);
}

#[tokio::test]
async fn when_not_ci_tracks_for_true() {
let workspace_graph = generate_workspace_graph("tasks").await;
let touched_files = FxHashSet::from_iter(["ci/file.txt".into()]);

let mut tracker = AffectedTracker::new(&workspace_graph, &touched_files);
tracker.set_ci_check(false);
tracker
.track_tasks_by_target(&[Target::parse("ci:enabled").unwrap()])
.unwrap();
let affected = tracker.build();

assert_eq!(
affected.tasks,
FxHashMap::from_iter([(
Target::parse("ci:enabled").unwrap(),
create_state_from_file("ci/file.txt")
)])
);
}

#[tokio::test]
async fn when_ci_doesnt_track_for_false() {
let workspace_graph = generate_workspace_graph("tasks").await;
let touched_files = FxHashSet::from_iter(["ci/file.txt".into()]);

let mut tracker = AffectedTracker::new(&workspace_graph, &touched_files);
tracker.set_ci_check(true);
tracker
.track_tasks_by_target(&[Target::parse("ci:disabled").unwrap()])
.unwrap();
let affected = tracker.build();

assert!(affected.tasks.is_empty());
}

#[tokio::test]
async fn when_not_ci_tracks_for_false() {
let workspace_graph = generate_workspace_graph("tasks").await;
let touched_files = FxHashSet::from_iter(["ci/file.txt".into()]);

let mut tracker = AffectedTracker::new(&workspace_graph, &touched_files);
tracker.set_ci_check(false);
tracker
.track_tasks_by_target(&[Target::parse("ci:disabled").unwrap()])
.unwrap();
let affected = tracker.build();

assert_eq!(
affected.tasks,
FxHashMap::from_iter([(
Target::parse("ci:disabled").unwrap(),
create_state_from_file("ci/file.txt")
)])
);
}

#[tokio::test]
async fn when_ci_always_tracks_if_not_touched() {
let workspace_graph = generate_workspace_graph("tasks").await;
let touched_files = FxHashSet::default();

let mut tracker = AffectedTracker::new(&workspace_graph, &touched_files);
tracker.set_ci_check(true);
tracker
.track_tasks_by_target(&[Target::parse("ci:always").unwrap()])
.unwrap();
let affected = tracker.build();

assert_eq!(
affected.tasks,
FxHashMap::from_iter([(
Target::parse("ci:always").unwrap(),
AffectedTaskState {
other: true,
..Default::default()
}
)])
);
}
}
}
6 changes: 4 additions & 2 deletions crates/config/src/project/task_options_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ fn validate_interactive<C>(

derive_enum!(
/// The pattern in which affected files will be passed to the affected task.
#[serde(untagged, expecting = "expected `args`, `env`, or a boolean")]
#[serde(expecting = "expected `args`, `env`, or a boolean")]
pub enum TaskOptionAffectedFiles {
/// Passed as command line arguments.
Args,
/// Passed as environment variables.
Env,
/// Passed as command line arguments and environment variables.
#[serde(untagged)]
Enabled(bool),
}
);
Expand Down Expand Up @@ -87,13 +88,14 @@ impl Schematic for TaskOptionEnvFile {

derive_enum!(
/// The pattern in which to run the task automatically in CI.
#[serde(untagged, expecting = "expected `always`, `affected`, or a boolean")]
#[serde(expecting = "expected `always`, `affected`, or a boolean")]
pub enum TaskOptionRunInCI {
/// Always run, regardless of affected.
Always,
/// Only run if affected by touched files.
Affected,
/// Either affected, or don't run at all.
#[serde(untagged)]
Enabled(bool),
}
);
Expand Down
11 changes: 3 additions & 8 deletions crates/config/tests/inherited_tasks_config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ mod utils;

use httpmock::prelude::*;
use moon_common::Id;
use moon_config::{
ConfigLoader, InheritedTasksConfig, InheritedTasksManager, InputPath, LanguageType,
PlatformType, ProjectType, StackType, TaskArgs, TaskConfig, TaskDependency,
TaskDependencyConfig, TaskMergeStrategy, TaskOptionsConfig,
};
use moon_config::*;
use moon_target::Target;
use rustc_hash::FxHashMap;
use schematic::Config;
Expand Down Expand Up @@ -97,7 +93,7 @@ tasks:
TaskConfig {
command: TaskArgs::String("e".to_owned()),
options: TaskOptionsConfig {
run_in_ci: Some(false),
run_in_ci: Some(TaskOptionRunInCI::Enabled(false)),
..TaskOptionsConfig::default()
},
..TaskConfig::default()
Expand Down Expand Up @@ -1111,7 +1107,6 @@ mod task_manager {
mod pkl {
use super::*;
use moon_common::Id;
use moon_config::*;
use starbase_sandbox::locate_fixture;

#[test]
Expand Down Expand Up @@ -1183,7 +1178,7 @@ mod task_manager {
persistent: Some(true),
retry_count: Some(3),
run_deps_in_parallel: Some(false),
run_in_ci: Some(true),
run_in_ci: Some(TaskOptionRunInCI::Enabled(true)),
run_from_workspace_root: Some(false),
shell: Some(false),
timeout: Some(60),
Expand Down
8 changes: 4 additions & 4 deletions crates/task-builder/tests/task_deps_builder_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ mod task_deps_builder {
#[should_panic(expected = "Task project:task cannot depend on task project:no-ci")]
fn errors_if_dep_not_run_in_ci() {
let mut task = create_task();
task.options.run_in_ci = true;
task.options.run_in_ci = TaskOptionRunInCI::Enabled(true);
task.deps
.push(TaskDependencyConfig::new(Target::parse("no-ci").unwrap()));

Expand All @@ -101,7 +101,7 @@ mod task_deps_builder {
FxHashMap::from_iter([(
Target::parse("project:no-ci").unwrap(),
TaskOptions {
run_in_ci: false,
run_in_ci: TaskOptionRunInCI::Enabled(false),
..Default::default()
},
)]),
Expand All @@ -111,7 +111,7 @@ mod task_deps_builder {
#[test]
fn doesnt_errors_if_dep_run_in_ci() {
let mut task = create_task();
task.options.run_in_ci = false;
task.options.run_in_ci = TaskOptionRunInCI::Enabled(false);
task.deps
.push(TaskDependencyConfig::new(Target::parse("ci").unwrap()));

Expand All @@ -120,7 +120,7 @@ mod task_deps_builder {
FxHashMap::from_iter([(
Target::parse("project:ci").unwrap(),
TaskOptions {
run_in_ci: true,
run_in_ci: TaskOptionRunInCI::Enabled(true),
..Default::default()
},
)]),
Expand Down
Loading

0 comments on commit af84e3a

Please sign in to comment.