license_detection.rs

  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
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        let worktree_abs_path = local_worktree.abs_path().clone();
287
288        let options = ChildEntriesOptions {
289            include_files: true,
290            include_dirs: false,
291            include_ignored: true,
292        };
293        for top_file in local_worktree.child_entries_with_options(Path::new(""), options) {
294            let path_bytes = top_file.path.as_os_str().as_encoded_bytes();
295            if top_file.is_created() && LICENSE_FILE_NAME_REGEX.is_match(path_bytes) {
296                let rel_path = top_file.path.clone();
297                files_to_check_tx.unbounded_send(rel_path).ok();
298            }
299        }
300
301        let _worktree_subscription =
302            cx.subscribe(worktree, move |_worktree, event, _cx| match event {
303                worktree::Event::UpdatedEntries(updated_entries) => {
304                    for updated_entry in updated_entries.iter() {
305                        let rel_path = &updated_entry.0;
306                        let path_bytes = rel_path.as_os_str().as_encoded_bytes();
307                        if LICENSE_FILE_NAME_REGEX.is_match(path_bytes) {
308                            files_to_check_tx.unbounded_send(rel_path.clone()).ok();
309                        }
310                    }
311                }
312                worktree::Event::DeletedEntry(_) | worktree::Event::UpdatedGitRepositories(_) => {}
313            });
314
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_abs_path.join(&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 fs::FakeFs;
388    use gpui::TestAppContext;
389    use rand::Rng as _;
390    use serde_json::json;
391    use settings::{Settings as _, SettingsStore};
392    use worktree::WorktreeSettings;
393
394    use super::*;
395
396    const APACHE_2_0_TXT: &str = include_str!("../license_examples/apache-2.0-ex0.txt");
397    const ISC_TXT: &str = include_str!("../license_examples/isc.txt");
398    const MIT_TXT: &str = include_str!("../license_examples/mit-ex0.txt");
399    const UPL_1_0_TXT: &str = include_str!("../license_examples/upl-1.0.txt");
400    const BSD_0_TXT: &str = include_str!("../license_examples/0bsd.txt");
401
402    #[track_caller]
403    fn assert_matches_license(text: &str, license: OpenSourceLicense) {
404        assert_eq!(detect_license(text), Some(license));
405        assert!(text.len() < LICENSE_PATTERNS.approximate_max_length);
406    }
407
408    /*
409    // Uncomment this and run with `cargo test -p zeta -- --no-capture &> licenses-output` to
410    // traverse your entire home directory and run license detection on every file that has a
411    // license-like name.
412    #[test]
413    fn test_check_all_licenses_in_home_dir() {
414        let mut detected = Vec::new();
415        let mut unrecognized = Vec::new();
416        let mut walked_entries = 0;
417        let homedir = std::env::home_dir().unwrap();
418        for entry in walkdir::WalkDir::new(&homedir) {
419            walked_entries += 1;
420            if walked_entries % 10000 == 0 {
421                println!(
422                    "So far visited {} files in {}",
423                    walked_entries,
424                    homedir.display()
425                );
426            }
427            let Ok(entry) = entry else {
428                continue;
429            };
430            if !LICENSE_FILE_NAME_REGEX.is_match(entry.file_name().as_encoded_bytes()) {
431                continue;
432            }
433            let Ok(contents) = std::fs::read_to_string(entry.path()) else {
434                continue;
435            };
436            let path_string = entry.path().to_string_lossy().to_string();
437            let license = detect_license(&contents);
438            match license {
439                Some(license) => detected.push((license, path_string)),
440                None => unrecognized.push(path_string),
441            }
442        }
443        println!("\nDetected licenses:\n");
444        detected.sort();
445        for (license, path) in &detected {
446            println!("{}: {}", license.spdx_identifier(), path);
447        }
448        println!("\nUnrecognized licenses:\n");
449        for path in &unrecognized {
450            println!("{}", path);
451        }
452        panic!(
453            "{} licenses detected, {} unrecognized",
454            detected.len(),
455            unrecognized.len()
456        );
457        println!("This line has a warning to make sure this test is always commented out");
458    }
459    */
460
461    #[test]
462    fn test_apache_positive_detection() {
463        assert_matches_license(APACHE_2_0_TXT, OpenSourceLicense::Apache2_0);
464        assert_matches_license(
465            include_str!("../license_examples/apache-2.0-ex1.txt"),
466            OpenSourceLicense::Apache2_0,
467        );
468        assert_matches_license(
469            include_str!("../license_examples/apache-2.0-ex2.txt"),
470            OpenSourceLicense::Apache2_0,
471        );
472        assert_matches_license(
473            include_str!("../license_examples/apache-2.0-ex3.txt"),
474            OpenSourceLicense::Apache2_0,
475        );
476        assert_matches_license(
477            include_str!("../license_examples/apache-2.0-ex4.txt"),
478            OpenSourceLicense::Apache2_0,
479        );
480        assert_matches_license(
481            include_str!("../../../LICENSE-APACHE"),
482            OpenSourceLicense::Apache2_0,
483        );
484    }
485
486    #[test]
487    fn test_apache_negative_detection() {
488        assert_eq!(
489            detect_license(&format!(
490                "{APACHE_2_0_TXT}\n\nThe terms in this license are void if P=NP."
491            )),
492            None
493        );
494    }
495
496    #[test]
497    fn test_bsd_1_clause_positive_detection() {
498        assert_matches_license(
499            include_str!("../license_examples/bsd-1-clause.txt"),
500            OpenSourceLicense::BSD,
501        );
502    }
503
504    #[test]
505    fn test_bsd_2_clause_positive_detection() {
506        assert_matches_license(
507            include_str!("../license_examples/bsd-2-clause-ex0.txt"),
508            OpenSourceLicense::BSD,
509        );
510    }
511
512    #[test]
513    fn test_bsd_3_clause_positive_detection() {
514        assert_matches_license(
515            include_str!("../license_examples/bsd-3-clause-ex0.txt"),
516            OpenSourceLicense::BSD,
517        );
518        assert_matches_license(
519            include_str!("../license_examples/bsd-3-clause-ex1.txt"),
520            OpenSourceLicense::BSD,
521        );
522        assert_matches_license(
523            include_str!("../license_examples/bsd-3-clause-ex2.txt"),
524            OpenSourceLicense::BSD,
525        );
526        assert_matches_license(
527            include_str!("../license_examples/bsd-3-clause-ex3.txt"),
528            OpenSourceLicense::BSD,
529        );
530        assert_matches_license(
531            include_str!("../license_examples/bsd-3-clause-ex4.txt"),
532            OpenSourceLicense::BSD,
533        );
534    }
535
536    #[test]
537    fn test_bsd_0_positive_detection() {
538        assert_matches_license(BSD_0_TXT, OpenSourceLicense::BSDZero);
539    }
540
541    #[test]
542    fn test_isc_positive_detection() {
543        assert_matches_license(ISC_TXT, OpenSourceLicense::ISC);
544    }
545
546    #[test]
547    fn test_isc_negative_detection() {
548        let license_text = format!(
549            r#"{ISC_TXT}
550
551            This project is dual licensed under the ISC License and the MIT License."#
552        );
553
554        assert_eq!(detect_license(&license_text), None);
555    }
556
557    #[test]
558    fn test_mit_positive_detection() {
559        assert_matches_license(MIT_TXT, OpenSourceLicense::MIT);
560        assert_matches_license(
561            include_str!("../license_examples/mit-ex1.txt"),
562            OpenSourceLicense::MIT,
563        );
564        assert_matches_license(
565            include_str!("../license_examples/mit-ex2.txt"),
566            OpenSourceLicense::MIT,
567        );
568        assert_matches_license(
569            include_str!("../license_examples/mit-ex3.txt"),
570            OpenSourceLicense::MIT,
571        );
572    }
573
574    #[test]
575    fn test_mit_negative_detection() {
576        let license_text = format!(
577            r#"{MIT_TXT}
578
579            This project is dual licensed under the MIT License and the Apache License, Version 2.0."#
580        );
581        assert_eq!(detect_license(&license_text), None);
582    }
583
584    #[test]
585    fn test_upl_positive_detection() {
586        assert_matches_license(UPL_1_0_TXT, OpenSourceLicense::UPL1_0);
587    }
588
589    #[test]
590    fn test_upl_negative_detection() {
591        let license_text = format!(
592            r#"{UPL_1_0_TXT}
593
594            This project is dual licensed under the UPL License and the MIT License."#
595        );
596
597        assert_eq!(detect_license(&license_text), None);
598    }
599
600    #[test]
601    fn test_zlib_positive_detection() {
602        assert_matches_license(
603            include_str!("../license_examples/zlib-ex0.txt"),
604            OpenSourceLicense::Zlib,
605        );
606    }
607
608    #[test]
609    fn random_strings_negative_detection() {
610        for _i in 0..20 {
611            let random_string = rand::rng()
612                .sample_iter::<char, _>(rand::distr::StandardUniform)
613                .take(512)
614                .collect::<String>();
615            assert_eq!(detect_license(&random_string), None);
616        }
617    }
618
619    #[test]
620    fn test_n_chars_before_offset() {
621        assert_eq!(n_chars_before_offset(2, 4, "hello"), 2);
622
623        let input = "ㄒ乇丂ㄒ";
624        assert_eq!(n_chars_before_offset(2, input.len(), input), "ㄒ乇".len());
625    }
626
627    #[test]
628    fn test_is_char_count_within_range() {
629        // TODO: make this into a proper property test.
630        for _i in 0..20 {
631            let mut rng = rand::rng();
632            let random_char_count = rng.random_range(0..64);
633            let random_string = rand::rng()
634                .sample_iter::<char, _>(rand::distr::StandardUniform)
635                .take(random_char_count)
636                .collect::<String>();
637            let min_chars = rng.random_range(0..10);
638            let max_chars = rng.random_range(min_chars..32);
639            let char_count_range = min_chars..max_chars;
640            assert_eq!(
641                is_char_count_within_range(&random_string, char_count_range.clone()),
642                char_count_range.contains(&random_char_count),
643            );
644        }
645    }
646
647    #[test]
648    fn test_license_file_name_regex() {
649        // Test basic license file names
650        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE"));
651        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE"));
652        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"license"));
653        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"licence"));
654
655        // Test with extensions
656        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.txt"));
657        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.md"));
658        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE.txt"));
659        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE.md"));
660
661        // Test with specific license types
662        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-APACHE"));
663        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-MIT"));
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-ISC"));
667        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-UPL"));
668
669        // Test with "license" coming after
670        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"APACHE-LICENSE"));
671
672        // Test version numbers
673        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"APACHE-2"));
674        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"APACHE-2.0"));
675        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"BSD-1"));
676        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"BSD-2"));
677        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"BSD-3"));
678        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"BSD-3-CLAUSE"));
679
680        // Test combinations
681        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-MIT.txt"));
682        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE.ISC.md"));
683        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"license_upl"));
684        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.APACHE.2.0"));
685
686        // Test case insensitive
687        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"License"));
688        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"license-mit.TXT"));
689        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"LICENCE_isc.MD"));
690
691        // Test edge cases that should match
692        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"license.mit"));
693        assert!(LICENSE_FILE_NAME_REGEX.is_match(b"licence-upl.txt"));
694
695        // Test non-matching patterns
696        assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"COPYING"));
697        assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.html"));
698        assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"MYLICENSE"));
699        assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"src/LICENSE"));
700        assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE.old"));
701        assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"LICENSE-GPL"));
702        assert!(!LICENSE_FILE_NAME_REGEX.is_match(b"LICENSEABC"));
703    }
704
705    #[test]
706    fn test_canonicalize_license_text() {
707        let input = "  Paragraph 1\nwith multiple lines\n\n\n\nParagraph 2\nwith more lines\n  ";
708        let expected = "paragraph 1 with multiple lines paragraph 2 with more lines";
709        assert_eq!(canonicalize_license_text(input), expected);
710
711        // Test tabs and mixed whitespace
712        let input = "Word1\t\tWord2\n\n   Word3\r\n\r\n\r\nWord4   ";
713        let expected = "word1 word2 word3 word4";
714        assert_eq!(canonicalize_license_text(input), expected);
715    }
716
717    fn init_test(cx: &mut TestAppContext) {
718        cx.update(|cx| {
719            let settings_store = SettingsStore::test(cx);
720            cx.set_global(settings_store);
721            WorktreeSettings::register(cx);
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}