|
|
|
@ -2,11 +2,42 @@ use anyhow::{Context, Result};
|
|
|
|
|
use crossterm::style::{style, StyledContent, Stylize};
|
|
|
|
|
use std::{
|
|
|
|
|
fmt::{self, Display, Formatter},
|
|
|
|
|
path::Path,
|
|
|
|
|
process::{Command, Output},
|
|
|
|
|
io::{Read, Write},
|
|
|
|
|
process::Command,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use crate::{info_file::Mode, terminal_link::TerminalFileLink, DEBUG_PROFILE};
|
|
|
|
|
use crate::{in_official_repo, info_file::Mode, terminal_link::TerminalFileLink, DEBUG_PROFILE};
|
|
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
pub const OUTPUT_CAPACITY: usize = 1 << 12;
|
|
|
|
|
|
|
|
|
|
fn run_command(mut cmd: Command, cmd_description: &str, output: &mut Vec<u8>) -> Result<bool> {
|
|
|
|
|
let (mut reader, writer) = os_pipe::pipe().with_context(|| {
|
|
|
|
|
format!("Failed to create a pipe to run the command `{cmd_description}``")
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
let mut handle = cmd
|
|
|
|
|
.stdout(writer.try_clone().with_context(|| {
|
|
|
|
|
format!("Failed to clone the pipe writer for the command `{cmd_description}`")
|
|
|
|
|
})?)
|
|
|
|
|
.stderr(writer)
|
|
|
|
|
.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())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Exercise {
|
|
|
|
|
pub dir: Option<&'static str>,
|
|
|
|
@ -22,13 +53,30 @@ pub struct Exercise {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Exercise {
|
|
|
|
|
fn cargo_cmd(&self, command: &str, args: &[&str]) -> Result<Output> {
|
|
|
|
|
fn run_bin(&self, output: &mut Vec<u8>) -> Result<bool> {
|
|
|
|
|
writeln!(output, "{}", "Output".bold().magenta().underlined())?;
|
|
|
|
|
|
|
|
|
|
let bin_path = format!("target/debug/{}", self.name);
|
|
|
|
|
run_command(Command::new(&bin_path), &bin_path, output)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn cargo_cmd(
|
|
|
|
|
&self,
|
|
|
|
|
command: &str,
|
|
|
|
|
args: &[&str],
|
|
|
|
|
cmd_description: &str,
|
|
|
|
|
output: &mut Vec<u8>,
|
|
|
|
|
dev: bool,
|
|
|
|
|
) -> Result<bool> {
|
|
|
|
|
let mut cmd = Command::new("cargo");
|
|
|
|
|
cmd.arg(command);
|
|
|
|
|
|
|
|
|
|
// A hack to make `cargo run` work when developing Rustlings.
|
|
|
|
|
if DEBUG_PROFILE && Path::new("tests").exists() {
|
|
|
|
|
cmd.arg("--manifest-path").arg("dev/Cargo.toml");
|
|
|
|
|
if dev {
|
|
|
|
|
cmd.arg("--manifest-path")
|
|
|
|
|
.arg("dev/Cargo.toml")
|
|
|
|
|
.arg("--target-dir")
|
|
|
|
|
.arg("target");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd.arg("--color")
|
|
|
|
@ -36,15 +84,43 @@ impl Exercise {
|
|
|
|
|
.arg("-q")
|
|
|
|
|
.arg("--bin")
|
|
|
|
|
.arg(self.name)
|
|
|
|
|
.args(args)
|
|
|
|
|
.output()
|
|
|
|
|
.context("Failed to run Cargo")
|
|
|
|
|
.args(args);
|
|
|
|
|
|
|
|
|
|
run_command(cmd, cmd_description, output)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn cargo_cmd_with_bin_output(
|
|
|
|
|
&self,
|
|
|
|
|
command: &str,
|
|
|
|
|
args: &[&str],
|
|
|
|
|
cmd_description: &str,
|
|
|
|
|
output: &mut Vec<u8>,
|
|
|
|
|
dev: bool,
|
|
|
|
|
) -> Result<bool> {
|
|
|
|
|
// Discard the output of `cargo build` because it will be shown again by the Cargo command.
|
|
|
|
|
output.clear();
|
|
|
|
|
|
|
|
|
|
let cargo_cmd_success = self.cargo_cmd(command, args, cmd_description, output, dev)?;
|
|
|
|
|
|
|
|
|
|
let run_success = self.run_bin(output)?;
|
|
|
|
|
|
|
|
|
|
Ok(cargo_cmd_success && run_success)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn run(&self) -> Result<Output> {
|
|
|
|
|
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)?;
|
|
|
|
|
if !build_success {
|
|
|
|
|
return Ok(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match self.mode {
|
|
|
|
|
Mode::Run => self.cargo_cmd("run", &[]),
|
|
|
|
|
Mode::Test => self.cargo_cmd(
|
|
|
|
|
Mode::Run => self.run_bin(output),
|
|
|
|
|
Mode::Test => self.cargo_cmd_with_bin_output(
|
|
|
|
|
"test",
|
|
|
|
|
&[
|
|
|
|
|
"--",
|
|
|
|
@ -54,10 +130,16 @@ impl Exercise {
|
|
|
|
|
"--format",
|
|
|
|
|
"pretty",
|
|
|
|
|
],
|
|
|
|
|
"cargo test …",
|
|
|
|
|
output,
|
|
|
|
|
dev,
|
|
|
|
|
),
|
|
|
|
|
Mode::Clippy => self.cargo_cmd(
|
|
|
|
|
Mode::Clippy => self.cargo_cmd_with_bin_output(
|
|
|
|
|
"clippy",
|
|
|
|
|
&["--", "-D", "warnings", "-D", "clippy::float_cmp"],
|
|
|
|
|
&["--", "-D", "warnings"],
|
|
|
|
|
"cargo clippy …",
|
|
|
|
|
output,
|
|
|
|
|
dev,
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|