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},
|
2024-04-25 04:25:45 +03:00
|
|
|
process::{Command, Stdio},
|
2024-04-08 01:36:26 +03:00
|
|
|
};
|
2019-04-11 23:41:24 +03:00
|
|
|
|
2024-04-25 04:25:45 +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
|
|
|
|
2024-04-25 04:25:45 +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}``")
|
|
|
|
})?;
|
|
|
|
|
2024-04-25 04:25:45 +03:00
|
|
|
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
|
2024-04-25 04:25:45 +03:00
|
|
|
.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
|
|
|
|
2019-04-11 23:41:24 +03:00
|
|
|
pub struct Exercise {
|
2024-04-23 20:18:25 +03:00
|
|
|
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,
|
2024-04-25 04:25:45 +03:00
|
|
|
pub test: bool,
|
|
|
|
pub strict_clippy: bool,
|
2020-06-04 17:31:17 +03:00
|
|
|
// The hint text associated with the exercise
|
2019-11-11 18:51:38 +03:00
|
|
|
pub hint: String,
|
2024-04-14 02:15:43 +03:00
|
|
|
pub done: bool,
|
2019-04-11 23:41:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Exercise {
|
2024-04-25 02:56:01 +03:00
|
|
|
fn run_bin(&self, output: &mut Vec<u8>) -> Result<bool> {
|
2024-04-25 04:25:45 +03:00
|
|
|
writeln!(output, "{}", "Output".underlined())?;
|
2024-04-25 02:56:01 +03:00
|
|
|
|
|
|
|
let bin_path = format!("target/debug/{}", self.name);
|
2024-04-25 04:25:45 +03:00
|
|
|
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,
|
2024-04-25 04:25:45 +03:00
|
|
|
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);
|
|
|
|
|
2024-04-25 04:25:45 +03:00
|
|
|
run_command(cmd, cmd_description, output, stderr)
|
2024-03-31 17:55:33 +03:00
|
|
|
}
|
2019-04-11 23:41:24 +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();
|
|
|
|
|
2024-04-25 04:25:45 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-04-25 04:25:45 +03:00
|
|
|
// 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"]
|
2024-04-25 04:25:45 +03:00
|
|
|
} else {
|
2024-04-25 17:08:07 +03:00
|
|
|
&["--profile", "test"]
|
2024-04-25 04:25:45 +03:00
|
|
|
};
|
|
|
|
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);
|
2020-02-20 22:11:53 +03:00
|
|
|
}
|
2024-04-25 04:25:45 +03:00
|
|
|
|
|
|
|
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)
|
2019-04-11 23:41:24 +03:00
|
|
|
}
|
2019-11-11 15:38:24 +03:00
|
|
|
|
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
|
|
|
}
|
2019-04-11 23:41:24 +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)
|
2019-04-11 23:41:24 +03:00
|
|
|
}
|
|
|
|
}
|