Skip to content

Commit c52db04

Browse files
committed
remove temporary tmc json files after parsing in python plugin
1 parent 5f16418 commit c52db04

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

tmc-langs-python3/src/plugin.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@ impl LanguagePlugin for Python3Plugin {
3333
Python3StudentFilePolicy::new(project_path.to_owned())
3434
}
3535

36-
fn scan_exercise(&self, path: &Path, exercise_name: String) -> Result<ExerciseDesc, TmcError> {
37-
let run_result = run_tmc_command(path, &["available_points"], None);
36+
fn scan_exercise(
37+
&self,
38+
exercise_directory: &Path,
39+
exercise_name: String,
40+
) -> Result<ExerciseDesc, TmcError> {
41+
let run_result = run_tmc_command(exercise_directory, &["available_points"], None);
3842

3943
if let Err(error) = run_result {
4044
log::error!("Failed to scan exercise. {}", error);
4145
}
4246

43-
let test_descs = parse_exercise_description(path)?;
47+
let test_descs = parse_exercise_description(exercise_directory)?;
4448
Ok(ExerciseDesc::new(exercise_name, test_descs))
4549
}
4650

@@ -77,16 +81,22 @@ impl LanguagePlugin for Python3Plugin {
7781
setup.exists() || requirements.exists() || test.exists() || tmc.exists()
7882
}
7983

80-
fn clean(&self, path: &Path) -> Result<(), TmcError> {
81-
for entry in WalkDir::new(path).into_iter().filter_map(|e| e.ok()) {
84+
fn clean(&self, exercise_path: &Path) -> Result<(), TmcError> {
85+
for entry in WalkDir::new(exercise_path)
86+
.into_iter()
87+
.filter_map(|e| e.ok())
88+
{
8289
if entry.file_name() == ".available_points.json"
8390
|| entry.file_name() == ".tmc_test_results.json"
91+
|| entry.file_name() == "__pycache__"
8492
{
85-
fs::remove_file(entry.path())
86-
.map_err(|e| PythonError::FileRemove(entry.path().to_path_buf(), e))?;
87-
} else if entry.file_name() == "__pycache__" {
88-
fs::remove_dir_all(entry.path())
89-
.map_err(|e| PythonError::DirRemove(entry.path().to_path_buf(), e))?;
93+
if entry.path().is_file() {
94+
fs::remove_file(entry.path())
95+
.map_err(|e| PythonError::FileRemove(entry.path().to_path_buf(), e))?;
96+
} else {
97+
fs::remove_dir_all(entry.path())
98+
.map_err(|e| PythonError::DirRemove(entry.path().to_path_buf(), e))?;
99+
}
90100
}
91101
}
92102
Ok(())
@@ -127,26 +137,29 @@ fn run_tmc_command(
127137
Ok(output)
128138
}
129139

130-
fn parse_exercise_description(path: &Path) -> Result<Vec<TestDesc>, PythonError> {
140+
/// Parse and remove(!) exercise description file
141+
fn parse_exercise_description(exercise_directory: &Path) -> Result<Vec<TestDesc>, PythonError> {
131142
let mut test_descs = vec![];
132-
let mut path = path.to_owned();
133-
path.push(".available_points.json");
134-
let file = File::open(&path).map_err(|e| PythonError::FileOpen(path.clone(), e))?;
143+
let points_path = exercise_directory.join(".available_points.json");
144+
let file =
145+
File::open(&points_path).map_err(|e| PythonError::FileOpen(points_path.clone(), e))?;
135146
// TODO: deserialize directly into Vec<TestDesc>?
136147
let json: HashMap<String, Vec<String>> = serde_json::from_reader(BufReader::new(file))
137-
.map_err(|e| PythonError::Deserialize(path, e))?;
148+
.map_err(|e| PythonError::Deserialize(points_path.clone(), e))?;
138149
for (key, value) in json {
139150
test_descs.push(TestDesc::new(key, value));
140151
}
152+
fs::remove_file(&points_path).map_err(|e| PythonError::FileRemove(points_path, e))?;
141153
Ok(test_descs)
142154
}
143155

144-
fn parse_test_result(path: &Path) -> Result<RunResult, PythonError> {
145-
let mut path = path.to_owned();
146-
path.push(".tmc_test_results.json");
147-
let results_file = File::open(&path).map_err(|e| PythonError::FileOpen(path.clone(), e))?;
156+
/// Parse and remove(!) test result file
157+
fn parse_test_result(exercise_directory: &Path) -> Result<RunResult, PythonError> {
158+
let test_results_path = exercise_directory.join(".tmc_test_results.json");
159+
let results_file = File::open(&test_results_path)
160+
.map_err(|e| PythonError::FileOpen(test_results_path.clone(), e))?;
148161
let test_results: Vec<PythonTestResult> = serde_json::from_reader(BufReader::new(results_file))
149-
.map_err(|e| PythonError::Deserialize(path, e))?;
162+
.map_err(|e| PythonError::Deserialize(test_results_path.clone(), e))?;
150163
let test_results: Vec<TestResult> = test_results
151164
.into_iter()
152165
.map(PythonTestResult::into_test_result)
@@ -158,6 +171,8 @@ fn parse_test_result(path: &Path) -> Result<RunResult, PythonError> {
158171
status = RunStatus::TestsFailed;
159172
}
160173
}
174+
fs::remove_file(&test_results_path)
175+
.map_err(|e| PythonError::FileRemove(test_results_path, e))?;
161176
Ok(RunResult::new(status, test_results, HashMap::new()))
162177
}
163178

0 commit comments

Comments
 (0)