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::{Settings as _, SettingsStore};
394 use worktree::WorktreeSettings;
395
396 use super::*;
397
398 const APACHE_2_0_TXT: &str = include_str!("../license_examples/apache-2.0-ex0.txt");
399 const ISC_TXT: &str = include_str!("../license_examples/isc.txt");
400 const MIT_TXT: &str = include_str!("../license_examples/mit-ex0.txt");
401 const UPL_1_0_TXT: &str = include_str!("../license_examples/upl-1.0.txt");
402 const BSD_0_TXT: &str = include_str!("../license_examples/0bsd.txt");
403
404 #[track_caller]
405 fn assert_matches_license(text: &str, license: OpenSourceLicense) {
406 assert_eq!(detect_license(text), Some(license));
407 assert!(text.len() < LICENSE_PATTERNS.approximate_max_length);
408 }
409
410 /*
411 // Uncomment this and run with `cargo test -p zeta -- --no-capture &> licenses-output` to
412 // traverse your entire home directory and run license detection on every file that has a
413 // license-like name.
414 #[test]
415 fn test_check_all_licenses_in_home_dir() {
416 let mut detected = Vec::new();
417 let mut unrecognized = Vec::new();
418 let mut walked_entries = 0;
419 let homedir = std::env::home_dir().unwrap();
420 for entry in walkdir::WalkDir::new(&homedir) {
421 walked_entries += 1;
422 if walked_entries % 10000 == 0 {
423 println!(
424 "So far visited {} files in {}",
425 walked_entries,
426 homedir.display()
427 );
428 }
429 let Ok(entry) = entry else {
430 continue;
431 };
432 if !LICENSE_FILE_NAME_REGEX.is_match(entry.file_name().as_encoded_bytes()) {
433 continue;
434 }
435 let Ok(contents) = std::fs::read_to_string(entry.path()) else {
436 continue;
437 };
438 let path_string = entry.path().to_string_lossy().into_owned();
439 let license = detect_license(&contents);
440 match license {
441 Some(license) => detected.push((license, path_string)),
442 None => unrecognized.push(path_string),
443 }
444 }
445 println!("\nDetected licenses:\n");
446 detected.sort();
447 for (license, path) in &detected {
448 println!("{}: {}", license.spdx_identifier(), path);
449 }
450 println!("\nUnrecognized licenses:\n");
451 for path in &unrecognized {
452 println!("{}", path);
453 }
454 panic!(
455 "{} licenses detected, {} unrecognized",
456 detected.len(),
457 unrecognized.len()
458 );
459 println!("This line has a warning to make sure this test is always commented out");
460 }
461 */
462
463 #[test]
464 fn test_apache_positive_detection() {
465 assert_matches_license(APACHE_2_0_TXT, OpenSourceLicense::Apache2_0);
466 assert_matches_license(
467 include_str!("../license_examples/apache-2.0-ex1.txt"),
468 OpenSourceLicense::Apache2_0,
469 );
470 assert_matches_license(
471 include_str!("../license_examples/apache-2.0-ex2.txt"),
472 OpenSourceLicense::Apache2_0,
473 );
474 assert_matches_license(
475 include_str!("../license_examples/apache-2.0-ex3.txt"),
476 OpenSourceLicense::Apache2_0,
477 );
478 assert_matches_license(
479 include_str!("../license_examples/apache-2.0-ex4.txt"),
480 OpenSourceLicense::Apache2_0,
481 );
482 assert_matches_license(
483 include_str!("../../../LICENSE-APACHE"),
484 OpenSourceLicense::Apache2_0,
485 );
486 }
487
488 #[test]
489 fn test_apache_negative_detection() {
490 assert_eq!(
491 detect_license(&format!(
492 "{APACHE_2_0_TXT}\n\nThe terms in this license are void if P=NP."
493 )),
494 None
495 );
496 }
497
498 #[test]
499 fn test_bsd_1_clause_positive_detection() {
500 assert_matches_license(
501 include_str!("../license_examples/bsd-1-clause.txt"),
502 OpenSourceLicense::BSD,
503 );
504 }
505
506 #[test]
507 fn test_bsd_2_clause_positive_detection() {
508 assert_matches_license(
509 include_str!("../license_examples/bsd-2-clause-ex0.txt"),
510 OpenSourceLicense::BSD,
511 );
512 }
513
514 #[test]
515 fn test_bsd_3_clause_positive_detection() {
516 assert_matches_license(
517 include_str!("../license_examples/bsd-3-clause-ex0.txt"),
518 OpenSourceLicense::BSD,
519 );
520 assert_matches_license(
521 include_str!("../license_examples/bsd-3-clause-ex1.txt"),
522 OpenSourceLicense::BSD,
523 );
524 assert_matches_license(
525 include_str!("../license_examples/bsd-3-clause-ex2.txt"),
526 OpenSourceLicense::BSD,
527 );
528 assert_matches_license(
529 include_str!("../license_examples/bsd-3-clause-ex3.txt"),
530 OpenSourceLicense::BSD,
531 );
532 assert_matches_license(
533 include_str!("../license_examples/bsd-3-clause-ex4.txt"),
534 OpenSourceLicense::BSD,
535 );
536 }
537
538 #[test]
539 fn test_bsd_0_positive_detection() {
540 assert_matches_license(BSD_0_TXT, OpenSourceLicense::BSDZero);
541 }
542
543 #[test]
544 fn test_isc_positive_detection() {
545 assert_matches_license(ISC_TXT, OpenSourceLicense::ISC);
546 }
547
548 #[test]
549 fn test_isc_negative_detection() {
550 let license_text = format!(
551 r#"{ISC_TXT}
552
553 This project is dual licensed under the ISC License and the MIT License."#
554 );
555
556 assert_eq!(detect_license(&license_text), None);
557 }
558
559 #[test]
560 fn test_mit_positive_detection() {
561 assert_matches_license(MIT_TXT, OpenSourceLicense::MIT);
562 assert_matches_license(
563 include_str!("../license_examples/mit-ex1.txt"),
564 OpenSourceLicense::MIT,
565 );
566 assert_matches_license(
567 include_str!("../license_examples/mit-ex2.txt"),
568 OpenSourceLicense::MIT,
569 );
570 assert_matches_license(
571 include_str!("../license_examples/mit-ex3.txt"),
572 OpenSourceLicense::MIT,
573 );
574 }
575
576 #[test]
577 fn test_mit_negative_detection() {
578 let license_text = format!(
579 r#"{MIT_TXT}
580
581 This project is dual licensed under the MIT License and the Apache License, Version 2.0."#
582 );
583 assert_eq!(detect_license(&license_text), None);
584 }
585
586 #[test]
587 fn test_upl_positive_detection() {
588 assert_matches_license(UPL_1_0_TXT, OpenSourceLicense::UPL1_0);
589 }
590
591 #[test]
592 fn test_upl_negative_detection() {
593 let license_text = format!(
594 r#"{UPL_1_0_TXT}
595
596 This project is dual licensed under the UPL License and the MIT License."#
597 );
598
599 assert_eq!(detect_license(&license_text), None);
600 }
601
602 #[test]
603 fn test_zlib_positive_detection() {
604 assert_matches_license(
605 include_str!("../license_examples/zlib-ex0.txt"),
606 OpenSourceLicense::Zlib,
607 );
608 }
609
610 #[test]
611 fn random_strings_negative_detection() {
612 for _i in 0..20 {
613 let random_string = rand::rng()
614 .sample_iter::<char, _>(rand::distr::StandardUniform)
615 .take(512)
616 .collect::<String>();
617 assert_eq!(detect_license(&random_string), None);
618 }
619 }
620
621 #[test]
622 fn test_n_chars_before_offset() {
623 assert_eq!(n_chars_before_offset(2, 4, "hello"), 2);
624
625 let input = "ㄒ乇丂ㄒ";
626 assert_eq!(n_chars_before_offset(2, input.len(), input), "ㄒ乇".len());
627 }
628
629 #[test]
630 fn test_is_char_count_within_range() {
631 // TODO: make this into a proper property test.
632 for _i in 0..20 {
633 let mut rng = rand::rng();
634 let random_char_count = rng.random_range(0..64);
635 let random_string = rand::rng()
636 .sample_iter::<char, _>(rand::distr::StandardUniform)
637 .take(random_char_count)
638 .collect::<String>();
639 let min_chars = rng.random_range(0..10);
640 let max_chars = rng.random_range(min_chars..32);
641 let char_count_range = min_chars..max_chars;
642 assert_eq!(
643 is_char_count_within_range(&random_string, char_count_range.clone()),
644 char_count_range.contains(&random_char_count),
645 );
646 }
647 }
648
649 #[test]
650 fn test_license_file_name_regex() {
651 // Test basic license file names
652 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE"));
653 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE"));
654 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"license"));
655 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"licence"));
656
657 // Test with extensions
658 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.txt"));
659 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.md"));
660 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE.txt"));
661 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE.md"));
662
663 // Test with specific license types
664 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-APACHE"));
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_MIT"));
668 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-ISC"));
669 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-UPL"));
670
671 // Test with "license" coming after
672 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"APACHE-LICENSE"));
673
674 // Test version numbers
675 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"APACHE-2"));
676 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"APACHE-2.0"));
677 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"BSD-1"));
678 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"BSD-2"));
679 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"BSD-3"));
680 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"BSD-3-CLAUSE"));
681
682 // Test combinations
683 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-MIT.txt"));
684 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE.ISC.md"));
685 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"license_upl"));
686 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.APACHE.2.0"));
687
688 // Test case insensitive
689 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"License"));
690 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"license-mit.TXT"));
691 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE_isc.MD"));
692
693 // Test edge cases that should match
694 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"license.mit"));
695 assert!(LICENSE_FILE_NAME_REGEX.is_match(b"licence-upl.txt"));
696
697 // Test non-matching patterns
698 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"COPYING"));
699 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.html"));
700 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"MYLICENSE"));
701 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"src/LICENSE"));
702 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.old"));
703 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-GPL"));
704 assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"LICENSEABC"));
705 }
706
707 #[test]
708 fn test_canonicalize_license_text() {
709 let input = " Paragraph 1\nwith multiple lines\n\n\n\nParagraph 2\nwith more lines\n ";
710 let expected = "paragraph 1 with multiple lines paragraph 2 with more lines";
711 assert_eq!(canonicalize_license_text(input), expected);
712
713 // Test tabs and mixed whitespace
714 let input = "Word1\t\tWord2\n\n Word3\r\n\r\n\r\nWord4 ";
715 let expected = "word1 word2 word3 word4";
716 assert_eq!(canonicalize_license_text(input), expected);
717 }
718
719 fn init_test(cx: &mut TestAppContext) {
720 cx.update(|cx| {
721 let settings_store = SettingsStore::test(cx);
722 cx.set_global(settings_store);
723 WorktreeSettings::register(cx);
724 });
725 }
726
727 #[gpui::test]
728 async fn test_watcher_single_file(cx: &mut TestAppContext) {
729 init_test(cx);
730
731 let fs = FakeFs::new(cx.background_executor.clone());
732 fs.insert_tree("/root", json!({ "main.rs": "fn main() {}" }))
733 .await;
734
735 let worktree = Worktree::local(
736 Path::new("/root/main.rs"),
737 true,
738 fs.clone(),
739 Default::default(),
740 &mut cx.to_async(),
741 )
742 .await
743 .unwrap();
744
745 let watcher = cx.update(|cx| LicenseDetectionWatcher::new(&worktree, cx));
746 assert!(matches!(watcher, LicenseDetectionWatcher::SingleFile));
747 assert!(!watcher.is_project_open_source());
748 }
749
750 #[gpui::test]
751 async fn test_watcher_updates_on_changes(cx: &mut TestAppContext) {
752 init_test(cx);
753
754 let fs = FakeFs::new(cx.background_executor.clone());
755 fs.insert_tree("/root", json!({ "main.rs": "fn main() {}" }))
756 .await;
757
758 let worktree = Worktree::local(
759 Path::new("/root"),
760 true,
761 fs.clone(),
762 Default::default(),
763 &mut cx.to_async(),
764 )
765 .await
766 .unwrap();
767
768 let watcher = cx.update(|cx| LicenseDetectionWatcher::new(&worktree, cx));
769 assert!(matches!(watcher, LicenseDetectionWatcher::Local { .. }));
770 assert!(!watcher.is_project_open_source());
771
772 fs.write(Path::new("/root/LICENSE-MIT"), MIT_TXT.as_bytes())
773 .await
774 .unwrap();
775
776 cx.background_executor.run_until_parked();
777 assert!(watcher.is_project_open_source());
778
779 fs.write(Path::new("/root/LICENSE-APACHE"), APACHE_2_0_TXT.as_bytes())
780 .await
781 .unwrap();
782
783 cx.background_executor.run_until_parked();
784 assert!(watcher.is_project_open_source());
785
786 fs.write(Path::new("/root/LICENSE-MIT"), "Nevermind".as_bytes())
787 .await
788 .unwrap();
789
790 // Still considered open source as LICENSE-APACHE is present
791 cx.background_executor.run_until_parked();
792 assert!(watcher.is_project_open_source());
793
794 fs.write(
795 Path::new("/root/LICENSE-APACHE"),
796 "Also nevermind".as_bytes(),
797 )
798 .await
799 .unwrap();
800
801 cx.background_executor.run_until_parked();
802 assert!(!watcher.is_project_open_source());
803 }
804
805 #[gpui::test]
806 async fn test_watcher_initially_opensource_and_then_deleted(cx: &mut TestAppContext) {
807 init_test(cx);
808
809 let fs = FakeFs::new(cx.background_executor.clone());
810 fs.insert_tree(
811 "/root",
812 json!({ "main.rs": "fn main() {}", "LICENSE-MIT": MIT_TXT }),
813 )
814 .await;
815
816 let worktree = Worktree::local(
817 Path::new("/root"),
818 true,
819 fs.clone(),
820 Default::default(),
821 &mut cx.to_async(),
822 )
823 .await
824 .unwrap();
825
826 let watcher = cx.update(|cx| LicenseDetectionWatcher::new(&worktree, cx));
827 assert!(matches!(watcher, LicenseDetectionWatcher::Local { .. }));
828
829 cx.background_executor.run_until_parked();
830 assert!(watcher.is_project_open_source());
831
832 fs.remove_file(
833 Path::new("/root/LICENSE-MIT"),
834 fs::RemoveOptions {
835 recursive: false,
836 ignore_if_not_exists: false,
837 },
838 )
839 .await
840 .unwrap();
841
842 cx.background_executor.run_until_parked();
843 assert!(!watcher.is_project_open_source());
844 }
845}