Skip to content

Commit 45d39fb

Browse files
committed
Fix path pattern construction on Windows
1 parent dee7e14 commit 45d39fb

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

nbgrader/coursedir.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import re
3+
import sys
34
import datetime
45
import typing
56
from pathlib import Path
@@ -367,6 +368,10 @@ def find_assignments(self,
367368
# Convert to a Path and back to a string to remove any instances of `/.`
368369
pattern = str(Path(self.directory_structure.format(**pattern_args)))
369370

371+
if sys.platform == 'win32':
372+
# Escape backslashes on Windows
373+
pattern = pattern.replace('\\', r'\\')
374+
370375
for dir in dirs:
371376
match = re.match(pattern, str(dir.relative_to(self.root)))
372377
if match:

nbgrader/tests/test_coursedir.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import pytest
22
import os
3+
import re
4+
from pathlib import Path
35

46
from traitlets.config import Config
57

@@ -24,7 +26,8 @@ def test_coursedir_format_path(conf):
2426
expected = os.path.join(coursedir.root, "step", "student_id", "assignment_id")
2527
assert coursedir.format_path("step", "student_id", "assignment_id") == expected
2628

27-
expected = os.path.join(coursedir.root.replace("-", r"\-"), "step", "student_id", "assignment_id")
29+
escaped = Path(re.escape(coursedir.root))
30+
expected = str(escaped / "step" / "student_id" / "assignment_id")
2831
assert coursedir.format_path("step", "student_id", "assignment_id", escape=True) == expected
2932

3033

@@ -35,5 +38,6 @@ def test_coursedir_format_path_with_specials(conf):
3538
expected = os.path.join("/[test] root", "step", "student_id", "assignment_id")
3639
assert coursedir.format_path("step", "student_id", "assignment_id") == expected
3740

38-
expected = os.path.join(r"/\[test\]\ root", "step", "student_id", "assignment_id")
41+
escaped = Path(re.escape(coursedir.root))
42+
expected = str(escaped / "step" / "student_id" / "assignment_id")
3943
assert coursedir.format_path("step", "student_id", "assignment_id", escape=True) == expected

0 commit comments

Comments
 (0)