1use std::{
2 collections::BTreeSet,
3 fmt::{Display, Formatter},
4 ops::Range,
5 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, rel_path::RelPath};
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
206 let search_range_end = n_chars_before_offset(match_any_chars.start, input_ix, input);
207 let search_range_start = n_chars_before_offset(
208 match_any_chars.len() + part.text.len(),
209 search_range_end,
210 input,
211 );
212 let found_ix = input[search_range_start..search_range_end].rfind(&part.text);
213
214 if let Some(found_ix) = found_ix {
215 input_ix = search_range_start + found_ix;
216 match_any_chars = part.match_any_chars.clone();
217 } else if !part.optional {
218 log::trace!(
219 "Failed to match pattern\n`...{}`\nagainst input\n`...{}`",
220 &part.text[n_chars_before_offset(128, part.text.len(), &part.text)..],
221 &input[n_chars_before_offset(128, search_range_end, input)..search_range_end],
222 );
223 return false;
224 }
225 }
226 is_char_count_within_range(&input[..input_ix], match_any_chars)
227}
228
229fn n_chars_before_offset(char_count: usize, offset: usize, string: &str) -> usize {
230 if char_count == 0 {
231 return offset;
232 }
233 string[..offset]
234 .char_indices()
235 .nth_back(char_count.saturating_sub(1))
236 .map_or(0, |(byte_ix, _)| byte_ix)
237}
238
239fn is_char_count_within_range(string: &str, char_count_range: Range<usize>) -> bool {
240 if string.len() >= char_count_range.start * 4 && string.len() < char_count_range.end {
241 return true;
242 }
243 if string.len() < char_count_range.start || string.len() >= char_count_range.end * 4 {
244 return false;
245 }
246 char_count_range.contains(&string.chars().count())
247}
248
249/// Canonicalizes license text by removing all non-alphanumeric characters, lowercasing, and turning
250/// runs of whitespace into a single space. Unicode alphanumeric characters are intentionally
251/// preserved since these should cause license mismatch when not within a portion of the license
252/// where arbitrary text is allowed.
253fn canonicalize_license_text(license: &str) -> String {
254 license
255 .chars()
256 .filter(|c| c.is_ascii_whitespace() || c.is_alphanumeric())
257 .map(|c| c.to_ascii_lowercase())
258 .collect::<String>()
259 .split_ascii_whitespace()
260 .join(" ")
261}
262
263pub enum LicenseDetectionWatcher {
264 Local {
265 is_open_source_rx: watch::Receiver<bool>,
266 _is_open_source_task: Task<()>,
267 _worktree_subscription: Subscription,
268 },
269 SingleFile,
270 Remote,
271}
272
273impl LicenseDetectionWatcher {
274 pub fn new(worktree: &Entity<Worktree>, cx: &mut App) -> Self {
275 let worktree_ref = worktree.read(cx);
276 if worktree_ref.is_single_file() {
277 return Self::SingleFile;
278 }
279
280 let (files_to_check_tx, mut files_to_check_rx) = futures::channel::mpsc::unbounded();
281
282 let Worktree::Local(local_worktree) = worktree_ref else {
283 return Self::Remote;
284 };
285 let fs = local_worktree.fs().clone();
286
287 let options = ChildEntriesOptions {
288 include_files: true,
289 include_dirs: false,
290 include_ignored: true,
291 };
292 for top_file in local_worktree.child_entries_with_options(RelPath::empty(), options) {
293 let path_bytes = top_file.path.as_unix_str().as_bytes();
294 if top_file.is_created() && LICENSE_FILE_NAME_REGEX.is_match(path_bytes) {
295 let rel_path = top_file.path.clone();
296 files_to_check_tx.unbounded_send(rel_path).ok();
297 }
298 }
299
300 let _worktree_subscription =
301 cx.subscribe(worktree, move |_worktree, event, _cx| match event {
302 worktree::Event::UpdatedEntries(updated_entries) => {
303 for updated_entry in updated_entries.iter() {
304 let rel_path = &updated_entry.0;
305 let path_bytes = rel_path.as_unix_str().as_bytes();
306 if LICENSE_FILE_NAME_REGEX.is_match(path_bytes) {
307 files_to_check_tx.unbounded_send(rel_path.clone()).ok();
308 }
309 }
310 }
311 worktree::Event::DeletedEntry(_) | worktree::Event::UpdatedGitRepositories(_) => {}
312 });
313
314 let worktree_snapshot = worktree.read(cx).snapshot();
315 let (mut is_open_source_tx, is_open_source_rx) = watch::channel_with::<bool>(false);
316
317 let _is_open_source_task = cx.background_spawn(async move {
318 let mut eligible_licenses = BTreeSet::new();
319 while let Some(rel_path) = files_to_check_rx.next().await {
320 let abs_path = worktree_snapshot.absolutize(&rel_path);
321 let was_open_source = !eligible_licenses.is_empty();
322 if Self::is_path_eligible(&fs, abs_path).await.unwrap_or(false) {
323 eligible_licenses.insert(rel_path);
324 } else {
325 eligible_licenses.remove(&rel_path);
326 }
327 let is_open_source = !eligible_licenses.is_empty();
328 if is_open_source != was_open_source {
329 *is_open_source_tx.borrow_mut() = is_open_source;
330 }
331 }
332 });
333
334 Self::Local {
335 is_open_source_rx,
336 _is_open_source_task,
337 _worktree_subscription,
338 }
339 }
340
341 async fn is_path_eligible(fs: &Arc<dyn Fs>, abs_path: PathBuf) -> Option<bool> {
342 log::debug!("checking if `{abs_path:?}` is an open source license");
343 // resolve symlinks so that the file size from metadata is correct
344 let Some(abs_path) = fs.canonicalize(&abs_path).await.ok() else {
345 log::debug!(
346 "`{abs_path:?}` license file probably deleted (error canonicalizing the path)"
347 );
348 return None;
349 };
350 let metadata = fs.metadata(&abs_path).await.log_err()??;
351 if metadata.len > LICENSE_PATTERNS.approximate_max_length as u64 {
352 log::debug!(
353 "`{abs_path:?}` license file was skipped \
354 because its size of {} bytes was larger than the max size of {} bytes",
355 metadata.len,
356 LICENSE_PATTERNS.approximate_max_length
357 );
358 return None;
359 }
360 let text = fs.load(&abs_path).await.log_err()?;
361 let is_eligible = detect_license(&text).is_some();
362 if is_eligible {
363 log::debug!(
364 "`{abs_path:?}` matches a license that is eligible for data collection (if enabled)"
365 );
366 } else {
367 log::debug!(
368 "`{abs_path:?}` does not match a license that is eligible for data collection"
369 );
370 }
371 Some(is_eligible)
372 }
373
374 /// Answers false until we find out it's open source
375 pub fn is_project_open_source(&self) -> bool {
376 match self {
377 Self::Local {
378 is_open_source_rx, ..
379 } => *is_open_source_rx.borrow(),
380 Self::SingleFile | Self::Remote => false,
381 }
382 }
383}
384
385#[cfg(test)]
386mod tests {
387 use std::path::Path;
388
389 use fs::FakeFs;
390 use gpui::TestAppContext;
391 use rand::Rng as _;
392 use serde_json::json;
393 use settings::SettingsStore;
394
395 use super::*;
396
397 const APACHE_2_0_TXT: &str = include_str!("../license_examples/apache-2.0-ex0.txt");
398 const ISC_TXT: &str = include_str!("../license_examples/isc.txt");
399 const MIT_TXT: &str = include_str!("../license_examples/mit-ex0.txt");
400 const UPL_1_0_TXT: &str = include_str!("../license_examples/upl-1.0.txt");
401 const BSD_0_TXT: &str = include_str!("../license_examples/0bsd.txt");
402
403 #[track_caller]
404 fn assert_matches_license(text: &str, license: OpenSourceLicense) {
405 assert_eq!(detect_license(text), Some(license));
406 assert!(text.len() < LICENSE_PATTERNS.approximate_max_length);
407 }
408
409 /*
410 // Uncomment this and run with `cargo test -p zeta -- --no-capture &> licenses-output` to
411 // traverse your entire home directory and run license detection on every file that has a
412 // license-like name.
413 #[test]
414 fn test_check_all_licenses_in_home_dir() {
415 let mut detected = Vec::new();
416 let mut unrecognized = Vec::new();
417 let mut walked_entries = 0;
418 let homedir = std::env::home_dir().unwrap();
419 for entry in walkdir::WalkDir::new(&homedir) {
420 walked_entries += 1;
421 if walked_entries % 10000 == 0 {
422 println!(
423 "So far visited {} files in {}",
424 walked_entries,
425 homedir.display()
426 );
427 }
428 let Ok(entry) = entry else {
429 continue;
430 };
431 if !LICENSE_FILE_NAME_REGEX.is_match(entry.file_name().as_encoded_bytes()) {
432 continue;
433 }
434 let Ok(contents) = std::fs::read_to_string(entry.path()) else {
435 continue;
436 };
437 let path_string = entry.path().to_string_lossy().into_owned();
438 let license = detect_license(&contents);
439 match license {
440 Some(license) => detected.push((license, path_string)),
441 None => unrecognized.push(path_string),
442 }
443 }
444 println!("\nDetected licenses:\n");
445 detected.sort();
446 for (license, path) in &detected {
447 println!("{}: {}", license.spdx_identifier(), path);
448 }
449 println!("\nUnrecognized licenses:\n");
450 for path in &unrecognized {
451 println!("{}", path);
452 }
453 panic!(
454 "{} licenses detected, {} unrecognized",
455 detected.len(),
456 unrecognized.len()
457 );
458 println!("This line has a warning to make sure this test is always commented out");
459 }
460 */
461
462 #[test]
463 fn test_apache_positive_detection() {
464 assert_matches_license(APACHE_2_0_TXT, OpenSourceLicense::Apache2_0);
465 assert_matches_license(
466 include_str!("../license_examples/apache-2.0-ex1.txt"),
467 OpenSourceLicense::Apache2_0,
468 );
469 assert_matches_license(
470 include_str!("../license_examples/apache-2.0-ex2.txt"),
471 OpenSourceLicense::Apache2_0,
472 );
473 assert_matches_license(
474 include_str!("../license_examples/apache-2.0-ex3.txt"),
475 OpenSourceLicense::Apache2_0,
476 );
477 assert_matches_license(
478 include_str!("../license_examples/apache-2.0-ex4.txt"),
479 OpenSourceLicense::Apache2_0,
480 );
481 assert_matches_license(
482 include_str!("../../../LICENSE-APACHE"),
483 OpenSourceLicense::Apache2_0,
484 );
485 }
486
487 #[test]
488 fn test_apache_negative_detection() {
489 assert_eq!(
490 detect_license(&format!(
491 "{APACHE_2_0_TXT}\n\nThe terms in this license are void if P=NP."
492 )),
493 None
494 );
495 }
496
497 #[test]
498 fn test_bsd_1_clause_positive_detection() {
499 assert_matches_license(
500 include_str!("../license_examples/bsd-1-clause.txt"),
501 OpenSourceLicense::BSD,
502 );
503 }
504
505 #[test]
506 fn test_bsd_2_clause_positive_detection() {
507 assert_matches_license(
508 include_str!("../license_examples/bsd-2-clause-ex0.txt"),
509 OpenSourceLicense::BSD,
510 );
511 }
512
513 #[test]
514 fn test_bsd_3_clause_positive_detection() {
515 assert_matches_license(
516 include_str!("../license_examples/bsd-3-clause-ex0.txt"),
517 OpenSourceLicense::BSD,
518 );
519 assert_matches_license(
520 include_str!("../license_examples/bsd-3-clause-ex1.txt"),
521 OpenSourceLicense::BSD,
522 );
523 assert_matches_license(
524 include_str!("../license_examples/bsd-3-clause-ex2.txt"),
525 OpenSourceLicense::BSD,
526 );
527 assert_matches_license(
528 include_str!("../license_examples/bsd-3-clause-ex3.txt"),
529 OpenSourceLicense::BSD,
530 );
531 assert_matches_license(
532 include_str!("../license_examples/bsd-3-clause-ex4.txt"),
533 OpenSourceLicense::BSD,
534 );
535 }
536
537 #[test]
538 fn test_bsd_0_positive_detection() {
539 assert_matches_license(BSD_0_TXT, OpenSourceLicense::BSDZero);
540 }
541
542 #[test]
543 fn test_isc_positive_detection() {
544 assert_matches_license(ISC_TXT, OpenSourceLicense::ISC);
545 }
546
547 #[test]
548 fn test_isc_negative_detection() {
549 let license_text = format!(
550 r#"{ISC_TXT}
551
552 This project is dual licensed under the ISC License and the MIT License."#
553 );
554
555 assert_eq!(detect_license(&license_text), None);
556 }
557
558 #[test]
559 fn test_mit_positive_detection() {
560 assert_matches_license(MIT_TXT, OpenSourceLicense::MIT);
561 assert_matches_license(
562 include_str!("../license_examples/mit-ex1.txt"),
563 OpenSourceLicense::MIT,
564 );
565 assert_matches_license(
566 include_str!("../license_examples/mit-ex2.txt"),
567 OpenSourceLicense::MIT,
568 );
569 assert_matches_license(
570 include_str!("../license_examples/mit-ex3.txt"),
571 OpenSourceLicense::MIT,
572 );
573 }
574
575 #[test]
576 fn test_mit_negative_detection() {
577 let license_text = format!(
578 r#"{MIT_TXT}
579
580 This project is dual licensed under the MIT License and the Apache License, Version 2.0."#
581 );
582 assert_eq!(detect_license(&license_text), None);
583 }
584
585 #[test]
586 fn test_upl_positive_detection() {
587 assert_matches_license(UPL_1_0_TXT, OpenSourceLicense::UPL1_0);
588 }
589
590 #[test]
591 fn test_upl_negative_detection() {
592 let license_text = format!(
593 r#"{UPL_1_0_TXT}
594
595 This project is dual licensed under the UPL License and the MIT License."#
596 );
597
598 assert_eq!(detect_license(&license_text), None);
599 }
600
601 #[test]
602 fn test_zlib_positive_detection() {
603 assert_matches_license(
604 include_str!("../license_examples/zlib-ex0.txt"),
605 OpenSourceLicense::Zlib,
606 );
607 }
608
609 #[test]
610 fn random_strings_negative_detection() {
611 for _i in 0..20 {
612 let random_string = rand::rng()
613 .sample_iter::<char, _>(rand::distr::StandardUniform)
614 .take(512)
615 .collect::<String>();
616 assert_eq!(detect_license(&random_string), None);
617 }
618 }
619
620 #[test]
621 fn test_n_chars_before_offset() {
622 assert_eq!(n_chars_before_offset(2, 4, "hello"), 2);
623
624 let input = "ㄒ乇丂ㄒ";
625 assert_eq!(n_chars_before_offset(2, input.len(), input), "ㄒ乇".len());
626 }
627
628 #[test]
629 fn test_is_char_count_within_range() {
630 // TODO: make this into a proper property test.
631 for _i in 0..20 {
632 let mut rng = rand::rng();
633 let random_char_count = rng.random_range(0..64);
634 let random_string = rand::rng()
635 .sample_iter::<char, _>(rand::distr::StandardUniform)
636 .take(random_char_count)
637 .collect::<String>();
638 let min_chars = rng.random_range(0..10);
639 let max_chars = rng.random_range(min_chars..32);
640 let char_count_range = min_chars..max_chars;
641 assert_eq!(
642 is_char_count_within_range(&random_string, char_count_range.clone()),
643 char_count_range.contains(&random_char_count),
644 );
645 }
646 }
647
648 #[test]
649 fn test_license_file_name_regex() {
650 // Test basic license file names
651 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE"));
652 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE"));
653 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"license"));
654 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"licence"));
655
656 // Test with extensions
657 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.txt"));
658 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.md"));
659 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE.txt"));
660 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE.md"));
661
662 // Test with specific license types
663 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-APACHE"));
664 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-MIT"));
665 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.MIT"));
666 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE_MIT"));
667 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-ISC"));
668 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-UPL"));
669
670 // Test with "license" coming after
671 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"APACHE-LICENSE"));
672
673 // Test version numbers
674 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"APACHE-2"));
675 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"APACHE-2.0"));
676 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"BSD-1"));
677 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"BSD-2"));
678 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"BSD-3"));
679 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"BSD-3-CLAUSE"));
680
681 // Test combinations
682 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-MIT.txt"));
683 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE.ISC.md"));
684 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"license_upl"));
685 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.APACHE.2.0"));
686
687 // Test case insensitive
688 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"License"));
689 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"license-mit.TXT"));
690 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE_isc.MD"));
691
692 // Test edge cases that should match
693 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"license.mit"));
694 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"licence-upl.txt"));
695
696 // Test non-matching patterns
697 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"COPYING"));
698 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.html"));
699 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"MYLICENSE"));
700 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"src/LICENSE"));
701 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.old"));
702 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-GPL"));
703 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"LICENSEABC"));
704 }
705
706 #[test]
707 fn test_canonicalize_license_text() {
708 let input = " Paragraph 1\nwith multiple lines\n\n\n\nParagraph 2\nwith more lines\n ";
709 let expected = "paragraph 1 with multiple lines paragraph 2 with more lines";
710 assert_eq!(canonicalize_license_text(input), expected);
711
712 // Test tabs and mixed whitespace
713 let input = "Word1\t\tWord2\n\n Word3\r\n\r\n\r\nWord4 ";
714 let expected = "word1 word2 word3 word4";
715 assert_eq!(canonicalize_license_text(input), expected);
716 }
717
718 fn init_test(cx: &mut TestAppContext) {
719 cx.update(|cx| {
720 let settings_store = SettingsStore::test(cx);
721 cx.set_global(settings_store);
722 });
723 }
724
725 #[gpui::test]
726 async fn test_watcher_single_file(cx: &mut TestAppContext) {
727 init_test(cx);
728
729 let fs = FakeFs::new(cx.background_executor.clone());
730 fs.insert_tree("/root", json!({ "main.rs": "fn main() {}" }))
731 .await;
732
733 let worktree = Worktree::local(
734 Path::new("/root/main.rs"),
735 true,
736 fs.clone(),
737 Default::default(),
738 &mut cx.to_async(),
739 )
740 .await
741 .unwrap();
742
743 let watcher = cx.update(|cx| LicenseDetectionWatcher::new(&worktree, cx));
744 assert!(matches!(watcher, LicenseDetectionWatcher::SingleFile));
745 assert!(!watcher.is_project_open_source());
746 }
747
748 #[gpui::test]
749 async fn test_watcher_updates_on_changes(cx: &mut TestAppContext) {
750 init_test(cx);
751
752 let fs = FakeFs::new(cx.background_executor.clone());
753 fs.insert_tree("/root", json!({ "main.rs": "fn main() {}" }))
754 .await;
755
756 let worktree = Worktree::local(
757 Path::new("/root"),
758 true,
759 fs.clone(),
760 Default::default(),
761 &mut cx.to_async(),
762 )
763 .await
764 .unwrap();
765
766 let watcher = cx.update(|cx| LicenseDetectionWatcher::new(&worktree, cx));
767 assert!(matches!(watcher, LicenseDetectionWatcher::Local { .. }));
768 assert!(!watcher.is_project_open_source());
769
770 fs.write(Path::new("/root/LICENSE-MIT"), MIT_TXT.as_bytes())
771 .await
772 .unwrap();
773
774 cx.background_executor.run_until_parked();
775 assert!(watcher.is_project_open_source());
776
777 fs.write(Path::new("/root/LICENSE-APACHE"), APACHE_2_0_TXT.as_bytes())
778 .await
779 .unwrap();
780
781 cx.background_executor.run_until_parked();
782 assert!(watcher.is_project_open_source());
783
784 fs.write(Path::new("/root/LICENSE-MIT"), "Nevermind".as_bytes())
785 .await
786 .unwrap();
787
788 // Still considered open source as LICENSE-APACHE is present
789 cx.background_executor.run_until_parked();
790 assert!(watcher.is_project_open_source());
791
792 fs.write(
793 Path::new("/root/LICENSE-APACHE"),
794 "Also nevermind".as_bytes(),
795 )
796 .await
797 .unwrap();
798
799 cx.background_executor.run_until_parked();
800 assert!(!watcher.is_project_open_source());
801 }
802
803 #[gpui::test]
804 async fn test_watcher_initially_opensource_and_then_deleted(cx: &mut TestAppContext) {
805 init_test(cx);
806
807 let fs = FakeFs::new(cx.background_executor.clone());
808 fs.insert_tree(
809 "/root",
810 json!({ "main.rs": "fn main() {}", "LICENSE-MIT": MIT_TXT }),
811 )
812 .await;
813
814 let worktree = Worktree::local(
815 Path::new("/root"),
816 true,
817 fs.clone(),
818 Default::default(),
819 &mut cx.to_async(),
820 )
821 .await
822 .unwrap();
823
824 let watcher = cx.update(|cx| LicenseDetectionWatcher::new(&worktree, cx));
825 assert!(matches!(watcher, LicenseDetectionWatcher::Local { .. }));
826
827 cx.background_executor.run_until_parked();
828 assert!(watcher.is_project_open_source());
829
830 fs.remove_file(
831 Path::new("/root/LICENSE-MIT"),
832 fs::RemoveOptions {
833 recursive: false,
834 ignore_if_not_exists: false,
835 },
836 )
837 .await
838 .unwrap();
839
840 cx.background_executor.run_until_parked();
841 assert!(!watcher.is_project_open_source());
842 }
843}