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