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