@@ -20,24 +20,25 @@ def test_coursedir_configurable(conf, course_dir):
2020 assert coursedir .root == course_dir
2121
2222
23- def test_coursedir_format_path (conf ):
23+ @pytest .mark .parametrize ("root" , [None , os .path .sep + "[special]~root" ])
24+ def test_coursedir_format_path (conf , root ):
25+ if root is not None :
26+ conf .CourseDirectory .root = root
2427 coursedir = CourseDirectory (config = conf )
2528
26- expected = os .path .join (coursedir .root , "step" , "student_id" , "assignment_id" )
27- assert coursedir .format_path ("step" , "student_id" , "assignment_id" ) == expected
29+ # The default includes the un-escaped root
30+ path = os .path .join (coursedir .root , "step" , "student_id" , "assignment1" )
31+ assert coursedir .format_path ("step" , "student_id" , "assignment1" ) == path
2832
33+ # The escape=True option escapes the root and path separators
2934 escaped = Path (re .escape (coursedir .root ))
30- expected = str ( escaped / "step" / "student_id" / "assignment_id" )
31- assert coursedir . format_path ( "step" , "student_id" , " assignment_id" , escape = True ) == expected
35+ expected = escaped . anchor + re . escape ( os . path . sep ). join (
36+ ( escaped / "step" / "student_id" / "(?P< assignment_id>.*)" ). parts [ 1 :])
3237
38+ actual = coursedir .format_path ("step" , "student_id" , "(?P<assignment_id>.*)" , escape = True )
39+ assert actual == expected
3340
34- def test_coursedir_format_path_with_specials (conf ):
35- conf .CourseDirectory .root = "/[test] root"
36- coursedir = CourseDirectory (config = conf )
37-
38- expected = os .path .join ("/[test] root" , "step" , "student_id" , "assignment_id" )
39- assert coursedir .format_path ("step" , "student_id" , "assignment_id" ) == expected
40-
41- escaped = Path (re .escape (coursedir .root ))
42- expected = str (escaped / "step" / "student_id" / "assignment_id" )
43- assert coursedir .format_path ("step" , "student_id" , "assignment_id" , escape = True ) == expected
41+ # The escape=True option produces a regex pattern which can match paths
42+ match = re .match (actual , path )
43+ assert match is not None
44+ assert match .group ("assignment_id" ) == "assignment1"
0 commit comments