1use std::{
2 collections::BTreeSet,
3 fmt::{Display, Formatter},
4 ops::Range,
5 path::{Path, PathBuf},
6 sync::{Arc, LazyLock},
7};
8
9use anyhow::{Result, anyhow};
10use fs::Fs;
11use futures::StreamExt as _;
12use gpui::{App, AppContext as _, Entity, Subscription, Task};
13use itertools::Itertools;
14use postage::watch;
15use project::Worktree;
16use strum::VariantArray;
17use util::{ResultExt as _, maybe};
18use worktree::ChildEntriesOptions;
19
20/// Matches the most common license locations, with US and UK English spelling.
21static LICENSE_FILE_NAME_REGEX: LazyLock<regex::bytes::Regex> = LazyLock::new(|| {
22 regex::bytes::RegexBuilder::new(
23 "^ \
24 (?: license | licence)? \
25 (?: [\\-._]? \
26 (?: apache (?: [\\-._] (?: 2.0 | 2 ))? | \
27 0? bsd (?: [\\-._] [0123])? (?: [\\-._] clause)? | \
28 isc | \
29 mit | \
30 upl | \
31 zlib))? \
32 (?: [\\-._]? (?: license | licence))? \
33 (?: \\.txt | \\.md)? \
34 $",
35 )
36 .ignore_whitespace(true)
37 .case_insensitive(true)
38 .build()
39 .unwrap()
40});
41
42#[derive(Debug, Clone, Copy, Eq, Ord, PartialOrd, PartialEq, VariantArray)]
43pub enum OpenSourceLicense {
44 Apache2_0,
45 BSDZero,
46 BSD,
47 ISC,
48 MIT,
49 UPL1_0,
50 Zlib,
51}
52
53impl Display for OpenSourceLicense {
54 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
55 write!(f, "{}", self.spdx_identifier())
56 }
57}
58
59impl OpenSourceLicense {
60 /// These are SPDX identifiers for the licenses, except for BSD, where the variants are not
61 /// distinguished.
62 pub fn spdx_identifier(&self) -> &'static str {
63 match self {
64 OpenSourceLicense::Apache2_0 => "apache-2.0",
65 OpenSourceLicense::BSDZero => "0bsd",
66 OpenSourceLicense::BSD => "bsd",
67 OpenSourceLicense::ISC => "isc",
68 OpenSourceLicense::MIT => "mit",
69 OpenSourceLicense::UPL1_0 => "upl-1.0",
70 OpenSourceLicense::Zlib => "zlib",
71 }
72 }
73
74 pub fn patterns(&self) -> &'static [&'static str] {
75 match self {
76 OpenSourceLicense::Apache2_0 => &[
77 include_str!("../license_patterns/apache-2.0-pattern"),
78 include_str!("../license_patterns/apache-2.0-reference-pattern"),
79 ],
80 OpenSourceLicense::BSDZero => &[include_str!("../license_patterns/0bsd-pattern")],
81 OpenSourceLicense::BSD => &[include_str!("../license_patterns/bsd-pattern")],
82 OpenSourceLicense::ISC => &[include_str!("../license_patterns/isc-pattern")],
83 OpenSourceLicense::MIT => &[include_str!("../license_patterns/mit-pattern")],
84 OpenSourceLicense::UPL1_0 => &[include_str!("../license_patterns/upl-1.0-pattern")],
85 OpenSourceLicense::Zlib => &[include_str!("../license_patterns/zlib-pattern")],
86 }
87 }
88}
89
90// TODO: Consider using databake or similar to not parse at runtime.
91static LICENSE_PATTERNS: LazyLock<LicensePatterns> = LazyLock::new(|| {
92 let mut approximate_max_length = 0;
93 let mut patterns = Vec::new();
94 for license in OpenSourceLicense::VARIANTS {
95 for pattern in license.patterns() {
96 let (pattern, length) = parse_pattern(pattern).unwrap();
97 patterns.push((*license, pattern));
98 approximate_max_length = approximate_max_length.max(length);
99 }
100 }
101 LicensePatterns {
102 patterns,
103 approximate_max_length,
104 }
105});
106
107fn detect_license(text: &str) -> Option<OpenSourceLicense> {
108 let text = canonicalize_license_text(text);
109 for (license, pattern) in LICENSE_PATTERNS.patterns.iter() {
110 log::trace!("Checking if license is {}", license);
111 if check_pattern(&pattern, &text) {
112 return Some(*license);
113 }
114 }
115
116 None
117}
118
119struct LicensePatterns {
120 patterns: Vec<(OpenSourceLicense, Vec<PatternPart>)>,
121 approximate_max_length: usize,
122}
123
124#[derive(Debug, Clone, Default, PartialEq, Eq)]
125struct PatternPart {
126 /// Indicates that matching `text` is optional. Skipping `match_any_chars` is conditional on
127 /// matching `text`.
128 optional: bool,
129 /// Indicates the number of characters that can be skipped before matching `text`.
130 match_any_chars: Range<usize>,
131 /// The text to match, may be empty.
132 text: String,
133}
134
135/// Lines that start with "-- " begin a `PatternPart`. `-- 1..10` specifies `match_any_chars:
136/// 1..10`. `-- 1..10 optional:` additionally specifies `optional: true`. It's a parse error for a
137/// line to start with `--` without matching this format.
138///
139/// Text that does not have `--` prefixes participate in the `text` field and are canonicalized by
140/// lowercasing, replacing all runs of whitespace with a single space, and otherwise only keeping
141/// ascii alphanumeric characters.
142fn parse_pattern(pattern_source: &str) -> Result<(Vec<PatternPart>, usize)> {
143 let mut pattern = Vec::new();
144 let mut part = PatternPart::default();
145 let mut approximate_max_length = 0;
146 for line in pattern_source.lines() {
147 if let Some(directive) = line.trim().strip_prefix("--") {
148 if part != PatternPart::default() {
149 pattern.push(part);
150 part = PatternPart::default();
151 }
152 let valid = maybe!({
153 let directive_chunks = directive.split_whitespace().collect::<Vec<_>>();
154 if !(1..=2).contains(&directive_chunks.len()) {
155 return None;
156 }
157 if directive_chunks.len() == 2 {
158 part.optional = true;
159 }
160 let range_chunks = directive_chunks[0].split("..").collect::<Vec<_>>();
161 if range_chunks.len() != 2 {
162 return None;
163 }
164 part.match_any_chars.start = range_chunks[0].parse::<usize>().ok()?;
165 part.match_any_chars.end = range_chunks[1].parse::<usize>().ok()?;
166 if part.match_any_chars.start > part.match_any_chars.end {
167 return None;
168 }
169 approximate_max_length += part.match_any_chars.end;
170 Some(())
171 });
172 if valid.is_none() {
173 return Err(anyhow!("Invalid pattern directive: {}", line));
174 }
175 continue;
176 }
177 approximate_max_length += line.len() + 1;
178 let line = canonicalize_license_text(line);
179 if line.is_empty() {
180 continue;
181 }
182 if !part.text.is_empty() {
183 part.text.push(' ');
184 }
185 part.text.push_str(&line);
186 }
187 if part != PatternPart::default() {
188 pattern.push(part);
189 }
190 Ok((pattern, approximate_max_length))
191}
192
193/// Checks a pattern against text by iterating over the pattern parts in reverse order, and checking
194/// matches with the end of a prefix of the input. Assumes that `canonicalize_license_text` has
195/// already been applied to the input.
196fn check_pattern(pattern: &[PatternPart], input: &str) -> bool {
197 let mut input_ix = input.len();
198 let mut match_any_chars = 0..0;
199 for part in pattern.iter().rev() {
200 if part.text.is_empty() {
201 match_any_chars.start += part.match_any_chars.start;
202 match_any_chars.end += part.match_any_chars.end;
203 continue;
204 }
205 let search_range_start = input_ix.saturating_sub(match_any_chars.end + part.text.len());
206 let search_range_end = input_ix.saturating_sub(match_any_chars.start);
207 let found_ix = &input[search_range_start..search_range_end].rfind(&part.text);
208 if let Some(found_ix) = found_ix {
209 input_ix = search_range_start + found_ix;
210 match_any_chars = part.match_any_chars.clone();
211 } else if !part.optional {
212 log::trace!(
213 "Failed to match pattern `...{}` against input `...{}`",
214 &part.text[part.text.len().saturating_sub(128)..],
215 &input[input_ix.saturating_sub(128)..]
216 );
217 return false;
218 }
219 }
220 match_any_chars.contains(&input_ix)
221}
222
223/// Canonicalizes license text by removing all non-alphanumeric characters, lowercasing, and turning
224/// runs of whitespace into a single space. Unicode alphanumeric characters are intentionally
225/// preserved since these should cause license mismatch when not within a portion of the license
226/// where arbitrary text is allowed.
227fn canonicalize_license_text(license: &str) -> String {
228 license
229 .chars()
230 .filter(|c| c.is_ascii_whitespace() || c.is_alphanumeric())
231 .map(|c| c.to_ascii_lowercase())
232 .collect::<String>()
233 .split_ascii_whitespace()
234 .join(" ")
235}
236
237pub enum LicenseDetectionWatcher {
238 Local {
239 is_open_source_rx: watch::Receiver<bool>,
240 _is_open_source_task: Task<()>,
241 _worktree_subscription: Subscription,
242 },
243 SingleFile,
244 Remote,
245}
246
247impl LicenseDetectionWatcher {
248 pub fn new(worktree: &Entity<Worktree>, cx: &mut App) -> Self {
249 let worktree_ref = worktree.read(cx);
250 if worktree_ref.is_single_file() {
251 return Self::SingleFile;
252 }
253
254 let (files_to_check_tx, mut files_to_check_rx) = futures::channel::mpsc::unbounded();
255
256 let Worktree::Local(local_worktree) = worktree_ref else {
257 return Self::Remote;
258 };
259 let fs = local_worktree.fs().clone();
260 let worktree_abs_path = local_worktree.abs_path().clone();
261
262 let options = ChildEntriesOptions {
263 include_files: true,
264 include_dirs: false,
265 include_ignored: true,
266 };
267 for top_file in local_worktree.child_entries_with_options(Path::new(""), options) {
268 let path_bytes = top_file.path.as_os_str().as_encoded_bytes();
269 if top_file.is_created() && LICENSE_FILE_NAME_REGEX.is_match(path_bytes) {
270 let rel_path = top_file.path.clone();
271 files_to_check_tx.unbounded_send(rel_path).ok();
272 }
273 }
274
275 let _worktree_subscription =
276 cx.subscribe(worktree, move |_worktree, event, _cx| match event {
277 worktree::Event::UpdatedEntries(updated_entries) => {
278 for updated_entry in updated_entries.iter() {
279 let rel_path = &updated_entry.0;
280 let path_bytes = rel_path.as_os_str().as_encoded_bytes();
281 if LICENSE_FILE_NAME_REGEX.is_match(path_bytes) {
282 files_to_check_tx.unbounded_send(rel_path.clone()).ok();
283 }
284 }
285 }
286 worktree::Event::DeletedEntry(_) | worktree::Event::UpdatedGitRepositories(_) => {}
287 });
288
289 let (mut is_open_source_tx, is_open_source_rx) = watch::channel_with::<bool>(false);
290
291 let _is_open_source_task = cx.background_spawn(async move {
292 let mut eligible_licenses = BTreeSet::new();
293 while let Some(rel_path) = files_to_check_rx.next().await {
294 let abs_path = worktree_abs_path.join(&rel_path);
295 let was_open_source = !eligible_licenses.is_empty();
296 if Self::is_path_eligible(&fs, abs_path).await.unwrap_or(false) {
297 eligible_licenses.insert(rel_path);
298 } else {
299 eligible_licenses.remove(&rel_path);
300 }
301 let is_open_source = !eligible_licenses.is_empty();
302 if is_open_source != was_open_source {
303 *is_open_source_tx.borrow_mut() = is_open_source;
304 }
305 }
306 });
307
308 Self::Local {
309 is_open_source_rx,
310 _is_open_source_task,
311 _worktree_subscription,
312 }
313 }
314
315 async fn is_path_eligible(fs: &Arc<dyn Fs>, abs_path: PathBuf) -> Option<bool> {
316 log::debug!("checking if `{abs_path:?}` is an open source license");
317 // resolve symlinks so that the file size from metadata is correct
318 let Some(abs_path) = fs.canonicalize(&abs_path).await.ok() else {
319 log::debug!(
320 "`{abs_path:?}` license file probably deleted (error canonicalizing the path)"
321 );
322 return None;
323 };
324 let metadata = fs.metadata(&abs_path).await.log_err()??;
325 if metadata.len > LICENSE_PATTERNS.approximate_max_length as u64 {
326 log::debug!(
327 "`{abs_path:?}` license file was skipped \
328 because its size of {} bytes was larger than the max size of {} bytes",
329 metadata.len,
330 LICENSE_PATTERNS.approximate_max_length
331 );
332 return None;
333 }
334 let text = fs.load(&abs_path).await.log_err()?;
335 let is_eligible = detect_license(&text).is_some();
336 if is_eligible {
337 log::debug!(
338 "`{abs_path:?}` matches a license that is eligible for data collection (if enabled)"
339 );
340 } else {
341 log::debug!(
342 "`{abs_path:?}` does not match a license that is eligible for data collection"
343 );
344 }
345 Some(is_eligible)
346 }
347
348 /// Answers false until we find out it's open source
349 pub fn is_project_open_source(&self) -> bool {
350 match self {
351 Self::Local {
352 is_open_source_rx, ..
353 } => *is_open_source_rx.borrow(),
354 Self::SingleFile | Self::Remote => false,
355 }
356 }
357}
358
359#[cfg(test)]
360mod tests {
361
362 use fs::FakeFs;
363 use gpui::TestAppContext;
364 use serde_json::json;
365 use settings::{Settings as _, SettingsStore};
366 use worktree::WorktreeSettings;
367
368 use super::*;
369
370 const APACHE_2_0_TXT: &str = include_str!("../license_examples/apache-2.0-ex0.txt");
371 const ISC_TXT: &str = include_str!("../license_examples/isc.txt");
372 const MIT_TXT: &str = include_str!("../license_examples/mit-ex0.txt");
373 const UPL_1_0_TXT: &str = include_str!("../license_examples/upl-1.0.txt");
374 const BSD_0_TXT: &str = include_str!("../license_examples/0bsd.txt");
375
376 #[track_caller]
377 fn assert_matches_license(text: &str, license: OpenSourceLicense) {
378 assert_eq!(detect_license(text), Some(license));
379 assert!(text.len() < LICENSE_PATTERNS.approximate_max_length);
380 }
381
382 /*
383 // Uncomment this and run with `cargo test -p zeta -- --no-capture &> licenses-output` to
384 // traverse your entire home directory and run license detection on every file that has a
385 // license-like name.
386 #[test]
387 fn test_check_all_licenses_in_home_dir() {
388 let mut detected = Vec::new();
389 let mut unrecognized = Vec::new();
390 let mut walked_entries = 0;
391 let homedir = std::env::home_dir().unwrap();
392 for entry in walkdir::WalkDir::new(&homedir) {
393 walked_entries += 1;
394 if walked_entries % 10000 == 0 {
395 println!(
396 "So far visited {} files in {}",
397 walked_entries,
398 homedir.display()
399 );
400 }
401 let Ok(entry) = entry else {
402 continue;
403 };
404 if !LICENSE_FILE_NAME_REGEX.is_match(entry.file_name().as_encoded_bytes()) {
405 continue;
406 }
407 let Ok(contents) = std::fs::read_to_string(entry.path()) else {
408 continue;
409 };
410 let path_string = entry.path().to_string_lossy().to_string();
411 let license = detect_license(&contents);
412 match license {
413 Some(license) => detected.push((license, path_string)),
414 None => unrecognized.push(path_string),
415 }
416 }
417 println!("\nDetected licenses:\n");
418 detected.sort();
419 for (license, path) in &detected {
420 println!("{}: {}", license.spdx_identifier(), path);
421 }
422 println!("\nUnrecognized licenses:\n");
423 for path in &unrecognized {
424 println!("{}", path);
425 }
426 panic!(
427 "{} licenses detected, {} unrecognized",
428 detected.len(),
429 unrecognized.len()
430 );
431 println!("This line has a warning to make sure this test is always commented out");
432 }
433 */
434
435 #[test]
436 fn test_apache_positive_detection() {
437 assert_matches_license(APACHE_2_0_TXT, OpenSourceLicense::Apache2_0);
438 assert_matches_license(
439 include_str!("../license_examples/apache-2.0-ex1.txt"),
440 OpenSourceLicense::Apache2_0,
441 );
442 assert_matches_license(
443 include_str!("../license_examples/apache-2.0-ex2.txt"),
444 OpenSourceLicense::Apache2_0,
445 );
446 assert_matches_license(
447 include_str!("../license_examples/apache-2.0-ex3.txt"),
448 OpenSourceLicense::Apache2_0,
449 );
450 assert_matches_license(
451 include_str!("../license_examples/apache-2.0-ex4.txt"),
452 OpenSourceLicense::Apache2_0,
453 );
454 assert_matches_license(
455 include_str!("../../../LICENSE-APACHE"),
456 OpenSourceLicense::Apache2_0,
457 );
458 }
459
460 #[test]
461 fn test_apache_negative_detection() {
462 assert_eq!(
463 detect_license(&format!(
464 "{APACHE_2_0_TXT}\n\nThe terms in this license are void if P=NP."
465 )),
466 None
467 );
468 }
469
470 #[test]
471 fn test_bsd_1_clause_positive_detection() {
472 assert_matches_license(
473 include_str!("../license_examples/bsd-1-clause.txt"),
474 OpenSourceLicense::BSD,
475 );
476 }
477
478 #[test]
479 fn test_bsd_2_clause_positive_detection() {
480 assert_matches_license(
481 include_str!("../license_examples/bsd-2-clause-ex0.txt"),
482 OpenSourceLicense::BSD,
483 );
484 }
485
486 #[test]
487 fn test_bsd_3_clause_positive_detection() {
488 assert_matches_license(
489 include_str!("../license_examples/bsd-3-clause-ex0.txt"),
490 OpenSourceLicense::BSD,
491 );
492 assert_matches_license(
493 include_str!("../license_examples/bsd-3-clause-ex1.txt"),
494 OpenSourceLicense::BSD,
495 );
496 assert_matches_license(
497 include_str!("../license_examples/bsd-3-clause-ex2.txt"),
498 OpenSourceLicense::BSD,
499 );
500 assert_matches_license(
501 include_str!("../license_examples/bsd-3-clause-ex3.txt"),
502 OpenSourceLicense::BSD,
503 );
504 assert_matches_license(
505 include_str!("../license_examples/bsd-3-clause-ex4.txt"),
506 OpenSourceLicense::BSD,
507 );
508 }
509
510 #[test]
511 fn test_bsd_0_positive_detection() {
512 assert_matches_license(BSD_0_TXT, OpenSourceLicense::BSDZero);
513 }
514
515 #[test]
516 fn test_isc_positive_detection() {
517 assert_matches_license(ISC_TXT, OpenSourceLicense::ISC);
518 }
519
520 #[test]
521 fn test_isc_negative_detection() {
522 let license_text = format!(
523 r#"{ISC_TXT}
524
525 This project is dual licensed under the ISC License and the MIT License."#
526 );
527
528 assert_eq!(detect_license(&license_text), None);
529 }
530
531 #[test]
532 fn test_mit_positive_detection() {
533 assert_matches_license(MIT_TXT, OpenSourceLicense::MIT);
534 assert_matches_license(
535 include_str!("../license_examples/mit-ex1.txt"),
536 OpenSourceLicense::MIT,
537 );
538 assert_matches_license(
539 include_str!("../license_examples/mit-ex2.txt"),
540 OpenSourceLicense::MIT,
541 );
542 assert_matches_license(
543 include_str!("../license_examples/mit-ex3.txt"),
544 OpenSourceLicense::MIT,
545 );
546 }
547
548 #[test]
549 fn test_mit_negative_detection() {
550 let license_text = format!(
551 r#"{MIT_TXT}
552
553 This project is dual licensed under the MIT License and the Apache License, Version 2.0."#
554 );
555 assert_eq!(detect_license(&license_text), None);
556 }
557
558 #[test]
559 fn test_upl_positive_detection() {
560 assert_matches_license(UPL_1_0_TXT, OpenSourceLicense::UPL1_0);
561 }
562
563 #[test]
564 fn test_upl_negative_detection() {
565 let license_text = format!(
566 r#"{UPL_1_0_TXT}
567
568 This project is dual licensed under the UPL License and the MIT License."#
569 );
570
571 assert_eq!(detect_license(&license_text), None);
572 }
573
574 #[test]
575 fn test_zlib_positive_detection() {
576 assert_matches_license(
577 include_str!("../license_examples/zlib-ex0.txt"),
578 OpenSourceLicense::Zlib,
579 );
580 }
581
582 #[test]
583 fn test_license_file_name_regex() {
584 // Test basic license file names
585 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE"));
586 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE"));
587 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"license"));
588 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"licence"));
589
590 // Test with extensions
591 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.txt"));
592 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.md"));
593 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE.txt"));
594 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE.md"));
595
596 // Test with specific license types
597 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-APACHE"));
598 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-MIT"));
599 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.MIT"));
600 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE_MIT"));
601 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-ISC"));
602 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-UPL"));
603
604 // Test with "license" coming after
605 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"APACHE-LICENSE"));
606
607 // Test version numbers
608 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"APACHE-2"));
609 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"APACHE-2.0"));
610 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"BSD-1"));
611 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"BSD-2"));
612 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"BSD-3"));
613 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"BSD-3-CLAUSE"));
614
615 // Test combinations
616 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-MIT.txt"));
617 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE.ISC.md"));
618 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"license_upl"));
619 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.APACHE.2.0"));
620
621 // Test case insensitive
622 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"License"));
623 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"license-mit.TXT"));
624 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE_isc.MD"));
625
626 // Test edge cases that should match
627 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"license.mit"));
628 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"licence-upl.txt"));
629
630 // Test non-matching patterns
631 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"COPYING"));
632 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.html"));
633 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"MYLICENSE"));
634 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"src/LICENSE"));
635 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.old"));
636 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-GPL"));
637 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"LICENSEABC"));
638 }
639
640 #[test]
641 fn test_canonicalize_license_text() {
642 let input = " Paragraph 1\nwith multiple lines\n\n\n\nParagraph 2\nwith more lines\n ";
643 let expected = "paragraph 1 with multiple lines paragraph 2 with more lines";
644 assert_eq!(canonicalize_license_text(input), expected);
645
646 // Test tabs and mixed whitespace
647 let input = "Word1\t\tWord2\n\n Word3\r\n\r\n\r\nWord4 ";
648 let expected = "word1 word2 word3 word4";
649 assert_eq!(canonicalize_license_text(input), expected);
650 }
651
652 fn init_test(cx: &mut TestAppContext) {
653 cx.update(|cx| {
654 let settings_store = SettingsStore::test(cx);
655 cx.set_global(settings_store);
656 WorktreeSettings::register(cx);
657 });
658 }
659
660 #[gpui::test]
661 async fn test_watcher_single_file(cx: &mut TestAppContext) {
662 init_test(cx);
663
664 let fs = FakeFs::new(cx.background_executor.clone());
665 fs.insert_tree("/root", json!({ "main.rs": "fn main() {}" }))
666 .await;
667
668 let worktree = Worktree::local(
669 Path::new("/root/main.rs"),
670 true,
671 fs.clone(),
672 Default::default(),
673 &mut cx.to_async(),
674 )
675 .await
676 .unwrap();
677
678 let watcher = cx.update(|cx| LicenseDetectionWatcher::new(&worktree, cx));
679 assert!(matches!(watcher, LicenseDetectionWatcher::SingleFile));
680 assert!(!watcher.is_project_open_source());
681 }
682
683 #[gpui::test]
684 async fn test_watcher_updates_on_changes(cx: &mut TestAppContext) {
685 init_test(cx);
686
687 let fs = FakeFs::new(cx.background_executor.clone());
688 fs.insert_tree("/root", json!({ "main.rs": "fn main() {}" }))
689 .await;
690
691 let worktree = Worktree::local(
692 Path::new("/root"),
693 true,
694 fs.clone(),
695 Default::default(),
696 &mut cx.to_async(),
697 )
698 .await
699 .unwrap();
700
701 let watcher = cx.update(|cx| LicenseDetectionWatcher::new(&worktree, cx));
702 assert!(matches!(watcher, LicenseDetectionWatcher::Local { .. }));
703 assert!(!watcher.is_project_open_source());
704
705 fs.write(Path::new("/root/LICENSE-MIT"), MIT_TXT.as_bytes())
706 .await
707 .unwrap();
708
709 cx.background_executor.run_until_parked();
710 assert!(watcher.is_project_open_source());
711
712 fs.write(Path::new("/root/LICENSE-APACHE"), APACHE_2_0_TXT.as_bytes())
713 .await
714 .unwrap();
715
716 cx.background_executor.run_until_parked();
717 assert!(watcher.is_project_open_source());
718
719 fs.write(Path::new("/root/LICENSE-MIT"), "Nevermind".as_bytes())
720 .await
721 .unwrap();
722
723 // Still considered open source as LICENSE-APACHE is present
724 cx.background_executor.run_until_parked();
725 assert!(watcher.is_project_open_source());
726
727 fs.write(
728 Path::new("/root/LICENSE-APACHE"),
729 "Also nevermind".as_bytes(),
730 )
731 .await
732 .unwrap();
733
734 cx.background_executor.run_until_parked();
735 assert!(!watcher.is_project_open_source());
736 }
737
738 #[gpui::test]
739 async fn test_watcher_initially_opensource_and_then_deleted(cx: &mut TestAppContext) {
740 init_test(cx);
741
742 let fs = FakeFs::new(cx.background_executor.clone());
743 fs.insert_tree(
744 "/root",
745 json!({ "main.rs": "fn main() {}", "LICENSE-MIT": MIT_TXT }),
746 )
747 .await;
748
749 let worktree = Worktree::local(
750 Path::new("/root"),
751 true,
752 fs.clone(),
753 Default::default(),
754 &mut cx.to_async(),
755 )
756 .await
757 .unwrap();
758
759 let watcher = cx.update(|cx| LicenseDetectionWatcher::new(&worktree, cx));
760 assert!(matches!(watcher, LicenseDetectionWatcher::Local { .. }));
761
762 cx.background_executor.run_until_parked();
763 assert!(watcher.is_project_open_source());
764
765 fs.remove_file(
766 Path::new("/root/LICENSE-MIT"),
767 fs::RemoveOptions {
768 recursive: false,
769 ignore_if_not_exists: false,
770 },
771 )
772 .await
773 .unwrap();
774
775 cx.background_executor.run_until_parked();
776 assert!(!watcher.is_project_open_source());
777 }
778}