1use std::path::Path;
2use util::{
3 paths::{PathMatcher, PathStyle},
4 rel_path::RelPath,
5};
6use worktree::*;
7
8fn make_settings_with_read_only(patterns: &[&str]) -> WorktreeSettings {
9 WorktreeSettings {
10 prevent_sharing_in_public_channels: false,
11 file_scan_exclusions: PathMatcher::default(),
12 file_scan_inclusions: PathMatcher::default(),
13 parent_dir_scan_inclusions: PathMatcher::default(),
14 private_files: PathMatcher::default(),
15 hidden_files: PathMatcher::default(),
16 read_only_files: PathMatcher::new(
17 patterns.iter().map(|s| s.to_string()),
18 PathStyle::local(),
19 )
20 .unwrap(),
21 }
22}
23
24#[test]
25fn test_is_path_read_only_with_glob_patterns() {
26 let settings = make_settings_with_read_only(&["**/generated/**", "**/*.gen.rs"]);
27
28 let generated_file =
29 RelPath::new(Path::new("src/generated/schema.rs"), PathStyle::local()).unwrap();
30 assert!(
31 settings.is_path_read_only(&generated_file),
32 "Files in generated directory should be read-only"
33 );
34
35 let gen_rs_file = RelPath::new(Path::new("src/types.gen.rs"), PathStyle::local()).unwrap();
36 assert!(
37 settings.is_path_read_only(&gen_rs_file),
38 "Files with .gen.rs extension should be read-only"
39 );
40
41 let regular_file = RelPath::new(Path::new("src/main.rs"), PathStyle::local()).unwrap();
42 assert!(
43 !settings.is_path_read_only(®ular_file),
44 "Regular files should not be read-only"
45 );
46
47 let similar_name = RelPath::new(Path::new("src/generator.rs"), PathStyle::local()).unwrap();
48 assert!(
49 !settings.is_path_read_only(&similar_name),
50 "Files with 'generator' in name but not in generated dir should not be read-only"
51 );
52}
53
54#[test]
55fn test_is_path_read_only_with_specific_paths() {
56 let settings = make_settings_with_read_only(&["vendor/**", "node_modules/**"]);
57
58 let vendor_file = RelPath::new(Path::new("vendor/lib/package.js"), PathStyle::local()).unwrap();
59 assert!(
60 settings.is_path_read_only(&vendor_file),
61 "Files in vendor directory should be read-only"
62 );
63
64 let node_modules_file = RelPath::new(
65 Path::new("node_modules/lodash/index.js"),
66 PathStyle::local(),
67 )
68 .unwrap();
69 assert!(
70 settings.is_path_read_only(&node_modules_file),
71 "Files in node_modules should be read-only"
72 );
73
74 let src_file = RelPath::new(Path::new("src/app.js"), PathStyle::local()).unwrap();
75 assert!(
76 !settings.is_path_read_only(&src_file),
77 "Files in src should not be read-only"
78 );
79}
80
81#[test]
82fn test_is_path_read_only_empty_patterns() {
83 let settings = make_settings_with_read_only(&[]);
84
85 let any_file = RelPath::new(Path::new("src/main.rs"), PathStyle::local()).unwrap();
86 assert!(
87 !settings.is_path_read_only(&any_file),
88 "No files should be read-only when patterns are empty"
89 );
90}
91
92#[test]
93fn test_is_path_read_only_with_extension_pattern() {
94 let settings = make_settings_with_read_only(&["**/*.lock", "**/*.min.js"]);
95
96 let lock_file = RelPath::new(Path::new("Cargo.lock"), PathStyle::local()).unwrap();
97 assert!(
98 settings.is_path_read_only(&lock_file),
99 "Lock files should be read-only"
100 );
101
102 let nested_lock =
103 RelPath::new(Path::new("packages/app/yarn.lock"), PathStyle::local()).unwrap();
104 assert!(
105 settings.is_path_read_only(&nested_lock),
106 "Nested lock files should be read-only"
107 );
108
109 let minified_js = RelPath::new(Path::new("dist/bundle.min.js"), PathStyle::local()).unwrap();
110 assert!(
111 settings.is_path_read_only(&minified_js),
112 "Minified JS files should be read-only"
113 );
114
115 let regular_js = RelPath::new(Path::new("src/app.js"), PathStyle::local()).unwrap();
116 assert!(
117 !settings.is_path_read_only(®ular_js),
118 "Regular JS files should not be read-only"
119 );
120}