Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
[[plan]]
name = "nested vp run"
args = ["run", "script2"]
compact = true
Original file line number Diff line number Diff line change
@@ -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": [
"<workspace>/",
"script2"
{
"#script2": {
"items": [
{
"#script1": []
}
],
"node": {
"task_display": {
"package_name": "",
"task_name": "script2",
"package_path": "<workspace>/"
},
"items": [
{
"execution_item_display": {
"task_display": {
"package_name": "",
"task_name": "script2",
"package_path": "<workspace>/"
},
"command": "vp run script1",
"and_item_index": null,
"cwd": "<workspace>/"
},
"kind": {
"Expanded": [
{
"key": [
"<workspace>/",
"script1"
],
"node": {
"task_display": {
"package_name": "",
"task_name": "script1",
"package_path": "<workspace>/"
},
"items": [
{
"execution_item_display": {
"task_display": {
"package_name": "",
"task_name": "script1",
"package_path": "<workspace>/"
},
"command": "echo hello",
"and_item_index": null,
"cwd": "<workspace>/"
},
"kind": {
"Leaf": {
"InProcess": {
"kind": {
"Echo": {
"strings": [
"hello"
],
"trailing_newline": true
}
}
}
}
}
}
]
},
"neighbors": []
}
]
}
}
]
},
"neighbors": []
}
]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[[plan]]
name = "allow `packages` in pnpm-workspace.yaml to be optional"
args = ["run", "hello"]
compact = true
Original file line number Diff line number Diff line change
@@ -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": [
"<workspace>/",
"hello"
],
"node": {
"task_display": {
"package_name": "additional-envs",
"task_name": "hello",
"package_path": "<workspace>/"
},
"items": [
{
"execution_item_display": {
"task_display": {
"package_name": "additional-envs",
"task_name": "hello",
"package_path": "<workspace>/"
},
"command": "echo hello",
"and_item_index": null,
"cwd": "<workspace>/"
},
"kind": {
"Leaf": {
"InProcess": {
"kind": {
"Echo": {
"strings": [
"hello"
],
"trailing_newline": true
}
}
}
}
}
}
]
},
"neighbors": []
}
]
{
"#hello": []
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[[plan]]
name = "vpr expands to vp run"
args = ["run", "all"]
compact = true
Original file line number Diff line number Diff line change
@@ -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": [
"<workspace>/",
"all"
{
"#all": {
"items": [
{
"#build": []
}
],
"node": {
"task_display": {
"package_name": "",
"task_name": "all",
"package_path": "<workspace>/"
},
"items": [
{
"execution_item_display": {
"task_display": {
"package_name": "",
"task_name": "all",
"package_path": "<workspace>/"
},
"command": "vpr build",
"and_item_index": null,
"cwd": "<workspace>/"
},
"kind": {
"Expanded": [
{
"key": [
"<workspace>/",
"build"
],
"node": {
"task_display": {
"package_name": "",
"task_name": "build",
"package_path": "<workspace>/"
},
"items": [
{
"execution_item_display": {
"task_display": {
"package_name": "",
"task_name": "build",
"package_path": "<workspace>/"
},
"command": "echo building",
"and_item_index": null,
"cwd": "<workspace>/"
},
"kind": {
"Leaf": {
"InProcess": {
"kind": {
"Echo": {
"strings": [
"building"
],
"trailing_newline": true
}
}
}
}
}
}
]
},
"neighbors": []
}
]
}
}
]
},
"neighbors": []
}
]
}
83 changes: 80 additions & 3 deletions crates/vite_task_plan/tests/plan_snapshots/main.rs
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -27,6 +34,8 @@ struct Plan {
pub args: Vec<Str>,
#[serde(default)]
pub cwd: RelativePathBuf,
#[serde(default)]
pub compact: bool,
}

#[derive(serde::Deserialize, Default)]
Expand All @@ -35,6 +44,68 @@ struct SnapshotsFile {
pub plan_cases: Vec<Plan>,
}

/// 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<Str, CompactNode>);

/// 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<Str>),
/// Has nested `Expanded` items
WithItems { items: Vec<CompactPlan>, neighbors: BTreeSet<Str> },
}

impl CompactPlan {
fn from_execution_graph(graph: &ExecutionGraph, workspace_root: &AbsolutePath) -> Self {
use petgraph::visit::EdgeRef as _;
let mut map = BTreeMap::<Str, CompactNode>::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<Str> = graph
.edges(node_idx)
.map(|edge| Self::task_key(&graph[edge.target()].task_display, workspace_root))
.collect();

let expanded_items: Vec<Self> = 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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
});
}
Expand Down