1
0
mirror of https://github.com/rust-lang/rustlings.git synced 2025-12-26 00:11:49 +02:00

Compare commits

..

53 Commits
1.0.1 ... 1.2.2

Author SHA1 Message Date
liv
1f2ee8cb62 1.2.2 2019-05-07 12:32:50 +02:00
liv
35c3d0b3fc Revert --nocapture flag
This closes #149 and #152
2019-05-07 12:31:02 +02:00
liv
0279294972 1.2.1 2019-04-22 19:12:30 +08:00
liv
7eddee6f7a add a slightly more helpful error message 2019-04-22 18:43:39 +08:00
liv
f2c48cfac5 fix the --nocapture functionality 2019-04-22 18:42:32 +08:00
liv
6ae0a00211 1.2.0 2019-04-22 13:09:28 +08:00
liv
bfcf38c8bc damn it 2019-04-22 07:05:46 +02:00
liv
9e328da641 use -- --nocapture when testing 2019-04-22 05:49:23 +02:00
bors
e336d04c79 Auto merge of #144 - yvan-sraka:patch-0, r=komaeda
Add errors to exercises that compile without user changes

Hi !

I played a bit with rustlings, and I felt that some exercises were incorrect because they passed the tests without me needing to edit the files!

This gave me the feeling that the exercise was skiped! Especially when I use `rustlings watch`, it is easy to miss an exercise because the compilation error that is displayed is the one of the next exercise ...

It is easy to identify "broken" exercises with:

```bash
% find exercises -name "*.rs" | xargs -n 1 rustlings run
...
 Successfully ran exercises/move_semantics/move_semantics4.rs
 Successfully tested exercises/test2.rs
```

My suggestion is to make sure that these files trigger a compilation error by adding a simple syntax error (e.g. with `???` in the code that must change) so that our Rustacean can then play with it!
2019-04-22 01:50:48 +00:00
Yvan Sraka
a71bc62c29 Add errors to exercises that compile without user changes 2019-04-22 00:09:30 +02:00
liv
4b0b7093e5 1.1.1 2019-04-14 18:29:32 +02:00
bors
8387de64d3 Auto merge of #143 - cjpearce:fix-exercise-path-matching, r=komaeda
Canonicalize paths to fix path matching

This PR should fix #126. The main solution to the issue was using `canonicalize()` on the paths we create for the exercises from `info.toml` and any user-specified paths, so that path `ends_with` matching will work correctly.

As adding calls to the canonicalize function everywhere requires unwrapping, I also decided to extract a struct representing an exercise and use serde to deserialize the paths from the `info.toml` file up front. I also tried to move the path handling out into the `exercise.rs` file and down into `main.rs` so that it doesn't create as much clutter. There was already a lot of unwrapping and path handling in the other files and I felt like it was getting a bit too repetitive.

If the approach is going too far (too many changes etc.) I'm happy to try to produce a smaller PR that fixes the bug without any refactoring.
2019-04-13 16:32:02 +00:00
Chris Pearce
77de6e5d6a Clean up test includes for File and Path 2019-04-12 23:14:15 +01:00
Chris Pearce
8c867a001a Remove unwrap on canonicalize result 2019-04-12 22:24:13 +01:00
Chris Pearce
d01a71f7de Extract exercise struct to encapsulate path logic 2019-04-12 08:58:25 +01:00
bors
04d1d4c00e Auto merge of #142 - diodfr:patch-1, r=komaeda
Fix links by deleting book version
2019-04-08 20:05:34 +00:00
Diod FR
d7e58ee1af Fix links by deleting book version 2019-04-08 22:02:04 +02:00
bors
ffb165ce26 Auto merge of #140 - cjpearce:fix/test-race-condition, r=komaeda
Fix intermittent test failure caused by race condition

First public pull request 😬

There's an intermittent integration test failure when you use multiple test threads (at least for me on a mac). I narrowed it down to two tests each spawning a process using `Command` which then try to compile the same file at the same time. If the timing doesn't work out, they both try to compile, and then one process runs `clean` before the other can run the executable - causing a panic.

![Screenshot 2019-04-07 at 19 54 55](https://user-images.githubusercontent.com/3453268/55688324-20520980-596f-11e9-8474-5215d61a4387.png)

You can prevent it from happening by running with a single thread (`cargo test -- --test-threads=1`), because the `Command` blocks. That's not a particularly good solution though because it's not something you can configure in `Cargo.toml`.

I considered making the affected tests just run serially, but it occurred to me that this could also happen if someone accidentally runs rustlings in watch mode in two terminals without realising it. I wound't consider this that unlikely given it's a tool for learning.

I fixed it by ensuring that the executables made from separate processes don't conflict by appending a process id to the output executable name. I also extracted the commands into a single file next to `clean` so that we don't have to repeat the generated file name everywhere and risk missing something.
2019-04-07 22:37:34 +00:00
Chris Pearce
65cb09eb2e Update ci test command to allow multithreaded tests 2019-04-07 21:23:02 +01:00
bors
78552ebd7a Auto merge of #141 - cjpearce:fix/run-panics-on-compile-fail, r=komaeda
Stop run from panicking when compile fails

Currently if you use the `rustlings run` command and your program fails to compile, rustlings will panic while trying to exit.

First I've added a couple of integration tests to cover this case, which also meant moving a few tests so that the new fixtures didn't cause `verify_all_success` to fail.

Then I noticed that the existing integration tests that test for failure pass even when rustlings panics, preventing the new tests from failing. I've updated the integration tests to distinguish between when rustlings has failed in the way that we want (exit code 1) rather than a panic (exit code 101).

Finally I fixed the actual panic, which was just caused by unwrapping when rustlings should probably be exiting cleanly.
2019-04-07 20:11:22 +00:00
Chris Pearce
0c7bd12372 Fix test failing due to panic 2019-04-07 20:13:04 +01:00
Chris Pearce
3d11d7685b Modify integration tests to fail on panic 2019-04-07 20:13:04 +01:00
Chris Pearce
592ae6b4d2 Add process id to temp file name 2019-04-07 17:28:51 +01:00
Chris Pearce
4fa79ee02f Extract command builders into util 2019-04-07 17:26:01 +01:00
bors
fbd0ccbd5b Auto merge of #134 - rust-lang:fix/windows-paths, r=komaeda
fix watch command path execution

@hades32 @guttume could you test whether this works on windows by checking out the branch locally and running `cargo run watch`?
2019-04-03 09:37:37 +00:00
komaeda
8c008a0e7d Merge pull request #137 from mgeier/patch-1
Fix order of true/false in tests for executables
2019-03-28 12:11:35 +01:00
Matthias Geier
11fe19d08a Fix order of true/false in tests for executables
1b3469f236 has fixed the tests themselves, but now the original error shows itself.
2019-03-28 11:53:29 +01:00
liv
1b3469f236 make installation command checks more thorough 2019-03-28 10:51:54 +01:00
liv
022921168d fix watch command path execution 2019-03-27 10:58:56 +01:00
komaeda
c6765eb3eb Merge pull request #133 from zacanger/bug/permissions
Fix permissions on exercise files
2019-03-24 15:44:34 +01:00
zacanger
c5a374fbf2 Fix permissions on source files 2019-03-23 14:19:42 -06:00
lyn
f3ee70489f 1.1.0 2019-03-20 21:27:27 +01:00
lyn
6a27ba735c cargo fmt 2019-03-20 21:25:45 +01:00
komaeda
91dce31265 Merge pull request #131 from ColinPitrat/master
Verify that rust version is recent enough to install rustlings.
2019-03-20 21:22:04 +01:00
lyn
040ca18a64 add travis config 2019-03-20 21:08:08 +01:00
lyn
f43cb124f6 add tests 2019-03-20 21:05:45 +01:00
komaeda
11875aed6e adjust author name 2019-03-20 14:51:28 +01:00
Colin Pitrat
f07703eb7a Fix comment position 2019-03-20 11:21:15 +00:00
Colin Pitrat
fd4eda8bda Verify that rust version is recent enough to install rustlings.
I would have liked to write some tests for the vercomp function I
introduce, but there doesn't seem to be any CI setup yet?
2019-03-20 11:18:39 +00:00
komaeda
bf8d927ab2 Merge pull request #123 from kisom/master
Be nicer when rustlings isn't run from the right directory.
2019-03-17 22:28:03 +01:00
Kyle Isom
9fc4a83987 Be nicer when rustlings isn't run from the right directory.
Before, rustlings would panic if it wasn't in the right directory. It
took me a minute to figure out why, and this wasn't my first intro to
Rust. It would probably help new users if they saw a helpful message
instead of a stack trace.
2019-03-17 11:43:47 -07:00
komaeda
63280ed9e4 Merge pull request #119 from LesnyRumcajs/patch-1
Add standard library types to exercises suite
2019-03-17 13:27:48 +01:00
komaeda
25f9d61410 Merge pull request #124 from kisom/update-link
errors2.rs: update link to Rust book.
2019-03-17 13:19:57 +01:00
Kyle Isom
c1f4257a91 errors2.rs: update link to Rust book. 2019-03-16 19:22:06 -07:00
komaeda
8f9d7ce3d8 Merge pull request #120 from abagshaw/master
Start verification at most recently modified file
2019-03-16 12:54:09 +01:00
Andrew Bagshaw
3b5dfac44e Remove unnessecary whitespace 2019-03-15 16:01:45 -07:00
Andrew Bagshaw
a6a8b61b12 Change to \n 2019-03-15 12:47:06 -07:00
Andrew Bagshaw
6cd42bb821 Add clear break between verify executions 2019-03-13 14:08:28 -07:00
Andrew Bagshaw
4d7ce6e571 deduplicate 2019-03-13 13:53:24 -07:00
Andrew Bagshaw
3f114cc069 Start verification at most recently modified file 2019-03-13 13:50:54 -07:00
LesnyRumcajs
58ccd72aff Add standard library types to exercises suite 2019-03-13 15:29:02 +01:00
lyn
abf175111d clippy-ify 2019-03-11 15:09:20 +01:00
lyn
9144c816bf remove obsolete paragraph on --test flag 2019-03-06 22:01:45 +01:00
62 changed files with 423 additions and 134 deletions

11
.travis.yml Normal file
View File

@@ -0,0 +1,11 @@
language: rust
rust:
- stable
- beta
- nightly
script: cargo test --verbose
matrix:
allow_failures:
- rust: nightly
fast_finish: true
cache: cargo

View File

@@ -1,7 +1,7 @@
[package]
name = "rustlings"
version = "1.0.1"
authors = ["Lynn <819880950@qq.com>", "Carol (Nichols || Goulding) <carol.nichols@gmail.com"]
version = "1.2.2"
authors = ["Olivia <819880950@qq.com>", "Carol (Nichols || Goulding) <carol.nichols@gmail.com"]
edition = "2018"
[dependencies]
@@ -11,3 +11,11 @@ console = "0.6.2"
syntect = "3.0.2"
notify = "4.0.0"
toml = "0.4.10"
serde = {version = "1.0.10", features = ["derive"]}
[[bin]]
name = "rustlings"
path = "src/main.rs"
[dev-dependencies]
assert_cmd = "0.11.0"

View File

@@ -68,12 +68,6 @@ In case you want to go by your own order, or want to only verify a single exerci
rustlings run exercises/path/to/exercise.rs
```
Or if it's a `#[test]`:
```bash
rustlings run --test exercises/path/to/exercise_with_test.rs
```
In case you get stuck, there is usually a hint at the bottom of each exercise.
## Testing yourself

View File

@@ -1,5 +1,5 @@
For this exercise check out the sections:
- [Error Handling](https://doc.rust-lang.org/book/2018-edition/ch09-02-recoverable-errors-with-result.html)
- [Generics](https://doc.rust-lang.org/book/2018-edition/ch10-01-syntax.html)
- [Error Handling](https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html)
- [Generics](https://doc.rust-lang.org/book/ch10-01-syntax.html)
of the Rust Book.
of the Rust Book.

0
exercises/error_handling/errors1.rs Executable file → Normal file
View File

2
exercises/error_handling/errors2.rs Executable file → Normal file
View File

@@ -68,5 +68,5 @@ mod tests {
// `Err(something)`. This pattern is very common in Rust, though, so there's
// a `?` operator that does pretty much what you would make that match statement
// do for you! Take a look at this section of the Error Handling chapter:
// https://doc.rust-lang.org/stable/book/second-edition/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator
// https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator
// and give it a try!

0
exercises/error_handling/errors3.rs Executable file → Normal file
View File

0
exercises/error_handling/errorsn.rs Executable file → Normal file
View File

0
exercises/error_handling/option1.rs Executable file → Normal file
View File

0
exercises/error_handling/result1.rs Executable file → Normal file
View File

0
exercises/functions/functions1.rs Executable file → Normal file
View File

0
exercises/functions/functions2.rs Executable file → Normal file
View File

0
exercises/functions/functions3.rs Executable file → Normal file
View File

0
exercises/functions/functions4.rs Executable file → Normal file
View File

0
exercises/functions/functions5.rs Executable file → Normal file
View File

0
exercises/if/if1.rs Executable file → Normal file
View File

0
exercises/macros/macros1.rs Executable file → Normal file
View File

0
exercises/macros/macros2.rs Executable file → Normal file
View File

0
exercises/macros/macros3.rs Executable file → Normal file
View File

0
exercises/macros/macros4.rs Executable file → Normal file
View File

0
exercises/modules/modules1.rs Executable file → Normal file
View File

0
exercises/modules/modules2.rs Executable file → Normal file
View File

0
exercises/move_semantics/move_semantics1.rs Executable file → Normal file
View File

0
exercises/move_semantics/move_semantics2.rs Executable file → Normal file
View File

0
exercises/move_semantics/move_semantics3.rs Executable file → Normal file
View File

3
exercises/move_semantics/move_semantics4.rs Executable file → Normal file
View File

@@ -16,7 +16,8 @@ fn main() {
}
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
// `fill_vec()` no longer take `vec: Vec<i32>` as argument
fn fill_vec() -> Vec<i32> {
let mut vec = vec;
vec.push(22);

0
exercises/primitive_types/primitive_types1.rs Executable file → Normal file
View File

0
exercises/primitive_types/primitive_types2.rs Executable file → Normal file
View File

0
exercises/primitive_types/primitive_types3.rs Executable file → Normal file
View File

0
exercises/primitive_types/primitive_types4.rs Executable file → Normal file
View File

0
exercises/primitive_types/primitive_types5.rs Executable file → Normal file
View File

0
exercises/primitive_types/primitive_types6.rs Executable file → Normal file
View File

0
exercises/standard_library_types/arc1.rs Executable file → Normal file
View File

0
exercises/standard_library_types/iterator3.rs Executable file → Normal file
View File

0
exercises/standard_library_types/iterators4.rs Executable file → Normal file
View File

0
exercises/strings/strings1.rs Executable file → Normal file
View File

0
exercises/strings/strings2.rs Executable file → Normal file
View File

7
exercises/test2.rs Executable file → Normal file
View File

@@ -17,6 +17,11 @@ mod tests {
#[test]
fn returns_twice_of_positive_numbers() {
assert_eq!(4, 4);
assert_eq!(times_two(4), ???);
}
#[test]
fn returns_twice_of_negative_numbers() {
// TODO write an assert for `times_two(-4)`
}
}

0
exercises/test3.rs Executable file → Normal file
View File

0
exercises/tests/tests1.rs Executable file → Normal file
View File

0
exercises/tests/tests2.rs Executable file → Normal file
View File

0
exercises/tests/tests3.rs Executable file → Normal file
View File

0
exercises/threads/threads1.rs Executable file → Normal file
View File

0
exercises/variables/variables1.rs Executable file → Normal file
View File

0
exercises/variables/variables2.rs Executable file → Normal file
View File

0
exercises/variables/variables3.rs Executable file → Normal file
View File

0
exercises/variables/variables4.rs Executable file → Normal file
View File

View File

@@ -196,4 +196,18 @@ mode = "test"
[[exercises]]
path = "exercises/threads/threads1.rs"
mode = "compile"
mode = "compile"
# STANDARD LIBRARY TYPES
[[exercises]]
path = "exercises/standard_library_types/arc1.rs"
mode = "compile"
[[exercises]]
path = "exercises/standard_library_types/iterator3.rs"
mode = "test"
[[exercises]]
path = "exercises/standard_library_types/iterators4.rs"
mode = "test"

View File

@@ -3,31 +3,83 @@
echo "Let's get you set up with Rustlings!"
echo "Checking requirements..."
if [ -x "$(git)" ]
if [ -x "$(command -v git)" ]
then
echo "SUCCESS: Git is installed"
else
echo "WARNING: Git does not seem to be installed."
echo "Please download Git using your package manager or over https://git-scm.com/!"
exit 1
else
echo "SUCCESS: Git is installed"
fi
if [ -x "$(rustc)" ]
if [ -x "$(command -v rustc)" ]
then
echo "SUCCESS: Rust is installed"
else
echo "WARNING: Rust does not seem to be installed."
echo "Please download Rust using https://rustup.rs!"
exit 1
else
echo "SUCCESS: Rust is installed"
fi
if [ -x "$(cargo)" ]
if [ -x "$(command -v cargo)" ]
then
echo "SUCCESS: Cargo is installed"
else
echo "WARNING: Cargo does not seem to be installed."
echo "Please download Rust and Cargo using https://rustup.rs!"
exit 1
fi
# Function that compares two versions strings v1 and v2 given in arguments (e.g 1.31 and 1.33.0).
# Returns 1 if v1 > v2, 0 if v1 == v2, 2 if v1 < v2.
function vercomp() {
if [[ $1 == $2 ]]
then
return 0
fi
v1=( ${1//./ } )
v2=( ${2//./ } )
len1=${#v1[@]}
len2=${#v2[@]}
max_len=$len1
if [[ $max_len -lt $len2 ]]
then
max_len=$len2
fi
for i in `seq 0 $max_len`
do
# Fill empty fields with zeros in v1
if [ -z "${v1[$i]}" ]
then
v1[$i]=0
fi
# And in v2
if [ -z "${v2[$i]}" ]
then
v2[$i]=0
fi
if [ ${v1[$i]} -gt ${v2[$i]} ]
then
return 1
fi
if [ ${v1[$i]} -lt ${v2[$i]} ]
then
return 2
fi
done
return 0
}
RustVersion=$(rustc --version | cut -d " " -f 2)
MinRustVersion=1.31
vercomp $RustVersion $MinRustVersion
if [ $? -eq 2 ]
then
echo "WARNING: Rust version is too old: $RustVersion - needs at least $MinRustVersion"
echo "Please update Rust with 'rustup update'"
exit 1
else
echo "SUCCESS: Cargo is installed"
echo "SUCCESS: Rust is up to date"
fi
Path=${1:-rustlings/}

79
src/exercise.rs Normal file
View File

@@ -0,0 +1,79 @@
use serde::Deserialize;
use std::fmt::{self, Display, Formatter};
use std::fs::{remove_file};
use std::path::{PathBuf};
use std::process::{self, Command, Output};
const RUSTC_COLOR_ARGS: &[&str] = &["--color", "always"];
fn temp_file() -> String {
format!("./temp_{}", process::id())
}
#[derive(Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Mode {
Compile,
Test,
}
#[derive(Deserialize)]
pub struct ExerciseList {
pub exercises: Vec<Exercise>,
}
#[derive(Deserialize)]
pub struct Exercise {
pub path: PathBuf,
pub mode: Mode,
}
impl Exercise {
pub fn compile(&self) -> Output {
match self.mode {
Mode::Compile => Command::new("rustc")
.args(&[self.path.to_str().unwrap(), "-o", &temp_file()])
.args(RUSTC_COLOR_ARGS)
.output(),
Mode::Test => Command::new("rustc")
.args(&["--test", self.path.to_str().unwrap(), "-o", &temp_file()])
.args(RUSTC_COLOR_ARGS)
.output(),
}
.expect("Failed to run 'compile' command.")
}
pub fn run(&self) -> Output {
Command::new(&temp_file())
.output()
.expect("Failed to run 'run' command")
}
pub fn clean(&self) {
let _ignored = remove_file(&temp_file());
}
}
impl Display for Exercise {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.path.to_str().unwrap())
}
}
#[cfg(test)]
mod test {
use super::*;
use std::path::Path;
use std::fs::File;
#[test]
fn test_clean() {
File::create(&temp_file()).unwrap();
let exercise = Exercise {
path: PathBuf::from("example.rs"),
mode: Mode::Test,
};
exercise.clean();
assert!(!Path::new(&temp_file()).exists());
}
}

View File

@@ -1,10 +1,13 @@
use crate::exercise::{Exercise, ExerciseList};
use crate::run::run;
use crate::verify::verify;
use clap::{crate_version, App, Arg, SubCommand};
use notify::DebouncedEvent;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use std::ffi::OsStr;
use std::fs;
use std::io::BufRead;
use std::path::Path;
use std::sync::mpsc::channel;
use std::time::Duration;
use syntect::easy::HighlightFile;
@@ -12,8 +15,8 @@ use syntect::highlighting::{Style, ThemeSet};
use syntect::parsing::SyntaxSet;
use syntect::util::as_24_bit_terminal_escaped;
mod exercise;
mod run;
mod util;
mod verify;
fn main() {
@@ -36,7 +39,7 @@ fn main() {
let ts = ThemeSet::load_defaults();
if None == matches.subcommand_name() {
println!("");
println!();
println!(r#" welcome to... "#);
println!(r#" _ _ _ "#);
println!(r#" _ __ _ _ ___| |_| (_)_ __ __ _ ___ "#);
@@ -44,25 +47,51 @@ fn main() {
println!(r#" | | | |_| \__ \ |_| | | | | | (_| \__ \ "#);
println!(r#" |_| \__,_|___/\__|_|_|_| |_|\__, |___/ "#);
println!(r#" |___/ "#);
println!("");
println!();
}
if let Some(matches) = matches.subcommand_matches("run") {
run(matches.clone()).unwrap();
if !Path::new("info.toml").exists() {
println!(
"{} must be run from the rustlings directory",
std::env::current_exe().unwrap().to_str().unwrap()
);
println!("Try `cd rustlings/`!");
std::process::exit(1);
}
if let Some(_) = matches.subcommand_matches("verify") {
match verify() {
Ok(_) => {}
Err(_) => std::process::exit(1),
}
let toml_str = &fs::read_to_string("info.toml").unwrap();
let exercises = toml::from_str::<ExerciseList>(toml_str).unwrap().exercises;
if let Some(ref matches) = matches.subcommand_matches("run") {
let filename = matches.value_of("file").unwrap_or_else(|| {
println!("Please supply a file name!");
std::process::exit(1);
});
let matching_exercise = |e: &&Exercise| {
Path::new(filename)
.canonicalize()
.map(|p| p.ends_with(&e.path))
.unwrap_or(false)
};
let exercise = exercises.iter().find(matching_exercise).unwrap_or_else(|| {
println!("No exercise found for your file name!");
std::process::exit(1)
});
run(&exercise).unwrap_or_else(|_| std::process::exit(1));
}
if let Some(_) = matches.subcommand_matches("watch") {
watch().unwrap();
if matches.subcommand_matches("verify").is_some() {
verify(&exercises).unwrap_or_else(|_| std::process::exit(1));
}
if let None = matches.subcommand_name() {
if matches.subcommand_matches("watch").is_some() {
watch(&exercises).unwrap();
}
if matches.subcommand_name().is_none() {
let mut highlighter =
HighlightFile::new("default_out.md", &ss, &ts.themes["base16-eighties.dark"]).unwrap();
for maybe_line in highlighter.reader.lines() {
@@ -75,20 +104,25 @@ fn main() {
println!("\x1b[0m");
}
fn watch() -> notify::Result<()> {
fn watch(exercises: &[Exercise]) -> notify::Result<()> {
let (tx, rx) = channel();
let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(2))?;
watcher.watch("./exercises", RecursiveMode::Recursive)?;
watcher.watch(Path::new("./exercises"), RecursiveMode::Recursive)?;
let _ignored = verify();
let _ignored = verify(exercises.iter());
loop {
match rx.recv() {
Ok(event) => match event {
DebouncedEvent::Create(b) | DebouncedEvent::Chmod(b) | DebouncedEvent::Write(b) => {
if b.extension() == Some(OsStr::new("rs")) {
let _ignored = verify();
println!("----------**********----------\n");
let filepath = b.as_path().canonicalize().unwrap();
let exercise = exercises
.iter()
.skip_while(|e| !filepath.ends_with(&e.path));
let _ignored = verify(exercise);
}
}
_ => {}

View File

@@ -1,72 +1,52 @@
use crate::util::clean;
use crate::exercise::{Mode, Exercise};
use crate::verify::test;
use console::{style, Emoji};
use indicatif::ProgressBar;
use std::fs;
use std::process::Command;
use toml::Value;
pub fn run(matches: clap::ArgMatches) -> Result<(), ()> {
if let Some(filename) = matches.value_of("file") {
let toml: Value = fs::read_to_string("info.toml").unwrap().parse().unwrap();
let tomlvec: &Vec<Value> = toml.get("exercises").unwrap().as_array().unwrap();
let mut exercises = tomlvec.clone();
exercises.retain(|i| i.get("path").unwrap().as_str().unwrap() == filename);
if exercises.is_empty() {
println!("No exercise found for your filename!");
std::process::exit(1);
}
let exercise: &Value = &exercises[0];
match exercise.get("mode").unwrap().as_str().unwrap() {
"test" => test(exercise.get("path").unwrap().as_str().unwrap())?,
"compile" => compile_and_run(exercise.get("path").unwrap().as_str().unwrap())?,
_ => (),
}
Ok(())
} else {
panic!("Please supply a filename!");
pub fn run(exercise: &Exercise) -> Result<(), ()> {
match exercise.mode {
Mode::Test => test(exercise)?,
Mode::Compile => compile_and_run(exercise)?,
}
Ok(())
}
pub fn compile_and_run(filename: &str) -> Result<(), ()> {
let bar = ProgressBar::new_spinner();
bar.set_message(format!("Compiling {}...", filename).as_str());
bar.enable_steady_tick(100);
let compilecmd = Command::new("rustc")
.args(&[filename, "-o", "temp", "--color", "always"])
.output()
.expect("fail");
bar.set_message(format!("Running {}...", filename).as_str());
pub fn compile_and_run(exercise: &Exercise) -> Result<(), ()> {
let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Compiling {}...", exercise).as_str());
progress_bar.enable_steady_tick(100);
let compilecmd = exercise.compile();
progress_bar.set_message(format!("Running {}...", exercise).as_str());
if compilecmd.status.success() {
let runcmd = Command::new("./temp").output().expect("fail");
bar.finish_and_clear();
let runcmd = exercise.run();
progress_bar.finish_and_clear();
if runcmd.status.success() {
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
let formatstr = format!("{} Successfully ran {}", Emoji("", ""), filename);
let formatstr = format!("{} Successfully ran {}", Emoji("", ""), exercise);
println!("{}", style(formatstr).green());
clean();
exercise.clean();
Ok(())
} else {
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
println!("{}", String::from_utf8_lossy(&runcmd.stderr));
let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename);
let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), exercise);
println!("{}", style(formatstr).red());
clean();
exercise.clean();
Err(())
}
} else {
bar.finish_and_clear();
progress_bar.finish_and_clear();
let formatstr = format!(
"{} Compilation of {} failed! Compiler error message:\n",
Emoji("⚠️ ", "!"),
filename
exercise
);
println!("{}", style(formatstr).red());
println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
clean();
exercise.clean();
Err(())
}
}

View File

@@ -1,5 +0,0 @@
use std::fs::remove_file;
pub fn clean() {
let _ignored = remove_file("temp");
}

View File

@@ -1,93 +1,83 @@
use crate::util::clean;
use crate::exercise::{Exercise, Mode};
use console::{style, Emoji};
use indicatif::ProgressBar;
use std::fs;
use std::process::Command;
use toml::Value;
pub fn verify() -> Result<(), ()> {
let toml: Value = fs::read_to_string("info.toml").unwrap().parse().unwrap();
let tomlvec: &Vec<Value> = toml.get("exercises").unwrap().as_array().unwrap();
for i in tomlvec {
match i.get("mode").unwrap().as_str().unwrap() {
"test" => test(i.get("path").unwrap().as_str().unwrap())?,
"compile" => compile_only(i.get("path").unwrap().as_str().unwrap())?,
_ => (),
pub fn verify<'a>(start_at: impl IntoIterator<Item=&'a Exercise>) -> Result<(), ()> {
for exercise in start_at {
match exercise.mode {
Mode::Test => test(&exercise)?,
Mode::Compile => compile_only(&exercise)?,
}
}
Ok(())
}
fn compile_only(filename: &str) -> Result<(), ()> {
let bar = ProgressBar::new_spinner();
bar.set_message(format!("Compiling {}...", filename).as_str());
bar.enable_steady_tick(100);
let compilecmd = Command::new("rustc")
.args(&[filename, "-o", "temp", "--color", "always"])
.output()
.expect("fail");
bar.finish_and_clear();
if compilecmd.status.success() {
fn compile_only(exercise: &Exercise) -> Result<(), ()> {
let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Compiling {}...", exercise).as_str());
progress_bar.enable_steady_tick(100);
let compile_output = exercise.compile();
progress_bar.finish_and_clear();
if compile_output.status.success() {
let formatstr = format!(
"{} Successfully compiled {}!",
Emoji("", ""),
filename
exercise
);
println!("{}", style(formatstr).green());
clean();
exercise.clean();
Ok(())
} else {
let formatstr = format!(
"{} Compilation of {} failed! Compiler error message:\n",
Emoji("⚠️ ", "!"),
filename
exercise
);
println!("{}", style(formatstr).red());
println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
clean();
println!("{}", String::from_utf8_lossy(&compile_output.stderr));
exercise.clean();
Err(())
}
}
pub fn test(filename: &str) -> Result<(), ()> {
let bar = ProgressBar::new_spinner();
bar.set_message(format!("Testing {}...", filename).as_str());
bar.enable_steady_tick(100);
let testcmd = Command::new("rustc")
.args(&["--test", filename, "-o", "temp", "--color", "always"])
.output()
.expect("fail");
if testcmd.status.success() {
bar.set_message(format!("Running {}...", filename).as_str());
let runcmd = Command::new("./temp").output().expect("fail");
bar.finish_and_clear();
pub fn test(exercise: &Exercise) -> Result<(), ()> {
let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Testing {}...", exercise).as_str());
progress_bar.enable_steady_tick(100);
let compile_output = exercise.compile();
if compile_output.status.success() {
progress_bar.set_message(format!("Running {}...", exercise).as_str());
let runcmd = exercise.run();
progress_bar.finish_and_clear();
if runcmd.status.success() {
let formatstr = format!("{} Successfully tested {}!", Emoji("", ""), filename);
let formatstr = format!("{} Successfully tested {}!", Emoji("", ""), exercise);
println!("{}", style(formatstr).green());
clean();
exercise.clean();
Ok(())
} else {
let formatstr = format!(
"{} Testing of {} failed! Please try again. Here's the output:",
Emoji("⚠️ ", "!"),
filename
exercise
);
println!("{}", style(formatstr).red());
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
clean();
exercise.clean();
Err(())
}
} else {
bar.finish_and_clear();
progress_bar.finish_and_clear();
let formatstr = format!(
"{} Compiling of {} failed! Please try again. Here's the output:",
Emoji("⚠️ ", "!"),
filename
exercise
);
println!("{}", style(formatstr).red());
println!("{}", String::from_utf8_lossy(&testcmd.stderr));
clean();
println!("{}", String::from_utf8_lossy(&compile_output.stderr));
exercise.clean();
Err(())
}
}

View File

@@ -0,0 +1,3 @@
fn main() {
let
}

View File

@@ -0,0 +1,2 @@
fn main() {
}

View File

@@ -0,0 +1,7 @@
[[exercises]]
path = "compFailure.rs"
mode = "compile"
[[exercises]]
path = "testFailure.rs"
mode = "test"

View File

@@ -0,0 +1,4 @@
#[test]
fn passing() {
asset!(true);
}

View File

@@ -0,0 +1,2 @@
fn main() {
}

View File

@@ -0,0 +1,7 @@
[[exercises]]
path = "compSuccess.rs"
mode = "compile"
[[exercises]]
path = "testSuccess.rs"
mode = "test"

View File

@@ -0,0 +1,4 @@
#[test]
fn passing() {
assert!(true);
}

View File

@@ -0,0 +1,97 @@
use assert_cmd::prelude::*;
use std::process::Command;
#[test]
fn runs_without_arguments() {
let mut cmd = Command::cargo_bin("rustlings").unwrap();
cmd.assert().success();
}
#[test]
fn fails_when_in_wrong_dir() {
Command::cargo_bin("rustlings")
.unwrap()
.current_dir("tests/")
.assert()
.code(1);
}
#[test]
fn verify_all_success() {
Command::cargo_bin("rustlings")
.unwrap()
.arg("v")
.current_dir("tests/fixture/success")
.assert()
.success();
}
#[test]
fn verify_all_failure() {
Command::cargo_bin("rustlings")
.unwrap()
.arg("v")
.current_dir("tests/fixture/failure")
.assert()
.code(1);
}
#[test]
fn run_single_compile_success() {
Command::cargo_bin("rustlings")
.unwrap()
.args(&["r", "compSuccess.rs"])
.current_dir("tests/fixture/success/")
.assert()
.success();
}
#[test]
fn run_single_compile_failure() {
Command::cargo_bin("rustlings")
.unwrap()
.args(&["r", "compFailure.rs"])
.current_dir("tests/fixture/failure/")
.assert()
.code(1);
}
#[test]
fn run_single_test_success() {
Command::cargo_bin("rustlings")
.unwrap()
.args(&["r", "testSuccess.rs"])
.current_dir("tests/fixture/success/")
.assert()
.success();
}
#[test]
fn run_single_test_failure() {
Command::cargo_bin("rustlings")
.unwrap()
.args(&["r", "testFailure.rs"])
.current_dir("tests/fixture/failure/")
.assert()
.code(1);
}
#[test]
fn run_single_test_no_filename() {
Command::cargo_bin("rustlings")
.unwrap()
.arg("r")
.current_dir("tests/fixture/")
.assert()
.code(1);
}
#[test]
fn run_single_test_no_exercise() {
Command::cargo_bin("rustlings")
.unwrap()
.args(&["r", "compNoExercise.rs"])
.current_dir("tests/fixture/failure")
.assert()
.code(1);
}