diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index f690e974567..ba04ee8c5b1 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -37,7 +37,9 @@ jobs: with: toolchain: 1.65.0 profile: minimal - - uses: Swatinem/rust-cache@v1 + - uses: Swatinem/rust-cache@v2 + with: + shared-key: benchmark - name: Run benchmarks uses: actions-rs/cargo@v1 with: diff --git a/.github/workflows/moon.yml b/.github/workflows/moon.yml index 89b876c3b66..69715c6d0d8 100644 --- a/.github/workflows/moon.yml +++ b/.github/workflows/moon.yml @@ -47,6 +47,8 @@ jobs: toolchain: 1.65.0 profile: minimal - uses: Swatinem/rust-cache@v2 + with: + shared-key: moon - uses: moonrepo/tool-version-action@v1 with: node: ${{ matrix.node-version }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4203943c783..4329154a8c6 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -80,6 +80,8 @@ jobs: target: ${{ matrix.target }} toolchain: stable - uses: Swatinem/rust-cache@v2 + with: + shared-key: moon - name: Generate lockfile uses: actions-rs/cargo@v1 if: ${{ !matrix.docker-target }} @@ -159,6 +161,8 @@ jobs: check-latest: true node-version: 16 - uses: Swatinem/rust-cache@v2 + with: + shared-key: moon - name: Setup toolchain if: ${{ matrix.setup }} run: ${{ matrix.setup }} diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 1a38b2abc47..1b241966eda 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -29,6 +29,8 @@ jobs: profile: minimal components: rustfmt - uses: Swatinem/rust-cache@v2 + with: + shared-key: format - uses: actions-rs/cargo@v1 name: Check formatting with: @@ -50,6 +52,8 @@ jobs: profile: minimal components: clippy - uses: Swatinem/rust-cache@v2 + with: + shared-key: lint - uses: davidB/rust-cargo-make@v1 - name: Run linter run: cargo make lint @@ -70,6 +74,8 @@ jobs: components: llvm-tools-preview - name: Cache cargo uses: Swatinem/rust-cache@v2 + with: + shared-key: test - name: Install cargo-make uses: davidB/rust-cargo-make@v1 - name: Install nextest diff --git a/Cargo.lock b/Cargo.lock index 3a5103036dc..437251d1789 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1828,12 +1828,12 @@ dependencies = [ name = "moon_archive" version = "0.1.0" dependencies = [ - "assert_fs", "criterion", "fake", "flate2", "moon_error", "moon_logger", + "moon_test_utils", "moon_utils", "rand 0.8.5", "rustc-hash", @@ -1847,13 +1847,13 @@ dependencies = [ name = "moon_cache" version = "0.1.0" dependencies = [ - "assert_fs", "filetime", "moon_archive", "moon_constants", "moon_error", "moon_logger", "moon_platform", + "moon_test_utils", "moon_utils", "rustc-hash", "serde", @@ -1865,7 +1865,6 @@ dependencies = [ name = "moon_cli" version = "0.19.1" dependencies = [ - "assert_cmd", "async-recursion", "clap 4.0.23", "clap_lex 0.3.0", @@ -1874,7 +1873,6 @@ dependencies = [ "dialoguer", "futures", "indicatif", - "insta", "itertools", "mimalloc", "moon_archive", @@ -1896,13 +1894,12 @@ dependencies = [ "moon_system_platform", "moon_task", "moon_terminal", + "moon_test_utils", "moon_toolchain", "moon_typescript_lang", "moon_utils", "moon_vcs", "moon_workspace", - "predicates", - "pretty_assertions", "rustc-hash", "serde", "serde_json", @@ -1918,13 +1915,12 @@ dependencies = [ name = "moon_config" version = "0.1.0" dependencies = [ - "assert_fs", "figment", "moon_constants", "moon_error", "moon_node_lang", + "moon_test_utils", "moon_utils", - "pretty_assertions", "reqwest", "rustc-hash", "schemars", @@ -1953,6 +1949,7 @@ dependencies = [ "moon_platform", "moon_project", "moon_task", + "moon_test_utils", "moon_utils", "moon_workspace", "serde", @@ -1973,7 +1970,6 @@ dependencies = [ name = "moon_generator" version = "0.1.0" dependencies = [ - "assert_fs", "convert_case", "futures", "lazy_static", @@ -1981,6 +1977,7 @@ dependencies = [ "moon_constants", "moon_error", "moon_logger", + "moon_test_utils", "moon_utils", "serde_json", "tera", @@ -2023,22 +2020,20 @@ dependencies = [ name = "moon_node_lang" version = "0.1.0" dependencies = [ - "assert_fs", "cached", "content_inspector", "lazy_static", "moon_error", "moon_lang", "moon_logger", + "moon_test_utils", "moon_utils", - "pretty_assertions", "regex", "reqwest", "rustc-hash", "serde", "serde_json", "serde_yaml", - "serial_test", "thiserror", "tokio", "yarn-lock-parser", @@ -2048,7 +2043,6 @@ dependencies = [ name = "moon_node_platform" version = "0.1.0" dependencies = [ - "insta", "lazy_static", "moon_action", "moon_cache", @@ -2064,11 +2058,11 @@ dependencies = [ "moon_runner_context", "moon_task", "moon_terminal", + "moon_test_utils", "moon_toolchain", "moon_typescript_lang", "moon_utils", "moon_workspace", - "pretty_assertions", "rustc-hash", "serde", "tokio", @@ -2118,8 +2112,8 @@ dependencies = [ "moon_error", "moon_logger", "moon_task", + "moon_test_utils", "moon_utils", - "pretty_assertions", "rustc-hash", "serde", "strum", @@ -2131,7 +2125,6 @@ name = "moon_project_graph" version = "0.1.0" dependencies = [ "criterion", - "insta", "moon_cache", "moon_config", "moon_error", @@ -2140,6 +2133,7 @@ dependencies = [ "moon_platform", "moon_project", "moon_task", + "moon_test_utils", "moon_utils", "petgraph", "rustc-hash", @@ -2154,7 +2148,6 @@ dependencies = [ "async-trait", "console", "criterion", - "insta", "moon_action", "moon_cache", "moon_config", @@ -2172,6 +2165,7 @@ dependencies = [ "moon_system_platform", "moon_task", "moon_terminal", + "moon_test_utils", "moon_toolchain", "moon_utils", "moon_vcs", @@ -2217,6 +2211,7 @@ dependencies = [ "moon_config", "moon_error", "moon_logger", + "moon_test_utils", "moon_utils", "rustc-hash", "serde", @@ -2235,6 +2230,22 @@ dependencies = [ "regex", ] +[[package]] +name = "moon_test_utils" +version = "0.1.0" +dependencies = [ + "assert_cmd", + "assert_fs", + "clean-path", + "dirs", + "insta", + "moon_config", + "predicates", + "pretty_assertions", + "rustc-hash", + "serde_yaml", +] + [[package]] name = "moon_toolchain" version = "0.1.0" @@ -2262,6 +2273,7 @@ dependencies = [ "moon_error", "moon_lang", "moon_logger", + "moon_test_utils", "moon_utils", "serde", "serde_json", @@ -2272,8 +2284,6 @@ dependencies = [ name = "moon_utils" version = "0.1.0" dependencies = [ - "assert_cmd", - "assert_fs", "async-recursion", "cached", "chrono", @@ -2284,13 +2294,13 @@ dependencies = [ "ec4rs", "futures", "humantime", - "insta", "json_comments", "lazy_static", "md5", "moon_constants", "moon_error", "moon_logger", + "moon_test_utils", "pathdiff", "regex", "semver", @@ -2311,6 +2321,7 @@ dependencies = [ "ignore", "moon_config", "moon_error", + "moon_test_utils", "moon_utils", "regex", "rustc-hash", diff --git a/Cargo.toml b/Cargo.toml index 6a36b8ad78d..17cee7fb522 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,6 @@ default-members = ["crates/cli"] cached = "0.40.0" clap = "4.0.23" criterion = { version = "0.4.0", features = ["async_tokio"] } -insta = "1.21.1" reqwest = "0.11.12" rustc-hash = "1.1.0" serde = { version = "1.0.145", features = ["derive"] } diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 1a24d874902..f7d73bb46a9 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -56,9 +56,6 @@ tokio = { workspace = true } moon_archive = { path = "../core/archive" } moon_cache = { path = "../core/cache" } moon_notifier = { path = "../core/notifier" } -assert_cmd = "2.0.6" -insta = { workspace = true } -predicates = "2.1.3" -pretty_assertions = "1.3.0" +moon_test_utils = { path = "../core/test-utils" } serial_test = "0.9.0" wiremock = "0.5.15" diff --git a/crates/cli/src/commands/init/mod.rs b/crates/cli/src/commands/init/mod.rs index 7de792bb832..828b588f150 100644 --- a/crates/cli/src/commands/init/mod.rs +++ b/crates/cli/src/commands/init/mod.rs @@ -229,7 +229,7 @@ pub async fn init( #[cfg(test)] mod tests { use super::*; - use insta::assert_snapshot; + use moon_test_utils::assert_snapshot; #[test] fn renders_default() { diff --git a/crates/cli/src/commands/init/node.rs b/crates/cli/src/commands/init/node.rs index fef9d576eb2..8cb8a7cadee 100644 --- a/crates/cli/src/commands/init/node.rs +++ b/crates/cli/src/commands/init/node.rs @@ -235,7 +235,7 @@ pub async fn init_node( #[cfg(test)] mod tests { use super::*; - use insta::assert_snapshot; + use moon_test_utils::assert_snapshot; #[test] fn renders_default() { diff --git a/crates/cli/src/commands/init/typescript.rs b/crates/cli/src/commands/init/typescript.rs index 9b05fade5c8..eeb816dc12b 100644 --- a/crates/cli/src/commands/init/typescript.rs +++ b/crates/cli/src/commands/init/typescript.rs @@ -59,7 +59,7 @@ pub async fn init_typescript( #[cfg(test)] mod tests { use super::*; - use insta::assert_snapshot; + use moon_test_utils::assert_snapshot; #[test] fn renders_default() { diff --git a/crates/cli/tests/bin_test.rs b/crates/cli/tests/bin_test.rs index 043365e56d7..c13a3599e7c 100644 --- a/crates/cli/tests/bin_test.rs +++ b/crates/cli/tests/bin_test.rs @@ -1,5 +1,6 @@ -use moon_utils::test::{create_moon_command, create_sandbox}; -use predicates::prelude::*; +use moon_test_utils::{ + create_sandbox_with_config, get_cases_fixture_configs, predicates::prelude::*, +}; // This requires installing the toolchain which is quite heavy in tests! // #[test] @@ -15,12 +16,17 @@ use predicates::prelude::*; #[test] fn invalid_tool() { - let fixture = create_sandbox("cases"); + let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); + let sandbox = create_sandbox_with_config( + "cases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("bin") - .arg("unknown") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("bin").arg("unknown"); + }); assert .failure() @@ -33,13 +39,19 @@ fn invalid_tool() { #[test] fn not_configured() { - let fixture = create_sandbox("cases"); + let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); + let sandbox = create_sandbox_with_config( + "cases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("bin") - .arg("yarn") - .env("MOON_NODE_VERSION", "17.0.0") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("bin") + .arg("yarn") + .env("MOON_NODE_VERSION", "17.0.0"); + }); assert.failure().code(1).stdout(""); } diff --git a/crates/cli/tests/check_test.rs b/crates/cli/tests/check_test.rs index 03fcff51d24..c93d818aa41 100644 --- a/crates/cli/tests/check_test.rs +++ b/crates/cli/tests/check_test.rs @@ -1,16 +1,22 @@ -use moon_utils::test::{create_moon_command, create_sandbox_with_git, get_assert_output}; -use predicates::prelude::*; +use moon_test_utils::{ + create_sandbox_with_config, get_cases_fixture_configs, predicates::prelude::*, +}; #[test] fn runs_tasks_in_project() { - let fixture = create_sandbox_with_git("cases"); + let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); + let sandbox = create_sandbox_with_config( + "cases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("check") - .arg("base") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("check").arg("base"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains("base:base").eval(&output)); assert!(predicate::str::contains("base:runFromProject").eval(&output)); @@ -20,13 +26,20 @@ fn runs_tasks_in_project() { #[test] fn runs_tasks_in_project_using_cwd() { - let fixture = create_sandbox_with_git("cases"); + let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); + let sandbox = create_sandbox_with_config( + "cases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path().join("base")) - .arg("check") - .assert(); + let cwd = sandbox.path().join("base"); + let assert = sandbox.run_moon(|cmd| { + cmd.current_dir(cwd).arg("check"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains("base:base").eval(&output)); assert!(predicate::str::contains("base:runFromProject").eval(&output)); @@ -36,15 +49,19 @@ fn runs_tasks_in_project_using_cwd() { #[test] fn runs_tasks_from_multiple_project() { - let fixture = create_sandbox_with_git("cases"); + let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); + let sandbox = create_sandbox_with_config( + "cases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("check") - .arg("base") - .arg("noop") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("check").arg("base").arg("noop"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains("base:base").eval(&output)); assert!(predicate::str::contains("base:runFromProject").eval(&output)); @@ -58,22 +75,42 @@ fn runs_tasks_from_multiple_project() { #[test] fn runs_for_all_projects_even_when_not_in_root_dir() { - let fixture = create_sandbox_with_git("cases"); - let assert = create_moon_command(fixture.path().join("base")) - .arg("check") - .arg("--all") - .assert(); - assert.stderr(predicate::str::contains("all projects")); + let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); + let sandbox = create_sandbox_with_config( + "cases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + let cwd = sandbox.path().join("base"); + + let assert = sandbox.run_moon(|cmd| { + cmd.current_dir(cwd).arg("check").arg("--all"); + }); + + assert + .inner + .stderr(predicate::str::contains("all projects")); } #[test] fn runs_on_all_projects_from_root_directory() { - let fixture = create_sandbox_with_git("cases"); - let assert = create_moon_command(fixture.path()) - .arg("check") - .arg("--all") - .assert(); - assert.stderr(predicate::str::contains("all projects")); + let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); + let sandbox = create_sandbox_with_config( + "cases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("check").arg("--all"); + }); + + assert + .inner + .stderr(predicate::str::contains("all projects")); } mod reports { @@ -81,26 +118,35 @@ mod reports { #[test] fn does_not_create_a_report_by_default() { - let fixture = create_sandbox_with_git("cases"); - - create_moon_command(fixture.path()) - .arg("check") - .arg("base") - .assert(); - - assert!(!fixture.path().join(".moon/cache/runReport.json").exists()); + let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); + let sandbox = create_sandbox_with_config( + "cases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + sandbox.run_moon(|cmd| { + cmd.arg("check").arg("base"); + }); + + assert!(!sandbox.path().join(".moon/cache/runReport.json").exists()); } #[test] fn creates_report_when_option_passed() { - let fixture = create_sandbox_with_git("cases"); - - create_moon_command(fixture.path()) - .arg("check") - .arg("base") - .arg("--report") - .assert(); - - assert!(fixture.path().join(".moon/cache/runReport.json").exists()); + let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); + let sandbox = create_sandbox_with_config( + "cases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + sandbox.run_moon(|cmd| { + cmd.arg("check").arg("base").arg("--report"); + }); + + assert!(sandbox.path().join(".moon/cache/runReport.json").exists()); } } diff --git a/crates/cli/tests/dep_graph_test.rs b/crates/cli/tests/dep_graph_test.rs index 2d2cc89cac3..d5fa650a7c4 100644 --- a/crates/cli/tests/dep_graph_test.rs +++ b/crates/cli/tests/dep_graph_test.rs @@ -1,14 +1,24 @@ -use insta::assert_snapshot; -use moon_utils::test::{create_moon_command, create_sandbox, get_assert_output}; +use moon_test_utils::{ + assert_snapshot, create_sandbox_with_config, get_project_graph_aliases_fixture_configs, + get_tasks_fixture_configs, +}; #[test] fn all_by_default() { - let fixture = create_sandbox("tasks"); + let (workspace_config, toolchain_config, projects_config) = get_tasks_fixture_configs(); - let assert = create_moon_command(fixture.path()) - .arg("dep-graph") - .assert(); - let dot = get_assert_output(&assert); + let sandbox = create_sandbox_with_config( + "tasks", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("dep-graph"); + }); + + let dot = assert.output(); // Snapshot is not deterministic assert_eq!(dot.split('\n').count(), 283); @@ -16,38 +26,56 @@ fn all_by_default() { #[test] fn focused_by_target() { - let fixture = create_sandbox("tasks"); + let (workspace_config, toolchain_config, projects_config) = get_tasks_fixture_configs(); - let assert = create_moon_command(fixture.path()) - .arg("dep-graph") - .arg("basic:lint") - .assert(); + let sandbox = create_sandbox_with_config( + "tasks", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - assert_snapshot!(get_assert_output(&assert)); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("dep-graph").arg("basic:lint"); + }); + + assert_snapshot!(assert.output()); } #[test] fn includes_dependencies_when_focused() { - let fixture = create_sandbox("tasks"); + let (workspace_config, toolchain_config, projects_config) = get_tasks_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "tasks", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("dep-graph") - .arg("chain:e") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("dep-graph").arg("chain:e"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn includes_dependents_when_focused() { - let fixture = create_sandbox("tasks"); + let (workspace_config, toolchain_config, projects_config) = get_tasks_fixture_configs(); - let assert = create_moon_command(fixture.path()) - .arg("dep-graph") - .arg("basic:build") - .assert(); + let sandbox = create_sandbox_with_config( + "tasks", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - assert_snapshot!(get_assert_output(&assert)); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("dep-graph").arg("basic:build"); + }); + + assert_snapshot!(assert.output()); } mod aliases { @@ -55,25 +83,39 @@ mod aliases { #[test] fn can_focus_using_an_alias() { - let fixture = create_sandbox("project-graph/aliases"); + let (workspace_config, toolchain_config, projects_config) = + get_project_graph_aliases_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "project-graph/aliases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("dep-graph") - .arg("@scope/pkg-foo:test") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("dep-graph").arg("@scope/pkg-foo:test"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn resolves_aliases_in_task_deps() { - let fixture = create_sandbox("project-graph/aliases"); + let (workspace_config, toolchain_config, projects_config) = + get_project_graph_aliases_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "project-graph/aliases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("dep-graph") - .arg("node:aliasDeps") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("dep-graph").arg("node:aliasDeps"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } } diff --git a/crates/cli/tests/docker_test.rs b/crates/cli/tests/docker_test.rs index db8c808830f..0fa3a63ecea 100644 --- a/crates/cli/tests/docker_test.rs +++ b/crates/cli/tests/docker_test.rs @@ -1,8 +1,8 @@ use moon_cli::commands::docker::DockerManifest; -use moon_utils::test::{ - create_moon_command, create_sandbox, create_sandbox_with_git, get_assert_output, +use moon_test_utils::{ + create_sandbox_with_config, get_cases_fixture_configs, get_node_depman_fixture_configs, + get_node_fixture_configs, get_projects_fixture_configs, predicates::prelude::*, }; -use predicates::prelude::*; use rustc_hash::FxHashSet; use std::{fs, path::Path}; @@ -23,15 +23,20 @@ mod scaffold_workspace { #[test] fn copies_all_manifests() { - let fixture = create_sandbox_with_git("node"); + let (workspace_config, toolchain_config, projects_config) = get_node_fixture_configs(); - create_moon_command(fixture.path()) - .arg("docker") - .arg("scaffold") - .arg("esbuild") - .assert(); + let sandbox = create_sandbox_with_config( + "node", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + sandbox.run_moon(|cmd| { + cmd.arg("docker").arg("scaffold").arg("esbuild"); + }); - let docker = fixture.join(".moon/docker/workspace"); + let docker = sandbox.path().join(".moon/docker/workspace"); assert!(docker.join("esbuild/package.json").exists()); assert!(docker.join("lifecycles/package.json").exists()); @@ -41,15 +46,20 @@ mod scaffold_workspace { #[test] fn copies_moon_configs() { - let fixture = create_sandbox_with_git("node"); + let (workspace_config, toolchain_config, projects_config) = get_node_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "node", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - create_moon_command(fixture.path()) - .arg("docker") - .arg("scaffold") - .arg("lifecycles") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("docker").arg("scaffold").arg("lifecycles"); + }); - let docker = fixture.join(".moon/docker/workspace"); + let docker = sandbox.path().join(".moon/docker/workspace"); assert!(docker.join(".moon/project.yml").exists()); assert!(docker.join(".moon/toolchain.yml").exists()); @@ -58,15 +68,20 @@ mod scaffold_workspace { #[test] fn copies_node_postinstalls() { - let fixture = create_sandbox_with_git("node"); + let (workspace_config, toolchain_config, projects_config) = get_node_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "node", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - create_moon_command(fixture.path()) - .arg("docker") - .arg("scaffold") - .arg("lifecycles") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("docker").arg("scaffold").arg("lifecycles"); + }); - let docker = fixture.join(".moon/docker/workspace"); + let docker = sandbox.path().join(".moon/docker/workspace"); assert!(docker.join("lifecycles/package.json").exists()); assert!(docker.join("lifecycles/postinstall.mjs").exists()); @@ -74,30 +89,42 @@ mod scaffold_workspace { #[test] fn copies_npm_files() { - let fixture = create_sandbox_with_git("node-npm"); + let (workspace_config, toolchain_config, projects_config) = + get_node_depman_fixture_configs("npm"); + + let sandbox = create_sandbox_with_config( + "node-npm", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - create_moon_command(fixture.path()) - .arg("docker") - .arg("scaffold") - .arg("npm") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("docker").arg("scaffold").arg("npm"); + }); - let docker = fixture.join(".moon/docker/workspace"); + let docker = sandbox.path().join(".moon/docker/workspace"); assert!(docker.join("package-lock.json").exists()); } #[test] fn copies_pnpm_files() { - let fixture = create_sandbox_with_git("node-pnpm"); + let (workspace_config, toolchain_config, projects_config) = + get_node_depman_fixture_configs("pnpm"); + + let sandbox = create_sandbox_with_config( + "node-pnpm", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - create_moon_command(fixture.path()) - .arg("docker") - .arg("scaffold") - .arg("pnpm") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("docker").arg("scaffold").arg("pnpm"); + }); - let docker = fixture.join(".moon/docker/workspace"); + let docker = sandbox.path().join(".moon/docker/workspace"); assert!(docker.join("pnpm-lock.yaml").exists()); assert!(docker.join("pnpm-workspace.yaml").exists()); @@ -105,15 +132,21 @@ mod scaffold_workspace { #[test] fn copies_yarn_files() { - let fixture = create_sandbox_with_git("node-yarn"); + let (workspace_config, toolchain_config, projects_config) = + get_node_depman_fixture_configs("yarn"); + + let sandbox = create_sandbox_with_config( + "node-yarn", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - create_moon_command(fixture.path()) - .arg("docker") - .arg("scaffold") - .arg("yarn") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("docker").arg("scaffold").arg("yarn"); + }); - let docker = fixture.join(".moon/docker/workspace"); + let docker = sandbox.path().join(".moon/docker/workspace"); assert!(docker.join(".yarnrc.yml").exists()); assert!(docker.join("yarn.lock").exists()); @@ -121,15 +154,21 @@ mod scaffold_workspace { #[test] fn copies_yarn1_files() { - let fixture = create_sandbox_with_git("node-yarn1"); + let (workspace_config, toolchain_config, projects_config) = + get_node_depman_fixture_configs("yarn1"); + + let sandbox = create_sandbox_with_config( + "node-yarn1", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - create_moon_command(fixture.path()) - .arg("docker") - .arg("scaffold") - .arg("yarn") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("docker").arg("scaffold").arg("yarn1"); + }); - let docker = fixture.join(".moon/docker/workspace"); + let docker = sandbox.path().join(".moon/docker/workspace"); assert!(docker.join("yarn.lock").exists()); } @@ -140,15 +179,20 @@ mod scaffold_sources { #[test] fn copies_project_and_deps() { - let fixture = create_sandbox_with_git("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - create_moon_command(fixture.path()) - .arg("docker") - .arg("scaffold") - .arg("basic") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("docker").arg("scaffold").arg("basic"); + }); - let docker = fixture.join(".moon/docker/sources"); + let docker = sandbox.path().join(".moon/docker/sources"); assert!(docker.join("basic/file.ts").exists()); assert!(docker.join("no-config/empty").exists()); @@ -162,16 +206,20 @@ mod scaffold_sources { #[test] fn copies_multiple_projects() { - let fixture = create_sandbox_with_git("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - create_moon_command(fixture.path()) - .arg("docker") - .arg("scaffold") - .arg("js") - .arg("bar") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("docker").arg("scaffold").arg("js").arg("bar"); + }); - let docker = fixture.join(".moon/docker/sources"); + let docker = sandbox.path().join(".moon/docker/sources"); assert!(docker.join("langs/js").exists()); assert!(docker.join("deps/bar").exists()); @@ -185,20 +233,27 @@ mod scaffold_sources { #[test] fn can_include_more_files() { - let fixture = create_sandbox_with_git("cases"); + let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); - create_moon_command(fixture.path()) - .arg("docker") - .arg("scaffold") - .arg("base") - // Janky but works - .arg("--include") - .arg("outputs/generate.js") - .arg("--include") - .arg("passthrough-args/*.sh") - .assert(); + let sandbox = create_sandbox_with_config( + "cases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + sandbox.run_moon(|cmd| { + cmd.arg("docker") + .arg("scaffold") + .arg("base") + // Janky but works + .arg("--include") + .arg("outputs/generate.js") + .arg("--include") + .arg("passthrough-args/*.sh"); + }); - let docker = fixture.join(".moon/docker/sources"); + let docker = sandbox.path().join(".moon/docker/sources"); assert!(docker.join("base").exists()); assert!(docker.join("outputs/generate.js").exists()); @@ -214,16 +269,22 @@ mod prune { #[test] fn errors_missing_manifest() { - let fixture = create_sandbox("node"); + let (workspace_config, toolchain_config, projects_config) = get_node_fixture_configs(); - let assert = create_moon_command(fixture.path()) - .arg("docker") - .arg("prune") - .assert(); + let sandbox = create_sandbox_with_config( + "node", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("docker").arg("prune"); + }); assert!( predicate::str::contains("Unable to prune, docker manifest missing. Has it been scaffolded with `moon docker scaffold`?") - .eval(&get_assert_output(&assert)) + .eval(&assert.output()) ); } } @@ -233,97 +294,125 @@ mod prune_node { #[test] fn focuses_for_npm() { - let fixture = create_sandbox("node-npm"); + let (workspace_config, toolchain_config, projects_config) = + get_node_depman_fixture_configs("npm"); + + let sandbox = create_sandbox_with_config( + "node-npm", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - write_manifest(fixture.path(), "other"); + write_manifest(sandbox.path(), "other"); - create_moon_command(fixture.path()) - .arg("docker") - .arg("prune") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("docker").arg("prune"); + }); // should exist - assert!(fixture.path().join("node_modules/solid-js").exists()); + assert!(sandbox.path().join("node_modules/solid-js").exists()); // should not exist - assert!(!fixture.path().join("npm/node_modules").exists()); - assert!(!fixture + assert!(!sandbox.path().join("npm/node_modules").exists()); + assert!(!sandbox .path() .join("node_modules/babel-preset-solid") .exists()); // npm installs prod deps for unfocused - // assert!(!fixture.path().join("node_modules/react").exists()); + // assert!(!sandbox.path().join("node_modules/react").exists()); } #[test] fn focuses_for_pnpm() { - let fixture = create_sandbox("node-pnpm"); + let (workspace_config, toolchain_config, projects_config) = + get_node_depman_fixture_configs("pnpm"); + + let sandbox = create_sandbox_with_config( + "node-pnpm", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - write_manifest(fixture.path(), "other"); + write_manifest(sandbox.path(), "other"); - create_moon_command(fixture.path()) - .arg("docker") - .arg("prune") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("docker").arg("prune"); + }); // should exist - assert!(fixture.path().join("other/node_modules/solid-js").exists()); + assert!(sandbox.path().join("other/node_modules/solid-js").exists()); // should not exist - assert!(!fixture.path().join("pnpm/node_modules").exists()); - assert!(!fixture + assert!(!sandbox.path().join("pnpm/node_modules").exists()); + assert!(!sandbox .path() .join("node_modules/babel-preset-solid") .exists()); - assert!(!fixture.path().join("node_modules/react").exists()); + assert!(!sandbox.path().join("node_modules/react").exists()); } #[test] fn focuses_for_yarn() { - let fixture = create_sandbox("node-yarn"); + let (workspace_config, toolchain_config, projects_config) = + get_node_depman_fixture_configs("yarn"); + + let sandbox = create_sandbox_with_config( + "node-yarn", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - write_manifest(fixture.path(), "other"); + write_manifest(sandbox.path(), "other"); - create_moon_command(fixture.path()) - .arg("docker") - .arg("prune") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("docker").arg("prune"); + }); // should exist - assert!(fixture.path().join("node_modules/solid-js").exists()); + assert!(sandbox.path().join("node_modules/solid-js").exists()); // should not exist - assert!(!fixture.path().join("npm/node_modules").exists()); - assert!(!fixture + assert!(!sandbox.path().join("npm/node_modules").exists()); + assert!(!sandbox .path() .join("node_modules/babel-preset-solid") .exists()); - assert!(!fixture.path().join("node_modules/react").exists()); + assert!(!sandbox.path().join("node_modules/react").exists()); } #[test] fn focuses_for_yarn1() { - let fixture = create_sandbox("node-yarn1"); + let (workspace_config, toolchain_config, projects_config) = + get_node_depman_fixture_configs("yarn1"); + + let sandbox = create_sandbox_with_config( + "node-yarn1", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - write_manifest(fixture.path(), "other"); + write_manifest(sandbox.path(), "other"); - create_moon_command(fixture.path()) - .arg("docker") - .arg("prune") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("docker").arg("prune"); + }); // should exist - assert!(fixture.path().join("node_modules/solid-js").exists()); + assert!(sandbox.path().join("node_modules/solid-js").exists()); // should not exist - assert!(!fixture.path().join("yarn/node_modules").exists()); - assert!(!fixture + assert!(!sandbox.path().join("yarn/node_modules").exists()); + assert!(!sandbox .path() .join("node_modules/babel-preset-solid") .exists()); // yarn 1 does not support focusing - // assert!(!fixture.path().join("node_modules/react").exists()); + // assert!(!sandbox.path().join("node_modules/react").exists()); } } diff --git a/crates/cli/tests/generate_test.rs b/crates/cli/tests/generate_test.rs index 8a572a1ec78..9519695011f 100644 --- a/crates/cli/tests/generate_test.rs +++ b/crates/cli/tests/generate_test.rs @@ -1,229 +1,224 @@ -use insta::assert_snapshot; -use moon_utils::test::{create_moon_command, create_sandbox, get_assert_output}; -use predicates::prelude::*; +use moon_test_utils::{ + assert_snapshot, create_sandbox_with_config, predicates::prelude::*, Sandbox, +}; use std::fs; -fn get_path_safe_output(assert: &assert_cmd::assert::Assert) -> String { - get_assert_output(assert).replace('\\', "/") +fn generate_sandbox() -> Sandbox { + create_sandbox_with_config("generator", None, None, None) } #[test] fn creates_a_new_template() { - let fixture = create_sandbox("generator"); + let sandbox = generate_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("generate") - .arg("new-name") - .arg("--template") - .assert(); - let output = get_path_safe_output(&assert); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("generate").arg("new-name").arg("--template"); + }); + + let output = assert.output(); assert!(predicate::str::contains("Created a new template new-name at").eval(&output)); - assert!(fixture.path().join("templates/new-name").exists()); + assert!(sandbox.path().join("templates/new-name").exists()); assert.success(); } #[test] fn generates_files_from_template() { - let fixture = create_sandbox("generator"); + let sandbox = generate_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("generate") - .arg("standard") - .arg("./test") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("generate").arg("standard").arg("./test"); + }); - assert_snapshot!(get_path_safe_output(&assert)); + assert_snapshot!(assert.output_standardized()); - assert!(fixture.path().join("test").exists()); - assert!(fixture.path().join("test/file.ts").exists()); - assert!(fixture.path().join("test/folder/nested-file.ts").exists()); - assert!(!fixture.path().join("test/template.yml").exists()); + assert!(sandbox.path().join("test").exists()); + assert!(sandbox.path().join("test/file.ts").exists()); + assert!(sandbox.path().join("test/folder/nested-file.ts").exists()); + assert!(!sandbox.path().join("test/template.yml").exists()); } #[test] fn doesnt_generate_files_when_dryrun() { - let fixture = create_sandbox("generator"); + let sandbox = generate_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("generate") - .arg("standard") - .arg("./test") - .arg("--dryRun") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("generate") + .arg("standard") + .arg("./test") + .arg("--dryRun"); + }); - assert_snapshot!(get_path_safe_output(&assert)); + assert_snapshot!(assert.output_standardized()); - assert!(!fixture.path().join("test").exists()); - assert!(!fixture.path().join("test/file.ts").exists()); - assert!(!fixture.path().join("test/folder/nested-file.ts").exists()); - assert!(!fixture.path().join("test/template.yml").exists()); + assert!(!sandbox.path().join("test").exists()); + assert!(!sandbox.path().join("test/file.ts").exists()); + assert!(!sandbox.path().join("test/folder/nested-file.ts").exists()); + assert!(!sandbox.path().join("test/template.yml").exists()); } #[test] fn overwrites_existing_files_when_forced() { - let fixture = create_sandbox("generator"); - - create_moon_command(fixture.path()) - .arg("generate") - .arg("standard") - .arg("./test") - .assert(); - - let assert = create_moon_command(fixture.path()) - .arg("generate") - .arg("standard") - .arg("./test") - .arg("--force") - .assert(); - - assert_snapshot!(get_path_safe_output(&assert)); - - assert!(fixture.path().join("test").exists()); - assert!(fixture.path().join("test/file.ts").exists()); - assert!(fixture.path().join("test/folder/nested-file.ts").exists()); - assert!(!fixture.path().join("test/template.yml").exists()); + let sandbox = generate_sandbox(); + + sandbox.run_moon(|cmd| { + cmd.arg("generate").arg("standard").arg("./test"); + }); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("generate") + .arg("standard") + .arg("./test") + .arg("--force"); + }); + + assert_snapshot!(assert.output_standardized()); + + assert!(sandbox.path().join("test").exists()); + assert!(sandbox.path().join("test/file.ts").exists()); + assert!(sandbox.path().join("test/folder/nested-file.ts").exists()); + assert!(!sandbox.path().join("test/template.yml").exists()); } #[test] fn overwrites_existing_files_when_interpolated_path() { - let fixture = create_sandbox("generator"); + let sandbox = generate_sandbox(); - create_moon_command(fixture.path()) - .arg("generate") - .arg("vars") - .arg("./test") - .arg("--defaults") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("generate") + .arg("vars") + .arg("./test") + .arg("--defaults"); + }); - let assert = create_moon_command(fixture.path()) - .arg("generate") - .arg("vars") - .arg("./test") - .arg("--defaults") - .arg("--force") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("generate") + .arg("vars") + .arg("./test") + .arg("--defaults") + .arg("--force"); + }); - assert_snapshot!(get_path_safe_output(&assert)); + assert_snapshot!(assert.output_standardized()); // file-[stringNotEmpty]-[number].txt - assert!(fixture.path().join("./test/file-default-0.txt").exists()); + assert!(sandbox.path().join("./test/file-default-0.txt").exists()); } #[test] fn renders_and_interpolates_templates() { - let fixture = create_sandbox("generator"); + let sandbox = generate_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("generate") - .arg("vars") - .arg("./test") - .arg("--defaults") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("generate") + .arg("vars") + .arg("./test") + .arg("--defaults"); + }); assert.success(); - assert_snapshot!(fs::read_to_string(fixture.path().join("./test/expressions.txt")).unwrap()); - assert_snapshot!(fs::read_to_string(fixture.path().join("./test/control.txt")).unwrap()); + assert_snapshot!(fs::read_to_string(sandbox.path().join("./test/expressions.txt")).unwrap()); + assert_snapshot!(fs::read_to_string(sandbox.path().join("./test/control.txt")).unwrap()); } #[test] fn renders_with_custom_vars_via_args() { - let fixture = create_sandbox("generator"); - - let assert = create_moon_command(fixture.path()) - .arg("generate") - .arg("vars") - .arg("./test") - .arg("--defaults") - .arg("--") - .args([ - "--no-boolTrue", - "--boolFalse", - "--string=abc", - "--stringNotEmpty", - "xyz", - "--number=123", - "--numberNotEmpty", - "456", - "--enum=c", - "--multenumNotEmpty", - "a", - ]) - .assert(); + let sandbox = generate_sandbox(); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("generate") + .arg("vars") + .arg("./test") + .arg("--defaults") + .arg("--") + .args([ + "--no-boolTrue", + "--boolFalse", + "--string=abc", + "--stringNotEmpty", + "xyz", + "--number=123", + "--numberNotEmpty", + "456", + "--enum=c", + "--multenumNotEmpty", + "a", + ]); + }); assert.success(); - assert_snapshot!(fs::read_to_string(fixture.path().join("./test/expressions.txt")).unwrap()); - assert_snapshot!(fs::read_to_string(fixture.path().join("./test/control.txt")).unwrap()); + assert_snapshot!(fs::read_to_string(sandbox.path().join("./test/expressions.txt")).unwrap()); + assert_snapshot!(fs::read_to_string(sandbox.path().join("./test/control.txt")).unwrap()); } #[test] fn interpolates_destination_path() { - let fixture = create_sandbox("generator"); + let sandbox = generate_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("generate") - .arg("vars") - .arg("./test") - .arg("--defaults") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("generate") + .arg("vars") + .arg("./test") + .arg("--defaults"); + }); // Verify output paths are correct - assert_snapshot!(get_path_safe_output(&assert)); + assert_snapshot!(assert.output_standardized()); // file-[stringNotEmpty]-[number].txt - assert!(fixture.path().join("./test/file-default-0.txt").exists()); + assert!(sandbox.path().join("./test/file-default-0.txt").exists()); } #[test] fn errors_when_parsing_custom_var_types() { - let fixture = create_sandbox("generator"); - - let assert = create_moon_command(fixture.path()) - .arg("generate") - .arg("vars") - .arg("./test") - .arg("--defaults") - .arg("--") - .arg("--number=abc") - .assert(); - - assert_snapshot!(get_path_safe_output(&assert)); + let sandbox = generate_sandbox(); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("generate") + .arg("vars") + .arg("./test") + .arg("--defaults") + .arg("--") + .arg("--number=abc"); + }); + + assert_snapshot!(assert.output_standardized()); } #[test] fn supports_custom_filters() { - let fixture = create_sandbox("generator"); + let sandbox = generate_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("generate") - .arg("vars") - .arg("./test") - .arg("--defaults") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("generate") + .arg("vars") + .arg("./test") + .arg("--defaults"); + }); assert.success(); - assert_snapshot!(fs::read_to_string(fixture.path().join("./test/filters.txt")).unwrap()); + assert_snapshot!(fs::read_to_string(sandbox.path().join("./test/filters.txt")).unwrap()); } #[test] fn supports_tera_twig_exts() { - let fixture = create_sandbox("generator"); + let sandbox = generate_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("generate") - .arg("extensions") - .arg("./test") - .arg("--defaults") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("generate") + .arg("extensions") + .arg("./test") + .arg("--defaults"); + }); assert.success(); - let tera = fixture.path().join("./test/file.ts"); - let twig = fixture.path().join("./test/file.tsx"); + let tera = sandbox.path().join("./test/file.ts"); + let twig = sandbox.path().join("./test/file.tsx"); assert!(tera.exists()); assert!(twig.exists()); @@ -243,76 +238,75 @@ mod frontmatter { #[test] fn changes_dest_path() { - let fixture = create_sandbox("generator"); + let sandbox = generate_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("generate") - .arg("frontmatter") - .arg("./test") - .arg("--defaults") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("generate") + .arg("frontmatter") + .arg("./test") + .arg("--defaults"); + }); assert.success(); - assert!(!fixture.path().join("./test/to.txt").exists()); - assert!(fixture.path().join("./test/to-NEW.txt").exists()); - assert_snapshot!(fs::read_to_string(fixture.path().join("./test/to-NEW.txt")).unwrap()); + assert!(!sandbox.path().join("./test/to.txt").exists()); + assert!(sandbox.path().join("./test/to-NEW.txt").exists()); + assert_snapshot!(fs::read_to_string(sandbox.path().join("./test/to-NEW.txt")).unwrap()); } #[test] fn force_writes_file() { - let fixture = create_sandbox("generator"); + let sandbox = generate_sandbox(); - fs::create_dir_all(fixture.path().join("test")).unwrap(); - fs::write(fixture.path().join("test/forced.txt"), "Original content").unwrap(); + sandbox.create_file("test/forced.txt", "Original content"); - let assert = create_moon_command(fixture.path()) - .arg("generate") - .arg("frontmatter") - .arg("./test") - .arg("--defaults") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("generate") + .arg("frontmatter") + .arg("./test") + .arg("--defaults"); + }); assert.success(); - assert_snapshot!(fs::read_to_string(fixture.path().join("./test/forced.txt")).unwrap()); + assert_snapshot!(fs::read_to_string(sandbox.path().join("./test/forced.txt")).unwrap()); } #[test] fn skips_over_file() { - let fixture = create_sandbox("generator"); + let sandbox = generate_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("generate") - .arg("frontmatter") - .arg("./test") - .arg("--defaults") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("generate") + .arg("frontmatter") + .arg("./test") + .arg("--defaults"); + }); assert.success(); - assert!(!fixture.path().join("./test/skipped.txt").exists()); + assert!(!sandbox.path().join("./test/skipped.txt").exists()); } #[test] fn supports_component_vars() { - let fixture = create_sandbox("generator"); + let sandbox = generate_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("generate") - .arg("frontmatter") - .arg("./test") - .arg("--defaults") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("generate") + .arg("frontmatter") + .arg("./test") + .arg("--defaults"); + }); assert.success(); - assert!(fixture + assert!(sandbox .path() .join("./test/components/SmallButton.tsx") .exists()); assert_snapshot!(fs::read_to_string( - fixture.path().join("./test/components/SmallButton.tsx") + sandbox.path().join("./test/components/SmallButton.tsx") ) .unwrap()); } diff --git a/crates/cli/tests/init_node_test.rs b/crates/cli/tests/init_node_test.rs index adfab12e24d..e19ee6eda7a 100644 --- a/crates/cli/tests/init_node_test.rs +++ b/crates/cli/tests/init_node_test.rs @@ -1,5 +1,4 @@ -use moon_utils::test::{create_moon_command, create_sandbox}; -use predicates::prelude::*; +use moon_test_utils::{create_sandbox, predicates::prelude::*}; use std::fs; mod init_node { @@ -7,17 +6,15 @@ mod init_node { #[test] fn infers_version_from_nvm() { - let fixture = create_sandbox("init-sandbox"); - let root = fixture.path(); + let sandbox = create_sandbox("init-sandbox"); + let root = sandbox.path().to_path_buf(); let config = root.join(".moon").join("toolchain.yml"); - fs::write(&root.join(".nvmrc"), "1.2.3").unwrap(); + sandbox.create_file(".nvmrc", "1.2.3"); - create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(root); + }); let content = fs::read_to_string(config).unwrap(); @@ -26,17 +23,15 @@ mod init_node { #[test] fn infers_version_from_nodenv() { - let fixture = create_sandbox("init-sandbox"); - let root = fixture.path(); + let sandbox = create_sandbox("init-sandbox"); + let root = sandbox.path().to_path_buf(); let config = root.join(".moon").join("toolchain.yml"); - fs::write(&root.join(".node-version"), "1.2.3").unwrap(); + sandbox.create_file(".node-version", "1.2.3"); - create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(root); + }); let content = fs::read_to_string(config).unwrap(); @@ -45,27 +40,17 @@ mod init_node { #[test] fn infers_globs_from_workspaces() { - let fixture = create_sandbox("init-sandbox"); - let root = fixture.path(); + let sandbox = create_sandbox("init-sandbox"); + let root = sandbox.path().to_path_buf(); let config = root.join(".moon").join("workspace.yml"); - fs::create_dir_all(root.join("packages").join("foo")).unwrap(); - fs::write(&root.join("packages").join("foo").join("README"), "Hello").unwrap(); + sandbox.create_file("packages/foo/README", "Hello"); + sandbox.create_file("app/README", "World"); + sandbox.create_file("package.json", r#"{"workspaces": ["packages/*", "app"] }"#); - fs::create_dir_all(root.join("app")).unwrap(); - fs::write(&root.join("app").join("README"), "World").unwrap(); - - fs::write( - &root.join("package.json"), - r#"{"workspaces": ["packages/*", "app"] }"#, - ) - .unwrap(); - - create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(root); + }); let content = fs::read_to_string(config).unwrap(); @@ -74,27 +59,20 @@ mod init_node { #[test] fn infers_globs_from_workspaces_expanded() { - let fixture = create_sandbox("init-sandbox"); - let root = fixture.path(); + let sandbox = create_sandbox("init-sandbox"); + let root = sandbox.path().to_path_buf(); let config = root.join(".moon").join("workspace.yml"); - fs::create_dir_all(root.join("packages").join("bar")).unwrap(); - fs::write(&root.join("packages").join("bar").join("README"), "Hello").unwrap(); - - fs::create_dir_all(root.join("app")).unwrap(); - fs::write(&root.join("app").join("README"), "World").unwrap(); - - fs::write( - &root.join("package.json"), + sandbox.create_file("packages/bar/README", "Hello"); + sandbox.create_file("app/README", "World"); + sandbox.create_file( + "package.json", r#"{"workspaces": { "packages": ["packages/*", "app"] }}"#, - ) - .unwrap(); + ); - create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(root); + }); let content = fs::read_to_string(config).unwrap(); @@ -106,17 +84,15 @@ mod init_node { #[test] fn infers_npm() { - let fixture = create_sandbox("init-sandbox"); - let root = fixture.path(); + let sandbox = create_sandbox("init-sandbox"); + let root = sandbox.path().to_path_buf(); let config = root.join(".moon").join("toolchain.yml"); - fs::write(&root.join("package-lock.json"), "").unwrap(); + sandbox.create_file("package-lock.json", ""); - create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(root); + }); let content = fs::read_to_string(config).unwrap(); @@ -125,21 +101,15 @@ mod init_node { #[test] fn infers_npm_from_package() { - let fixture = create_sandbox("init-sandbox"); - let root = fixture.path(); + let sandbox = create_sandbox("init-sandbox"); + let root = sandbox.path().to_path_buf(); let config = root.join(".moon").join("toolchain.yml"); - fs::write( - &root.join("package.json"), - r#"{"packageManager":"npm@4.5.6"}"#, - ) - .unwrap(); + sandbox.create_file("package.json", r#"{"packageManager":"npm@4.5.6"}"#); - create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(root); + }); let content = fs::read_to_string(config).unwrap(); @@ -149,17 +119,15 @@ mod init_node { #[test] fn infers_pnpm() { - let fixture = create_sandbox("init-sandbox"); - let root = fixture.path(); + let sandbox = create_sandbox("init-sandbox"); + let root = sandbox.path().to_path_buf(); let config = root.join(".moon").join("toolchain.yml"); - fs::write(&root.join("pnpm-lock.yaml"), "").unwrap(); + sandbox.create_file("pnpm-lock.yaml", ""); - create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(root); + }); let content = fs::read_to_string(config).unwrap(); @@ -168,21 +136,15 @@ mod init_node { #[test] fn infers_pnpm_from_package() { - let fixture = create_sandbox("init-sandbox"); - let root = fixture.path(); + let sandbox = create_sandbox("init-sandbox"); + let root = sandbox.path().to_path_buf(); let config = root.join(".moon").join("toolchain.yml"); - fs::write( - &root.join("package.json"), - r#"{"packageManager":"pnpm@4.5.6"}"#, - ) - .unwrap(); + sandbox.create_file("package.json", r#"{"packageManager":"pnpm@4.5.6"}"#); - create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(root); + }); let content = fs::read_to_string(config).unwrap(); @@ -192,17 +154,15 @@ mod init_node { #[test] fn infers_yarn() { - let fixture = create_sandbox("init-sandbox"); - let root = fixture.path(); + let sandbox = create_sandbox("init-sandbox"); + let root = sandbox.path().to_path_buf(); let config = root.join(".moon").join("toolchain.yml"); - fs::write(&root.join("yarn.lock"), "").unwrap(); + sandbox.create_file("yarn.lock", ""); - create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(root); + }); let content = fs::read_to_string(config).unwrap(); @@ -211,21 +171,15 @@ mod init_node { #[test] fn infers_yarn_from_package() { - let fixture = create_sandbox("init-sandbox"); - let root = fixture.path(); + let sandbox = create_sandbox("init-sandbox"); + let root = sandbox.path().to_path_buf(); let config = root.join(".moon").join("toolchain.yml"); - fs::write( - &root.join("package.json"), - r#"{"packageManager":"yarn@4.5.6"}"#, - ) - .unwrap(); - - create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + sandbox.create_file("package.json", r#"{"packageManager":"yarn@4.5.6"}"#); + + sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(root); + }); let content = fs::read_to_string(config).unwrap(); diff --git a/crates/cli/tests/init_test.rs b/crates/cli/tests/init_test.rs index 0a03bb44afc..aad091ca994 100644 --- a/crates/cli/tests/init_test.rs +++ b/crates/cli/tests/init_test.rs @@ -1,14 +1,11 @@ use moon_constants::{CONFIG_GLOBAL_PROJECT_FILENAME, CONFIG_WORKSPACE_FILENAME}; -use moon_utils::test::{ - create_moon_command, create_sandbox, create_sandbox_with_git, run_git_command, -}; -use predicates::prelude::*; +use moon_test_utils::{create_sandbox, predicates::prelude::*}; use std::fs; #[test] fn creates_files_in_dest() { - let fixture = create_sandbox("init-sandbox"); - let root = fixture.path(); + let sandbox = create_sandbox("init-sandbox"); + let root = sandbox.path().to_path_buf(); let workspace_config = root.join(".moon").join(CONFIG_WORKSPACE_FILENAME); let project_config = root.join(".moon").join(CONFIG_GLOBAL_PROJECT_FILENAME); let gitignore = root.join(".gitignore"); @@ -17,11 +14,9 @@ fn creates_files_in_dest() { assert!(!project_config.exists()); assert!(!gitignore.exists()); - let assert = create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(root); + }); assert.success().code(0).stdout(predicate::str::contains( "moon has successfully been initialized in", @@ -34,15 +29,13 @@ fn creates_files_in_dest() { #[test] fn creates_workspace_config_from_template() { - let fixture = create_sandbox("init-sandbox"); - let root = fixture.path(); + let sandbox = create_sandbox("init-sandbox"); + let root = sandbox.path().to_path_buf(); let workspace_config = root.join(".moon").join("workspace.yml"); - create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(root); + }); assert!( predicate::str::contains("https://moonrepo.dev/schemas/workspace.json") @@ -52,17 +45,15 @@ fn creates_workspace_config_from_template() { #[test] fn creates_project_config_from_template() { - let fixture = create_sandbox("init-sandbox"); - let root = fixture.path(); + let sandbox = create_sandbox("init-sandbox"); + let root = sandbox.path().to_path_buf(); let project_config = root .join(".moon") .join(moon_constants::CONFIG_GLOBAL_PROJECT_FILENAME); - create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(root); + }); assert!( predicate::str::contains("https://moonrepo.dev/schemas/global-project.json") @@ -72,15 +63,13 @@ fn creates_project_config_from_template() { #[test] fn creates_gitignore_file() { - let fixture = create_sandbox("init-sandbox"); - let root = fixture.path(); + let sandbox = create_sandbox("init-sandbox"); + let root = sandbox.path().to_path_buf(); let gitignore = root.join(".gitignore"); - create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(root); + }); assert_eq!( fs::read_to_string(gitignore).unwrap(), @@ -90,42 +79,34 @@ fn creates_gitignore_file() { #[test] fn appends_existing_gitignore_file() { - let fixture = create_sandbox("init-sandbox"); - let root = fixture.path(); - let gitignore = root.join(".gitignore"); + let sandbox = create_sandbox("init-sandbox"); + let root = sandbox.path().to_path_buf(); - fs::write(&gitignore, "*.js\n*.log").unwrap(); + sandbox.create_file(".gitignore", "*.js\n*.log"); - create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(&root); + }); assert_eq!( - fs::read_to_string(gitignore).unwrap(), + fs::read_to_string(root.join(".gitignore")).unwrap(), "*.js\n*.log\n# moon\n.moon/cache\n.moon/docker\n" ); } #[test] fn does_overwrite_existing_config_if_force_passed() { - let fixture = create_sandbox("init-sandbox"); - let root = fixture.path(); + let sandbox = create_sandbox("init-sandbox"); + let root = sandbox.path().to_path_buf(); - create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(&root); + }); // Run again - let assert = create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .arg("--force") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(root).arg("--force"); + }); assert.success().code(0).stdout(predicate::str::contains( "moon has successfully been initialized in", @@ -136,26 +117,25 @@ mod vcs { use super::*; #[test] - fn detects_git() { - let fixture = create_sandbox_with_git("init-sandbox"); - let root = fixture.path(); + let sandbox = create_sandbox("init-sandbox"); + sandbox.enable_git(); + + let root = sandbox.path().to_path_buf(); let workspace_config = root.join(".moon").join("workspace.yml"); // Checkout a new branch - run_git_command(root, |cmd| { - cmd.args(["checkout", "-b", "fixtures-test"]); + sandbox.run_git(|cmd| { + cmd.args(["checkout", "-b", "sandboxs-test"]); }); - create_moon_command(root) - .arg("init") - .arg("--yes") - .arg(root) - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("init").arg("--yes").arg(root); + }); let content = fs::read_to_string(workspace_config).unwrap(); assert!(predicate::str::contains("manager: 'git'").eval(&content)); - assert!(predicate::str::contains("defaultBranch: 'fixtures-test'").eval(&content)); + assert!(predicate::str::contains("defaultBranch: 'sandboxs-test'").eval(&content)); } } diff --git a/crates/cli/tests/migrate_test.rs b/crates/cli/tests/migrate_test.rs index 6e63ffc5e98..ba511924aa0 100644 --- a/crates/cli/tests/migrate_test.rs +++ b/crates/cli/tests/migrate_test.rs @@ -1,19 +1,34 @@ -use insta::assert_snapshot; -use moon_utils::test::{create_moon_command, create_sandbox, create_sandbox_with_git}; -use predicates::str::contains; +use moon_config::{WorkspaceConfig, WorkspaceProjects}; +use moon_test_utils::{ + assert_snapshot, create_sandbox_with_config, predicates::str::contains, Sandbox, +}; +use moon_utils::string_vec; use std::fs; +fn migrate_sandbox() -> Sandbox { + let workspace_config = WorkspaceConfig { + projects: WorkspaceProjects::Globs(string_vec!["package-json/*"]), + ..WorkspaceConfig::default() + }; + + create_sandbox_with_config("migrate", Some(&workspace_config), None, None) +} + mod from_package_json { use super::*; #[test] fn dirty_repository_raises_an_error() { - let fixture = create_sandbox_with_git("migrate"); - // create a new file at fixture path to simulate a dirty repository - fs::write(fixture.path().join("new_file"), "new_file").unwrap(); - let assert = create_moon_command(fixture.path()) - .args(["migrate", "from-package-json", "common"]) - .assert(); + let sandbox = migrate_sandbox(); + sandbox.enable_git(); + + // create a new file at sandbox path to simulate a dirty repository + sandbox.create_file("new_file", "new_file"); + + let assert = sandbox.run_moon(|cmd| { + cmd.args(["migrate", "from-package-json", "common"]); + }); + assert .failure() .code(1) @@ -23,24 +38,24 @@ mod from_package_json { #[test] fn converts_scripts() { - let fixture = create_sandbox("migrate"); + let sandbox = migrate_sandbox(); - let assert = create_moon_command(fixture.path()) - .args([ + let assert = sandbox.run_moon(|cmd| { + cmd.args([ "migrate", "--skipTouchedFilesCheck", "from-package-json", "common", - ]) - .assert(); + ]); + }); assert_snapshot!(fs::read_to_string( - fixture.path().join("package-json/common/package.json") + sandbox.path().join("package-json/common/package.json") ) .unwrap()); assert_snapshot!( - fs::read_to_string(fixture.path().join("package-json/common/moon.yml")).unwrap() + fs::read_to_string(sandbox.path().join("package-json/common/moon.yml")).unwrap() ); assert.success(); @@ -48,23 +63,23 @@ mod from_package_json { #[test] fn links_depends_on() { - let fixture = create_sandbox("migrate"); + let sandbox = migrate_sandbox(); - let assert = create_moon_command(fixture.path()) - .args([ + let assert = sandbox.run_moon(|cmd| { + cmd.args([ "migrate", "--skipTouchedFilesCheck", "from-package-json", "deps", - ]) - .assert(); + ]); + }); assert_snapshot!( - fs::read_to_string(fixture.path().join("package-json/deps/package.json")).unwrap() + fs::read_to_string(sandbox.path().join("package-json/deps/package.json")).unwrap() ); assert_snapshot!( - fs::read_to_string(fixture.path().join("package-json/deps/moon.yml")).unwrap() + fs::read_to_string(sandbox.path().join("package-json/deps/moon.yml")).unwrap() ); assert.success(); diff --git a/crates/cli/tests/node_test.rs b/crates/cli/tests/node_test.rs index eedf2ff5b41..c2b28c766f9 100644 --- a/crates/cli/tests/node_test.rs +++ b/crates/cli/tests/node_test.rs @@ -1,18 +1,8 @@ -use moon_utils::test::{create_moon_command, create_sandbox_with_git, get_fixtures_dir}; -use predicates::prelude::*; +use moon_test_utils::{ + create_sandbox_with_config, get_fixtures_path, get_node_depman_fixture_configs, + predicates::prelude::*, +}; use serial_test::serial; -use std::path::Path; - -fn setup_toolchain(path: &Path, target_id: &str) { - if target_id.is_empty() { - create_moon_command(path).args(["setup"]).assert().success(); - } else { - create_moon_command(path) - .args(["run", target_id]) - .assert() - .success(); - } -} mod run_script { use super::*; @@ -20,13 +10,19 @@ mod run_script { #[test] #[serial] fn errors_if_no_project() { - let fixture = create_sandbox_with_git("node-npm"); + let (workspace_config, toolchain_config, projects_config) = + get_node_depman_fixture_configs("npm"); - setup_toolchain(fixture.path(), ""); + let sandbox = create_sandbox_with_config( + "node-npm", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .args(["node", "run-script", "unknown"]) - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.args(["node", "run-script", "unknown"]); + }); assert.failure().stderr(predicate::str::contains( "This command must be ran within the context of a project.", @@ -36,13 +32,19 @@ mod run_script { #[test] #[serial] fn errors_for_unknown_script() { - let fixture = create_sandbox_with_git("node-npm"); + let (workspace_config, toolchain_config, projects_config) = + get_node_depman_fixture_configs("npm"); - setup_toolchain(fixture.path(), ""); + let sandbox = create_sandbox_with_config( + "node-npm", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .args(["node", "run-script", "unknown", "--project", "npm"]) - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.args(["node", "run-script", "unknown", "--project", "npm"]); + }); assert .failure() @@ -52,13 +54,19 @@ mod run_script { #[test] #[serial] fn runs_with_project_option() { - let fixture = create_sandbox_with_git("node-npm"); + let (workspace_config, toolchain_config, projects_config) = + get_node_depman_fixture_configs("npm"); - setup_toolchain(fixture.path(), "npm:installDep"); + let sandbox = create_sandbox_with_config( + "node-npm", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .args(["node", "run-script", "test", "--project", "npm"]) - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.args(["node", "run-script", "test", "--project", "npm"]); + }); assert.success().stdout(predicate::str::contains("> test")); } @@ -66,17 +74,20 @@ mod run_script { #[test] #[serial] fn runs_with_env_var() { - let fixture = create_sandbox_with_git("node-npm"); + let (workspace_config, toolchain_config, projects_config) = + get_node_depman_fixture_configs("npm"); - setup_toolchain(fixture.path(), "npm:installDep"); + let sandbox = create_sandbox_with_config( + "node-npm", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .args(["node", "run-script", "test"]) - .env( - "MOON_PROJECT_ROOT", - get_fixtures_dir("node-npm").join("base"), - ) - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.args(["node", "run-script", "test"]) + .env("MOON_PROJECT_ROOT", get_fixtures_path("node-npm/base")); + }); assert.success().stdout(predicate::str::contains("> test")); } @@ -84,13 +95,19 @@ mod run_script { #[test] #[serial] fn works_with_pnpm() { - let fixture = create_sandbox_with_git("node-pnpm"); + let (workspace_config, toolchain_config, projects_config) = + get_node_depman_fixture_configs("pnpm"); - setup_toolchain(fixture.path(), "pnpm:installDep"); + let sandbox = create_sandbox_with_config( + "node-pnpm", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .args(["node", "run-script", "lint", "--project", "pnpm"]) - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.args(["node", "run-script", "lint", "--project", "pnpm"]); + }); assert.success().stdout(predicate::str::contains("lint")); } @@ -98,13 +115,19 @@ mod run_script { #[test] #[serial] fn works_with_yarn() { - let fixture = create_sandbox_with_git("node-yarn"); + let (workspace_config, toolchain_config, projects_config) = + get_node_depman_fixture_configs("yarn"); - setup_toolchain(fixture.path(), "yarn:installDep"); + let sandbox = create_sandbox_with_config( + "node-yarn", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .args(["node", "run-script", "build", "--project", "yarn"]) - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.args(["node", "run-script", "build", "--project", "yarn"]); + }); assert.success().stdout(predicate::str::contains("build")); } @@ -112,13 +135,19 @@ mod run_script { #[test] #[serial] fn works_with_yarn1() { - let fixture = create_sandbox_with_git("node-yarn1"); - - setup_toolchain(fixture.path(), "yarn:installDep"); - - let assert = create_moon_command(fixture.path()) - .args(["node", "run-script", "build", "--project", "yarn"]) - .assert(); + let (workspace_config, toolchain_config, projects_config) = + get_node_depman_fixture_configs("yarn1"); + + let sandbox = create_sandbox_with_config( + "node-yarn1", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + let assert = sandbox.run_moon(|cmd| { + cmd.args(["node", "run-script", "build", "--project", "yarn1"]); + }); assert.success().stdout(predicate::str::contains("build")); } diff --git a/crates/cli/tests/project_graph_test.rs b/crates/cli/tests/project_graph_test.rs index 1ffca76c668..62e3b213079 100644 --- a/crates/cli/tests/project_graph_test.rs +++ b/crates/cli/tests/project_graph_test.rs @@ -1,50 +1,71 @@ -use insta::assert_snapshot; -use moon_utils::test::{create_moon_command, create_sandbox, get_assert_output}; +use moon_test_utils::{ + assert_snapshot, create_sandbox_with_config, get_project_graph_aliases_fixture_configs, + get_projects_fixture_configs, +}; #[test] fn no_projects() { - let fixture = create_sandbox("base"); + let sandbox = create_sandbox_with_config("base", None, None, None); - let assert = create_moon_command(fixture.path()) - .arg("project-graph") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("project-graph"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn many_projects() { - let fixture = create_sandbox("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); - let assert = create_moon_command(fixture.path()) - .arg("project-graph") - .assert(); + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - assert_snapshot!(get_assert_output(&assert)); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("project-graph"); + }); + + assert_snapshot!(assert.output()); } #[test] fn single_project_with_dependencies() { - let fixture = create_sandbox("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("project-graph") - .arg("foo") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("project-graph").arg("foo"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn single_project_no_dependencies() { - let fixture = create_sandbox("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("project-graph") - .arg("baz") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("project-graph").arg("baz"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } mod aliases { @@ -52,36 +73,58 @@ mod aliases { #[test] fn uses_ids_in_graph() { - let fixture = create_sandbox("project-graph/aliases"); + let (workspace_config, toolchain_config, projects_config) = + get_project_graph_aliases_fixture_configs(); - let assert = create_moon_command(fixture.path()) - .arg("project-graph") - .assert(); + let sandbox = create_sandbox_with_config( + "project-graph/aliases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - assert_snapshot!(get_assert_output(&assert)); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("project-graph"); + }); + + assert_snapshot!(assert.output()); } #[test] fn can_focus_using_an_alias() { - let fixture = create_sandbox("project-graph/aliases"); + let (workspace_config, toolchain_config, projects_config) = + get_project_graph_aliases_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "project-graph/aliases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("project-graph") - .arg("@scope/pkg-foo") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("project-graph").arg("@scope/pkg-foo"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn resolves_aliases_in_depends_on() { - let fixture = create_sandbox("project-graph/aliases"); + let (workspace_config, toolchain_config, projects_config) = + get_project_graph_aliases_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "project-graph/aliases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("project-graph") - .arg("noLang") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("project-graph").arg("noLang"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } } diff --git a/crates/cli/tests/project_test.rs b/crates/cli/tests/project_test.rs index 4be91ca55c9..a1ffcff49bd 100644 --- a/crates/cli/tests/project_test.rs +++ b/crates/cli/tests/project_test.rs @@ -1,105 +1,153 @@ -use insta::assert_snapshot; -use moon_utils::test::{ - create_moon_command, create_sandbox, get_assert_output, get_assert_stderr_output_clean, +use moon_test_utils::{ + assert_snapshot, create_sandbox_with_config, get_assert_stderr_output, + get_cases_fixture_configs, get_projects_fixture_configs, }; #[test] fn unknown_project() { - let fixture = create_sandbox("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); - let assert = create_moon_command(fixture.path()) - .arg("project") - .arg("unknown") - .assert(); + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - assert_snapshot!(get_assert_stderr_output_clean(&assert)); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("project").arg("unknown"); + }); + + assert_snapshot!(get_assert_stderr_output(&assert.inner)); assert.failure().code(1); } #[test] fn empty_config() { - let fixture = create_sandbox("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("project") - .arg("emptyConfig") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("project").arg("emptyConfig"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn no_config() { - let fixture = create_sandbox("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); - let assert = create_moon_command(fixture.path()) - .arg("project") - .arg("noConfig") - .assert(); + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - assert_snapshot!(get_assert_output(&assert)); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("project").arg("noConfig"); + }); + + assert_snapshot!(assert.output()); } #[test] fn basic_config() { // with dependsOn and fileGroups - let fixture = create_sandbox("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("project") - .arg("basic") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("project").arg("basic"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn advanced_config() { // with project metadata - let fixture = create_sandbox("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); - let assert = create_moon_command(fixture.path()) - .arg("project") - .arg("advanced") - .assert(); + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - assert_snapshot!(get_assert_output(&assert)); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("project").arg("advanced"); + }); + + assert_snapshot!(assert.output()); } #[test] fn depends_on_paths() { // shows dependsOn paths when they exist - let fixture = create_sandbox("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("project") - .arg("foo") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("project").arg("foo"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn with_tasks() { - let fixture = create_sandbox("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); - let assert = create_moon_command(fixture.path()) - .arg("project") - .arg("tasks") - .assert(); + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - assert_snapshot!(get_assert_output(&assert)); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("project").arg("tasks"); + }); + + assert_snapshot!(assert.output()); } #[test] fn root_level() { - let fixture = create_sandbox("cases"); + let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "cases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("project") - .arg("root") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("project").arg("root"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } diff --git a/crates/cli/tests/query_test.rs b/crates/cli/tests/query_test.rs index 087e576f534..881d1e4d12f 100644 --- a/crates/cli/tests/query_test.rs +++ b/crates/cli/tests/query_test.rs @@ -1,52 +1,54 @@ use moon_cli::enums::TouchedStatus; use moon_cli::queries::projects::QueryProjectsResult; use moon_cli::queries::touched_files::QueryTouchedFilesResult; -use moon_utils::test::{ - create_moon_command, create_sandbox, create_sandbox_with_git, get_assert_output, - run_git_command, +use moon_test_utils::{ + create_sandbox_with_config, get_assert_stdout_output, get_cases_fixture_configs, + get_projects_fixture_configs, Sandbox, }; use moon_utils::{is_ci, string_vec}; -use std::fs; -use std::path::Path; -fn change_branch(path: &Path) { - run_git_command(path, |cmd| { +fn change_branch(sandbox: &Sandbox) { + sandbox.run_git(|cmd| { cmd.args(["checkout", "-b", "branch"]); }); } -fn touch_file(path: &Path) { - fs::write(path.join("advanced/file"), "contents").unwrap(); +fn touch_file(sandbox: &Sandbox) { + sandbox.create_file("advanced/file", "contents"); // CI uses `git diff` while local uses `git status` if is_ci() { - change_branch(path); + change_branch(sandbox); - run_git_command(path, |cmd| { + sandbox.run_git(|cmd| { cmd.args(["add", "advanced/file"]); }); - run_git_command(path, |cmd| { + sandbox.run_git(|cmd| { cmd.args(["commit", "-m", "Touch"]); }); } } mod projects { - use moon_utils::test::get_assert_stdout_output; - use super::*; #[test] fn returns_all_by_default() { - let fixture = create_sandbox("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("query") - .arg("projects") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("query").arg("projects"); + }); - let json: QueryProjectsResult = serde_json::from_str(&get_assert_output(&assert)).unwrap(); + let json: QueryProjectsResult = serde_json::from_str(&assert.output()).unwrap(); let ids: Vec = json.projects.iter().map(|p| p.id.clone()).collect(); assert_eq!( @@ -70,17 +72,23 @@ mod projects { #[test] fn can_filter_by_affected() { - let fixture = create_sandbox_with_git("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); - touch_file(fixture.path()); + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + sandbox.enable_git(); + + touch_file(&sandbox); - let assert = create_moon_command(fixture.path()) - .arg("query") - .arg("projects") - .arg("--affected") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("query").arg("projects").arg("--affected"); + }); - let json: QueryProjectsResult = serde_json::from_str(&get_assert_output(&assert)).unwrap(); + let json: QueryProjectsResult = serde_json::from_str(&assert.output()).unwrap(); let ids: Vec = json.projects.iter().map(|p| p.id.clone()).collect(); assert_eq!(ids, string_vec!["advanced"]); @@ -89,27 +97,34 @@ mod projects { #[test] fn can_filter_by_affected_via_stdin() { - let fixture = create_sandbox_with_git("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); - touch_file(fixture.path()); + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + sandbox.enable_git(); - let mut query = create_moon_command(fixture.path()); - query.arg("query").arg("touched-files"); + touch_file(&sandbox); - if !is_ci() { - query.arg("--local"); - } + let query = sandbox.run_moon(|cmd| { + cmd.arg("query").arg("touched-files"); - let query = query.assert(); + if !is_ci() { + cmd.arg("--local"); + } + }); - let assert = create_moon_command(fixture.path()) - .arg("query") - .arg("projects") - .arg("--affected") - .write_stdin(get_assert_stdout_output(&query)) - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("query") + .arg("projects") + .arg("--affected") + .write_stdin(get_assert_stdout_output(&query.inner)); + }); - let json: QueryProjectsResult = serde_json::from_str(&get_assert_output(&assert)).unwrap(); + let json: QueryProjectsResult = serde_json::from_str(&assert.output()).unwrap(); let ids: Vec = json.projects.iter().map(|p| p.id.clone()).collect(); assert_eq!(ids, string_vec!["advanced"]); @@ -118,15 +133,20 @@ mod projects { #[test] fn can_filter_by_id() { - let fixture = create_sandbox("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); - let assert = create_moon_command(fixture.path()) - .arg("query") - .arg("projects") - .args(["--id", "ba(r|z)"]) - .assert(); + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let json: QueryProjectsResult = serde_json::from_str(&get_assert_output(&assert)).unwrap(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("query").arg("projects").args(["--id", "ba(r|z)"]); + }); + + let json: QueryProjectsResult = serde_json::from_str(&assert.output()).unwrap(); let ids: Vec = json.projects.iter().map(|p| p.id.clone()).collect(); assert_eq!(ids, string_vec!["bar", "baz"]); @@ -135,15 +155,22 @@ mod projects { #[test] fn can_filter_by_source() { - let fixture = create_sandbox("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("query") - .arg("projects") - .args(["--source", "config$"]) - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("query") + .arg("projects") + .args(["--source", "config$"]); + }); - let json: QueryProjectsResult = serde_json::from_str(&get_assert_output(&assert)).unwrap(); + let json: QueryProjectsResult = serde_json::from_str(&assert.output()).unwrap(); let ids: Vec = json.projects.iter().map(|p| p.id.clone()).collect(); assert_eq!(ids, string_vec!["emptyConfig", "noConfig"]); @@ -152,15 +179,20 @@ mod projects { #[test] fn can_filter_by_tasks() { - let fixture = create_sandbox("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); - let assert = create_moon_command(fixture.path()) - .arg("query") - .arg("projects") - .args(["--tasks", "lint"]) - .assert(); + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let json: QueryProjectsResult = serde_json::from_str(&get_assert_output(&assert)).unwrap(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("query").arg("projects").args(["--tasks", "lint"]); + }); + + let json: QueryProjectsResult = serde_json::from_str(&assert.output()).unwrap(); let ids: Vec = json.projects.iter().map(|p| p.id.clone()).collect(); assert_eq!(ids, string_vec!["tasks"]); @@ -169,15 +201,22 @@ mod projects { #[test] fn can_filter_by_language() { - let fixture = create_sandbox("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("query") - .arg("projects") - .args(["--language", "java|bash"]) - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("query") + .arg("projects") + .args(["--language", "java|bash"]); + }); - let json: QueryProjectsResult = serde_json::from_str(&get_assert_output(&assert)).unwrap(); + let json: QueryProjectsResult = serde_json::from_str(&assert.output()).unwrap(); let ids: Vec = json.projects.iter().map(|p| p.id.clone()).collect(); assert_eq!(ids, string_vec!["bash", "basic", "foo", "js"]); @@ -186,15 +225,20 @@ mod projects { #[test] fn can_filter_by_type() { - let fixture = create_sandbox("projects"); + let (workspace_config, toolchain_config, projects_config) = get_projects_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("query") - .arg("projects") - .args(["--type", "app"]) - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("query").arg("projects").args(["--type", "app"]); + }); - let json: QueryProjectsResult = serde_json::from_str(&get_assert_output(&assert)).unwrap(); + let json: QueryProjectsResult = serde_json::from_str(&assert.output()).unwrap(); let ids: Vec = json.projects.iter().map(|p| p.id.clone()).collect(); assert_eq!(ids, string_vec!["advanced", "foo", "ts"]); @@ -204,24 +248,28 @@ mod projects { mod touched_files { use super::*; - use moon_utils::test::create_sandbox_with_git; #[test] fn can_change_options() { - let fixture = create_sandbox_with_git("cases"); + let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); - change_branch(fixture.path()); + let sandbox = create_sandbox_with_config( + "cases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + sandbox.enable_git(); + + change_branch(&sandbox); - let assert = create_moon_command(fixture.path()) - .arg("query") - .arg("touched-files") - .args([ + let assert = sandbox.run_moon(|cmd| { + cmd.arg("query").arg("touched-files").args([ "--base", "master", "--head", "branch", "--status", "deleted", - ]) - .assert(); + ]); + }); - let json: QueryTouchedFilesResult = - serde_json::from_str(&get_assert_output(&assert)).unwrap(); + let json: QueryTouchedFilesResult = serde_json::from_str(&assert.output()).unwrap(); assert_eq!(json.options.base, "master".to_string()); assert_eq!(json.options.head, "branch".to_string()); diff --git a/crates/cli/tests/run_node_test.rs b/crates/cli/tests/run_node_test.rs index 40701aed3da..36139aad093 100644 --- a/crates/cli/tests/run_node_test.rs +++ b/crates/cli/tests/run_node_test.rs @@ -1,136 +1,199 @@ -mod utils; - -use insta::assert_snapshot; -use moon_utils::test::{create_moon_command, create_sandbox_with_git, get_assert_output}; -use predicates::prelude::*; +use moon_config::{NodeConfig, TypeScriptConfig}; +use moon_test_utils::{ + assert_snapshot, create_sandbox_with_config, get_node_depman_fixture_configs, + get_node_fixture_configs, get_typescript_fixture_configs, predicates::prelude::*, Sandbox, +}; +use moon_utils::string_vec; use std::fs::read_to_string; -use utils::{append_toolchain_config, get_path_safe_output, update_toolchain_config}; + +fn node_sandbox() -> Sandbox { + let (workspace_config, toolchain_config, projects_config) = get_node_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "node", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + sandbox.enable_git(); + sandbox +} + +fn node_sandbox_with_config(callback: C) -> Sandbox +where + C: FnOnce(&mut NodeConfig), +{ + let (workspace_config, mut toolchain_config, projects_config) = get_node_fixture_configs(); + + if let Some(node_config) = &mut toolchain_config.node { + callback(node_config); + } + + let sandbox = create_sandbox_with_config( + "node", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + sandbox.enable_git(); + sandbox +} + +fn depman_sandbox(depman: &str) -> Sandbox { + let (workspace_config, toolchain_config, projects_config) = + get_node_depman_fixture_configs(depman); + + let sandbox = create_sandbox_with_config( + format!("node-{}", depman), + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + sandbox.enable_git(); + sandbox +} + +fn typescript_sandbox(callback: C) -> Sandbox +where + C: FnOnce(&mut TypeScriptConfig), +{ + let (workspace_config, mut toolchain_config, projects_config) = + get_typescript_fixture_configs(); + + if let Some(ts_config) = &mut toolchain_config.typescript { + callback(ts_config); + } + + let sandbox = create_sandbox_with_config( + "typescript", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + sandbox.enable_git(); + sandbox +} #[test] fn runs_package_managers() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:npm") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:npm"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn runs_standard_script() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:standard") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:standard"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn runs_cjs_files() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:cjs") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:cjs"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn runs_mjs_files() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:mjs") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:mjs"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn supports_top_level_await() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:topLevelAwait") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:topLevelAwait"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn handles_process_exit_zero() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:processExitZero") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:processExitZero"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn handles_process_exit_nonzero() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:processExitNonZero") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:processExitNonZero"); + }); if cfg!(windows) { assert.code(1); } else { - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } } #[test] fn handles_process_exit_code_zero() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:exitCodeZero") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:exitCodeZero"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn handles_process_exit_code_nonzero() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:exitCodeNonZero") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:exitCodeNonZero"); + }); if cfg!(windows) { assert.code(1); } else { - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } } #[test] fn handles_throw_error() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:throwError"); + }); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:throwError") - .assert(); - let output = get_assert_output(&assert); + let output = assert.output(); // Output contains file paths that we cant snapshot assert!(predicate::str::contains("Error: Oops").eval(&output)); @@ -138,132 +201,122 @@ fn handles_throw_error() { #[test] fn handles_unhandled_promise() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:unhandledPromise") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:unhandledPromise"); + }); if cfg!(windows) { assert.code(1); } else { - assert_snapshot!(get_path_safe_output(&assert, fixture.path())); + assert_snapshot!(assert.output()); } } #[test] fn passes_args_through() { - let fixture = create_sandbox_with_git("node"); - - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:passthroughArgs") - .arg("--") - .arg("-aBc") - .arg("--opt") - .arg("value") - .arg("--optCamel=value") - .arg("foo") - .arg("'bar baz'") - .arg("--opt-kebab") - .arg("123") - .assert(); - - assert_snapshot!(get_assert_output(&assert)); + let sandbox = node_sandbox(); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("node:passthroughArgs") + .arg("--") + .arg("-aBc") + .arg("--opt") + .arg("value") + .arg("--optCamel=value") + .arg("foo") + .arg("'bar baz'") + .arg("--opt-kebab") + .arg("123"); + }); + + assert_snapshot!(assert.output()); } #[test] fn passes_args_to_the_node_bin() { - let fixture = create_sandbox_with_git("node"); - - append_toolchain_config( - fixture.path(), - " binExecArgs:\n - '--preserve-symlinks'\n", - ); - - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:binExecArgs") - .arg("--") - .arg("--extraArg") - .assert(); - - assert_snapshot!(get_path_safe_output(&assert, fixture.path())); + let sandbox = node_sandbox_with_config(|cfg| { + cfg.bin_exec_args = string_vec!["--preserve-symlinks"]; + }); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("node:binExecArgs") + .arg("--") + .arg("--extraArg"); + }); + + assert_snapshot!(assert.output()); } #[test] fn sets_env_vars() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:envVars") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:envVars"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn inherits_moon_env_vars() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:envVarsMoon") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:envVarsMoon"); + }); - assert_snapshot!(get_path_safe_output(&assert, fixture.path())); + assert_snapshot!(assert.output()); } #[test] fn runs_from_project_root() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:runFromProject") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:runFromProject"); + }); - assert_snapshot!(get_path_safe_output(&assert, fixture.path())); + assert_snapshot!(assert.output()); } #[test] fn runs_from_workspace_root() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:runFromWorkspace") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:runFromWorkspace"); + }); - assert_snapshot!(get_path_safe_output(&assert, fixture.path())); + assert_snapshot!(assert.output()); } #[test] fn retries_on_failure_till_count() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:retryCount"); + }); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:retryCount") - .assert(); - let output = get_assert_output(&assert); + let output = assert.output(); - assert!(predicate::str::contains("Process ~/.moon/tools/node/16.1.0").eval(&output)); + assert!(predicate::str::contains("Process ~/.moon/tools/node/18.0.0").eval(&output)); } #[test] fn can_run_many_targets() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:cjs") - .arg("node:mjs") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:cjs").arg("node:mjs"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains("node:cjs | stdout").eval(&output)); assert!(predicate::str::contains("node:mjs | stdout").eval(&output)); @@ -276,18 +329,19 @@ mod install_deps { #[test] fn installs_on_first_run() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); + + assert!(!sandbox.path().join("node_modules").exists()); - assert!(!fixture.path().join("node_modules").exists()); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("node:standard") + .env_remove("MOON_TEST_HIDE_INSTALL_OUTPUT"); + }); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:standard") - .env_remove("MOON_TEST_HIDE_INSTALL_OUTPUT") - .assert(); - let output = get_assert_output(&assert); + let output = assert.output(); - assert!(fixture.path().join("node_modules").exists()); + assert!(sandbox.path().join("node_modules").exists()); assert!(predicate::str::contains("added").eval(&output)); assert!(predicate::str::contains("packages").eval(&output)); @@ -295,24 +349,26 @@ mod install_deps { #[test] fn doesnt_reinstall_on_second_run() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:standard") - .env_remove("MOON_TEST_HIDE_INSTALL_OUTPUT") - .assert(); - let output1 = get_assert_output(&assert); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("node:standard") + .env_remove("MOON_TEST_HIDE_INSTALL_OUTPUT"); + }); + + let output1 = assert.output(); assert!(predicate::str::contains("added").eval(&output1)); assert!(predicate::str::contains("packages").eval(&output1)); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:standard") - .env_remove("MOON_TEST_HIDE_INSTALL_OUTPUT") - .assert(); - let output2 = get_assert_output(&assert); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("node:standard") + .env_remove("MOON_TEST_HIDE_INSTALL_OUTPUT"); + }); + + let output2 = assert.output(); assert!(!predicate::str::contains("added").eval(&output2)); assert!(!predicate::str::contains("packages").eval(&output2)); @@ -320,37 +376,46 @@ mod install_deps { #[test] fn creates_tool_state_cache() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - create_moon_command(fixture.path()) - .arg("run") - .arg("node:standard") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:standard"); + }); - assert!(fixture + assert!(sandbox .path() - .join(".moon/cache/states/toolNode-16.1.0.json") + .join(".moon/cache/states/toolNode-18.0.0.json") .exists()); } #[test] fn installs_deps_into_each_project_when_not_using_workspaces() { - let fixture = create_sandbox_with_git("node-non-workspaces"); + let (workspace_config, toolchain_config, projects_config) = + get_typescript_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "node-non-workspaces", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("foo:noop") - .arg("bar:noop") - .arg("baz:noop") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("foo:noop") + .arg("bar:noop") + .arg("baz:noop"); + }); assert!(predicate::str::contains("npm install") .count(3) - .eval(&get_assert_output(&assert))); + .eval(&assert.output())); - assert!(fixture.path().join("foo/package-lock.json").exists()); - assert!(fixture.path().join("bar/package-lock.json").exists()); - assert!(fixture.path().join("baz/package-lock.json").exists()); + assert!(sandbox.path().join("foo/package-lock.json").exists()); + assert!(sandbox.path().join("bar/package-lock.json").exists()); + assert!(sandbox.path().join("baz/package-lock.json").exists()); } } @@ -359,34 +424,28 @@ mod engines { #[test] fn adds_engines_constraint() { - let fixture = create_sandbox_with_git("node"); - - update_toolchain_config( - fixture.path(), - "addEnginesConstraint: false", - "addEnginesConstraint: true", - ); + let sandbox = node_sandbox_with_config(|cfg| { + cfg.add_engines_constraint = true; + }); - create_moon_command(fixture.path()) - .arg("run") - .arg("node:standard") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:standard"); + }); - assert_snapshot!(read_to_string(fixture.path().join("package.json")).unwrap()); + assert_snapshot!(read_to_string(sandbox.path().join("package.json")).unwrap()); } #[test] fn doesnt_add_engines_constraint() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox_with_config(|cfg| { + cfg.add_engines_constraint = false; + }); - append_toolchain_config(fixture.path(), r#" addEnginesConstraint: false"#); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:standard"); + }); - create_moon_command(fixture.path()) - .arg("run") - .arg("node:standard") - .assert(); - - assert_snapshot!(read_to_string(fixture.path().join("package.json")).unwrap()); + assert_snapshot!(read_to_string(sandbox.path().join("package.json")).unwrap()); } } @@ -395,167 +454,132 @@ mod version_manager { #[test] fn adds_no_file_by_default() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - create_moon_command(fixture.path()) - .arg("run") - .arg("node:standard") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:standard"); + }); - assert!(!fixture.path().join(".nvmrc").exists()); - assert!(!fixture.path().join(".node-version").exists()); + assert!(!sandbox.path().join(".nvmrc").exists()); + assert!(!sandbox.path().join(".node-version").exists()); } #[test] fn adds_nvmrc_file() { - let fixture = create_sandbox_with_git("node"); - - append_toolchain_config(fixture.path(), r#" syncVersionManagerConfig: nvm"#); + let sandbox = node_sandbox_with_config(|cfg| { + cfg.sync_version_manager_config = Some(moon_config::NodeVersionManager::Nvm); + }); - create_moon_command(fixture.path()) - .arg("run") - .arg("node:standard") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:standard"); + }); - assert!(fixture.path().join(".nvmrc").exists()); + assert!(sandbox.path().join(".nvmrc").exists()); assert_eq!( - read_to_string(fixture.path().join(".nvmrc")).unwrap(), - "16.1.0" + read_to_string(sandbox.path().join(".nvmrc")).unwrap(), + "18.0.0" ); } #[test] fn adds_nodenv_file() { - let fixture = create_sandbox_with_git("node"); - - append_toolchain_config(fixture.path(), r#" syncVersionManagerConfig: nodenv"#); + let sandbox = node_sandbox_with_config(|cfg| { + cfg.sync_version_manager_config = Some(moon_config::NodeVersionManager::Nodenv); + }); - create_moon_command(fixture.path()) - .arg("run") - .arg("node:standard") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:standard"); + }); - assert!(fixture.path().join(".node-version").exists()); + assert!(sandbox.path().join(".node-version").exists()); assert_eq!( - read_to_string(fixture.path().join(".node-version")).unwrap(), - "16.1.0" + read_to_string(sandbox.path().join(".node-version")).unwrap(), + "18.0.0" ); } - - #[test] - fn errors_for_invalid_value() { - let fixture = create_sandbox_with_git("node"); - - append_toolchain_config(fixture.path(), r#" syncVersionManagerConfig: invalid"#); - - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:standard") - .assert(); - - let output = get_assert_output(&assert); - - assert!(predicate::str::contains( - "unknown variant: found `invalid`, expected ``nodenv` or `nvm``" - ) - .eval(&output)); - } } mod sync_depends_on { use super::*; + use moon_config::NodeVersionFormat; - fn test_depends_on_format(format: &str) { - let fixture = create_sandbox_with_git("node"); + fn test_depends_on_format(format: NodeVersionFormat) { + let sandbox = node_sandbox_with_config(|cfg| { + cfg.sync_project_workspace_dependencies = true; + cfg.dependency_version_format = format.clone(); + }); - update_toolchain_config( - fixture.path(), - "syncProjectWorkspaceDependencies: false", - "syncProjectWorkspaceDependencies: true", - ); - - append_toolchain_config( - fixture.path(), - &format!(" dependencyVersionFormat: {}", format), - ); - - create_moon_command(fixture.path()) - .arg("run") - .arg("dependsOn:standard") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("dependsOn:standard"); + }); // deps-c does not have a `package.json` on purpose assert_snapshot!( - format!("format_{}", format), - read_to_string(fixture.path().join("depends-on/package.json")).unwrap() + format!("format_{:?}", format), + read_to_string(sandbox.path().join("depends-on/package.json")).unwrap() ); } #[test] fn syncs_as_file_dependency() { - test_depends_on_format("file"); + test_depends_on_format(NodeVersionFormat::File); } #[test] fn syncs_as_link_dependency() { - test_depends_on_format("link"); + test_depends_on_format(NodeVersionFormat::Link); } #[test] fn syncs_as_star_dependency() { - test_depends_on_format("star"); + test_depends_on_format(NodeVersionFormat::Star); } #[test] fn syncs_as_version_dependency() { - test_depends_on_format("version"); + test_depends_on_format(NodeVersionFormat::Version); } #[test] fn syncs_as_version_caret_dependency() { - test_depends_on_format("version-caret"); + test_depends_on_format(NodeVersionFormat::VersionCaret); } #[test] fn syncs_as_version_tilde_dependency() { - test_depends_on_format("version-tilde"); + test_depends_on_format(NodeVersionFormat::VersionTilde); } #[test] fn syncs_as_workspace_dependency() { - test_depends_on_format("workspace"); + test_depends_on_format(NodeVersionFormat::Workspace); } #[test] fn syncs_as_workspace_caret_dependency() { - test_depends_on_format("workspace-caret"); + test_depends_on_format(NodeVersionFormat::WorkspaceCaret); } #[test] fn syncs_as_workspace_tilde_dependency() { - test_depends_on_format("workspace-tilde"); + test_depends_on_format(NodeVersionFormat::WorkspaceTilde); } #[test] fn syncs_depends_on_with_scopes() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox_with_config(|cfg| { + cfg.sync_project_workspace_dependencies = true; + }); - update_toolchain_config( - fixture.path(), - "syncProjectWorkspaceDependencies: false", - "syncProjectWorkspaceDependencies: true", - ); - - create_moon_command(fixture.path()) - .arg("run") - .arg("dependsOnScopes:standard") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("dependsOnScopes:standard"); + }); // deps-c does not have a `package.json` on purpose assert_snapshot!( - read_to_string(fixture.path().join("depends-on-scopes/package.json")).unwrap() + read_to_string(sandbox.path().join("depends-on-scopes/package.json")).unwrap() ); } } @@ -565,54 +589,50 @@ mod npm { #[test] fn installs_correct_version() { - let fixture = create_sandbox_with_git("node-npm"); + let sandbox = depman_sandbox("npm"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("npm:version") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("npm:version"); + }); - assert!(predicate::str::contains("8.0.0").eval(&get_assert_output(&assert))); + assert!(predicate::str::contains("8.0.0").eval(&assert.output())); } #[test] fn can_install_a_dep() { - let fixture = create_sandbox_with_git("node-npm"); + let sandbox = depman_sandbox("npm"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("npm:installDep") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("npm:installDep"); + }); assert.success(); } #[test] fn can_run_a_script() { - let fixture = create_sandbox_with_git("node-npm"); + let sandbox = depman_sandbox("npm"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("npm:runScript") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("npm:runScript"); + }); - assert!(predicate::str::contains("test").eval(&get_assert_output(&assert))); + assert!(predicate::str::contains("test").eval(&assert.output())); assert.success(); } #[test] fn can_run_a_deps_bin() { - let fixture = create_sandbox_with_git("node-npm"); + let sandbox = depman_sandbox("npm"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("npm:runDep") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("npm:runDep"); + }); assert!( predicate::str::contains("All matched files use Prettier code style!") - .eval(&get_assert_output(&assert)) + .eval(&assert.output()) ); assert.success(); @@ -620,25 +640,25 @@ mod npm { #[test] fn installs_deps_in_non_workspace_project() { - let fixture = create_sandbox_with_git("node-npm"); + let sandbox = depman_sandbox("npm"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("notInWorkspace:noop") - // Run other package so we can see both working - .arg("npm:noop") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("notInWorkspace:noop") + // Run other package so we can see both working + .arg("npm:noop"); + }); assert!(predicate::str::contains("npm install") .count(2) - .eval(&get_assert_output(&assert))); + .eval(&assert.output())); - assert!(fixture.path().join("package-lock.json").exists()); - assert!(fixture + assert!(sandbox.path().join("package-lock.json").exists()); + assert!(sandbox .path() .join("not-in-workspace/package-lock.json") .exists()); - assert!(fixture + assert!(sandbox .path() .join("not-in-workspace/node_modules") .exists()); @@ -649,60 +669,55 @@ mod npm { mod pnpm { use super::*; - use std::fs; #[test] fn installs_correct_version() { - let fixture = create_sandbox_with_git("node-pnpm"); + let sandbox = depman_sandbox("pnpm"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("pnpm:version") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("pnpm:version"); + }); - assert!(predicate::str::contains("7.5.0").eval(&get_assert_output(&assert))); + assert!(predicate::str::contains("7.5.0").eval(&assert.output())); } #[test] fn can_install_a_dep() { - let fixture = create_sandbox_with_git("node-pnpm"); + let sandbox = depman_sandbox("pnpm"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("pnpm:installDep") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("pnpm:installDep"); + }); assert.success(); } #[test] fn can_run_a_script() { - let fixture = create_sandbox_with_git("node-pnpm"); + let sandbox = depman_sandbox("pnpm"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("pnpm:runScript") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("pnpm:runScript"); + }); - assert!(predicate::str::contains("lint").eval(&get_assert_output(&assert))); + assert!(predicate::str::contains("lint").eval(&assert.output())); assert.success(); } #[test] fn can_run_a_deps_bin_isolated() { - let fixture = create_sandbox_with_git("node-pnpm"); + let sandbox = depman_sandbox("pnpm"); - fs::write(fixture.path().join(".npmrc"), "node-linker=isolated").unwrap(); + sandbox.create_file(".npmrc", "node-linker=isolated"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("pnpm:runDep") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("pnpm:runDep"); + }); assert!( predicate::str::contains("All matched files use Prettier code style!") - .eval(&get_assert_output(&assert)) + .eval(&assert.output()) ); assert.success(); @@ -710,18 +725,17 @@ mod pnpm { #[test] fn can_run_a_deps_bin_hoisted() { - let fixture = create_sandbox_with_git("node-pnpm"); + let sandbox = depman_sandbox("pnpm"); - fs::write(fixture.path().join(".npmrc"), "node-linker=hoisted").unwrap(); + sandbox.create_file(".npmrc", "node-linker=hoisted"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("pnpm:runDep") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("pnpm:runDep"); + }); assert!( predicate::str::contains("All matched files use Prettier code style!") - .eval(&get_assert_output(&assert)) + .eval(&assert.output()) ); assert.success(); @@ -730,23 +744,23 @@ mod pnpm { // NOTE: pnpm does not support nested lockfiles. // #[test] // fn installs_deps_in_non_workspace_project() { - // let fixture = create_sandbox_with_git("node-pnpm"); + // let sandbox = depman_sandbox("pnpm"); - // let assert = create_moon_command(fixture.path()) + // let assert = create_moon_command(sandbox.path()) // .arg("run") // .arg("notInWorkspace:noop") // // Run other package so we can see both working // .arg("pnpm:noop") // .assert(); - // assert_snapshot!(get_assert_output(&assert)); + // assert_snapshot!(assert.output()); - // assert!(fixture.path().join("pnpm-lock.yaml").exists()); - // assert!(fixture + // assert!(sandbox.path().join("pnpm-lock.yaml").exists()); + // assert!(sandbox // .path() // .join("not-in-workspace/pnpm-lock.yaml") // .exists()); - // assert!(fixture + // assert!(sandbox // .path() // .join("not-in-workspace/node_modules") // .exists()); @@ -760,54 +774,50 @@ mod yarn1 { #[test] fn installs_correct_version() { - let fixture = create_sandbox_with_git("node-yarn1"); + let sandbox = depman_sandbox("yarn1"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("yarn:version") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("yarn1:version"); + }); - assert!(predicate::str::contains("1.22.0").eval(&get_assert_output(&assert))); + assert!(predicate::str::contains("1.22.0").eval(&assert.output())); } #[test] fn can_install_a_dep() { - let fixture = create_sandbox_with_git("node-yarn1"); + let sandbox = depman_sandbox("yarn1"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("yarn:installDep") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("yarn1:installDep"); + }); assert.success(); } #[test] fn can_run_a_script() { - let fixture = create_sandbox_with_git("node-yarn1"); + let sandbox = depman_sandbox("yarn1"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("yarn:runScript") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("yarn1:runScript"); + }); - assert!(predicate::str::contains("build").eval(&get_assert_output(&assert))); + assert!(predicate::str::contains("build").eval(&assert.output())); assert.success(); } #[test] fn can_run_a_deps_bin() { - let fixture = create_sandbox_with_git("node-yarn1"); + let sandbox = depman_sandbox("yarn1"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("yarn:runDep") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("yarn1:runDep"); + }); assert!( predicate::str::contains("All matched files use Prettier code style!") - .eval(&get_assert_output(&assert)) + .eval(&assert.output()) ); assert.success(); @@ -815,22 +825,22 @@ mod yarn1 { #[test] fn installs_deps_in_non_workspace_project() { - let fixture = create_sandbox_with_git("node-yarn1"); + let sandbox = depman_sandbox("yarn1"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("notInWorkspace:noop") - // Run other package so we can see both working - .arg("yarn:noop") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("notInWorkspace:noop") + // Run other package so we can see both working + .arg("yarn1:noop"); + }); assert!(predicate::str::contains("yarn install") .count(2) - .eval(&get_assert_output(&assert))); + .eval(&assert.output())); - assert!(fixture.path().join("yarn.lock").exists()); - assert!(fixture.path().join("not-in-workspace/yarn.lock").exists()); - assert!(fixture + assert!(sandbox.path().join("yarn.lock").exists()); + assert!(sandbox.path().join("not-in-workspace/yarn.lock").exists()); + assert!(sandbox .path() .join("not-in-workspace/node_modules") .exists()); @@ -844,54 +854,50 @@ mod yarn { #[test] fn installs_correct_version() { - let fixture = create_sandbox_with_git("node-yarn"); + let sandbox = depman_sandbox("yarn"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("yarn:version") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("yarn:version"); + }); - assert!(predicate::str::contains("3.0.0").eval(&get_assert_output(&assert))); + assert!(predicate::str::contains("3.3.0").eval(&assert.output())); } #[test] fn can_install_a_dep() { - let fixture = create_sandbox_with_git("node-yarn"); + let sandbox = depman_sandbox("yarn"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("yarn:installDep") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("yarn:installDep"); + }); assert.success(); } #[test] fn can_run_a_script() { - let fixture = create_sandbox_with_git("node-yarn"); + let sandbox = depman_sandbox("yarn"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("yarn:runScript") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("yarn:runScript"); + }); - assert!(predicate::str::contains("build").eval(&get_assert_output(&assert))); + assert!(predicate::str::contains("build").eval(&assert.output())); assert.success(); } #[test] fn can_run_a_deps_bin() { - let fixture = create_sandbox_with_git("node-yarn"); + let sandbox = depman_sandbox("yarn"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("yarn:runDep") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("yarn:runDep"); + }); assert!( predicate::str::contains("All matched files use Prettier code style!") - .eval(&get_assert_output(&assert)) + .eval(&assert.output()) ); assert.success(); @@ -899,22 +905,22 @@ mod yarn { #[test] fn installs_deps_in_non_workspace_project() { - let fixture = create_sandbox_with_git("node-yarn"); + let sandbox = depman_sandbox("yarn"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("notInWorkspace:noop") - // Run other package so we can see both working - .arg("yarn:noop") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("notInWorkspace:noop") + // Run other package so we can see both working + .arg("yarn:noop"); + }); assert!(predicate::str::contains("yarn install") .count(2) - .eval(&get_assert_output(&assert))); + .eval(&assert.output())); - assert!(fixture.path().join("yarn.lock").exists()); - assert!(fixture.path().join("not-in-workspace/yarn.lock").exists()); - assert!(fixture + assert!(sandbox.path().join("yarn.lock").exists()); + assert!(sandbox.path().join("not-in-workspace/yarn.lock").exists()); + assert!(sandbox .path() .join("not-in-workspace/node_modules") .exists()); @@ -928,16 +934,16 @@ mod profile { #[test] fn record_a_cpu_profile() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - create_moon_command(fixture.path()) - .arg("run") - .arg("--profile") - .arg("cpu") - .arg("node:standard") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("--profile") + .arg("cpu") + .arg("node:standard"); + }); - let profile = fixture + let profile = sandbox .path() .join(".moon/cache/states/node/standard/snapshot.cpuprofile"); @@ -946,16 +952,16 @@ mod profile { #[test] fn record_a_heap_profile() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - create_moon_command(fixture.path()) - .arg("run") - .arg("--profile") - .arg("heap") - .arg("node:standard") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("--profile") + .arg("heap") + .arg("node:standard"); + }); - let profile = fixture + let profile = sandbox .path() .join(".moon/cache/states/node/standard/snapshot.heapprofile"); @@ -965,17 +971,25 @@ mod profile { mod aliases { use super::*; + use moon_test_utils::get_project_graph_aliases_fixture_configs; #[test] fn runs_via_package_name() { - let fixture = create_sandbox_with_git("project-graph/aliases"); + let (workspace_config, toolchain_config, projects_config) = + get_project_graph_aliases_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "project-graph/aliases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("@scope/pkg-foo:standard") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("@scope/pkg-foo:standard"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } } @@ -985,15 +999,14 @@ mod non_js_bins { #[test] fn works_with_esbuild() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("esbuild:build") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("esbuild:build"); + }); assert_eq!( - fs::read_to_string(fixture.path().join("esbuild/output.js")).unwrap(), + fs::read_to_string(sandbox.path().join("esbuild/output.js")).unwrap(), "(() => {\n // input.js\n var ESBUILD = \"esbuild\";\n})();\n" ); @@ -1002,15 +1015,14 @@ mod non_js_bins { #[test] fn works_with_swc() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("swc:build") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("swc:build"); + }); assert_eq!( - fs::read_to_string(fixture.path().join("swc/output.js")).unwrap(), + fs::read_to_string(sandbox.path().join("swc/output.js")).unwrap(), "export var SWC = \"swc\";\n\n\n//# sourceMappingURL=output.js.map" ); @@ -1023,92 +1035,85 @@ mod typescript { #[test] fn creates_missing_tsconfig() { - let fixture = create_sandbox_with_git("typescript"); + let sandbox = typescript_sandbox(|cfg| { + cfg.create_missing_config = true; + }); - assert!(!fixture.path().join("create-config/tsconfig.json").exists()); + assert!(!sandbox.path().join("create-config/tsconfig.json").exists()); - create_moon_command(fixture.path()) - .arg("run") - .arg("create-config:test") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("create-config:noop"); + }); - assert!(fixture.path().join("create-config/tsconfig.json").exists()); + assert!(sandbox.path().join("create-config/tsconfig.json").exists()); // root - assert_snapshot!(read_to_string(fixture.path().join("tsconfig.json")).unwrap()); + assert_snapshot!(read_to_string(sandbox.path().join("tsconfig.json")).unwrap()); // project assert_snapshot!( - read_to_string(fixture.path().join("create-config/tsconfig.json")).unwrap() + read_to_string(sandbox.path().join("create-config/tsconfig.json")).unwrap() ); } #[test] fn doesnt_create_missing_tsconfig_if_setting_off() { - let fixture = create_sandbox_with_git("typescript"); - - update_toolchain_config( - fixture.path(), - "createMissingConfig: true", - "createMissingConfig: false", - ); + let sandbox = typescript_sandbox(|cfg| { + cfg.create_missing_config = false; + }); - assert!(!fixture.path().join("create-config/tsconfig.json").exists()); + assert!(!sandbox.path().join("create-config/tsconfig.json").exists()); - create_moon_command(fixture.path()) - .arg("run") - .arg("create-config:test") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("create-config:noop"); + }); - assert!(!fixture.path().join("create-config/tsconfig.json").exists()); + assert!(!sandbox.path().join("create-config/tsconfig.json").exists()); } #[test] fn doesnt_create_missing_tsconfig_if_syncing_off() { - let fixture = create_sandbox_with_git("typescript"); - - update_toolchain_config( - fixture.path(), - "syncProjectReferences: true", - "syncProjectReferences: false", - ); + let sandbox = typescript_sandbox(|cfg| { + cfg.create_missing_config = true; + cfg.sync_project_references = false; + }); - assert!(!fixture.path().join("create-config/tsconfig.json").exists()); + assert!(!sandbox.path().join("create-config/tsconfig.json").exists()); - create_moon_command(fixture.path()) - .arg("run") - .arg("create-config:test") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("create-config:noop"); + }); - assert!(!fixture.path().join("create-config/tsconfig.json").exists()); + assert!(!sandbox.path().join("create-config/tsconfig.json").exists()); } #[test] fn doesnt_create_missing_tsconfig_if_project_disabled() { - let fixture = create_sandbox_with_git("typescript"); + let sandbox = typescript_sandbox(|cfg| { + cfg.create_missing_config = true; + cfg.sync_project_references = true; + }); - assert!(!fixture.path().join("create-config/tsconfig.json").exists()); + assert!(!sandbox.path().join("create-config/tsconfig.json").exists()); - create_moon_command(fixture.path()) - .arg("run") - .arg("create-config-disabled:test") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("create-config-disabled:noop"); + }); - assert!(!fixture.path().join("create-config/tsconfig.json").exists()); + assert!(!sandbox.path().join("create-config/tsconfig.json").exists()); } #[test] fn syncs_ref_to_root_config() { - let fixture = create_sandbox_with_git("typescript"); + let sandbox = typescript_sandbox(|_| {}); - let initial_root = read_to_string(fixture.path().join("tsconfig.json")).unwrap(); + let initial_root = read_to_string(sandbox.path().join("tsconfig.json")).unwrap(); - create_moon_command(fixture.path()) - .arg("run") - .arg("create-config:test") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("create-config:noop"); + }); - let synced_root = read_to_string(fixture.path().join("tsconfig.json")).unwrap(); + let synced_root = read_to_string(sandbox.path().join("tsconfig.json")).unwrap(); assert_ne!(initial_root, synced_root); assert_snapshot!(synced_root); @@ -1116,21 +1121,20 @@ mod typescript { #[test] fn syncs_depends_on_as_refs() { - let fixture = create_sandbox_with_git("typescript"); + let sandbox = typescript_sandbox(|_| {}); - assert!(!fixture + assert!(!sandbox .path() .join("syncs-deps-refs/tsconfig.json") .exists()); - create_moon_command(fixture.path()) - .arg("run") - .arg("syncs-deps-refs:test") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("syncs-deps-refs:noop"); + }); // should not have `deps-no-config-disabled` or `deps-with-config-disabled` assert_snapshot!( - read_to_string(fixture.path().join("syncs-deps-refs/tsconfig.json")).unwrap() + read_to_string(sandbox.path().join("syncs-deps-refs/tsconfig.json")).unwrap() ); } @@ -1139,41 +1143,31 @@ mod typescript { #[test] fn routes_to_cache() { - let fixture = create_sandbox_with_git("typescript"); - - update_toolchain_config( - fixture.path(), - "routeOutDirToCache: false", - "routeOutDirToCache: true", - ); + let sandbox = typescript_sandbox(|cfg| { + cfg.route_out_dir_to_cache = true; + }); - create_moon_command(fixture.path()) - .arg("run") - .arg("out-dir-routing:test") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("out-dir-routing:noop"); + }); assert_snapshot!( - read_to_string(fixture.path().join("out-dir-routing/tsconfig.json")).unwrap() + read_to_string(sandbox.path().join("out-dir-routing/tsconfig.json")).unwrap() ); } #[test] fn routes_to_cache_when_no_compiler_options() { - let fixture = create_sandbox_with_git("typescript"); + let sandbox = typescript_sandbox(|cfg| { + cfg.route_out_dir_to_cache = true; + }); - update_toolchain_config( - fixture.path(), - "routeOutDirToCache: false", - "routeOutDirToCache: true", - ); - - create_moon_command(fixture.path()) - .arg("run") - .arg("out-dir-routing-no-options:test") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("out-dir-routing-no-options:noop"); + }); assert_snapshot!(read_to_string( - fixture + sandbox .path() .join("out-dir-routing-no-options/tsconfig.json") ) @@ -1182,15 +1176,16 @@ mod typescript { #[test] fn doesnt_route_to_cache_if_disabled() { - let fixture = create_sandbox_with_git("typescript"); + let sandbox = typescript_sandbox(|cfg| { + cfg.route_out_dir_to_cache = false; + }); - create_moon_command(fixture.path()) - .arg("run") - .arg("out-dir-routing:test") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("out-dir-routing:noop"); + }); assert_snapshot!( - read_to_string(fixture.path().join("out-dir-routing/tsconfig.json")).unwrap() + read_to_string(sandbox.path().join("out-dir-routing/tsconfig.json")).unwrap() ); } } @@ -1200,63 +1195,49 @@ mod typescript { #[test] fn maps_paths() { - let fixture = create_sandbox_with_git("typescript"); + let sandbox = typescript_sandbox(|cfg| { + cfg.sync_project_references_to_paths = true; + }); - update_toolchain_config( - fixture.path(), - "syncProjectReferencesToPaths: false", - "syncProjectReferencesToPaths: true", - ); - - create_moon_command(fixture.path()) - .arg("run") - .arg("syncs-paths-refs:test") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("syncs-paths-refs:noop"); + }); assert_snapshot!( - read_to_string(fixture.path().join("syncs-paths-refs/tsconfig.json")).unwrap() + read_to_string(sandbox.path().join("syncs-paths-refs/tsconfig.json")).unwrap() ); } #[test] fn doesnt_map_paths_if_no_refs() { - let fixture = create_sandbox_with_git("typescript"); + let sandbox = typescript_sandbox(|cfg| { + cfg.sync_project_references = false; + cfg.sync_project_references_to_paths = true; + }); - update_toolchain_config( - fixture.path(), - "syncProjectReferences: true", - "syncProjectReferences: false", - ); + std::fs::remove_file(sandbox.path().join("syncs-paths-refs/moon.yml")).unwrap(); - update_toolchain_config( - fixture.path(), - "syncProjectReferencesToPaths: false", - "syncProjectReferencesToPaths: true", - ); - - std::fs::remove_file(fixture.path().join("syncs-paths-refs/moon.yml")).unwrap(); - - create_moon_command(fixture.path()) - .arg("run") - .arg("syncs-paths-refs:test") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("syncs-paths-refs:noop"); + }); assert_snapshot!( - read_to_string(fixture.path().join("syncs-paths-refs/tsconfig.json")).unwrap() + read_to_string(sandbox.path().join("syncs-paths-refs/tsconfig.json")).unwrap() ); } #[test] fn doesnt_map_paths_if_disabled() { - let fixture = create_sandbox_with_git("typescript"); + let sandbox = typescript_sandbox(|cfg| { + cfg.sync_project_references_to_paths = false; + }); - create_moon_command(fixture.path()) - .arg("run") - .arg("syncs-paths-refs:test") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("syncs-paths-refs:noop"); + }); assert_snapshot!( - read_to_string(fixture.path().join("syncs-paths-refs/tsconfig.json")).unwrap() + read_to_string(sandbox.path().join("syncs-paths-refs/tsconfig.json")).unwrap() ); } } @@ -1267,24 +1248,20 @@ mod workspace_overrides { #[test] fn can_override_version() { - let fixture = create_sandbox_with_git("node"); - - update_toolchain_config( - fixture.path(), - "dedupeOnLockfileChange: true", - "dedupeOnLockfileChange: false", - ); + let sandbox = node_sandbox_with_config(|cfg| { + cfg.dedupe_on_lockfile_change = false; + }); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:version") - .arg("versionOverride:version") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("node:version") + .arg("versionOverride:version"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains("v18.0.0").eval(&output)); - assert!(predicate::str::contains("v16.1.0").eval(&output)); + assert!(predicate::str::contains("v19.0.0").eval(&output)); assert.success(); } @@ -1292,34 +1269,32 @@ mod workspace_overrides { mod affected_files { use super::*; - use std::fs; #[test] fn uses_dot_when_not_affected() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:affectedFiles"); + }); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:affectedFiles") - .assert(); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains("Args: .\n").eval(&output)); } #[test] fn uses_rel_paths_when_affected() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); - fs::write(fixture.path().join("base/input1.js"), "").unwrap(); - fs::write(fixture.path().join("base/input2.js"), "").unwrap(); + sandbox.create_file("base/input1.js", ""); + sandbox.create_file("base/input2.js", ""); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:affectedFiles") - .arg("--affected") - .assert(); - let output = get_assert_output(&assert); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:affectedFiles").arg("--affected"); + }); + + let output = assert.output(); if cfg!(windows) { assert!(predicate::str::contains("Args: .\\input1.js .\\input2.js").eval(&output)); @@ -1330,17 +1305,18 @@ mod affected_files { #[test] fn sets_env_var() { - let fixture = create_sandbox_with_git("node"); + let sandbox = node_sandbox(); + + sandbox.create_file("base/input1.js", ""); + sandbox.create_file("base/input2.js", ""); - fs::write(fixture.path().join("base/input1.js"), "").unwrap(); - fs::write(fixture.path().join("base/input2.js"), "").unwrap(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("node:affectedFilesEnvVar") + .arg("--affected"); + }); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("node:affectedFilesEnvVar") - .arg("--affected") - .assert(); - let output = get_assert_output(&assert); + let output = assert.output(); assert!( predicate::str::contains("MOON_AFFECTED_FILES=./input1.js,./input2.js").eval(&output) diff --git a/crates/cli/tests/run_system_test.rs b/crates/cli/tests/run_system_test.rs index 961df23fce7..bb2ab7b16e6 100644 --- a/crates/cli/tests/run_system_test.rs +++ b/crates/cli/tests/run_system_test.rs @@ -1,9 +1,29 @@ -mod utils; - -use insta::assert_snapshot; -use moon_utils::test::{create_moon_command, create_sandbox_with_git, get_assert_output}; -use predicates::prelude::*; -use utils::get_path_safe_output; +use moon_config::{RunnerConfig, WorkspaceConfig, WorkspaceProjects}; +use moon_test_utils::{ + assert_snapshot, create_sandbox_with_config, predicates::prelude::*, Sandbox, +}; +use moon_utils::string_vec; +use rustc_hash::FxHashMap; + +fn system_sandbox() -> Sandbox { + let workspace_config = WorkspaceConfig { + projects: WorkspaceProjects::Sources(FxHashMap::from_iter([ + ("unix".to_owned(), "unix".to_owned()), + ("windows".to_owned(), "windows".to_owned()), + ])), + runner: RunnerConfig { + // Avoid these in hashes or snapshots + implicit_inputs: string_vec![], + ..RunnerConfig::default() + }, + ..WorkspaceConfig::default() + }; + + let sandbox = create_sandbox_with_config("system", Some(&workspace_config), None, None); + + sandbox.enable_git(); + sandbox +} #[cfg(not(windows))] mod unix { @@ -11,157 +31,147 @@ mod unix { #[test] fn handles_echo() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:echo") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:echo"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn handles_ls() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:ls") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:ls"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn runs_bash_script() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:bash") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:bash"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn handles_process_exit_zero() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:exitZero") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:exitZero"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn handles_process_exit_nonzero() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:exitNonZero") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:exitNonZero"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn passes_args_through() { - let fixture = create_sandbox_with_git("system"); - - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:passthroughArgs") - .arg("--") - .arg("-aBc") - .arg("--opt") - .arg("value") - .arg("--optCamel=value") - .arg("foo") - .arg("'bar baz'") - .arg("--opt-kebab") - .arg("123") - .assert(); - - assert_snapshot!(get_assert_output(&assert)); + let sandbox = system_sandbox(); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("unix:passthroughArgs") + .arg("--") + .arg("-aBc") + .arg("--opt") + .arg("value") + .arg("--optCamel=value") + .arg("foo") + .arg("'bar baz'") + .arg("--opt-kebab") + .arg("123"); + }); + + assert_snapshot!(assert.output()); } #[test] fn sets_env_vars() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:envVars") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:envVars"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn inherits_moon_env_vars() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:envVarsMoon") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:envVarsMoon"); + }); - assert_snapshot!(get_path_safe_output(&assert, fixture.path())); + assert_snapshot!(assert.output()); } #[test] fn runs_from_project_root() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:runFromProject") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:runFromProject"); + }); - assert_snapshot!(get_path_safe_output(&assert, fixture.path())); + assert_snapshot!(assert.output()); } #[test] fn runs_from_workspace_root() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:runFromWorkspace") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:runFromWorkspace"); + }); - assert_snapshot!(get_path_safe_output(&assert, fixture.path())); + assert_snapshot!(assert.output()); } #[test] fn retries_on_failure_till_count() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:retryCount") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:retryCount"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn can_run_many_targets() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:foo") - .arg("unix:bar") - .arg("unix:baz") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("unix:foo") + .arg("unix:bar") + .arg("unix:baz"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains("unix:foo | foo").eval(&output)); assert!(predicate::str::contains("unix:bar | bar").eval(&output)); @@ -175,33 +185,30 @@ mod unix { #[test] fn uses_cache_on_subsequent_runs() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:outputs") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:outputs"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:outputs") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:outputs"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn creates_runfile() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - create_moon_command(fixture.path()) - .arg("run") - .arg("unix:outputs") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:outputs"); + }); - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/states/unix/runfile.json") .exists()); @@ -209,14 +216,13 @@ mod unix { #[tokio::test] async fn creates_run_state_cache() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - create_moon_command(fixture.path()) - .arg("run") - .arg("unix:outputs") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:outputs"); + }); - let cache_path = fixture + let cache_path = sandbox .path() .join(".moon/cache/states/unix/outputs/lastRun.json"); @@ -225,22 +231,22 @@ mod unix { let state = RunTargetState::load(cache_path, 0).await.unwrap(); assert_snapshot!(fs::read_to_string( - fixture + sandbox .path() .join(format!(".moon/cache/hashes/{}.json", state.hash)) ) .unwrap()); - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/outputs") .join(format!("{}.tar.gz", state.hash)) .exists()); - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/states/unix/outputs/stdout.log") .exists()); - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/states/unix/outputs/stderr.log") .exists()); @@ -249,51 +255,47 @@ mod unix { mod affected_files { use super::*; - use std::fs; #[test] fn uses_dot_when_not_affected() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:affectedFiles") - .assert(); - let output = get_assert_output(&assert); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:affectedFiles"); + }); + let output = assert.output(); assert!(predicate::str::contains("Args: .\n").eval(&output)); } #[test] fn uses_rel_paths_when_affected() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - fs::write(fixture.path().join("unix/input1.txt"), "").unwrap(); - fs::write(fixture.path().join("unix/input2.txt"), "").unwrap(); + sandbox.create_file("unix/input1.txt", ""); + sandbox.create_file("unix/input2.txt", ""); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:affectedFiles") - .arg("--affected") - .assert(); - let output = get_assert_output(&assert); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:affectedFiles").arg("--affected"); + }); + let output = assert.output(); assert!(predicate::str::contains("Args: ./input1.txt ./input2.txt").eval(&output)); } #[test] fn sets_env_var() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - fs::write(fixture.path().join("unix/input1.txt"), "").unwrap(); - fs::write(fixture.path().join("unix/input2.txt"), "").unwrap(); + sandbox.create_file("unix/input1.txt", ""); + sandbox.create_file("unix/input2.txt", ""); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unix:affectedFilesEnvVar") - .arg("--affected") - .assert(); - let output = get_assert_output(&assert); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("unix:affectedFilesEnvVar") + .arg("--affected"); + }); + let output = assert.output(); assert!( predicate::str::contains("MOON_AFFECTED_FILES=./input1.txt,./input2.txt") @@ -309,133 +311,125 @@ mod system_windows { #[test] fn runs_bat_script() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("windows:bat") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("windows:bat"); + }); - assert_snapshot!(get_path_safe_output(&assert, fixture.path())); + assert_snapshot!(assert.output()); } #[test] fn handles_process_exit_zero() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("windows:exitZero") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("windows:exitZero"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn handles_process_exit_nonzero() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("windows:exitNonZero") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("windows:exitNonZero"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn passes_args_through() { - let fixture = create_sandbox_with_git("system"); - - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("windows:passthroughArgs") - .arg("--") - .arg("-aBc") - .arg("--opt") - .arg("value") - .arg("--optCamel=value") - .arg("foo") - .arg("'bar baz'") - .arg("--opt-kebab") - .arg("123") - .assert(); - - assert_snapshot!(get_assert_output(&assert)); + let sandbox = system_sandbox(); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("windows:passthroughArgs") + .arg("--") + .arg("-aBc") + .arg("--opt") + .arg("value") + .arg("--optCamel=value") + .arg("foo") + .arg("'bar baz'") + .arg("--opt-kebab") + .arg("123"); + }); + + assert_snapshot!(assert.output()); } #[test] fn sets_env_vars() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("windows:envVars") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("windows:envVars"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn inherits_moon_env_vars() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("windows:envVarsMoon") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("windows:envVarsMoon"); + }); - assert_snapshot!(get_path_safe_output(&assert, fixture.path())); + assert_snapshot!(assert.output()); } #[test] fn runs_from_project_root() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("windows:runFromProject") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("windows:runFromProject"); + }); - assert_snapshot!(get_path_safe_output(&assert, fixture.path())); + assert_snapshot!(assert.output()); } #[test] fn runs_from_workspace_root() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("windows:runFromWorkspace") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("windows:runFromWorkspace"); + }); - assert_snapshot!(get_path_safe_output(&assert, fixture.path())); + assert_snapshot!(assert.output()); } #[test] fn retries_on_failure_till_count() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("windows:retryCount") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("windows:retryCount"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn can_run_many_targets() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("windows:foo") - .arg("windows:bar") - .arg("windows:baz") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("windows:foo") + .arg("windows:bar") + .arg("windows:baz"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains("windows:foo | foo").eval(&output)); assert!(predicate::str::contains("windows:bar | bar").eval(&output)); @@ -449,33 +443,30 @@ mod system_windows { #[test] fn uses_cache_on_subsequent_runs() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("windows:outputs") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("windows:outputs"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("windows:outputs") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("windows:outputs"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn creates_runfile() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - create_moon_command(fixture.path()) - .arg("run") - .arg("windows:outputs") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("windows:outputs"); + }); - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/states/windows/runfile.json") .exists()); @@ -483,14 +474,13 @@ mod system_windows { #[tokio::test] async fn creates_run_state_cache() { - let fixture = create_sandbox_with_git("system"); + let sandbox = system_sandbox(); - create_moon_command(fixture.path()) - .arg("run") - .arg("windows:outputs") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("windows:outputs"); + }); - let cache_path = fixture + let cache_path = sandbox .path() .join(".moon/cache/states/windows/outputs/lastRun.json"); @@ -499,22 +489,22 @@ mod system_windows { let state = RunTargetState::load(cache_path, 0).await.unwrap(); assert_snapshot!(fs::read_to_string( - fixture + sandbox .path() .join(format!(".moon/cache/hashes/{}.json", state.hash)) ) .unwrap()); - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/outputs") .join(format!("{}.tar.gz", state.hash)) .exists()); - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/states/windows/outputs/stdout.log") .exists()); - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/states/windows/outputs/stderr.log") .exists()); diff --git a/crates/cli/tests/run_test.rs b/crates/cli/tests/run_test.rs index 72d86bd0d85..24fbf095857 100644 --- a/crates/cli/tests/run_test.rs +++ b/crates/cli/tests/run_test.rs @@ -1,15 +1,38 @@ -mod utils; - -use insta::assert_snapshot; use moon_cache::CacheEngine; -use moon_utils::path::standardize_separators; -use moon_utils::test::{ - create_moon_command, create_sandbox, create_sandbox_with_git, get_assert_output, +use moon_config::WorkspaceConfig; +use moon_test_utils::{ + assert_snapshot, create_sandbox_with_config, get_cases_fixture_configs, predicates::prelude::*, + Sandbox, }; -use predicates::prelude::*; use std::fs; use std::path::Path; -use utils::get_path_safe_output; + +fn cases_sandbox() -> Sandbox { + let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); + + create_sandbox_with_config( + "cases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ) +} + +fn cases_sandbox_with_config(callback: C) -> Sandbox +where + C: FnOnce(&mut WorkspaceConfig), +{ + let (mut workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); + + callback(&mut workspace_config); + + create_sandbox_with_config( + "cases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ) +} async fn extract_hash_from_run(fixture: &Path, target_id: &str) -> String { let engine = CacheEngine::load(fixture).await.unwrap(); @@ -20,166 +43,155 @@ async fn extract_hash_from_run(fixture: &Path, target_id: &str) -> String { #[test] fn errors_for_unknown_project() { - let fixture = create_sandbox("cases"); + let sandbox = cases_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("unknown:test") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unknown:test"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn errors_for_unknown_task_in_project() { - let fixture = create_sandbox("cases"); + let sandbox = cases_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("base:unknown") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("base:unknown"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn errors_for_unknown_all_target() { - let fixture = create_sandbox("cases"); + let sandbox = cases_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg(":unknown") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg(":unknown"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn errors_for_cycle_in_task_deps() { - let fixture = create_sandbox("cases"); + let sandbox = cases_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("depsA:taskCycle") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("depsA:taskCycle"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[cfg(not(windows))] mod general { use super::*; - use utils::append_workspace_config; #[test] fn logs_command_for_project_root() { - let fixture = create_sandbox_with_git("cases"); - - append_workspace_config(fixture.path(), "runner:\n logRunningCommand: true"); + let sandbox = cases_sandbox_with_config(|cfg| { + cfg.runner.log_running_command = true; + }); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("base:runFromProject") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("base:runFromProject"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn logs_command_for_workspace_root() { - let fixture = create_sandbox_with_git("cases"); - - append_workspace_config(fixture.path(), "runner:\n logRunningCommand: true"); + let sandbox = cases_sandbox_with_config(|cfg| { + cfg.runner.log_running_command = true; + }); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("base:runFromWorkspace") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("base:runFromWorkspace"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } } mod configs { use super::*; - use std::path::PathBuf; #[test] fn bubbles_up_invalid_workspace_config() { - let fixture = create_sandbox("config-invalid-workspace"); + let sandbox = cases_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("project:task") - .assert(); + sandbox.create_file(".moon/workspace.yml", "projects: true"); - assert_snapshot!(standardize_separators(get_path_safe_output( - &assert, - &PathBuf::from("./fake/path") - ))); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("base:noop"); + }); + + assert_snapshot!(assert.output_standardized()); } #[test] fn bubbles_up_invalid_global_project_config() { - let fixture = create_sandbox("config-invalid-global-project"); + let sandbox = cases_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("project:task") - .assert(); + sandbox.create_file(".moon/project.yml", "tasks: 123"); - assert_snapshot!(standardize_separators(get_path_safe_output( - &assert, - &PathBuf::from("./fake/path") - ))); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("base:noop"); + }); + + assert_snapshot!(assert.output_standardized()); } #[test] fn bubbles_up_invalid_project_config() { - let fixture = create_sandbox("config-invalid-project"); + let sandbox = cases_sandbox(); + + sandbox.create_file("base/moon.yml", "project:\n type: library"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("test:task") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("base:noop"); + }); - assert_snapshot!(standardize_separators(get_path_safe_output( - &assert, - &PathBuf::from("./fake/path") - ))); + assert_snapshot!(assert.output_standardized()); } } mod logs { use super::*; - use moon_utils::test::create_sandbox_with_git; #[test] fn creates_log_file() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - create_moon_command(fixture.path()) - .arg("--logFile=output.log") - .arg("run") - .arg("node:standard") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("--logFile=output.log") + .arg("run") + .arg("node:standard"); + }); - let output_path = fixture.path().join("output.log"); + let output_path = sandbox.path().join("output.log"); assert!(output_path.exists()); } #[test] fn creates_nested_log_file() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - create_moon_command(fixture.path()) - .arg("--logFile=nested/output.log") - .arg("run") - .arg("node:standard") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("--logFile=nested/output.log") + .arg("run") + .arg("node:standard"); + }); - let output_path = fixture.path().join("nested/output.log"); + let output_path = sandbox.path().join("nested/output.log"); assert!(output_path.exists()); } @@ -190,92 +202,91 @@ mod dependencies { #[test] fn runs_the_graph_in_order() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("depsA:dependencyOrder") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("depsA:dependencyOrder"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn runs_the_graph_in_order_not_from_head() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("depsB:dependencyOrder") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("depsB:dependencyOrder"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn can_run_deps_in_serial() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("dependsOn:serialDeps") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("dependsOn:serialDeps"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[tokio::test] async fn generates_unique_hashes_for_each_target() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:withDeps") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:withDeps"); + }); assert_eq!( - extract_hash_from_run(fixture.path(), "outputs:asDep").await, - "92c5b8c6dceccedc0547032c9eeb5be64d225545ac679c6b1bb7d41baf892d77" + extract_hash_from_run(sandbox.path(), "outputs:asDep").await, + "fff0ae3851832c56a94c7bcc8996abcc1bd89a29d6d7d2cb51441236b6e4c9ec" ); assert_eq!( - extract_hash_from_run(fixture.path(), "outputs:withDeps").await, - "3d4fef133338cff776bd701538bf4861c92dce2e1f1282feb35d42a1b13c4b3b" + extract_hash_from_run(sandbox.path(), "outputs:withDeps").await, + "94afe73174ac40676d51e7574947265af2bc8ffe64ced566151572dd64e19f8f" ); } #[tokio::test] async fn changes_primary_hash_if_deps_hash_changes() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:withDeps") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:withDeps"); + }); assert_eq!( - extract_hash_from_run(fixture.path(), "outputs:asDep").await, - "92c5b8c6dceccedc0547032c9eeb5be64d225545ac679c6b1bb7d41baf892d77" + extract_hash_from_run(sandbox.path(), "outputs:asDep").await, + "fff0ae3851832c56a94c7bcc8996abcc1bd89a29d6d7d2cb51441236b6e4c9ec" ); assert_eq!( - extract_hash_from_run(fixture.path(), "outputs:withDeps").await, - "3d4fef133338cff776bd701538bf4861c92dce2e1f1282feb35d42a1b13c4b3b" + extract_hash_from_run(sandbox.path(), "outputs:withDeps").await, + "94afe73174ac40676d51e7574947265af2bc8ffe64ced566151572dd64e19f8f" ); // Create an `inputs` file for `outputs:asDep` - fs::write(fixture.path().join("outputs/random.js"), "").unwrap(); + sandbox.create_file("outputs/random.js", ""); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:withDeps") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:withDeps"); + }); assert_eq!( - extract_hash_from_run(fixture.path(), "outputs:asDep").await, - "296411f059717096bc78f8bd1a5f33019de11bd57480c900d9d3a25ea5441ca1" + extract_hash_from_run(sandbox.path(), "outputs:asDep").await, + "09bfb40435c99cb3d2ab88697ca8fc72fac361e4e456659cab336acba46bea62" ); assert_eq!( - extract_hash_from_run(fixture.path(), "outputs:withDeps").await, - "b7af1e952f2146ce128b74809598233fdc17cc0dadd3e863edaf1cb8c69f019b" + extract_hash_from_run(sandbox.path(), "outputs:withDeps").await, + "a6dcc84a82022d4e8a1003752dca38045b3af9554a66a8f8d520891ceddfcb3c" ); } } @@ -285,37 +296,35 @@ mod target_scopes { #[test] fn errors_for_deps_scope() { - let fixture = create_sandbox("cases"); + let sandbox = cases_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("^:test") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("^:test"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn errors_for_self_scope() { - let fixture = create_sandbox("cases"); + let sandbox = cases_sandbox(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("~:test") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("~:test"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn supports_all_scope() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg(":all") - .assert(); - let output = get_assert_output(&assert); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg(":all"); + }); + let output = assert.output(); assert!(predicate::str::contains("targetScopeA:all").eval(&output)); assert!(predicate::str::contains("targetScopeB:all").eval(&output)); @@ -325,13 +334,13 @@ mod target_scopes { #[test] fn supports_deps_scope_in_task() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("targetScopeA:deps") - .assert(); - let output = get_assert_output(&assert); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("targetScopeA:deps"); + }); + let output = assert.output(); assert!(predicate::str::contains("targetScopeA:deps").eval(&output)); assert!(predicate::str::contains("depsA:standard").eval(&output)); @@ -342,13 +351,13 @@ mod target_scopes { #[test] fn supports_self_scope_in_task() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("targetScopeB:self") - .assert(); - let output = get_assert_output(&assert); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("targetScopeB:self"); + }); + let output = assert.output(); assert!(predicate::str::contains("targetScopeB:self").eval(&output)); assert!(predicate::str::contains("scope=self").eval(&output)); @@ -363,28 +372,28 @@ mod outputs { #[tokio::test] async fn errors_if_output_missing() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:missingOutput") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:missingOutput"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains("Target outputs:missingOutput defines the output unknown, but this output does not exist after being ran.").eval(&output)); } #[tokio::test] async fn doesnt_cache_if_cache_disabled() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:noCache") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:noCache"); + }); - let hash = extract_hash_from_run(fixture.path(), "outputs:noCache").await; + let hash = extract_hash_from_run(sandbox.path(), "outputs:noCache").await; assert_eq!(hash, ""); @@ -393,24 +402,24 @@ mod outputs { #[tokio::test] async fn caches_single_file() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:generateFile") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFile"); + }); - let hash = extract_hash_from_run(fixture.path(), "outputs:generateFile").await; + let hash = extract_hash_from_run(sandbox.path(), "outputs:generateFile").await; // hash - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/hashes") .join(format!("{}.json", hash)) .exists()); // outputs - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/outputs") .join(format!("{}.tar.gz", hash)) @@ -419,24 +428,24 @@ mod outputs { #[tokio::test] async fn caches_multiple_files() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:generateFiles") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFiles"); + }); - let hash = extract_hash_from_run(fixture.path(), "outputs:generateFiles").await; + let hash = extract_hash_from_run(sandbox.path(), "outputs:generateFiles").await; // hash - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/hashes") .join(format!("{}.json", hash)) .exists()); // outputs - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/outputs") .join(format!("{}.tar.gz", hash)) @@ -445,24 +454,24 @@ mod outputs { #[tokio::test] async fn caches_single_folder() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:generateFolder") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFolder"); + }); - let hash = extract_hash_from_run(fixture.path(), "outputs:generateFolder").await; + let hash = extract_hash_from_run(sandbox.path(), "outputs:generateFolder").await; // hash - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/hashes") .join(format!("{}.json", hash)) .exists()); // outputs - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/outputs") .join(format!("{}.tar.gz", hash)) @@ -471,24 +480,24 @@ mod outputs { #[tokio::test] async fn caches_multiple_folders() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:generateFolders") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFolders"); + }); - let hash = extract_hash_from_run(fixture.path(), "outputs:generateFolders").await; + let hash = extract_hash_from_run(sandbox.path(), "outputs:generateFolders").await; // hash - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/hashes") .join(format!("{}.json", hash)) .exists()); // outputs - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/outputs") .join(format!("{}.tar.gz", hash)) @@ -497,24 +506,24 @@ mod outputs { #[tokio::test] async fn caches_both_file_and_folder() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:generateFileAndFolder") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFileAndFolder"); + }); - let hash = extract_hash_from_run(fixture.path(), "outputs:generateFileAndFolder").await; + let hash = extract_hash_from_run(sandbox.path(), "outputs:generateFileAndFolder").await; // hash - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/hashes") .join(format!("{}.json", hash)) .exists()); // outputs - assert!(fixture + assert!(sandbox .path() .join(".moon/cache/outputs") .join(format!("{}.tar.gz", hash)) @@ -523,19 +532,19 @@ mod outputs { #[tokio::test] async fn caches_output_logs_in_tarball() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:generateFile") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFile"); + }); - let hash = extract_hash_from_run(fixture.path(), "outputs:generateFile").await; - let tarball = fixture + let hash = extract_hash_from_run(sandbox.path(), "outputs:generateFile").await; + let tarball = sandbox .path() .join(".moon/cache/outputs") .join(format!("{}.tar.gz", hash)); - let dir = fixture.path().join(".moon/cache/outputs").join(hash); + let dir = sandbox.path().join(".moon/cache/outputs").join(hash); moon_archive::untar(tarball, &dir, None).unwrap(); @@ -545,124 +554,119 @@ mod outputs { mod hydration { use super::*; - use pretty_assertions::assert_eq; + use moon_test_utils::pretty_assertions::assert_eq; #[tokio::test] async fn reuses_cache_from_previous_run() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert1 = create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:generateFileAndFolder") - .assert(); + let assert1 = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFileAndFolder"); + }); let hash1 = - extract_hash_from_run(fixture.path(), "outputs:generateFileAndFolder").await; + extract_hash_from_run(sandbox.path(), "outputs:generateFileAndFolder").await; - let assert2 = create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:generateFileAndFolder") - .assert(); + let assert2 = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFileAndFolder"); + }); let hash2 = - extract_hash_from_run(fixture.path(), "outputs:generateFileAndFolder").await; + extract_hash_from_run(sandbox.path(), "outputs:generateFileAndFolder").await; assert_eq!(hash1, hash2); - assert_snapshot!(get_assert_output(&assert1)); - assert_snapshot!(get_assert_output(&assert2)); + assert_snapshot!(assert1.output()); + assert_snapshot!(assert2.output()); } #[tokio::test] async fn doesnt_keep_output_logs_in_project() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:generateFileAndFolder") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFileAndFolder"); + }); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:generateFileAndFolder") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFileAndFolder"); + }); - assert!(!fixture.path().join("outputs/stdout.log").exists()); - assert!(!fixture.path().join("outputs/stderr.log").exists()); + assert!(!sandbox.path().join("outputs/stdout.log").exists()); + assert!(!sandbox.path().join("outputs/stderr.log").exists()); } #[tokio::test] async fn hydrates_missing_outputs_from_previous_run() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:generateFileAndFolder") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFileAndFolder"); + }); // Remove outputs - fs::remove_dir_all(fixture.path().join("outputs/esm")).unwrap(); - fs::remove_dir_all(fixture.path().join("outputs/lib")).unwrap(); + fs::remove_dir_all(sandbox.path().join("outputs/esm")).unwrap(); + fs::remove_dir_all(sandbox.path().join("outputs/lib")).unwrap(); - assert!(!fixture.path().join("outputs/esm").exists()); - assert!(!fixture.path().join("outputs/lib").exists()); + assert!(!sandbox.path().join("outputs/esm").exists()); + assert!(!sandbox.path().join("outputs/lib").exists()); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:generateFileAndFolder") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFileAndFolder"); + }); // Outputs should come back - assert!(fixture.path().join("outputs/esm").exists()); - assert!(fixture.path().join("outputs/lib").exists()); + assert!(sandbox.path().join("outputs/esm").exists()); + assert!(sandbox.path().join("outputs/lib").exists()); } #[tokio::test] async fn hydrates_with_a_different_hash_cache() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:generateFileAndFolder") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFileAndFolder"); + }); let hash1 = - extract_hash_from_run(fixture.path(), "outputs:generateFileAndFolder").await; - let contents1 = fs::read_to_string(fixture.path().join("outputs/lib/one.js")).unwrap(); + extract_hash_from_run(sandbox.path(), "outputs:generateFileAndFolder").await; + let contents1 = fs::read_to_string(sandbox.path().join("outputs/lib/one.js")).unwrap(); // Create a file to trigger an inputs change - fs::write(fixture.path().join("outputs/trigger.js"), "").unwrap(); + sandbox.create_file("outputs/trigger.js", ""); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:generateFileAndFolder") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFileAndFolder"); + }); let hash2 = - extract_hash_from_run(fixture.path(), "outputs:generateFileAndFolder").await; - let contents2 = fs::read_to_string(fixture.path().join("outputs/lib/one.js")).unwrap(); + extract_hash_from_run(sandbox.path(), "outputs:generateFileAndFolder").await; + let contents2 = fs::read_to_string(sandbox.path().join("outputs/lib/one.js")).unwrap(); // Hashes and contents should be different! assert_ne!(hash1, hash2); assert_ne!(contents1, contents2); // Remove outputs - fs::remove_dir_all(fixture.path().join("outputs/esm")).unwrap(); - fs::remove_dir_all(fixture.path().join("outputs/lib")).unwrap(); + fs::remove_dir_all(sandbox.path().join("outputs/esm")).unwrap(); + fs::remove_dir_all(sandbox.path().join("outputs/lib")).unwrap(); - assert!(!fixture.path().join("outputs/esm").exists()); - assert!(!fixture.path().join("outputs/lib").exists()); + assert!(!sandbox.path().join("outputs/esm").exists()); + assert!(!sandbox.path().join("outputs/lib").exists()); // Remove the trigger file - fs::remove_file(fixture.path().join("outputs/trigger.js")).unwrap(); + fs::remove_file(sandbox.path().join("outputs/trigger.js")).unwrap(); - create_moon_command(fixture.path()) - .arg("run") - .arg("outputs:generateFileAndFolder") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFileAndFolder"); + }); let hash3 = - extract_hash_from_run(fixture.path(), "outputs:generateFileAndFolder").await; - let contents3 = fs::read_to_string(fixture.path().join("outputs/lib/one.js")).unwrap(); + extract_hash_from_run(sandbox.path(), "outputs:generateFileAndFolder").await; + let contents3 = fs::read_to_string(sandbox.path().join("outputs/lib/one.js")).unwrap(); // Hashes and contents should match the original! assert_eq!(hash1, hash3); @@ -670,8 +674,8 @@ mod outputs { assert_ne!(contents2, contents3); // Outputs should come back - assert!(fixture.path().join("outputs/esm").exists()); - assert!(fixture.path().join("outputs/lib").exists()); + assert!(sandbox.path().join("outputs/esm").exists()); + assert!(sandbox.path().join("outputs/lib").exists()); } } } @@ -681,26 +685,26 @@ mod noop { #[test] fn runs_noop() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("noop:noop") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("noop:noop"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn runs_noop_deps() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("noop:noopWithDeps") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("noop:noopWithDeps"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } } @@ -709,14 +713,14 @@ mod root_level { #[test] fn runs_a_task() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("root:oneOff") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("root:oneOff"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains("root one off").eval(&output)); } @@ -727,75 +731,75 @@ mod output_styles { #[test] fn buffer() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("outputStyles:bufferPrimary") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputStyles:bufferPrimary"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn buffer_on_failure_when_success() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("outputStyles:bufferFailurePassPrimary") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputStyles:bufferFailurePassPrimary"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[cfg(not(windows))] // Different path output in snapshot #[test] fn buffer_on_failure_when_failure() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("outputStyles:bufferFailureFailPrimary") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputStyles:bufferFailureFailPrimary"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn hash() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("outputStyles:hashPrimary") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputStyles:hashPrimary"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn none() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("outputStyles:nonePrimary") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputStyles:nonePrimary"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } #[test] fn stream() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("outputStyles:streamPrimary") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputStyles:streamPrimary"); + }); - assert_snapshot!(get_assert_output(&assert)); + assert_snapshot!(assert.output()); } } @@ -804,45 +808,42 @@ mod reports { #[test] fn doesnt_create_a_report_by_default() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - create_moon_command(fixture.path()) - .arg("run") - .arg("base:base") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("base:base"); + }); - assert!(!fixture.path().join(".moon/cache/runReport.json").exists()); + assert!(!sandbox.path().join(".moon/cache/runReport.json").exists()); } #[test] fn creates_report_when_option_passed() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - create_moon_command(fixture.path()) - .arg("run") - .arg("base:base") - .arg("--report") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("base:base").arg("--report"); + }); - assert!(fixture.path().join(".moon/cache/runReport.json").exists()); + assert!(sandbox.path().join(".moon/cache/runReport.json").exists()); } } mod affected { use super::*; - use moon_utils::test::run_git_command; #[test] fn doesnt_run_if_not_affected() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("files:noop") - .arg("--affected") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("files:noop").arg("--affected"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!( predicate::str::contains("Target(s) files:noop not affected by touched files") @@ -852,36 +853,36 @@ mod affected { #[test] fn runs_if_affected() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - fs::write(fixture.path().join("files/other.txt"), "").unwrap(); + sandbox.create_file("files/other.txt", ""); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("files:noop") - .arg("--affected") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("files:noop").arg("--affected"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains("Tasks: 1 completed").eval(&output)); } #[test] fn doesnt_run_if_affected_but_wrong_status() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - fs::write(fixture.path().join("files/other.txt"), "").unwrap(); + sandbox.create_file("files/other.txt", ""); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("files:noop") - .arg("--affected") - .arg("--status") - .arg("deleted") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("files:noop") + .arg("--affected") + .arg("--status") + .arg("deleted"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains( "Target(s) files:noop not affected by touched files (using status deleted)" @@ -891,80 +892,84 @@ mod affected { #[test] fn handles_untracked() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - fs::write(fixture.path().join("files/other.txt"), "").unwrap(); + sandbox.create_file("files/other.txt", ""); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("files:noop") - .arg("--affected") - .arg("--status") - .arg("untracked") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("files:noop") + .arg("--affected") + .arg("--status") + .arg("untracked"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains("Tasks: 1 completed").eval(&output)); } #[test] fn handles_added() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - fs::write(fixture.path().join("files/other.txt"), "").unwrap(); + sandbox.create_file("files/other.txt", ""); - run_git_command(fixture.path(), |cmd| { + sandbox.run_git(|cmd| { cmd.args(["add", "files/other.txt"]); }); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("files:noop") - .arg("--affected") - .arg("--status") - .arg("added") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("files:noop") + .arg("--affected") + .arg("--status") + .arg("added"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains("Tasks: 1 completed").eval(&output)); } #[test] fn handles_modified() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - fs::write(fixture.path().join("files/file.txt"), "modified").unwrap(); + sandbox.create_file("files/file.txt", "modified"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("files:noop") - .arg("--affected") - .arg("--status") - .arg("modified") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("files:noop") + .arg("--affected") + .arg("--status") + .arg("modified"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains("Tasks: 1 completed").eval(&output)); } #[test] fn handles_deleted() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - fs::remove_file(fixture.path().join("files/file.txt")).unwrap(); + fs::remove_file(sandbox.path().join("files/file.txt")).unwrap(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("files:noop") - .arg("--affected") - .arg("--status") - .arg("deleted") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("files:noop") + .arg("--affected") + .arg("--status") + .arg("deleted"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains("Tasks: 1 completed").eval(&output)); } @@ -974,15 +979,14 @@ mod affected { #[test] fn doesnt_run_if_not_affected() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("root:noop") - .arg("--affected") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("root:noop").arg("--affected"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!( predicate::str::contains("Target(s) root:noop not affected by touched files") @@ -992,36 +996,36 @@ mod affected { #[test] fn runs_if_affected() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - fs::write(fixture.path().join("tsconfig.json"), "{}").unwrap(); + sandbox.create_file("tsconfig.json", "{}"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("root:noop") - .arg("--affected") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("root:noop").arg("--affected"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains("Tasks: 1 completed").eval(&output)); } #[test] fn doesnt_run_if_affected_but_wrong_status() { - let fixture = create_sandbox_with_git("cases"); + let sandbox = cases_sandbox(); + sandbox.enable_git(); - fs::write(fixture.path().join("tsconfig.json"), "{}").unwrap(); + sandbox.create_file("tsconfig.json", "{}"); - let assert = create_moon_command(fixture.path()) - .arg("run") - .arg("root:noop") - .arg("--affected") - .arg("--status") - .arg("deleted") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("root:noop") + .arg("--affected") + .arg("--status") + .arg("deleted"); + }); - let output = get_assert_output(&assert); + let output = assert.output(); assert!(predicate::str::contains( "Target(s) root:noop not affected by touched files (using status deleted)" diff --git a/crates/cli/tests/run_webhooks_test.rs b/crates/cli/tests/run_webhooks_test.rs index a20216edbff..a478d911d8a 100644 --- a/crates/cli/tests/run_webhooks_test.rs +++ b/crates/cli/tests/run_webhooks_test.rs @@ -1,15 +1,30 @@ -mod utils; - +use moon_config::NotifierConfig; use moon_notifier::WebhookPayload; -use moon_utils::test::{create_moon_command, create_sandbox_with_git}; -use utils::append_workspace_config; +use moon_test_utils::{create_sandbox_with_config, get_node_fixture_configs, Sandbox}; use wiremock::matchers::{method, path}; use wiremock::{Mock, MockServer, ResponseTemplate}; +fn sandbox(uri: String) -> Sandbox { + let (mut workspace_config, toolchain_config, projects_config) = get_node_fixture_configs(); + + workspace_config.notifier = NotifierConfig { + webhook_url: Some(format!("{}/webhook", uri)), + }; + + let sandbox = create_sandbox_with_config( + "node", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + sandbox.enable_git(); + sandbox +} + #[tokio::test] async fn sends_webhooks() { let server = MockServer::start().await; - let fixture = create_sandbox_with_git("node"); Mock::given(method("POST")) .and(path("/webhook")) @@ -18,21 +33,18 @@ async fn sends_webhooks() { .mount(&server) .await; - append_workspace_config( - fixture.path(), - &format!("notifier:\n webhookUrl: '{}/webhook'", server.uri()), - ); + let sandbox = sandbox(server.uri()); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:cjs"); + }); - create_moon_command(fixture.path()) - .arg("run") - .arg("node:cjs") - .assert(); + assert.success(); } #[tokio::test] async fn sends_webhooks_for_cache_events() { let server = MockServer::start().await; - let fixture = create_sandbox_with_git("node"); Mock::given(method("POST")) .and(path("/webhook")) @@ -41,27 +53,23 @@ async fn sends_webhooks_for_cache_events() { .mount(&server) .await; - append_workspace_config( - fixture.path(), - &format!("notifier:\n webhookUrl: '{}/webhook'", server.uri()), - ); + let sandbox = sandbox(server.uri()); - create_moon_command(fixture.path()) - .arg("run") - .arg("node:cjs") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:cjs"); + }); // Run again to hit the cache - create_moon_command(fixture.path()) - .arg("run") - .arg("node:cjs") - .assert(); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:cjs"); + }); + + assert.success(); } #[tokio::test] async fn doesnt_send_webhooks_if_first_fails() { let server = MockServer::start().await; - let fixture = create_sandbox_with_git("node"); Mock::given(method("POST")) .and(path("/webhook")) @@ -70,21 +78,16 @@ async fn doesnt_send_webhooks_if_first_fails() { .mount(&server) .await; - append_workspace_config( - fixture.path(), - &format!("notifier:\n webhookUrl: '{}/webhook'", server.uri()), - ); + let sandbox = sandbox(server.uri()); - create_moon_command(fixture.path()) - .arg("run") - .arg("node:cjs") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:cjs"); + }); } #[tokio::test] async fn all_webhooks_have_same_uuid() { let server = MockServer::start().await; - let fixture = create_sandbox_with_git("node"); Mock::given(method("POST")) .and(path("/webhook")) @@ -92,15 +95,11 @@ async fn all_webhooks_have_same_uuid() { .mount(&server) .await; - append_workspace_config( - fixture.path(), - &format!("notifier:\n webhookUrl: '{}/webhook'", server.uri()), - ); + let sandbox = sandbox(server.uri()); - create_moon_command(fixture.path()) - .arg("run") - .arg("node:cjs") - .assert(); + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:cjs"); + }); let received_requests = server.received_requests().await.unwrap(); let mut uuid = None; diff --git a/crates/cli/tests/setup_teardown_test.rs b/crates/cli/tests/setup_teardown_test.rs index 95fb57849eb..2e6a8c7af24 100644 --- a/crates/cli/tests/setup_teardown_test.rs +++ b/crates/cli/tests/setup_teardown_test.rs @@ -1,6 +1,6 @@ +use moon_test_utils::{create_sandbox_with_config, get_cases_fixture_configs}; use moon_utils::is_ci; use moon_utils::path::get_home_dir; -use moon_utils::test::{create_moon_command, create_sandbox}; #[test] fn sets_up_and_tears_down() { @@ -17,21 +17,26 @@ fn sets_up_and_tears_down() { assert!(!node_dir.exists()); - let fixture = create_sandbox("cases"); + let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); - let setup = create_moon_command(fixture.path()) - .arg("setup") - .env("MOON_NODE_VERSION", node_version) - .assert(); + let sandbox = create_sandbox_with_config( + "cases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + let setup = sandbox.run_moon(|cmd| { + cmd.arg("setup").env("MOON_NODE_VERSION", node_version); + }); setup.success().code(0); assert!(node_dir.exists()); - let teardown = create_moon_command(fixture.path()) - .arg("teardown") - .env("MOON_NODE_VERSION", node_version) - .assert(); + let teardown = sandbox.run_moon(|cmd| { + cmd.arg("teardown").env("MOON_NODE_VERSION", node_version); + }); teardown.success().code(0); diff --git a/crates/cli/tests/snapshots/dep_graph_test__aliases__can_focus_using_an_alias.snap b/crates/cli/tests/snapshots/dep_graph_test__aliases__can_focus_using_an_alias.snap index 1760ab02751..fe3bded3977 100644 --- a/crates/cli/tests/snapshots/dep_graph_test__aliases__can_focus_using_an_alias.snap +++ b/crates/cli/tests/snapshots/dep_graph_test__aliases__can_focus_using_an_alias.snap @@ -1,11 +1,11 @@ --- source: crates/cli/tests/dep_graph_test.rs -assertion_line: 65 +assertion_line: 100 expression: get_assert_output(&assert) --- digraph { - 0 [ label="SetupNodeTool(16.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] - 1 [ label="InstallNodeDeps(16.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] + 0 [ label="SetupNodeTool(18.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] + 1 [ label="InstallNodeDeps(18.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 2 [ label="SyncNodeProject(nodeNameScope)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 3 [ label="RunTarget(nodeNameScope:test)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 4 [ label="SyncNodeProject(node)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] diff --git a/crates/cli/tests/snapshots/dep_graph_test__aliases__resolves_aliases_in_task_deps.snap b/crates/cli/tests/snapshots/dep_graph_test__aliases__resolves_aliases_in_task_deps.snap index 71e590b7421..e985ba0aa70 100644 --- a/crates/cli/tests/snapshots/dep_graph_test__aliases__resolves_aliases_in_task_deps.snap +++ b/crates/cli/tests/snapshots/dep_graph_test__aliases__resolves_aliases_in_task_deps.snap @@ -1,11 +1,11 @@ --- source: crates/cli/tests/dep_graph_test.rs -assertion_line: 77 +assertion_line: 119 expression: get_assert_output(&assert) --- digraph { - 0 [ label="SetupNodeTool(16.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] - 1 [ label="InstallNodeDeps(16.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] + 0 [ label="SetupNodeTool(18.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] + 1 [ label="InstallNodeDeps(18.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 2 [ label="SyncNodeProject(node)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 3 [ label="SyncNodeProject(nodeNameOnly)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 4 [ label="SyncNodeProject(nodeNameScope)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] diff --git a/crates/cli/tests/snapshots/dep_graph_test__focused_by_target.snap b/crates/cli/tests/snapshots/dep_graph_test__focused_by_target.snap index 982a3ffd5c8..005cfc5b777 100644 --- a/crates/cli/tests/snapshots/dep_graph_test__focused_by_target.snap +++ b/crates/cli/tests/snapshots/dep_graph_test__focused_by_target.snap @@ -1,11 +1,11 @@ --- source: crates/cli/tests/dep_graph_test.rs -assertion_line: 26 +assertion_line: 42 expression: get_assert_output(&assert) --- digraph { - 0 [ label="SetupNodeTool(16.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] - 1 [ label="InstallNodeDeps(16.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] + 0 [ label="SetupNodeTool(18.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] + 1 [ label="InstallNodeDeps(18.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 2 [ label="SyncNodeProject(basic)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 3 [ label="RunTarget(basic:lint)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 1 -> 0 [ arrowhead=box, arrowtail=box] diff --git a/crates/cli/tests/snapshots/dep_graph_test__includes_dependencies_when_focused.snap b/crates/cli/tests/snapshots/dep_graph_test__includes_dependencies_when_focused.snap index 67edddf25cd..17a6204d342 100644 --- a/crates/cli/tests/snapshots/dep_graph_test__includes_dependencies_when_focused.snap +++ b/crates/cli/tests/snapshots/dep_graph_test__includes_dependencies_when_focused.snap @@ -1,11 +1,11 @@ --- source: crates/cli/tests/dep_graph_test.rs -assertion_line: 38 +assertion_line: 60 expression: get_assert_output(&assert) --- digraph { - 0 [ label="SetupNodeTool(16.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] - 1 [ label="InstallNodeDeps(16.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] + 0 [ label="SetupNodeTool(18.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] + 1 [ label="InstallNodeDeps(18.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 2 [ label="SyncNodeProject(chain)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 3 [ label="RunTarget(chain:e)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 4 [ label="RunTarget(chain:f)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] diff --git a/crates/cli/tests/snapshots/dep_graph_test__includes_dependents_when_focused.snap b/crates/cli/tests/snapshots/dep_graph_test__includes_dependents_when_focused.snap index 17180d10612..5af20a6207f 100644 --- a/crates/cli/tests/snapshots/dep_graph_test__includes_dependents_when_focused.snap +++ b/crates/cli/tests/snapshots/dep_graph_test__includes_dependents_when_focused.snap @@ -1,11 +1,11 @@ --- source: crates/cli/tests/dep_graph_test.rs -assertion_line: 50 +assertion_line: 78 expression: get_assert_output(&assert) --- digraph { - 0 [ label="SetupNodeTool(16.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] - 1 [ label="InstallNodeDeps(16.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] + 0 [ label="SetupNodeTool(18.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] + 1 [ label="InstallNodeDeps(18.0.0)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 2 [ label="SyncNodeProject(basic)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 3 [ label="RunTarget(basic:build)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] 4 [ label="SyncNodeProject(buildA)" style=filled, shape=oval, fillcolor=gray, fontcolor=black] diff --git a/crates/cli/tests/snapshots/run_node_test__caching__creates_run_state_cache.snap b/crates/cli/tests/snapshots/run_node_test__caching__creates_run_state_cache.snap index 89c2cdc138c..8f69eb8ef64 100644 --- a/crates/cli/tests/snapshots/run_node_test__caching__creates_run_state_cache.snap +++ b/crates/cli/tests/snapshots/run_node_test__caching__creates_run_state_cache.snap @@ -38,7 +38,7 @@ expression: "fs::read_to_string(fixture.path().join(format!(\".moon/cache/hashes "version": "1" }, { - "nodeVersion": "16.1.0", + "nodeVersion": "18.0.0", "dependencies": {}, "tsconfigCompilerOptions": {}, "version": "1" diff --git a/crates/cli/tests/snapshots/run_node_test__engines__adds_engines_constraint.snap b/crates/cli/tests/snapshots/run_node_test__engines__adds_engines_constraint.snap index d255cf201ce..e03aba21b8f 100644 --- a/crates/cli/tests/snapshots/run_node_test__engines__adds_engines_constraint.snap +++ b/crates/cli/tests/snapshots/run_node_test__engines__adds_engines_constraint.snap @@ -1,7 +1,7 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 377 -expression: "read_to_string(fixture.path().join(\"package.json\")).unwrap()" +assertion_line: 439 +expression: "read_to_string(sandbox.path().join(\"package.json\")).unwrap()" --- { "name": "test-node", @@ -14,7 +14,7 @@ expression: "read_to_string(fixture.path().join(\"package.json\")).unwrap()" "version-override" ], "engines": { - "node": "16.1.0" + "node": "18.0.0" }, "packageManager": "npm@8.19.2" } diff --git a/crates/cli/tests/snapshots/run_node_test__engines__doesnt_add_engines_constraint.snap b/crates/cli/tests/snapshots/run_node_test__engines__doesnt_add_engines_constraint.snap index 438cd1c94cf..9c680366bb6 100644 --- a/crates/cli/tests/snapshots/run_node_test__engines__doesnt_add_engines_constraint.snap +++ b/crates/cli/tests/snapshots/run_node_test__engines__doesnt_add_engines_constraint.snap @@ -1,7 +1,7 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 387 -expression: "read_to_string(fixture.path().join(\"package.json\")).unwrap()" +assertion_line: 452 +expression: "read_to_string(sandbox.path().join(\"package.json\")).unwrap()" --- { "name": "test-node", @@ -12,6 +12,7 @@ expression: "read_to_string(fixture.path().join(\"package.json\")).unwrap()" "lifecycles", "swc", "version-override" - ] + ], + "packageManager": "npm@8.19.2" } diff --git a/crates/cli/tests/snapshots/run_node_test__handles_process_exit_code_nonzero.snap b/crates/cli/tests/snapshots/run_node_test__handles_process_exit_code_nonzero.snap index 7f447d2033d..09285b25e3c 100644 --- a/crates/cli/tests/snapshots/run_node_test__handles_process_exit_code_nonzero.snap +++ b/crates/cli/tests/snapshots/run_node_test__handles_process_exit_code_nonzero.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 121 +assertion_line: 188 expression: get_assert_output(&assert) --- ▪▪▪▪ npm install @@ -10,7 +10,7 @@ This should appear! ▪▪▪▪ node:exitCodeNonZero (100ms) stderr - ERROR Process ~/.moon/tools/node/16.1.0/bin/node failed with a 1 exit code. + ERROR Process ~/.moon/tools/node/18.0.0/bin/node failed with a 1 exit code. diff --git a/crates/cli/tests/snapshots/run_node_test__handles_process_exit_nonzero.snap b/crates/cli/tests/snapshots/run_node_test__handles_process_exit_nonzero.snap index abb752e618d..aec0d5b460c 100644 --- a/crates/cli/tests/snapshots/run_node_test__handles_process_exit_nonzero.snap +++ b/crates/cli/tests/snapshots/run_node_test__handles_process_exit_nonzero.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 93 +assertion_line: 162 expression: get_assert_output(&assert) --- ▪▪▪▪ npm install @@ -9,7 +9,7 @@ stdout ▪▪▪▪ node:processExitNonZero (100ms) stderr - ERROR Process ~/.moon/tools/node/16.1.0/bin/node failed with a 1 exit code. + ERROR Process ~/.moon/tools/node/18.0.0/bin/node failed with a 1 exit code. diff --git a/crates/cli/tests/snapshots/run_node_test__handles_unhandled_promise.snap b/crates/cli/tests/snapshots/run_node_test__handles_unhandled_promise.snap index b646aa7b63f..a9e267e6931 100644 --- a/crates/cli/tests/snapshots/run_node_test__handles_unhandled_promise.snap +++ b/crates/cli/tests/snapshots/run_node_test__handles_unhandled_promise.snap @@ -1,22 +1,24 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 151 -expression: "get_path_safe_output(&assert, fixture.path())" +assertion_line: 217 +expression: "get_path_safe_output(&assert, sandbox.path())" --- ▪▪▪▪ npm install ▪▪▪▪ node:unhandledPromise stdout ▪▪▪▪ node:unhandledPromise (100ms) stderr -node:internal/process/promises:246 - triggerUncaughtException(err, true /* fromPromise */); - ^ +node:internal/process/promises:288 + triggerUncaughtException(err, true /* fromPromise */); + ^ [UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "Oops".] { code: 'ERR_UNHANDLED_REJECTION' } - ERROR Process ~/.moon/tools/node/16.1.0/bin/node failed with a 1 exit code. +Node.js v18.0.0 + + ERROR Process ~/.moon/tools/node/18.0.0/bin/node failed with a 1 exit code. diff --git a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_file.snap b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_File.snap similarity index 74% rename from crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_file.snap rename to crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_File.snap index 8270e0d8ca1..1168443bc1f 100644 --- a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_file.snap +++ b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_File.snap @@ -1,7 +1,7 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 492 -expression: "read_to_string(fixture.path().join(\"depends-on/package.json\")).unwrap()" +assertion_line: 523 +expression: "read_to_string(sandbox.path().join(\"depends-on/package.json\")).unwrap()" --- { "name": "test-node-depends-on", diff --git a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_link.snap b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_Link.snap similarity index 74% rename from crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_link.snap rename to crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_Link.snap index 89729866ff4..0b4f0418ab3 100644 --- a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_link.snap +++ b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_Link.snap @@ -1,7 +1,7 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 492 -expression: "read_to_string(fixture.path().join(\"depends-on/package.json\")).unwrap()" +assertion_line: 523 +expression: "read_to_string(sandbox.path().join(\"depends-on/package.json\")).unwrap()" --- { "name": "test-node-depends-on", diff --git a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_star.snap b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_Star.snap similarity index 72% rename from crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_star.snap rename to crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_Star.snap index 8bd183b0b64..015fd15887b 100644 --- a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_star.snap +++ b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_Star.snap @@ -1,7 +1,7 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 492 -expression: "read_to_string(fixture.path().join(\"depends-on/package.json\")).unwrap()" +assertion_line: 523 +expression: "read_to_string(sandbox.path().join(\"depends-on/package.json\")).unwrap()" --- { "name": "test-node-depends-on", diff --git a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_version.snap b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_Version.snap similarity index 72% rename from crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_version.snap rename to crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_Version.snap index b8581fec32f..f3ca9ed2222 100644 --- a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_version.snap +++ b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_Version.snap @@ -1,7 +1,7 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 492 -expression: "read_to_string(fixture.path().join(\"depends-on/package.json\")).unwrap()" +assertion_line: 523 +expression: "read_to_string(sandbox.path().join(\"depends-on/package.json\")).unwrap()" --- { "name": "test-node-depends-on", diff --git a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_version-caret.snap b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_VersionCaret.snap similarity index 72% rename from crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_version-caret.snap rename to crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_VersionCaret.snap index a1287cb415c..dda9069ca26 100644 --- a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_version-caret.snap +++ b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_VersionCaret.snap @@ -1,7 +1,7 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 492 -expression: "read_to_string(fixture.path().join(\"depends-on/package.json\")).unwrap()" +assertion_line: 523 +expression: "read_to_string(sandbox.path().join(\"depends-on/package.json\")).unwrap()" --- { "name": "test-node-depends-on", diff --git a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_version-tilde.snap b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_VersionTilde.snap similarity index 72% rename from crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_version-tilde.snap rename to crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_VersionTilde.snap index 52ae34c52c4..f5056d395a2 100644 --- a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_version-tilde.snap +++ b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_VersionTilde.snap @@ -1,7 +1,7 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 492 -expression: "read_to_string(fixture.path().join(\"depends-on/package.json\")).unwrap()" +assertion_line: 523 +expression: "read_to_string(sandbox.path().join(\"depends-on/package.json\")).unwrap()" --- { "name": "test-node-depends-on", diff --git a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_workspace.snap b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_Workspace.snap similarity index 73% rename from crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_workspace.snap rename to crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_Workspace.snap index 444ee94295b..dae47e6e4e4 100644 --- a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_workspace.snap +++ b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_Workspace.snap @@ -1,7 +1,7 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 492 -expression: "read_to_string(fixture.path().join(\"depends-on/package.json\")).unwrap()" +assertion_line: 523 +expression: "read_to_string(sandbox.path().join(\"depends-on/package.json\")).unwrap()" --- { "name": "test-node-depends-on", diff --git a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_workspace-caret.snap b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_WorkspaceCaret.snap similarity index 73% rename from crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_workspace-caret.snap rename to crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_WorkspaceCaret.snap index 7a3cf0f788d..5301e4631e1 100644 --- a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_workspace-caret.snap +++ b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_WorkspaceCaret.snap @@ -1,7 +1,7 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 492 -expression: "read_to_string(fixture.path().join(\"depends-on/package.json\")).unwrap()" +assertion_line: 523 +expression: "read_to_string(sandbox.path().join(\"depends-on/package.json\")).unwrap()" --- { "name": "test-node-depends-on", diff --git a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_workspace-tilde.snap b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_WorkspaceTilde.snap similarity index 73% rename from crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_workspace-tilde.snap rename to crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_WorkspaceTilde.snap index 8cbe98fd7d4..90f7c1dbbed 100644 --- a/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_workspace-tilde.snap +++ b/crates/cli/tests/snapshots/run_node_test__sync_depends_on__format_WorkspaceTilde.snap @@ -1,7 +1,7 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 492 -expression: "read_to_string(fixture.path().join(\"depends-on/package.json\")).unwrap()" +assertion_line: 523 +expression: "read_to_string(sandbox.path().join(\"depends-on/package.json\")).unwrap()" --- { "name": "test-node-depends-on", diff --git a/crates/cli/tests/snapshots/run_system_test__system_windows__caching__creates_run_state_cache.snap b/crates/cli/tests/snapshots/run_system_test__system_windows__caching__creates_run_state_cache.snap index caec39eb445..d5751b8d726 100644 --- a/crates/cli/tests/snapshots/run_system_test__system_windows__caching__creates_run_state_cache.snap +++ b/crates/cli/tests/snapshots/run_system_test__system_windows__caching__creates_run_state_cache.snap @@ -13,7 +13,6 @@ expression: "fs::read_to_string(fixture.path().join(format!(\".moon/cache/hashes "deps": {}, "envVars": {}, "inputs": { - ".moon/workspace.yml": "c448710a2eaaec20fe6897f6d62d1e77f39edf4e", "windows/cwd.bat": "f97d5bfb654f63a59a4158e4969707c8e1c29ece", "windows/echo.bat": "cf9137992257ec89c223a5d866458a94a361e0fd", "windows/envVars.bat": "af068672f8e5f2ca1aa1b5fec93df8fb253bdd0c", diff --git a/crates/cli/tests/snapshots/run_system_test__unix__caching__creates_run_state_cache.snap b/crates/cli/tests/snapshots/run_system_test__unix__caching__creates_run_state_cache.snap index 30f1ec0b7ac..16c43e89ee3 100644 --- a/crates/cli/tests/snapshots/run_system_test__unix__caching__creates_run_state_cache.snap +++ b/crates/cli/tests/snapshots/run_system_test__unix__caching__creates_run_state_cache.snap @@ -1,7 +1,7 @@ --- source: crates/cli/tests/run_system_test.rs -assertion_line: 227 -expression: "fs::read_to_string(fixture.path().join(format!(\".moon/cache/hashes/{}.json\",\n state.hash))).unwrap()" +assertion_line: 236 +expression: "fs::read_to_string(sandbox.path().join(format!(\".moon/cache/hashes/{}.json\",\n state.hash))).unwrap()" --- [ { @@ -12,7 +12,6 @@ expression: "fs::read_to_string(fixture.path().join(format!(\".moon/cache/hashes "deps": {}, "envVars": {}, "inputs": { - ".moon/workspace.yml": "c448710a2eaaec20fe6897f6d62d1e77f39edf4e", "unix/args.sh": "ac54a54b387de2281f14259a6e974688f6465c57", "unix/cwd.sh": "9a5ea47dd8809abadf1c61817ffe6be07883af0d", "unix/envVars.sh": "63cd12999af89a6823e908f32f2a08bf7f27182e", diff --git a/crates/cli/tests/snapshots/run_test__configs__bubbles_up_invalid_project_config.snap b/crates/cli/tests/snapshots/run_test__configs__bubbles_up_invalid_project_config.snap index ef6a721899e..3f9c098d794 100644 --- a/crates/cli/tests/snapshots/run_test__configs__bubbles_up_invalid_project_config.snap +++ b/crates/cli/tests/snapshots/run_test__configs__bubbles_up_invalid_project_config.snap @@ -1,14 +1,14 @@ --- source: crates/cli/tests/run_test.rs -assertion_line: 146 +assertion_line: 174 expression: "standardize_separators(get_path_safe_output(&assert,\n &PathBuf::from(\"./fake/path\")))" --- ERROR -Failed to validate test/moon.yml configuration file. +Failed to validate base/moon.yml configuration file. - ▪ missing field `name` for key "project.project" in test/moon.yml YAML Extended file + ▪ missing field `name` for key "project.project" in base/moon.yml YAML Extended file diff --git a/crates/cli/tests/snapshots/run_test__output_styles__buffer_on_failure_when_failure.snap b/crates/cli/tests/snapshots/run_test__output_styles__buffer_on_failure_when_failure.snap index 7cc6f8c2f1e..77971b7a3e2 100644 --- a/crates/cli/tests/snapshots/run_test__output_styles__buffer_on_failure_when_failure.snap +++ b/crates/cli/tests/snapshots/run_test__output_styles__buffer_on_failure_when_failure.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_test.rs -assertion_line: 762 +assertion_line: 783 expression: get_assert_output(&assert) --- ▪▪▪▪ npm install @@ -11,7 +11,7 @@ stdout stderr - ERROR Process ~/.moon/tools/node/16.0.0/bin/node failed with a 1 exit code. + ERROR Process ~/.moon/tools/node/18.0.0/bin/node failed with a 1 exit code. diff --git a/crates/cli/tests/snapshots/run_test__output_styles__hash.snap b/crates/cli/tests/snapshots/run_test__output_styles__hash.snap index eff786eb515..1990a5d1a44 100644 --- a/crates/cli/tests/snapshots/run_test__output_styles__hash.snap +++ b/crates/cli/tests/snapshots/run_test__output_styles__hash.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_test.rs -assertion_line: 774 +assertion_line: 795 expression: get_assert_output(&assert) --- ▪▪▪▪ npm install @@ -11,6 +11,6 @@ expression: get_assert_output(&assert) Tasks: 2 completed Time: 100ms -2d21e4bd3ec7c137de9e76e16d797237bfe5c75e08d49f2d2ae6c310f77aa412 +d5e3e5016e39e84b6cda3106d306a8d77bd9ab74393b38bb4cbb91a59d1b726e diff --git a/crates/cli/tests/snapshots/run_test__outputs__hydration__reuses_cache_from_previous_run-2.snap b/crates/cli/tests/snapshots/run_test__outputs__hydration__reuses_cache_from_previous_run-2.snap index 76594b75aaf..0ceff83d71b 100644 --- a/crates/cli/tests/snapshots/run_test__outputs__hydration__reuses_cache_from_previous_run-2.snap +++ b/crates/cli/tests/snapshots/run_test__outputs__hydration__reuses_cache_from_previous_run-2.snap @@ -1,9 +1,9 @@ --- source: crates/cli/tests/run_test.rs -assertion_line: 572 +assertion_line: 597 expression: get_assert_output(&assert2) --- -▪▪▪▪ outputs:generateFileAndFolder (cached from previous run, d1594c00) +▪▪▪▪ outputs:generateFileAndFolder (cached from previous run, 271d9358) Tasks: 1 completed (1 cached) Time: 100ms ❯❯❯❯ to the moon diff --git a/crates/cli/tests/sync_test.rs b/crates/cli/tests/sync_test.rs index 278a3203259..0681771b296 100644 --- a/crates/cli/tests/sync_test.rs +++ b/crates/cli/tests/sync_test.rs @@ -1,13 +1,31 @@ -use insta::assert_snapshot; -use moon_utils::test::{create_moon_command, create_sandbox, get_assert_output}; +use moon_config::{WorkspaceConfig, WorkspaceProjects}; +use moon_test_utils::{assert_snapshot, create_sandbox_with_config}; +use rustc_hash::FxHashMap; #[test] fn syncs_all_projects() { - let fixture = create_sandbox("project-graph/dependencies"); + let workspace_config = WorkspaceConfig { + projects: WorkspaceProjects::Sources(FxHashMap::from_iter([ + ("a".to_owned(), "a".to_owned()), + ("b".to_owned(), "b".to_owned()), + ("c".to_owned(), "c".to_owned()), + ("d".to_owned(), "d".to_owned()), + ])), + ..WorkspaceConfig::default() + }; - let assert = create_moon_command(fixture.path()).arg("sync").assert(); + let sandbox = create_sandbox_with_config( + "project-graph/dependencies", + Some(&workspace_config), + None, + None, + ); - assert_snapshot!(get_assert_output(&assert)); + let assert = sandbox.run_moon(|cmd| { + cmd.arg("sync"); + }); + + assert_snapshot!(assert.output()); assert.success(); } diff --git a/crates/cli/tests/utils.rs b/crates/cli/tests/utils.rs deleted file mode 100644 index 4bdbef23401..00000000000 --- a/crates/cli/tests/utils.rs +++ /dev/null @@ -1,45 +0,0 @@ -#![allow(dead_code)] - -use moon_utils::path::replace_home_dir; -use moon_utils::test::{get_assert_output, replace_fixtures_dir}; -use std::fs; -use std::fs::OpenOptions; -use std::io::prelude::*; -use std::path::Path; - -pub fn append_workspace_config(root: &Path, yaml: &str) { - let mut file = OpenOptions::new() - .write(true) - .append(true) - .open(root.join(".moon/workspace.yml")) - .unwrap(); - - writeln!(file, "{}", yaml).unwrap(); -} - -pub fn append_toolchain_config(root: &Path, yaml: &str) { - let mut file = OpenOptions::new() - .write(true) - .append(true) - .open(root.join(".moon/toolchain.yml")) - .unwrap(); - - writeln!(file, "{}", yaml).unwrap(); -} - -pub fn update_toolchain_config(dir: &Path, old: &str, new: &str) { - let mut config = fs::read_to_string(dir.join(".moon/toolchain.yml")).unwrap(); - - config = config.replace(old, new); - - fs::write(dir.join(".moon/toolchain.yml"), config).unwrap(); -} - -pub fn get_path_safe_output(assert: &assert_cmd::assert::Assert, fixtures_dir: &Path) -> String { - let result = replace_home_dir(replace_fixtures_dir( - get_assert_output(assert), - fixtures_dir, - )); - - result.replace("/private<", "<") -} diff --git a/crates/core/archive/Cargo.toml b/crates/core/archive/Cargo.toml index b075fc83eab..b3f8dbc0491 100644 --- a/crates/core/archive/Cargo.toml +++ b/crates/core/archive/Cargo.toml @@ -22,7 +22,7 @@ tokio = { workspace = true } zip = "0.6.3" [dev-dependencies] -assert_fs = "1.0.9" +moon_test_utils = { path = "../test-utils" } criterion = { workspace = true } fake = "2.5.0" rand = "0.8.5" diff --git a/crates/core/archive/benches/tar_benchmark.rs b/crates/core/archive/benches/tar_benchmark.rs index fd567144ad2..ee0b4c69632 100644 --- a/crates/core/archive/benches/tar_benchmark.rs +++ b/crates/core/archive/benches/tar_benchmark.rs @@ -1,16 +1,16 @@ // Allow `temp_dir` so that files arent removed when dropping scope #![allow(unused_variables)] -use assert_fs::TempDir; use criterion::{criterion_group, criterion_main, Criterion}; use fake::{Fake, Faker}; use moon_archive::{tar, untar, untar_with_diff, TreeDiffer}; +use moon_test_utils::{assert_fs::TempDir, create_temp_dir}; use moon_utils::string_vec; use std::fs; use std::path::PathBuf; fn create_tree() -> (TempDir, PathBuf, PathBuf, Vec) { - let temp_dir = TempDir::new().unwrap(); + let temp_dir = create_temp_dir(); let sources_dir = temp_dir.path().join("sources"); let archive_file = temp_dir.path().join("archive.tar.gz"); let dirs = string_vec!['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']; diff --git a/crates/core/archive/tests/tar_test.rs b/crates/core/archive/tests/tar_test.rs index d9dbe1e433e..bfa13519483 100644 --- a/crates/core/archive/tests/tar_test.rs +++ b/crates/core/archive/tests/tar_test.rs @@ -1,6 +1,6 @@ use moon_archive::{tar, untar}; +use moon_test_utils::create_sandbox; use moon_utils::string_vec; -use moon_utils::test::create_sandbox; use std::fs; use std::path::Path; @@ -10,11 +10,11 @@ fn file_contents_match(a: &Path, b: &Path) -> bool { #[test] fn tars_file() { - let fixture = create_sandbox("archives"); + let sandbox = create_sandbox("archives"); // Pack - let input = fixture.path(); - let archive = fixture.path().join("out.tar.gz"); + let input = sandbox.path(); + let archive = sandbox.path().join("out.tar.gz"); tar(input, &string_vec!["file.txt"], &archive, None).unwrap(); @@ -22,7 +22,7 @@ fn tars_file() { assert_ne!(archive.metadata().unwrap().len(), 0); // Unpack - let output = fixture.path().join("out"); + let output = sandbox.path().join("out"); untar(&archive, &output, None).unwrap(); @@ -38,11 +38,11 @@ fn tars_file() { #[test] fn tars_file_with_prefix() { - let fixture = create_sandbox("archives"); + let sandbox = create_sandbox("archives"); // Pack - let input = fixture.path(); - let archive = fixture.path().join("out.tar.gz"); + let input = sandbox.path(); + let archive = sandbox.path().join("out.tar.gz"); tar( input, @@ -56,7 +56,7 @@ fn tars_file_with_prefix() { assert_ne!(archive.metadata().unwrap().len(), 0); // Unpack - let output = fixture.path().join("out"); + let output = sandbox.path().join("out"); untar(&archive, &output, None).unwrap(); @@ -72,11 +72,11 @@ fn tars_file_with_prefix() { #[test] fn tars_file_with_prefix_thats_removed() { - let fixture = create_sandbox("archives"); + let sandbox = create_sandbox("archives"); // Pack - let input = fixture.path(); - let archive = fixture.path().join("out.tar.gz"); + let input = sandbox.path(); + let archive = sandbox.path().join("out.tar.gz"); tar( input, @@ -90,7 +90,7 @@ fn tars_file_with_prefix_thats_removed() { assert_ne!(archive.metadata().unwrap().len(), 0); // Unpack - let output = fixture.path().join("out"); + let output = sandbox.path().join("out"); untar(&archive, &output, Some("some/prefix")).unwrap(); @@ -106,11 +106,11 @@ fn tars_file_with_prefix_thats_removed() { #[test] fn tars_nested_file_and_preserves_path() { - let fixture = create_sandbox("archives"); + let sandbox = create_sandbox("archives"); // Pack - let input = fixture.path(); - let archive = fixture.path().join("out.tar.gz"); + let input = sandbox.path(); + let archive = sandbox.path().join("out.tar.gz"); tar( input, @@ -124,7 +124,7 @@ fn tars_nested_file_and_preserves_path() { assert_ne!(archive.metadata().unwrap().len(), 0); // Unpack - let output = fixture.path().join("out"); + let output = sandbox.path().join("out"); untar(&archive, &output, None).unwrap(); @@ -140,11 +140,11 @@ fn tars_nested_file_and_preserves_path() { #[test] fn tars_file_and_dir_explicitly() { - let fixture = create_sandbox("archives"); + let sandbox = create_sandbox("archives"); // Pack - let input = fixture.path(); - let archive = fixture.path().join("out.tar.gz"); + let input = sandbox.path(); + let archive = sandbox.path().join("out.tar.gz"); tar( input, @@ -158,7 +158,7 @@ fn tars_file_and_dir_explicitly() { assert_ne!(archive.metadata().unwrap().len(), 0); // Unpack - let output = fixture.path().join("out"); + let output = sandbox.path().join("out"); untar(&archive, &output, None).unwrap(); @@ -180,11 +180,11 @@ fn tars_file_and_dir_explicitly() { #[test] fn tars_dir() { - let fixture = create_sandbox("archives"); + let sandbox = create_sandbox("archives"); // Pack - let input = fixture.path(); - let archive = fixture.path().join("out.tar.gz"); + let input = sandbox.path(); + let archive = sandbox.path().join("out.tar.gz"); tar(input, &string_vec!["folder"], &archive, None).unwrap(); @@ -192,7 +192,7 @@ fn tars_dir() { assert_ne!(archive.metadata().unwrap().len(), 0); // Unpack - let output = fixture.path().join("out"); + let output = sandbox.path().join("out"); untar(&archive, &output, None).unwrap(); @@ -213,11 +213,11 @@ fn tars_dir() { #[test] fn tars_dir_with_prefix() { - let fixture = create_sandbox("archives"); + let sandbox = create_sandbox("archives"); // Pack - let input = fixture.path(); - let archive = fixture.path().join("out.tar.gz"); + let input = sandbox.path(); + let archive = sandbox.path().join("out.tar.gz"); tar(input, &string_vec!["folder"], &archive, Some("some/prefix")).unwrap(); @@ -225,7 +225,7 @@ fn tars_dir_with_prefix() { assert_ne!(archive.metadata().unwrap().len(), 0); // Unpack - let output = fixture.path().join("out"); + let output = sandbox.path().join("out"); untar(&archive, &output, None).unwrap(); @@ -246,11 +246,11 @@ fn tars_dir_with_prefix() { #[test] fn tars_dir_with_prefix_thats_removed() { - let fixture = create_sandbox("archives"); + let sandbox = create_sandbox("archives"); // Pack - let input = fixture.path(); - let archive = fixture.path().join("out.tar.gz"); + let input = sandbox.path(); + let archive = sandbox.path().join("out.tar.gz"); tar(input, &string_vec!["folder"], &archive, Some("some/prefix")).unwrap(); @@ -258,7 +258,7 @@ fn tars_dir_with_prefix_thats_removed() { assert_ne!(archive.metadata().unwrap().len(), 0); // Unpack - let output = fixture.path().join("out"); + let output = sandbox.path().join("out"); untar(&archive, &output, Some("some/prefix")).unwrap(); diff --git a/crates/core/archive/tests/tree_differ_test.rs b/crates/core/archive/tests/tree_differ_test.rs index 9fde8f4891e..7b5f1bbe182 100644 --- a/crates/core/archive/tests/tree_differ_test.rs +++ b/crates/core/archive/tests/tree_differ_test.rs @@ -1,24 +1,25 @@ use moon_archive::TreeDiffer; -use moon_utils::{string_vec, test::create_sandbox}; +use moon_test_utils::create_sandbox; +use moon_utils::string_vec; use std::fs::{self, File}; #[test] fn loads_all_files() { - let fixture = create_sandbox("generator"); - let differ = TreeDiffer::load(fixture.path(), &string_vec!["templates"]).unwrap(); + let sandbox = create_sandbox("generator"); + let differ = TreeDiffer::load(sandbox.path(), &string_vec!["templates"]).unwrap(); - assert_eq!(differ.files.len(), 18); + assert_eq!(differ.files.len(), 21); } #[test] fn removes_stale_files() { - let fixture = create_sandbox("generator"); - let mut differ = TreeDiffer::load(fixture.path(), &string_vec!["templates"]).unwrap(); + let sandbox = create_sandbox("generator"); + let mut differ = TreeDiffer::load(sandbox.path(), &string_vec!["templates"]).unwrap(); // Delete everything, hah differ.remove_stale_tracked_files(); - let differ = TreeDiffer::load(fixture.path(), &string_vec!["templates"]).unwrap(); + let differ = TreeDiffer::load(sandbox.path(), &string_vec!["templates"]).unwrap(); assert_eq!(differ.files.len(), 0); } @@ -28,14 +29,14 @@ mod equal_check { #[test] fn returns_true_if_equal() { - let fixture = create_sandbox("generator"); - let differ = TreeDiffer::load(fixture.path(), &string_vec!["templates"]).unwrap(); + let sandbox = create_sandbox("generator"); + let differ = TreeDiffer::load(sandbox.path(), &string_vec!["templates"]).unwrap(); - let source_path = fixture.path().join("templates/standard/file-source.txt"); + let source_path = sandbox.path().join("templates/standard/file-source.txt"); fs::write(&source_path, "content").unwrap(); let mut source = File::open(&source_path).unwrap(); - let dest_path = fixture.path().join("templates/standard/file.txt"); + let dest_path = sandbox.path().join("templates/standard/file.txt"); fs::write(&dest_path, "content").unwrap(); let mut dest = File::open(&dest_path).unwrap(); @@ -44,14 +45,14 @@ mod equal_check { #[test] fn returns_false_if_diff_sizes() { - let fixture = create_sandbox("generator"); - let differ = TreeDiffer::load(fixture.path(), &string_vec!["templates"]).unwrap(); + let sandbox = create_sandbox("generator"); + let differ = TreeDiffer::load(sandbox.path(), &string_vec!["templates"]).unwrap(); - let source_path = fixture.path().join("templates/standard/file-source.txt"); + let source_path = sandbox.path().join("templates/standard/file-source.txt"); fs::write(&source_path, "data").unwrap(); let mut source = File::open(&source_path).unwrap(); - let dest_path = fixture.path().join("templates/standard/file.txt"); + let dest_path = sandbox.path().join("templates/standard/file.txt"); fs::write(&dest_path, "content").unwrap(); let mut dest = File::open(&dest_path).unwrap(); @@ -60,14 +61,14 @@ mod equal_check { #[test] fn returns_false_if_diff_data() { - let fixture = create_sandbox("generator"); - let differ = TreeDiffer::load(fixture.path(), &string_vec!["templates"]).unwrap(); + let sandbox = create_sandbox("generator"); + let differ = TreeDiffer::load(sandbox.path(), &string_vec!["templates"]).unwrap(); - let source_path = fixture.path().join("templates/standard/file-source.txt"); + let source_path = sandbox.path().join("templates/standard/file-source.txt"); fs::write(&source_path, "cont...").unwrap(); let mut source = File::open(&source_path).unwrap(); - let dest_path = fixture.path().join("templates/standard/file.txt"); + let dest_path = sandbox.path().join("templates/standard/file.txt"); fs::write(&dest_path, "content").unwrap(); let mut dest = File::open(&dest_path).unwrap(); diff --git a/crates/core/archive/tests/zip_test.rs b/crates/core/archive/tests/zip_test.rs index 612f837b3fe..6e4b44e957e 100644 --- a/crates/core/archive/tests/zip_test.rs +++ b/crates/core/archive/tests/zip_test.rs @@ -1,6 +1,6 @@ use moon_archive::{unzip, zip}; +use moon_test_utils::create_sandbox; use moon_utils::string_vec; -use moon_utils::test::create_sandbox; use std::fs; use std::path::Path; @@ -10,11 +10,11 @@ fn file_contents_match(a: &Path, b: &Path) -> bool { #[test] fn zips_file() { - let fixture = create_sandbox("archives"); + let sandbox = create_sandbox("archives"); // Pack - let input = fixture.path(); - let archive = fixture.path().join("out.zip"); + let input = sandbox.path(); + let archive = sandbox.path().join("out.zip"); zip(input, &string_vec!["file.txt"], &archive, None).unwrap(); @@ -22,7 +22,7 @@ fn zips_file() { assert_ne!(archive.metadata().unwrap().len(), 0); // Unpack - let output = fixture.path().join("out"); + let output = sandbox.path().join("out"); unzip(&archive, &output, None).unwrap(); @@ -38,11 +38,11 @@ fn zips_file() { #[test] fn zips_file_with_prefix() { - let fixture = create_sandbox("archives"); + let sandbox = create_sandbox("archives"); // Pack - let input = fixture.path(); - let archive = fixture.path().join("out.zip"); + let input = sandbox.path(); + let archive = sandbox.path().join("out.zip"); zip( input, @@ -56,7 +56,7 @@ fn zips_file_with_prefix() { assert_ne!(archive.metadata().unwrap().len(), 0); // Unpack - let output = fixture.path().join("out"); + let output = sandbox.path().join("out"); unzip(&archive, &output, None).unwrap(); @@ -72,11 +72,11 @@ fn zips_file_with_prefix() { #[test] fn zips_file_with_prefix_thats_removed() { - let fixture = create_sandbox("archives"); + let sandbox = create_sandbox("archives"); // Pack - let input = fixture.path(); - let archive = fixture.path().join("out.zip"); + let input = sandbox.path(); + let archive = sandbox.path().join("out.zip"); zip( input, @@ -90,7 +90,7 @@ fn zips_file_with_prefix_thats_removed() { assert_ne!(archive.metadata().unwrap().len(), 0); // Unpack - let output = fixture.path().join("out"); + let output = sandbox.path().join("out"); unzip(&archive, &output, Some("some/prefix")).unwrap(); @@ -106,11 +106,11 @@ fn zips_file_with_prefix_thats_removed() { #[test] fn zips_nested_file_and_preserves_path() { - let fixture = create_sandbox("archives"); + let sandbox = create_sandbox("archives"); // Pack - let input = fixture.path(); - let archive = fixture.path().join("out.zip"); + let input = sandbox.path(); + let archive = sandbox.path().join("out.zip"); zip( input, @@ -124,7 +124,7 @@ fn zips_nested_file_and_preserves_path() { assert_ne!(archive.metadata().unwrap().len(), 0); // Unpack - let output = fixture.path().join("out"); + let output = sandbox.path().join("out"); unzip(&archive, &output, None).unwrap(); @@ -140,11 +140,11 @@ fn zips_nested_file_and_preserves_path() { #[test] fn zips_file_and_dir_explicitly() { - let fixture = create_sandbox("archives"); + let sandbox = create_sandbox("archives"); // Pack - let input = fixture.path(); - let archive = fixture.path().join("out.zip"); + let input = sandbox.path(); + let archive = sandbox.path().join("out.zip"); zip( input, @@ -158,7 +158,7 @@ fn zips_file_and_dir_explicitly() { assert_ne!(archive.metadata().unwrap().len(), 0); // Unpack - let output = fixture.path().join("out"); + let output = sandbox.path().join("out"); unzip(&archive, &output, None).unwrap(); @@ -180,11 +180,11 @@ fn zips_file_and_dir_explicitly() { #[test] fn zips_dir() { - let fixture = create_sandbox("archives"); + let sandbox = create_sandbox("archives"); // Pack - let input = fixture.path(); - let archive = fixture.path().join("out.zip"); + let input = sandbox.path(); + let archive = sandbox.path().join("out.zip"); zip(input, &string_vec!["folder"], &archive, None).unwrap(); @@ -192,7 +192,7 @@ fn zips_dir() { assert_ne!(archive.metadata().unwrap().len(), 0); // Unpack - let output = fixture.path().join("out"); + let output = sandbox.path().join("out"); unzip(&archive, &output, None).unwrap(); @@ -213,11 +213,11 @@ fn zips_dir() { #[test] fn zips_dir_with_prefix() { - let fixture = create_sandbox("archives"); + let sandbox = create_sandbox("archives"); // Pack - let input = fixture.path(); - let archive = fixture.path().join("out.zip"); + let input = sandbox.path(); + let archive = sandbox.path().join("out.zip"); zip(input, &string_vec!["folder"], &archive, Some("some/prefix")).unwrap(); @@ -225,7 +225,7 @@ fn zips_dir_with_prefix() { assert_ne!(archive.metadata().unwrap().len(), 0); // Unpack - let output = fixture.path().join("out"); + let output = sandbox.path().join("out"); unzip(&archive, &output, None).unwrap(); @@ -246,11 +246,11 @@ fn zips_dir_with_prefix() { #[test] fn zips_dir_with_prefix_thats_removed() { - let fixture = create_sandbox("archives"); + let sandbox = create_sandbox("archives"); // Pack - let input = fixture.path(); - let archive = fixture.path().join("out.zip"); + let input = sandbox.path(); + let archive = sandbox.path().join("out.zip"); zip(input, &string_vec!["folder"], &archive, Some("some/prefix")).unwrap(); @@ -258,7 +258,7 @@ fn zips_dir_with_prefix_thats_removed() { assert_ne!(archive.metadata().unwrap().len(), 0); // Unpack - let output = fixture.path().join("out"); + let output = sandbox.path().join("out"); unzip(&archive, &output, Some("some/prefix")).unwrap(); diff --git a/crates/core/cache/Cargo.toml b/crates/core/cache/Cargo.toml index 8df409f2fbb..0f000a38d9a 100644 --- a/crates/core/cache/Cargo.toml +++ b/crates/core/cache/Cargo.toml @@ -14,7 +14,7 @@ rustc-hash = { workspace = true } serde = { workspace = true } [dev-dependencies] -assert_fs = "1.0.9" +moon_test_utils = { path = "../test-utils" } filetime = "0.2.18" -tokio = { workspace = true, features = ["test-util"] } serial_test = "0.9.0" +tokio = { workspace = true } diff --git a/crates/core/cache/tests/engine_test.rs b/crates/core/cache/tests/engine_test.rs index 8247d3a0027..d2f92ec82bd 100644 --- a/crates/core/cache/tests/engine_test.rs +++ b/crates/core/cache/tests/engine_test.rs @@ -1,5 +1,5 @@ -use assert_fs::prelude::*; use moon_cache::{CacheEngine, ProjectsState, RunTargetState, ToolState}; +use moon_test_utils::{assert_fs::prelude::*, create_temp_dir}; use moon_utils::time; use serde::Serialize; use serial_test::serial; @@ -30,7 +30,7 @@ mod create { #[tokio::test] #[serial] async fn creates_dirs() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); CacheEngine::load(dir.path()).await.unwrap(); @@ -49,7 +49,7 @@ mod create_runfile { #[tokio::test] #[serial] async fn creates_runfile_on_call() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); let cache = CacheEngine::load(dir.path()).await.unwrap(); let runfile = cache .create_runfile("123", &"content".to_owned()) @@ -73,7 +73,7 @@ mod cache_run_target_state { #[tokio::test] #[serial] async fn creates_parent_dir_on_call() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); let cache = CacheEngine::load(dir.path()).await.unwrap(); let item = cache.cache_run_target_state("foo:bar").await.unwrap(); @@ -86,7 +86,7 @@ mod cache_run_target_state { #[tokio::test] #[serial] async fn loads_cache_if_it_exists() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); dir.child(".moon/cache/states/foo/bar/lastRun.json") .write_str(r#"{"exitCode":123,"hash":"","lastRunTime":0,"stderr":"","stdout":"","target":"foo:bar"}"#) @@ -111,7 +111,7 @@ mod cache_run_target_state { #[tokio::test] #[serial] async fn loads_cache_if_it_exists_and_cache_is_readonly() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); dir.child(".moon/cache/states/foo/bar/lastRun.json") .write_str(r#"{"exitCode":123,"hash":"","lastRunTime":0,"stderr":"","stdout":"","target":"foo:bar"}"#) @@ -138,7 +138,7 @@ mod cache_run_target_state { #[tokio::test] #[serial] async fn doesnt_load_if_it_exists_but_cache_is_off() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); dir.child(".moon/cache/states/foo/bar/lastRun.json") .write_str(r#"{"exitCode":123,"hash":"","lastRunTime":0,"stderr":"","stdout":"","target":"foo:bar"}"#) @@ -164,7 +164,7 @@ mod cache_run_target_state { #[tokio::test] #[serial] async fn saves_to_cache() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); let cache = CacheEngine::load(dir.path()).await.unwrap(); let mut item = cache.cache_run_target_state("foo:bar").await.unwrap(); @@ -183,7 +183,7 @@ mod cache_run_target_state { #[tokio::test] #[serial] async fn doesnt_save_if_cache_off() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); let cache = CacheEngine::load(dir.path()).await.unwrap(); let mut item = cache.cache_run_target_state("foo:bar").await.unwrap(); @@ -199,7 +199,7 @@ mod cache_run_target_state { #[tokio::test] #[serial] async fn doesnt_save_if_cache_readonly() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); let cache = CacheEngine::load(dir.path()).await.unwrap(); let mut item = cache.cache_run_target_state("foo:bar").await.unwrap(); @@ -221,7 +221,7 @@ mod cache_tool_state { #[tokio::test] #[serial] async fn creates_parent_dir_on_call() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); let cache = CacheEngine::load(dir.path()).await.unwrap(); let item = cache .cache_tool_state(&Runtime::Node(Version("1.2.3".into(), false))) @@ -237,7 +237,7 @@ mod cache_tool_state { #[tokio::test] #[serial] async fn loads_cache_if_it_exists() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); dir.child(".moon/cache/states/toolNode-1.2.3.json") .write_str(r#"{"lastVersionCheckTime":123}"#) @@ -264,7 +264,7 @@ mod cache_tool_state { #[tokio::test] #[serial] async fn loads_cache_if_it_exists_and_cache_is_readonly() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); dir.child(".moon/cache/states/toolNode-4.5.6.json") .write_str(r#"{"lastVersionCheckTime":123}"#) @@ -291,7 +291,7 @@ mod cache_tool_state { #[tokio::test] #[serial] async fn doesnt_load_if_it_exists_but_cache_is_off() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); dir.child(".moon/cache/states/toolSystem-latest.json") .write_str(r#"{"lastVersionCheckTime":123}"#) @@ -316,7 +316,7 @@ mod cache_tool_state { #[tokio::test] #[serial] async fn saves_to_cache() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); let cache = CacheEngine::load(dir.path()).await.unwrap(); let mut item = cache .cache_tool_state(&Runtime::Node(Version("7.8.9".into(), false))) @@ -329,7 +329,7 @@ mod cache_tool_state { assert_eq!( fs::read_to_string(item.path).unwrap(), - r#"{"lastVersionCheckTime":123}"# + r#"{"lastVersions":{},"lastVersionCheckTime":123}"# ); dir.close().unwrap(); @@ -346,7 +346,7 @@ mod cache_projects_state { #[tokio::test] #[serial] async fn creates_parent_dir_on_call() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); let cache = CacheEngine::load(dir.path()).await.unwrap(); let item = cache.cache_projects_state().await.unwrap(); @@ -359,7 +359,7 @@ mod cache_projects_state { #[tokio::test] #[serial] async fn loads_cache_if_it_exists() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); dir.child(".moon/cache/states/projects.json") .write_str(r#"{"globs":["**/*"],"projects":{"foo":"bar"}}"#) @@ -383,7 +383,7 @@ mod cache_projects_state { #[tokio::test] #[serial] async fn loads_cache_if_it_exists_and_cache_is_readonly() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); dir.child(".moon/cache/states/projects.json") .write_str(r#"{"globs":["**/*"],"projects":{"foo":"bar"}}"#) @@ -409,7 +409,7 @@ mod cache_projects_state { #[tokio::test] #[serial] async fn doesnt_load_if_it_exists_but_cache_is_off() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); dir.child(".moon/cache/states/projects.json") .write_str(r#"{"globs":[],"projects":{"foo":"bar"}}"#) @@ -434,7 +434,7 @@ mod cache_projects_state { #[tokio::test] #[serial] async fn doesnt_load_if_it_exists_but_cache_is_stale() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); dir.child(".moon/cache/states/projects.json") .write_str(r#"{"globs":[],"projects":{"foo":"bar"}}"#) @@ -465,7 +465,7 @@ mod cache_projects_state { #[tokio::test] #[serial] async fn saves_to_cache() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); let cache = CacheEngine::load(dir.path()).await.unwrap(); let mut item = cache.cache_projects_state().await.unwrap(); @@ -494,7 +494,7 @@ mod create_hash_manifest { #[tokio::test] #[serial] async fn creates_hash_file() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); let cache = CacheEngine::load(dir.path()).await.unwrap(); let hasher = TestHasher::default(); @@ -508,7 +508,7 @@ mod create_hash_manifest { #[tokio::test] #[serial] async fn doesnt_create_if_cache_off() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); let cache = CacheEngine::load(dir.path()).await.unwrap(); let hasher = TestHasher::default(); @@ -524,7 +524,7 @@ mod create_hash_manifest { #[tokio::test] #[serial] async fn doesnt_create_if_cache_readonly() { - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); let cache = CacheEngine::load(dir.path()).await.unwrap(); let hasher = TestHasher::default(); diff --git a/crates/core/config/Cargo.toml b/crates/core/config/Cargo.toml index efe2da0e7ce..9ef66507330 100644 --- a/crates/core/config/Cargo.toml +++ b/crates/core/config/Cargo.toml @@ -29,6 +29,5 @@ thiserror = { workspace = true } validator = { version = "0.16.0", features = ["derive"] } [dev-dependencies] -assert_fs = "1.0.9" -pretty_assertions = "1.3.0" -tokio = { workspace = true, features = ["test-util"] } +moon_test_utils = { path = "../test-utils" } +tokio = { workspace = true } diff --git a/crates/core/config/src/project/global_config.rs b/crates/core/config/src/project/global_config.rs index a7a42f3e1d4..94bf7a5de28 100644 --- a/crates/core/config/src/project/global_config.rs +++ b/crates/core/config/src/project/global_config.rs @@ -48,6 +48,7 @@ fn validate_tasks(map: &BTreeMap) -> Result<(), ValidationEr // We use serde(default) because extended configs may not have defined these fields #[serde(default, rename_all = "camelCase")] pub struct GlobalProjectConfig { + #[serde(skip_serializing_if = "Option::is_none")] #[validate(custom = "validate_extends")] pub extends: Option, diff --git a/crates/core/config/src/project/workspace.rs b/crates/core/config/src/project/workspace.rs index 1e82323cfb0..b5227d8833a 100644 --- a/crates/core/config/src/project/workspace.rs +++ b/crates/core/config/src/project/workspace.rs @@ -17,18 +17,10 @@ pub struct ProjectWorkspaceInheritedTasksConfig { pub rename: Option>, } -#[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize, Validate)] +#[derive(Clone, Debug, Default, Deserialize, Eq, JsonSchema, PartialEq, Serialize, Validate)] #[schemars(default)] #[serde(default, rename_all = "camelCase")] pub struct ProjectWorkspaceConfig { #[validate] pub inherited_tasks: ProjectWorkspaceInheritedTasksConfig, } - -impl Default for ProjectWorkspaceConfig { - fn default() -> Self { - ProjectWorkspaceConfig { - inherited_tasks: ProjectWorkspaceInheritedTasksConfig::default(), - } - } -} diff --git a/crates/core/config/src/toolchain/config.rs b/crates/core/config/src/toolchain/config.rs index d15cb76ddd3..be5b0045e2a 100644 --- a/crates/core/config/src/toolchain/config.rs +++ b/crates/core/config/src/toolchain/config.rs @@ -21,6 +21,7 @@ use validator::Validate; #[schemars(default)] #[serde(rename_all = "camelCase")] pub struct ToolchainConfig { + #[serde(skip_serializing_if = "Option::is_none")] #[validate(custom = "validate_extends")] pub extends: Option, diff --git a/crates/core/config/src/workspace/config.rs b/crates/core/config/src/workspace/config.rs index 7d2bbe69a35..b6e3f2363d5 100644 --- a/crates/core/config/src/workspace/config.rs +++ b/crates/core/config/src/workspace/config.rs @@ -71,6 +71,7 @@ impl Default for WorkspaceProjects { #[schemars(default)] #[serde(rename_all = "camelCase")] pub struct WorkspaceConfig { + #[serde(skip_serializing_if = "Option::is_none")] #[validate(custom = "validate_extends")] pub extends: Option, diff --git a/crates/core/config/tests/global_project_test.rs b/crates/core/config/tests/global_project_test.rs index 38c3a43c4d2..9286cdf0ce9 100644 --- a/crates/core/config/tests/global_project_test.rs +++ b/crates/core/config/tests/global_project_test.rs @@ -1,7 +1,7 @@ use moon_config::{ConfigError, GlobalProjectConfig, TaskCommandArgs}; use moon_constants::CONFIG_GLOBAL_PROJECT_FILENAME; +use moon_test_utils::get_fixtures_path; use moon_utils::string_vec; -use moon_utils::test::get_fixtures_dir; use rustc_hash::FxHashMap; use std::collections::BTreeMap; use std::path::Path; @@ -50,12 +50,12 @@ fileGroups: mod extends { use super::*; use moon_config::{TaskConfig, TaskOptionsConfig}; - use pretty_assertions::assert_eq; + use moon_test_utils::pretty_assertions::assert_eq; use std::fs; #[test] fn recursive_merges() { - let fixture = get_fixtures_dir("config-extends/project"); + let fixture = get_fixtures_path("config-extends/project"); let config = GlobalProjectConfig::load(fixture.join("global-2.yml")).unwrap(); assert_eq!( @@ -226,7 +226,7 @@ mod extends { #[test] fn loads_from_file() { - use pretty_assertions::assert_eq; + use moon_test_utils::pretty_assertions::assert_eq; figment::Jail::expect_with(|jail| { fs::create_dir_all(jail.directory().join("shared")).unwrap(); @@ -269,7 +269,7 @@ fileGroups: #[test] fn loads_from_url() { - use pretty_assertions::assert_eq; + use moon_test_utils::pretty_assertions::assert_eq; figment::Jail::expect_with(|jail| { jail.set_env( diff --git a/crates/core/config/tests/tasks_test.rs b/crates/core/config/tests/tasks_test.rs index 82084351201..bbca478549b 100644 --- a/crates/core/config/tests/tasks_test.rs +++ b/crates/core/config/tests/tasks_test.rs @@ -2,8 +2,7 @@ use figment::{ providers::{Format, YamlExtended}, Figment, }; -use moon_config::map_validation_errors_to_figment_errors; -use moon_config::{TaskCommandArgs, TaskConfig}; +use moon_config::{map_validation_errors_to_figment_errors, TaskCommandArgs, TaskConfig}; use moon_utils::string_vec; use std::path::PathBuf; use validator::Validate; diff --git a/crates/core/config/tests/toolchain_test.rs b/crates/core/config/tests/toolchain_test.rs index 4ab81c18c33..d8b74dd8ab6 100644 --- a/crates/core/config/tests/toolchain_test.rs +++ b/crates/core/config/tests/toolchain_test.rs @@ -1,6 +1,6 @@ use moon_config::{ConfigError, NodeConfig, ToolchainConfig}; use moon_constants::CONFIG_WORKSPACE_FILENAME; -use moon_utils::test::get_fixtures_dir; +use moon_test_utils::get_fixtures_path; use std::path::Path; fn load_jailed_config(root: &Path) -> Result { @@ -38,12 +38,12 @@ fn loads_defaults() { mod extends { use super::*; use moon_config::{NodePackageManager, TypeScriptConfig, YarnConfig}; - use pretty_assertions::assert_eq; + use moon_test_utils::pretty_assertions::assert_eq; use std::fs; #[test] fn recursive_merges() { - let fixture = get_fixtures_dir("config-extends/workspace"); + let fixture = get_fixtures_path("config-extends/workspace"); let config = ToolchainConfig::load(fixture.join("base-2.yml")).unwrap(); assert_eq!( @@ -56,7 +56,7 @@ mod extends { package_manager: NodePackageManager::Yarn, yarn: Some(YarnConfig { plugins: None, - version: "3.0.0".into() + version: "3.3.0".into() }), ..NodeConfig::default() }), @@ -67,7 +67,7 @@ mod extends { #[test] fn recursive_merges_typescript() { - let fixture = get_fixtures_dir("config-extends/workspace"); + let fixture = get_fixtures_path("config-extends/workspace"); let config = ToolchainConfig::load(fixture.join("typescript-2.yml")).unwrap(); assert_eq!( diff --git a/crates/core/config/tests/workspace_test.rs b/crates/core/config/tests/workspace_test.rs index 5aaee7fa8ee..f70389cf61f 100644 --- a/crates/core/config/tests/workspace_test.rs +++ b/crates/core/config/tests/workspace_test.rs @@ -3,7 +3,7 @@ use moon_config::{ VcsManager, WorkspaceConfig, WorkspaceProjects, }; use moon_constants::CONFIG_WORKSPACE_FILENAME; -use moon_utils::test::get_fixtures_dir; +use moon_test_utils::get_fixtures_path; use std::path::Path; fn load_jailed_config(root: &Path) -> Result { @@ -44,12 +44,12 @@ fn loads_defaults() { mod extends { use super::*; - use pretty_assertions::assert_eq; + use moon_test_utils::pretty_assertions::assert_eq; use std::fs; #[test] fn recursive_merges() { - let fixture = get_fixtures_dir("config-extends/workspace"); + let fixture = get_fixtures_path("config-extends/workspace"); let config = WorkspaceConfig::load(fixture.join("base-2.yml")).unwrap(); assert_eq!( diff --git a/crates/core/emitter/Cargo.toml b/crates/core/emitter/Cargo.toml index 6f68583f58f..446a2053fd8 100644 --- a/crates/core/emitter/Cargo.toml +++ b/crates/core/emitter/Cargo.toml @@ -24,4 +24,5 @@ serde = { workspace = true } tokio = { workspace = true } [dev-dependencies] +moon_test_utils = { path = "../test-utils" } criterion = { workspace = true } diff --git a/crates/core/emitter/benches/emitter_benchmark.rs b/crates/core/emitter/benches/emitter_benchmark.rs index 8b0a701e93b..73f600dd5b7 100644 --- a/crates/core/emitter/benches/emitter_benchmark.rs +++ b/crates/core/emitter/benches/emitter_benchmark.rs @@ -1,7 +1,7 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; use moon_emitter::{Emitter, Event, EventFlow, Subscriber}; use moon_error::MoonError; -use moon_utils::test::get_fixtures_dir; +use moon_test_utils::get_fixtures_path; use moon_workspace::Workspace; use std::sync::Arc; use tokio::sync::RwLock; @@ -20,7 +20,7 @@ impl Subscriber for TestSubscriber { } pub fn emit_benchmark(c: &mut Criterion) { - let workspace_root = get_fixtures_dir("cases"); + let workspace_root = get_fixtures_path("cases"); let runtime = tokio::runtime::Runtime::new().unwrap(); c.bench_function("emitter_emit", |b| { diff --git a/crates/core/generator/Cargo.toml b/crates/core/generator/Cargo.toml index 356c4e074ad..6a31482442a 100644 --- a/crates/core/generator/Cargo.toml +++ b/crates/core/generator/Cargo.toml @@ -17,5 +17,5 @@ tera = { version = "1.17.1", features = ["preserve_order"] } thiserror = { workspace = true } [dev-dependencies] -assert_fs = "1.0.9" -tokio = { workspace = true, features = ["test-util"] } +moon_test_utils = { path = "../test-utils" } +tokio = { workspace = true } diff --git a/crates/core/generator/tests/generator_test.rs b/crates/core/generator/tests/generator_test.rs index 92686aab38e..31ed2934e37 100644 --- a/crates/core/generator/tests/generator_test.rs +++ b/crates/core/generator/tests/generator_test.rs @@ -1,6 +1,7 @@ use moon_config::GeneratorConfig; use moon_generator::Generator; -use moon_utils::{string_vec, test::create_sandbox}; +use moon_test_utils::create_sandbox; +use moon_utils::string_vec; mod create_template { use super::*; @@ -8,9 +9,9 @@ mod create_template { #[tokio::test] #[should_panic(expected = "ExistingTemplate(\"standard\"")] async fn errors_if_already_exists() { - let dir = create_sandbox("generator"); + let sandbox = create_sandbox("generator"); - Generator::load(dir.path(), &GeneratorConfig::default()) + Generator::load(sandbox.path(), &GeneratorConfig::default()) .unwrap() .create_template("standard") .await @@ -19,27 +20,30 @@ mod create_template { #[tokio::test] async fn creates_the_template() { - let dir = create_sandbox("generator"); + let sandbox = create_sandbox("generator"); - let template = Generator::load(dir.path(), &GeneratorConfig::default()) + let template = Generator::load(sandbox.path(), &GeneratorConfig::default()) .unwrap() .create_template("new-template") .await .unwrap(); - assert!(dir.join("templates/new-template").exists()); - assert!(dir.join("templates/new-template/template.yml").exists()); + assert!(sandbox.path().join("templates/new-template").exists()); + assert!(sandbox + .path() + .join("templates/new-template/template.yml") + .exists()); assert_eq!(template.name, "new-template".to_owned()); - assert_eq!(template.root, dir.join("templates/new-template")); + assert_eq!(template.root, sandbox.path().join("templates/new-template")); } #[tokio::test] async fn creates_the_template_from_another_dir() { - let dir = create_sandbox("generator"); + let sandbox = create_sandbox("generator"); let template = Generator::load( - dir.path(), + sandbox.path(), &GeneratorConfig { templates: string_vec!["./scaffolding"], }, @@ -49,27 +53,35 @@ mod create_template { .await .unwrap(); - assert!(dir.join("scaffolding/new-template").exists()); - assert!(dir.join("scaffolding/new-template/template.yml").exists()); + assert!(sandbox.path().join("scaffolding/new-template").exists()); + assert!(sandbox + .path() + .join("scaffolding/new-template/template.yml") + .exists()); assert_eq!(template.name, "new-template".to_owned()); - assert_eq!(template.root, dir.join("scaffolding/new-template")); + assert_eq!( + template.root, + sandbox.path().join("scaffolding/new-template") + ); } #[tokio::test] async fn cleans_and_formats_the_name() { - let dir = create_sandbox("generator"); + let sandbox = create_sandbox("generator"); - let template = Generator::load(dir.path(), &GeneratorConfig::default()) + let template = Generator::load(sandbox.path(), &GeneratorConfig::default()) .unwrap() .create_template("so&me temPlatE- with Ran!dom-Valu^es 123_") .await .unwrap(); - assert!(dir + assert!(sandbox + .path() .join("templates/so-me-temPlatE--with-Ran-dom-Valu-es-123_") .exists()); - assert!(dir + assert!(sandbox + .path() .join("templates/so-me-temPlatE--with-Ran-dom-Valu-es-123_/template.yml") .exists()); @@ -79,7 +91,9 @@ mod create_template { ); assert_eq!( template.root, - dir.join("templates/so-me-temPlatE--with-Ran-dom-Valu-es-123_") + sandbox + .path() + .join("templates/so-me-temPlatE--with-Ran-dom-Valu-es-123_") ); } } diff --git a/crates/core/generator/tests/template_test.rs b/crates/core/generator/tests/template_test.rs index c8d86e2cf85..19fce84f89a 100644 --- a/crates/core/generator/tests/template_test.rs +++ b/crates/core/generator/tests/template_test.rs @@ -1,6 +1,6 @@ use moon_constants::CONFIG_TEMPLATE_FILENAME; use moon_generator::{Template, TemplateContext, TemplateFile}; -use moon_utils::test::get_fixtures_dir; +use moon_test_utils::get_fixtures_path; use std::path::PathBuf; fn create_template_file() -> TemplateFile { @@ -10,7 +10,7 @@ fn create_template_file() -> TemplateFile { fn create_template() -> Template { Template::new( "standard".into(), - get_fixtures_dir("generator").join("templates/standard"), + get_fixtures_path("generator/templates/standard"), ) .unwrap() } @@ -28,11 +28,10 @@ mod load_files { #[tokio::test] async fn filters_out_schema_file() { - let dest = assert_fs::TempDir::new().unwrap(); let mut template = create_template(); template - .load_files(dest.path(), &create_context()) + .load_files(&get_fixtures_path("generator"), &create_context()) .await .unwrap(); @@ -135,9 +134,8 @@ mod interpolate_path { } mod set_content { - use moon_config::TemplateFrontmatterConfig; - use super::*; + use moon_config::TemplateFrontmatterConfig; #[test] fn works_without_frontmatter() { diff --git a/crates/core/project-graph/Cargo.toml b/crates/core/project-graph/Cargo.toml index b81bd84c8e6..af8cbcba8e9 100644 --- a/crates/core/project-graph/Cargo.toml +++ b/crates/core/project-graph/Cargo.toml @@ -25,7 +25,7 @@ thiserror = { workspace = true } [dev-dependencies] moon_node_platform = { path = "../../node/platform" } +moon_test_utils = { path = "../test-utils" } criterion = { workspace = true } -insta = { workspace = true } -tokio = { workspace = true, features = ["test-util"] } +tokio = { workspace = true } diff --git a/crates/core/project-graph/benches/project_graph_benchmark.rs b/crates/core/project-graph/benches/project_graph_benchmark.rs index cd30d3fee16..1a5a9ffbc6e 100644 --- a/crates/core/project-graph/benches/project_graph_benchmark.rs +++ b/crates/core/project-graph/benches/project_graph_benchmark.rs @@ -2,11 +2,11 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; use moon_cache::CacheEngine; use moon_config::{GlobalProjectConfig, ToolchainConfig, WorkspaceConfig, WorkspaceProjects}; use moon_project_graph::ProjectGraph; -use moon_utils::test::get_fixtures_dir; +use moon_test_utils::get_fixtures_path; use rustc_hash::FxHashMap; pub fn load_benchmark(c: &mut Criterion) { - let workspace_root = get_fixtures_dir("cases"); + let workspace_root = get_fixtures_path("cases"); let workspace_config = WorkspaceConfig { projects: WorkspaceProjects::Sources(FxHashMap::from_iter([( "base".to_owned(), @@ -40,7 +40,7 @@ pub fn load_benchmark(c: &mut Criterion) { } pub fn load_all_benchmark(c: &mut Criterion) { - let workspace_root = get_fixtures_dir("cases"); + let workspace_root = get_fixtures_path("cases"); let workspace_config = WorkspaceConfig::default(); c.bench_function("project_graph_load_all", |b| { diff --git a/crates/core/project-graph/tests/project_graph_test.rs b/crates/core/project-graph/tests/project_graph_test.rs index b3fe626fe02..694101f7ea0 100644 --- a/crates/core/project-graph/tests/project_graph_test.rs +++ b/crates/core/project-graph/tests/project_graph_test.rs @@ -1,53 +1,40 @@ -use insta::assert_snapshot; use moon_cache::CacheEngine; -use moon_config::{ - GlobalProjectConfig, NodeConfig, NodeProjectAliasFormat, ToolchainConfig, WorkspaceConfig, - WorkspaceProjects, -}; +use moon_config::{GlobalProjectConfig, ToolchainConfig, WorkspaceConfig, WorkspaceProjects}; +use moon_node_platform::NodePlatform; +use moon_platform::Platformable; +use moon_project::{ProjectDependency, ProjectDependencySource}; use moon_project_graph::ProjectGraph; +use moon_test_utils::{ + assert_snapshot, create_sandbox_with_config, get_project_graph_aliases_fixture_configs, Sandbox, +}; use moon_utils::string_vec; -use moon_utils::test::{create_sandbox, create_sandbox_with_git, get_fixtures_dir}; use rustc_hash::FxHashMap; -use std::fs; -async fn get_aliases_graph() -> ProjectGraph { - let workspace_root = get_fixtures_dir("project-graph/aliases"); - let workspace_config = WorkspaceConfig { - projects: WorkspaceProjects::Sources(FxHashMap::from_iter([ - ("explicit".to_owned(), "explicit".to_owned()), - ( - "explicitAndImplicit".to_owned(), - "explicit-and-implicit".to_owned(), - ), - ("implicit".to_owned(), "implicit".to_owned()), - ("noLang".to_owned(), "no-lang".to_owned()), - ("node".to_owned(), "node".to_owned()), - ("nodeNameOnly".to_owned(), "node-name-only".to_owned()), - ("nodeNameScope".to_owned(), "node-name-scope".to_owned()), - ])), - ..WorkspaceConfig::default() - }; - let toolchain_config = ToolchainConfig { - node: Some(NodeConfig { - alias_package_names: Some(NodeProjectAliasFormat::NameAndScope), - ..NodeConfig::default() - }), - ..ToolchainConfig::default() - }; +async fn get_aliases_graph() -> (ProjectGraph, Sandbox) { + let (workspace_config, toolchain_config, projects_config) = + get_project_graph_aliases_fixture_configs(); + + let sandbox = create_sandbox_with_config( + "project-graph/aliases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); - ProjectGraph::generate( - &workspace_root, + let graph = ProjectGraph::generate( + sandbox.path(), &workspace_config, &toolchain_config, GlobalProjectConfig::default(), - &CacheEngine::load(&workspace_root).await.unwrap(), + &CacheEngine::load(sandbox.path()).await.unwrap(), ) .await - .unwrap() + .unwrap(); + + (graph, sandbox) } -async fn get_dependencies_graph() -> ProjectGraph { - let workspace_root = get_fixtures_dir("project-graph/dependencies"); +async fn get_dependencies_graph() -> (ProjectGraph, Sandbox) { let workspace_config = WorkspaceConfig { projects: WorkspaceProjects::Sources(FxHashMap::from_iter([ ("a".to_owned(), "a".to_owned()), @@ -58,19 +45,27 @@ async fn get_dependencies_graph() -> ProjectGraph { ..WorkspaceConfig::default() }; - ProjectGraph::generate( - &workspace_root, + let sandbox = create_sandbox_with_config( + "project-graph/dependencies", + Some(&workspace_config), + None, + None, + ); + + let graph = ProjectGraph::generate( + sandbox.path(), &workspace_config, &ToolchainConfig::default(), GlobalProjectConfig::default(), - &CacheEngine::load(&workspace_root).await.unwrap(), + &CacheEngine::load(sandbox.path()).await.unwrap(), ) .await - .unwrap() + .unwrap(); + + (graph, sandbox) } -async fn get_dependents_graph() -> ProjectGraph { - let workspace_root = get_fixtures_dir("project-graph/dependents"); +async fn get_dependents_graph() -> (ProjectGraph, Sandbox) { let workspace_config = WorkspaceConfig { projects: WorkspaceProjects::Sources(FxHashMap::from_iter([ ("a".to_owned(), "a".to_owned()), @@ -81,35 +76,28 @@ async fn get_dependents_graph() -> ProjectGraph { ..WorkspaceConfig::default() }; - ProjectGraph::generate( - &workspace_root, + let sandbox = create_sandbox_with_config( + "project-graph/dependents", + Some(&workspace_config), + None, + None, + ); + + let graph = ProjectGraph::generate( + sandbox.path(), &workspace_config, &ToolchainConfig::default(), GlobalProjectConfig::default(), - &CacheEngine::load(&workspace_root).await.unwrap(), + &CacheEngine::load(sandbox.path()).await.unwrap(), ) .await - .unwrap() + .unwrap(); + + (graph, sandbox) } #[tokio::test] async fn can_use_map_and_globs_setting() { - let fixture = create_sandbox("projects"); - - fs::write( - fixture.path().join(".moon/workspace.yml"), - r#" -extends: '../shared-workspace.yml' -projects: - globs: - - 'deps/*' - sources: - basic: basic - noConfig: noConfig -"#, - ) - .unwrap(); - let workspace_config = WorkspaceConfig { projects: WorkspaceProjects::Both { globs: string_vec!["deps/*"], @@ -121,12 +109,14 @@ projects: ..WorkspaceConfig::default() }; + let sandbox = create_sandbox_with_config("projects", Some(&workspace_config), None, None); + let graph = ProjectGraph::generate( - fixture.path(), + sandbox.path(), &workspace_config, &ToolchainConfig::default(), GlobalProjectConfig::default(), - &CacheEngine::load(fixture.path()).await.unwrap(), + &CacheEngine::load(sandbox.path()).await.unwrap(), ) .await .unwrap(); @@ -148,24 +138,24 @@ mod globs { #[tokio::test] async fn ignores_dot_folders() { - // Use git so we can test against the .git folder - let fixture = create_sandbox_with_git("projects"); - - // Create fake node modules - fs::create_dir_all(fixture.path().join("node_modules/moon")).unwrap(); - fs::write(fixture.path().join("node_modules/moon/package.json"), "{}").unwrap(); - let workspace_config = WorkspaceConfig { projects: WorkspaceProjects::Globs(string_vec!["**"]), ..WorkspaceConfig::default() }; + // Use git so we can test against the .git folder + let sandbox = create_sandbox_with_config("projects", Some(&workspace_config), None, None); + sandbox.enable_git(); + + // Create fake node modules + sandbox.create_file("node_modules/moon/package.json", "{}"); + let graph = ProjectGraph::generate( - fixture.path(), + sandbox.path(), &workspace_config, &ToolchainConfig::default(), GlobalProjectConfig::default(), - &CacheEngine::load(fixture.path()).await.unwrap(), + &CacheEngine::load(sandbox.path()).await.unwrap(), ) .await .unwrap(); @@ -194,19 +184,20 @@ mod globs { #[tokio::test] async fn supports_all_id_formats() { - let fixture = create_sandbox("project-graph/ids"); - let workspace_config = WorkspaceConfig { projects: WorkspaceProjects::Globs(string_vec!["*"]), ..WorkspaceConfig::default() }; + let sandbox = + create_sandbox_with_config("project-graph/ids", Some(&workspace_config), None, None); + let graph = ProjectGraph::generate( - fixture.path(), + sandbox.path(), &workspace_config, &ToolchainConfig::default(), GlobalProjectConfig::default(), - &CacheEngine::load(fixture.path()).await.unwrap(), + &CacheEngine::load(sandbox.path()).await.unwrap(), ) .await .unwrap(); @@ -230,7 +221,7 @@ mod get_dependencies_of { #[tokio::test] async fn returns_dep_list() { - let graph = get_dependencies_graph().await; + let (graph, _sandbox) = get_dependencies_graph().await; let a = graph.load("a").unwrap(); let b = graph.load("b").unwrap(); @@ -252,7 +243,7 @@ mod get_dependents_of { #[tokio::test] async fn returns_dep_list() { - let graph = get_dependents_graph().await; + let (graph, _sandbox) = get_dependents_graph().await; let a = graph.load("a").unwrap(); let b = graph.load("b").unwrap(); @@ -274,7 +265,7 @@ mod to_dot { #[tokio::test] async fn renders_tree() { - let graph = get_dependencies_graph().await; + let (graph, _sandbox) = get_dependencies_graph().await; graph.load("a").unwrap(); graph.load("b").unwrap(); @@ -287,13 +278,10 @@ mod to_dot { mod implicit_explicit_deps { use super::*; - use moon_node_platform::NodePlatform; - use moon_platform::Platformable; - use moon_project::{ProjectDependency, ProjectDependencySource}; #[tokio::test] async fn loads_implicit() { - let mut graph = get_aliases_graph().await; + let (mut graph, _sandbox) = get_aliases_graph().await; graph .register_platform(Box::new(NodePlatform::default())) @@ -328,7 +316,7 @@ mod implicit_explicit_deps { #[tokio::test] async fn loads_explicit() { - let mut graph = get_aliases_graph().await; + let (mut graph, _sandbox) = get_aliases_graph().await; graph .register_platform(Box::new(NodePlatform::default())) @@ -363,7 +351,7 @@ mod implicit_explicit_deps { #[tokio::test] async fn loads_explicit_and_implicit() { - let mut graph = get_aliases_graph().await; + let (mut graph, _sandbox) = get_aliases_graph().await; graph .register_platform(Box::new(NodePlatform::default())) diff --git a/crates/core/project/Cargo.toml b/crates/core/project/Cargo.toml index d4ef3aefc9b..f3dc179e3fd 100644 --- a/crates/core/project/Cargo.toml +++ b/crates/core/project/Cargo.toml @@ -16,4 +16,4 @@ strum = { version = "0.24.1", features = ["derive"] } thiserror = { workspace = true } [dev-dependencies] -pretty_assertions = "1.3.0" +moon_test_utils = { path = "../test-utils" } diff --git a/crates/core/project/tests/project_test.rs b/crates/core/project/tests/project_test.rs index 03e3a3b1474..6b2aea35e4c 100644 --- a/crates/core/project/tests/project_test.rs +++ b/crates/core/project/tests/project_test.rs @@ -5,8 +5,8 @@ use moon_config::{ }; use moon_project::{Project, ProjectError}; use moon_task::{EnvVars, FileGroup, Target, Task}; +use moon_test_utils::{get_fixtures_path, get_fixtures_root}; use moon_utils::string_vec; -use moon_utils::test::{get_fixtures_dir, get_fixtures_root}; use rustc_hash::{FxHashMap, FxHashSet}; use std::collections::BTreeMap; use std::path::Path; @@ -34,9 +34,7 @@ fn create_expanded_project( config: &GlobalProjectConfig, ) -> Project { let mut project = Project::new(id, source, workspace_root, config).unwrap(); - project.expand_tasks(workspace_root, &[], &[]).unwrap(); - project } @@ -222,8 +220,8 @@ mod tasks { use moon_task::test::{ create_expanded_task as create_expanded_task_internal, create_file_groups_config, }; + use moon_test_utils::pretty_assertions::assert_eq; use moon_utils::glob; - use pretty_assertions::assert_eq; fn mock_task_config(command: &str) -> TaskConfig { TaskConfig { @@ -904,14 +902,14 @@ mod tasks { mod expands_deps { use super::*; - use pretty_assertions::assert_eq; + use moon_test_utils::pretty_assertions::assert_eq; #[test] fn resolves_self_scope() { let project = create_expanded_project( "id", "self", - &get_fixtures_dir("task-deps"), + &get_fixtures_path("task-deps"), &mock_global_project_config(), ); @@ -926,7 +924,7 @@ mod tasks { let project = create_expanded_project( "id", "self-no-prefix", - &get_fixtures_dir("task-deps"), + &get_fixtures_path("task-deps"), &mock_global_project_config(), ); @@ -941,7 +939,7 @@ mod tasks { let project = create_expanded_project( "id", "self-dupes", - &get_fixtures_dir("task-deps"), + &get_fixtures_path("task-deps"), &mock_global_project_config(), ); @@ -956,7 +954,7 @@ mod tasks { let project = create_expanded_project( "id", "deps", - &get_fixtures_dir("task-deps"), + &get_fixtures_path("task-deps"), &mock_global_project_config(), ); @@ -972,7 +970,7 @@ mod tasks { let project = create_expanded_project( "id", "deps-dupes", - &get_fixtures_dir("task-deps"), + &get_fixtures_path("task-deps"), &mock_global_project_config(), ); @@ -989,7 +987,7 @@ mod tasks { create_expanded_project( "id", "all", - &get_fixtures_dir("task-deps"), + &get_fixtures_path("task-deps"), &mock_global_project_config(), ); } @@ -999,7 +997,7 @@ mod tasks { use super::*; use moon_config::DependencyConfig; use moon_project::ProjectDependency; - use pretty_assertions::assert_eq; + use moon_test_utils::pretty_assertions::assert_eq; use std::path::PathBuf; #[test] @@ -1191,7 +1189,7 @@ mod tasks { #[test] fn expands_inputs() { - let workspace_root = get_fixtures_dir("base"); + let workspace_root = get_fixtures_path("base"); let project_root = workspace_root.join("files-and-dirs"); let project = create_expanded_project( "id", @@ -1249,7 +1247,7 @@ mod tasks { #[test] fn expands_implicit_deps() { - let workspace_root = get_fixtures_dir("base"); + let workspace_root = get_fixtures_path("base"); let mut project = Project::new( "id", "files-and-dirs", @@ -1294,7 +1292,7 @@ mod tasks { #[test] fn expands_implicit_inputs() { - let workspace_root = get_fixtures_dir("base"); + let workspace_root = get_fixtures_path("base"); let project_root = workspace_root.join("files-and-dirs"); let mut project = Project::new( "id", @@ -1398,7 +1396,7 @@ mod workspace { let project = create_expanded_project( "id", "include", - &get_fixtures_dir("task-inheritance"), + &get_fixtures_path("task-inheritance"), &mock_global_project_config(), ); @@ -1410,7 +1408,7 @@ mod workspace { let project = create_expanded_project( "id", "include-none", - &get_fixtures_dir("task-inheritance"), + &get_fixtures_path("task-inheritance"), &mock_global_project_config(), ); @@ -1422,7 +1420,7 @@ mod workspace { let project = create_expanded_project( "id", "exclude", - &get_fixtures_dir("task-inheritance"), + &get_fixtures_path("task-inheritance"), &mock_global_project_config(), ); @@ -1434,7 +1432,7 @@ mod workspace { let project = create_expanded_project( "id", "exclude-all", - &get_fixtures_dir("task-inheritance"), + &get_fixtures_path("task-inheritance"), &mock_global_project_config(), ); @@ -1446,7 +1444,7 @@ mod workspace { let project = create_expanded_project( "id", "exclude-none", - &get_fixtures_dir("task-inheritance"), + &get_fixtures_path("task-inheritance"), &mock_global_project_config(), ); @@ -1458,7 +1456,7 @@ mod workspace { let project = create_expanded_project( "id", "rename", - &get_fixtures_dir("task-inheritance"), + &get_fixtures_path("task-inheritance"), &mock_global_project_config(), ); @@ -1470,7 +1468,7 @@ mod workspace { #[test] fn rename_merge() { - let workspace_root = get_fixtures_dir("task-inheritance"); + let workspace_root = get_fixtures_path("task-inheritance"); let project = create_expanded_project( "id", "rename-merge", @@ -1497,7 +1495,7 @@ mod workspace { let project = create_expanded_project( "id", "include-exclude", - &get_fixtures_dir("task-inheritance"), + &get_fixtures_path("task-inheritance"), &mock_global_project_config(), ); @@ -1509,7 +1507,7 @@ mod workspace { let project = create_expanded_project( "id", "include-exclude-rename", - &get_fixtures_dir("task-inheritance"), + &get_fixtures_path("task-inheritance"), &mock_global_project_config(), ); diff --git a/crates/core/runner/Cargo.toml b/crates/core/runner/Cargo.toml index 025c961db5a..019a44d59e9 100644 --- a/crates/core/runner/Cargo.toml +++ b/crates/core/runner/Cargo.toml @@ -47,5 +47,5 @@ tokio = { workspace = true } [dev-dependencies] moon_cache = { path = "../cache" } +moon_test_utils = { path = "../test-utils" } criterion = { workspace = true } -insta = { workspace = true } diff --git a/crates/core/runner/benches/dep_graph_benchmark.rs b/crates/core/runner/benches/dep_graph_benchmark.rs index 5c415d5413d..6f6b1fb039e 100644 --- a/crates/core/runner/benches/dep_graph_benchmark.rs +++ b/crates/core/runner/benches/dep_graph_benchmark.rs @@ -1,13 +1,13 @@ mod utils; use criterion::{criterion_group, criterion_main, Criterion}; -use moon_utils::test::get_fixtures_dir; +use moon_test_utils::get_fixtures_path; use moon_workspace::Workspace; pub fn load_benchmark(c: &mut Criterion) { c.bench_function("dep_graph_load", |b| { b.iter(|| async { - let workspace = Workspace::load_from(get_fixtures_dir("cases")) + let workspace = Workspace::load_from(get_fixtures_path("cases")) .await .unwrap(); @@ -19,7 +19,7 @@ pub fn load_benchmark(c: &mut Criterion) { pub fn load_with_platforms_benchmark(c: &mut Criterion) { c.bench_function("dep_graph_load_with_platforms", |b| { b.iter(|| async { - let mut workspace = Workspace::load_from(get_fixtures_dir("cases")) + let mut workspace = Workspace::load_from(get_fixtures_path("cases")) .await .unwrap(); diff --git a/crates/core/runner/benches/runner_benchmark.rs b/crates/core/runner/benches/runner_benchmark.rs index 730dfae8523..3c98f9d6513 100644 --- a/crates/core/runner/benches/runner_benchmark.rs +++ b/crates/core/runner/benches/runner_benchmark.rs @@ -3,13 +3,13 @@ mod utils; use criterion::{black_box, criterion_group, criterion_main, Criterion}; use moon_runner::Runner; use moon_runner_context::RunnerContext; -use moon_utils::test::get_fixtures_dir; +use moon_test_utils::get_fixtures_path; use moon_workspace::Workspace; pub fn runner_benchmark(c: &mut Criterion) { c.bench_function("runner", |b| { b.iter(|| async { - let workspace = Workspace::load_from(get_fixtures_dir("cases")) + let workspace = Workspace::load_from(get_fixtures_path("cases")) .await .unwrap(); @@ -28,7 +28,7 @@ pub fn runner_benchmark(c: &mut Criterion) { pub fn runner_with_platforms_benchmark(c: &mut Criterion) { c.bench_function("runner_with_platforms", |b| { b.iter(|| async { - let mut workspace = Workspace::load_from(get_fixtures_dir("cases")) + let mut workspace = Workspace::load_from(get_fixtures_path("cases")) .await .unwrap(); diff --git a/crates/core/runner/tests/dep_graph_test.rs b/crates/core/runner/tests/dep_graph_test.rs index 6f034bd8331..fb26973a4fb 100644 --- a/crates/core/runner/tests/dep_graph_test.rs +++ b/crates/core/runner/tests/dep_graph_test.rs @@ -1,4 +1,3 @@ -use insta::assert_snapshot; use moon_cache::CacheEngine; use moon_config::{ GlobalProjectConfig, NodeConfig, ToolchainConfig, WorkspaceConfig, WorkspaceProjects, @@ -9,7 +8,8 @@ use moon_project_graph::ProjectGraph; use moon_runner::{BatchedTopoSort, DepGraph, NodeIndex}; use moon_system_platform::SystemPlatform; use moon_task::Target; -use moon_utils::test::{create_sandbox, TempDir}; +use moon_test_utils::{assert_snapshot, create_sandbox_with_config, Sandbox}; +use moon_utils::string_vec; use rustc_hash::{FxHashMap, FxHashSet}; fn register_platforms(project_graph: &mut ProjectGraph) { @@ -21,50 +21,66 @@ fn register_platforms(project_graph: &mut ProjectGraph) { .unwrap(); } -async fn create_project_graph() -> (ProjectGraph, TempDir) { - let fixture = create_sandbox("projects"); - let workspace_root = fixture.path(); +async fn create_project_graph() -> (ProjectGraph, Sandbox) { let workspace_config = WorkspaceConfig { projects: WorkspaceProjects::Sources(FxHashMap::from_iter([ ("advanced".to_owned(), "advanced".to_owned()), ("basic".to_owned(), "basic".to_owned()), ("emptyConfig".to_owned(), "empty-config".to_owned()), ("noConfig".to_owned(), "no-config".to_owned()), + // Deps ("foo".to_owned(), "deps/foo".to_owned()), ("bar".to_owned(), "deps/bar".to_owned()), ("baz".to_owned(), "deps/baz".to_owned()), + // Tasks ("tasks".to_owned(), "tasks".to_owned()), + // Languages + ("js".to_owned(), "langs/js".to_owned()), + ("ts".to_owned(), "langs/ts".to_owned()), + ("bash".to_owned(), "langs/bash".to_owned()), ("platforms".to_owned(), "platforms".to_owned()), ])), ..WorkspaceConfig::default() }; let toolchain_config = ToolchainConfig { node: Some(NodeConfig { - // Consistent snapshots version: "16.0.0".into(), + dedupe_on_lockfile_change: false, ..NodeConfig::default() }), ..ToolchainConfig::default() }; + let projects_config = GlobalProjectConfig { + file_groups: FxHashMap::from_iter([ + ("sources".to_owned(), string_vec!["src/**/*", "types/**/*"]), + ("tests".to_owned(), string_vec!["tests/**/*"]), + ]), + ..GlobalProjectConfig::default() + }; + + let sandbox = create_sandbox_with_config( + "projects", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); let mut graph = ProjectGraph::generate( - workspace_root, + sandbox.path(), &workspace_config, &toolchain_config, - GlobalProjectConfig::default(), - &CacheEngine::load(workspace_root).await.unwrap(), + projects_config, + &CacheEngine::load(sandbox.path()).await.unwrap(), ) .await .unwrap(); register_platforms(&mut graph); - (graph, fixture) + (graph, sandbox) } -async fn create_tasks_project_graph() -> (ProjectGraph, TempDir) { - let fixture = create_sandbox("tasks"); - let workspace_root = fixture.path(); +async fn create_tasks_project_graph() -> (ProjectGraph, Sandbox) { let workspace_config = WorkspaceConfig { projects: WorkspaceProjects::Sources(FxHashMap::from_iter([ ("basic".to_owned(), "basic".to_owned()), @@ -76,6 +92,10 @@ async fn create_tasks_project_graph() -> (ProjectGraph, TempDir) { ("inputA".to_owned(), "input-a".to_owned()), ("inputB".to_owned(), "input-b".to_owned()), ("inputC".to_owned(), "input-c".to_owned()), + ( + "mergeAllStrategies".to_owned(), + "merge-all-strategies".to_owned(), + ), ("mergeAppend".to_owned(), "merge-append".to_owned()), ("mergePrepend".to_owned(), "merge-prepend".to_owned()), ("mergeReplace".to_owned(), "merge-replace".to_owned()), @@ -85,30 +105,36 @@ async fn create_tasks_project_graph() -> (ProjectGraph, TempDir) { }; let toolchain_config = ToolchainConfig { node: Some(NodeConfig { - // Consistent snapshots version: "16.0.0".into(), ..NodeConfig::default() }), ..ToolchainConfig::default() }; - let global_config = GlobalProjectConfig { + let projects_config = GlobalProjectConfig { file_groups: FxHashMap::from_iter([("sources".to_owned(), vec!["src/**/*".to_owned()])]), ..GlobalProjectConfig::default() }; + let sandbox = create_sandbox_with_config( + "tasks", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + let mut graph = ProjectGraph::generate( - workspace_root, + sandbox.path(), &workspace_config, &toolchain_config, - global_config, - &CacheEngine::load(workspace_root).await.unwrap(), + projects_config, + &CacheEngine::load(sandbox.path()).await.unwrap(), ) .await .unwrap(); register_platforms(&mut graph); - (graph, fixture) + (graph, sandbox) } fn sort_batches(batches: BatchedTopoSort) -> BatchedTopoSort { @@ -137,7 +163,7 @@ fn default_graph() { expected = "CycleDetected(\"RunTarget(cycle:a) → RunTarget(cycle:b) → RunTarget(cycle:c)\")" )] async fn detects_cycles() { - let (projects, _fixture) = create_tasks_project_graph().await; + let (projects, _sandbox) = create_tasks_project_graph().await; let mut graph = DepGraph::default(); graph @@ -161,7 +187,7 @@ mod run_target { #[tokio::test] async fn single_targets() { - let (projects, _fixture) = create_project_graph().await; + let (projects, _sandbox) = create_project_graph().await; let mut graph = DepGraph::default(); graph @@ -194,7 +220,7 @@ mod run_target { #[tokio::test] async fn deps_chain_target() { - let (projects, _fixture) = create_tasks_project_graph().await; + let (projects, _sandbox) = create_tasks_project_graph().await; let mut graph = DepGraph::default(); graph @@ -242,7 +268,7 @@ mod run_target { #[tokio::test] async fn avoids_dupe_targets() { - let (projects, _fixture) = create_project_graph().await; + let (projects, _sandbox) = create_project_graph().await; let mut graph = DepGraph::default(); graph @@ -278,7 +304,7 @@ mod run_target { #[tokio::test] async fn runs_all_projects_for_target_all_scope() { - let (projects, _fixture) = create_tasks_project_graph().await; + let (projects, _sandbox) = create_tasks_project_graph().await; let mut graph = DepGraph::default(); graph @@ -321,7 +347,7 @@ mod run_target { #[tokio::test] #[should_panic(expected = "Target(NoProjectDepsInRunContext)")] async fn errors_for_target_deps_scope() { - let (projects, _fixture) = create_project_graph().await; + let (projects, _sandbox) = create_project_graph().await; let mut graph = DepGraph::default(); graph @@ -332,7 +358,7 @@ mod run_target { #[tokio::test] #[should_panic(expected = "Target(NoProjectSelfInRunContext)")] async fn errors_for_target_self_scope() { - let (projects, _fixture) = create_project_graph().await; + let (projects, _sandbox) = create_project_graph().await; let mut graph = DepGraph::default(); graph @@ -343,7 +369,7 @@ mod run_target { #[tokio::test] #[should_panic(expected = "Project(UnconfiguredID(\"unknown\"))")] async fn errors_for_unknown_project() { - let (projects, _fixture) = create_project_graph().await; + let (projects, _sandbox) = create_project_graph().await; let mut graph = DepGraph::default(); graph @@ -356,7 +382,7 @@ mod run_target { #[tokio::test] #[should_panic(expected = "Project(UnconfiguredTask(\"build\", \"tasks\"))")] async fn errors_for_unknown_task() { - let (projects, _fixture) = create_project_graph().await; + let (projects, _sandbox) = create_project_graph().await; let mut graph = DepGraph::default(); graph @@ -372,11 +398,11 @@ mod run_target_if_touched { #[tokio::test] async fn skips_if_untouched_project() { - let (projects, fixture) = create_tasks_project_graph().await; + let (projects, sandbox) = create_tasks_project_graph().await; let mut touched_files = FxHashSet::default(); - touched_files.insert(fixture.path().join("input-a/a.ts")); - touched_files.insert(fixture.path().join("input-c/c.ts")); + touched_files.insert(sandbox.path().join("input-a/a.ts")); + touched_files.insert(sandbox.path().join("input-c/c.ts")); let touched_files = Some(touched_files); let mut graph = DepGraph::default(); @@ -400,12 +426,12 @@ mod run_target_if_touched { #[tokio::test] async fn skips_if_untouched_task() { - let (projects, fixture) = create_tasks_project_graph().await; + let (projects, sandbox) = create_tasks_project_graph().await; let mut touched_files = FxHashSet::default(); - touched_files.insert(fixture.path().join("input-a/a2.ts")); - touched_files.insert(fixture.path().join("input-b/b2.ts")); - touched_files.insert(fixture.path().join("input-c/any.ts")); + touched_files.insert(sandbox.path().join("input-a/a2.ts")); + touched_files.insert(sandbox.path().join("input-b/b2.ts")); + touched_files.insert(sandbox.path().join("input-c/any.ts")); let touched_files = Some(touched_files); let mut graph = DepGraph::default(); @@ -448,7 +474,7 @@ mod sync_project { #[tokio::test] async fn isolated_projects() { - let (projects, _fixture) = create_project_graph().await; + let (projects, _sandbox) = create_project_graph().await; let mut graph = DepGraph::default(); sync_projects( @@ -482,7 +508,7 @@ mod sync_project { #[tokio::test] async fn projects_with_deps() { - let (projects, _fixture) = create_project_graph().await; + let (projects, _sandbox) = create_project_graph().await; let mut graph = DepGraph::default(); sync_projects(&mut graph, &projects, &["foo", "bar", "baz", "basic"]); @@ -519,7 +545,7 @@ mod sync_project { #[tokio::test] async fn projects_with_tasks() { - let (projects, _fixture) = create_project_graph().await; + let (projects, _sandbox) = create_project_graph().await; let mut graph = DepGraph::default(); sync_projects(&mut graph, &projects, &["noConfig", "tasks"]); @@ -546,7 +572,7 @@ mod sync_project { #[tokio::test] async fn avoids_dupe_projects() { - let (projects, _fixture) = create_project_graph().await; + let (projects, _sandbox) = create_project_graph().await; let mut graph = DepGraph::default(); sync_projects(&mut graph, &projects, &["advanced", "advanced", "advanced"]); @@ -557,7 +583,7 @@ mod sync_project { #[tokio::test] #[should_panic(expected = "UnconfiguredID(\"unknown\")")] async fn errors_for_unknown_project() { - let (projects, _fixture) = create_project_graph().await; + let (projects, _sandbox) = create_project_graph().await; let mut graph = DepGraph::default(); sync_projects(&mut graph, &projects, &["unknown"]); @@ -571,7 +597,7 @@ mod installs_deps { #[tokio::test] async fn tool_is_based_on_task_platform() { - let (projects, _fixture) = create_project_graph().await; + let (projects, _sandbox) = create_project_graph().await; let mut graph = DepGraph::default(); graph diff --git a/crates/core/task/Cargo.toml b/crates/core/task/Cargo.toml index d18ccf12e0b..e217b414c7a 100644 --- a/crates/core/task/Cargo.toml +++ b/crates/core/task/Cargo.toml @@ -14,3 +14,6 @@ rustc-hash = { workspace = true } serde = { workspace = true } strum = { version = "0.24.1", features = ["derive"] } thiserror = { workspace = true } + +[dev-dependencies] +moon_test_utils = { path = "../test-utils" } diff --git a/crates/core/task/src/file_group.rs b/crates/core/task/src/file_group.rs index bfd45ae202c..14980705d62 100644 --- a/crates/core/task/src/file_group.rs +++ b/crates/core/task/src/file_group.rs @@ -170,184 +170,3 @@ impl FileGroup { Ok(list) } } - -#[cfg(test)] -mod tests { - use super::*; - use moon_utils::string_vec; - use moon_utils::test::get_fixtures_dir; - - mod merge { - use super::*; - - #[test] - fn overwrites() { - let mut file_group = FileGroup::new("id", string_vec!["**/*"]); - - file_group.merge(string_vec!["*"]); - - assert_eq!(file_group.files, string_vec!["*"]); - } - } - - mod all { - use super::*; - - #[test] - fn returns_all() { - let workspace_root = get_fixtures_dir("base"); - let project_root = workspace_root.join("files-and-dirs"); - let file_group = FileGroup::new( - "id", - string_vec!["**/*", "file.js", "folder/index.ts", "/root.js", "/root/*"], - ); - - assert_eq!( - file_group.all(&workspace_root, &project_root).unwrap(), - ( - vec![ - project_root.join("file.js"), - project_root.join("folder/index.ts"), - workspace_root.join("root.js") - ], - vec![ - glob::normalize(project_root.join("**/*")).unwrap(), - glob::normalize(workspace_root.join("root/*")).unwrap() - ] - ) - ); - } - } - - mod dirs { - use super::*; - - #[test] - fn returns_all_dirs() { - let workspace_root = get_fixtures_dir("base"); - let project_root = workspace_root.join("files-and-dirs"); - let file_group = FileGroup::new("id", string_vec!["**/*"]); - - assert_eq!( - file_group.dirs(&workspace_root, &project_root).unwrap(), - vec![project_root.join("dir"), project_root.join("dir/subdir")] - ); - } - - #[test] - fn doesnt_return_files() { - let workspace_root = get_fixtures_dir("base"); - let project_root = workspace_root.join("files-and-dirs"); - let file_group = FileGroup::new("id", string_vec!["file.ts"]); - let result: Vec = vec![]; - - assert_eq!( - file_group.dirs(&workspace_root, &project_root).unwrap(), - result - ); - } - } - - mod files { - use super::*; - - #[test] - fn returns_all_files() { - let workspace_root = get_fixtures_dir("base"); - let project_root = workspace_root.join("files-and-dirs"); - let file_group = FileGroup::new( - "id", - string_vec![ - // Globs - "**/*.{ts,tsx}", - "/*.json", - // Literals - "README.md", - "/README.md" - ], - ); - - let mut files = file_group.files(&workspace_root, &project_root).unwrap(); - files.sort(); - - assert_eq!( - files, - vec![ - workspace_root.join("README.md"), - project_root.join("README.md"), - project_root.join("dir/other.tsx"), - project_root.join("dir/subdir/another.ts"), - project_root.join("file.ts"), - workspace_root.join("package.json"), - ] - ); - } - - #[test] - fn doesnt_return_dirs() { - let workspace_root = get_fixtures_dir("base"); - let project_root = workspace_root.join("files-and-dirs"); - let file_group = FileGroup::new("id", string_vec!["dir"]); - let result: Vec = vec![]; - - assert_eq!( - file_group.files(&workspace_root, &project_root).unwrap(), - result - ); - } - } - - mod globs { - use super::*; - - #[test] - fn returns_only_globs() { - let workspace_root = get_fixtures_dir("base"); - let project_root = workspace_root.join("files-and-dirs"); - let file_group = - FileGroup::new("id", string_vec!["**/*", "*.rs", "file.ts", "dir", "/*.js"]); - - assert_eq!( - file_group.globs(&workspace_root, &project_root).unwrap(), - vec![ - glob::normalize(project_root.join("**/*")).unwrap(), - glob::normalize(project_root.join("*.rs")).unwrap(), - glob::normalize(workspace_root.join("*.js")).unwrap() - ] - ); - } - } - - mod root { - use super::*; - - #[test] - fn returns_lowest_dir() { - let workspace_root = get_fixtures_dir("base"); - let project_root = workspace_root.join("files-and-dirs"); - let file_group = FileGroup::new("id", string_vec!["**/*"]); - - assert_eq!( - file_group.root(&project_root).unwrap(), - project_root.join("dir") - ); - } - - #[test] - fn returns_root_when_many() { - let workspace_root = get_fixtures_dir("projects"); - let file_group = FileGroup::new("id", string_vec!["**/*"]); - - assert_eq!(file_group.root(&workspace_root).unwrap(), workspace_root); - } - - #[test] - fn returns_root_when_no_dirs() { - let workspace_root = get_fixtures_dir("base"); - let project_root = workspace_root.join("files-and-dirs"); - let file_group = FileGroup::new("id", string_vec![]); - - assert_eq!(file_group.root(&project_root).unwrap(), project_root); - } - } -} diff --git a/crates/core/task/src/lib.rs b/crates/core/task/src/lib.rs index ef03066c876..cc422fd746d 100644 --- a/crates/core/task/src/lib.rs +++ b/crates/core/task/src/lib.rs @@ -6,10 +6,9 @@ pub mod test; mod token; mod types; -pub use moon_config::{PlatformType, TargetID, TaskConfig, TaskID, TaskOptionsConfig}; - pub use errors::*; pub use file_group::FileGroup; +pub use moon_config::{PlatformType, TargetID, TaskConfig, TaskID, TaskOptionsConfig}; pub use target::{Target, TargetProjectScope}; pub use task::{Task, TaskOptions}; pub use token::{ResolverData, ResolverType, TokenResolver, TokenType}; diff --git a/crates/core/task/tests/file_group_test.rs b/crates/core/task/tests/file_group_test.rs new file mode 100644 index 00000000000..035173fbee0 --- /dev/null +++ b/crates/core/task/tests/file_group_test.rs @@ -0,0 +1,178 @@ +use moon_task::FileGroup; +use moon_test_utils::get_fixtures_path; +use moon_utils::{glob, string_vec}; +use std::path::PathBuf; + +mod merge { + use super::*; + + #[test] + fn overwrites() { + let mut file_group = FileGroup::new("id", string_vec!["**/*"]); + + file_group.merge(string_vec!["*"]); + + assert_eq!(file_group.files, string_vec!["*"]); + } +} + +mod all { + use super::*; + + #[test] + fn returns_all() { + let workspace_root = get_fixtures_path("base"); + let project_root = workspace_root.join("files-and-dirs"); + let file_group = FileGroup::new( + "id", + string_vec!["**/*", "file.js", "folder/index.ts", "/root.js", "/root/*"], + ); + + assert_eq!( + file_group.all(&workspace_root, &project_root).unwrap(), + ( + vec![ + project_root.join("file.js"), + project_root.join("folder/index.ts"), + workspace_root.join("root.js") + ], + vec![ + glob::normalize(project_root.join("**/*")).unwrap(), + glob::normalize(workspace_root.join("root/*")).unwrap() + ] + ) + ); + } +} + +mod dirs { + use super::*; + + #[test] + fn returns_all_dirs() { + let workspace_root = get_fixtures_path("base"); + let project_root = workspace_root.join("files-and-dirs"); + let file_group = FileGroup::new("id", string_vec!["**/*"]); + + assert_eq!( + file_group.dirs(&workspace_root, &project_root).unwrap(), + vec![project_root.join("dir"), project_root.join("dir/subdir")] + ); + } + + #[test] + fn doesnt_return_files() { + let workspace_root = get_fixtures_path("base"); + let project_root = workspace_root.join("files-and-dirs"); + let file_group = FileGroup::new("id", string_vec!["file.ts"]); + let result: Vec = vec![]; + + assert_eq!( + file_group.dirs(&workspace_root, &project_root).unwrap(), + result + ); + } +} + +mod files { + use super::*; + + #[test] + fn returns_all_files() { + let workspace_root = get_fixtures_path("base"); + let project_root = workspace_root.join("files-and-dirs"); + let file_group = FileGroup::new( + "id", + string_vec![ + // Globs + "**/*.{ts,tsx}", + "/*.json", + // Literals + "README.md", + "/README.md" + ], + ); + + let mut files = file_group.files(&workspace_root, &project_root).unwrap(); + files.sort(); + + assert_eq!( + files, + vec![ + workspace_root.join("README.md"), + project_root.join("README.md"), + project_root.join("dir/other.tsx"), + project_root.join("dir/subdir/another.ts"), + project_root.join("file.ts"), + workspace_root.join("package.json"), + ] + ); + } + + #[test] + fn doesnt_return_dirs() { + let workspace_root = get_fixtures_path("base"); + let project_root = workspace_root.join("files-and-dirs"); + let file_group = FileGroup::new("id", string_vec!["dir"]); + let result: Vec = vec![]; + + assert_eq!( + file_group.files(&workspace_root, &project_root).unwrap(), + result + ); + } +} + +mod globs { + use super::*; + + #[test] + fn returns_only_globs() { + let workspace_root = get_fixtures_path("base"); + let project_root = workspace_root.join("files-and-dirs"); + let file_group = + FileGroup::new("id", string_vec!["**/*", "*.rs", "file.ts", "dir", "/*.js"]); + + assert_eq!( + file_group.globs(&workspace_root, &project_root).unwrap(), + vec![ + glob::normalize(project_root.join("**/*")).unwrap(), + glob::normalize(project_root.join("*.rs")).unwrap(), + glob::normalize(workspace_root.join("*.js")).unwrap() + ] + ); + } +} + +mod root { + use super::*; + + #[test] + fn returns_lowest_dir() { + let workspace_root = get_fixtures_path("base"); + let project_root = workspace_root.join("files-and-dirs"); + let file_group = FileGroup::new("id", string_vec!["**/*"]); + + assert_eq!( + file_group.root(&project_root).unwrap(), + project_root.join("dir") + ); + } + + #[test] + fn returns_root_when_many() { + let workspace_root = get_fixtures_path("projects"); + let file_group = FileGroup::new("id", string_vec!["**/*"]); + + assert_eq!(file_group.root(&workspace_root).unwrap(), workspace_root); + } + + #[test] + fn returns_root_when_no_dirs() { + let workspace_root = get_fixtures_path("base"); + let project_root = workspace_root.join("files-and-dirs"); + let file_group = FileGroup::new("id", string_vec![]); + + assert_eq!(file_group.root(&project_root).unwrap(), project_root); + } +} diff --git a/crates/core/task/tests/task_test.rs b/crates/core/task/tests/task_test.rs index 0b073f4591a..32986cfabf2 100644 --- a/crates/core/task/tests/task_test.rs +++ b/crates/core/task/tests/task_test.rs @@ -1,7 +1,7 @@ use moon_config::{TaskCommandArgs, TaskConfig, TaskOptionEnvFile, TaskOptionsConfig}; use moon_task::test::create_expanded_task; use moon_task::{Target, Task, TaskOptions}; -use moon_utils::test::{create_sandbox, get_fixtures_dir}; +use moon_test_utils::{create_sandbox, get_fixtures_path}; use moon_utils::{glob, string_vec}; use rustc_hash::{FxHashMap, FxHashSet}; use std::env; @@ -9,7 +9,7 @@ use std::env; #[test] #[should_panic(expected = "NoOutputGlob")] fn errors_for_output_glob() { - let workspace_root = get_fixtures_dir("projects"); + let workspace_root = get_fixtures_path("projects"); let project_root = workspace_root.join("basic"); create_expanded_task( @@ -24,9 +24,8 @@ fn errors_for_output_glob() { } mod from_config { - use moon_config::{TaskMergeStrategy, TaskOutputStyle}; - use super::*; + use moon_config::{TaskMergeStrategy, TaskOutputStyle}; #[test] fn sets_defaults() { @@ -345,7 +344,7 @@ mod is_affected { #[test] fn returns_true_if_var_truthy() { - let workspace_root = get_fixtures_dir("base"); + let workspace_root = get_fixtures_path("base"); let project_root = workspace_root.join("files-and-dirs"); let task = create_expanded_task( &workspace_root, @@ -366,7 +365,7 @@ mod is_affected { #[test] fn returns_false_if_var_missing() { - let workspace_root = get_fixtures_dir("base"); + let workspace_root = get_fixtures_path("base"); let project_root = workspace_root.join("files-and-dirs"); let task = create_expanded_task( &workspace_root, @@ -383,7 +382,7 @@ mod is_affected { #[test] fn returns_false_if_var_empty() { - let workspace_root = get_fixtures_dir("base"); + let workspace_root = get_fixtures_path("base"); let project_root = workspace_root.join("files-and-dirs"); let task = create_expanded_task( &workspace_root, @@ -404,7 +403,7 @@ mod is_affected { #[test] fn returns_true_if_matches_file() { - let workspace_root = get_fixtures_dir("base"); + let workspace_root = get_fixtures_path("base"); let project_root = workspace_root.join("files-and-dirs"); let task = create_expanded_task( &workspace_root, @@ -424,7 +423,7 @@ mod is_affected { #[test] fn returns_true_if_matches_glob() { - let workspace_root = get_fixtures_dir("base"); + let workspace_root = get_fixtures_path("base"); let project_root = workspace_root.join("files-and-dirs"); let task = create_expanded_task( &workspace_root, @@ -444,7 +443,7 @@ mod is_affected { #[test] fn returns_true_when_referencing_root_files() { - let workspace_root = get_fixtures_dir("base"); + let workspace_root = get_fixtures_path("base"); let project_root = workspace_root.join("files-and-dirs"); let task = create_expanded_task( &workspace_root, @@ -464,7 +463,7 @@ mod is_affected { #[test] fn returns_false_if_outside_project() { - let workspace_root = get_fixtures_dir("base"); + let workspace_root = get_fixtures_path("base"); let project_root = workspace_root.join("files-and-dirs"); let task = create_expanded_task( &workspace_root, @@ -484,7 +483,7 @@ mod is_affected { #[test] fn returns_false_if_no_match() { - let workspace_root = get_fixtures_dir("base"); + let workspace_root = get_fixtures_path("base"); let project_root = workspace_root.join("files-and-dirs"); let task = create_expanded_task( &workspace_root, @@ -505,18 +504,17 @@ mod is_affected { mod expand_env { use super::*; - use std::fs; #[test] #[should_panic(expected = "Error parsing line: 'FOO', error at line index: 3")] fn errors_on_invalid_file() { - let fixture = create_sandbox("cases"); - let project_root = fixture.path().join("base"); + let sandbox = create_sandbox("cases"); + let project_root = sandbox.path().join("base"); - fs::write(project_root.join(".env"), "FOO").unwrap(); + sandbox.create_file(".env", "FOO"); create_expanded_task( - fixture.path(), + sandbox.path(), &project_root, Some(TaskConfig { options: TaskOptionsConfig { @@ -538,11 +536,11 @@ mod expand_env { if moon_utils::is_ci() { panic!("InvalidEnvFile"); } else { - let fixture = create_sandbox("cases"); - let project_root = fixture.path().join("base"); + let sandbox = create_sandbox("cases"); + let project_root = sandbox.path().join("base"); create_expanded_task( - fixture.path(), + sandbox.path(), &project_root, Some(TaskConfig { options: TaskOptionsConfig { @@ -558,13 +556,13 @@ mod expand_env { #[test] fn loads_using_bool() { - let fixture = create_sandbox("cases"); - let project_root = fixture.path().join("base"); + let sandbox = create_sandbox("cases"); + let project_root = sandbox.path().join("base"); - fs::write(project_root.join(".env"), "FOO=foo\nBAR=123").unwrap(); + sandbox.create_file(".env", "FOO=foo\nBAR=123"); let task = create_expanded_task( - fixture.path(), + sandbox.path(), &project_root, Some(TaskConfig { options: TaskOptionsConfig { @@ -587,13 +585,13 @@ mod expand_env { #[test] fn loads_using_custom_path() { - let fixture = create_sandbox("cases"); - let project_root = fixture.path().join("base"); + let sandbox = create_sandbox("cases"); + let project_root = sandbox.path().join("base"); - fs::write(project_root.join(".env.production"), "FOO=foo\nBAR=123").unwrap(); + sandbox.create_file(".env.production", "FOO=foo\nBAR=123"); let task = create_expanded_task( - fixture.path(), + sandbox.path(), &project_root, Some(TaskConfig { options: TaskOptionsConfig { @@ -616,13 +614,13 @@ mod expand_env { #[test] fn doesnt_override_other_env() { - let fixture = create_sandbox("cases"); - let project_root = fixture.path().join("base"); + let sandbox = create_sandbox("cases"); + let project_root = sandbox.path().join("base"); - fs::write(project_root.join(".env"), "FOO=foo\nBAR=123").unwrap(); + sandbox.create_file(".env", "FOO=foo\nBAR=123"); let task = create_expanded_task( - fixture.path(), + sandbox.path(), &project_root, Some(TaskConfig { env: Some(FxHashMap::from_iter([( @@ -653,7 +651,7 @@ mod expand_inputs { #[test] fn filters_into_correct_types() { - let workspace_root = get_fixtures_dir("base"); + let workspace_root = get_fixtures_path("base"); let project_root = workspace_root.join("files-and-dirs"); let task = create_expanded_task( &workspace_root, diff --git a/crates/core/task/tests/token_test.rs b/crates/core/task/tests/token_test.rs index 337129386e8..fbae58dccfe 100644 --- a/crates/core/task/tests/token_test.rs +++ b/crates/core/task/tests/token_test.rs @@ -1,12 +1,12 @@ use moon_config::{ProjectConfig, ProjectLanguage, ProjectType, TaskConfig}; use moon_task::test::{create_expanded_task, create_file_groups, create_initial_task}; use moon_task::{ResolverData, TokenResolver}; -use moon_utils::test::get_fixtures_dir; +use moon_test_utils::get_fixtures_path; use moon_utils::{glob, string_vec}; use std::path::PathBuf; fn get_workspace_root() -> PathBuf { - get_fixtures_dir("base") + get_fixtures_path("base") } fn get_project_root() -> PathBuf { diff --git a/crates/core/test-utils/Cargo.toml b/crates/core/test-utils/Cargo.toml new file mode 100644 index 00000000000..951402a9de4 --- /dev/null +++ b/crates/core/test-utils/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "moon_test_utils" +version = "0.1.0" +edition = "2021" + +[dependencies] +moon_config = { path = "../config" } +assert_cmd = "2.0.6" +assert_fs = "1.0.9" +clean-path = "0.2.1" +dirs = "4.0.0" +insta = "1.21.1" +predicates = "2.1.3" +pretty_assertions = "1.3.0" +rustc-hash = { workspace = true } +serde_yaml = { workspace = true } diff --git a/crates/core/test-utils/src/cli.rs b/crates/core/test-utils/src/cli.rs new file mode 100644 index 00000000000..ccda2155d26 --- /dev/null +++ b/crates/core/test-utils/src/cli.rs @@ -0,0 +1,114 @@ +use crate::sandbox::{debug_sandbox_files, Sandbox}; +use assert_cmd::assert::Assert; +use dirs::home_dir; +use std::path::Path; + +pub fn create_moon_command>(path: T) -> assert_cmd::Command { + let mut cmd = assert_cmd::Command::cargo_bin("moon").unwrap(); + cmd.current_dir(path); + cmd.timeout(std::time::Duration::from_secs(90)); + cmd.env("RUST_BACKTRACE", "1"); + // Let our code know we're running tests + cmd.env("MOON_TEST", "true"); + // Hide install output as it disrupts testing snapshots + cmd.env("MOON_TEST_HIDE_INSTALL_OUTPUT", "true"); + // Standardize file system paths for testing snapshots + cmd.env("MOON_TEST_STANDARDIZE_PATHS", "true"); + // Enable logging for code coverage + cmd.env("MOON_LOG", "trace"); + cmd +} + +pub fn output_to_string(data: &[u8]) -> String { + String::from_utf8(data.to_vec()).unwrap_or_default() +} + +pub fn get_assert_output(assert: &Assert) -> String { + get_assert_stdout_output(assert) + &get_assert_stderr_output(assert) +} + +pub fn get_assert_stderr_output(assert: &Assert) -> String { + let mut output = String::new(); + let stderr = output_to_string(&assert.get_output().stderr); + + // We need to always show logs for proper code coverage, + // but this breaks snapshots, and as such, we need to manually + // filter out log lines and env vars! + for line in stderr.split('\n') { + if !line.starts_with("[error") + && !line.starts_with("[ warn") + && !line.starts_with("[ info") + && !line.starts_with("[debug") + && !line.starts_with("[trace") + && !line.starts_with(" MOON_") + && !line.starts_with(" NODE_") + { + output.push_str(line); + output.push('\n'); + } + } + + output +} + +pub fn get_assert_stdout_output(assert: &Assert) -> String { + output_to_string(&assert.get_output().stdout) +} + +pub struct SandboxAssert<'s> { + pub inner: Assert, + pub sandbox: &'s Sandbox, +} + +impl<'s> SandboxAssert<'s> { + pub fn debug(&self) -> &Self { + println!("sandbox:"); + debug_sandbox_files(self.sandbox.path()); + println!("\n"); + + let output = self.inner.get_output(); + + println!("stdout:\n{}\n", output_to_string(&output.stdout)); + println!("stderr:\n{}\n", output_to_string(&output.stderr)); + println!("status: {:#?}", output.status); + + self + } + + pub fn code(self, num: i32) -> Assert { + self.inner.code(num) + } + + pub fn failure(self) -> Assert { + self.inner.failure() + } + + pub fn success(self) -> Assert { + self.inner.success() + } + + pub fn output(&self) -> String { + let mut output = + get_assert_stdout_output(&self.inner) + &get_assert_stderr_output(&self.inner); + + // Replace fixture path + let root = self.sandbox.path().to_str().unwrap(); + + output = output.replace(root, ""); + output = output.replace(&root.replace('\\', "/"), ""); + + // Replace home dir + if let Some(home_dir) = home_dir() { + let home = home_dir.to_str().unwrap(); + + output = output.replace(home, "~"); + output = output.replace(&home.replace('\\', "/"), "~"); + } + + output.replace("/private<", "<") + } + + pub fn output_standardized(&self) -> String { + self.output().replace('\\', "/") + } +} diff --git a/crates/core/test-utils/src/configs.rs b/crates/core/test-utils/src/configs.rs new file mode 100644 index 00000000000..7a899762331 --- /dev/null +++ b/crates/core/test-utils/src/configs.rs @@ -0,0 +1,319 @@ +use moon_config::{ + GlobalProjectConfig, NodeConfig, NodePackageManager, NodeProjectAliasFormat, NpmConfig, + PnpmConfig, TaskCommandArgs, TaskConfig, ToolchainConfig, TypeScriptConfig, WorkspaceConfig, + WorkspaceProjects, YarnConfig, +}; +use rustc_hash::FxHashMap; +use std::collections::BTreeMap; + +// Turn everything off by default +fn get_default_toolchain() -> ToolchainConfig { + ToolchainConfig { + node: Some(NodeConfig { + version: "18.0.0".into(), + add_engines_constraint: false, + dedupe_on_lockfile_change: false, + infer_tasks_from_scripts: false, + sync_project_workspace_dependencies: false, + ..NodeConfig::default() + }), + typescript: Some(TypeScriptConfig { + create_missing_config: false, + route_out_dir_to_cache: false, + sync_project_references: false, + sync_project_references_to_paths: false, + ..TypeScriptConfig::default() + }), + ..ToolchainConfig::default() + } +} + +pub fn get_cases_fixture_configs() -> (WorkspaceConfig, ToolchainConfig, GlobalProjectConfig) { + let workspace_config = WorkspaceConfig { + projects: WorkspaceProjects::Sources(FxHashMap::from_iter([ + ("root".to_owned(), ".".to_owned()), + ("base".to_owned(), "base".to_owned()), + ("noop".to_owned(), "noop".to_owned()), + ("files".to_owned(), "files".to_owned()), + // Runner + ("passthroughArgs".to_owned(), "passthrough-args".to_owned()), + // Project/task deps + ("depsA".to_owned(), "deps-a".to_owned()), + ("depsB".to_owned(), "deps-b".to_owned()), + ("depsC".to_owned(), "deps-c".to_owned()), + ("dependsOn".to_owned(), "depends-on".to_owned()), + // Target scopes + ("targetScopeA".to_owned(), "target-scope-a".to_owned()), + ("targetScopeB".to_owned(), "target-scope-b".to_owned()), + ("targetScopeC".to_owned(), "target-scope-c".to_owned()), + // Outputs + ("outputs".to_owned(), "outputs".to_owned()), + ("outputStyles".to_owned(), "output-styles".to_owned()), + ])), + ..WorkspaceConfig::default() + }; + + let toolchain_config = get_default_toolchain(); + + let projects_config = GlobalProjectConfig { + tasks: BTreeMap::from_iter([( + "noop".to_owned(), + TaskConfig { + command: Some(TaskCommandArgs::String("noop".into())), + ..TaskConfig::default() + }, + )]), + ..GlobalProjectConfig::default() + }; + + (workspace_config, toolchain_config, projects_config) +} + +pub fn get_projects_fixture_configs() -> (WorkspaceConfig, ToolchainConfig, GlobalProjectConfig) { + let workspace_config = WorkspaceConfig { + projects: WorkspaceProjects::Sources(FxHashMap::from_iter([ + ("advanced".to_owned(), "advanced".to_owned()), + ("basic".to_owned(), "basic".to_owned()), + ("emptyConfig".to_owned(), "empty-config".to_owned()), + ("noConfig".to_owned(), "no-config".to_owned()), + ("tasks".to_owned(), "tasks".to_owned()), + // Deps + ("foo".to_owned(), "deps/foo".to_owned()), + ("bar".to_owned(), "deps/bar".to_owned()), + ("baz".to_owned(), "deps/baz".to_owned()), + // Langs + ("js".to_owned(), "langs/js".to_owned()), + ("ts".to_owned(), "langs/ts".to_owned()), + ("bash".to_owned(), "langs/bash".to_owned()), + ("platforms".to_owned(), "platforms".to_owned()), + ])), + ..WorkspaceConfig::default() + }; + + let toolchain_config = get_default_toolchain(); + + let projects_config = GlobalProjectConfig { + file_groups: FxHashMap::from_iter([ + ( + "sources".into(), + vec!["src/**/*".into(), "types/**/*".into()], + ), + ("tests".into(), vec!["tests/**/*".into()]), + ]), + ..GlobalProjectConfig::default() + }; + + (workspace_config, toolchain_config, projects_config) +} + +pub fn get_project_graph_aliases_fixture_configs( +) -> (WorkspaceConfig, ToolchainConfig, GlobalProjectConfig) { + let workspace_config = WorkspaceConfig { + projects: WorkspaceProjects::Sources(FxHashMap::from_iter([ + ("explicit".to_owned(), "explicit".to_owned()), + ( + "explicitAndImplicit".to_owned(), + "explicit-and-implicit".to_owned(), + ), + ("implicit".to_owned(), "implicit".to_owned()), + ("noLang".to_owned(), "no-lang".to_owned()), + // Node.js + ("node".to_owned(), "node".to_owned()), + ("nodeNameOnly".to_owned(), "node-name-only".to_owned()), + ("nodeNameScope".to_owned(), "node-name-scope".to_owned()), + ])), + ..WorkspaceConfig::default() + }; + + let toolchain_config = ToolchainConfig { + node: Some(NodeConfig { + version: "18.0.0".into(), + add_engines_constraint: false, + alias_package_names: Some(NodeProjectAliasFormat::NameAndScope), + dedupe_on_lockfile_change: false, + ..NodeConfig::default() + }), + ..ToolchainConfig::default() + }; + + let projects_config = GlobalProjectConfig::default(); + + (workspace_config, toolchain_config, projects_config) +} + +pub fn get_tasks_fixture_configs() -> (WorkspaceConfig, ToolchainConfig, GlobalProjectConfig) { + let workspace_config = WorkspaceConfig { + projects: WorkspaceProjects::Sources(FxHashMap::from_iter([ + ("basic".to_owned(), "basic".to_owned()), + ("buildA".to_owned(), "build-a".to_owned()), + ("buildB".to_owned(), "build-b".to_owned()), + ("buildC".to_owned(), "build-c".to_owned()), + ("chain".to_owned(), "chain".to_owned()), + ("cycle".to_owned(), "cycle".to_owned()), + ("inputA".to_owned(), "input-a".to_owned()), + ("inputB".to_owned(), "input-b".to_owned()), + ("inputC".to_owned(), "input-c".to_owned()), + ( + "mergeAllStrategies".to_owned(), + "merge-all-strategies".to_owned(), + ), + ("mergeAppend".to_owned(), "merge-append".to_owned()), + ("mergePrepend".to_owned(), "merge-prepend".to_owned()), + ("mergeReplace".to_owned(), "merge-replace".to_owned()), + ("noTasks".to_owned(), "no-tasks".to_owned()), + ])), + ..WorkspaceConfig::default() + }; + + let toolchain_config = get_default_toolchain(); + + let projects_config = GlobalProjectConfig { + tasks: BTreeMap::from_iter([ + ( + "standard".to_owned(), + TaskConfig { + command: Some(TaskCommandArgs::String("cmd".into())), + ..TaskConfig::default() + }, + ), + ( + "withArgs".to_owned(), + TaskConfig { + command: Some(TaskCommandArgs::String("cmd".into())), + args: Some(TaskCommandArgs::Sequence(vec![ + "--foo".into(), + "--bar".into(), + "baz".into(), + ])), + ..TaskConfig::default() + }, + ), + ( + "withInputs".to_owned(), + TaskConfig { + command: Some(TaskCommandArgs::String("cmd".into())), + inputs: Some(vec!["rel/file.*".into(), "/root.*".into()]), + ..TaskConfig::default() + }, + ), + ( + "withOutputs".to_owned(), + TaskConfig { + command: Some(TaskCommandArgs::String("cmd".into())), + inputs: Some(vec!["lib".into(), "/build".into()]), + ..TaskConfig::default() + }, + ), + ]), + ..GlobalProjectConfig::default() + }; + + (workspace_config, toolchain_config, projects_config) +} + +// NODE.JS + +pub fn get_node_fixture_configs() -> (WorkspaceConfig, ToolchainConfig, GlobalProjectConfig) { + let workspace_config = WorkspaceConfig { + projects: WorkspaceProjects::Sources(FxHashMap::from_iter([ + ("node".to_owned(), "base".to_owned()), + ("lifecycles".to_owned(), "lifecycles".to_owned()), + ("versionOverride".to_owned(), "version-override".to_owned()), + // Binaries + ("esbuild".to_owned(), "esbuild".to_owned()), + ("swc".to_owned(), "swc".to_owned()), + // Project/task deps + ("depsA".to_owned(), "deps-a".to_owned()), + ("depsB".to_owned(), "deps-b".to_owned()), + ("depsC".to_owned(), "deps-c".to_owned()), + ("depsD".to_owned(), "deps-d".to_owned()), + ("dependsOn".to_owned(), "depends-on".to_owned()), + ("dependsOnScopes".to_owned(), "depends-on-scopes".to_owned()), + ])), + ..WorkspaceConfig::default() + }; + + let toolchain_config = get_default_toolchain(); + + let projects_config = GlobalProjectConfig { + tasks: BTreeMap::from_iter([ + ( + "version".to_owned(), + TaskConfig { + command: Some(TaskCommandArgs::String("node".into())), + args: Some(TaskCommandArgs::String("--version".into())), + ..TaskConfig::default() + }, + ), + ( + "noop".to_owned(), + TaskConfig { + command: Some(TaskCommandArgs::String("noop".into())), + ..TaskConfig::default() + }, + ), + ]), + ..GlobalProjectConfig::default() + }; + + (workspace_config, toolchain_config, projects_config) +} + +pub fn get_node_depman_fixture_configs( + depman: &str, +) -> (WorkspaceConfig, ToolchainConfig, GlobalProjectConfig) { + let (mut workspace_config, mut toolchain_config, projects_config) = get_node_fixture_configs(); + + workspace_config.projects = WorkspaceProjects::Sources(FxHashMap::from_iter([ + (depman.to_owned(), "base".to_owned()), + ("other".to_owned(), "other".to_owned()), + ("notInWorkspace".to_owned(), "not-in-workspace".to_owned()), + ])); + + if let Some(node_config) = &mut toolchain_config.node { + match depman { + "npm" => { + node_config.package_manager = NodePackageManager::Npm; + node_config.npm = NpmConfig { + version: "8.0.0".into(), + }; + } + "pnpm" => { + node_config.package_manager = NodePackageManager::Pnpm; + node_config.pnpm = Some(PnpmConfig { + version: "7.5.0".into(), + }); + } + "yarn" => { + node_config.package_manager = NodePackageManager::Yarn; + node_config.yarn = Some(YarnConfig { + version: "3.3.0".into(), + plugins: Some(vec!["workspace-tools".into()]), + }); + } + "yarn1" => { + node_config.package_manager = NodePackageManager::Yarn; + node_config.yarn = Some(YarnConfig { + version: "1.22.0".into(), + plugins: None, + }); + } + _ => {} + } + } + + (workspace_config, toolchain_config, projects_config) +} + +pub fn get_typescript_fixture_configs() -> (WorkspaceConfig, ToolchainConfig, GlobalProjectConfig) { + let (mut workspace_config, mut toolchain_config, projects_config) = get_node_fixture_configs(); + + workspace_config.projects = WorkspaceProjects::Globs(vec!["*".into()]); + + if let Some(ts_config) = &mut toolchain_config.typescript { + ts_config.create_missing_config = true; + ts_config.sync_project_references = true; + } + + (workspace_config, toolchain_config, projects_config) +} diff --git a/crates/core/test-utils/src/lib.rs b/crates/core/test-utils/src/lib.rs new file mode 100644 index 00000000000..d91c02e43e8 --- /dev/null +++ b/crates/core/test-utils/src/lib.rs @@ -0,0 +1,34 @@ +mod cli; +mod configs; +mod sandbox; + +pub use assert_cmd; +pub use assert_fs; +pub use cli::*; +pub use configs::*; +pub use insta::*; +pub use predicates; +pub use pretty_assertions; +pub use sandbox::*; + +use clean_path::Clean; +use std::path::PathBuf; + +pub fn get_fixtures_root() -> PathBuf { + let mut root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + root.push("../../../tests/fixtures"); + root.clean() +} + +pub fn get_fixtures_path>(name: T) -> PathBuf { + let path = get_fixtures_root().join(name.as_ref()); + + if !path.exists() { + panic!( + "{}", + format!("Fixture {} does no exist.", path.to_string_lossy()) + ); + } + + path +} diff --git a/crates/core/test-utils/src/sandbox.rs b/crates/core/test-utils/src/sandbox.rs new file mode 100644 index 00000000000..7ce33b53ba4 --- /dev/null +++ b/crates/core/test-utils/src/sandbox.rs @@ -0,0 +1,176 @@ +use crate::cli::{create_moon_command, output_to_string, SandboxAssert}; +use crate::get_fixtures_path; +use assert_cmd::Command; +use assert_fs::prelude::*; +pub use assert_fs::TempDir; +use moon_config::{GlobalProjectConfig, ToolchainConfig, WorkspaceConfig}; +use std::fs; +use std::path::Path; +use std::process::Command as StdCommand; + +pub struct Sandbox { + pub fixture: TempDir, +} + +impl Sandbox { + pub fn path(&self) -> &Path { + self.fixture.path() + } + + pub fn create_file>(&self, name: &str, content: T) -> &Self { + self.fixture + .child(name) + .write_str(content.as_ref()) + .unwrap(); + + self + } + + pub fn debug_configs(&self) -> &Self { + for cfg in [ + ".moon/workspace.yml", + ".moon/toolchain.yml", + ".moon/project.yml", + ] { + let path = self.path().join(cfg); + + if path.exists() { + println!("{} = {}", cfg, fs::read_to_string(path).unwrap()); + } + } + + self + } + + pub fn debug_files(&self) -> &Self { + debug_sandbox_files(self.path()); + + self + } + + pub fn enable_git(&self) -> &Self { + if !self.path().join(".gitignore").exists() { + self.create_file(".gitignore", "node_modules"); + } + + // Initialize a git repo so that VCS commands work + self.run_git(|cmd| { + cmd.args(["init", "--initial-branch", "master"]); + }); + + // We must also add the files to the index + self.run_git(|cmd| { + cmd.args(["add", "--all", "."]); + }); + + // And commit them... this seems like a lot of overhead? + self.run_git(|cmd| { + cmd.args(["commit", "-m", "Fixtures"]) + .env("GIT_AUTHOR_NAME", "moon tests") + .env("GIT_AUTHOR_EMAIL", "fakeemail@moonrepo.dev") + .env("GIT_COMMITTER_NAME", "moon tests") + .env("GIT_COMMITTER_EMAIL", "fakeemail@moonrepo.dev"); + }); + + self + } + + pub fn run_git(&self, handler: C) -> &Self + where + C: FnOnce(&mut StdCommand), + { + let mut cmd = StdCommand::new(if cfg!(windows) { "git.exe" } else { "git" }); + cmd.current_dir(self.path()); + + handler(&mut cmd); + + let out = cmd.output().unwrap_or_else(|e| { + panic!("{:#?}", e); + }); + + if !out.status.success() { + println!("{}", output_to_string(&out.stdout)); + eprintln!("{}", output_to_string(&out.stderr)); + } + + self + } + + pub fn run_moon(&self, handler: C) -> SandboxAssert + where + C: FnOnce(&mut Command), + { + let mut cmd = create_moon_command(self.path()); + + handler(&mut cmd); + + SandboxAssert { + inner: cmd.assert(), + sandbox: self, + } + } +} + +pub fn create_temp_dir() -> TempDir { + TempDir::new().unwrap() +} + +pub fn create_sandbox>(fixture: T) -> Sandbox { + let temp_dir = create_temp_dir(); + + temp_dir + .copy_from(get_fixtures_path(fixture), &["**/*"]) + .unwrap(); + + Sandbox { + // command: None, + fixture: temp_dir, + } +} + +pub fn create_sandbox_with_config>( + fixture: T, + workspace_config: Option<&WorkspaceConfig>, + toolchain_config: Option<&ToolchainConfig>, + projects_config: Option<&GlobalProjectConfig>, +) -> Sandbox { + let sandbox = create_sandbox(fixture); + + sandbox.create_file( + ".moon/workspace.yml", + serde_yaml::to_string( + &workspace_config + .map(|c| c.to_owned()) + .unwrap_or_else(WorkspaceConfig::default), + ) + .unwrap(), + ); + + sandbox.create_file( + ".moon/toolchain.yml", + serde_yaml::to_string( + &toolchain_config + .map(|c| c.to_owned()) + .unwrap_or_else(ToolchainConfig::default), + ) + .unwrap(), + ); + + if let Some(config) = projects_config { + sandbox.create_file(".moon/project.yml", serde_yaml::to_string(&config).unwrap()); + } + + sandbox +} + +pub fn debug_sandbox_files(dir: &Path) { + for entry in std::fs::read_dir(dir).unwrap() { + let path = entry.unwrap().path(); + + if path.is_dir() { + debug_sandbox_files(&path); + } else { + println!("- {}", path.to_string_lossy()); + } + } +} diff --git a/crates/core/utils/Cargo.toml b/crates/core/utils/Cargo.toml index fb6fc883ed4..448073ac0ed 100644 --- a/crates/core/utils/Cargo.toml +++ b/crates/core/utils/Cargo.toml @@ -7,8 +7,6 @@ edition = "2021" moon_constants = { path = "../constants" } moon_error = { path = "../error" } moon_logger = { path = "../logger" } -assert_cmd = "2.0.6" -assert_fs = "1.0.9" async-recursion = "1.0.0" cached = { workspace = true } chrono = { version = "0.4.23", features = ["serde"] } @@ -33,4 +31,4 @@ tokio = { workspace = true } wax = "0.5.0" [dev-dependencies] -insta = { workspace = true } +moon_test_utils = { path = "../test-utils" } diff --git a/crates/core/utils/src/fs.rs b/crates/core/utils/src/fs.rs index 4ea3d8ddce2..389f8080d2e 100644 --- a/crates/core/utils/src/fs.rs +++ b/crates/core/utils/src/fs.rs @@ -291,7 +291,7 @@ pub async fn write>(path: T, data: impl AsRef<[u8]>) -> Result<() pub mod temp { use super::*; use moon_constants::CONFIG_DIRNAME; - use std::fs; // TEMPORARILY sync + use std::fs; pub fn get_dir() -> PathBuf { crate::get_workspace_root() diff --git a/crates/core/utils/src/lib.rs b/crates/core/utils/src/lib.rs index ff5c3dcc3cb..9d600725dc3 100644 --- a/crates/core/utils/src/lib.rs +++ b/crates/core/utils/src/lib.rs @@ -6,7 +6,6 @@ pub mod process; pub mod regex; pub mod semver; pub mod shell; -pub mod test; pub mod time; pub mod yaml; diff --git a/crates/core/utils/src/test.rs b/crates/core/utils/src/test.rs deleted file mode 100644 index 899b6a59461..00000000000 --- a/crates/core/utils/src/test.rs +++ /dev/null @@ -1,176 +0,0 @@ -use crate::path; -use crate::process::output_to_string; -use std::env; -use std::path::{Path, PathBuf}; -use std::process::Command; - -pub use assert_fs::TempDir; - -pub fn run_git_command(dir: P, handler: F) -where - P: AsRef, - F: FnOnce(&mut Command), -{ - let mut cmd = Command::new(if cfg!(windows) { "git.exe" } else { "git" }); - cmd.current_dir(dir.as_ref()); - - handler(&mut cmd); - - let out = cmd.output().unwrap_or_else(|e| { - panic!("{:#?}", e); - }); - - if !out.status.success() { - println!("{}", output_to_string(&out.stdout)); - eprintln!("{}", output_to_string(&out.stderr)); - } -} - -pub fn create_sandbox>(fixture: T) -> assert_fs::TempDir { - use assert_fs::prelude::*; - - let temp_dir = assert_fs::TempDir::new().unwrap(); - - temp_dir - .copy_from(get_fixtures_dir(fixture), &["**/*"]) - .unwrap(); - - temp_dir - .copy_from( - get_fixtures_root(), - &["shared-toolchain.yml", "shared-workspace.yml"], - ) - .unwrap(); - - temp_dir -} - -pub fn create_sandbox_with_git>(fixture: T) -> assert_fs::TempDir { - use assert_fs::prelude::*; - - let temp_dir = create_sandbox(fixture); - - if !temp_dir.path().join(".gitignore").exists() { - temp_dir - .child(".gitignore") - .write_str("node_modules") - .unwrap(); - } - - // Initialize a git repo so that VCS commands work - run_git_command(temp_dir.path(), |cmd| { - cmd.args(["init", "--initial-branch", "master"]); - }); - - // We must also add the files to the index - run_git_command(temp_dir.path(), |cmd| { - cmd.args(["add", "--all", "."]); - }); - - // And commit them... this seems like a lot of overhead? - run_git_command(temp_dir.path(), |cmd| { - cmd.args(["commit", "-m", "Fixtures"]) - .env("GIT_AUTHOR_NAME", "moon tests") - .env("GIT_AUTHOR_EMAIL", "fakeemail@moonrepo.dev") - .env("GIT_COMMITTER_NAME", "moon tests") - .env("GIT_COMMITTER_EMAIL", "fakeemail@moonrepo.dev"); - }); - - temp_dir -} - -pub fn get_fixtures_root() -> PathBuf { - let mut root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - root.push("../../../tests/fixtures"); - - path::normalize(root) -} - -pub fn get_fixtures_dir>(dir: T) -> PathBuf { - get_fixtures_root().join(dir.as_ref()) -} - -pub fn replace_fixtures_dir, P: AsRef>(value: T, dir: P) -> String { - let dir_str = dir.as_ref().to_str().unwrap(); - - // Replace both forward and backward slashes - value - .as_ref() - .replace(dir_str, "") - .replace(&path::standardize_separators(dir_str), "") -} - -pub fn create_moon_command>(path: T) -> assert_cmd::Command { - let mut cmd = assert_cmd::Command::cargo_bin("moon").unwrap(); - cmd.current_dir(path); - cmd.timeout(std::time::Duration::from_secs(90)); - cmd.env("RUST_BACKTRACE", "1"); - // Let our code know were running tests - cmd.env("MOON_TEST", "true"); - // Hide install output as it disrupts testing snapshots - cmd.env("MOON_TEST_HIDE_INSTALL_OUTPUT", "true"); - // Standardize file system paths for testing snapshots - cmd.env("MOON_TEST_STANDARDIZE_PATHS", "true"); - // Enable logging for code coverage - cmd.env("MOON_LOG", "trace"); - cmd -} - -pub fn get_assert_output(assert: &assert_cmd::assert::Assert) -> String { - get_assert_stdout_output(assert) + &get_assert_stderr_output_clean(assert) -} - -pub fn get_assert_stderr_output_clean(assert: &assert_cmd::assert::Assert) -> String { - let mut output = String::new(); - - // We need to always show logs for proper code coverage, - // but this breaks snapshots, and as such, we need to manually - // filter out log lines and env vars! - for line in get_assert_stderr_output(assert).split('\n') { - if !line.starts_with("[error") - && !line.starts_with("[ warn") - && !line.starts_with("[ info") - && !line.starts_with("[debug") - && !line.starts_with("[trace") - && !line.starts_with(" MOON_") - && !line.starts_with(" NODE_") - { - output.push_str(line); - output.push('\n'); - } - } - - output -} - -pub fn get_assert_stderr_output(assert: &assert_cmd::assert::Assert) -> String { - String::from_utf8(assert.get_output().stderr.to_owned()).unwrap() -} - -pub fn get_assert_stdout_output(assert: &assert_cmd::assert::Assert) -> String { - String::from_utf8(assert.get_output().stdout.to_owned()).unwrap() -} - -pub fn debug_sandbox_files(dir: &Path) { - for entry in std::fs::read_dir(dir).unwrap() { - let path = entry.unwrap().path(); - - if path.is_dir() { - debug_sandbox_files(&path); - } else { - println!("- {}", path.to_string_lossy()); - } - } -} - -pub fn debug_sandbox(fixture: &assert_fs::TempDir, assert: &assert_cmd::assert::Assert) { - // List all files in the sandbox - println!("sandbox:"); - debug_sandbox_files(fixture.path()); - println!("\n"); - - // Debug outputs - println!("stdout:\n{}\n", get_assert_stdout_output(assert)); - println!("stderr:\n{}\n", get_assert_stderr_output(assert)); - println!("status: {:#?}", assert.get_output().status); -} diff --git a/crates/core/utils/tests/json_test.rs b/crates/core/utils/tests/json_test.rs index 05ad88c2ea3..abb9384a35b 100644 --- a/crates/core/utils/tests/json_test.rs +++ b/crates/core/utils/tests/json_test.rs @@ -1,6 +1,5 @@ -use insta::assert_snapshot; +use moon_test_utils::{assert_snapshot, create_sandbox}; use moon_utils::json; -use moon_utils::test::create_sandbox; use std::fs; use std::fs::OpenOptions; use std::io::prelude::*; @@ -74,8 +73,8 @@ mod editor_config { #[test] fn uses_defaults_when_no_config() { - let fixture = create_sandbox("editor-config"); - let path = fixture.path().join("file.json"); + let sandbox = create_sandbox("editor-config"); + let path = sandbox.path().join("file.json"); json::write_with_config(&path, json::read(&path).unwrap(), true).unwrap(); @@ -84,8 +83,8 @@ mod editor_config { #[test] fn writes_ugly() { - let fixture = create_sandbox("editor-config"); - let path = fixture.path().join("file.json"); + let sandbox = create_sandbox("editor-config"); + let path = sandbox.path().join("file.json"); json::write_with_config(&path, json::read(&path).unwrap(), false).unwrap(); @@ -94,10 +93,10 @@ mod editor_config { #[test] fn can_change_space_indent() { - let fixture = create_sandbox("editor-config"); - let path = fixture.path().join("file.json"); + let sandbox = create_sandbox("editor-config"); + let path = sandbox.path().join("file.json"); - append_editor_config(fixture.path(), "[*.json]\nindent_size = 8"); + append_editor_config(sandbox.path(), "[*.json]\nindent_size = 8"); json::write_with_config(&path, json::read(&path).unwrap(), true).unwrap(); @@ -106,10 +105,10 @@ mod editor_config { #[test] fn can_change_tab_indent() { - let fixture = create_sandbox("editor-config"); - let path = fixture.path().join("file.json"); + let sandbox = create_sandbox("editor-config"); + let path = sandbox.path().join("file.json"); - append_editor_config(fixture.path(), "[*.json]\nindent_style = tab"); + append_editor_config(sandbox.path(), "[*.json]\nindent_style = tab"); json::write_with_config(&path, json::read(&path).unwrap(), true).unwrap(); @@ -118,10 +117,10 @@ mod editor_config { #[test] fn can_enable_trailing_line() { - let fixture = create_sandbox("editor-config"); - let path = fixture.path().join("file.json"); + let sandbox = create_sandbox("editor-config"); + let path = sandbox.path().join("file.json"); - append_editor_config(fixture.path(), "[*.json]\ninsert_final_newline = true"); + append_editor_config(sandbox.path(), "[*.json]\ninsert_final_newline = true"); json::write_with_config(&path, json::read(&path).unwrap(), true).unwrap(); @@ -130,10 +129,10 @@ mod editor_config { #[test] fn can_disable_trailing_line() { - let fixture = create_sandbox("editor-config"); - let path = fixture.path().join("file.json"); + let sandbox = create_sandbox("editor-config"); + let path = sandbox.path().join("file.json"); - append_editor_config(fixture.path(), "[*.json]\ninsert_final_newline = false"); + append_editor_config(sandbox.path(), "[*.json]\ninsert_final_newline = false"); json::write_with_config(&path, json::read(&path).unwrap(), true).unwrap(); diff --git a/crates/core/utils/tests/yaml_test.rs b/crates/core/utils/tests/yaml_test.rs index 537c2781971..381be933926 100644 --- a/crates/core/utils/tests/yaml_test.rs +++ b/crates/core/utils/tests/yaml_test.rs @@ -1,5 +1,4 @@ -use insta::assert_snapshot; -use moon_utils::test::create_sandbox; +use moon_test_utils::{assert_snapshot, create_sandbox}; use moon_utils::yaml; use std::fs; use std::fs::OpenOptions; @@ -21,8 +20,8 @@ mod editor_config { #[test] fn uses_defaults_when_no_config() { - let fixture = create_sandbox("editor-config"); - let path = fixture.path().join("file.yaml"); + let sandbox = create_sandbox("editor-config"); + let path = sandbox.path().join("file.yaml"); yaml::write_with_config(&path, yaml::read(&path).unwrap()).unwrap(); @@ -31,10 +30,10 @@ mod editor_config { #[test] fn can_change_space_indent() { - let fixture = create_sandbox("editor-config"); - let path = fixture.path().join("file.yaml"); + let sandbox = create_sandbox("editor-config"); + let path = sandbox.path().join("file.yaml"); - append_editor_config(fixture.path(), "[*.yaml]\nindent_size = 8"); + append_editor_config(sandbox.path(), "[*.yaml]\nindent_size = 8"); yaml::write_with_config(&path, yaml::read(&path).unwrap()).unwrap(); @@ -43,10 +42,10 @@ mod editor_config { #[test] fn can_change_tab_indent() { - let fixture = create_sandbox("editor-config"); - let path = fixture.path().join("file.yaml"); + let sandbox = create_sandbox("editor-config"); + let path = sandbox.path().join("file.yaml"); - append_editor_config(fixture.path(), "[*.yaml]\nindent_style = tab"); + append_editor_config(sandbox.path(), "[*.yaml]\nindent_style = tab"); yaml::write_with_config(&path, yaml::read(&path).unwrap()).unwrap(); @@ -55,10 +54,10 @@ mod editor_config { #[test] fn can_enable_trailing_line() { - let fixture = create_sandbox("editor-config"); - let path = fixture.path().join("file.yaml"); + let sandbox = create_sandbox("editor-config"); + let path = sandbox.path().join("file.yaml"); - append_editor_config(fixture.path(), "[*.yaml]\ninsert_final_newline = true"); + append_editor_config(sandbox.path(), "[*.yaml]\ninsert_final_newline = true"); yaml::write_with_config(&path, yaml::read(&path).unwrap()).unwrap(); @@ -67,10 +66,10 @@ mod editor_config { #[test] fn can_disable_trailing_line() { - let fixture = create_sandbox("editor-config"); - let path = fixture.path().join("file.yaml"); + let sandbox = create_sandbox("editor-config"); + let path = sandbox.path().join("file.yaml"); - append_editor_config(fixture.path(), "[*.yaml]\ninsert_final_newline = false"); + append_editor_config(sandbox.path(), "[*.yaml]\ninsert_final_newline = false"); yaml::write_with_config(&path, yaml::read(&path).unwrap()).unwrap(); diff --git a/crates/core/vcs/Cargo.toml b/crates/core/vcs/Cargo.toml index 62ed74329e2..519be582b0d 100644 --- a/crates/core/vcs/Cargo.toml +++ b/crates/core/vcs/Cargo.toml @@ -15,3 +15,6 @@ rustc-hash = { workspace = true } url = "2.3.1" thiserror = { workspace = true } tokio = { workspace = true } + +[dev-dependencies] +moon_test_utils = { path = "../test-utils" } diff --git a/crates/core/vcs/tests/git_test.rs b/crates/core/vcs/tests/git_test.rs index 94278ff75c2..b62fa7ced01 100644 --- a/crates/core/vcs/tests/git_test.rs +++ b/crates/core/vcs/tests/git_test.rs @@ -1,8 +1,6 @@ use moon_config::{VcsConfig, VcsManager}; -use moon_utils::{ - string_vec, - test::{create_sandbox_with_git, run_git_command}, -}; +use moon_test_utils::create_sandbox; +use moon_utils::string_vec; use moon_vcs::{Git, Vcs}; use std::collections::BTreeMap; use std::fs; @@ -17,8 +15,10 @@ fn create_config(branch: &str) -> VcsConfig { #[tokio::test] async fn returns_local_branch() { - let fixture = create_sandbox_with_git("vcs"); - let git = Git::load(&create_config("default"), fixture.path()).unwrap(); + let sandbox = create_sandbox("vcs"); + sandbox.enable_git(); + + let git = Git::load(&create_config("default"), sandbox.path()).unwrap(); assert_eq!(git.get_local_branch().await.unwrap(), "master"); assert_ne!(git.get_local_branch_revision().await.unwrap(), ""); @@ -29,8 +29,10 @@ mod file_hashing { #[tokio::test] async fn hashes_a_list_of_files() { - let fixture = create_sandbox_with_git("vcs"); - let git = Git::load(&create_config("default"), fixture.path()).unwrap(); + let sandbox = create_sandbox("vcs"); + sandbox.enable_git(); + + let git = Git::load(&create_config("default"), sandbox.path()).unwrap(); assert_eq!( git.get_file_hashes(&string_vec!["existing.txt", "rename-me.txt"]) @@ -51,11 +53,12 @@ mod file_hashing { #[tokio::test] async fn ignores_files_when_hashing() { - let fixture = create_sandbox_with_git("vcs"); + let sandbox = create_sandbox("vcs"); + sandbox.enable_git(); - fs::write(fixture.path().join(".gitignore"), "existing.txt").unwrap(); + sandbox.create_file(".gitignore", "existing.txt"); - let git = Git::load(&create_config("default"), fixture.path()).unwrap(); + let git = Git::load(&create_config("default"), sandbox.path()).unwrap(); assert_eq!( git.get_file_hashes(&string_vec!["existing.txt", "rename-me.txt"]) @@ -70,8 +73,10 @@ mod file_hashing { #[tokio::test] async fn hashes_an_entire_folder() { - let fixture = create_sandbox_with_git("vcs"); - let git = Git::load(&create_config("default"), fixture.path()).unwrap(); + let sandbox = create_sandbox("vcs"); + sandbox.enable_git(); + + let git = Git::load(&create_config("default"), sandbox.path()).unwrap(); assert_eq!( git.get_file_tree_hashes(".").await.unwrap(), @@ -92,18 +97,16 @@ mod file_hashing { "existing.txt".to_owned(), "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391".to_owned() ), - ( - "shared-workspace.yml".to_owned(), - "b4be93368a88e7038c02969b78d024a23ebe97a5".to_owned() - ), ]) ); } #[tokio::test] async fn filters_ignored_files() { - let fixture = create_sandbox_with_git("ignore"); - let git = Git::load(&create_config("master"), fixture.path()).unwrap(); + let sandbox = create_sandbox("ignore"); + sandbox.enable_git(); + + let git = Git::load(&create_config("master"), sandbox.path()).unwrap(); assert_eq!( git.get_file_hashes(&string_vec!["foo", "bar", "dir/baz", "dir/qux"]) @@ -124,8 +127,10 @@ mod file_hashing { #[tokio::test] async fn filters_ignored_files_tree() { - let fixture = create_sandbox_with_git("ignore"); - let git = Git::load(&create_config("master"), fixture.path()).unwrap(); + let sandbox = create_sandbox("ignore"); + sandbox.enable_git(); + + let git = Git::load(&create_config("master"), sandbox.path()).unwrap(); assert_eq!( git.get_file_tree_hashes(".").await.unwrap(), @@ -141,10 +146,6 @@ mod file_hashing { ( "foo".to_owned(), "257cc5642cb1a054f08cc83f2d943e56fd3ebe99".to_owned() - ), - ( - "shared-workspace.yml".to_owned(), - "b4be93368a88e7038c02969b78d024a23ebe97a5".to_owned() ) ]) ); @@ -158,8 +159,10 @@ mod touched_files { #[tokio::test] async fn returns_defaults_when_nothing() { - let fixture = create_sandbox_with_git("vcs"); - let git = Git::load(&create_config("default"), fixture.path()).unwrap(); + let sandbox = create_sandbox("vcs"); + sandbox.enable_git(); + + let git = Git::load(&create_config("default"), sandbox.path()).unwrap(); assert_eq!( git.get_touched_files().await.unwrap(), @@ -169,10 +172,12 @@ mod touched_files { #[tokio::test] async fn handles_untracked() { - let fixture = create_sandbox_with_git("vcs"); - let git = Git::load(&create_config("default"), fixture.path()).unwrap(); + let sandbox = create_sandbox("vcs"); + sandbox.enable_git(); + + let git = Git::load(&create_config("default"), sandbox.path()).unwrap(); - fs::write(fixture.path().join("added.txt"), "").unwrap(); + sandbox.create_file("added.txt", ""); assert_eq!( git.get_touched_files().await.unwrap(), @@ -186,12 +191,14 @@ mod touched_files { #[tokio::test] async fn handles_added() { - let fixture = create_sandbox_with_git("vcs"); - let git = Git::load(&create_config("default"), fixture.path()).unwrap(); + let sandbox = create_sandbox("vcs"); + sandbox.enable_git(); - fs::write(fixture.path().join("added.txt"), "").unwrap(); + let git = Git::load(&create_config("default"), sandbox.path()).unwrap(); - run_git_command(fixture.path(), |cmd| { + sandbox.create_file("added.txt", ""); + + sandbox.run_git(|cmd| { cmd.args(["add", "added.txt"]); }); @@ -208,10 +215,12 @@ mod touched_files { #[tokio::test] async fn handles_deleted() { - let fixture = create_sandbox_with_git("vcs"); - let git = Git::load(&create_config("default"), fixture.path()).unwrap(); + let sandbox = create_sandbox("vcs"); + sandbox.enable_git(); + + let git = Git::load(&create_config("default"), sandbox.path()).unwrap(); - fs::remove_file(fixture.path().join("delete-me.txt")).unwrap(); + fs::remove_file(sandbox.path().join("delete-me.txt")).unwrap(); assert_eq!( git.get_touched_files().await.unwrap(), @@ -226,10 +235,12 @@ mod touched_files { #[tokio::test] async fn handles_modified() { - let fixture = create_sandbox_with_git("vcs"); - let git = Git::load(&create_config("default"), fixture.path()).unwrap(); + let sandbox = create_sandbox("vcs"); + sandbox.enable_git(); + + let git = Git::load(&create_config("default"), sandbox.path()).unwrap(); - fs::write(fixture.path().join("existing.txt"), "modified").unwrap(); + sandbox.create_file("existing.txt", "modified"); assert_eq!( git.get_touched_files().await.unwrap(), @@ -244,12 +255,14 @@ mod touched_files { #[tokio::test] async fn handles_renamed() { - let fixture = create_sandbox_with_git("vcs"); - let git = Git::load(&create_config("default"), fixture.path()).unwrap(); + let sandbox = create_sandbox("vcs"); + sandbox.enable_git(); + + let git = Git::load(&create_config("default"), sandbox.path()).unwrap(); fs::rename( - fixture.path().join("rename-me.txt"), - fixture.path().join("renamed.txt"), + sandbox.path().join("rename-me.txt"), + sandbox.path().join("renamed.txt"), ) .unwrap(); @@ -273,10 +286,12 @@ mod touched_files_via_diff { #[tokio::test] async fn returns_defaults_when_nothing() { - let fixture = create_sandbox_with_git("vcs"); - let git = Git::load(&create_config("default"), fixture.path()).unwrap(); + let sandbox = create_sandbox("vcs"); + sandbox.enable_git(); + + let git = Git::load(&create_config("default"), sandbox.path()).unwrap(); - run_git_command(fixture.path(), |cmd| { + sandbox.run_git(|cmd| { cmd.args(["checkout", "-b", "current"]); }); @@ -290,14 +305,16 @@ mod touched_files_via_diff { #[tokio::test] async fn handles_untracked() { - let fixture = create_sandbox_with_git("vcs"); - let git = Git::load(&create_config("default"), fixture.path()).unwrap(); + let sandbox = create_sandbox("vcs"); + sandbox.enable_git(); - run_git_command(fixture.path(), |cmd| { + let git = Git::load(&create_config("default"), sandbox.path()).unwrap(); + + sandbox.run_git(|cmd| { cmd.args(["checkout", "-b", "current"]); }); - fs::write(fixture.path().join("added.txt"), "").unwrap(); + sandbox.create_file("added.txt", ""); assert_eq!( git.get_touched_files_between_revisions("master", "current") @@ -310,16 +327,18 @@ mod touched_files_via_diff { #[tokio::test] async fn handles_added() { - let fixture = create_sandbox_with_git("vcs"); - let git = Git::load(&create_config("default"), fixture.path()).unwrap(); + let sandbox = create_sandbox("vcs"); + sandbox.enable_git(); + + let git = Git::load(&create_config("default"), sandbox.path()).unwrap(); - run_git_command(fixture.path(), |cmd| { + sandbox.run_git(|cmd| { cmd.args(["checkout", "-b", "current"]); }); - fs::write(fixture.path().join("added.txt"), "").unwrap(); + sandbox.create_file("added.txt", ""); - run_git_command(fixture.path(), |cmd| { + sandbox.run_git(|cmd| { cmd.args(["add", "added.txt"]); }); @@ -338,14 +357,16 @@ mod touched_files_via_diff { #[tokio::test] async fn handles_deleted() { - let fixture = create_sandbox_with_git("vcs"); - let git = Git::load(&create_config("default"), fixture.path()).unwrap(); + let sandbox = create_sandbox("vcs"); + sandbox.enable_git(); + + let git = Git::load(&create_config("default"), sandbox.path()).unwrap(); - run_git_command(fixture.path(), |cmd| { + sandbox.run_git(|cmd| { cmd.args(["checkout", "-b", "current"]); }); - fs::remove_file(fixture.path().join("delete-me.txt")).unwrap(); + fs::remove_file(sandbox.path().join("delete-me.txt")).unwrap(); assert_eq!( git.get_touched_files_between_revisions("master", "current") @@ -362,14 +383,16 @@ mod touched_files_via_diff { #[tokio::test] async fn handles_modified() { - let fixture = create_sandbox_with_git("vcs"); - let git = Git::load(&create_config("default"), fixture.path()).unwrap(); + let sandbox = create_sandbox("vcs"); + sandbox.enable_git(); - run_git_command(fixture.path(), |cmd| { + let git = Git::load(&create_config("default"), sandbox.path()).unwrap(); + + sandbox.run_git(|cmd| { cmd.args(["checkout", "-b", "current"]); }); - fs::write(fixture.path().join("existing.txt"), "modified").unwrap(); + sandbox.create_file("existing.txt", "modified"); assert_eq!( git.get_touched_files_between_revisions("master", "current") @@ -386,16 +409,18 @@ mod touched_files_via_diff { #[tokio::test] async fn handles_renamed() { - let fixture = create_sandbox_with_git("vcs"); - let git = Git::load(&create_config("default"), fixture.path()).unwrap(); + let sandbox = create_sandbox("vcs"); + sandbox.enable_git(); + + let git = Git::load(&create_config("default"), sandbox.path()).unwrap(); - run_git_command(fixture.path(), |cmd| { + sandbox.run_git(|cmd| { cmd.args(["checkout", "-b", "current"]); }); fs::rename( - fixture.path().join("rename-me.txt"), - fixture.path().join("renamed.txt"), + sandbox.path().join("rename-me.txt"), + sandbox.path().join("renamed.txt"), ) .unwrap(); diff --git a/crates/node/lang/Cargo.toml b/crates/node/lang/Cargo.toml index 8dd5f7cc5e4..1f3f8cda570 100644 --- a/crates/node/lang/Cargo.toml +++ b/crates/node/lang/Cargo.toml @@ -20,8 +20,6 @@ thiserror = { workspace = true } yarn-lock-parser = "0.4.1" [dev-dependencies] -assert_fs = "1.0.9" -pretty_assertions = "1.3.0" +moon_test_utils = { path = "../../core/test-utils" } reqwest = { workspace = true, features = ["blocking"] } -serial_test = "0.9.0" -tokio = { workspace = true, features = ["test-util"] } +tokio = { workspace = true } diff --git a/crates/node/lang/src/node.rs b/crates/node/lang/src/node.rs index 7d879603907..3341ddc4c2e 100644 --- a/crates/node/lang/src/node.rs +++ b/crates/node/lang/src/node.rs @@ -215,8 +215,7 @@ pub fn parse_package_name(package_name: &str) -> (Option, String) { #[cfg(test)] mod tests { use super::*; - use assert_fs::prelude::*; - use assert_fs::TempDir; + use moon_test_utils::{assert_fs::prelude::*, create_temp_dir, TempDir}; fn create_cmd(path: &str) -> String { format!( @@ -233,7 +232,7 @@ mod tests { } fn create_node_modules_sandbox() -> TempDir { - let sandbox = TempDir::new().unwrap(); + let sandbox = create_temp_dir(); sandbox .child("node_modules/@scope/pkg-foo/package.json") diff --git a/crates/node/lang/src/npm/mod.rs b/crates/node/lang/src/npm/mod.rs index b85ba16a59d..bec3c17c2be 100644 --- a/crates/node/lang/src/npm/mod.rs +++ b/crates/node/lang/src/npm/mod.rs @@ -66,13 +66,12 @@ pub fn load_lockfile_dependencies(path: PathBuf) -> Result Result<(), MoonEr #[cfg(test)] mod test { use super::*; - use assert_fs::prelude::*; + use moon_test_utils::{assert_fs::prelude::*, create_temp_dir}; #[test] fn preserves_order_when_de_to_ser() { let json = r#"{"name": "hello", "description": "world", "private": true}"#; - let dir = assert_fs::TempDir::new().unwrap(); + let dir = create_temp_dir(); let file = dir.child("package.json"); file.write_str(json).unwrap(); diff --git a/crates/node/lang/src/pnpm/mod.rs b/crates/node/lang/src/pnpm/mod.rs index 24c3668e1e2..ccb8ebf88c3 100644 --- a/crates/node/lang/src/pnpm/mod.rs +++ b/crates/node/lang/src/pnpm/mod.rs @@ -78,14 +78,13 @@ pub fn load_lockfile_dependencies(path: PathBuf) -> Result ProjectGraph { - let workspace_root = get_fixtures_dir("project-graph/aliases"); - let workspace_config = WorkspaceConfig { - projects: WorkspaceProjects::Sources(FxHashMap::from_iter([ - ("noLang".to_owned(), "no-lang".to_owned()), - ("nodeNameOnly".to_owned(), "node-name-only".to_owned()), - ("nodeNameScope".to_owned(), "node-name-scope".to_owned()), - ])), - ..WorkspaceConfig::default() - }; - let toolchain_config = ToolchainConfig { - node: Some(node_config), - ..ToolchainConfig::default() - }; +async fn get_aliases_graph(node_config: NodeConfig) -> (ProjectGraph, Sandbox) { + let (workspace_config, mut toolchain_config, projects_config) = + get_project_graph_aliases_fixture_configs(); + + toolchain_config.node = Some(node_config); + + let sandbox = create_sandbox_with_config( + "project-graph/aliases", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); let mut graph = ProjectGraph::generate( - &workspace_root, + sandbox.path(), &workspace_config, &toolchain_config, - GlobalProjectConfig::default(), - &CacheEngine::load(&workspace_root).await.unwrap(), + projects_config, + &CacheEngine::load(sandbox.path()).await.unwrap(), ) .await .unwrap(); @@ -39,12 +35,12 @@ async fn get_aliases_graph(node_config: NodeConfig) -> ProjectGraph { .register_platform(Box::new(NodePlatform::default())) .unwrap(); - graph + (graph, sandbox) } #[tokio::test] async fn loads_node_aliases_name_only() { - let graph = get_aliases_graph(NodeConfig { + let (graph, _sandbox) = get_aliases_graph(NodeConfig { alias_package_names: Some(NodeProjectAliasFormat::NameOnly), ..NodeConfig::default() }) @@ -61,7 +57,7 @@ async fn loads_node_aliases_name_only() { #[tokio::test] async fn loads_node_aliases_name_scopes() { - let graph = get_aliases_graph(NodeConfig { + let (graph, _sandbox) = get_aliases_graph(NodeConfig { alias_package_names: Some(NodeProjectAliasFormat::NameAndScope), ..NodeConfig::default() }) @@ -78,7 +74,7 @@ async fn loads_node_aliases_name_scopes() { #[tokio::test] async fn returns_project_using_alias() { - let graph = get_aliases_graph(NodeConfig { + let (graph, _sandbox) = get_aliases_graph(NodeConfig { alias_package_names: Some(NodeProjectAliasFormat::NameAndScope), ..NodeConfig::default() }) @@ -92,8 +88,9 @@ async fn returns_project_using_alias() { #[tokio::test] async fn graph_uses_id_for_nodes() { - let graph = get_aliases_graph(NodeConfig { + let (graph, _sandbox) = get_aliases_graph(NodeConfig { alias_package_names: Some(NodeProjectAliasFormat::NameAndScope), + version: "18.0.0".into(), ..NodeConfig::default() }) .await; diff --git a/crates/node/platform/tests/sync_project_test.rs b/crates/node/platform/tests/sync_project_test.rs new file mode 100644 index 00000000000..b128129eb75 --- /dev/null +++ b/crates/node/platform/tests/sync_project_test.rs @@ -0,0 +1,121 @@ +use moon_config::{GlobalProjectConfig, TypeScriptConfig}; +use moon_node_platform::actions::create_missing_tsconfig; +use moon_project::Project; +use moon_test_utils::{create_sandbox_with_config, get_node_fixture_configs}; +use moon_typescript_lang::TsConfigJson; +use moon_utils::string_vec; + +mod missing_tsconfig { + use super::*; + + #[tokio::test] + async fn creates_tsconfig() { + let (workspace_config, toolchain_config, projects_config) = get_node_fixture_configs(); + let sandbox = create_sandbox_with_config( + "node", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + let project = Project::new( + "deps-a", + "deps-a", + sandbox.path(), + &GlobalProjectConfig::default(), + ) + .unwrap(); + + let tsconfig_path = project.root.join("tsconfig.json"); + + assert!(!tsconfig_path.exists()); + + create_missing_tsconfig(&project, &TypeScriptConfig::default(), sandbox.path()) + .await + .unwrap(); + + assert!(tsconfig_path.exists()); + + let tsconfig = TsConfigJson::read(tsconfig_path).unwrap().unwrap(); + + assert_eq!( + tsconfig.extends, + Some("../tsconfig.options.json".to_owned()) + ); + assert_eq!(tsconfig.include, Some(string_vec!["**/*"])); + } + + #[tokio::test] + async fn creates_tsconfig_with_custom_settings() { + let (workspace_config, toolchain_config, projects_config) = get_node_fixture_configs(); + let sandbox = create_sandbox_with_config( + "node", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + let project = Project::new( + "deps-a", + "deps-a", + sandbox.path(), + &GlobalProjectConfig::default(), + ) + .unwrap(); + + let tsconfig_path = project.root.join("tsconfig.ref.json"); + + assert!(!tsconfig_path.exists()); + + create_missing_tsconfig( + &project, + &TypeScriptConfig { + project_config_file_name: "tsconfig.ref.json".to_string(), + root_options_config_file_name: "tsconfig.base.json".to_string(), + ..TypeScriptConfig::default() + }, + sandbox.path(), + ) + .await + .unwrap(); + + assert!(tsconfig_path.exists()); + + let tsconfig = TsConfigJson::read_with_name(&project.root, "tsconfig.ref.json") + .unwrap() + .unwrap(); + + assert_eq!(tsconfig.extends, Some("../tsconfig.base.json".to_owned())); + assert_eq!(tsconfig.include, Some(string_vec!["**/*"])); + } + + #[tokio::test] + async fn doesnt_create_if_a_config_exists() { + let (workspace_config, toolchain_config, projects_config) = get_node_fixture_configs(); + let sandbox = create_sandbox_with_config( + "node", + Some(&workspace_config), + Some(&toolchain_config), + Some(&projects_config), + ); + + let project = Project::new( + "deps-b", + "deps-b", + sandbox.path(), + &GlobalProjectConfig::default(), + ) + .unwrap(); + + let tsconfig_path = project.root.join("tsconfig.json"); + + assert!(tsconfig_path.exists()); + + let created = + create_missing_tsconfig(&project, &TypeScriptConfig::default(), sandbox.path()) + .await + .unwrap(); + + assert!(!created); + } +} diff --git a/crates/node/platform/tests/task_test.rs b/crates/node/platform/tests/task_test.rs index 69d1e2d30bd..ac8bb406c60 100644 --- a/crates/node/platform/tests/task_test.rs +++ b/crates/node/platform/tests/task_test.rs @@ -529,7 +529,7 @@ mod infer_tasks_from_scripts { mod create_tasks_from_scripts { use super::*; - use pretty_assertions::assert_eq; + use moon_test_utils::pretty_assertions::assert_eq; #[test] fn ignores_unsupported_syntax() { @@ -633,7 +633,7 @@ mod create_tasks_from_scripts { mod pre_post { use super::*; - use pretty_assertions::assert_eq; + use moon_test_utils::pretty_assertions::assert_eq; #[test] fn creates_pre_and_post() { @@ -828,7 +828,7 @@ mod create_tasks_from_scripts { mod pm_run { use super::*; - use pretty_assertions::assert_eq; + use moon_test_utils::pretty_assertions::assert_eq; #[test] fn skips_when_pointing_to_an_unknown() { @@ -1064,7 +1064,7 @@ mod create_tasks_from_scripts { mod life_cycle { use super::*; - use pretty_assertions::assert_eq; + use moon_test_utils::pretty_assertions::assert_eq; #[test] fn rewrites_run_commands() { @@ -1134,7 +1134,7 @@ mod create_tasks_from_scripts { mod complex_examples { use super::*; - use pretty_assertions::assert_eq; + use moon_test_utils::pretty_assertions::assert_eq; // https://github.com/babel/babel/blob/main/package.json #[test] diff --git a/crates/proto/node/tests/node_depman_test.rs b/crates/proto/node/tests/node_depman_test.rs index 9895d167f85..a9fecfc6d49 100644 --- a/crates/proto/node/tests/node_depman_test.rs +++ b/crates/proto/node/tests/node_depman_test.rs @@ -71,9 +71,9 @@ async fn downloads_verifies_installs_yarn_berry() { let fixture = assert_fs::TempDir::new().unwrap(); let proto = create_proto(fixture.path()); let mut tool = - NodeDependencyManager::new(&proto, NodeDependencyManagerType::Yarn, Some("3.0.0")); + NodeDependencyManager::new(&proto, NodeDependencyManagerType::Yarn, Some("3.3.0")); - tool.setup("3.0.0").await.unwrap(); + tool.setup("3.3.0").await.unwrap(); assert!(!tool.get_download_path().unwrap().exists()); assert!(!tool.get_checksum_path().unwrap().exists()); diff --git a/crates/typescript/lang/Cargo.toml b/crates/typescript/lang/Cargo.toml index 353fb6ada7b..fcd2c290acb 100644 --- a/crates/typescript/lang/Cargo.toml +++ b/crates/typescript/lang/Cargo.toml @@ -13,4 +13,5 @@ serde = { workspace = true } serde_json = { workspace = true, features = ["preserve_order"] } [dev-dependencies] -tokio = { workspace = true, features = ["test-util"] } +moon_test_utils = { path = "../../core/test-utils" } +tokio = { workspace = true } diff --git a/crates/typescript/lang/src/tsconfig.rs b/crates/typescript/lang/src/tsconfig.rs index 04c51122d09..8c5067242f4 100644 --- a/crates/typescript/lang/src/tsconfig.rs +++ b/crates/typescript/lang/src/tsconfig.rs @@ -773,8 +773,8 @@ fn write_preserved_json(path: &Path, tsconfig: &TsConfigJson) -> Result<(), Moon #[cfg(test)] mod test { use super::*; + use moon_test_utils::get_fixtures_path; use moon_utils::string_vec; - use moon_utils::test::get_fixtures_dir; #[test] fn serializes_special_fields() { @@ -893,7 +893,7 @@ mod test { #[tokio::test] async fn parse_basic_file() { - let path = get_fixtures_dir("base/tsconfig-json"); + let path = get_fixtures_path("base/tsconfig-json"); let config = TsConfigJson::read_with_name(path, "tsconfig.default.json") .unwrap() .unwrap(); @@ -911,7 +911,7 @@ mod test { #[tokio::test] async fn parse_inheriting_file() { - let path = get_fixtures_dir("base/tsconfig-json/tsconfig.inherits.json"); + let path = get_fixtures_path("base/tsconfig-json/tsconfig.inherits.json"); let config = TsConfigJson::load_with_extends(&path).await.unwrap(); assert_eq!( @@ -936,7 +936,7 @@ mod test { #[tokio::test] async fn parse_inheritance_chain() { - let path = get_fixtures_dir("base/tsconfig-json/a/tsconfig.json"); + let path = get_fixtures_path("base/tsconfig-json/a/tsconfig.json"); let config = TsConfigJson::load_with_extends(&path).await.unwrap(); assert_eq!( diff --git a/tests/fixtures/base/.moon/project.yml b/tests/fixtures/base/.moon/project.yml deleted file mode 100644 index 591a548ac52..00000000000 --- a/tests/fixtures/base/.moon/project.yml +++ /dev/null @@ -1 +0,0 @@ -fileGroups: {} diff --git a/tests/fixtures/base/.moon/workspace.yml b/tests/fixtures/base/.moon/workspace.yml deleted file mode 100644 index c18f7200c5d..00000000000 --- a/tests/fixtures/base/.moon/workspace.yml +++ /dev/null @@ -1,3 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: {} diff --git a/tests/fixtures/cases/.moon/toolchain.yml b/tests/fixtures/cases/.moon/toolchain.yml deleted file mode 100644 index c70ba1a1a8b..00000000000 --- a/tests/fixtures/cases/.moon/toolchain.yml +++ /dev/null @@ -1,7 +0,0 @@ -extends: '../shared-toolchain.yml' - -node: - version: '16.0.0' - addEnginesConstraint: false - dedupeOnLockfileChange: false - syncProjectWorkspaceDependencies: false diff --git a/tests/fixtures/cases/.moon/workspace.yml b/tests/fixtures/cases/.moon/workspace.yml deleted file mode 100644 index 3f8146e3574..00000000000 --- a/tests/fixtures/cases/.moon/workspace.yml +++ /dev/null @@ -1,23 +0,0 @@ -projects: - root: '.' - base: 'base' - noop: 'noop' - files: 'files' - - # Project/task deps - depsA: 'deps-a' - depsB: 'deps-b' - depsC: 'deps-c' - dependsOn: 'depends-on' - - # Target scopes - targetScopeA: 'target-scope-a' - targetScopeB: 'target-scope-b' - targetScopeC: 'target-scope-c' - - # Outputs - outputs: 'outputs' - outputStyles: 'output-styles' - - # Runner - passthroughArgs: 'passthrough-args' diff --git a/tests/fixtures/config-extends/toolchain/base-1.yml b/tests/fixtures/config-extends/toolchain/base-1.yml index 6f4176d6dda..353749b4fff 100644 --- a/tests/fixtures/config-extends/toolchain/base-1.yml +++ b/tests/fixtures/config-extends/toolchain/base-1.yml @@ -3,4 +3,4 @@ extends: './base-0.yml' node: packageManager: 'yarn' yarn: - version: '3.0.0' + version: '3.3.0' diff --git a/tests/fixtures/config-invalid-global-project/.moon/project.yml b/tests/fixtures/config-invalid-global-project/.moon/project.yml deleted file mode 100644 index 4a4d97d9a6b..00000000000 --- a/tests/fixtures/config-invalid-global-project/.moon/project.yml +++ /dev/null @@ -1 +0,0 @@ -tasks: 123 diff --git a/tests/fixtures/config-invalid-global-project/.moon/workspace.yml b/tests/fixtures/config-invalid-global-project/.moon/workspace.yml deleted file mode 100644 index c18f7200c5d..00000000000 --- a/tests/fixtures/config-invalid-global-project/.moon/workspace.yml +++ /dev/null @@ -1,3 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: {} diff --git a/tests/fixtures/config-invalid-project/.moon/workspace.yml b/tests/fixtures/config-invalid-project/.moon/workspace.yml deleted file mode 100644 index 7382be4356d..00000000000 --- a/tests/fixtures/config-invalid-project/.moon/workspace.yml +++ /dev/null @@ -1,4 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: - test: test diff --git a/tests/fixtures/config-invalid-project/package.json b/tests/fixtures/config-invalid-project/package.json deleted file mode 100644 index 534a7dc0956..00000000000 --- a/tests/fixtures/config-invalid-project/package.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "name": "test-config-invalid-project", - "private": true, - "workspaces": [ - "*" - ] -} diff --git a/tests/fixtures/config-invalid-project/test/moon.yml b/tests/fixtures/config-invalid-project/test/moon.yml deleted file mode 100644 index 05f40183429..00000000000 --- a/tests/fixtures/config-invalid-project/test/moon.yml +++ /dev/null @@ -1,2 +0,0 @@ -project: - type: library diff --git a/tests/fixtures/config-invalid-workspace/.moon/workspace.yml b/tests/fixtures/config-invalid-workspace/.moon/workspace.yml deleted file mode 100644 index 23fb678d41c..00000000000 --- a/tests/fixtures/config-invalid-workspace/.moon/workspace.yml +++ /dev/null @@ -1,3 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: true diff --git a/tests/fixtures/generator/.moon/workspace.yml b/tests/fixtures/generator/.moon/workspace.yml deleted file mode 100644 index 27529a2dada..00000000000 --- a/tests/fixtures/generator/.moon/workspace.yml +++ /dev/null @@ -1 +0,0 @@ -extends: '../shared-workspace.yml' diff --git a/tests/fixtures/migrate/.moon/toolchain.yml b/tests/fixtures/migrate/.moon/toolchain.yml deleted file mode 100644 index dc3814fe6d9..00000000000 --- a/tests/fixtures/migrate/.moon/toolchain.yml +++ /dev/null @@ -1 +0,0 @@ -extends: '../shared-toolchain.yml' diff --git a/tests/fixtures/migrate/.moon/workspace.yml b/tests/fixtures/migrate/.moon/workspace.yml deleted file mode 100644 index be890e39ec2..00000000000 --- a/tests/fixtures/migrate/.moon/workspace.yml +++ /dev/null @@ -1,4 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: - - 'package-json/*' diff --git a/tests/fixtures/node-non-workspaces/.moon/project.yml b/tests/fixtures/node-non-workspaces/.moon/project.yml deleted file mode 100644 index 30c8caac912..00000000000 --- a/tests/fixtures/node-non-workspaces/.moon/project.yml +++ /dev/null @@ -1,3 +0,0 @@ -tasks: - noop: - command: noop diff --git a/tests/fixtures/node-non-workspaces/.moon/toolchain.yml b/tests/fixtures/node-non-workspaces/.moon/toolchain.yml deleted file mode 100644 index dc3814fe6d9..00000000000 --- a/tests/fixtures/node-non-workspaces/.moon/toolchain.yml +++ /dev/null @@ -1 +0,0 @@ -extends: '../shared-toolchain.yml' diff --git a/tests/fixtures/node-non-workspaces/.moon/workspace.yml b/tests/fixtures/node-non-workspaces/.moon/workspace.yml deleted file mode 100644 index 45222fca493..00000000000 --- a/tests/fixtures/node-non-workspaces/.moon/workspace.yml +++ /dev/null @@ -1,4 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: - - '*' diff --git a/tests/fixtures/node-npm/.moon/project.yml b/tests/fixtures/node-npm/.moon/project.yml deleted file mode 100644 index 30c8caac912..00000000000 --- a/tests/fixtures/node-npm/.moon/project.yml +++ /dev/null @@ -1,3 +0,0 @@ -tasks: - noop: - command: noop diff --git a/tests/fixtures/node-npm/.moon/toolchain.yml b/tests/fixtures/node-npm/.moon/toolchain.yml deleted file mode 100644 index f36b4a5795d..00000000000 --- a/tests/fixtures/node-npm/.moon/toolchain.yml +++ /dev/null @@ -1,9 +0,0 @@ -extends: '../shared-toolchain.yml' - -node: - # Use a unique version as to not collide with other tests - version: '16.1.0' - packageManager: 'npm' - npm: - version: '8.0.0' - addEnginesConstraint: false diff --git a/tests/fixtures/node-npm/.moon/workspace.yml b/tests/fixtures/node-npm/.moon/workspace.yml deleted file mode 100644 index ae18d35f4c0..00000000000 --- a/tests/fixtures/node-npm/.moon/workspace.yml +++ /dev/null @@ -1,6 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: - npm: base - other: other - notInWorkspace: not-in-workspace diff --git a/tests/fixtures/node-npm/base/moon.yml b/tests/fixtures/node-npm/base/moon.yml index 222c33da6b3..d807f02db6f 100644 --- a/tests/fixtures/node-npm/base/moon.yml +++ b/tests/fixtures/node-npm/base/moon.yml @@ -3,7 +3,6 @@ language: javascript tasks: version: command: npm - args: --version installDep: command: npm args: install react@17.0.0 diff --git a/tests/fixtures/node-pnpm/.moon/project.yml b/tests/fixtures/node-pnpm/.moon/project.yml deleted file mode 100644 index 30c8caac912..00000000000 --- a/tests/fixtures/node-pnpm/.moon/project.yml +++ /dev/null @@ -1,3 +0,0 @@ -tasks: - noop: - command: noop diff --git a/tests/fixtures/node-pnpm/.moon/toolchain.yml b/tests/fixtures/node-pnpm/.moon/toolchain.yml deleted file mode 100644 index 83e249bdd1c..00000000000 --- a/tests/fixtures/node-pnpm/.moon/toolchain.yml +++ /dev/null @@ -1,9 +0,0 @@ -extends: '../shared-toolchain.yml' - -node: - # Use a unique version as to not collide with other tests - version: '16.2.0' - packageManager: 'pnpm' - pnpm: - version: '7.5.0' - addEnginesConstraint: false diff --git a/tests/fixtures/node-pnpm/.moon/workspace.yml b/tests/fixtures/node-pnpm/.moon/workspace.yml deleted file mode 100644 index 10757b49286..00000000000 --- a/tests/fixtures/node-pnpm/.moon/workspace.yml +++ /dev/null @@ -1,6 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: - pnpm: base - other: other - notInWorkspace: not-in-workspace diff --git a/tests/fixtures/node-pnpm/base/moon.yml b/tests/fixtures/node-pnpm/base/moon.yml index 0110da9252f..ec8542df13d 100644 --- a/tests/fixtures/node-pnpm/base/moon.yml +++ b/tests/fixtures/node-pnpm/base/moon.yml @@ -3,7 +3,6 @@ language: javascript tasks: version: command: pnpm - args: --version installDep: command: pnpm args: add react@17.0.0 diff --git a/tests/fixtures/node-yarn/.moon/project.yml b/tests/fixtures/node-yarn/.moon/project.yml deleted file mode 100644 index 30c8caac912..00000000000 --- a/tests/fixtures/node-yarn/.moon/project.yml +++ /dev/null @@ -1,3 +0,0 @@ -tasks: - noop: - command: noop diff --git a/tests/fixtures/node-yarn/.moon/toolchain.yml b/tests/fixtures/node-yarn/.moon/toolchain.yml deleted file mode 100644 index d05867fd033..00000000000 --- a/tests/fixtures/node-yarn/.moon/toolchain.yml +++ /dev/null @@ -1,11 +0,0 @@ -extends: '../shared-toolchain.yml' - -node: - # Use a unique version as to not collide with other tests because of corepack - version: '16.4.0' - packageManager: 'yarn' - yarn: - version: '3.0.0' - plugins: - - 'workspace-tools' - addEnginesConstraint: false diff --git a/tests/fixtures/node-yarn/.moon/workspace.yml b/tests/fixtures/node-yarn/.moon/workspace.yml deleted file mode 100644 index 586b9f49572..00000000000 --- a/tests/fixtures/node-yarn/.moon/workspace.yml +++ /dev/null @@ -1,6 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: - yarn: base - other: other - notInWorkspace: not-in-workspace diff --git a/tests/fixtures/node-yarn/base/moon.yml b/tests/fixtures/node-yarn/base/moon.yml index a0643c5df3c..2e6ba57cb08 100644 --- a/tests/fixtures/node-yarn/base/moon.yml +++ b/tests/fixtures/node-yarn/base/moon.yml @@ -3,7 +3,6 @@ language: javascript tasks: version: command: yarn - args: --version installDep: command: yarn args: add react@17.0.0 diff --git a/tests/fixtures/node-yarn/not-in-workspace/yarn.lock b/tests/fixtures/node-yarn/not-in-workspace/yarn.lock index 662075e382c..235ebc2f673 100644 --- a/tests/fixtures/node-yarn/not-in-workspace/yarn.lock +++ b/tests/fixtures/node-yarn/not-in-workspace/yarn.lock @@ -2,7 +2,8 @@ # Manual changes might be lost - proceed with caution! __metadata: - version: 4 + version: 6 + cacheKey: 8 "test-node-yarn-not-in-workspace@workspace:.": version: 0.0.0-use.local @@ -18,14 +19,16 @@ typescript@4.8.4: bin: tsc: bin/tsc tsserver: bin/tsserver + checksum: 3e4f061658e0c8f36c820802fa809e0fd812b85687a9a2f5430bc3d0368e37d1c9605c3ce9b39df9a05af2ece67b1d844f9f6ea8ff42819f13bcb80f85629af0 languageName: node linkType: hard "typescript@patch:typescript@4.8.4#~builtin": version: 4.8.4 - resolution: "typescript@patch:typescript@npm%3A4.8.4#~builtin::version=4.8.4&hash=d8b4e7" + resolution: "typescript@patch:typescript@npm%3A4.8.4#~builtin::version=4.8.4&hash=0102e9" bin: tsc: bin/tsc tsserver: bin/tsserver + checksum: 301459fc3eb3b1a38fe91bf96d98eb55da88a9cb17b4ef80b4d105d620f4d547ba776cc27b44cc2ef58b66eda23fe0a74142feb5e79a6fb99f54fc018a696afa languageName: node linkType: hard diff --git a/tests/fixtures/node-yarn/yarn.lock b/tests/fixtures/node-yarn/yarn.lock index af2855f5a5e..1e451741373 100644 --- a/tests/fixtures/node-yarn/yarn.lock +++ b/tests/fixtures/node-yarn/yarn.lock @@ -2,7 +2,7 @@ # Manual changes might be lost - proceed with caution! __metadata: - version: 4 + version: 6 cacheKey: 8 "@babel/helper-module-imports@npm:7.16.0": diff --git a/tests/fixtures/node-yarn1/.moon/project.yml b/tests/fixtures/node-yarn1/.moon/project.yml deleted file mode 100644 index 30c8caac912..00000000000 --- a/tests/fixtures/node-yarn1/.moon/project.yml +++ /dev/null @@ -1,3 +0,0 @@ -tasks: - noop: - command: noop diff --git a/tests/fixtures/node-yarn1/.moon/toolchain.yml b/tests/fixtures/node-yarn1/.moon/toolchain.yml deleted file mode 100644 index 94761d22092..00000000000 --- a/tests/fixtures/node-yarn1/.moon/toolchain.yml +++ /dev/null @@ -1,9 +0,0 @@ -extends: '../shared-toolchain.yml' - -node: - # Use a unique version as to not collide with other tests because of corepack - version: '16.3.0' - packageManager: 'yarn' - yarn: - version: '1.22.0' - addEnginesConstraint: false diff --git a/tests/fixtures/node-yarn1/.moon/workspace.yml b/tests/fixtures/node-yarn1/.moon/workspace.yml deleted file mode 100644 index 586b9f49572..00000000000 --- a/tests/fixtures/node-yarn1/.moon/workspace.yml +++ /dev/null @@ -1,6 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: - yarn: base - other: other - notInWorkspace: not-in-workspace diff --git a/tests/fixtures/node/.moon/project.yml b/tests/fixtures/node/.moon/project.yml deleted file mode 100644 index 744b4e94f56..00000000000 --- a/tests/fixtures/node/.moon/project.yml +++ /dev/null @@ -1,3 +0,0 @@ -tasks: - version: - command: node --version diff --git a/tests/fixtures/node/.moon/toolchain.yml b/tests/fixtures/node/.moon/toolchain.yml deleted file mode 100644 index cf9a55b27be..00000000000 --- a/tests/fixtures/node/.moon/toolchain.yml +++ /dev/null @@ -1,12 +0,0 @@ -extends: '../shared-toolchain.yml' - -typescript: - syncProjectReferences: false - -# Put at the bottom so we can append settings to test -node: - # Use a unique version as to not collide with other tests - version: '16.1.0' - addEnginesConstraint: false - dedupeOnLockfileChange: false - syncProjectWorkspaceDependencies: false diff --git a/tests/fixtures/node/.moon/workspace.yml b/tests/fixtures/node/.moon/workspace.yml deleted file mode 100644 index 59b227c5a98..00000000000 --- a/tests/fixtures/node/.moon/workspace.yml +++ /dev/null @@ -1,16 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: - node: base - esbuild: esbuild - lifecycles: lifecycles - swc: swc - versionOverride: version-override - - # Project/task deps - depsA: 'deps-a' - depsB: 'deps-b' - depsC: 'deps-c' - depsD: 'deps-d' - dependsOn: 'depends-on' - dependsOnScopes: 'depends-on-scopes' diff --git a/tests/fixtures/node/version-override/moon.yml b/tests/fixtures/node/version-override/moon.yml index 908357930e4..e936a476144 100644 --- a/tests/fixtures/node/version-override/moon.yml +++ b/tests/fixtures/node/version-override/moon.yml @@ -2,4 +2,4 @@ language: javascript toolchain: node: - version: '18.0.0' + version: '19.0.0' diff --git a/tests/fixtures/project-graph/aliases/.moon/toolchain.yml b/tests/fixtures/project-graph/aliases/.moon/toolchain.yml deleted file mode 100644 index f2a1c180997..00000000000 --- a/tests/fixtures/project-graph/aliases/.moon/toolchain.yml +++ /dev/null @@ -1,4 +0,0 @@ -extends: '../shared-toolchain.yml' - -node: - aliasPackageNames: 'name-and-scope' diff --git a/tests/fixtures/project-graph/aliases/.moon/workspace.yml b/tests/fixtures/project-graph/aliases/.moon/workspace.yml deleted file mode 100644 index 8e05f1f6fc1..00000000000 --- a/tests/fixtures/project-graph/aliases/.moon/workspace.yml +++ /dev/null @@ -1,10 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: - noLang: no-lang - node: node - nodeNameOnly: node-name-only - nodeNameScope: node-name-scope - explicit: explicit - explicitAndImplicit: explicit-and-implicit - implicit: implicit diff --git a/tests/fixtures/project-graph/dependencies/.moon/workspace.yml b/tests/fixtures/project-graph/dependencies/.moon/workspace.yml deleted file mode 100644 index e79746ae40f..00000000000 --- a/tests/fixtures/project-graph/dependencies/.moon/workspace.yml +++ /dev/null @@ -1,7 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: - a: a - b: b - c: c - d: d diff --git a/tests/fixtures/project-graph/dependents/.moon/workspace.yml b/tests/fixtures/project-graph/dependents/.moon/workspace.yml deleted file mode 100644 index e79746ae40f..00000000000 --- a/tests/fixtures/project-graph/dependents/.moon/workspace.yml +++ /dev/null @@ -1,7 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: - a: a - b: b - c: c - d: d diff --git a/tests/fixtures/project-graph/ids/.moon/workspace.yml b/tests/fixtures/project-graph/ids/.moon/workspace.yml deleted file mode 100644 index 45222fca493..00000000000 --- a/tests/fixtures/project-graph/ids/.moon/workspace.yml +++ /dev/null @@ -1,4 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: - - '*' diff --git a/tests/fixtures/projects/.moon/project.yml b/tests/fixtures/projects/.moon/project.yml deleted file mode 100644 index 76402318074..00000000000 --- a/tests/fixtures/projects/.moon/project.yml +++ /dev/null @@ -1,6 +0,0 @@ -fileGroups: - sources: - - 'src/**/*' - - 'types/**/*' - tests: - - 'tests/**/*' diff --git a/tests/fixtures/projects/.moon/toolchain.yml b/tests/fixtures/projects/.moon/toolchain.yml deleted file mode 100644 index dc3814fe6d9..00000000000 --- a/tests/fixtures/projects/.moon/toolchain.yml +++ /dev/null @@ -1 +0,0 @@ -extends: '../shared-toolchain.yml' diff --git a/tests/fixtures/projects/.moon/workspace.yml b/tests/fixtures/projects/.moon/workspace.yml deleted file mode 100644 index 561245f2057..00000000000 --- a/tests/fixtures/projects/.moon/workspace.yml +++ /dev/null @@ -1,21 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: - advanced: advanced - basic: basic - emptyConfig: empty-config - noConfig: no-config - - # Deps - foo: deps/foo - bar: deps/bar - baz: deps/baz - - # Tasks - tasks: tasks - - # Languages + types - js: langs/js - ts: langs/ts - bash: langs/bash - platforms: platforms diff --git a/tests/fixtures/shared-toolchain.yml b/tests/fixtures/shared-toolchain.yml deleted file mode 100644 index 85c6309963b..00000000000 --- a/tests/fixtures/shared-toolchain.yml +++ /dev/null @@ -1,9 +0,0 @@ -node: - version: '16.0.0' - dedupeOnLockfileChange: false - -typescript: - createMissingConfig: false - routeOutDirToCache: false - syncProjectReferences: false - syncProjectReferencesToPaths: false diff --git a/tests/fixtures/shared-workspace.yml b/tests/fixtures/shared-workspace.yml deleted file mode 100644 index b117f8a6ff8..00000000000 --- a/tests/fixtures/shared-workspace.yml +++ /dev/null @@ -1 +0,0 @@ -projects: {} diff --git a/tests/fixtures/system/.moon/workspace.yml b/tests/fixtures/system/.moon/workspace.yml deleted file mode 100644 index c448710a2ea..00000000000 --- a/tests/fixtures/system/.moon/workspace.yml +++ /dev/null @@ -1,3 +0,0 @@ -projects: - unix: 'unix' - windows: 'windows' diff --git a/tests/fixtures/tasks/.moon/project.yml b/tests/fixtures/tasks/.moon/project.yml deleted file mode 100644 index 135139dde4a..00000000000 --- a/tests/fixtures/tasks/.moon/project.yml +++ /dev/null @@ -1,19 +0,0 @@ -tasks: - standard: - command: cmd - withArgs: - command: cmd - args: - - --foo - - --bar - - baz - withInputs: - command: cmd - inputs: - - rel/file.* - - /root.* - withOutputs: - command: cmd - outputs: - - lib - - /build diff --git a/tests/fixtures/tasks/.moon/toolchain.yml b/tests/fixtures/tasks/.moon/toolchain.yml deleted file mode 100644 index dc3814fe6d9..00000000000 --- a/tests/fixtures/tasks/.moon/toolchain.yml +++ /dev/null @@ -1 +0,0 @@ -extends: '../shared-toolchain.yml' diff --git a/tests/fixtures/tasks/.moon/workspace.yml b/tests/fixtures/tasks/.moon/workspace.yml deleted file mode 100644 index 1128a4995b3..00000000000 --- a/tests/fixtures/tasks/.moon/workspace.yml +++ /dev/null @@ -1,17 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: - basic: 'basic' - buildA: 'build-a' - buildB: 'build-b' - buildC: 'build-c' - chain: chain - cycle: cycle - inputA: 'input-a' - inputB: 'input-b' - inputC: 'input-c' - mergeAllStrategies: 'merge-all-strategies' - mergeAppend: 'merge-append' - mergePrepend: 'merge-prepend' - mergeReplace: 'merge-replace' - noTasks: 'no-tasks' diff --git a/tests/fixtures/typescript/.moon/project.yml b/tests/fixtures/typescript/.moon/project.yml deleted file mode 100644 index b56e564ca93..00000000000 --- a/tests/fixtures/typescript/.moon/project.yml +++ /dev/null @@ -1,3 +0,0 @@ -tasks: - test: - command: noop diff --git a/tests/fixtures/typescript/.moon/toolchain.yml b/tests/fixtures/typescript/.moon/toolchain.yml deleted file mode 100644 index 1f0be78ce7d..00000000000 --- a/tests/fixtures/typescript/.moon/toolchain.yml +++ /dev/null @@ -1,7 +0,0 @@ -extends: '../shared-toolchain.yml' - -typescript: - createMissingConfig: true - routeOutDirToCache: false - syncProjectReferences: true - syncProjectReferencesToPaths: false diff --git a/tests/fixtures/typescript/.moon/workspace.yml b/tests/fixtures/typescript/.moon/workspace.yml deleted file mode 100644 index 45222fca493..00000000000 --- a/tests/fixtures/typescript/.moon/workspace.yml +++ /dev/null @@ -1,4 +0,0 @@ -extends: '../shared-workspace.yml' - -projects: - - '*' diff --git a/website/docs/config/global-project.mdx b/website/docs/config/global-project.mdx index af97a50abbd..f27b91b11f7 100644 --- a/website/docs/config/global-project.mdx +++ b/website/docs/config/global-project.mdx @@ -9,6 +9,10 @@ The `.moon/project.yml` file configures file groups and tasks that are inherited in the workspace. Projects can override or merge with these settings within their respective [`moon.yml`](./project). +```yaml title=".moon/project.yml" +$schema: 'https://moonrepo.dev/schemas/global-project.json' +``` + ## `extends` diff --git a/website/docs/config/project.mdx b/website/docs/config/project.mdx index 9ac7d134df0..8fd82af7493 100644 --- a/website/docs/config/project.mdx +++ b/website/docs/config/project.mdx @@ -11,6 +11,10 @@ The `moon.yml` configuration file _is not required_ but can be used to define ad for a project, override inherited tasks, and more at the project-level. When used, this file must exist in a project's root, as configured in [`projects`](./workspace#projects). +```yaml title="moon.yml" +$schema: 'https://moonrepo.dev/schemas/project.json' +``` + ## `dependsOn` diff --git a/website/docs/config/template.mdx b/website/docs/config/template.mdx index a0f4e1c8494..c7813323ef0 100644 --- a/website/docs/config/template.mdx +++ b/website/docs/config/template.mdx @@ -12,6 +12,10 @@ import VersionLabel from '@site/src/components/Docs/VersionLabel'; The `template.yml` file configures metadata and variables for a template, [used by the generator](../guides/codegen), and must exist at the root of a named template folder. +```yaml title="template.yml" +$schema: 'https://moonrepo.dev/schemas/template.json' +``` + ## `title` diff --git a/website/docs/config/toolchain.mdx b/website/docs/config/toolchain.mdx index 26d140ad654..8845844e262 100644 --- a/website/docs/config/toolchain.mdx +++ b/website/docs/config/toolchain.mdx @@ -13,6 +13,10 @@ import VersionLabel from '@site/src/components/Docs/VersionLabel'; The `.moon/toolchain.yml` file configures the toolchain and the workspace development environment. +```yaml title=".moon/toolchain.yml" +$schema: 'https://moonrepo.dev/schemas/toolchain.json' +``` + ## `extends` diff --git a/website/docs/config/workspace.mdx b/website/docs/config/workspace.mdx index a1f34e509ce..c7cb701db6c 100644 --- a/website/docs/config/workspace.mdx +++ b/website/docs/config/workspace.mdx @@ -11,6 +11,10 @@ import VersionLabel from '@site/src/components/Docs/VersionLabel'; The `.moon/workspace.yml` file configures projects and services in the workspace. +```yaml title=".moon/workspace.yml" +$schema: 'https://moonrepo.dev/schemas/workspace.json' +``` + ## `extends` diff --git a/website/static/schemas/global-project.json b/website/static/schemas/global-project.json index 14629639608..40a24422527 100644 --- a/website/static/schemas/global-project.json +++ b/website/static/schemas/global-project.json @@ -5,7 +5,6 @@ "type": "object", "properties": { "extends": { - "default": null, "type": [ "string", "null" diff --git a/website/static/schemas/project.json b/website/static/schemas/project.json index 7c651163833..720f3feaca7 100644 --- a/website/static/schemas/project.json +++ b/website/static/schemas/project.json @@ -47,6 +47,17 @@ "$ref": "#/definitions/TaskConfig" } }, + "toolchain": { + "default": { + "node": null, + "typescript": true + }, + "allOf": [ + { + "$ref": "#/definitions/ProjectToolchainConfig" + } + ] + }, "type": { "default": "unknown", "allOf": [ @@ -61,9 +72,7 @@ "exclude": null, "include": null, "rename": null - }, - "node": null, - "typescript": true + } }, "allOf": [ { @@ -156,6 +165,38 @@ } } }, + "ProjectToolchainConfig": { + "type": "object", + "properties": { + "node": { + "default": null, + "anyOf": [ + { + "$ref": "#/definitions/ProjectToolchainNodeConfig" + }, + { + "type": "null" + } + ] + }, + "typescript": { + "default": true, + "type": "boolean" + } + } + }, + "ProjectToolchainNodeConfig": { + "type": "object", + "properties": { + "version": { + "default": null, + "type": [ + "string", + "null" + ] + } + } + }, "ProjectType": { "type": "string", "enum": [ @@ -179,21 +220,6 @@ "$ref": "#/definitions/ProjectWorkspaceInheritedTasksConfig" } ] - }, - "node": { - "default": null, - "anyOf": [ - { - "$ref": "#/definitions/ProjectWorkspaceNodeConfig" - }, - { - "type": "null" - } - ] - }, - "typescript": { - "default": true, - "type": "boolean" } } }, @@ -232,18 +258,6 @@ } } }, - "ProjectWorkspaceNodeConfig": { - "type": "object", - "properties": { - "version": { - "default": null, - "type": [ - "string", - "null" - ] - } - } - }, "TaskCommandArgs": { "anyOf": [ { diff --git a/website/static/schemas/toolchain.json b/website/static/schemas/toolchain.json index 611ea60a1a7..a45290e8db5 100644 --- a/website/static/schemas/toolchain.json +++ b/website/static/schemas/toolchain.json @@ -5,7 +5,6 @@ "type": "object", "properties": { "extends": { - "default": null, "type": [ "string", "null" diff --git a/website/static/schemas/workspace.json b/website/static/schemas/workspace.json index e8f7996f5e6..54fcab6f296 100644 --- a/website/static/schemas/workspace.json +++ b/website/static/schemas/workspace.json @@ -5,7 +5,6 @@ "type": "object", "properties": { "extends": { - "default": null, "type": [ "string", "null" @@ -58,6 +57,7 @@ "implicitInputs": [ "package.json", "/.moon/project.yml", + "/.moon/toolchain.yml", "/.moon/workspace.yml" ], "inheritColorsForPipedTasks": true, @@ -150,6 +150,7 @@ "default": [ "package.json", "/.moon/project.yml", + "/.moon/toolchain.yml", "/.moon/workspace.yml" ], "type": "array",