license_detection.rs

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