sephera_scan 0.7.1

Fast line-of-code counting with language-aware comment detection.
Documentation
use std::fs;

use tempfile::tempdir;

use super::{CodeLoc, IgnoreMatcher, LocMetrics, scan_content};
use sephera_core::config::CommentStyle;
use sephera_core::language_data::{
    C_STYLE, HTML_STYLE, LISP_STYLE, NO_COMMENT, PYTHON_STYLE, SHELL_STYLE,
    SQL_STYLE,
};

fn assert_metrics(
    metrics: LocMetrics,
    code_lines: u64,
    comment_lines: u64,
    empty_lines: u64,
    size_bytes: u64,
) {
    assert_eq!(
        metrics,
        LocMetrics {
            code_lines,
            comment_lines,
            empty_lines,
            size_bytes,
        }
    );
}

#[test]
fn a_divider_before_a_comment_does_not_hide_the_comment() {
    // The `/` in `total / count` starts no comment. The `/*` a few bytes later
    // does, and it runs into the next line, so a search that took the first `/` it
    // found and stopped would leave the block unopened and count that next line as
    // code.
    //
    // This is the case that tells a *verified* delimiter search apart from one that
    // only checks the shared byte, and it has to run the block comment past the
    // end of its line to do it: a block that opens and closes on one line counts as
    // code either way, so the two mistakes cancel and the test passes against
    // broken code. That is what the first version of this test did, and it is why
    // the corpus pins were green with `find_delimiter` skipping its check.
    let metrics = scan_content(
        b"let ratio = total / count; /* note\nlet after = 2;\n",
        &C_STYLE,
    );

    // One code line, and one line inside the block.
    assert_metrics(metrics, 1, 1, 0, 0);
}

#[test]
fn counts_c_style_comment_only_lines() {
    let metrics = scan_content(b"/* comment */\n", &C_STYLE);
    assert_metrics(metrics, 0, 1, 0, 0);
}

#[test]
fn counts_mixed_code_and_comment_lines_as_code() {
    let metrics = scan_content(b"let value = 1; /* comment */\n", &C_STYLE);
    assert_metrics(metrics, 1, 0, 0, 0);
}

#[test]
fn counts_block_comment_then_code_on_following_line() {
    let metrics = scan_content(b"/* comment\n*/ let value = 1;\n", &C_STYLE);
    assert_metrics(metrics, 1, 1, 0, 0);
}

#[test]
fn supports_nested_block_comments() {
    let metrics = scan_content(
        b"/* outer\n/* inner */\nclosing */\nlet value = 1;\n",
        &C_STYLE,
    );
    assert_metrics(metrics, 1, 3, 0, 0);
}

#[test]
fn supports_shell_python_sql_html_and_lisp_styles() {
    let shell_metrics = scan_content(b"# comment\nvalue\n", &SHELL_STYLE);
    let python_metrics =
        scan_content(b"\"\"\"comment\"\"\"\nvalue = 1\n", &PYTHON_STYLE);
    let sql_metrics = scan_content(b"-- comment\nSELECT 1;\n", &SQL_STYLE);
    let html_metrics =
        scan_content(b"<!-- comment -->\n<div></div>\n", &HTML_STYLE);
    let lisp_metrics = scan_content(b"; comment\n(value)\n", &LISP_STYLE);

    assert_metrics(shell_metrics, 1, 1, 0, 0);
    assert_metrics(python_metrics, 1, 1, 0, 0);
    assert_metrics(sql_metrics, 1, 1, 0, 0);
    assert_metrics(html_metrics, 1, 1, 0, 0);
    assert_metrics(lisp_metrics, 1, 1, 0, 0);
}

#[test]
fn counts_commentless_formats_with_crlf_and_without_trailing_newline() {
    let metrics = scan_content(b"{\r\n\r\n}\r\nfinal", &NO_COMMENT);
    assert_metrics(metrics, 3, 0, 1, 0);
}

#[test]
fn treats_whitespace_inside_block_comment_as_empty() {
    let metrics = scan_content(b"/*\n   \n*/\n", &C_STYLE);
    assert_metrics(metrics, 0, 2, 1, 0);
}

