Skip to content

Commit fed2a42

Browse files
committed
comments, renamed functions
1 parent a560e63 commit fed2a42

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

tmc-langs-util/src/task_executor/course_refresher.rs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ impl CourseRefresher {
148148
}
149149

150150
log::info!("updating course options");
151-
let course_options = update_course_options(&course.clone_path, &course.name)?;
151+
let course_options = get_course_options(&course.clone_path, &course.name)?;
152152
self.progress_reporter
153153
.finish_step("Updated course options".to_string(), None)?;
154154

155155
// add_records_for_new_exercises & delete_records_for_removed_exercises
156156
log::info!("updating exercises");
157157
let (new_exercises, removed_exercises) =
158-
update_exercises(&course.clone_path, &course.exercises)?;
158+
get_exercise_changes(&course.clone_path, &course.exercises)?;
159159
self.progress_reporter
160160
.finish_step("Updated exercises".to_string(), None)?;
161161
log::info!("updating exercise options");
@@ -274,13 +274,22 @@ pub fn refresh_course(
274274
)
275275
}
276276

277+
/// Checks old_cache_path/clone for a git repo.
278+
/// If found, copies it to course_clone_path fetches origin from course_source_url, checks out origin/course_git_branch, cleans and checks out the repo.
279+
/// If not found or found but one of the git commands causes an error, deletes course_clone_path and clones course_git_branch from course_source_url there.
280+
/// NOP during testing.
277281
fn update_or_clone_repository(
278282
course_source_backend: &SourceBackend,
279283
course_clone_path: &Path,
280284
course_source_url: &str,
281285
course_git_branch: &str,
282286
old_cache_path: &Path,
283287
) -> Result<(), UtilError> {
288+
// NOP during tests
289+
if cfg!(test) {
290+
return Ok(());
291+
}
292+
284293
if course_source_backend != &SourceBackend::Git {
285294
log::error!("Source types other than git not yet implemented");
286295
return Err(UtilError::UnsupportedSourceBackend);
@@ -341,6 +350,8 @@ fn update_or_clone_repository(
341350
Ok(())
342351
}
343352

353+
/// Makes sure no directory directly under path is an exercise directory containing a dash in the relative path from path to the dir.
354+
/// (For some reason)
344355
fn check_directory_names(path: &Path) -> Result<(), UtilError> {
345356
// exercise directories in canonicalized form
346357
let exercise_dirs = {
@@ -372,23 +383,23 @@ fn check_directory_names(path: &Path) -> Result<(), UtilError> {
372383
Ok(())
373384
}
374385

375-
fn update_course_options(
376-
course_clone_path: &Path,
377-
course_name: &str,
378-
) -> Result<Mapping, UtilError> {
386+
/// Checks for a course_clone_path/course_options.yml
387+
/// If found, course-specific options are merged into it and it is returned.
388+
/// Else, an empty mapping is returned.
389+
fn get_course_options(course_clone_path: &Path, course_name: &str) -> Result<Mapping, UtilError> {
379390
let options_file = course_clone_path.join("course_options.yml");
380-
let opts = if options_file.exists() {
391+
if options_file.exists() {
381392
let file = file_util::open_file(options_file)?;
382393
let mut course_options: Mapping = serde_yaml::from_reader(file).unwrap();
383-
merge_course_specific_options(course_name, &mut course_options);
384-
course_options
394+
merge_course_options(course_name, &mut course_options);
395+
Ok(course_options)
385396
} else {
386-
Mapping::new()
387-
};
388-
Ok(opts)
397+
Ok(Mapping::new())
398+
}
389399
}
390400

391-
fn merge_course_specific_options(course_name: &str, opts: &mut Mapping) {
401+
/// Checks for a courses: course_name map in opts and merges that map with opts if it exists.
402+
fn merge_course_options(course_name: &str, opts: &mut Mapping) {
392403
// try to remove the "courses" map
393404
if let Some(serde_yaml::Value::Mapping(mut courses)) =
394405
opts.remove(&serde_yaml::Value::String("courses".to_string()))
@@ -405,7 +416,10 @@ fn merge_course_specific_options(course_name: &str, opts: &mut Mapping) {
405416
}
406417
}
407418

408-
fn update_exercises(
419+
/// Finds exercise directories, and converts the directories to "exercise names" by swapping the separators for dashes.
420+
/// Adds exercise names not in course_exercises to new exercises
421+
/// Adds exercise names in course_exercises but not in exercise names to removed exercises
422+
fn get_exercise_changes(
409423
course_clone_path: &Path,
410424
course_exercises: &[RefreshExercise],
411425
) -> Result<(Vec<String>, Vec<String>), UtilError> {
@@ -480,7 +494,7 @@ fn update_exercise_options(
480494
if metadata_path.exists() {
481495
let file = file_util::open_file(metadata_path)?;
482496
if let Ok(mut yaml) = serde_yaml::from_reader::<_, Mapping>(file) {
483-
merge_course_specific_options(course_name, &mut yaml);
497+
merge_course_options(course_name, &mut yaml);
484498
recursive_merge(yaml, &mut metadata);
485499
}
486500
}
@@ -815,7 +829,7 @@ mod test {
815829

816830
let clone = tempfile::TempDir::new().unwrap();
817831
let name = "name";
818-
let options = update_course_options(clone.path(), name).unwrap();
832+
let options = get_course_options(clone.path(), name).unwrap();
819833
assert!(options.is_empty());
820834
}
821835

@@ -825,7 +839,7 @@ mod test {
825839

826840
let clone_path = Path::new("tests/data/course_refresher");
827841
let name = "course-name";
828-
let options = update_course_options(clone_path, name).unwrap();
842+
let options = get_course_options(clone_path, name).unwrap();
829843
assert!(!options.is_empty());
830844

831845
let b = options
@@ -853,12 +867,12 @@ mod test {
853867
init();
854868

855869
let clone_path = Path::new("tests/data/course_refresher/empty");
856-
let (new, removed) = update_exercises(clone_path, &[]).unwrap();
870+
let (new, removed) = get_exercise_changes(clone_path, &[]).unwrap();
857871
assert!(new.is_empty());
858872
assert!(removed.is_empty());
859873

860874
let clone_path = Path::new("tests/data/course_refresher/valid_exercises");
861-
let (new, removed) = update_exercises(
875+
let (new, removed) = get_exercise_changes(
862876
clone_path,
863877
&[
864878
RefreshExercise {

0 commit comments

Comments
 (0)