Skip to content

Commit b588353

Browse files
authored
Merge pull request #217 from rage/policies
Policies
2 parents 3275148 + dfa206b commit b588353

File tree

75 files changed

+607
-411
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+607
-411
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/target
22
**/*.rs.bk
33
.vscode
4+
/test-cache

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings/tmc-langs-node/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ fn login(mut cx: FunctionContext) -> JsResult<JsValue> {
547547
password
548548
};
549549
let token = with_client(&client_name, client_version, |client| {
550-
tmc_langs::login_with_password(client, &client_name, email, decoded)
550+
tmc_langs::login_with_password(client, email, decoded)
551551
})
552552
.map_err(|e| convert_err(&mut cx, e))?;
553553

crates/plugins/csharp/src/policy.rs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
11
//! Student file policy for the C# plugin.
22
3-
use std::path::Path;
3+
use std::{ffi::OsStr, path::Path};
44
use tmc_langs_framework::{StudentFilePolicy, TmcProjectYml};
55

66
pub struct CSharpStudentFilePolicy {
77
project_config: TmcProjectYml,
88
}
99

10-
impl CSharpStudentFilePolicy {
11-
/// Goes up directories until a bin or obj directory is found, either indicating that the path is in a binary directory.
12-
fn is_child_of_binary_dir(path: &Path) -> bool {
13-
// checks each parent directory for bin or obj
14-
for ancestor in path.ancestors().skip(1) {
15-
if let Some(file_name) = ancestor.file_name() {
16-
if file_name == "bin" || file_name == "obj" {
17-
return true;
18-
}
19-
}
20-
}
21-
false
22-
}
23-
}
24-
2510
impl StudentFilePolicy for CSharpStudentFilePolicy {
2611
fn new_with_project_config(project_config: TmcProjectYml) -> Self
2712
where
@@ -34,14 +19,9 @@ impl StudentFilePolicy for CSharpStudentFilePolicy {
3419
&self.project_config
3520
}
3621

37-
// false for .csproj files and files in bin or obj directories
38-
// true for files in src except for .csproj files
22+
// .cs files in src
3923
fn is_non_extra_student_file(&self, path: &Path) -> bool {
40-
path.starts_with("src")
41-
// exclude files in bin
42-
&& !Self::is_child_of_binary_dir(path)
43-
// exclude .csproj files
44-
&& !path.extension().map(|ext| ext == "csproj").unwrap_or_default()
24+
path.starts_with("src") && path.extension() == Some(OsStr::new("cs"))
4525
}
4626
}
4727

@@ -53,14 +33,14 @@ mod test {
5333
fn file_in_binary_dir_is_not_student_file() {
5434
let policy = CSharpStudentFilePolicy::new(Path::new(".")).unwrap();
5535
assert!(!policy.is_student_file(Path::new("src/bin/any/file")));
56-
assert!(!policy.is_student_file(Path::new("obj/any/src/file")));
36+
assert!(!policy.is_student_file(Path::new("obj/any/src/file.cs")));
5737
}
5838

5939
#[test]
60-
fn file_in_src_is_student_file() {
40+
fn cs_file_in_src_is_student_file() {
6141
let policy = CSharpStudentFilePolicy::new(Path::new(".")).unwrap();
62-
assert!(policy.is_student_file(Path::new("src/file")));
63-
assert!(policy.is_student_file(Path::new("src/any/file")));
42+
assert!(policy.is_student_file(Path::new("src/file.cs")));
43+
assert!(policy.is_student_file(Path::new("src/any/file.cs")));
6444
}
6545

6646
#[test]

crates/plugins/java/src/ant_policy.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Ant student file policy
22
3-
use std::path::Path;
3+
use std::{ffi::OsStr, path::Path};
44
use tmc_langs_framework::{StudentFilePolicy, TmcProjectYml};
55

66
pub struct AntStudentFilePolicy {
@@ -20,7 +20,7 @@ impl StudentFilePolicy for AntStudentFilePolicy {
2020
}
2121

2222
fn is_non_extra_student_file(&self, path: &Path) -> bool {
23-
path.starts_with("src")
23+
path.starts_with("src") && path.extension() == Some(OsStr::new("java"))
2424
}
2525
}
2626

@@ -31,13 +31,14 @@ mod test {
3131
#[test]
3232
fn is_student_file() {
3333
let policy = AntStudentFilePolicy::new(Path::new(".")).unwrap();
34-
assert!(policy.is_student_file(Path::new("src/file")));
35-
assert!(policy.is_student_file(Path::new("src/dir/file")));
34+
assert!(policy.is_student_file(Path::new("src/file.java")));
35+
assert!(policy.is_student_file(Path::new("src/dir/file.java")));
3636
}
3737

3838
#[test]
3939
fn is_not_student_source_file() {
4040
let policy = AntStudentFilePolicy::new(Path::new(".")).unwrap();
41+
assert!(!policy.is_student_file(Path::new("src/file")));
4142
assert!(!policy.is_student_file(Path::new("file")));
4243
assert!(!policy.is_student_file(Path::new("dir/src/file")));
4344
assert!(!policy.is_student_file(Path::new("srca/file")));

crates/plugins/java/src/maven_plugin.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
};
77
use flate2::read::GzDecoder;
88
use std::{
9-
ffi::OsString,
9+
ffi::{OsStr, OsString},
1010
io::{Cursor, Read, Seek},
1111
ops::ControlFlow::{Break, Continue},
1212
path::{Path, PathBuf},
@@ -17,7 +17,7 @@ use tmc_langs_framework::{
1717
Archive, ExerciseDesc, Language, LanguagePlugin, RunResult, StyleValidationResult, TmcCommand,
1818
TmcError, nom::IResult, nom_language::error::VerboseError,
1919
};
20-
use tmc_langs_util::{file_util, path_util};
20+
use tmc_langs_util::file_util;
2121

2222
const MVN_ARCHIVE: &[u8] = include_bytes!("../deps/apache-maven-3.8.1-bin.tar.gz");
2323
const MVN_PATH_IN_ARCHIVE: &str = "apache-maven-3.8.1"; // the name of the base directory in the maven archive
@@ -132,14 +132,14 @@ impl LanguagePlugin for MavenPlugin {
132132
archive: &mut Archive<R>,
133133
) -> Result<PathBuf, TmcError> {
134134
let mut iter = archive.iter()?;
135+
135136
let project_dir = loop {
136137
let next = iter.with_next(|file| {
137138
let file_path = file.path()?;
138139

139-
if file.is_file() {
140-
// check for pom.xml
141-
if let Some(parent) = path_util::get_parent_of_named(&file_path, "pom.xml") {
142-
return Ok(Break(Some(parent)));
140+
if file.is_file() && file_path.file_name() == Some(OsStr::new("pom.xml")) {
141+
if let Some(pom_parent) = file_path.parent() {
142+
return Ok(Break(Some(pom_parent.to_path_buf())));
143143
}
144144
}
145145
Ok(Continue(()))

crates/plugins/java/src/maven_policy.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Maven student file policy
22
3-
use std::path::Path;
3+
use std::{ffi::OsStr, path::Path};
44
use tmc_langs_framework::{StudentFilePolicy, TmcProjectYml};
55

66
pub struct MavenStudentFilePolicy {
@@ -20,7 +20,8 @@ impl StudentFilePolicy for MavenStudentFilePolicy {
2020
}
2121

2222
fn is_non_extra_student_file(&self, path: &Path) -> bool {
23-
path.starts_with("src/main")
23+
// technically pom.xml would need to be included to differentiate between maven and ant projects
24+
path.starts_with("src/main") && path.extension() == Some(OsStr::new("java"))
2425
}
2526
}
2627

@@ -31,8 +32,8 @@ mod test {
3132
#[test]
3233
fn is_student_file() {
3334
let policy = MavenStudentFilePolicy::new(Path::new(".")).unwrap();
34-
assert!(policy.is_student_file(Path::new("src/main/file")));
35-
assert!(policy.is_student_file(Path::new("src/main/dir/file")));
35+
assert!(policy.is_student_file(Path::new("src/main/file.java")));
36+
assert!(policy.is_student_file(Path::new("src/main/dir/file.java")));
3637
}
3738

3839
#[test]

crates/plugins/make/src/policy.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Contains the language policy for the plugin.
22
3-
use std::path::Path;
3+
use std::{ffi::OsStr, path::Path};
44
use tmc_langs_framework::{StudentFilePolicy, TmcProjectYml};
55

66
pub struct MakeStudentFilePolicy {
@@ -20,7 +20,8 @@ impl StudentFilePolicy for MakeStudentFilePolicy {
2020
}
2121

2222
fn is_non_extra_student_file(&self, path: &Path) -> bool {
23-
path.starts_with("src")
23+
let ext = path.extension();
24+
path.starts_with("src") && (ext == Some(OsStr::new("c")) || ext == Some(OsStr::new("h")))
2425
}
2526
}
2627

@@ -31,14 +32,17 @@ mod test {
3132
#[test]
3233
fn is_student_file() {
3334
let policy = MakeStudentFilePolicy::new(Path::new(".")).unwrap();
34-
assert!(policy.is_student_file(Path::new("src")));
35-
assert!(policy.is_student_file(Path::new("src/file")));
36-
assert!(policy.is_student_file(Path::new("src/dir/file")));
35+
assert!(policy.is_student_file(Path::new("src/file.c")));
36+
assert!(policy.is_student_file(Path::new("src/file.h")));
37+
assert!(policy.is_student_file(Path::new("src/dir/file.c")));
38+
assert!(policy.is_student_file(Path::new("src/dir/file.h")));
3739
}
3840

3941
#[test]
4042
fn is_not_student_source_file() {
4143
let policy = MakeStudentFilePolicy::new(Path::new(".")).unwrap();
44+
assert!(!policy.is_student_file(Path::new("a.c")));
45+
assert!(!policy.is_student_file(Path::new("a.h")));
4246
assert!(!policy.is_student_file(Path::new("srcc")));
4347
assert!(!policy.is_student_file(Path::new("dir/src/file")));
4448
}

crates/plugins/make/tests/data/valgrind-failing-exercise/src/source.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <stdio.h>
22
#include "source.h"
3+
#include <stdlib.h>
34

45
int one(void)
56
{

crates/plugins/python3/src/policy.rs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,20 @@ impl StudentFilePolicy for Python3StudentFilePolicy {
2020
}
2121

2222
fn is_non_extra_student_file(&self, path: &Path) -> bool {
23-
// no files in tmc, test and venv subdirectories are considered student files
24-
let is_cache_file = path.extension() == Some(OsStr::new("pyc"))
25-
|| path
26-
.components()
27-
.any(|c| c.as_os_str() == OsStr::new("__pycache__"));
28-
let is_in_exercise_subdir = path.starts_with("test") || path.starts_with("tmc");
29-
let is_in_venv = path.starts_with(".venv") || path.starts_with("venv");
30-
if is_cache_file || is_in_exercise_subdir || is_in_venv {
23+
// never include pyc files
24+
let is_pyc = path.extension() == Some(OsStr::new("pyc"));
25+
if is_pyc {
3126
return false;
3227
}
3328

34-
// all non-pyc or __pycache__ files in src are student source files
3529
let in_src = path.starts_with("src");
36-
// .py files in exercise root are student source files
37-
let is_in_project_root = match path.parent() {
30+
let in_exercise_root = match path.parent() {
3831
Some(s) => s.as_os_str().is_empty(),
3932
None => true,
4033
};
41-
let is_py_file = path.extension() == Some(OsStr::new("py"));
34+
let is_py = path.extension() == Some(OsStr::new("py"));
4235
let is_ipynb = path.extension() == Some(OsStr::new("ipynb"));
43-
44-
// all in all, excluding cache files and the exception subdirs,
45-
// we take non-cache files in src, py files in root, everything not in the root and not in src, and all ipynb files
46-
in_src
47-
|| is_in_project_root && is_py_file
48-
|| !is_in_exercise_subdir && !is_in_project_root
49-
|| is_ipynb
36+
(in_src || in_exercise_root) && (is_py || is_ipynb)
5037
}
5138
}
5239

@@ -83,10 +70,10 @@ mod test {
8370
}
8471

8572
#[test]
86-
fn subdirs_are_student_files() {
73+
fn subdirs_are_not_student_files() {
8774
let policy = Python3StudentFilePolicy::new(Path::new(".")).unwrap();
88-
assert!(policy.is_student_file(Path::new("subdir/something")));
89-
assert!(policy.is_student_file(Path::new("another/mid/else")));
75+
assert!(!policy.is_student_file(Path::new("subdir/something")));
76+
assert!(!policy.is_student_file(Path::new("another/mid/else")));
9077
}
9178

9279
#[test]

0 commit comments

Comments
 (0)