Skip to content

Commit 5f16418

Browse files
committed
set __pycache__ and .pyc files to not be student source files
1 parent cd10286 commit 5f16418

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tmc-langs-python3/src/policy.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Contains the Python3 student file policy
22
3+
use std::ffi::OsStr;
34
use std::path::{Path, PathBuf};
45
use tmc_langs_framework::policy::StudentFilePolicy;
56

@@ -22,5 +23,35 @@ impl StudentFilePolicy for Python3StudentFilePolicy {
2223

2324
fn is_student_source_file(&self, path: &Path) -> bool {
2425
path.starts_with("src")
26+
&& path.extension() != Some(OsStr::new("pyc"))
27+
&& !path
28+
.components()
29+
.any(|c| c.as_os_str() == OsStr::new("__pycache__"))
30+
}
31+
}
32+
33+
#[cfg(test)]
34+
mod test {
35+
use super::*;
36+
37+
#[test]
38+
fn in_src_is_source_file() {
39+
let policy = Python3StudentFilePolicy::new(PathBuf::from(""));
40+
assert!(policy.is_student_source_file(Path::new("src/some_file.py")));
41+
}
42+
43+
#[test]
44+
fn pycache_is_not_source_file() {
45+
let policy = Python3StudentFilePolicy::new(PathBuf::from(""));
46+
assert!(!policy.is_student_source_file(Path::new("__pycache__")));
47+
assert!(!policy.is_student_source_file(Path::new("__pycache__/cachefile")));
48+
assert!(!policy.is_student_source_file(Path::new("src/__pycache__")));
49+
}
50+
51+
#[test]
52+
fn pyc_is_not_source_file() {
53+
let policy = Python3StudentFilePolicy::new(PathBuf::from(""));
54+
assert!(!policy.is_student_source_file(Path::new("some.pyc")));
55+
assert!(!policy.is_student_source_file(Path::new("src/other.pyc")));
2556
}
2657
}

0 commit comments

Comments
 (0)