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 manifest = package_json(root);
721    let configured_roots = crate::source_discovery::configured_source_roots(environment);
722    let DiscoveredSourceScope {
723        source_files,
724        source_roots,
725        scope,
726        limitations,
727    } = discover_source_scope(root, configured_roots.as_deref())?;
728    if source_files.is_empty() {
729        return Err(ProjectDiscoveryError::NoSourceFiles);
730    }
731    // The configuration the command names comes before any found by name: a
732    // root `playwright.config.ts` beside `--config e2e/playwright.config.ts`
733    // made the run load the root one, and run other tests than the command.
734    let expanded = expanded_command(root, command);
735    let playwright_config = configured_path(root, environment, "SUPERCOV_PLAYWRIGHT_CONFIG")
736        .or_else(|| configured_by_command(root, &expanded, "playwright"))
737        .or_else(|| first_config(root, PLAYWRIGHT_CONFIGS))
738        .or_else(|| nested_playwright_configs(root).into_iter().next());
739    let vitest_config = configured_path(root, environment, "SUPERCOV_VITEST_CONFIG")
740        .or_else(|| configured_by_command(root, &expanded, "vitest"))
741        .or_else(|| first_config(root, VITEST_CONFIGS));
742    let jest_config = configured_path(root, environment, "SUPERCOV_JEST_CONFIG")
743        .or_else(|| configured_by_command(root, &expanded, "jest"))
744        .or_else(|| first_config(root, JEST_CONFIGS));
745    let discovered_playwright = discover_playwright_adapter(root);
746    let playwright_module = environment
747        .get("SUPERCOV_PLAYWRIGHT_MODULE")
748        .cloned()
749        .unwrap_or_else(|| discovered_playwright.module.clone());
750    let playwright_test_export = environment
751        .get("SUPERCOV_PLAYWRIGHT_TEST_EXPORT")
752        .cloned()
753        .unwrap_or_else(|| {
754            if playwright_module == discovered_playwright.module {
755                discovered_playwright.test_export.clone()
756            } else {
757                "test".into()
758            }
759        });
760    let expanded_test_command = expanded;
761    let tokens = command_tokens(&expanded_test_command);
762    let uses_jest =
763        jest_config.is_some() || has_tool(&tokens, "jest") || manifest.get("jest").is_some();
764    let script_tokens = reachable_script_tokens(&manifest, &tokens);
765    let browser_runner = ["karma", "testem", "airtap", "zuul", "mochify"]
766        .iter()
767        .any(|tool| has_tool(&script_tokens, tool));
768    let source_transforming_runner = has_tool(&tokens, "jest") || has_tool(&tokens, "vitest");
769    let node_test = has_tool(&tokens, "node") && tokens.iter().any(|token| token == "--test");
770    let typescript_test = tokens.iter().any(|token| {
771        [".ts", ".tsx", ".cts", ".mts"]
772            .iter()
773            .any(|extension| token.ends_with(extension) || token.contains(&format!("{extension}*")))
774    });
775    let owns_build = ["vite", "tsc", "webpack", "rollup", "next", "remix"]
776        .iter()
777        .any(|tool| has_tool(&tokens, tool));
778    // Reading the suite is the expensive half of this question, so it stays on
779    // the right of `&&`: a command that never executes source directly answers
780    // it without parsing a single test file.
781    let executes_source_directly = (source_transforming_runner
782        || (node_test && typescript_test && !owns_build))
783        && !tests_require_build_output(root, &manifest);
784    // Supercov runs the command it is given and no build of its own. It used
785    // to run the project's `build` script first whenever the tests might
786    // need its output: twenty seconds nobody asked for before a suite that
787    // builds the application itself, in two of five popular repositories,
788    // and a run cancelled when that build failed. The build is only what a
789    // failed run names, when the command does not already reach it.
790    let build_script = script(&manifest, "build")
791        .map(command_tokens)
792        .unwrap_or_default();
793    let command_builds = !build_script.is_empty()
794        && build_script
795            .iter()
796            .all(|token| script_tokens.contains(token));
797    let unrun_build = if !build_script.is_empty() && !command_builds && !executes_source_directly {
798        vec![build_package_manager(command), "run".into(), "build".into()]
799    } else {
800        Vec::new()
801    };
802    let playwright_exports = if playwright_module == discovered_playwright.module {
803        discovered_playwright.exports
804    } else {
805        vec![playwright_test_export.clone(), "expect".into()]
806    };
807    Ok(CoverageProject {
808        root: root.to_owned(),
809        source_roots,
810        source_files,
811        source_scope: scope,
812        source_limitations: limitations,
813        playwright_config,
814        vitest_config,
815        jest_config,
816        uses_jest,
817        browser_runner,
818        playwright_module,
819        playwright_test_export,
820        playwright_exports,
821        unrun_build,
822    })
823}
824
825#[cfg(test)]
826mod tests {
827    use std::time::{SystemTime, UNIX_EPOCH};
828
829    use super::*;
830
831    fn project(label: &str, files: &[(&str, &str)]) -> PathBuf {
832        let nonce = SystemTime::now()
833            .duration_since(UNIX_EPOCH)
834            .unwrap()
835            .as_nanos();
836        let root = std::env::temp_dir().join(format!(
837            "supercov-project-{label}-{}-{nonce}",
838            std::process::id()
839        ));
840        fs::create_dir_all(&root).unwrap();
841        for (file, contents) in files {
842            let path = root.join(file);
843            fs::create_dir_all(path.parent().unwrap()).unwrap();
844            fs::write(path, contents).unwrap();
845        }
846        root
847    }
848
849    fn command(values: &[&str]) -> Vec<String> {
850        values.iter().map(|value| (*value).into()).collect()
851    }
852
853    #[test]
854    fn the_configuration_the_command_names_comes_first() {
855        // A root configuration beside the one the command names: the run
856        // loaded the root one and ran other tests than the command.
857        let root = project(
858            "named-config",
859            &[
860                (
861                    "package.json",
862                    r#"{"scripts":{"test:e2e":"playwright test --config e2e/Playwright.config.ts","test:unit":"vitest run -c config/vitest.unit.ts"}}"#,
863                ),
864                ("src/main.ts", "export const ready = true"),
865                ("playwright.config.ts", "export default {}"),
866                ("e2e/Playwright.config.ts", "export default {}"),
867                ("vitest.config.ts", "export default {}"),
868                ("config/vitest.unit.ts", "export default {}"),
869            ],
870        );
871        let discover = |args: &[&str]| {
872            discover_coverage_project(&root, &BTreeMap::new(), &command(args)).unwrap()
873        };
874        assert_eq!(
875            discover(&["npm", "run", "test:e2e"]).playwright_config,
876            Some(root.join("e2e/Playwright.config.ts"))
877        );
878        assert_eq!(
879            discover(&[
880                "npx",
881                "playwright",
882                "test",
883                "--config=e2e/Playwright.config.ts"
884            ])
885            .playwright_config,
886            Some(root.join("e2e/Playwright.config.ts"))
887        );
888        assert_eq!(
889            discover(&["npm", "run", "test:unit"]).vitest_config,
890            Some(root.join("config/vitest.unit.ts"))
891        );
892        // Named for another tool, or not at all: found by name as before.
893        assert_eq!(
894            discover(&["npx", "playwright", "test"]).playwright_config,
895            Some(root.join("playwright.config.ts"))
896        );
897        assert_eq!(
898            discover(&["npx", "vitest", "run", "--config", "missing.ts"]).vitest_config,
899            Some(root.join("vitest.config.ts"))
900        );
901        fs::remove_dir_all(root).unwrap();
902    }
903
904    #[test]
905    fn discovers_conventional_vite_playwright_and_vitest_configuration() {
906        let root = project(
907            "vite",
908            &[
909                (
910                    "package.json",
911                    r#"{"scripts":{"build":"vite build"},"devDependencies":{"vite":"1"}}"#,
912                ),
913                ("src/main.ts", "export const ready = true"),
914                ("playwright.config.ts", "export default {}"),
915                ("vitest.config.ts", "export default {}"),
916                (
917                    "tests/example.spec.ts",
918                    "import { test } from '@playwright/test'",
919                ),
920            ],
921        );
922        let discovered = discover_coverage_project(&root, &BTreeMap::new(), &[]).unwrap();
923        assert_eq!(discovered.source_roots, ["src"]);
924        assert_eq!(
925            discovered.playwright_config,
926            Some(root.join("playwright.config.ts"))
927        );
928        assert_eq!(
929            discovered.vitest_config,
930            Some(root.join("vitest.config.ts"))
931        );
932        assert_eq!(discovered.playwright_module, "@playwright/test");
933        assert_eq!(discovered.playwright_test_export, "test");
934        assert_eq!(discovered.playwright_exports, ["test"]);
935        // The command runs as given: the project\'s build is only what a
936        // failed run names.
937        assert_eq!(discovered.unrun_build, command(&["npm", "run", "build"]));
938        fs::remove_dir_all(root).unwrap();
939    }
940
941    #[test]
942    fn a_browser_runner_the_tests_reach_through_nested_scripts_is_found() {
943        // debug's `npm test` runs `npm run test:browser`, which runs karma.
944        let root = project(
945            "karma",
946            &[
947                (
948                    "package.json",
949                    r#"{"scripts":{"test":"npm run test:node && npm run test:browser","test:node":"mocha test.js","test:browser":"karma start --single-run"}}"#,
950                ),
951                ("src/index.js", "module.exports = 1"),
952            ],
953        );
954        let command = ["npm".to_owned(), "test".to_owned()];
955        let discovered = discover_coverage_project(&root, &BTreeMap::new(), &command).unwrap();
956        assert!(discovered.browser_runner);
957        let node_only = ["npm".to_owned(), "run".to_owned(), "test:node".to_owned()];
958        let discovered = discover_coverage_project(&root, &BTreeMap::new(), &node_only).unwrap();
959        assert!(!discovered.browser_runner);
960    }
961
962    #[test]
963    fn skips_unrelated_builds_for_source_transforming_and_node_test_suites() {
964        for (label, test_script, test_file) in [
965            ("jest", "jest", "require('../src/index.ts')"),
966            ("vitest", "vitest run", "import '../src/index.ts'"),
967            (
968                "node",
969                "node --test tests/*.test.ts",
970                "import '../src/index.ts'",
971            ),
972        ] {
973            let root = project(
974                label,
975                &[
976                    (
977                        "package.json",
978                        &format!(
979                            r#"{{"scripts":{{"build":"node build","test":"{test_script}"}}}}"#
980                        ),
981                    ),
982                    ("src/index.ts", "export const ready = true"),
983                    ("tests/index.test.ts", test_file),
984                ],
985            );
986            let discovered =
987                discover_coverage_project(&root, &BTreeMap::new(), &command(&["npm", "test"]))
988                    .unwrap();
989            assert!(discovered.unrun_build.is_empty(), "{label}");
990            fs::remove_dir_all(root).unwrap();
991        }
992    }
993
994    #[test]
995    fn names_the_build_with_the_package_manager_the_tests_were_started_with() {
996        let root = project(
997            "yarn-build",
998            &[
999                (
1000                    "package.json",
1001                    r#"{"scripts":{"build":"tsc","test":"jest --runInBand"}}"#,
1002                ),
1003                ("src/index.ts", "export const ready = true"),
1004                ("test/index.test.js", "require('../dist/index.js')"),
1005            ],
1006        );
1007        let discovered =
1008            discover_coverage_project(&root, &BTreeMap::new(), &command(&["yarn", "test"]))
1009                .unwrap();
1010        assert_eq!(discovered.unrun_build, command(&["yarn", "run", "build"]));
1011        fs::remove_dir_all(root).unwrap();
1012    }
1013
1014    #[test]
1015    fn names_the_build_when_tests_import_compiled_output() {
1016        let root = project(
1017            "compiled",
1018            &[
1019                (
1020                    "package.json",
1021                    r#"{"scripts":{"build":"tsc","test":"jest --runInBand"}}"#,
1022                ),
1023                ("src/index.ts", "export const ready = true"),
1024                ("test/index.test.js", "require('../dist/index.js')"),
1025            ],
1026        );
1027        let discovered =
1028            discover_coverage_project(&root, &BTreeMap::new(), &command(&["npm", "test"])).unwrap();
1029        assert_eq!(discovered.unrun_build, command(&["npm", "run", "build"]));
1030        assert!(discovered.uses_jest);
1031        fs::remove_dir_all(root).unwrap();
1032    }
1033
1034    #[test]
1035    fn names_the_build_when_source_direct_tests_spawn_a_compiled_package_script() {
1036        let root = project(
1037            "spawned-compiled-script",
1038            &[
1039                (
1040                    "package.json",
1041                    r#"{"scripts":{"build":"tsc","start":"node dist/index.js","test":"node --test tests/*.test.ts"}}"#,
1042                ),
1043                ("src/index.ts", "export const ready = true"),
1044                (
1045                    "tests/index.test.ts",
1046                    "import { spawn } from 'node:child_process';\nspawn('npm', ['run', 'start']);\n",
1047                ),
1048            ],
1049        );
1050        let discovered =
1051            discover_coverage_project(&root, &BTreeMap::new(), &command(&["npm", "test"])).unwrap();
1052        assert_eq!(discovered.unrun_build, command(&["npm", "run", "build"]));
1053        fs::remove_dir_all(root).unwrap();
1054    }
1055
1056    #[test]
1057    fn names_the_build_when_tests_exec_a_compiled_package_script_as_one_string() {
1058        // `execSync("npm run start")` hands the shell a single string. It is
1059        // how most suites start the server they test against, and the
1060        // array-only match let it through to the same hang as an unbuilt
1061        // gateway.
1062        let root = project(
1063            "exec-compiled-script",
1064            &[
1065                (
1066                    "package.json",
1067                    r#"{"scripts":{"build":"tsc","start":"node dist/index.js","test":"node --test tests/*.test.ts"}}"#,
1068                ),
1069                ("src/index.ts", "export const ready = true"),
1070                (
1071                    "tests/index.test.ts",
1072                    "import { execSync } from 'node:child_process';\nexecSync('npm run start');\n",
1073                ),
1074            ],
1075        );
1076        let discovered =
1077            discover_coverage_project(&root, &BTreeMap::new(), &command(&["npm", "test"])).unwrap();
1078        assert_eq!(discovered.unrun_build, command(&["npm", "run", "build"]));
1079        fs::remove_dir_all(root).unwrap();
1080    }
1081
1082    #[test]
1083    fn keeps_source_direct_when_a_spawned_script_only_runs_a_build_subcommand() {
1084        let root = project(
1085            "spawned-build-subcommand",
1086            &[
1087                (
1088                    "package.json",
1089                    r#"{"scripts":{"build":"tsc","dev":"vite build","test":"node --test tests/*.test.ts"}}"#,
1090                ),
1091                ("src/index.ts", "export const ready = true"),
1092                (
1093                    "tests/index.test.ts",
1094                    "import { spawn } from 'node:child_process';\nspawn('npm', ['run', 'dev']);\n",
1095                ),
1096            ],
1097        );
1098        let discovered =
1099            discover_coverage_project(&root, &BTreeMap::new(), &command(&["npm", "test"])).unwrap();
1100        assert!(discovered.unrun_build.is_empty());
1101        fs::remove_dir_all(root).unwrap();
1102    }
1103
1104    #[test]
1105    fn discovers_a_project_owned_playwright_fixture_via_the_ast() {
1106        let root = project(
1107            "fixture",
1108            &[
1109                ("package.json", r#"{"scripts":{"build":"vite build"}}"#),
1110                ("app/root.tsx", "export default null"),
1111                (
1112                    "tests/nested/playwright.browser.config.ts",
1113                    "export default {}",
1114                ),
1115                (
1116                    "tests/example.spec.ts",
1117                    "import { type Ignored, browserTest as test, expect, fixtureValue } from '@acme/browser-fixtures'\n\
1118                     import { createFixtureProduct } from '@acme/browser-fixtures'",
1119                ),
1120            ],
1121        );
1122        let discovered = discover_coverage_project(&root, &BTreeMap::new(), &[]).unwrap();
1123        assert_eq!(
1124            discovered.playwright_config,
1125            Some(root.join("tests/nested/playwright.browser.config.ts"))
1126        );
1127        assert_eq!(discovered.playwright_module, "@acme/browser-fixtures");
1128        assert_eq!(discovered.playwright_test_export, "browserTest");
1129        // The helper imported in its own statement must reach the shim: a
1130        // facade's non-test exports vanish otherwise, and every spec importing
1131        // one fails to link.
1132        assert_eq!(
1133            discovered.playwright_exports,
1134            [
1135                "browserTest",
1136                "createFixtureProduct",
1137                "expect",
1138                "fixtureValue"
1139            ]
1140        );
1141        fs::remove_dir_all(root).unwrap();
1142    }
1143
1144    #[test]
1145    fn environment_overrides_are_authoritative() {
1146        let root = project(
1147            "override",
1148            &[
1149                ("package.json", r#"{"scripts":{"build":"vite build"}}"#),
1150                ("custom/main.ts", "main"),
1151                ("configs/browser.ts", "config"),
1152                (
1153                    "tests/example.spec.ts",
1154                    "import { test } from '@playwright/test'",
1155                ),
1156            ],
1157        );
1158        let environment = BTreeMap::from([
1159            ("SUPERCOV_SOURCE_ROOTS".into(), "custom".into()),
1160            (
1161                "SUPERCOV_PLAYWRIGHT_CONFIG".into(),
1162                "configs/browser.ts".into(),
1163            ),
1164            ("SUPERCOV_PLAYWRIGHT_MODULE".into(), "@custom/test".into()),
1165            ("SUPERCOV_PLAYWRIGHT_TEST_EXPORT".into(), "scenario".into()),
1166        ]);
1167        let discovered = discover_coverage_project(&root, &environment, &[]).unwrap();
1168        assert_eq!(discovered.source_roots, ["custom"]);
1169        assert_eq!(
1170            discovered.playwright_config,
1171            Some(root.join("configs/browser.ts"))
1172        );
1173        assert_eq!(discovered.playwright_module, "@custom/test");
1174        assert_eq!(discovered.playwright_test_export, "scenario");
1175        assert_eq!(discovered.playwright_exports, ["scenario", "expect"]);
1176        fs::remove_dir_all(root).unwrap();
1177    }
1178}