Skip to content

Commit 48f9778

Browse files
committed
Simplify student file policies
1 parent 3275148 commit 48f9778

File tree

73 files changed

+598
-391
lines changed

Some content is hidden

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

73 files changed

+598
-391
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: 3 additions & 23 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

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: 8 additions & 5 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},
@@ -132,14 +132,17 @@ 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.extension() == Some(OsStr::new("java")) {
141+
// check if java file has src as ancestor
142+
for ancestor in file_path.ancestors() {
143+
if let Some(src_parent) = path_util::get_parent_of_named(ancestor, "src") {
144+
return Ok(Break(Some(src_parent)));
145+
}
143146
}
144147
}
145148
Ok(Continue(()))

crates/plugins/java/src/maven_policy.rs

Lines changed: 2 additions & 2 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,7 @@ impl StudentFilePolicy for MavenStudentFilePolicy {
2020
}
2121

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

crates/plugins/make/src/policy.rs

Lines changed: 3 additions & 2 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

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]

crates/plugins/r/src/policy.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Contains the R 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 RStudentFilePolicy {
@@ -20,7 +20,7 @@ impl StudentFilePolicy for RStudentFilePolicy {
2020
}
2121

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

@@ -40,7 +40,7 @@ mod test {
4040

4141
let policy = RStudentFilePolicy::new(Path::new(".")).unwrap();
4242
assert!(policy.is_student_file(Path::new("R")));
43-
assert!(policy.is_student_file(Path::new("R/file")));
43+
assert!(policy.is_student_file(Path::new("R/file.R")));
4444
}
4545

4646
#[test]

0 commit comments

Comments
 (0)