#[test]
fn prefers_longer_multiline_token_when_it_overlaps_single_line_prefix() {
    const PREFIX_OVERLAP_STYLE: CommentStyle =
        CommentStyle::new(Some("#"), Some("#="), Some("=#"));

    let metrics =
        scan_content(b"#=\ncomment\n=#\nvalue\n", &PREFIX_OVERLAP_STYLE);
    assert_metrics(metrics, 1, 3, 0, 0);
}

#[test]
fn progress_reports_the_file_count_and_finishes_at_it() {
    use sephera_core::progress::Progress;
    use std::sync::Mutex;

    /// Reports the total once, then counts advances.
    #[derive(Default)]
    struct Counting {
        total: Mutex<u64>,
        advanced: Mutex<u64>,
    }

    impl Counting {
        fn total(&self) -> u64 {
            *self.total.lock().unwrap()
        }

        fn advanced(&self) -> u64 {
            *self.advanced.lock().unwrap()
        }
    }

    impl Progress for Counting {
        fn set_total(&self, total: u64) {
            *self.total.lock().unwrap() = total;
        }

        fn advance(&self, by: u64) {
            *self.advanced.lock().unwrap() += by;
        }
    }

    let temp_dir = tempdir().unwrap();
    for name in ["one.rs", "two.rs", "three.py"] {
        fs::write(temp_dir.path().join(name), "fn f() {}\n").unwrap();
    }

    let progress = Counting::default();
    let report = CodeLoc::new(temp_dir.path(), IgnoreMatcher::empty())
        .analyze_with(&progress)
        .unwrap();

    assert_eq!(
        progress.total(),
        report.files_scanned,
        "the total reported must be the files actually scanned"
    );
    assert_eq!(
        progress.advanced(),
        report.files_scanned,
        "every file must be counted exactly once, so the bar reaches the end \
         rather than stopping short or overshooting"
    );
}

#[test]
fn ignore_matcher_supports_glob_and_regex() {
    let patterns = vec!["*.rs".to_owned(), "target|node_modules".to_owned()];
    let matcher = IgnoreMatcher::from_patterns(&patterns).unwrap();

    assert!(matcher.is_ignored(std::path::Path::new("src/main.rs")));
    assert!(matcher.is_ignored(std::path::Path::new("target/generated.c")));
    assert!(!matcher.is_ignored(std::path::Path::new("src/main.py")));
}

#[test]
fn invalid_regex_pattern_is_rejected() {
    let patterns = vec!["(".to_owned()];
    assert!(IgnoreMatcher::from_patterns(&patterns).is_err());
}

#[test]
fn analyzes_directory_and_aggregates_per_language_metrics() {
    let temp_dir = tempdir().unwrap();
    let src_dir = temp_dir.path().join("src");
    let target_dir = temp_dir.path().join("target");
    fs::create_dir_all(&src_dir).unwrap();
    fs::create_dir_all(&target_dir).unwrap();

    fs::write(src_dir.join("main.rs"), b"// comment\nfn main() {}\n\n")
        .unwrap();
    fs::write(src_dir.join("script.py"), b"# comment\nvalue = 1\n").unwrap();
    fs::write(target_dir.join("ignored.rs"), b"fn ignored() {}\n").unwrap();

    let ignore = IgnoreMatcher::from_patterns(&["target".to_owned()]).unwrap();
    let report = CodeLoc::new(temp_dir.path(), ignore).analyze().unwrap();

    assert_eq!(report.files_scanned, 2);
    assert_eq!(report.languages_detected, 2);
    assert_eq!(report.totals.code_lines, 2);
    assert_eq!(report.totals.comment_lines, 2);
    assert_eq!(report.totals.empty_lines, 1);
    assert!(report.elapsed >= std::time::Duration::ZERO);

    let rust_metrics = report
        .by_language
        .iter()
        .find(|language| language.language == "Rust")
        .unwrap();
    let python_metrics = report
        .by_language
        .iter()
        .find(|language| language.language == "Python")
        .unwrap();

    assert_eq!(rust_metrics.metrics.code_lines, 1);
    assert_eq!(rust_metrics.metrics.comment_lines, 1);
    assert_eq!(rust_metrics.metrics.empty_lines, 1);
    assert_eq!(python_metrics.metrics.code_lines, 1);
    assert_eq!(python_metrics.metrics.comment_lines, 1);
}