Skip to main content

pacenotes_installer\selftest/
conflict_report_check.rs

1/// `--selftest-report <game> [--server URL] [--out FILE]`: posts a fabricated
2/// conflict report, the only way to exercise the POST path, since a real one
3/// needs a catalogue where two packages write one file. The overlap is built as
4/// `engine::overlaps` builds one, so the keys, refs and trim are the real ones.
5pub struct ConflictReportSelfTest {
6    game: String,
7    server: String,
8    out: Option<std::path::PathBuf>,
9}
10
11impl ConflictReportSelfTest {
12    pub fn from_args() -> Option<ConflictReportSelfTest> {
13        let mut arguments = std::env::args().skip(1);
14        let mut game = None;
15        let mut server = None;
16        let mut out = None;
17        while let Some(argument) = arguments.next() {
18            match argument.as_str() {
19                "--selftest-report" => game = arguments.next(),
20                "--server" => server = arguments.next(),
21                "--out" => out = arguments.next().map(std::path::PathBuf::from),
22                _ => {}
23            }
24        }
25        game.map(|game| ConflictReportSelfTest {
26            game,
27            server: server.unwrap_or_else(|| crate::DEFAULT_SERVER.to_string()),
28            out,
29        })
30    }
31
32    /// The process exit code: 0 when the server took the report.
33    pub fn run(&self) -> i32 {
34        let cancel = std::sync::atomic::AtomicBool::new(false);
35        let api = crate::api::Api::new(&self.server, None);
36        let overlaps = vec![pacenotes_installer::engine::Overlap {
37            path: "Plugins/Pacenote/PaceNote.ini".to_string(),
38            packages: vec![
39                format!("{}/public/language/selftest", self.game),
40                format!("{}/public/codriver/selftest", self.game),
41            ],
42        }];
43
44        let (exit_code, report) = match api.report_conflicts(&self.game, &overlaps, &cancel) {
45            Ok(()) => (
46                0,
47                format!(
48                    "RESULT: OK\nGAME: {}\nCONFLICTS: {}\n",
49                    self.game,
50                    overlaps.len()
51                ),
52            ),
53            Err(e) => (
54                1,
55                format!(
56                    "RESULT: FAIL\nGAME: {}\nSTATUS: {}\nMESSAGE: {}\n",
57                    self.game,
58                    e.status
59                        .map_or_else(|| "-".to_string(), |status| status.to_string()),
60                    e.message
61                ),
62            ),
63        };
64        super::report::deliver(self.out.as_deref(), &report, exit_code)
65    }
66}