Skip to main content

supercov_engine/
project_discovery.rs

1//! Runner/build/project discovery for zero-configuration JavaScript suites.
2
3use std::{
4    collections::{BTreeMap, BTreeSet, HashMap},
5    fs, io,
6    path::{Path, PathBuf},
7};
8
9use oxc_allocator::Allocator;
10use oxc_ast::ast::{
11    Argument, ArrayExpressionElement, CallExpression, Expression, ImportDeclarationSpecifier,
12    ImportExpression, ImportOrExportKind, Program, Statement,
13};
14use oxc_ast_visit::{Visit, walk};
15use oxc_parser::Parser;
16use serde::{Deserialize, Serialize};
17use serde_json::Value;
18
19use crate::source_discovery::{
20    DiscoveredSourceScope, SourceDiscoveryError, SourceLimitation, SourceScope,
21    discover_source_scope,
22};
23
24const PLAYWRIGHT_CONFIGS: &[&str] = &[
25    "playwright.config.ts",
26    "playwright.config.mts",
27    "playwright.config.js",
28    "playwright.config.mjs",
29    "playwright.config.cts",
30    "playwright.config.cjs",
31];
32const VITEST_CONFIGS: &[&str] = &[
33    "vitest.config.ts",
34    "vitest.config.mts",
35    "vitest.config.js",
36    "vitest.config.mjs",
37    "vitest.config.cts",
38    "vitest.config.cjs",
39    "vite.config.ts",
40    "vite.config.mts",
41    "vite.config.js",
42    "vite.config.mjs",
43];
44const JEST_CONFIGS: &[&str] = &[
45    "jest.config.ts",
46    "jest.config.mts",
47    "jest.config.js",
48    "jest.config.mjs",
49    "jest.config.cts",
50    "jest.config.cjs",
51];
52const TEST_DIRECTORIES: &[&str] = &["test", "tests", "e2e", "spec", "specs"];
53
54#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
55#[serde(rename_all = "camelCase", deny_unknown_fields)]
56pub struct CoverageProject {
57    pub root: PathBuf,
58    pub source_roots: Vec<String>,
59    pub source_files: Vec<String>,
60    pub source_scope: SourceScope,
61    pub source_limitations: Vec<SourceLimitation>,
62    #[serde(skip_serializing_if = "Option::is_none")]
63    pub playwright_config: Option<PathBuf>,
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub vitest_config: Option<PathBuf>,
66    #[serde(skip_serializing_if = "Option::is_none")]
67    pub jest_config: Option<PathBuf>,
68    pub uses_jest: bool,
69    /// The tests bundle sources for a browser through a runner Supercov has
70    /// no adapter for -- karma, testem, airtap, zuul, mochify -- so each
71    /// script-style file carries the runtime the page otherwise lacks.
72    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
73    pub browser_runner: bool,
74    pub playwright_module: String,
75    pub playwright_test_export: String,
76    pub playwright_exports: Vec<String>,
77    /// The project's build, when the tests may need its output and the
78    /// command does not run it: what a failed run names. Supercov runs the
79    /// command it is given and no build of its own.
80    #[serde(default, skip_serializing_if = "Vec::is_empty")]
81    pub unrun_build: Vec<String>,
82}
83
84#[derive(Debug)]
85pub enum ProjectDiscoveryError {
86    Source(SourceDiscoveryError),
87    Io { path: PathBuf, source: io::Error },
88    NoSourceFiles,
89}
90
91impl From<SourceDiscoveryError> for ProjectDiscoveryError {
92    fn from(value: SourceDiscoveryError) -> Self {
93        Self::Source(value)
94    }
95}
96
97impl std::fmt::Display for ProjectDiscoveryError {
98    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99        match self {
100            Self::Source(error) => write!(formatter, "{error}"),
101            Self::Io { path, source } => write!(formatter, "{}: {source}", path.display()),
102            Self::NoSourceFiles => write!(
103                formatter,
104                "No application source files were discovered. If your sources live somewhere unusual, set SUPERCOV_SOURCE_ROOTS=src,app. Alternatively, Supercov may not support your project layout or test framework yet — if so, please open an issue or PR: https://github.com/supercorp-ai/supercov"
105            ),
106        }
107    }
108}
109
110impl std::error::Error for ProjectDiscoveryError {}
111
112fn package_json(root: &Path) -> Value {
113    fs::read(root.join("package.json"))
114        .ok()
115        .and_then(|contents| serde_json::from_slice(&contents).ok())
116        .unwrap_or(Value::Null)
117}
118
119fn script<'a>(manifest: &'a Value, name: &str) -> Option<&'a str> {
120    manifest.get("scripts")?.get(name)?.as_str()
121}
122
123fn regular_file(path: &Path) -> bool {
124    fs::symlink_metadata(path)
125        .map(|metadata| metadata.file_type().is_file())
126        .unwrap_or(false)
127}
128
129fn source_file(path: &Path) -> bool {
130    let name = path
131        .file_name()
132        .and_then(|name| name.to_str())
133        .unwrap_or("");
134    let lower = name.to_ascii_lowercase();
135    [
136        ".js", ".jsx", ".ts", ".tsx", ".cjs", ".cjsx", ".cts", ".ctsx", ".mjs", ".mjsx", ".mts",
137        ".mtsx",
138    ]
139    .iter()
140    .any(|extension| lower.ends_with(extension))
141}
142
143fn read_directory(path: &Path) -> Result<Vec<fs::DirEntry>, ProjectDiscoveryError> {
144    let mut entries = fs::read_dir(path)
145        .map_err(|source| ProjectDiscoveryError::Io {
146            path: path.to_owned(),
147            source,
148        })?
149        .collect::<Result<Vec<_>, _>>()
150        .map_err(|source| ProjectDiscoveryError::Io {
151            path: path.to_owned(),
152            source,
153        })?;
154    entries.sort_by_key(fs::DirEntry::file_name);
155    Ok(entries)
156}
157
158fn parse_program<'a>(
159    allocator: &'a Allocator,
160    path: &Path,
161    source: &'a str,
162) -> Option<Program<'a>> {
163    let source_type = crate::js_instrumenter::project_source_type(path).ok()?;
164    let parsed = Parser::new(allocator, source, source_type).parse();
165    parsed.errors.is_empty().then_some(parsed.program)
166}
167
168#[derive(Debug, Clone)]
169struct TestApiCandidate {
170    module: String,
171    score: usize,
172    test_export: Option<String>,
173    exports: Vec<String>,
174}
175
176fn imported_test_apis(path: &Path, source: &str) -> Vec<TestApiCandidate> {
177    let allocator = Allocator::default();
178    let Some(program) = parse_program(&allocator, path, source) else {
179        return Vec::new();
180    };
181    // Aggregate per module across the whole file: a facade's helpers are
182    // often imported in their own statement (`import { createTestProduct }
183    // from "@acme/fixtures"`), and those names must reach the generated shim
184    // even though that statement alone carries no test-API signal.
185    let mut by_module = BTreeMap::<String, TestApiCandidate>::new();
186    for statement in &program.body {
187        let Statement::ImportDeclaration(declaration) = statement else {
188            continue;
189        };
190        if declaration.import_kind == ImportOrExportKind::Type {
191            continue;
192        }
193        let module = declaration.source.value.to_string();
194        let candidate = by_module
195            .entry(module.clone())
196            .or_insert_with(|| TestApiCandidate {
197                module,
198                score: 0,
199                test_export: None,
200                exports: Vec::new(),
201            });
202        for specifier in declaration.specifiers.iter().flatten() {
203            let ImportDeclarationSpecifier::ImportSpecifier(specifier) = specifier else {
204                continue;
205            };
206            if specifier.import_kind == ImportOrExportKind::Type {
207                continue;
208            }
209            let imported = specifier.imported.name().to_string();
210            let local = specifier.local.name.as_str();
211            candidate.exports.push(imported.clone());
212            if local == "test" {
213                candidate.score += 20;
214                candidate.test_export = Some(imported.clone());
215            } else if imported.to_ascii_lowercase().ends_with("test") {
216                candidate.score += 8;
217            }
218            if local == "expect" || imported == "expect" {
219                candidate.score += 10;
220            }
221        }
222    }
223    by_module
224        .into_values()
225        .filter(|candidate| candidate.score > 0)
226        .map(|mut candidate| {
227            if candidate.module == "@playwright/test" {
228                candidate.score += 100;
229            } else if candidate.module.to_ascii_lowercase().contains("playwright") {
230                candidate.score += 5;
231            }
232            candidate
233        })
234        .collect()
235}
236
237fn test_api_candidates(directory: &Path, output: &mut Vec<TestApiCandidate>) {
238    let Ok(entries) = read_directory(directory) else {
239        return;
240    };
241    for entry in entries {
242        let name = entry.file_name();
243        let Some(name) = name.to_str() else { continue };
244        if name == "node_modules" || name == "results" || name.starts_with('.') {
245            continue;
246        }
247        let path = entry.path();
248        let Ok(file_type) = entry.file_type() else {
249            continue;
250        };
251        if file_type.is_symlink() {
252            continue;
253        }
254        if file_type.is_dir() {
255            test_api_candidates(&path, output);
256        } else if file_type.is_file()
257            && source_file(&path)
258            && let Ok(source) = fs::read_to_string(&path)
259        {
260            output.extend(imported_test_apis(&path, &source));
261        }
262    }
263}
264
265#[derive(Debug, Clone)]
266struct PlaywrightAdapter {
267    module: String,
268    test_export: String,
269    exports: Vec<String>,
270}
271
272fn discover_playwright_adapter(root: &Path) -> PlaywrightAdapter {
273    let mut candidates = Vec::new();
274    for directory in TEST_DIRECTORIES {
275        test_api_candidates(&root.join(directory), &mut candidates);
276    }
277    let mut scores = HashMap::<String, usize>::new();
278    for candidate in &candidates {
279        *scores.entry(candidate.module.clone()).or_default() += candidate.score;
280    }
281    let module = scores
282        .into_iter()
283        .min_by(|(left_module, left_score), (right_module, right_score)| {
284            right_score
285                .cmp(left_score)
286                .then_with(|| left_module.cmp(right_module))
287        })
288        .map(|(module, _)| module)
289        .unwrap_or_else(|| "@playwright/test".into());
290    let matching = candidates
291        .iter()
292        .filter(|candidate| candidate.module == module)
293        .collect::<Vec<_>>();
294    let test_export = matching
295        .iter()
296        .filter_map(|candidate| {
297            candidate
298                .test_export
299                .as_ref()
300                .map(|export| (candidate.score, export))
301        })
302        .max_by_key(|(score, _)| *score)
303        .map(|(_, export)| export.clone())
304        .unwrap_or_else(|| "test".into());
305    let exports = matching
306        .iter()
307        .flat_map(|candidate| candidate.exports.iter().cloned())
308        .collect::<BTreeSet<_>>()
309        .into_iter()
310        .collect();
311    PlaywrightAdapter {
312        module,
313        test_export,
314        exports,
315    }
316}
317
318fn nested_playwright_configs(root: &Path) -> Vec<PathBuf> {
319    fn visit(directory: &Path, depth: usize, found: &mut Vec<PathBuf>) {
320        if depth > 4 {
321            return;
322        }
323        let Ok(entries) = read_directory(directory) else {
324            return;
325        };
326        for entry in entries {
327            let name = entry.file_name();
328            let Some(name) = name.to_str() else { continue };
329            if name.starts_with('.') || name == "node_modules" {
330                continue;
331            }
332            let path = entry.path();
333            let Ok(file_type) = entry.file_type() else {
334                continue;
335            };
336            if file_type.is_symlink() {
337                continue;
338            }
339            if file_type.is_dir() {
340                visit(&path, depth + 1, found);
341            } else if file_type.is_file() && playwright_config_name(name) {
342                found.push(path);
343            }
344        }
345    }
346
347    let mut found = Vec::new();
348    for directory in ["test", "tests", "e2e"] {
349        visit(&root.join(directory), 0, &mut found);
350    }
351    found.sort();
352    found
353}
354
355fn playwright_config_name(name: &str) -> bool {
356    let lower = name.to_ascii_lowercase();
357    if !lower.starts_with("playwright") || !lower.contains(".config.") {
358        return false;
359    }
360    source_file(Path::new(name))
361        && lower[..lower.find(".config.").unwrap_or(0)]
362            .chars()
363            .all(|character| {
364                character.is_ascii_alphanumeric() || matches!(character, '.' | '-' | '_')
365            })
366}
367
368pub fn expanded_command(root: &Path, command: &[String]) -> String {
369    let manifest = package_json(root);
370    let executable = command
371        .first()
372        .and_then(|value| Path::new(value).file_name())
373        .and_then(|value| value.to_str())
374        .unwrap_or("")
375        .trim_end_matches(".cmd")
376        .trim_end_matches(".exe")
377        .to_ascii_lowercase();
378    let run_index = command.iter().position(|argument| argument == "run");
379    let script_name = run_index
380        .and_then(|index| command.get(index + 1))
381        .or_else(|| {
382            (["npm", "pnpm", "yarn", "bun"].contains(&executable.as_str()) && run_index.is_none())
383                .then(|| command.get(1))
384                .flatten()
385        });
386    let joined = command.join(" ");
387    if ["npm", "pnpm", "yarn", "bun"].contains(&executable.as_str())
388        && let Some(script_name) = script_name
389    {
390        return format!("{joined} {}", script(&manifest, script_name).unwrap_or(""));
391    }
392    joined
393}
394
395fn relative_build_output(source: &str) -> bool {
396    let mut rest = source;
397    let mut relative = false;
398    while let Some(stripped) = rest.strip_prefix("../").or_else(|| rest.strip_prefix("./")) {
399        relative = true;
400        rest = stripped;
401    }
402    relative
403        && matches!(
404            rest.split('/').next(),
405            Some("dist" | "build" | "out" | "output")
406        )
407}
408
409fn string_expression<'a>(expression: &'a Expression<'_>) -> Option<&'a str> {
410    let Expression::StringLiteral(literal) = expression else {
411        return None;
412    };
413    Some(literal.value.as_str())
414}
415
416struct BuildOutputScanner<'a> {
417    found: bool,
418    build_output_scripts: &'a BTreeSet<String>,
419}
420
421fn child_process_launcher(expression: &Expression<'_>) -> bool {
422    let name = match expression {
423        Expression::Identifier(identifier) => Some(identifier.name.as_str()),
424        Expression::StaticMemberExpression(member) => Some(member.property.name.as_str()),
425        _ => None,
426    };
427    name.is_some_and(|name| {
428        [
429            "spawn",
430            "spawnSync",
431            "exec",
432            "execSync",
433            "execFile",
434            "execFileSync",
435        ]
436        .contains(&name)
437    })
438}
439
440/// The package script a launch runs, whether the call names a program and an
441/// argument array (`spawn("npm", ["run", "start"])`) or hands the shell one
442/// command string (`execSync("npm run start")`, or `spawn` with `shell`). The
443/// string form is how most suites start a server, and it is the form the
444/// array-only match missed.
445fn launched_package_script<'a>(
446    program: &'a str,
447    arguments: Option<&'a oxc_ast::ast::ArrayExpression<'a>>,
448) -> Option<String> {
449    let words: Vec<String> = match arguments {
450        Some(arguments) => {
451            if !package_manager(program) {
452                return None;
453            }
454            arguments
455                .elements
456                .iter()
457                .map(|element| match element {
458                    ArrayExpressionElement::StringLiteral(value) => Some(value.value.to_string()),
459                    _ => None,
460                })
461                .collect::<Option<Vec<_>>>()?
462        }
463        None => {
464            let mut tokens = command_tokens(program);
465            if tokens.is_empty() || !package_manager(&tokens.remove(0)) {
466                return None;
467            }
468            tokens
469        }
470    };
471    match words.first().map(String::as_str) {
472        Some("run") => words.get(1).cloned(),
473        Some(_) => words.first().cloned(),
474        None => None,
475    }
476}
477
478fn package_manager(value: &str) -> bool {
479    let executable = value
480        .rsplit(['/', '\\'])
481        .next()
482        .unwrap_or(value)
483        .trim_end_matches(".cmd")
484        .trim_end_matches(".exe");
485    ["npm", "pnpm", "yarn", "bun"].contains(&executable)
486}
487
488fn script_references_build_output(command: &str) -> bool {
489    command_tokens(command).iter().any(|token| {
490        let token = token.trim_start_matches("../").trim_start_matches("./");
491        let mut segments = token.split(['/', '\\']);
492        let directory = segments.next();
493        // A bare `build` is a subcommand -- `vite build` produces output, it
494        // does not consume any -- so only a path reaching INTO the directory
495        // means the script runs something already compiled.
496        segments.next().is_some() && matches!(directory, Some("dist" | "build" | "out" | "output"))
497    })
498}
499
500fn build_output_scripts(manifest: &Value) -> BTreeSet<String> {
501    manifest
502        .get("scripts")
503        .and_then(Value::as_object)
504        .into_iter()
505        .flatten()
506        .filter_map(|(name, command)| match command.as_str() {
507            Some(command) if script_references_build_output(command) => Some(name.clone()),
508            _ => None,
509        })
510        .collect()
511}
512
513impl<'a> Visit<'a> for BuildOutputScanner<'_> {
514    fn visit_import_declaration(&mut self, declaration: &oxc_ast::ast::ImportDeclaration<'a>) {
515        self.found |= relative_build_output(declaration.source.value.as_str());
516        walk::walk_import_declaration(self, declaration);
517    }
518
519    fn visit_import_expression(&mut self, expression: &ImportExpression<'a>) {
520        if let Some(source) = string_expression(&expression.source) {
521            self.found |= relative_build_output(source);
522        }
523        walk::walk_import_expression(self, expression);
524    }
525
526    fn visit_call_expression(&mut self, call: &CallExpression<'a>) {
527        if matches!(&call.callee, Expression::Identifier(identifier) if identifier.name == "require")
528            && let Some(Argument::StringLiteral(source)) = call.arguments.first()
529        {
530            self.found |= relative_build_output(source.value.as_str());
531        }
532        if child_process_launcher(&call.callee)
533            && let Some(Argument::StringLiteral(program)) = call.arguments.first()
534        {
535            let arguments = match call.arguments.get(1) {
536                Some(Argument::ArrayExpression(arguments)) => Some(&**arguments),
537                _ => None,
538            };
539            self.found |= launched_package_script(program.value.as_str(), arguments)
540                .is_some_and(|script| self.build_output_scripts.contains(&script));
541        }
542        walk::walk_call_expression(self, call);
543    }
544}
545
546/// Whether tests consume compiled output directly or launch a package script
547/// that does. Either form requires the instrumented build before the runner.
548fn tests_require_build_output(root: &Path, manifest: &Value) -> bool {
549    fn visit(directory: &Path, build_output_scripts: &BTreeSet<String>) -> bool {
550        let Ok(entries) = read_directory(directory) else {
551            return false;
552        };
553        for entry in entries {
554            let name = entry.file_name();
555            let Some(name) = name.to_str() else { continue };
556            if name == "node_modules" || name.starts_with('.') {
557                continue;
558            }
559            let path = entry.path();
560            let Ok(file_type) = entry.file_type() else {
561                continue;
562            };
563            if file_type.is_symlink() {
564                continue;
565            }
566            if file_type.is_dir() && visit(&path, build_output_scripts) {
567                return true;
568            }
569            if file_type.is_file()
570                && source_file(&path)
571                && let Ok(source) = fs::read_to_string(&path)
572            {
573                let allocator = Allocator::default();
574                if let Some(program) = parse_program(&allocator, &path, &source) {
575                    let mut scanner = BuildOutputScanner {
576                        found: false,
577                        build_output_scripts,
578                    };
579                    scanner.visit_program(&program);
580                    if scanner.found {
581                        return true;
582                    }
583                }
584            }
585        }
586        false
587    }
588
589    let build_output_scripts = build_output_scripts(manifest);
590    ["test", "tests", "spec", "specs", "e2e", "__tests__"]
591        .iter()
592        .any(|directory| visit(&root.join(directory), &build_output_scripts))
593}
594
595fn command_tokens(value: &str) -> Vec<String> {
596    value
597        .split_whitespace()
598        .map(|token| {
599            token
600                .trim_matches(|character: char| matches!(character, '\'' | '"' | '(' | ')'))
601                .trim_end_matches([';', ','])
602                .to_ascii_lowercase()
603        })
604        .collect()
605}
606
607/// The configuration file a runner is given in the command: `--config
608/// <file>`, `--config=<file>` or `-c <file>` after the runner's name, before
609/// the next command of a chain. Case is kept: it is a path.
610fn configured_by_command(root: &Path, command: &str, tool: &str) -> Option<PathBuf> {
611    let tokens = command
612        .split_whitespace()
613        .map(|token| token.trim_matches(|character: char| matches!(character, '\'' | '"')))
614        .collect::<Vec<_>>();
615    let mut in_tool = false;
616    for (index, token) in tokens.iter().enumerate() {
617        if matches!(*token, "&&" | "||" | ";" | "|") {
618            in_tool = false;
619            continue;
620        }
621        let file = token.rsplit(['/', '\\']).next().unwrap_or(token);
622        let file = file.trim_end_matches(".cmd").trim_end_matches(".exe");
623        if file == tool {
624            in_tool = true;
625            continue;
626        }
627        if !in_tool {
628            continue;
629        }
630        let value = match token.strip_prefix("--config=") {
631            Some(value) => Some(value),
632            None if matches!(*token, "--config" | "-c") => tokens.get(index + 1).copied(),
633            None => None,
634        };
635        if let Some(value) = value
636            .map(|value| root.join(value))
637            .filter(|path| path.is_file())
638        {
639            return Some(value);
640        }
641    }
642    None
643}
644
645fn has_tool(tokens: &[String], tool: &str) -> bool {
646    tokens.iter().any(|token| {
647        let file = token.rsplit('/').next().unwrap_or(token);
648        file == tool
649            || file.strip_suffix(".cmd") == Some(tool)
650            || file.strip_suffix(".exe") == Some(tool)
651    })
652}
653
654/// `tokens` and the tokens of every package script they run, however deep:
655/// debug's `npm test` runs `npm run test:browser`, which runs karma.
656fn reachable_script_tokens(manifest: &Value, tokens: &[String]) -> Vec<String> {
657    let mut all = tokens.to_vec();
658    let mut visited = BTreeSet::new();
659    let mut index = 0;
660    while index < all.len() {
661        let runner = all[index]
662            .rsplit('/')
663            .next()
664            .unwrap_or(&all[index])
665            .to_owned();
666        let named = match runner.as_str() {
667            "npm" | "pnpm" | "yarn" | "bun" => {
668                let next = all.get(index + 1).map(String::as_str);
669                match next {
670                    Some("run" | "run-script") => all.get(index + 2).cloned(),
671                    Some("test" | "t") => Some("test".to_owned()),
672                    Some(name) if runner != "npm" => Some(name.to_owned()),
673                    _ => None,
674                }
675            }
676            _ => None,
677        };
678        if let Some(name) = named
679            && visited.insert(name.clone())
680            && let Some(body) = script(manifest, &name)
681        {
682            all.extend(command_tokens(body));
683        }
684        index += 1;
685    }
686    all
687}
688
689fn configured_path(
690    root: &Path,
691    environment: &BTreeMap<String, String>,
692    key: &str,
693) -> Option<PathBuf> {
694    environment.get(key).map(|value| root.join(value))
695}
696
697fn first_config(root: &Path, candidates: &[&str]) -> Option<PathBuf> {
698    candidates
699        .iter()
700        .map(|candidate| root.join(candidate))
701        .find(|path| regular_file(path))
702}
703
704/// The package manager the project's tests were started with, to run its
705/// build the same way. A Yarn or pnpm workspace's scripts call the manager
706/// that runs them; building through npm changed that for no reason.
707fn build_package_manager(command: &[String]) -> String {
708    command
709        .first()
710        .filter(|program| package_manager(program))
711        .cloned()
712        .unwrap_or_else(|| "npm".into())
713}
714
715pub fn discover_coverage_project(
716    root: &Path,
717    environment: &BTreeMap<String, String>,
718    command: &[String],
719) -> Result<CoverageProject, ProjectDiscoveryError> {
720    let project = discover_coverage_project_of_any_size(root, environment, command)?;
721    if project.source_files.is_empty() {
722        return Err(ProjectDiscoveryError::NoSourceFiles);
723    }
724    Ok(project)
725}
726
727/// `discover_coverage_project` for a suite whose application may hold no
728/// JavaScript at all: browser tests of a Python server.
729pub fn discover_coverage_project_of_any_size(
730    root: &Path,
731    environment: &BTreeMap<String, String>,
732    command: &[String],
733) -> Result<CoverageProject, ProjectDiscoveryError> {
734    let manifest = package_json(root);
735    let configured_roots = crate::source_discovery::configured_source_roots(environment);
736    let DiscoveredSourceScope {
737        source_files,
738        source_roots,
739        scope,
740        limitations,
741    } = discover_source_scope(root, configured_roots.as_deref())?;
742    // The configuration the command names comes before any found by name: a
743    // root `playwright.config.ts` beside `--config e2e/playwright.config.ts`
744    // made the run load the root one, and run other tests than the command.
745    let expanded = expanded_command(root, command);
746    let playwright_config = configured_path(root, environment, "SUPERCOV_PLAYWRIGHT_CONFIG")
747        .or_else(|| configured_by_command(root, &expanded, "playwright"))
748        .or_else(|| first_config(root, PLAYWRIGHT_CONFIGS))
749        .or_else(|| nested_playwright_configs(root).into_iter().next());
750    let vitest_config = configured_path(root, environment, "SUPERCOV_VITEST_CONFIG")
751        .or_else(|| configured_by_command(root, &expanded, "vitest"))
752        .or_else(|| first_config(root, VITEST_CONFIGS));
753    let jest_config = configured_path(root, environment, "SUPERCOV_JEST_CONFIG")
754        .or_else(|| configured_by_command(root, &expanded, "jest"))
755        .or_else(|| first_config(root, JEST_CONFIGS));
756    let discovered_playwright = discover_playwright_adapter(root);
757    let playwright_module = environment
758        .get("SUPERCOV_PLAYWRIGHT_MODULE")
759        .cloned()
760        .unwrap_or_else(|| discovered_playwright.module.clone());
761    let playwright_test_export = environment
762        .get("SUPERCOV_PLAYWRIGHT_TEST_EXPORT")
763        .cloned()
764        .unwrap_or_else(|| {
765            if playwright_module == discovered_playwright.module {
766                discovered_playwright.test_export.clone()
767            } else {
768                "test".into()
769            }
770        });
771    let expanded_test_command = expanded;
772    let tokens = command_tokens(&expanded_test_command);
773    let uses_jest =
774        jest_config.is_some() || has_tool(&tokens, "jest") || manifest.get("jest").is_some();
775    let script_tokens = reachable_script_tokens(&manifest, &tokens);
776    let browser_runner = ["karma", "testem", "airtap", "zuul", "mochify"]
777        .iter()
778        .any(|tool| has_tool(&script_tokens, tool));
779    let source_transforming_runner = has_tool(&tokens, "jest") || has_tool(&tokens, "vitest");
780    let node_test = has_tool(&tokens, "node") && tokens.iter().any(|token| token == "--test");
781    let typescript_test = tokens.iter().any(|token| {
782        [".ts", ".tsx", ".cts", ".mts"]
783            .iter()
784            .any(|extension| token.ends_with(extension) || token.contains(&format!("{extension}*")))
785    });
786    let owns_build = ["vite", "tsc", "webpack", "rollup", "next", "remix"]
787        .iter()
788        .any(|tool| has_tool(&tokens, tool));
789    // Reading the suite is the expensive half of this question, so it stays on
790    // the right of `&&`: a command that never executes source directly answers
791    // it without parsing a single test file.
792    let executes_source_directly = (source_transforming_runner
793        || (node_test && typescript_test && !owns_build))
794        && !tests_require_build_output(root, &manifest);
795    // Supercov runs the command it is given and no build of its own. It used
796    // to run the project's `build` script first whenever the tests might
797    // need its output: twenty seconds nobody asked for before a suite that
798    // builds the application itself, in two of five popular repositories,
799    // and a run cancelled when that build failed. The build is only what a
800    // failed run names, when the command does not already reach it.
801    let build_script = script(&manifest, "build")
802        .map(command_tokens)
803        .unwrap_or_default();
804    let command_builds = !build_script.is_empty()
805        && build_script
806            .iter()
807            .all(|token| script_tokens.contains(token));
808    let unrun_build = if !build_script.is_empty() && !command_builds && !executes_source_directly {
809        vec![build_package_manager(command), "run".into(), "build".into()]
810    } else {
811        Vec::new()
812    };
813    let playwright_exports = if playwright_module == discovered_playwright.module {
814        discovered_playwright.exports
815    } else {
816        vec![playwright_test_export.clone(), "expect".into()]
817    };
818    Ok(CoverageProject {
819        root: root.to_owned(),
820        source_roots,
821        source_files,
822        source_scope: scope,
823        source_limitations: limitations,
824        playwright_config,
825        vitest_config,
826        jest_config,
827        uses_jest,
828        browser_runner,
829        playwright_module,
830        playwright_test_export,
831        playwright_exports,
832        unrun_build,
833    })
834}
835
836#[cfg(test)]
837mod tests {
838    use std::time::{SystemTime, UNIX_EPOCH};
839
840    use super::*;
841
842    fn project(label: &str, files: &[(&str, &str)]) -> PathBuf {
843        let nonce = SystemTime::now()
844            .duration_since(UNIX_EPOCH)
845            .unwrap()
846            .as_nanos();
847        let root = std::env::temp_dir().join(format!(
848            "supercov-project-{label}-{}-{nonce}",
849            std::process::id()
850        ));
851        fs::create_dir_all(&root).unwrap();
852        for (file, contents) in files {
853            let path = root.join(file);
854            fs::create_dir_all(path.parent().unwrap()).unwrap();
855            fs::write(path, contents).unwrap();
856        }
857        root
858    }
859
860    fn command(values: &[&str]) -> Vec<String> {
861        values.iter().map(|value| (*value).into()).collect()
862    }
863
864    #[test]
865    fn the_configuration_the_command_names_comes_first() {
866        // A root configuration beside the one the command names: the run
867        // loaded the root one and ran other tests than the command.
868        let root = project(
869            "named-config",
870            &[
871                (
872                    "package.json",
873                    r#"{"scripts":{"test:e2e":"playwright test --config e2e/Playwright.config.ts","test:unit":"vitest run -c config/vitest.unit.ts"}}"#,
874                ),
875                ("src/main.ts", "export const ready = true"),
876                ("playwright.config.ts", "export default {}"),
877                ("e2e/Playwright.config.ts", "export default {}"),
878                ("vitest.config.ts", "export default {}"),
879                ("config/vitest.unit.ts", "export default {}"),
880            ],
881        );
882        let discover = |args: &[&str]| {
883            discover_coverage_project(&root, &BTreeMap::new(), &command(args)).unwrap()
884        };
885        assert_eq!(
886            discover(&["npm", "run", "test:e2e"]).playwright_config,
887            Some(root.join("e2e/Playwright.config.ts"))
888        );
889        assert_eq!(
890            discover(&[
891                "npx",
892                "playwright",
893                "test",
894                "--config=e2e/Playwright.config.ts"
895            ])
896            .playwright_config,
897            Some(root.join("e2e/Playwright.config.ts"))
898        );
899        assert_eq!(
900            discover(&["npm", "run", "test:unit"]).vitest_config,
901            Some(root.join("config/vitest.unit.ts"))
902        );
903        // Named for another tool, or not at all: found by name as before.
904        assert_eq!(
905            discover(&["npx", "playwright", "test"]).playwright_config,
906            Some(root.join("playwright.config.ts"))
907        );
908        assert_eq!(
909            discover(&["npx", "vitest", "run", "--config", "missing.ts"]).vitest_config,
910            Some(root.join("vitest.config.ts"))
911        );
912        fs::remove_dir_all(root).unwrap();
913    }
914
915    #[test]
916    fn discovers_conventional_vite_playwright_and_vitest_configuration() {
917        let root = project(
918            "vite",
919            &[
920                (
921                    "package.json",
922                    r#"{"scripts":{"build":"vite build"},"devDependencies":{"vite":"1"}}"#,
923                ),
924                ("src/main.ts", "export const ready = true"),
925                ("playwright.config.ts", "export default {}"),
926                ("vitest.config.ts", "export default {}"),
927                (
928                    "tests/example.spec.ts",
929                    "import { test } from '@playwright/test'",
930                ),
931            ],
932        );
933        let discovered = discover_coverage_project(&root, &BTreeMap::new(), &[]).unwrap();
934        assert_eq!(discovered.source_roots, ["src"]);
935        assert_eq!(
936            discovered.playwright_config,
937            Some(root.join("playwright.config.ts"))
938        );
939        assert_eq!(
940            discovered.vitest_config,
941            Some(root.join("vitest.config.ts"))
942        );
943        assert_eq!(discovered.playwright_module, "@playwright/test");
944        assert_eq!(discovered.playwright_test_export, "test");
945        assert_eq!(discovered.playwright_exports, ["test"]);
946        // The command runs as given: the project\'s build is only what a
947        // failed run names.
948        assert_eq!(discovered.unrun_build, command(&["npm", "run", "build"]));
949        fs::remove_dir_all(root).unwrap();
950    }
951
952    #[test]
953    fn a_browser_runner_the_tests_reach_through_nested_scripts_is_found() {
954        // debug's `npm test` runs `npm run test:browser`, which runs karma.
955        let root = project(
956            "karma",
957            &[
958                (
959                    "package.json",
960                    r#"{"scripts":{"test":"npm run test:node && npm run test:browser","test:node":"mocha test.js","test:browser":"karma start --single-run"}}"#,
961                ),
962                ("src/index.js", "module.exports = 1"),
963            ],
964        );
965        let command = ["npm".to_owned(), "test".to_owned()];
966        let discovered = discover_coverage_project(&root, &BTreeMap::new(), &command).unwrap();
967        assert!(discovered.browser_runner);
968        let node_only = ["npm".to_owned(), "run".to_owned(), "test:node".to_owned()];
969        let discovered = discover_coverage_project(&root, &BTreeMap::new(), &node_only).unwrap();
970        assert!(!discovered.browser_runner);
971    }
972
973    #[test]
974    fn skips_unrelated_builds_for_source_transforming_and_node_test_suites() {
975        for (label, test_script, test_file) in [
976            ("jest", "jest", "require('../src/index.ts')"),
977            ("vitest", "vitest run", "import '../src/index.ts'"),
978            (
979                "node",
980                "node --test tests/*.test.ts",
981                "import '../src/index.ts'",
982            ),
983        ] {
984            let root = project(
985                label,
986                &[
987                    (
988                        "package.json",
989                        &format!(
990                            r#"{{"scripts":{{"build":"node build","test":"{test_script}"}}}}"#
991                        ),
992                    ),
993                    ("src/index.ts", "export const ready = true"),
994                    ("tests/index.test.ts", test_file),
995                ],
996            );
997            let discovered =
998                discover_coverage_project(&root, &BTreeMap::new(), &command(&["npm", "test"]))
999                    .unwrap();
1000            assert!(discovered.unrun_build.is_empty(), "{label}");
1001            fs::remove_dir_all(root).unwrap();
1002        }
1003    }
1004
1005    #[test]
1006    fn names_the_build_with_the_package_manager_the_tests_were_started_with() {
1007        let root = project(
1008            "yarn-build",
1009            &[
1010                (
1011                    "package.json",
1012                    r#"{"scripts":{"build":"tsc","test":"jest --runInBand"}}"#,
1013                ),
1014                ("src/index.ts", "export const ready = true"),
1015                ("test/index.test.js", "require('../dist/index.js')"),
1016            ],
1017        );
1018        let discovered =
1019            discover_coverage_project(&root, &BTreeMap::new(), &command(&["yarn", "test"]))
1020                .unwrap();
1021        assert_eq!(discovered.unrun_build, command(&["yarn", "run", "build"]));
1022        fs::remove_dir_all(root).unwrap();
1023    }
1024
1025    #[test]
1026    fn names_the_build_when_tests_import_compiled_output() {
1027        let root = project(
1028            "compiled",
1029            &[
1030                (
1031                    "package.json",
1032                    r#"{"scripts":{"build":"tsc","test":"jest --runInBand"}}"#,
1033                ),
1034                ("src/index.ts", "export const ready = true"),
1035                ("test/index.test.js", "require('../dist/index.js')"),
1036            ],
1037        );
1038        let discovered =
1039            discover_coverage_project(&root, &BTreeMap::new(), &command(&["npm", "test"])).unwrap();
1040        assert_eq!(discovered.unrun_build, command(&["npm", "run", "build"]));
1041        assert!(discovered.uses_jest);
1042        fs::remove_dir_all(root).unwrap();
1043    }
1044
1045    #[test]
1046    fn names_the_build_when_source_direct_tests_spawn_a_compiled_package_script() {
1047        let root = project(
1048            "spawned-compiled-script",
1049            &[
1050                (
1051                    "package.json",
1052                    r#"{"scripts":{"build":"tsc","start":"node dist/index.js","test":"node --test tests/*.test.ts"}}"#,
1053                ),
1054                ("src/index.ts", "export const ready = true"),
1055                (
1056                    "tests/index.test.ts",
1057                    "import { spawn } from 'node:child_process';\nspawn('npm', ['run', 'start']);\n",
1058                ),
1059            ],
1060        );
1061        let discovered =
1062            discover_coverage_project(&root, &BTreeMap::new(), &command(&["npm", "test"])).unwrap();
1063        assert_eq!(discovered.unrun_build, command(&["npm", "run", "build"]));
1064        fs::remove_dir_all(root).unwrap();
1065    }
1066
1067    #[test]
1068    fn names_the_build_when_tests_exec_a_compiled_package_script_as_one_string() {
1069        // `execSync("npm run start")` hands the shell a single string. It is
1070        // how most suites start the server they test against, and the
1071        // array-only match let it through to the same hang as an unbuilt
1072        // gateway.
1073        let root = project(
1074            "exec-compiled-script",
1075            &[
1076                (
1077                    "package.json",
1078                    r#"{"scripts":{"build":"tsc","start":"node dist/index.js","test":"node --test tests/*.test.ts"}}"#,
1079                ),
1080                ("src/index.ts", "export const ready = true"),
1081                (
1082                    "tests/index.test.ts",
1083                    "import { execSync } from 'node:child_process';\nexecSync('npm run start');\n",
1084                ),
1085            ],
1086        );
1087        let discovered =
1088            discover_coverage_project(&root, &BTreeMap::new(), &command(&["npm", "test"])).unwrap();
1089        assert_eq!(discovered.unrun_build, command(&["npm", "run", "build"]));
1090        fs::remove_dir_all(root).unwrap();
1091    }
1092
1093    #[test]
1094    fn keeps_source_direct_when_a_spawned_script_only_runs_a_build_subcommand() {
1095        let root = project(
1096            "spawned-build-subcommand",
1097            &[
1098                (
1099                    "package.json",
1100                    r#"{"scripts":{"build":"tsc","dev":"vite build","test":"node --test tests/*.test.ts"}}"#,
1101                ),
1102                ("src/index.ts", "export const ready = true"),
1103                (
1104                    "tests/index.test.ts",
1105                    "import { spawn } from 'node:child_process';\nspawn('npm', ['run', 'dev']);\n",
1106                ),
1107            ],
1108        );
1109        let discovered =
1110            discover_coverage_project(&root, &BTreeMap::new(), &command(&["npm", "test"])).unwrap();
1111        assert!(discovered.unrun_build.is_empty());
1112        fs::remove_dir_all(root).unwrap();
1113    }
1114
1115    #[test]
1116    fn discovers_a_project_owned_playwright_fixture_via_the_ast() {
1117        let root = project(
1118            "fixture",
1119            &[
1120                ("package.json", r#"{"scripts":{"build":"vite build"}}"#),
1121                ("app/root.tsx", "export default null"),
1122                (
1123                    "tests/nested/playwright.browser.config.ts",
1124                    "export default {}",
1125                ),
1126                (
1127                    "tests/example.spec.ts",
1128                    "import { type Ignored, browserTest as test, expect, fixtureValue } from '@acme/browser-fixtures'\n\
1129                     import { createFixtureProduct } from '@acme/browser-fixtures'",
1130                ),
1131            ],
1132        );
1133        let discovered = discover_coverage_project(&root, &BTreeMap::new(), &[]).unwrap();
1134        assert_eq!(
1135            discovered.playwright_config,
1136            Some(root.join("tests/nested/playwright.browser.config.ts"))
1137        );
1138        assert_eq!(discovered.playwright_module, "@acme/browser-fixtures");
1139        assert_eq!(discovered.playwright_test_export, "browserTest");
1140        // The helper imported in its own statement must reach the shim: a
1141        // facade's non-test exports vanish otherwise, and every spec importing
1142        // one fails to link.
1143        assert_eq!(
1144            discovered.playwright_exports,
1145            [
1146                "browserTest",
1147                "createFixtureProduct",
1148                "expect",
1149                "fixtureValue"
1150            ]
1151        );
1152        fs::remove_dir_all(root).unwrap();
1153    }
1154
1155    #[test]
1156    fn environment_overrides_are_authoritative() {
1157        let root = project(
1158            "override",
1159            &[
1160                ("package.json", r#"{"scripts":{"build":"vite build"}}"#),
1161                ("custom/main.ts", "main"),
1162                ("configs/browser.ts", "config"),
1163                (
1164                    "tests/example.spec.ts",
1165                    "import { test } from '@playwright/test'",
1166                ),
1167            ],
1168        );
1169        let environment = BTreeMap::from([
1170            ("SUPERCOV_SOURCE_ROOTS".into(), "custom".into()),
1171            (
1172                "SUPERCOV_PLAYWRIGHT_CONFIG".into(),
1173                "configs/browser.ts".into(),
1174            ),
1175            ("SUPERCOV_PLAYWRIGHT_MODULE".into(), "@custom/test".into()),
1176            ("SUPERCOV_PLAYWRIGHT_TEST_EXPORT".into(), "scenario".into()),
1177        ]);
1178        let discovered = discover_coverage_project(&root, &environment, &[]).unwrap();
1179        assert_eq!(discovered.source_roots, ["custom"]);
1180        assert_eq!(
1181            discovered.playwright_config,
1182            Some(root.join("configs/browser.ts"))
1183        );
1184        assert_eq!(discovered.playwright_module, "@custom/test");
1185        assert_eq!(discovered.playwright_test_export, "scenario");
1186        assert_eq!(discovered.playwright_exports, ["scenario", "expect"]);
1187        fs::remove_dir_all(root).unwrap();
1188    }
1189}