This commit is contained in:
Enrico Bozzolini 2020-08-21 21:20:13 +02:00
commit 86f728acd2
11 changed files with 202 additions and 64 deletions

View File

@ -43,7 +43,7 @@ Set-ExecutionPolicy RemoteSigned
Then, you can run:
```ps
Invoke-WebRequest https://git.io/rustlings-win | Select-Object -ExpandProperty Content | Out-File $env:TMP/install_rustlings.ps1; Unblock-File $env:TMP/install_rustlings.ps1; Invoke-Expression $env:TMP/install_rustlings.ps1
Start-BitsTransfer -Source https://git.io/rustlings-win -Destination $env:TMP/install_rustlings.ps1; Unblock-File $env:TMP/install_rustlings.ps1; Invoke-Expression $env:TMP/install_rustlings.ps1
```
To install Rustlings. Same as on MacOS/Linux, you will have access to the `rustlings` command after it.

View File

@ -36,4 +36,16 @@ mod tests {
let s = "Cafe au lait";
assert_eq!(char_counter(s), byte_counter(s));
}
#[test]
fn different_counts_using_string() {
let s = String::from("Café au lait");
assert_ne!(char_counter(s.clone()), byte_counter(s));
}
#[test]
fn same_counts_using_string() {
let s = String::from("Cafe au lait");
assert_eq!(char_counter(s.clone()), byte_counter(s));
}
}

View File

