Skip to content

Commit 1fdb09a

Browse files
committed
test: add unit tests for resolve_synthetic_cache_config input merging
1 parent 8c3cc35 commit 1fdb09a

1 file changed

Lines changed: 209 additions & 0 deletions

File tree

crates/vite_task_plan/src/plan.rs

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,3 +767,212 @@ pub async fn plan_query_request(
767767
Error::CycleDependencyDetected(displays)
768768
})
769769
}
770+
771+
#[cfg(test)]
772+
mod tests {
773+
use std::collections::BTreeSet;
774+
775+
use rustc_hash::FxHashSet;
776+
use vite_path::AbsolutePathBuf;
777+
use vite_str::Str;
778+
use vite_task_graph::config::{
779+
CacheConfig, EnvConfig, ResolvedInputConfig,
780+
user::{EnabledCacheConfig, UserCacheConfig, UserInputEntry},
781+
};
782+
783+
use super::{ParentCacheConfig, resolve_synthetic_cache_config};
784+
785+
fn make_workspace_path() -> AbsolutePathBuf {
786+
#[cfg(unix)]
787+
{
788+
AbsolutePathBuf::new("/workspace".into()).unwrap()
789+
}
790+
#[cfg(windows)]
791+
{
792+
AbsolutePathBuf::new("C:\\workspace".into()).unwrap()
793+
}
794+
}
795+
796+
fn make_parent_config() -> CacheConfig {
797+
CacheConfig {
798+
env_config: EnvConfig {
799+
fingerprinted_envs: FxHashSet::default(),
800+
untracked_env: FxHashSet::default(),
801+
},
802+
input_config: ResolvedInputConfig::default_auto(),
803+
}
804+
}
805+
806+
#[test]
807+
fn inherited_disabled_synthetic_returns_none() {
808+
let workspace = make_workspace_path();
809+
let cwd = workspace.clone().into();
810+
let result = resolve_synthetic_cache_config(
811+
ParentCacheConfig::Inherited(make_parent_config()),
812+
UserCacheConfig::disabled(),
813+
&cwd,
814+
&workspace,
815+
)
816+
.unwrap();
817+
assert!(result.is_none());
818+
}
819+
820+
#[test]
821+
fn disabled_parent_returns_none() {
822+
let workspace = make_workspace_path();
823+
let cwd = workspace.clone().into();
824+
let result = resolve_synthetic_cache_config(
825+
ParentCacheConfig::Disabled,
826+
UserCacheConfig::with_config(EnabledCacheConfig {
827+
env: None,
828+
untracked_env: None,
829+
input: None,
830+
}),
831+
&cwd,
832+
&workspace,
833+
)
834+
.unwrap();
835+
assert!(result.is_none());
836+
}
837+
838+
#[test]
839+
fn inherited_merges_env() {
840+
let workspace = make_workspace_path();
841+
let cwd = workspace.clone().into();
842+
let result = resolve_synthetic_cache_config(
843+
ParentCacheConfig::Inherited(make_parent_config()),
844+
UserCacheConfig::with_config(EnabledCacheConfig {
845+
env: Some(Box::new([Str::from("VITE_*")])),
846+
untracked_env: Some(vec![Str::from("HOME")]),
847+
input: None,
848+
}),
849+
&cwd,
850+
&workspace,
851+
)
852+
.unwrap()
853+
.unwrap();
854+
assert!(result.env_config.fingerprinted_envs.contains("VITE_*"));
855+
assert!(result.env_config.untracked_env.contains("HOME"));
856+
}
857+
858+
#[test]
859+
fn inherited_merges_negative_input_globs() {
860+
let workspace = make_workspace_path();
861+
let cwd = workspace.clone().into();
862+
let result = resolve_synthetic_cache_config(
863+
ParentCacheConfig::Inherited(make_parent_config()),
864+
UserCacheConfig::with_config(EnabledCacheConfig {
865+
env: None,
866+
untracked_env: None,
867+
input: Some(vec![
868+
UserInputEntry::Auto { auto: true },
869+
UserInputEntry::Glob(Str::from("!node_modules/.vite-temp/**")),
870+
]),
871+
}),
872+
&cwd,
873+
&workspace,
874+
)
875+
.unwrap()
876+
.unwrap();
877+
assert!(result.input_config.includes_auto);
878+
assert!(
879+
result.input_config.negative_globs.contains(&Str::from("node_modules/.vite-temp/**")),
880+
"negative globs should contain the vite-temp exclusion, got: {:?}",
881+
result.input_config.negative_globs,
882+
);
883+
}
884+
885+
#[test]
886+
fn inherited_merges_positive_input_globs() {
887+
let workspace = make_workspace_path();
888+
let cwd = workspace.clone().into();
889+
let result = resolve_synthetic_cache_config(
890+
ParentCacheConfig::Inherited(make_parent_config()),
891+
UserCacheConfig::with_config(EnabledCacheConfig {
892+
env: None,
893+
untracked_env: None,
894+
input: Some(vec![UserInputEntry::Glob(Str::from("src/**"))]),
895+
}),
896+
&cwd,
897+
&workspace,
898+
)
899+
.unwrap()
900+
.unwrap();
901+
assert!(
902+
result.input_config.positive_globs.contains(&Str::from("src/**")),
903+
"positive globs should contain src/**, got: {:?}",
904+
result.input_config.positive_globs,
905+
);
906+
}
907+
908+
#[test]
909+
fn inherited_input_none_preserves_parent_config() {
910+
let workspace = make_workspace_path();
911+
let cwd = workspace.clone().into();
912+
let mut parent = make_parent_config();
913+
parent.input_config.negative_globs.insert(Str::from("existing/**"));
914+
let result = resolve_synthetic_cache_config(
915+
ParentCacheConfig::Inherited(parent),
916+
UserCacheConfig::with_config(EnabledCacheConfig {
917+
env: None,
918+
untracked_env: None,
919+
input: None,
920+
}),
921+
&cwd,
922+
&workspace,
923+
)
924+
.unwrap()
925+
.unwrap();
926+
assert_eq!(result.input_config.negative_globs, BTreeSet::from([Str::from("existing/**")]),);
927+
}
928+
929+
#[test]
930+
fn inherited_merges_input_with_existing_parent_globs() {
931+
let workspace = make_workspace_path();
932+
let cwd = workspace.clone().into();
933+
let mut parent = make_parent_config();
934+
parent.input_config.negative_globs.insert(Str::from("dist/**"));
935+
let result = resolve_synthetic_cache_config(
936+
ParentCacheConfig::Inherited(parent),
937+
UserCacheConfig::with_config(EnabledCacheConfig {
938+
env: None,
939+
untracked_env: None,
940+
input: Some(vec![UserInputEntry::Glob(Str::from("!node_modules/.vite-temp/**"))]),
941+
}),
942+
&cwd,
943+
&workspace,
944+
)
945+
.unwrap()
946+
.unwrap();
947+
assert_eq!(
948+
result.input_config.negative_globs,
949+
BTreeSet::from([Str::from("dist/**"), Str::from("node_modules/.vite-temp/**"),]),
950+
);
951+
}
952+
953+
#[test]
954+
fn inherited_input_globs_resolved_relative_to_workspace_root() {
955+
let workspace = make_workspace_path();
956+
// cwd is a subdirectory, but globs should resolve relative to workspace root
957+
let cwd = workspace.join("packages/hello").into();
958+
let result = resolve_synthetic_cache_config(
959+
ParentCacheConfig::Inherited(make_parent_config()),
960+
UserCacheConfig::with_config(EnabledCacheConfig {
961+
env: None,
962+
untracked_env: None,
963+
input: Some(vec![UserInputEntry::Glob(Str::from("!node_modules/.vite-temp/**"))]),
964+
}),
965+
&cwd,
966+
&workspace,
967+
)
968+
.unwrap()
969+
.unwrap();
970+
// The glob should be workspace-root-relative, NOT cwd-relative
971+
// (i.e., "node_modules/.vite-temp/**", NOT "packages/hello/node_modules/.vite-temp/**")
972+
assert!(
973+
result.input_config.negative_globs.contains(&Str::from("node_modules/.vite-temp/**")),
974+
"glob should be workspace-root-relative, got: {:?}",
975+
result.input_config.negative_globs,
976+
);
977+
}
978+
}

0 commit comments

Comments
 (0)