Skip to content

Commit 707407d

Browse files
committed
Normalize paths on windows to forward slashes
1 parent 4bd6529 commit 707407d

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pre_commit/commands/run.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ def filter_by_include_exclude(names, include, exclude):
3434

3535
class Classifier(object):
3636
def __init__(self, filenames):
37+
# on windows we normalize all filenames to use forward slashes
38+
# this makes it easier to filter using the `files:` regex
39+
# this also makes improperly quoted shell-based hooks work better
40+
# see #1173
41+
if os.altsep == '/' and os.sep == '\\':
42+
filenames = (f.replace(os.sep, os.altsep) for f in filenames)
3743
self.filenames = [f for f in filenames if os.path.lexists(f)]
3844
self._types_cache = {}
3945

tests/commands/run_test.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import subprocess
88
import sys
99

10+
import mock
1011
import pytest
1112

1213
import pre_commit.constants as C
@@ -782,7 +783,7 @@ def test_files_running_subdir(repo_with_passing_hook, tempdir_factory):
782783
'--files', 'foo.py',
783784
tempdir_factory=tempdir_factory,
784785
)
785-
assert 'subdir/foo.py'.replace('/', os.sep) in stdout
786+
assert 'subdir/foo.py' in stdout
786787

787788

788789
@pytest.mark.parametrize(
@@ -826,6 +827,23 @@ def test_classifier_removes_dne():
826827
assert classifier.filenames == []
827828

828829

830+
def test_classifier_normalizes_filenames_on_windows_to_forward_slashes(tmpdir):
831+
with tmpdir.as_cwd():
832+
tmpdir.join('a/b/c').ensure()
833+
with mock.patch.object(os, 'altsep', '/'):
834+
with mock.patch.object(os, 'sep', '\\'):
835+
classifier = Classifier((r'a\b\c',))
836+
assert classifier.filenames == ['a/b/c']
837+
838+
839+
def test_classifier_does_not_normalize_backslashes_non_windows(tmpdir):
840+
with mock.patch.object(os.path, 'lexists', return_value=True):
841+
with mock.patch.object(os, 'altsep', None):
842+
with mock.patch.object(os, 'sep', '/'):
843+
classifier = Classifier((r'a/b\c',))
844+
assert classifier.filenames == [r'a/b\c']
845+
846+
829847
@pytest.fixture
830848
def some_filenames():
831849
return (

0 commit comments

Comments
 (0)