From c80264cfd849e3fbb4d00fb1cddac46a94d30b76 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Wed, 26 Jul 2023 13:10:22 -0700 Subject: [PATCH] Get tests working. --- ...aliases_test__graph_uses_id_for_nodes.snap | 24 ++-- nextgen/project-graph/src/project_graph.rs | 4 +- nextgen/task-expander/src/tasks_expander.rs | 6 +- .../tests/tasks_expander_test.rs | 123 +++++++++++------- 4 files changed, 93 insertions(+), 64 deletions(-) diff --git a/crates/node/platform/tests/snapshots/project_aliases_test__graph_uses_id_for_nodes.snap b/crates/node/platform/tests/snapshots/project_aliases_test__graph_uses_id_for_nodes.snap index 1fcd4013f74..0a84ee1f237 100644 --- a/crates/node/platform/tests/snapshots/project_aliases_test__graph_uses_id_for_nodes.snap +++ b/crates/node/platform/tests/snapshots/project_aliases_test__graph_uses_id_for_nodes.snap @@ -7,17 +7,17 @@ digraph { 1 [ label="nodeNameOnly" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 2 [ label="node" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 3 [ label="explicitAndImplicit" style=filled, shape=oval, fillcolor=gray, fontcolor=black] - 4 [ label="noLang" style=filled, shape=oval, fillcolor=gray, fontcolor=black] - 5 [ label="implicit" style=filled, shape=oval, fillcolor=gray, fontcolor=black] - 6 [ label="explicit" style=filled, shape=oval, fillcolor=gray, fontcolor=black] - 2 -> 0 [ arrowhead=box, arrowtail=box] - 2 -> 1 [ arrowhead=box, arrowtail=box] - 3 -> 0 [ arrowhead=box, arrowtail=box] - 3 -> 1 [ arrowhead=box, arrowtail=box] - 3 -> 2 [ arrowhead=box, arrowtail=box] - 5 -> 0 [ arrowhead=box, arrowtail=box] - 5 -> 2 [ arrowhead=box, arrowtail=box] - 6 -> 0 [ arrowhead=box, arrowtail=box] - 6 -> 2 [ arrowhead=box, arrowtail=box] + 4 [ label="explicit" style=filled, shape=oval, fillcolor=gray, fontcolor=black] + 5 [ label="noLang" style=filled, shape=oval, fillcolor=gray, fontcolor=black] + 6 [ label="implicit" style=filled, shape=oval, fillcolor=gray, fontcolor=black] + 2 -> 1 [ label="production" arrowhead=box, arrowtail=box] + 2 -> 0 [ label="production" arrowhead=box, arrowtail=box] + 3 -> 0 [ label="production" arrowhead=box, arrowtail=box] + 3 -> 1 [ label="peer" arrowhead=box, arrowtail=box] + 3 -> 2 [ label="development" arrowhead=box, arrowtail=box] + 4 -> 0 [ label="production" arrowhead=box, arrowtail=box] + 4 -> 2 [ label="development" arrowhead=box, arrowtail=box] + 6 -> 0 [ label="development" arrowhead=box, arrowtail=box] + 6 -> 2 [ label="production" arrowhead=box, arrowtail=box] } diff --git a/nextgen/project-graph/src/project_graph.rs b/nextgen/project-graph/src/project_graph.rs index 0901071d95c..690aa039008 100644 --- a/nextgen/project-graph/src/project_graph.rs +++ b/nextgen/project-graph/src/project_graph.rs @@ -236,7 +236,7 @@ impl ProjectGraph { } fn expand_project(&self, id: &Id) -> miette::Result { - debug!(id = id.as_str(), "Expanding project {}", color::id(&id)); + debug!(id = id.as_str(), "Expanding project {}", color::id(id)); let unexpanded_project = self.get_unexpanded(id)?; let mut project = unexpanded_project.to_owned(); @@ -261,7 +261,7 @@ impl ProjectGraph { for task_id in unexpanded_project.tasks.keys() { // Resolve in this order! expander.expand_env(task_id)?; - expander.expand_deps(task_id, &query)?; + expander.expand_deps(task_id, query)?; expander.expand_inputs(task_id)?; expander.expand_outputs(task_id)?; expander.expand_args(task_id)?; diff --git a/nextgen/task-expander/src/tasks_expander.rs b/nextgen/task-expander/src/tasks_expander.rs index 931f15f32ea..0fe0bee5727 100644 --- a/nextgen/task-expander/src/tasks_expander.rs +++ b/nextgen/task-expander/src/tasks_expander.rs @@ -210,7 +210,7 @@ impl<'proj> TasksExpander<'proj> { }; for dep_project in query(input)? { - check_and_push_dep(&dep_project, &dep_target.task_id, true)?; + check_and_push_dep(dep_project, &dep_target.task_id, true)?; } } } @@ -245,7 +245,7 @@ impl<'proj> TasksExpander<'proj> { } for dep_project in results { - check_and_push_dep(&dep_project, &dep_target.task_id, false)?; + check_and_push_dep(dep_project, &dep_target.task_id, false)?; } } } @@ -255,7 +255,7 @@ impl<'proj> TasksExpander<'proj> { if dep_project.id == project.id { // Avoid circular references } else { - check_and_push_dep(&dep_project, &dep_target.task_id, true)?; + check_and_push_dep(dep_project, &dep_target.task_id, true)?; } } } diff --git a/nextgen/task-expander/tests/tasks_expander_test.rs b/nextgen/task-expander/tests/tasks_expander_test.rs index 15051535594..fcf1470ad20 100644 --- a/nextgen/task-expander/tests/tasks_expander_test.rs +++ b/nextgen/task-expander/tests/tasks_expander_test.rs @@ -8,8 +8,7 @@ use moon_task_expander::TasksExpander; use rustc_hash::{FxHashMap, FxHashSet}; use starbase_sandbox::{create_empty_sandbox, create_sandbox}; use std::env; -use std::path::{Path, PathBuf}; -use std::sync::Arc; +use std::path::Path; use utils::{create_project, create_project_with_tasks}; fn create_path_set(inputs: Vec<&str>) -> FxHashSet { @@ -79,7 +78,10 @@ mod tasks_expander { env::remove_var("FOO"); env::remove_var("BAZ_QUX"); - assert_eq!(project.get_task("task").unwrap().command, "./foo//baz-qux"); + assert_eq!( + project.get_task("task").unwrap().command, + "./foo/${BAR}/baz-qux" + ); } #[test] @@ -222,18 +224,42 @@ mod tasks_expander { mod expand_deps { use super::*; - fn no_query<'l>(_: String) -> miette::Result> { - Ok(vec![]) + struct QueryContainer { + projects: Vec, } - fn do_query<'l>(_: String) -> miette::Result> { - let root = PathBuf::from("/root"); + impl QueryContainer { + pub fn new(root: &Path) -> Self { + Self { + projects: vec![ + create_project_with_tasks(root, "foo"), + create_project_with_tasks(root, "bar"), + create_project_with_tasks(root, "baz"), + ], + } + } + + pub fn all(&self, _: String) -> miette::Result> { + Ok(vec![ + &self.projects[0], + &self.projects[1], + &self.projects[2], + ]) + } + + pub fn filtered(&self, input: String) -> miette::Result> { + Ok(vec![if input.contains("foo") { + &self.projects[0] + } else if input.contains("bar") { + &self.projects[1] + } else { + &self.projects[2] + }]) + } - Ok(vec![ - // Arc::new(create_project_with_tasks(&root, "foo")), - // Arc::new(create_project_with_tasks(&root, "bar")), - // Arc::new(create_project_with_tasks(&root, "baz")), - ]) + pub fn none(&self, _: String) -> miette::Result> { + Ok(vec![]) + } } mod all { @@ -246,11 +272,12 @@ mod tasks_expander { fn errors() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); create_expander(sandbox.path(), &mut project, |task| { task.deps.push(Target::parse(":build").unwrap()); }) - .expand_deps("task", do_query) + .expand_deps("task", |i| query.all(i)) .unwrap(); } } @@ -262,11 +289,12 @@ mod tasks_expander { fn no_depends_on() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); create_expander(sandbox.path(), &mut project, |task| { task.deps.push(Target::parse("^:build").unwrap()); }) - .expand_deps("task", no_query) // Tested here + .expand_deps("task", |i| query.none(i)) // Tested here .unwrap(); assert_eq!(project.get_task("task").unwrap().deps, vec![]); @@ -276,8 +304,9 @@ mod tasks_expander { fn returns_tasks_of_same_name() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); - // The valid list comes from `do_query` but we need a + // The valid list comes from `query` but we need a // non-empty set for the expansion to work. project .dependencies @@ -286,7 +315,7 @@ mod tasks_expander { create_expander(sandbox.path(), &mut project, |task| { task.deps.push(Target::parse("^:build").unwrap()); }) - .expand_deps("task", do_query) + .expand_deps("task", |i| query.all(i)) .unwrap(); assert_eq!( @@ -306,8 +335,9 @@ mod tasks_expander { fn errors_for_persistent_chain() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); - // The valid list comes from `do_query` but we need a + // The valid list comes from `query` but we need a // non-empty set for the expansion to work. project .dependencies @@ -317,7 +347,7 @@ mod tasks_expander { task.options.persistent = false; task.deps.push(Target::parse("^:dev").unwrap()); }) - .expand_deps("task", do_query) + .expand_deps("task", |i| query.all(i)) .unwrap(); } } @@ -329,12 +359,13 @@ mod tasks_expander { fn refs_sibling_task() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); create_expander(sandbox.path(), &mut project, |task| { task.deps.push(Target::parse("~:build").unwrap()); task.deps.push(Target::parse("lint").unwrap()); }) - .expand_deps("task", no_query) + .expand_deps("task", |i| query.none(i)) .unwrap(); assert_eq!( @@ -350,11 +381,12 @@ mod tasks_expander { fn ignores_self_ref_cycle() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); create_expander(sandbox.path(), &mut project, |task| { task.deps.push(Target::parse("task").unwrap()); }) - .expand_deps("task", no_query) + .expand_deps("task", |i| query.none(i)) .unwrap(); assert_eq!(project.get_task("task").unwrap().deps, vec![]); @@ -364,12 +396,13 @@ mod tasks_expander { fn ignores_dupes() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); create_expander(sandbox.path(), &mut project, |task| { task.deps.push(Target::parse("~:test").unwrap()); task.deps.push(Target::parse("test").unwrap()); }) - .expand_deps("task", no_query) + .expand_deps("task", |i| query.none(i)) .unwrap(); assert_eq!( @@ -385,11 +418,12 @@ mod tasks_expander { fn errors_unknown_sibling_task() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); create_expander(sandbox.path(), &mut project, |task| { task.deps.push(Target::parse("~:unknown").unwrap()); }) - .expand_deps("task", no_query) + .expand_deps("task", |i| query.none(i)) .unwrap(); } @@ -400,12 +434,13 @@ mod tasks_expander { fn errors_for_persistent_chain() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); create_expander(sandbox.path(), &mut project, |task| { task.options.persistent = false; task.deps.push(Target::parse("~:dev").unwrap()); }) - .expand_deps("task", do_query) + .expand_deps("task", |i| query.all(i)) .unwrap(); } } @@ -417,11 +452,12 @@ mod tasks_expander { fn refs_sibling_task() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); create_expander(sandbox.path(), &mut project, |task| { task.deps.push(Target::parse("project:build").unwrap()); }) - .expand_deps("task", no_query) + .expand_deps("task", |i| query.none(i)) .unwrap(); assert_eq!( @@ -434,11 +470,12 @@ mod tasks_expander { fn ignores_self_ref_cycle() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); create_expander(sandbox.path(), &mut project, |task| { task.deps.push(Target::parse("project:task").unwrap()); }) - .expand_deps("task", no_query) + .expand_deps("task", |i| query.none(i)) .unwrap(); assert_eq!(project.get_task("task").unwrap().deps, vec![]); @@ -448,26 +485,14 @@ mod tasks_expander { fn refs_other_project_tasks() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); create_expander(sandbox.path(), &mut project, |task| { task.deps.push(Target::parse("foo:build").unwrap()); task.deps.push(Target::parse("bar:lint").unwrap()); task.deps.push(Target::parse("baz:test").unwrap()); }) - .expand_deps("task", |input| { - Ok(vec![ - // Arc::new(create_project_with_tasks( - // sandbox.path(), - // if input.contains("foo") { - // "foo" - // } else if input.contains("bar") { - // "bar" - // } else { - // "baz" - // }, - // )) - ]) - }) + .expand_deps("task", |i| query.filtered(i)) .unwrap(); assert_eq!( @@ -487,11 +512,12 @@ mod tasks_expander { fn errors_unknown_task() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); create_expander(sandbox.path(), &mut project, |task| { task.deps.push(Target::parse("foo:unknown").unwrap()); }) - .expand_deps("task", no_query) + .expand_deps("task", |i| query.none(i)) .unwrap(); } @@ -502,12 +528,13 @@ mod tasks_expander { fn errors_for_persistent_chain() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); create_expander(sandbox.path(), &mut project, |task| { task.options.persistent = false; task.deps.push(Target::parse("foo:dev").unwrap()); }) - .expand_deps("task", do_query) + .expand_deps("task", |i| query.all(i)) .unwrap(); } } @@ -519,11 +546,12 @@ mod tasks_expander { fn no_tags() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); create_expander(sandbox.path(), &mut project, |task| { task.deps.push(Target::parse("#tag:build").unwrap()); }) - .expand_deps("task", no_query) // Tested here + .expand_deps("task", |i| query.none(i)) // Tested here .unwrap(); assert_eq!(project.get_task("task").unwrap().deps, vec![]); @@ -533,11 +561,12 @@ mod tasks_expander { fn returns_tasks_of_same_name() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); create_expander(sandbox.path(), &mut project, |task| { task.deps.push(Target::parse("#tag:build").unwrap()); }) - .expand_deps("task", do_query) + .expand_deps("task", |i| query.all(i)) .unwrap(); assert_eq!( @@ -555,13 +584,12 @@ mod tasks_expander { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); - let cloned_project = Arc::new(project.clone()); + let cloned_project = project.clone(); create_expander(sandbox.path(), &mut project, |task| { task.deps.push(Target::parse("#tag:task").unwrap()); }) - // .expand_deps("task", move |_| Ok(vec![cloned_project.clone()])) - .expand_deps("task", move |_| Ok(vec![])) + .expand_deps("task", |_| Ok(vec![&cloned_project])) .unwrap(); assert_eq!(project.get_task("task").unwrap().deps, vec![]); @@ -574,12 +602,13 @@ mod tasks_expander { fn errors_for_persistent_chain() { let sandbox = create_empty_sandbox(); let mut project = create_project_with_tasks(sandbox.path(), "project"); + let query = QueryContainer::new(sandbox.path()); create_expander(sandbox.path(), &mut project, |task| { task.options.persistent = false; task.deps.push(Target::parse("#tag:dev").unwrap()); }) - .expand_deps("task", do_query) + .expand_deps("task", |i| query.all(i)) .unwrap(); } }