|
| 1 | +use moon_action::{Action, ActionStatus, InstallDependenciesNode}; |
| 2 | +use moon_action_context::ActionContext; |
| 3 | +use moon_actions::actions::install_dependencies; |
| 4 | +use moon_common::{Id, is_ci, path::WorkspaceRelativePathBuf}; |
| 5 | +use moon_test_utils::WorkspaceMocker; |
| 6 | +use starbase_sandbox::{Sandbox, create_empty_sandbox}; |
| 7 | +use starbase_utils::json::JsonValue; |
| 8 | + |
| 9 | +fn create_workspace() -> (Sandbox, WorkspaceMocker) { |
| 10 | + let sandbox = create_empty_sandbox(); |
| 11 | + |
| 12 | + // The tier1 toolchain (inherited by tc-tier2) registers `tc.lock` |
| 13 | + // as its lockfile and `vendor` as its vendor directory |
| 14 | + sandbox.create_file("tc.lock", "v1"); |
| 15 | + |
| 16 | + let mocker = WorkspaceMocker::new(sandbox.path()) |
| 17 | + .with_default_projects() |
| 18 | + .with_test_toolchains() |
| 19 | + // Opt-in to the install/dedupe commands returned by tc-tier2 |
| 20 | + .update_toolchains_config(|config| { |
| 21 | + config |
| 22 | + .plugins |
| 23 | + .get_mut(&Id::raw("tc-tier2")) |
| 24 | + .unwrap() |
| 25 | + .config |
| 26 | + .insert("testInstallCommands".into(), JsonValue::Bool(true)); |
| 27 | + }) |
| 28 | + // Hash with the cache engine instead of the VCS, |
| 29 | + // as the sandbox is not a git repository |
| 30 | + .update_workspace_config(|config| { |
| 31 | + config.experiments.native_file_hashing = true; |
| 32 | + }); |
| 33 | + |
| 34 | + (sandbox, mocker) |
| 35 | +} |
| 36 | + |
| 37 | +async fn run_action(ws: &WorkspaceMocker) -> (Action, ActionStatus) { |
| 38 | + let mut action = Action::default(); |
| 39 | + let node = InstallDependenciesNode { |
| 40 | + members: None, |
| 41 | + project_id: None, |
| 42 | + root: WorkspaceRelativePathBuf::default(), |
| 43 | + toolchain_id: Id::raw("tc-tier2"), |
| 44 | + }; |
| 45 | + |
| 46 | + let status = install_dependencies( |
| 47 | + &mut action, |
| 48 | + ActionContext::default().into(), |
| 49 | + ws.mock_app_context().into(), |
| 50 | + ws.mock_workspace_graph().await.into(), |
| 51 | + &node, |
| 52 | + ) |
| 53 | + .await |
| 54 | + .unwrap(); |
| 55 | + |
| 56 | + (action, status) |
| 57 | +} |
| 58 | + |
| 59 | +fn count_execs(action: &Action) -> usize { |
| 60 | + action |
| 61 | + .operations |
| 62 | + .iter() |
| 63 | + .filter(|op| op.meta.is_process_execution()) |
| 64 | + .count() |
| 65 | +} |
| 66 | + |
| 67 | +mod install_dependencies { |
| 68 | + use super::*; |
| 69 | + |
| 70 | + #[serial_test::serial] |
| 71 | + #[tokio::test(flavor = "multi_thread")] |
| 72 | + async fn does_not_dedupe_on_first_install() { |
| 73 | + let (_sandbox, ws) = create_workspace(); |
| 74 | + |
| 75 | + // No vendor directory exists yet, so only the install command must |
| 76 | + // run. Deduping would rewrite a pristine lockfile, since a cold |
| 77 | + // cache always hashes as "changed" |
| 78 | + let (action, status) = run_action(&ws).await; |
| 79 | + |
| 80 | + assert_eq!(status, ActionStatus::Passed); |
| 81 | + assert_eq!(count_execs(&action), 1); |
| 82 | + } |
| 83 | + |
| 84 | + #[serial_test::serial] |
| 85 | + #[tokio::test(flavor = "multi_thread")] |
| 86 | + async fn dedupes_when_dependencies_already_installed() { |
| 87 | + let (sandbox, ws) = create_workspace(); |
| 88 | + sandbox.create_file("vendor/dep/manifest", ""); |
| 89 | + |
| 90 | + let (action, status) = run_action(&ws).await; |
| 91 | + |
| 92 | + if is_ci() { |
| 93 | + // In CI, an existing vendor directory skips the action entirely |
| 94 | + assert_eq!(status, ActionStatus::Skipped); |
| 95 | + assert_eq!(count_execs(&action), 0); |
| 96 | + } else { |
| 97 | + // Install + dedupe |
| 98 | + assert_eq!(status, ActionStatus::Passed); |
| 99 | + assert_eq!(count_execs(&action), 2); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + #[serial_test::serial] |
| 104 | + #[tokio::test(flavor = "multi_thread")] |
| 105 | + async fn dedupes_on_lockfile_change_after_first_install() { |
| 106 | + let (sandbox, ws) = create_workspace(); |
| 107 | + |
| 108 | + // Cold cache: install only, no dedupe |
| 109 | + let (action, status) = run_action(&ws).await; |
| 110 | + |
| 111 | + assert_eq!(status, ActionStatus::Passed); |
| 112 | + assert_eq!(count_execs(&action), 1); |
| 113 | + |
| 114 | + // Simulate the install having populated the vendor directory |
| 115 | + sandbox.create_file("vendor/dep/manifest", ""); |
| 116 | + |
| 117 | + // Nothing changed: nothing to install or dedupe |
| 118 | + let (action, status) = run_action(&ws).await; |
| 119 | + |
| 120 | + assert_eq!(status, ActionStatus::Skipped); |
| 121 | + assert_eq!(count_execs(&action), 0); |
| 122 | + |
| 123 | + // Lockfile changed: install and dedupe |
| 124 | + sandbox.create_file("tc.lock", "v2"); |
| 125 | + |
| 126 | + let (action, status) = run_action(&ws).await; |
| 127 | + |
| 128 | + if is_ci() { |
| 129 | + assert_eq!(status, ActionStatus::Skipped); |
| 130 | + assert_eq!(count_execs(&action), 0); |
| 131 | + } else { |
| 132 | + assert_eq!(status, ActionStatus::Passed); |
| 133 | + assert_eq!(count_execs(&action), 2); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments