Skip to content

Commit 829f285

Browse files
authored
Merge pull request #101 from rage/parse-fix
Parse fix
2 parents ba54438 + 2817782 commit 829f285

File tree

11 files changed

+189
-61
lines changed

11 files changed

+189
-61
lines changed

plugins/csharp/src/plugin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use tmc_langs_framework::{
1717
},
1818
error::{CommandError, FileIo},
1919
file_util,
20-
nom::{bytes, character, combinator, sequence, IResult},
20+
nom::{bytes, character, combinator, error::VerboseError, sequence, IResult},
2121
plugin::Language,
2222
zip::ZipArchive,
2323
LanguagePlugin, TmcError,
@@ -353,7 +353,7 @@ impl LanguagePlugin for CSharpPlugin {
353353
vec![PathBuf::from("test")]
354354
}
355355

356-
fn points_parser(i: &str) -> IResult<&str, &str> {
356+
fn points_parser(i: &str) -> IResult<&str, &str, VerboseError<&str>> {
357357
combinator::map(
358358
sequence::delimited(
359359
sequence::tuple((

plugins/java/src/ant_plugin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use tmc_langs_framework::{
1414
command::TmcCommand,
1515
domain::{ExerciseDesc, RunResult, StyleValidationResult},
1616
file_util,
17-
nom::IResult,
17+
nom::{error::VerboseError, IResult},
1818
plugin::{Language, LanguagePlugin},
1919
TmcError,
2020
};
@@ -126,7 +126,7 @@ impl LanguagePlugin for AntPlugin {
126126
Ok(())
127127
}
128128

129-
fn points_parser(i: &str) -> IResult<&str, &str> {
129+
fn points_parser(i: &str) -> IResult<&str, &str, VerboseError<&str>> {
130130
Self::java_points_parser(i)
131131
}
132132

plugins/java/src/java_plugin.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use tmc_langs_framework::{
1111
command::TmcCommand,
1212
domain::{ExerciseDesc, RunResult, RunStatus, StyleValidationResult, TestDesc, TestResult},
1313
file_util,
14-
nom::{bytes, character, combinator, sequence, IResult},
14+
nom::{bytes, character, combinator, error::VerboseError, sequence, IResult},
1515
plugin::{Language, LanguagePlugin},
1616
};
1717
use walkdir::WalkDir;
@@ -254,7 +254,7 @@ pub(crate) trait JavaPlugin: LanguagePlugin {
254254
}
255255

256256
/// Parses @Points("1.1") point annotations.
257-
fn java_points_parser(i: &str) -> IResult<&str, &str> {
257+
fn java_points_parser(i: &str) -> IResult<&str, &str, VerboseError<&str>> {
258258
combinator::map(
259259
sequence::delimited(
260260
sequence::tuple((
@@ -285,7 +285,7 @@ mod test {
285285
use crate::SEPARATOR;
286286

287287
use super::*;
288-
use tmc_langs_framework::{anyhow, TmcError};
288+
use tmc_langs_framework::{anyhow, nom, TmcError};
289289

290290
fn init() {
291291
use log::*;
@@ -363,7 +363,7 @@ mod test {
363363
unimplemented!()
364364
}
365365

366-
fn points_parser<'a>(i: &'a str) -> IResult<&'a str, &'a str> {
366+
fn points_parser(i: &str) -> IResult<&str, &str, nom::error::VerboseError<&str>> {
367367
Self::java_points_parser(i)
368368
}
369369
}

plugins/java/src/maven_plugin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use tmc_langs_framework::{
1616
command::TmcCommand,
1717
domain::{ExerciseDesc, RunResult, StyleValidationResult},
1818
file_util,
19-
nom::IResult,
19+
nom::{error::VerboseError, IResult},
2020
plugin::{Language, LanguagePlugin},
2121
TmcError,
2222
};
@@ -133,7 +133,7 @@ impl LanguagePlugin for MavenPlugin {
133133
vec![PathBuf::from("src/test")]
134134
}
135135

136-
fn points_parser(i: &str) -> IResult<&str, &str> {
136+
fn points_parser(i: &str) -> IResult<&str, &str, VerboseError<&str>> {
137137
Self::java_points_parser(i)
138138
}
139139
}

plugins/make/src/plugin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use tmc_langs_framework::{
1616
domain::{ExerciseDesc, RunResult, RunStatus, TestDesc},
1717
error::{CommandError, FileIo},
1818
file_util,
19-
nom::{bytes, character, combinator, sequence, IResult},
19+
nom::{bytes, character, combinator, error::VerboseError, sequence, IResult},
2020
plugin::LanguagePlugin,
2121
subprocess::PopenError,
2222
zip::ZipArchive,
@@ -348,7 +348,7 @@ impl LanguagePlugin for MakePlugin {
348348
vec![PathBuf::from("test")]
349349
}
350350

351-
fn points_parser(i: &str) -> IResult<&str, &str> {
351+
fn points_parser(i: &str) -> IResult<&str, &str, VerboseError<&str>> {
352352
combinator::map(
353353
sequence::delimited(
354354
sequence::tuple((

plugins/notests/src/plugin.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::time::Duration;
88
use tmc_langs_framework::{
99
anyhow,
1010
domain::{ExerciseDesc, RunResult, RunStatus, TestDesc, TestResult},
11-
nom::IResult,
11+
nom::{self, error::VerboseError, IResult},
1212
zip::ZipArchive,
1313
LanguagePlugin, StudentFilePolicy, TmcError,
1414
};
@@ -102,8 +102,9 @@ impl LanguagePlugin for NoTestsPlugin {
102102
vec![PathBuf::from("test")]
103103
}
104104

105-
fn points_parser(_: &str) -> IResult<&str, &str> {
106-
Ok(("", ""))
105+
fn points_parser(i: &str) -> IResult<&str, &str, VerboseError<&str>> {
106+
// does not match any characters
107+
nom::combinator::value("", nom::character::complete::one_of(""))(i)
107108
}
108109
}
109110

@@ -228,4 +229,26 @@ no-tests: false
228229
);
229230
assert!(!NoTestsPlugin::is_exercise_type_correct(temp_dir.path()));
230231
}
232+
233+
#[test]
234+
fn parses_empty() {
235+
init();
236+
237+
let temp = tempfile::tempdir().unwrap();
238+
file_to(&temp, "test/.keep", r#""#);
239+
240+
let points = NoTestsPlugin::get_available_points(temp.path()).unwrap();
241+
assert!(points.is_empty());
242+
243+
let temp = tempfile::tempdir().unwrap();
244+
file_to(
245+
&temp,
246+
"test/.keep",
247+
r#"
248+
"#,
249+
);
250+
251+
let points = NoTestsPlugin::get_available_points(temp.path()).unwrap();
252+
assert!(points.is_empty());
253+
}
231254
}

plugins/python3/src/plugin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use tmc_langs_framework::{
1515
domain::{ExerciseDesc, RunResult, RunStatus, TestDesc, TestResult},
1616
error::CommandError,
1717
file_util,
18-
nom::{branch, bytes, character, combinator, sequence, IResult},
18+
nom::{branch, bytes, character, combinator, error::VerboseError, sequence, IResult},
1919
plugin::LanguagePlugin,
2020
TmcError, TmcProjectYml,
2121
};
@@ -334,7 +334,7 @@ impl LanguagePlugin for Python3Plugin {
334334
vec![PathBuf::from("test"), PathBuf::from("tmc")]
335335
}
336336

337-
fn points_parser(i: &str) -> IResult<&str, &str> {
337+
fn points_parser(i: &str) -> IResult<&str, &str, VerboseError<&str>> {
338338
combinator::map(
339339
sequence::delimited(
340340
sequence::tuple((

plugins/r/src/plugin.rs

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tmc_langs_framework::{
1313
command::TmcCommand,
1414
domain::{ExerciseDesc, RunResult, TestDesc},
1515
file_util,
16-
nom::{bytes, character, combinator, sequence, IResult},
16+
nom::{branch, bytes, character, combinator, error::VerboseError, sequence, IResult},
1717
zip::ZipArchive,
1818
LanguagePlugin, TmcError,
1919
};
@@ -151,26 +151,44 @@ impl LanguagePlugin for RPlugin {
151151
vec![PathBuf::from("tests")]
152152
}
153153

154-
fn points_parser(i: &str) -> IResult<&str, &str> {
155-
combinator::map(
154+
fn points_parser(i: &str) -> IResult<&str, &str, VerboseError<&str>> {
155+
let test_parser = sequence::delimited(
156+
sequence::tuple((
157+
bytes::complete::tag("test"),
158+
character::complete::multispace0,
159+
character::complete::char('('),
160+
bytes::complete::take_until(","),
161+
bytes::complete::take_until("\""),
162+
)),
163+
sequence::delimited(
164+
character::complete::char('"'),
165+
bytes::complete::is_not("\""),
166+
character::complete::char('"'),
167+
),
168+
sequence::tuple((
169+
character::complete::multispace0,
170+
character::complete::char(')'),
171+
)),
172+
);
173+
let points_for_all_tests_parser = sequence::delimited(
174+
sequence::tuple((
175+
bytes::complete::tag("points_for_all_tests"),
176+
character::complete::multispace0,
177+
character::complete::char('('),
178+
bytes::complete::take_until("\""),
179+
)),
156180
sequence::delimited(
157-
sequence::tuple((
158-
bytes::complete::tag("test"),
159-
character::complete::multispace0,
160-
character::complete::char('('),
161-
bytes::complete::take_until(","),
162-
bytes::complete::take_until("\""),
163-
)),
164-
sequence::delimited(
165-
character::complete::char('"'),
166-
bytes::complete::is_not("\""),
167-
character::complete::char('"'),
168-
),
169-
sequence::tuple((
170-
character::complete::multispace0,
171-
character::complete::char(')'),
172-
)),
181+
character::complete::char('"'),
182+
bytes::complete::is_not("\""),
183+
character::complete::char('"'),
173184
),
185+
sequence::tuple((
186+
character::complete::multispace0,
187+
character::complete::char(')'),
188+
)),
189+
);
190+
combinator::map(
191+
branch::alt((test_parser, points_for_all_tests_parser)),
174192
str::trim,
175193
)(i)
176194
}
@@ -475,4 +493,39 @@ test("sample", c("r1.1"), {
475493
"#;
476494
assert_eq!(RPlugin::points_parser(target).unwrap().1, "W1A.1.2");
477495
}
496+
497+
#[test]
498+
fn parsing_regression_test() {
499+
init();
500+
501+
let temp = tempfile::tempdir().unwrap();
502+
// a file like this used to cause an error before for some reason...
503+
file_to(
504+
&temp,
505+
"tests/testthat/testExercise.R",
506+
r#"library('testthat')
507+
"#,
508+
);
509+
510+
let _points = RPlugin::get_available_points(temp.path()).unwrap();
511+
}
512+
513+
#[test]
514+
fn parses_points_for_all_tests() {
515+
init();
516+
517+
let temp = tempfile::tempdir().unwrap();
518+
file_to(
519+
&temp,
520+
"tests/testthat/testExercise.R",
521+
r#"
522+
something
523+
points_for_all_tests(c("r1"))
524+
etc
525+
"#,
526+
);
527+
528+
let points = RPlugin::get_available_points(temp.path()).unwrap();
529+
assert_eq!(points, &["r1"]);
530+
}
478531
}

tmc-langs-framework/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ serde_yaml = "0.8"
1717
zip = "0.5"
1818
schemars = "0.8"
1919
once_cell = "1"
20-
nom = "6"
20+
nom = { version = "6", features = ["alloc"] }
2121
funty = "=1.1.0" # temporary workaround, remove later
2222
subprocess = "0.2"
2323
tempfile = "3"

tmc-langs-framework/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ pub enum TmcError {
2727
Canonicalize(PathBuf, #[source] std::io::Error),
2828
#[error("File {0} not in given project root {1}")]
2929
FileNotInProject(PathBuf, PathBuf),
30-
#[error("Error while parsing available points: {0}")]
31-
PointParse(String),
30+
#[error("Error while parsing available points from {0}")]
31+
PointParse(PathBuf, #[source] nom::error::VerboseError<String>),
3232

3333
#[error("Path {0} contained no file name")]
3434
NoFileName(PathBuf),

0 commit comments

Comments
 (0)