Skip to main content

necessist_core/
core.rs

1use crate::{
2    __ToConsoleString, Backup, Outcome, Rewriter, SourceFile, Span, WarnFlags, Warning, config,
3    framework::{self, Applicable, Postprocess, SourceFileSpanTestMap, SpanKind, ToImplementation},
4    note, source_warn, sqlite, util, warn,
5};
6use ansi_term::Style;
7use anyhow::{Context as _, Result, anyhow, bail, ensure};
8use heck::ToKebabCase;
9use indexmap::IndexSet;
10use indicatif::ProgressBar;
11use itertools::{PeekNth, peek_nth};
12use log::debug;
13use once_cell::sync::OnceCell;
14use std::{
15    cell::RefCell,
16    collections::BTreeMap,
17    env::{current_dir, var},
18    fmt::Display,
19    io::{IsTerminal, Write},
20    iter::Peekable,
21    path::{Path, PathBuf},
22    process::{Command, ExitStatus as StdExitStatus, Stdio},
23    rc::Rc,
24    sync::atomic::{AtomicBool, Ordering},
25    time::Duration,
26};
27use strum::IntoEnumIterator;
28use subprocess::{Exec, ExitStatus};
29
30const DEFAULT_TIMEOUT: Duration = Duration::from_mins(1);
31
32static CTRLC: AtomicBool = AtomicBool::new(false);
33
34#[derive(Clone)]
35pub(crate) struct Removal {
36    pub span: Span,
37    pub text: String,
38    pub outcome: Outcome,
39}
40
41#[derive(Debug)]
42enum MismatchKind {
43    Missing,
44    Unexpected,
45}
46
47impl Display for MismatchKind {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        write!(f, "{}", format!("{self:?}").to_kebab_case())
50    }
51}
52
53struct Mismatch {
54    kind: MismatchKind,
55    removal: Removal,
56}
57
58struct Context<'a> {
59    opts: Necessist,
60    root: Rc<PathBuf>,
61    println: &'a dyn Fn(&dyn AsRef<str>),
62    backend: Box<dyn framework::Interface>,
63    progress: Option<&'a ProgressBar>,
64}
65
66impl Context<'_> {
67    fn light(&self) -> LightContext<'_> {
68        LightContext {
69            opts: &self.opts,
70            root: &self.root,
71            println: self.println,
72        }
73    }
74}
75
76pub struct LightContext<'a> {
77    pub opts: &'a Necessist,
78    pub root: &'a Rc<PathBuf>,
79    pub println: &'a dyn Fn(&dyn AsRef<str>),
80}
81
82#[allow(clippy::struct_excessive_bools)]
83#[derive(Clone, Default)]
84pub struct Necessist {
85    pub allow: Vec<Warning>,
86    pub default_config: bool,
87    pub deny: Vec<Warning>,
88    pub dump: bool,
89    pub dump_candidate_counts: bool,
90    pub dump_candidates: bool,
91    pub no_lines_or_columns: bool,
92    pub no_local_functions: bool,
93    pub no_sqlite: bool,
94    pub quiet: bool,
95    pub reset: bool,
96    pub resume: bool,
97    pub root: Option<PathBuf>,
98    pub timeout: Option<u64>,
99    pub verbose: bool,
100    pub source_files: Vec<PathBuf>,
101    pub args: Vec<String>,
102}
103
104/// Necessist's main entrypoint.
105// smoelius: The reason `framework` is not included as a field in `Necessist` is to avoid having
106// to parameterize every function that takes a `Necessist` as an argument.
107pub fn necessist<Identifier: Applicable + Display + IntoEnumIterator + ToImplementation>(
108    opts: &Necessist,
109    framework: framework::Auto<Identifier>,
110) -> Result<()> {
111    let opts = opts.clone();
112
113    process_options(&opts)?;
114
115    let root = opts
116        .root
117        .as_ref()
118        .map_or_else(current_dir, dunce::canonicalize)
119        .map(Rc::new)?;
120
121    #[cfg(feature = "lock_root")]
122    let _file: std::fs::File = lock_root(&root)?;
123
124    let mut context = LightContext {
125        opts: &opts,
126        root: &root,
127        println: &|_| {},
128    };
129
130    let println = |msg: &dyn AsRef<str>| {
131        println!("{}", msg.as_ref());
132    };
133
134    if !opts.quiet {
135        context.println = &println;
136    }
137
138    if opts.no_local_functions {
139        warn(
140            &context,
141            Warning::OptionDeprecated,
142            "--no-local-functions is now the default; hence, this option is deprecated",
143            WarnFlags::empty(),
144        )?;
145    }
146
147    let Some((backend, n_spans, source_file_span_test_map)) = prepare(&context, framework)? else {
148        return Ok(());
149    };
150
151    let mut context = Context {
152        opts,
153        root,
154        println: &|_| {},
155        backend,
156        progress: None,
157    };
158
159    if !context.opts.quiet {
160        context.println = &println;
161    }
162
163    let progress =
164        if var("RUST_LOG").is_err() && !context.opts.quiet && std::io::stdout().is_terminal() {
165            Some(ProgressBar::new(n_spans as u64))
166        } else {
167            None
168        };
169
170    let progress_println = |msg: &dyn AsRef<str>| {
171        // SAFETY: This closure should only be called when `progress` is `Some`.
172        // The unwrap is intentional - we want to panic if this invariant is violated.
173        #[allow(clippy::unwrap_used)]
174        progress.as_ref().unwrap().println(msg);
175    };
176
177    // Only set this function as the printer when `progress` exists
178    if progress.is_some() {
179        context.println = &progress_println;
180        context.progress = progress.as_ref();
181    }
182
183    run(context, source_file_span_test_map)
184}
185
186#[allow(clippy::type_complexity)]
187#[cfg_attr(dylint_lib = "supplementary", allow(commented_out_code))]
188fn prepare<Identifier: Applicable + Display + IntoEnumIterator + ToImplementation>(
189    context: &LightContext,
190    framework: framework::Auto<Identifier>,
191) -> Result<Option<(Box<dyn framework::Interface>, usize, SourceFileSpanTestMap)>> {
192    if context.opts.default_config {
193        default_config(context, context.root)?;
194        return Ok(None);
195    }
196
197    let config = config::Toml::read(context, context.root)?;
198
199    if context.opts.dump {
200        let past_removals = past_removals_init_lazy(context)?;
201        dump(context, &past_removals);
202        return Ok(None);
203    }
204
205    let mut backend = backend_for_framework(context, framework)?;
206
207    let paths = canonicalize_source_files(context)?;
208
209    let (n_tests, source_file_span_test_map) = backend.parse(
210        context,
211        &config,
212        &paths.iter().map(AsRef::as_ref).collect::<Vec<_>>(),
213    )?;
214
215    let n_spans = source_file_span_test_map
216        .values()
217        .map(|span_test_maps| {
218            span_test_maps
219                .statement
220                .values()
221                .map(IndexSet::len)
222                .sum::<usize>()
223                + span_test_maps
224                    .method_call
225                    .values()
226                    .map(IndexSet::len)
227                    .sum::<usize>()
228        })
229        .sum();
230
231    if context.opts.dump_candidates {
232        dump_candidates(context, &source_file_span_test_map)?;
233        return Ok(None);
234    }
235
236    if context.opts.dump_candidate_counts {
237        dump_candidate_counts(context, &source_file_span_test_map);
238        return Ok(None);
239    }
240
241    // smoelius: Curious. The code used to look like:
242    // ```
243    //     (context.println)({
244    //         let n_source_files = source_file_span_test_map.keys().len();
245    //         &format!(
246    //             ...
247    //         )
248    //     });
249    // ```
250    // But with Rust Edition 2024, that would cause the compiler to say:
251    // ```
252    // error[E0716]: temporary value dropped while borrowed
253    //    --> core/src/core.rs:233:10
254    //     |
255    // 233 |            &format!(
256    //     |   _________-^
257    //     |  |__________|
258    // 234 | ||             "{} candidates in {} test{} in {} source file{}",
259    // 235 | ||             n_spans,
260    // 236 | ||             n_tests,
261    // ...   ||
262    // 239 | ||             if n_source_files == 1 { "" } else { "s" }
263    // 240 | ||         )
264    //     | ||         ^
265    //     | ||         |
266    //     | ||_________temporary value is freed at the end of this statement
267    //     |  |_________creates a temporary value which is freed while still in use
268    //     |            borrow later used here
269    //     |
270    //     = note: consider using a `let` binding to create a longer lived value
271    //     = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
272    // ```
273    let n_source_files = source_file_span_test_map.keys().len();
274    (context.println)(&format!(
275        "{} candidates in {} test{} in {} source file{}",
276        n_spans,
277        n_tests,
278        if n_tests == 1 { "" } else { "s" },
279        n_source_files,
280        if n_source_files == 1 { "" } else { "s" }
281    ));
282
283    Ok(Some((backend, n_spans, source_file_span_test_map)))
284}
285
286fn run(mut context: Context, source_file_span_test_map: SourceFileSpanTestMap) -> Result<()> {
287    ctrlc::set_handler(|| CTRLC.store(true, Ordering::SeqCst))?;
288
289    let past_removals = past_removals_init_lazy(&context.light())?;
290
291    let mut past_removal_iter = past_removals.into_iter().peekable();
292
293    for (source_file, span_test_maps) in source_file_span_test_map {
294        let mut span_test_iter = peek_nth(span_test_maps.iter());
295
296        let (mismatch, n) = skip_past_removals(&mut span_test_iter, &mut past_removal_iter);
297
298        update_progress(&context, mismatch, n)?;
299
300        if span_test_iter.peek().is_none() {
301            continue;
302        }
303
304        (context.println)(&format!(
305            "{}: dry running",
306            util::strip_current_dir(&source_file).to_string_lossy()
307        ));
308
309        let result = context.backend.dry_run(&context.light(), &source_file);
310
311        if let Err(error) = &result {
312            source_warn(
313                &context.light(),
314                Warning::DryRunFailed,
315                &source_file,
316                &format!("dry run failed: {error:?}"),
317                WarnFlags::empty(),
318            )?;
319        }
320
321        if CTRLC.load(Ordering::SeqCst) {
322            bail!("Ctrl-C detected");
323        }
324
325        if result.is_err() {
326            let n = skip_present_spans(&context, span_test_iter)?;
327            update_progress(&context, None, n)?;
328            continue;
329        }
330
331        (context.println)(&format!(
332            "{}: mutilating",
333            util::strip_current_dir(&source_file).to_string_lossy()
334        ));
335
336        let mut instrumentation_backup =
337            instrument_statements(&context, &source_file, &mut span_test_iter)?;
338
339        loop {
340            let (mismatch, n) = skip_past_removals(&mut span_test_iter, &mut past_removal_iter);
341
342            update_progress(&context, mismatch, n)?;
343
344            let Some((span, span_kind, test_names)) = span_test_iter.next() else {
345                break;
346            };
347
348            if span_kind != SpanKind::Statement {
349                drop(instrumentation_backup.take());
350            }
351
352            let text = span.source_text()?;
353
354            let explicit_removal =
355                instrumentation_backup.is_none() || span_kind != SpanKind::Statement;
356
357            let _explicit_backup = if explicit_removal {
358                let (_, explicit_backup) = span.remove()?;
359                Some(explicit_backup)
360            } else {
361                None
362            };
363
364            let outcome =
365                test_names
366                    .into_iter()
367                    .try_fold(Some(Outcome::Passed), |outcome, test_name| {
368                        if outcome != Some(Outcome::Passed) {
369                            return Ok(outcome);
370                        }
371
372                        let result = context.backend.exec(&context.light(), test_name, span)?;
373
374                        match result {
375                            Ok((exec, postprocess)) => {
376                                // smoelius: Even if the removal is explicit (i.e., not with
377                                // instrumentation), it doesn't hurt to set `NECESSIST_REMOVAL`.
378                                let exec = exec.env("NECESSIST_REMOVAL", span.id());
379
380                                perform_exec(&context, exec, postprocess)
381                            }
382                            Err(error) => {
383                                assert!(
384                                    explicit_removal,
385                                    "Instrumentation failed to build after it was verified to: \
386                                     {error}"
387                                );
388
389                                Ok(Some(Outcome::Nonbuildable))
390                            }
391                        }
392                    })?;
393
394            if CTRLC.load(Ordering::SeqCst) {
395                bail!("Ctrl-C detected");
396            }
397
398            if let Some(outcome) = outcome {
399                emit(&mut context, span, &text, outcome)?;
400            }
401
402            update_progress(&context, None, 1)?;
403        }
404    }
405
406    context.progress.map(ProgressBar::finish);
407
408    Ok(())
409}
410
411macro_rules! incompatible {
412    ($opts:ident, $x:ident, $y:ident) => {
413        ensure!(
414            !($opts.$x && $opts.$y),
415            "--{} and --{} are incompatible",
416            stringify!($x).to_kebab_case(),
417            stringify!($y).to_kebab_case()
418        );
419    };
420}
421
422fn process_options(opts: &Necessist) -> Result<()> {
423    // smoelius: This list of incompatibilities is not exhaustive.
424    incompatible!(opts, dump, quiet);
425    incompatible!(opts, dump, reset);
426    incompatible!(opts, dump, resume);
427    incompatible!(opts, dump, no_sqlite);
428    incompatible!(opts, quiet, verbose);
429    incompatible!(opts, reset, no_sqlite);
430    incompatible!(opts, resume, no_sqlite);
431
432    Ok(())
433}
434
435#[cfg(feature = "lock_root")]
436fn lock_root(root: &Path) -> Result<std::fs::File> {
437    if enabled("TRYCMD") {
438        crate::flock::lock_path(root)
439    } else {
440        crate::flock::try_lock_path(root)
441    }
442    .with_context(|| format!("Failed to lock `{}`", root.display()))
443}
444
445#[cfg(feature = "lock_root")]
446fn enabled(key: &str) -> bool {
447    var(key).is_ok_and(|value| value != "0")
448}
449
450fn default_config(_context: &LightContext, root: &Path) -> Result<()> {
451    let path_buf = root.join("necessist.toml");
452
453    if path_buf.try_exists()? {
454        bail!(
455            "A configuration file already exists at `{}`",
456            path_buf.display()
457        );
458    }
459
460    let toml = toml::to_string(&config::Toml::default())?;
461
462    std::fs::write(path_buf, toml).map_err(Into::into)
463}
464
465fn dump(context: &LightContext, removals: &[Removal]) {
466    let mut other_than_passed = false;
467    for removal in removals {
468        emit_to_console(context, removal);
469        other_than_passed |= removal.outcome != Outcome::Passed;
470    }
471
472    if !context.opts.verbose && other_than_passed {
473        note(context, "More output would be produced with --verbose");
474    }
475}
476
477fn backend_for_framework<Identifier: Applicable + Display + IntoEnumIterator + ToImplementation>(
478    context: &LightContext,
479    identifier: framework::Auto<Identifier>,
480) -> Result<Box<dyn framework::Interface>> {
481    let implementation = identifier.to_implementation(context)?;
482
483    drop(identifier);
484
485    implementation.ok_or_else(|| anyhow!("Found no applicable frameworks"))
486}
487
488fn canonicalize_source_files(context: &LightContext) -> Result<Vec<PathBuf>> {
489    context
490        .opts
491        .source_files
492        .iter()
493        .map(|path| {
494            let path_buf = dunce::canonicalize(path)
495                .with_context(|| format!("Failed to canonicalize `{}`", path.display()))?;
496            ensure!(
497                path_buf.starts_with(context.root.as_path()),
498                "`{}` is not in `{}`",
499                path_buf.display(),
500                context.root.display()
501            );
502            Ok(path_buf)
503        })
504        .collect::<Result<Vec<_>>>()
505}
506
507#[must_use]
508fn skip_past_removals<'a, I, J>(
509    span_test_iter: &mut PeekNth<I>,
510    removal_iter: &mut Peekable<J>,
511) -> (Option<Mismatch>, usize)
512where
513    I: Iterator<Item = (&'a Span, SpanKind, &'a IndexSet<String>)>,
514    J: Iterator<Item = Removal>,
515{
516    let mut mismatch = None;
517    let mut n = 0;
518    while let Some(&(span, _, _)) = span_test_iter.peek() {
519        let Some(removal) = removal_iter.peek() else {
520            break;
521        };
522        match span.cmp(&removal.span) {
523            std::cmp::Ordering::Less => {
524                mismatch = Some(Mismatch {
525                    kind: MismatchKind::Unexpected,
526                    removal: removal.clone(),
527                });
528                break;
529            }
530            std::cmp::Ordering::Equal => {
531                let _: Option<(&Span, _, _)> = span_test_iter.next();
532                let _removal: Option<Removal> = removal_iter.next();
533                n += 1;
534            }
535            std::cmp::Ordering::Greater => {
536                if mismatch.is_none() {
537                    mismatch = Some(Mismatch {
538                        kind: MismatchKind::Missing,
539                        removal: removal.clone(),
540                    });
541                }
542                let _removal: Option<Removal> = removal_iter.next();
543            }
544        }
545    }
546
547    (mismatch, n)
548}
549
550fn skip_present_spans<'a>(
551    context: &Context,
552    span_test_iter: impl IntoIterator<Item = (&'a Span, SpanKind, &'a IndexSet<String>)>,
553) -> Result<usize> {
554    let mut n = 0;
555
556    let sqlite = sqlite_init_lazy(&context.light())?;
557
558    for (span, _, _) in span_test_iter {
559        if let Some(sqlite) = sqlite.borrow_mut().as_mut() {
560            let text = span.source_text()?;
561            let removal = Removal {
562                span: span.clone(),
563                text,
564                outcome: Outcome::Skipped,
565            };
566            sqlite::insert(sqlite, &removal)?;
567        }
568        n += 1;
569    }
570
571    Ok(n)
572}
573
574fn update_progress(context: &Context, mismatch: Option<Mismatch>, n: usize) -> Result<()> {
575    if let Some(Mismatch {
576        kind,
577        removal: Removal { span, text, .. },
578    }) = mismatch
579    {
580        warn(
581            &context.light(),
582            Warning::FilesChanged,
583            &format!(
584                "\
585Configuration or source files have changed since necessist.db was created; the following entry is \
586                 {kind}:
587    {}: `{}`",
588                span.to_console_string(),
589                text.replace('\r', ""),
590            ),
591            WarnFlags::ONCE,
592        )?;
593    }
594
595    if let Some(bar) = context.progress {
596        bar.inc(n as u64);
597    }
598
599    Ok(())
600}
601
602fn dump_candidates(
603    context: &LightContext,
604    source_file_span_test_map: &SourceFileSpanTestMap,
605) -> Result<()> {
606    for span in source_file_span_test_map
607        .values()
608        .flat_map(|span_test_maps| {
609            span_test_maps
610                .statement
611                .keys()
612                .chain(span_test_maps.method_call.keys())
613        })
614    {
615        let loc = if context.opts.no_lines_or_columns {
616            span.source_file().to_console_string()
617        } else {
618            span.to_console_string()
619        };
620        let text = span.source_text()?;
621
622        (context.println)(&format!("{loc}: `{}`", text.replace('\r', "")));
623    }
624
625    Ok(())
626}
627
628fn dump_candidate_counts(
629    context: &LightContext,
630    source_file_span_test_map: &SourceFileSpanTestMap,
631) {
632    let mut candidate_counts = source_file_span_test_map
633        .iter()
634        .map(|(source_file, span_test_maps)| {
635            (
636                span_test_maps.statement.keys().count() + span_test_maps.method_call.keys().count(),
637                source_file,
638            )
639        })
640        .collect::<Vec<_>>();
641
642    candidate_counts.sort();
643
644    let Some(width) = candidate_counts
645        .iter()
646        .map(|(count, _)| count.to_string().len())
647        .max()
648    else {
649        return;
650    };
651
652    for (count, source_file) in candidate_counts {
653        (context.println)(&format!(
654            "{count:width$} {}",
655            source_file.to_console_string(),
656        ));
657    }
658}
659
660fn instrument_statements<'a, I>(
661    context: &Context,
662    source_file: &SourceFile,
663    span_test_iter: &mut PeekNth<I>,
664) -> Result<Option<Backup>>
665where
666    I: Iterator<Item = (&'a Span, SpanKind, &'a IndexSet<String>)>,
667{
668    let backup = Backup::new(source_file)?;
669
670    let mut rewriter =
671        Rewriter::with_offset_calculator(source_file.contents(), source_file.offset_calculator());
672
673    let n_instrumentable_statements = count_instrumentable_statements(span_test_iter);
674
675    context.backend.instrument_source_file(
676        &context.light(),
677        &mut rewriter,
678        source_file,
679        n_instrumentable_statements,
680    )?;
681
682    let mut i_span = 0;
683    let mut insertion_map = BTreeMap::<_, Vec<_>>::new();
684    // smoelius: Do not advance the underlying iterator while instrumenting. This way, if a
685    // statement cannot be removed with instrumentation, it will be removed explicitly.
686    while let Some((span, SpanKind::Statement, _)) = span_test_iter.peek_nth(i_span) {
687        let (prefix, suffix) = context.backend.statement_prefix_and_suffix(span)?;
688        let insertions = insertion_map.entry(span.start()).or_default();
689        insertions.push(prefix);
690        let insertions = insertion_map.entry(span.end()).or_default();
691        insertions.push(suffix);
692        i_span += 1;
693    }
694
695    assert_eq!(n_instrumentable_statements, i_span);
696
697    for (line_column, insertions) in insertion_map {
698        for insertion in insertions {
699            source_file.insert(&mut rewriter, line_column, &insertion);
700        }
701    }
702
703    let mut file = std::fs::OpenOptions::new()
704        .truncate(true)
705        .write(true)
706        .open(source_file)?;
707    file.write_all(rewriter.contents().as_bytes())?;
708    drop(file);
709
710    let result = context
711        .backend
712        .build_source_file(&context.light(), source_file);
713    if let Err(error) = result {
714        warn(
715            &context.light(),
716            Warning::InstrumentationNonbuildable,
717            &format!(
718                "Instrumentation caused `{}` to be nonbuildable: {error:?}",
719                source_file.to_console_string(),
720            ),
721            WarnFlags::empty(),
722        )?;
723        return Ok(None);
724    }
725
726    Ok(Some(backup))
727}
728
729fn count_instrumentable_statements<'a, I>(span_test_iter: &mut PeekNth<I>) -> usize
730where
731    I: Iterator<Item = (&'a Span, SpanKind, &'a IndexSet<String>)>,
732{
733    let mut n_instrumentable_statements = 0;
734    while matches!(
735        span_test_iter.peek_nth(n_instrumentable_statements),
736        Some((_, SpanKind::Statement, _))
737    ) {
738        n_instrumentable_statements += 1;
739    }
740    n_instrumentable_statements
741}
742
743fn perform_exec(
744    context: &Context,
745    exec: Exec,
746    postprocess: Option<Box<Postprocess>>,
747) -> Result<Option<Outcome>> {
748    debug!("{exec:?}");
749
750    #[cfg(all(feature = "limit_threads", unix))]
751    let nprocs_prev = rlimit::set_soft_rlimit(
752        rlimit::Resource::NPROC,
753        *rlimit::NPROC_INIT + rlimit::NPROC_ALLOWANCE,
754    )?;
755
756    let job = exec.start()?;
757    let status = if let Some(dur) = timeout(&context.opts) {
758        job.wait_timeout(dur)?
759    } else {
760        job.wait().map(Option::Some)?
761    };
762
763    #[cfg(all(feature = "limit_threads", unix))]
764    rlimit::set_soft_rlimit(rlimit::Resource::NPROC, nprocs_prev)?;
765
766    if status.is_some() {
767        if let Some(postprocess) = postprocess
768            && !postprocess(&context.light(), job)?
769        {
770            return Ok(None);
771        }
772    } else {
773        let pid = job.pid();
774        transitive_kill(pid)?;
775        let _: ExitStatus = job.wait()?;
776    }
777
778    let Some(status) = status else {
779        return Ok(Some(Outcome::TimedOut));
780    };
781
782    Ok(Some(if status.success() {
783        Outcome::Passed
784    } else {
785        Outcome::Failed
786    }))
787}
788
789#[cfg_attr(dylint_lib = "general", allow(non_local_effect_before_error_return))]
790fn emit(context: &mut Context, span: &Span, text: &str, outcome: Outcome) -> Result<()> {
791    let removal = Removal {
792        span: span.clone(),
793        text: text.to_owned(),
794        outcome,
795    };
796
797    let sqlite = sqlite_init_lazy(&context.light())?;
798
799    if let Some(sqlite) = sqlite.borrow_mut().as_mut() {
800        sqlite::insert(sqlite, &removal)?;
801    }
802
803    emit_to_console(&context.light(), &removal);
804
805    Ok(())
806}
807
808fn emit_to_console(context: &LightContext, removal: &Removal) {
809    let Removal {
810        span,
811        text,
812        outcome,
813    } = removal;
814
815    if !context.opts.quiet && (context.opts.verbose || *outcome == Outcome::Passed) {
816        let loc = if context.opts.no_lines_or_columns {
817            span.source_file().to_console_string()
818        } else {
819            span.to_console_string()
820        };
821        let msg = format!(
822            "{loc}: `{}` {}",
823            text.replace('\r', ""),
824            if std::io::stdout().is_terminal() {
825                outcome.style().bold()
826            } else {
827                Style::default()
828            }
829            .paint(outcome.to_string())
830        );
831        (context.println)(&msg);
832    }
833}
834
835fn sqlite_init_lazy(context: &LightContext) -> Result<Rc<RefCell<Option<sqlite::Sqlite>>>> {
836    let (sqlite, _) = sqlite_and_past_removals_init_lazy(context)?;
837    Ok(sqlite)
838}
839
840fn past_removals_init_lazy(context: &LightContext) -> Result<Vec<Removal>> {
841    let (_, past_removals) = sqlite_and_past_removals_init_lazy(context)?;
842    Ok(past_removals.take())
843}
844
845thread_local! {
846    #[allow(clippy::type_complexity)]
847    static SQLITE_AND_PAST_REMOVALS: OnceCell<(
848        Rc<RefCell<Option<sqlite::Sqlite>>>,
849        Rc<RefCell<Vec<Removal>>>,
850    )> = const { OnceCell::new() };
851}
852
853#[allow(clippy::type_complexity)]
854fn sqlite_and_past_removals_init_lazy(
855    context: &LightContext,
856) -> Result<(
857    Rc<RefCell<Option<sqlite::Sqlite>>>,
858    Rc<RefCell<Vec<Removal>>>,
859)> {
860    SQLITE_AND_PAST_REMOVALS.with(|sqlite_and_past_removals| {
861        sqlite_and_past_removals
862            .get_or_try_init(|| {
863                if context.opts.no_sqlite {
864                    Ok((
865                        Rc::new(RefCell::new(None)),
866                        Rc::new(RefCell::new(Vec::new())),
867                    ))
868                } else {
869                    let (sqlite, mut past_removals) = sqlite::init(
870                        context,
871                        context.root,
872                        context.opts.dump,
873                        context.opts.reset,
874                        context.opts.resume,
875                    )?;
876                    past_removals.sort_by(|left, right| left.span.cmp(&right.span));
877                    Ok((
878                        Rc::new(RefCell::new(Some(sqlite))),
879                        Rc::new(RefCell::new(past_removals)),
880                    ))
881                }
882            })
883            .cloned()
884    })
885}
886
887#[allow(clippy::module_name_repetitions)]
888#[cfg(all(feature = "limit_threads", unix))]
889mod rlimit {
890    use anyhow::Result;
891    pub use rlimit::Resource;
892    use rlimit::{getrlimit, setrlimit};
893    use std::{process::Command, sync::LazyLock};
894
895    #[allow(clippy::unwrap_used)]
896    pub static NPROC_INIT: LazyLock<u64> = LazyLock::new(|| {
897        let output = Command::new("ps").arg("-eL").output().unwrap();
898        let stdout = std::str::from_utf8(&output.stdout).unwrap();
899        stdout.lines().count().try_into().unwrap()
900    });
901
902    // smoelius: Limit the number of threads that a test can allocate to approximately 1024 (an
903    // arbitrary choice).
904    //
905    // The limit is not strict for the following reason. `NPROC_INIT` counts the number of threads
906    // *started by any user*. But `setrlimit` (used to enforce the limit) applies to just the
907    // current user. So by setting the limit to `NPROC_INIT + NPROC_ALLOWANCE`, the number of
908    // threads the test can allocate is actually 1024 plus the number of threads started by other
909    // users.
910    pub const NPROC_ALLOWANCE: u64 = 1024;
911
912    pub fn set_soft_rlimit(resource: Resource, limit: u64) -> Result<u64> {
913        let (soft, hard) = getrlimit(resource)?;
914        setrlimit(Resource::NPROC, std::cmp::min(hard, limit), hard)?;
915        Ok(soft)
916    }
917}
918
919fn timeout(opts: &Necessist) -> Option<Duration> {
920    match opts.timeout {
921        None => Some(DEFAULT_TIMEOUT),
922        Some(0) => None,
923        Some(secs) => Some(Duration::from_secs(secs)),
924    }
925}
926
927#[cfg_attr(dylint_lib = "supplementary", allow(commented_out_code))]
928fn transitive_kill(pid: u32) -> Result<()> {
929    let mut pids = vec![(pid, false)];
930
931    while let Some((pid, visited)) = pids.pop() {
932        if visited {
933            let _status: StdExitStatus = kill()
934                .arg(pid.to_string())
935                .stdout(Stdio::null())
936                .stderr(Stdio::null())
937                .status()?;
938            // smoelius: The process may have already exited.
939            // ensure!(status.success());
940        } else {
941            pids.push((pid, true));
942
943            for line in child_processes(pid)? {
944                let pid = line
945                    .parse::<u32>()
946                    .with_context(|| format!("failed to parse `{line}`"))?;
947                pids.push((pid, false));
948            }
949        }
950    }
951
952    Ok(())
953}
954
955#[cfg(not(windows))]
956fn kill() -> Command {
957    Command::new("kill")
958}
959
960#[cfg(windows)]
961fn kill() -> Command {
962    let mut command = Command::new("taskkill");
963    command.args(["/f", "/pid"]);
964    command
965}
966
967#[cfg(not(windows))]
968fn child_processes(pid: u32) -> Result<Vec<String>> {
969    let output = Command::new("pgrep")
970        .args(["-P", &pid.to_string()])
971        .output()?;
972    let stdout = String::from_utf8(output.stdout)?;
973    Ok(stdout.lines().map(ToOwned::to_owned).collect())
974}
975
976#[cfg(windows)]
977fn child_processes(pid: u32) -> Result<Vec<String>> {
978    let mut command = Command::new("powershell");
979    command.arg(format!(
980        r#"(Get-CimInstance -ClassName Win32_Process -Filter "ParentProcessId = {pid}").ProcessId"#
981    ));
982    let output = command.output()?;
983    let stdout = String::from_utf8(output.stdout)?;
984    Ok(stdout.lines().map(|s| s.trim_end().to_owned()).collect())
985}