From 04c782cfd231eadd6ac24b8c0f1628981f030269 Mon Sep 17 00:00:00 2001 From: branchseer Date: Thu, 26 Feb 2026 21:43:20 +0800 Subject: [PATCH 1/2] feat: add `compact` option to plan snapshot tests When `compact = true` in `[[plan]]`, the snapshot only contains the top-level graph topology as `"relative_path#task_name" -> [neighbors]`, omitting execution details like spawn commands, env vars, and cache metadata. Nodes with nested `Expanded` items include an `items` array. Co-Authored-By: Claude Opus 4.6 --- .../tests/plan_snapshots/main.rs | 83 ++++++++++++++++++- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/crates/vite_task_plan/tests/plan_snapshots/main.rs b/crates/vite_task_plan/tests/plan_snapshots/main.rs index fe5d49d2..c9eea30d 100644 --- a/crates/vite_task_plan/tests/plan_snapshots/main.rs +++ b/crates/vite_task_plan/tests/plan_snapshots/main.rs @@ -1,16 +1,23 @@ mod redact; -use std::{ffi::OsStr, sync::Arc}; +use std::{ + collections::{BTreeMap, BTreeSet}, + ffi::OsStr, + sync::Arc, +}; use clap::Parser; use copy_dir::copy_dir; use cow_utils::CowUtils as _; use redact::redact_snapshot; use rustc_hash::FxHashMap; +use serde::Serialize; use tokio::runtime::Runtime; use vite_path::{AbsolutePath, AbsolutePathBuf, RelativePathBuf}; use vite_str::Str; use vite_task::{Command, Session}; +use vite_task_graph::display::TaskDisplay; +use vite_task_plan::{ExecutionGraph, ExecutionItemKind}; use vite_workspace::find_workspace_root; /// Local parser wrapper for `BuiltInCommand` @@ -27,6 +34,8 @@ struct Plan { pub args: Vec, #[serde(default)] pub cwd: RelativePathBuf, + #[serde(default)] + pub compact: bool, } #[derive(serde::Deserialize, Default)] @@ -35,6 +44,68 @@ struct SnapshotsFile { pub plan_cases: Vec, } +/// Compact plan: maps `"relative_path#task_name"` to either just neighbors (simple) +/// or `{ items, neighbors }` when the node has nested `Expanded` execution items. +#[derive(Serialize)] +#[serde(transparent)] +struct CompactPlan(BTreeMap); + +/// Untagged enum so simple nodes serialize as just an array, and nodes with +/// expanded items serialize as `{ "items": [...], "neighbors": [...] }`. +#[derive(Serialize)] +#[serde(untagged)] +enum CompactNode { + /// No nested `Expanded` items — just the neighbor list + Simple(BTreeSet), + /// Has nested `Expanded` items + WithItems { items: Vec, neighbors: BTreeSet }, +} + +impl CompactPlan { + fn from_execution_graph(graph: &ExecutionGraph, workspace_root: &AbsolutePath) -> Self { + use petgraph::visit::EdgeRef as _; + let mut map = BTreeMap::::new(); + for node_idx in graph.node_indices() { + let node = &graph[node_idx]; + let key = Self::task_key(&node.task_display, workspace_root); + + let neighbors: BTreeSet = graph + .edges(node_idx) + .map(|edge| Self::task_key(&graph[edge.target()].task_display, workspace_root)) + .collect(); + + let expanded_items: Vec = node + .items + .iter() + .filter_map(|item| { + if let ExecutionItemKind::Expanded(sub_graph) = &item.kind { + Some(Self::from_execution_graph(sub_graph, workspace_root)) + } else { + None + } + }) + .collect(); + + let compact_node = if expanded_items.is_empty() { + CompactNode::Simple(neighbors) + } else { + CompactNode::WithItems { items: expanded_items, neighbors } + }; + map.insert(key, compact_node); + } + Self(map) + } + + fn task_key(task_display: &TaskDisplay, workspace_root: &AbsolutePath) -> Str { + let relative = task_display + .package_path + .strip_prefix(workspace_root) + .expect("strip_prefix should not produce invalid path data") + .expect("package_path must be under workspace_root"); + vite_str::format!("{}#{}", relative, task_display.task_name) + } +} + #[expect(clippy::disallowed_types, reason = "Path required by insta::glob! callback signature")] fn run_case( runtime: &Runtime, @@ -157,6 +228,7 @@ fn run_case_inner( for plan in cases_file.plan_cases { let snapshot_name = vite_str::format!("query - {}", plan.name); + let compact = plan.compact; let cli = match Cli::try_parse_from( std::iter::once("vp") // dummy program name @@ -208,8 +280,13 @@ fn run_case_inner( } }; - let plan_json = redact_snapshot(&plan, workspace_root_str); - insta::assert_json_snapshot!(snapshot_name.as_str(), &plan_json); + if compact { + let compact_plan = CompactPlan::from_execution_graph(&plan, &workspace_root.path); + insta::assert_json_snapshot!(snapshot_name.as_str(), &compact_plan); + } else { + let plan_json = redact_snapshot(&plan, workspace_root_str); + insta::assert_json_snapshot!(snapshot_name.as_str(), &plan_json); + } } }); } From 6a0899c615b369cb60113fe6d58aef6bbdbe7593 Mon Sep 17 00:00:00 2001 From: branchseer Date: Thu, 26 Feb 2026 21:50:59 +0800 Subject: [PATCH 2/2] test: enable compact plan snapshots for structure-only test cases Enable compact = true for nested-tasks, pnpm-workspace-packages-optional, and vpr-shorthand fixtures since they only verify graph structure, not execution details like cache metadata or env vars. Co-Authored-By: Claude Opus 4.6 --- .../fixtures/nested-tasks/snapshots.toml | 1 + .../snapshots/query - nested vp run.snap | 82 ++----------------- .../snapshots.toml | 1 + ...in pnpm-workspace.yaml to be optional.snap | 49 +---------- .../fixtures/vpr-shorthand/snapshots.toml | 1 + .../query - vpr expands to vp run.snap | 82 ++----------------- 6 files changed, 23 insertions(+), 193 deletions(-) diff --git a/crates/vite_task_plan/tests/plan_snapshots/fixtures/nested-tasks/snapshots.toml b/crates/vite_task_plan/tests/plan_snapshots/fixtures/nested-tasks/snapshots.toml index 022ce196..6a235730 100644 --- a/crates/vite_task_plan/tests/plan_snapshots/fixtures/nested-tasks/snapshots.toml +++ b/crates/vite_task_plan/tests/plan_snapshots/fixtures/nested-tasks/snapshots.toml @@ -3,3 +3,4 @@ [[plan]] name = "nested vp run" args = ["run", "script2"] +compact = true diff --git a/crates/vite_task_plan/tests/plan_snapshots/fixtures/nested-tasks/snapshots/query - nested vp run.snap b/crates/vite_task_plan/tests/plan_snapshots/fixtures/nested-tasks/snapshots/query - nested vp run.snap index 5a40c0d8..0e9edb74 100644 --- a/crates/vite_task_plan/tests/plan_snapshots/fixtures/nested-tasks/snapshots/query - nested vp run.snap +++ b/crates/vite_task_plan/tests/plan_snapshots/fixtures/nested-tasks/snapshots/query - nested vp run.snap @@ -1,81 +1,15 @@ --- source: crates/vite_task_plan/tests/plan_snapshots/main.rs -expression: "&plan_json" +expression: "&compact_plan" input_file: crates/vite_task_plan/tests/plan_snapshots/fixtures/nested-tasks --- -[ - { - "key": [ - "/", - "script2" +{ + "#script2": { + "items": [ + { + "#script1": [] + } ], - "node": { - "task_display": { - "package_name": "", - "task_name": "script2", - "package_path": "/" - }, - "items": [ - { - "execution_item_display": { - "task_display": { - "package_name": "", - "task_name": "script2", - "package_path": "/" - }, - "command": "vp run script1", - "and_item_index": null, - "cwd": "/" - }, - "kind": { - "Expanded": [ - { - "key": [ - "/", - "script1" - ], - "node": { - "task_display": { - "package_name": "", - "task_name": "script1", - "package_path": "/" - }, - "items": [ - { - "execution_item_display": { - "task_display": { - "package_name": "", - "task_name": "script1", - "package_path": "/" - }, - "command": "echo hello", - "and_item_index": null, - "cwd": "/" - }, - "kind": { - "Leaf": { - "InProcess": { - "kind": { - "Echo": { - "strings": [ - "hello" - ], - "trailing_newline": true - } - } - } - } - } - } - ] - }, - "neighbors": [] - } - ] - } - } - ] - }, "neighbors": [] } -] +} diff --git a/crates/vite_task_plan/tests/plan_snapshots/fixtures/pnpm-workspace-packages-optional/snapshots.toml b/crates/vite_task_plan/tests/plan_snapshots/fixtures/pnpm-workspace-packages-optional/snapshots.toml index 84446107..08e3bcc3 100644 --- a/crates/vite_task_plan/tests/plan_snapshots/fixtures/pnpm-workspace-packages-optional/snapshots.toml +++ b/crates/vite_task_plan/tests/plan_snapshots/fixtures/pnpm-workspace-packages-optional/snapshots.toml @@ -1,3 +1,4 @@ [[plan]] name = "allow `packages` in pnpm-workspace.yaml to be optional" args = ["run", "hello"] +compact = true diff --git a/crates/vite_task_plan/tests/plan_snapshots/fixtures/pnpm-workspace-packages-optional/snapshots/query - allow `packages` in pnpm-workspace.yaml to be optional.snap b/crates/vite_task_plan/tests/plan_snapshots/fixtures/pnpm-workspace-packages-optional/snapshots/query - allow `packages` in pnpm-workspace.yaml to be optional.snap index e903be3c..0769c36b 100644 --- a/crates/vite_task_plan/tests/plan_snapshots/fixtures/pnpm-workspace-packages-optional/snapshots/query - allow `packages` in pnpm-workspace.yaml to be optional.snap +++ b/crates/vite_task_plan/tests/plan_snapshots/fixtures/pnpm-workspace-packages-optional/snapshots/query - allow `packages` in pnpm-workspace.yaml to be optional.snap @@ -1,49 +1,8 @@ --- source: crates/vite_task_plan/tests/plan_snapshots/main.rs -expression: "&plan_json" +expression: "&compact_plan" input_file: crates/vite_task_plan/tests/plan_snapshots/fixtures/pnpm-workspace-packages-optional --- -[ - { - "key": [ - "/", - "hello" - ], - "node": { - "task_display": { - "package_name": "additional-envs", - "task_name": "hello", - "package_path": "/" - }, - "items": [ - { - "execution_item_display": { - "task_display": { - "package_name": "additional-envs", - "task_name": "hello", - "package_path": "/" - }, - "command": "echo hello", - "and_item_index": null, - "cwd": "/" - }, - "kind": { - "Leaf": { - "InProcess": { - "kind": { - "Echo": { - "strings": [ - "hello" - ], - "trailing_newline": true - } - } - } - } - } - } - ] - }, - "neighbors": [] - } -] +{ + "#hello": [] +} diff --git a/crates/vite_task_plan/tests/plan_snapshots/fixtures/vpr-shorthand/snapshots.toml b/crates/vite_task_plan/tests/plan_snapshots/fixtures/vpr-shorthand/snapshots.toml index 7ccda380..ade1855f 100644 --- a/crates/vite_task_plan/tests/plan_snapshots/fixtures/vpr-shorthand/snapshots.toml +++ b/crates/vite_task_plan/tests/plan_snapshots/fixtures/vpr-shorthand/snapshots.toml @@ -1,3 +1,4 @@ [[plan]] name = "vpr expands to vp run" args = ["run", "all"] +compact = true diff --git a/crates/vite_task_plan/tests/plan_snapshots/fixtures/vpr-shorthand/snapshots/query - vpr expands to vp run.snap b/crates/vite_task_plan/tests/plan_snapshots/fixtures/vpr-shorthand/snapshots/query - vpr expands to vp run.snap index 693bf502..b581a566 100644 --- a/crates/vite_task_plan/tests/plan_snapshots/fixtures/vpr-shorthand/snapshots/query - vpr expands to vp run.snap +++ b/crates/vite_task_plan/tests/plan_snapshots/fixtures/vpr-shorthand/snapshots/query - vpr expands to vp run.snap @@ -1,81 +1,15 @@ --- source: crates/vite_task_plan/tests/plan_snapshots/main.rs -expression: "&plan_json" +expression: "&compact_plan" input_file: crates/vite_task_plan/tests/plan_snapshots/fixtures/vpr-shorthand --- -[ - { - "key": [ - "/", - "all" +{ + "#all": { + "items": [ + { + "#build": [] + } ], - "node": { - "task_display": { - "package_name": "", - "task_name": "all", - "package_path": "/" - }, - "items": [ - { - "execution_item_display": { - "task_display": { - "package_name": "", - "task_name": "all", - "package_path": "/" - }, - "command": "vpr build", - "and_item_index": null, - "cwd": "/" - }, - "kind": { - "Expanded": [ - { - "key": [ - "/", - "build" - ], - "node": { - "task_display": { - "package_name": "", - "task_name": "build", - "package_path": "/" - }, - "items": [ - { - "execution_item_display": { - "task_display": { - "package_name": "", - "task_name": "build", - "package_path": "/" - }, - "command": "echo building", - "and_item_index": null, - "cwd": "/" - }, - "kind": { - "Leaf": { - "InProcess": { - "kind": { - "Echo": { - "strings": [ - "building" - ], - "trailing_newline": true - } - } - } - } - } - } - ] - }, - "neighbors": [] - } - ] - } - } - ] - }, "neighbors": [] } -] +}