aoc-runtime 0.7.0

a runtime automation tool for Advent of Code: scaffold, run and submit puzzle solutions
Documentation
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Child process execution.
//!
//! Commands are described as data ([`CommandSpec`]) and executed through the
//! [`CommandRunner`] trait, so the per-language command tables can be asserted
//! in tests without ever spawning `cargo`, `dotnet` or `javac`.

use std::{
    ffi::{OsStr, OsString},
    fmt::Write as _,
    io,
    path::{Path, PathBuf},
    process::{Command, Stdio},
};

/// A fully described child process invocation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandSpec {
    program: OsString,
    args: Vec<OsString>,
    current_dir: Option<PathBuf>,
}

impl CommandSpec {
    /// Creates a spec for `program` with no arguments.
    pub fn new(program: impl AsRef<OsStr>) -> Self {
        Self {
            program: program.as_ref().to_os_string(),
            args: Vec::new(),
            current_dir: None,
        }
    }

    /// Appends a single argument.
    #[must_use]
    pub fn arg(mut self, arg: impl AsRef<OsStr>) -> Self {
        self.args.push(arg.as_ref().to_os_string());
        self
    }

    /// Appends multiple arguments.
    #[must_use]
    pub fn args<I, S>(mut self, args: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<OsStr>,
    {
        self.args
            .extend(args.into_iter().map(|arg| arg.as_ref().to_os_string()));
        self
    }

    /// Sets the working directory the command runs in.
    #[must_use]
    pub fn current_dir(mut self, dir: impl AsRef<Path>) -> Self {
        self.current_dir = Some(dir.as_ref().to_path_buf());
        self
    }

    /// The program to execute.
    #[must_use]
    pub fn program(&self) -> &OsStr {
        &self.program
    }

    /// The arguments passed to the program.
    #[must_use]
    pub fn arguments(&self) -> &[OsString] {
        &self.args
    }

    /// The working directory, if one was set.
    #[must_use]
    pub fn working_dir(&self) -> Option<&Path> {
        self.current_dir.as_deref()
    }

    /// Renders the invocation for diagnostics, quoting arguments with spaces.
    #[must_use]
    pub fn to_display_string(&self) -> String {
        let mut rendered = self.program.to_string_lossy().into_owned();
        for arg in &self.args {
            let arg = arg.to_string_lossy();
            if arg.contains(char::is_whitespace) {
                let _ = write!(rendered, " \"{arg}\"");
            } else {
                let _ = write!(rendered, " {arg}");
            }
        }
        rendered
    }

    fn to_command(&self) -> Command {
        let mut command = Command::new(&self.program);
        command.args(&self.args);
        if let Some(dir) = &self.current_dir {
            command.current_dir(dir);
        }
        command
    }
}

/// What to do with one of a child process's output streams.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Stream {
    /// Capture the stream for inspection.
    Capture,
    /// Pass the stream through to this process's own.
    Inherit,
}

impl Stream {
    fn stdio(self) -> Stdio {
        match self {
            Self::Capture => Stdio::piped(),
            Self::Inherit => Stdio::inherit(),
        }
    }
}

/// How a child process's output streams are handled.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct IoPolicy {
    /// Handling for standard output.
    pub stdout: Stream,
    /// Handling for standard error.
    pub stderr: Stream,
}

impl IoPolicy {
    /// Capture everything - used for build steps, whose output is only
    /// interesting when they fail.
    pub const SILENT: Self = Self {
        stdout: Stream::Capture,
        stderr: Stream::Capture,
    };

    /// Capture standard output but let standard error through live - used for
    /// the solution itself, whose stdout carries the answers and whose stderr
    /// carries progress the user should see as it happens.
    pub const ANSWER: Self = Self {
        stdout: Stream::Capture,
        stderr: Stream::Inherit,
    };

    /// Pass both streams through - used for scaffolding commands.
    pub const INHERIT: Self = Self {
        stdout: Stream::Inherit,
        stderr: Stream::Inherit,
    };
}

/// The captured result of a finished child process.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandOutput {
    /// The exit code, or `None` if the process was terminated by a signal.
    pub code: Option<i32>,
    /// Standard output, lossily decoded as UTF-8, empty if it was inherited.
    pub stdout: String,
    /// Standard error, lossily decoded as UTF-8, empty if it was inherited.
    pub stderr: String,
}