@ -5,17 +5,18 @@
#[derive(Debug)]
struct Package {
from: String,
to: String,
weight: f32
sender_country: String,
recipient_country: String,
weight_in_grams: i32,
}
impl Package {
fn new(from: String, to: String, weight: f32) -> Package {
if weight <= 0.0 {
fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package {
if weight_in_grams <= 0 {
// Something goes here...
panic!("Package is lighter then air!")
} else {
return Package {from, to, weight};
return Package {sender_country, recipient_country, weight_in_grams};
}
}
@ -24,8 +25,8 @@ impl Package {
self.from != self.to
}
fn get_fees(&self, cost_per_kg: f32) -> f32 {
// Something goes here...
fn get_fees(&self, cents_per_kg: i32) -> i32 {
// Something goes here... (beware of grams to kg conversion)
self.weight * cost_per_kg
}
}
@ -37,31 +38,31 @@ mod tests {
#[test]
#[should_panic]
fn fail_creating_weightless_package() {
let country_from = String::from("Spain");
let country_to = String::from("Austria");
let sender_country = String::from("Spain");
let recipient_country = String::from("Austria");
Package::new(country_from, country_to, -2.21);
Package::new(sender_country, recipient_country, -2210);
}
#[test]
fn create_international_package() {
let country_from = String::from("Spain");
let country_to = String::from("Russia");
let sender_country = String::from("Spain");
let recipient_country = String::from("Russia");
let package = Package::new(country_from, country_to, 1.2);
let package = Package::new(sender_country, recipient_country, 1200);
assert!(package.is_international());
}
#[test]
fn calculate_transport_fees() {
let country_from = String::from("Spain");
let country_to = String::from("Spain");
let sender_country = String::from("Spain");
let recipient_country = String::from("Spain");
let country_fee = 8.0;
let cents_per_kg = 8.0;
let package = Package::new(country_from, country_to, 22.0);
let package = Package::new(sender_country, recipient_country, 1500);
assert_eq!(package.get_fees(country_fee), 176.0);
assert_eq!(package.get_fees(cents_per_kg), 4500);
}
}

View File

@ -724,8 +724,10 @@ name = "generics2"
path = "exercises/generics/generics2.rs"
mode = "test"
hint = """
Think carefully about what we need to do here. Currently we are wrapping only values of
type 'u32'. Maybe we need to update the explicit references to this data type somehow?
Currently we are wrapping only values of type 'u32'.
Maybe we could update the explicit references to this data type somehow?
If you are still stuck https://doc.rust-lang.org/stable/book/ch10-01-syntax.html#in-method-definitions
"""
[[exercises]]
@ -802,7 +804,7 @@ name = "try_from_into"
path = "exercises/conversions/try_from_into.rs"
mode = "test"
hint = """
Follow the steps provided right before the `From` implementation.
Follow the steps provided right before the `TryFrom` implementation.
You can also use the example at https://doc.rust-lang.org/std/convert/trait.TryFrom.html"""
[[exercises]]
@ -819,4 +821,4 @@ mode = "test"
hint = """
The implementation of FromStr should return an Ok with a Person object,
or an Err with a string if the string is not valid.
This is a some like an `try_from_into` exercise."""
This is almost like the `try_from_into` exercise."""

View File

@ -102,12 +102,30 @@ Path=${1:-rustlings/}
echo "Cloning Rustlings at $Path..."
git clone -q https://github.com/rust-lang/rustlings $Path
cd $Path
Version=$(curl -s https://api.github.com/repos/rust-lang/rustlings/releases/latest | ${PY} -c "import json,sys;obj=json.load(sys.stdin);print(obj['tag_name']);")
CargoBin="${CARGO_HOME:-$HOME/.cargo}/bin"
if [[ -z ${Version} ]]
then
echo "The latest tag version could not be fetched remotely."
echo "Using the local git repository..."
Version=$(ls -tr .git/refs/tags/ | tail -1)
if [[ -z ${Version} ]]
then
echo "No valid tag version found"
echo "Rustlings will be installed using the master branch"
Version="master"
else
Version="tags/${Version}"
fi
else
Version="tags/${Version}"
fi
echo "Checking out version $Version..."
cd $Path
git checkout -q tags/$Version
git checkout -q ${Version}
echo "Installing the 'rustlings' executable..."
cargo install --force --path .

View File

@ -11,15 +11,21 @@ const I_AM_DONE_REGEX: &str = r"(?m)^\s*///?\s*I\s+AM\s+NOT\s+DONE";
const CONTEXT: usize = 2;
const CLIPPY_CARGO_TOML_PATH: &str = "./exercises/clippy/Cargo.toml";
// Get a temporary file name that is hopefully unique to this process
#[inline]
fn temp_file() -> String {
format!("./temp_{}", process::id())
}
// The mode of the exercise.
#[derive(Deserialize, Copy, Clone)]
#[serde(rename_all = "lowercase")]
pub enum Mode {
// Indicates that the exercise should be compiled as a binary
Compile,
// Indicates that the exercise should be compiled as a test harness
Test,
// Indicates that the exercise should be linted with clippy
Clippy,
}
@ -28,41 +34,60 @@ pub struct ExerciseList {
pub exercises: Vec<Exercise>,
}
// A representation of a rustlings exercise.
// This is deserialized from the accompanying info.toml file
#[derive(Deserialize)]
pub struct Exercise {
// Name of the exercise
pub name: String,
// The path to the file containing the exercise's source code
pub path: PathBuf,
// The mode of the exercise (Test, Compile, or Clippy)
pub mode: Mode,
// The hint text associated with the exercise
pub hint: String,
}
// An enum to track of the state of an Exercise.
// An Exercise can be either Done or Pending
#[derive(PartialEq, Debug)]
pub enum State {
// The state of the exercise once it's been completed
Done,
// The state of the exercise while it's not completed yet
Pending(Vec<ContextLine>),
}
// The context information of a pending exercise
#[derive(PartialEq, Debug)]
pub struct ContextLine {
// The source code that is still pending completion
pub line: String,
// The line number of the source code still pending completion
pub number: usize,
// Whether or not this is important
pub important: bool,
}
// The result of compiling an exercise
pub struct CompiledExercise<'a> {
exercise: &'a Exercise,
_handle: FileHandle,
}
impl<'a> CompiledExercise<'a> {
// Run the compiled exercise
pub fn run(&self) -> Result<ExerciseOutput, ExerciseOutput> {
self.exercise.run()
}
}
// A representation of an already executed binary
#[derive(Debug)]
pub struct ExerciseOutput {
// The textual contents of the standard output of the binary
pub stdout: String,
// The textual contents of the standard error of the binary
pub stderr: String,
}
@ -140,7 +165,11 @@ path = "{}.rs""#,
}
fn run(&self) -> Result<ExerciseOutput, ExerciseOutput> {
let cmd = Command::new(&temp_file())
let arg = match self.mode {
Mode::Test => "--show-output",
_ => ""
};
let cmd = Command::new(&temp_file()).arg(arg)
.output()
.expect("Failed to run 'run' command");
@ -205,6 +234,7 @@ impl Display for Exercise {
}
}
#[inline]
fn clean() {
let _ignored = remove_file(&temp_file());
}
@ -280,4 +310,16 @@ mod test {
assert_eq!(exercise.state(), State::Done);
}
#[test]
fn test_exercise_with_output() {
let exercise = Exercise {
name: "finished_exercise".into(),
path: PathBuf::from("tests/fixture/success/testSuccess.rs"),
mode: Mode::Test,
hint: String::new(),
};
let out = exercise.compile().unwrap().run().unwrap();
assert!(out.stdout.contains("THIS TEST TOO SHALL PASS"));
}
}

View File

@ -27,8 +27,21 @@ fn main() {
.version(crate_version!())
.author("Olivia Hugger, Carol Nichols")
.about("Rustlings is a collection of small exercises to get you used to writing and reading Rust code")
.subcommand(SubCommand::with_name("verify").alias("v").about("Verifies all exercises according to the recommended order"))
.subcommand(SubCommand::with_name("watch").alias("w").about("Reruns `verify` when files were edited"))
.arg(
Arg::with_name("nocapture")
.long("nocapture")
.help("Show outputs from the test exercises")
)
.subcommand(
SubCommand::with_name("verify")
.alias("v")
.about("Verifies all exercises according to the recommended order")
)
.subcommand(
SubCommand::with_name("watch")
.alias("w")
.about("Reruns `verify` when files were edited")
)
.subcommand(
SubCommand::with_name("run")
.alias("r")
@ -43,7 +56,7 @@ fn main() {
)
.get_matches();
if None == matches.subcommand_name() {
if matches.subcommand_name().is_none() {
println!();
println!(r#" welcome to... "#);
println!(r#" _ _ _ "#);
@ -73,6 +86,7 @@ fn main() {
let toml_str = &fs::read_to_string("info.toml").unwrap();
let exercises = toml::from_str::<ExerciseList>(toml_str).unwrap().exercises;
let verbose = matches.is_present("nocapture");
if let Some(ref matches) = matches.subcommand_matches("run") {
let name = matches.value_of("name").unwrap();
@ -84,7 +98,7 @@ fn main() {
std::process::exit(1)
});
run(&exercise).unwrap_or_else(|_| std::process::exit(1));
run(&exercise, verbose).unwrap_or_else(|_| std::process::exit(1));
}
if let Some(ref matches) = matches.subcommand_matches("hint") {
@ -102,25 +116,23 @@ fn main() {
}
if matches.subcommand_matches("verify").is_some() {
verify(&exercises).unwrap_or_else(|_| std::process::exit(1));
verify(&exercises, verbose).unwrap_or_else(|_| std::process::exit(1));
}
if matches.subcommand_matches("watch").is_some() {
if watch(&exercises).is_ok() {
println!(
"{emoji} All exercises completed! {emoji}",
emoji = Emoji("🎉", "")
);
println!("");
println!("We hope you enjoyed learning about the various aspects of Rust!");
println!(
"If you noticed any issues, please don't hesitate to report them to our repo."
);
println!("You can also contribute your own exercises to help the greater community!");
println!("");
println!("Before reporting an issue or contributing, please read our guidelines:");
println!("https://github.com/rust-lang/rustlings/blob/master/CONTRIBUTING.md");
}
if matches.subcommand_matches("watch").is_some() && watch(&exercises, verbose).is_ok() {
println!(
"{emoji} All exercises completed! {emoji}",
emoji = Emoji("🎉", "")
);
println!();
println!("We hope you enjoyed learning about the various aspects of Rust!");
println!(
"If you noticed any issues, please don't hesitate to report them to our repo."
);
println!("You can also contribute your own exercises to help the greater community!");
println!();
println!("Before reporting an issue or contributing, please read our guidelines:");
println!("https://github.com/rust-lang/rustlings/blob/master/CONTRIBUTING.md");
}
if matches.subcommand_name().is_none() {
@ -149,7 +161,7 @@ fn spawn_watch_shell(failed_exercise_hint: &Arc<Mutex<Option<String>>>) {
});
}
fn watch(exercises: &[Exercise]) -> notify::Result<()> {
fn watch(exercises: &[Exercise], verbose: bool) -> notify::Result<()> {
/* Clears the terminal with an ANSI escape code.
Works in UNIX and newer Windows terminals. */
fn clear_screen() {
@ -164,7 +176,7 @@ fn watch(exercises: &[Exercise]) -> notify::Result<()> {
clear_screen();
let to_owned_hint = |t: &Exercise| t.hint.to_owned();
let failed_exercise_hint = match verify(exercises.iter()) {
let failed_exercise_hint = match verify(exercises.iter(), verbose) {
Ok(_) => return Ok(()),
Err(exercise) => Arc::new(Mutex::new(Some(to_owned_hint(exercise)))),
};
@ -179,7 +191,7 @@ fn watch(exercises: &[Exercise]) -> notify::Result<()> {
.iter()
.skip_while(|e| !filepath.ends_with(&e.path));
clear_screen();
match verify(pending_exercises) {
match verify(pending_exercises, verbose) {
Ok(_) => return Ok(()),
Err(exercise) => {
let mut failed_exercise_hint = failed_exercise_hint.lock().unwrap();

View File

@ -2,15 +2,22 @@ use crate::exercise::{Exercise, Mode};
use crate::verify::test;
use indicatif::ProgressBar;
pub fn run(exercise: &Exercise) -> Result<(), ()> {
// Invoke the rust compiler on the path of the given exercise,
// and run the ensuing binary.
// The verbose argument helps determine whether or not to show
// the output from the test harnesses (if the mode of the exercise is test)
pub fn run(exercise: &Exercise, verbose: bool) -> Result<(), ()> {
match exercise.mode {
Mode::Test => test(exercise)?,
Mode::Test => test(exercise, verbose)?,
Mode::Compile => compile_and_run(exercise)?,
Mode::Clippy => compile_and_run(exercise)?,
}
Ok(())
}
// Invoke the rust compiler on the path of the given exercise
// and run the ensuing binary.
// This is strictly for non-test binaries, so output is displayed
fn compile_and_run(exercise: &Exercise) -> Result<(), ()> {
let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Compiling {}...", exercise).as_str());

View File

@ -2,10 +2,18 @@ use crate::exercise::{CompiledExercise, Exercise, Mode, State};
use console::style;
use indicatif::ProgressBar;
pub fn verify<'a>(start_at: impl IntoIterator<Item = &'a Exercise>) -> Result<(), &'a Exercise> {
// Verify that the provided container of Exercise objects
// can be compiled and run without any failures.
// Any such failures will be reported to the end user.
// If the Exercise being verified is a test, the verbose boolean
// determines whether or not the test harness outputs are displayed.
pub fn verify<'a>(
start_at: impl IntoIterator<Item = &'a Exercise>,
verbose: bool
) -> Result<(), &'a Exercise> {
for exercise in start_at {
let compile_result = match exercise.mode {
Mode::Test => compile_and_test(&exercise, RunMode::Interactive),
Mode::Test => compile_and_test(&exercise, RunMode::Interactive, verbose),
Mode::Compile => compile_and_run_interactively(&exercise),
Mode::Clippy => compile_only(&exercise),
};
@ -21,11 +29,13 @@ enum RunMode {
NonInteractive,
}
pub fn test(exercise: &Exercise) -> Result<(), ()> {
compile_and_test(exercise, RunMode::NonInteractive)?;
// Compile and run the resulting test harness of the given Exercise
pub fn test(exercise: &Exercise, verbose: bool) -> Result<(), ()> {
compile_and_test(exercise, RunMode::NonInteractive, verbose)?;
Ok(())
}
// Invoke the rust compiler without running the resulting binary
fn compile_only(exercise: &Exercise) -> Result<bool, ()> {
let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Compiling {}...", exercise).as_str());
@ -38,6 +48,7 @@ fn compile_only(exercise: &Exercise) -> Result<bool, ()> {
Ok(prompt_for_completion(&exercise, None))
}
// Compile the given Exercise and run the resulting binary in an interactive mode
fn compile_and_run_interactively(exercise: &Exercise) -> Result<bool, ()> {
let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Compiling {}...", exercise).as_str());
@ -54,6 +65,7 @@ fn compile_and_run_interactively(exercise: &Exercise) -> Result<bool, ()> {
Err(output) => {
warn!("Ran {} with errors", exercise);
println!("{}", output.stdout);
println!("{}", output.stderr);
return Err(());
}
};
@ -63,7 +75,11 @@ fn compile_and_run_interactively(exercise: &Exercise) -> Result<bool, ()> {
Ok(prompt_for_completion(&exercise, Some(output.stdout)))
}
fn compile_and_test(exercise: &Exercise, run_mode: RunMode) -> Result<bool, ()> {
// Compile the given Exercise as a test harness and display
// the output if verbose is set to true
fn compile_and_test(
exercise: &Exercise, run_mode: RunMode, verbose: bool
) -> Result<bool, ()> {
let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Testing {}...", exercise).as_str());
progress_bar.enable_steady_tick(100);
@ -73,7 +89,10 @@ fn compile_and_test(exercise: &Exercise, run_mode: RunMode) -> Result<bool, ()>
progress_bar.finish_and_clear();
match result {
Ok(_) => {
Ok(output) => {
if verbose {
println!("{}", output.stdout);
}
success!("Successfully tested {}", &exercise);
if let RunMode::Interactive = run_mode {
Ok(prompt_for_completion(&exercise, None))
@ -92,6 +111,8 @@ fn compile_and_test(exercise: &Exercise, run_mode: RunMode) -> Result<bool, ()>
}
}
// Compile the given Exercise and return an object with information
// about the state of the compilation
fn compile<'a, 'b>(
exercise: &'a Exercise,
progress_bar: &'b ProgressBar,
@ -124,16 +145,16 @@ fn prompt_for_completion(exercise: &Exercise, prompt_output: Option<String>) ->
Mode::Clippy => "The code is compiling, and 📎 Clippy 📎 is happy!",
};
println!("");
println!();
println!("🎉 🎉 {} 🎉 🎉", success_msg);
println!("");
println!();
if let Some(output) = prompt_output {
println!("Output:");
println!("{}", separator());
println!("{}", output);
println!("{}", separator());
println!("");
println!();
}
println!("You can keep working on this exercise,");
@ -141,12 +162,12 @@ fn prompt_for_completion(exercise: &Exercise, prompt_output: Option<String>) ->
"or jump into the next one by removing the {} comment:",
style("`I AM NOT DONE`").bold()
);
println!("");
println!();
for context_line in context {
let formatted_line = if context_line.important {
format!("{}", style(context_line.line).bold())
} else {
format!("{}", context_line.line)
context_line.line.to_string()
};
println!(

View File

@ -1,4 +1,5 @@
#[test]
fn passing() {
println!("THIS TEST TOO SHALL PASS");
assert!(true);
}

View File

@ -159,3 +159,25 @@ fn run_test_exercise_does_not_prompt() {
.code(0)
.stdout(predicates::str::contains("I AM NOT DONE").not());
}
#[test]
fn run_single_test_success_with_output() {
Command::cargo_bin("rustlings")
.unwrap()
.args(&["--nocapture", "r", "testSuccess"])
.current_dir("tests/fixture/success/")
.assert()
.code(0)
.stdout(predicates::str::contains("THIS TEST TOO SHALL PAS"));
}
#[test]
fn run_single_test_success_without_output() {
Command::cargo_bin("rustlings")
.unwrap()
.args(&["r", "testSuccess"])
.current_dir("tests/fixture/success/")
.assert()
.code(0)
.stdout(predicates::str::contains("THIS TEST TOO SHALL PAS").not());
}