summaryrefslogtreecommitdiffstats
path: root/vendor/sysinfo/tests/code_checkers/signals.rs
blob: 68e47f76e5a124e41ee731dbebbbdc3af11c5c77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Take a look at the license at the top of the repository in the LICENSE file.

use super::utils::{show_error, TestResult};
use std::path::Path;

fn check_supported_signals_decl<'a>(lines: &mut impl Iterator<Item = &'a str>, p: &Path) -> usize {
    for line in lines {
        let trimmed = line.trim();
        if trimmed.starts_with("const SUPPORTED_SIGNALS: &'static [Signal]") {
            if trimmed != "const SUPPORTED_SIGNALS: &'static [Signal] = supported_signals();" {
                show_error(
                    p,
                    "SystemExt::SUPPORTED_SIGNALS should be declared using `supported_signals()`",
                );
                return 1;
            }
            break;
        }
    }
    0
}

fn check_kill_decl<'a>(lines: &mut impl Iterator<Item = &'a str>, p: &Path) -> usize {
    let mut errors = 0;

    while let Some(line) = lines.next() {
        let trimmed = line.trim();
        if trimmed.starts_with("fn kill(") {
            show_error(p, "`ProcessExt::kill` should not be reimplemented!");
            errors += 1;
        } else if trimmed.starts_with("fn kill_with(") {
            if let Some(line) = lines.next() {
                let trimmed = line.trim();
                if trimmed.ends_with("::system::convert_signal(signal)?;") || trimmed == "None" {
                    continue;
                } else {
                    show_error(p, "`ProcessExt::kill_with` should use `convert_signal`");
                    errors += 1;
                }
            }
        }
    }
    errors
}

pub fn check_signals(content: &str, p: &Path) -> TestResult {
    let mut lines = content.lines();
    let mut res = TestResult {
        nb_tests: 0,
        nb_errors: 0,
    };

    while let Some(line) = lines.next() {
        let trimmed = line.trim();
        if trimmed.starts_with("impl SystemExt for System {") {
            res.nb_tests += 1;
            res.nb_errors += check_supported_signals_decl(&mut lines, p);
        } else if trimmed.starts_with("impl ProcessExt for Process {") {
            res.nb_tests += 1;
            res.nb_errors += check_kill_decl(&mut lines, p);
        }
    }
    res
}