rustlings/src/exercise.rs

179 lines
4.7 KiB
Rust
Raw Normal View History

2024-04-18 13:41:17 +03:00
use anyhow::{Context, Result};
2024-04-14 03:41:19 +03:00
use crossterm::style::{style, StyledContent, Stylize};
2024-04-08 01:36:26 +03:00
use std::{
2024-04-14 02:15:43 +03:00
fmt::{self, Display, Formatter},
2024-04-25 02:56:01 +03:00
io::{Read, Write},
process::{Command, Stdio},
2024-04-08 01:36:26 +03:00
};
use crate::{in_official_repo, terminal_link::TerminalFileLink, DEBUG_PROFILE};
2024-04-25 02:56:01 +03:00
2024-04-25 15:43:02 +03:00
pub const OUTPUT_CAPACITY: usize = 1 << 14;
2024-04-25 02:56:01 +03:00
fn run_command(
mut cmd: Command,
cmd_description: &str,
output: &mut Vec<u8>,
stderr: bool,
) -> Result<bool> {
2024-04-25 02:56:01 +03:00
let (mut reader, writer) = os_pipe::pipe().with_context(|| {
format!("Failed to create a pipe to run the command `{cmd_description}``")
})?;
let (stdout, stderr) = if stderr {
(
Stdio::from(writer.try_clone().with_context(|| {
format!("Failed to clone the pipe writer for the command `{cmd_description}`")
})?),
Stdio::from(writer),
)
} else {
(Stdio::from(writer), Stdio::null())
};
2024-04-25 02:56:01 +03:00
let mut handle = cmd
.stdout(stdout)
.stderr(stderr)
2024-04-25 02:56:01 +03:00
.spawn()
.with_context(|| format!("Failed to run the command `{cmd_description}`"))?;
// Prevent pipe deadlock.
drop(cmd);
reader
.read_to_end(output)
.with_context(|| format!("Failed to read the output of the command `{cmd_description}`"))?;
output.push(b'\n');
handle
.wait()
.with_context(|| format!("Failed to wait on the command `{cmd_description}` to exit"))
.map(|status| status.success())
}
2024-04-14 03:41:19 +03:00
pub struct Exercise {
pub dir: Option<&'static str>,
2024-04-14 02:15:43 +03:00
// Exercise's unique name
pub name: &'static str,
// Exercise's path
2024-04-14 03:41:19 +03:00
pub path: &'static str,
pub test: bool,
pub strict_clippy: bool,
// The hint text associated with the exercise
pub hint: String,
2024-04-14 02:15:43 +03:00
pub done: bool,
}
impl Exercise {
2024-04-25 02:56:01 +03:00
fn run_bin(&self, output: &mut Vec<u8>) -> Result<bool> {
writeln!(output, "{}", "Output".underlined())?;
2024-04-25 02:56:01 +03:00
let bin_path = format!("target/debug/{}", self.name);
let success = run_command(Command::new(&bin_path), &bin_path, output, true)?;
if !success {
writeln!(
output,
"{}",
"The exercise didn't run successfully (nonzero exit code)"
.bold()
.red()
)?;
}
Ok(success)
2024-04-25 02:56:01 +03:00
}
fn cargo_cmd(
&self,
command: &str,
args: &[&str],
cmd_description: &str,
output: &mut Vec<u8>,
dev: bool,
stderr: bool,
2024-04-25 02:56:01 +03:00
) -> Result<bool> {
2024-04-01 03:11:52 +03:00
let mut cmd = Command::new("cargo");
cmd.arg(command);
// A hack to make `cargo run` work when developing Rustlings.
2024-04-25 02:56:01 +03:00
if dev {
cmd.arg("--manifest-path")
.arg("dev/Cargo.toml")
.arg("--target-dir")
.arg("target");
2024-04-01 03:11:52 +03:00
}
cmd.arg("--color")
2024-03-31 17:55:33 +03:00
.arg("always")
.arg("-q")
.arg("--bin")
2024-04-14 02:15:43 +03:00
.arg(self.name)
2024-04-25 02:56:01 +03:00
.args(args);
run_command(cmd, cmd_description, output, stderr)
2024-03-31 17:55:33 +03:00
}
2024-04-25 02:56:01 +03:00
pub fn run(&self, output: &mut Vec<u8>) -> Result<bool> {
output.clear();
// Developing the official Rustlings.
let dev = DEBUG_PROFILE && in_official_repo();
let build_success = self.cargo_cmd("build", &[], "cargo build …", output, dev, true)?;
2024-04-25 02:56:01 +03:00
if !build_success {
return Ok(false);
}
// Discard the output of `cargo build` because it will be shown again by the Cargo command.
output.clear();
let clippy_args: &[&str] = if self.strict_clippy {
2024-04-25 17:08:07 +03:00
&["--profile", "test", "--", "-D", "warnings"]
} else {
2024-04-25 17:08:07 +03:00
&["--profile", "test"]
};
let clippy_success =
self.cargo_cmd("clippy", clippy_args, "cargo clippy …", output, dev, true)?;
if !clippy_success {
return Ok(false);
}
if !self.test {
return self.run_bin(output);
}
let test_success = self.cargo_cmd(
"test",
&[
"--",
"--color",
"always",
"--nocapture",
"--format",
"pretty",
],
"cargo test …",
output,
dev,
// Hide warnings because they are shown by Clippy.
false,
)?;
let run_success = self.run_bin(output)?;
Ok(test_success && run_success)
}
2024-04-14 03:41:19 +03:00
pub fn terminal_link(&self) -> StyledContent<TerminalFileLink<'_>> {
2024-04-24 03:52:30 +03:00
style(TerminalFileLink(self.path)).underlined().blue()
2024-04-14 03:41:19 +03:00
}
}
impl Display for Exercise {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
2024-04-14 03:41:19 +03:00
self.path.fmt(f)
}
}