impl CommandOutput {
    /// Whether the process exited successfully.
    #[must_use]
    pub fn success(&self) -> bool {
        self.code == Some(0)
    }

    fn status_description(&self) -> String {
        self.code.map_or_else(
            || "terminated by signal".to_owned(),
            |code| format!("exit code {code}"),
        )
    }
}

/// Executes [`CommandSpec`]s.
pub trait CommandRunner {
    /// Runs the command to completion.
    ///
    /// # Errors
    ///
    /// Returns [`ProcessError::NotFound`] or [`ProcessError::Spawn`] if the
    /// program could not be started.
    fn run(&self, spec: &CommandSpec, io: IoPolicy) -> Result<CommandOutput, ProcessError>;

    /// Starts the command without waiting for it to finish.
    ///
    /// # Errors
    ///
    /// Returns [`ProcessError::NotFound`] or [`ProcessError::Spawn`] if the
    /// program could not be started.
    fn spawn_detached(&self, spec: &CommandSpec) -> Result<(), ProcessError>;

    /// Runs the command and fails if it exits unsuccessfully.
    ///
    /// # Errors
    ///
    /// As [`CommandRunner::run`], plus [`ProcessError::Failed`] if the process
    /// exited with a non-success status.
    fn run_checked(&self, spec: &CommandSpec, io: IoPolicy) -> Result<CommandOutput, ProcessError> {
        let output = self.run(spec, io)?;
        if output.success() {
            Ok(output)
        } else {
            Err(ProcessError::Failed {
                command: spec.to_display_string(),
                status: output.status_description(),
                stderr: output.stderr.trim_end().to_owned(),
            })
        }
    }
}

/// Runs commands as real child processes.
#[derive(Debug, Default, Clone, Copy)]
pub struct SystemRunner;

impl CommandRunner for SystemRunner {
    fn run(&self, spec: &CommandSpec, io: IoPolicy) -> Result<CommandOutput, ProcessError> {
        let output = spec
            .to_command()
            .stdin(Stdio::null())
            .stdout(io.stdout.stdio())
            .stderr(io.stderr.stdio())
            .spawn()
            .map_err(|source| spawn_error(spec, source))?
            .wait_with_output()
            .map_err(|source| spawn_error(spec, source))?;

        Ok(CommandOutput {
            code: output.status.code(),
            stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
            stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
        })
    }

    fn spawn_detached(&self, spec: &CommandSpec) -> Result<(), ProcessError> {
        spec.to_command()
            .spawn()
            .map(|_| ())
            .map_err(|source| spawn_error(spec, source))
    }
}

fn spawn_error(spec: &CommandSpec, source: io::Error) -> ProcessError {
    if source.kind() == io::ErrorKind::NotFound {
        ProcessError::NotFound {
            program: spec.program().to_string_lossy().into_owned(),
        }
    } else {
        ProcessError::Spawn {
            command: spec.to_display_string(),
            source,
        }
    }
}

/// Errors produced while building or executing a child process.
#[derive(Debug, thiserror::Error)]
pub enum ProcessError {
    /// The program is not installed or not on `PATH`.
    #[error("`{program}` was not found - is it installed and on your PATH?")]
    NotFound {
        /// The program that could not be located.
        program: String,
    },
    /// The process could not be started.
    #[error("failed to execute `{command}`")]
    Spawn {
        /// The rendered invocation.
        command: String,
        /// The underlying I/O error.
        #[source]
        source: io::Error,
    },
    /// The process ran but exited unsuccessfully.
    #[error("`{command}` failed ({status})\n{stderr}")]
    Failed {
        /// The rendered invocation.
        command: String,
        /// How the process terminated.
        status: String,
        /// Captured standard error.
        stderr: String,
    },
    /// A command needed the project's directory name but the path has none.
    #[error("project path has no directory name: {path}")]
    NoDirectoryName {
        /// The offending path.
        path: PathBuf,
    },
}

#[cfg(test)]
pub(crate) mod fake {
    use super::{CommandOutput, CommandRunner, CommandSpec, IoPolicy, ProcessError};
    use std::cell::RefCell;
    use std::collections::VecDeque;

    #[derive(Debug, Default)]
    pub(crate) struct FakeRunner {
        queued: RefCell<VecDeque<CommandOutput>>,
        executed: RefCell<Vec<(CommandSpec, IoPolicy)>>,
        spawned: RefCell<Vec<CommandSpec>>,
    }

    impl FakeRunner {
        pub(crate) fn new() -> Self {
            Self::default()
        }

        pub(crate) fn push_stdout(&self, stdout: &str) -> &Self {
            self.queued.borrow_mut().push_back(CommandOutput {
                code: Some(0),
                stdout: stdout.to_owned(),
                stderr: String::new(),
            });
            self
        }

        pub(crate) fn push_failure(&self, code: i32, stderr: &str) -> &Self {
            self.queued.borrow_mut().push_back(CommandOutput {
                code: Some(code),
                stdout: String::new(),
                stderr: stderr.to_owned(),
            });
            self
        }

        pub(crate) fn executed(&self) -> Vec<CommandSpec> {
            self.executed
                .borrow()
                .iter()
                .map(|(spec, _)| spec.clone())
                .collect()
        }

        pub(crate) fn policies(&self) -> Vec<IoPolicy> {
            self.executed.borrow().iter().map(|(_, io)| *io).collect()
        }

        pub(crate) fn spawned(&self) -> Vec<CommandSpec> {
            self.spawned.borrow().clone()
        }
    }

    impl CommandRunner for FakeRunner {
        fn run(&self, spec: &CommandSpec, io: IoPolicy) -> Result<CommandOutput, ProcessError> {
            self.executed.borrow_mut().push((spec.clone(), io));
            Ok(self
                .queued
                .borrow_mut()
                .pop_front()
                .unwrap_or(CommandOutput {
                    code: Some(0),
                    stdout: String::new(),
                    stderr: String::new(),
                }))
        }

        fn spawn_detached(&self, spec: &CommandSpec) -> Result<(), ProcessError> {
            self.spawned.borrow_mut().push(spec.clone());
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn renders_invocation_for_diagnostics() {
        let spec = CommandSpec::new("cargo")
            .args(["run", "--release"])
            .arg("/tmp/my project");

        assert_eq!(
            spec.to_display_string(),
            "cargo run --release \"/tmp/my project\""
        );
    }

    #[test]
    fn records_program_arguments_and_directory() {
        let spec = CommandSpec::new("javac")
            .arg("Main.java")
            .current_dir("/tmp/day01");

        assert_eq!(spec.program(), "javac");
        assert_eq!(spec.arguments(), ["Main.java"]);
        assert_eq!(spec.working_dir(), Some(Path::new("/tmp/day01")));
    }

    #[test]
    fn reports_missing_programs_distinctly() {
        let spec = CommandSpec::new("definitely-not-a-real-program-9271");

        let error = SystemRunner
            .run(&spec, IoPolicy::SILENT)
            .expect_err("program should not exist");

        assert!(
            matches!(error, ProcessError::NotFound { .. }),
            "got {error:?}"
        );
    }

    #[test]
    fn captures_output_of_real_processes() {
        let spec = CommandSpec::new("echo").arg("hello");

        let output = SystemRunner
            .run_checked(&spec, IoPolicy::SILENT)
            .expect("echo should succeed");

        assert_eq!(output.stdout.trim_end(), "hello");
        assert!(output.success());
    }

    #[test]
    fn run_checked_surfaces_stderr_of_failing_commands() {
        let runner = fake::FakeRunner::new();
        runner.push_failure(101, "error: could not compile\n");

        let error = runner
            .run_checked(&CommandSpec::new("cargo").arg("build"), IoPolicy::SILENT)
            .expect_err("command should fail");

        let message = error.to_string();
        assert!(message.contains("cargo build"), "{message}");
        assert!(message.contains("exit code 101"), "{message}");
        assert!(message.contains("could not compile"), "{message}");
    }

    #[test]
    fn run_checked_returns_output_on_success() {
        let runner = fake::FakeRunner::new();
        runner.push_stdout("42\n");

        let output = runner
            .run_checked(
                &CommandSpec::new("python3").arg("main.py"),
                IoPolicy::ANSWER,
            )
            .expect("command should succeed");

        assert_eq!(output.stdout, "42\n");
        assert_eq!(runner.executed().len(), 1);
        assert_eq!(runner.policies(), [IoPolicy::ANSWER]);
    }
}