Skip to content

Commit 8fb644e

Browse files
committed
Simplify prefix a bit
1 parent 06ee69b commit 8fb644e

File tree

3 files changed

+15
-34
lines changed

3 files changed

+15
-34
lines changed

pre_commit/languages/script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
def run_hook(prefix, hook, file_args):
1414
cmd = helpers.to_cmd(hook)
15-
cmd = (prefix.prefix_dir + cmd[0],) + cmd[1:]
15+
cmd = (prefix.path(cmd[0]),) + cmd[1:]
1616
return xargs(cmd, file_args)

pre_commit/prefix.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55

66
class Prefix(object):
77
def __init__(self, prefix_dir):
8-
self.prefix_dir = prefix_dir.rstrip(os.sep) + os.sep
8+
self.prefix_dir = prefix_dir
99

1010
def path(self, *parts):
11-
path = os.path.join(self.prefix_dir, *parts)
12-
return os.path.normpath(path)
11+
return os.path.normpath(os.path.join(self.prefix_dir, *parts))
1312

1413
def exists(self, *parts):
1514
return os.path.exists(self.path(*parts))
1615

1716
def star(self, end):
18-
return tuple(
19-
path for path in os.listdir(self.prefix_dir) if path.endswith(end)
20-
)
17+
paths = os.listdir(self.prefix_dir)
18+
return tuple(path for path in paths if path.endswith(end))

tests/prefix_test.py

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import unicode_literals
22

3-
import os
3+
import os.path
44

55
import pytest
66

@@ -12,30 +12,16 @@ def norm_slash(*args):
1212

1313

1414
@pytest.mark.parametrize(
15-
('input', 'expected_prefix'), (
16-
norm_slash('.', './'),
17-
norm_slash('foo', 'foo/'),
18-
norm_slash('bar/', 'bar/'),
19-
norm_slash('foo/bar', 'foo/bar/'),
20-
norm_slash('foo/bar/', 'foo/bar/'),
15+
('prefix', 'path_end', 'expected_output'),
16+
(
17+
norm_slash('foo', '', 'foo'),
18+
norm_slash('foo', 'bar', 'foo/bar'),
19+
norm_slash('foo/bar', '../baz', 'foo/baz'),
20+
norm_slash('./', 'bar', 'bar'),
21+
norm_slash('./', '', '.'),
22+
norm_slash('/tmp/foo', '/tmp/bar', '/tmp/bar'),
2123
),
2224
)
23-
def test_init_normalizes_path_endings(input, expected_prefix):
24-
instance = Prefix(input)
25-
assert instance.prefix_dir == expected_prefix
26-
27-
28-
PATH_TESTS = (
29-
norm_slash('foo', '', 'foo'),
30-
norm_slash('foo', 'bar', 'foo/bar'),
31-
norm_slash('foo/bar', '../baz', 'foo/baz'),
32-
norm_slash('./', 'bar', 'bar'),
33-
norm_slash('./', '', '.'),
34-
norm_slash('/tmp/foo', '/tmp/bar', '/tmp/bar'),
35-
)
36-
37-
38-
@pytest.mark.parametrize(('prefix', 'path_end', 'expected_output'), PATH_TESTS)
3925
def test_path(prefix, path_end, expected_output):
4026
instance = Prefix(prefix)
4127
ret = instance.path(path_end)
@@ -48,10 +34,7 @@ def test_path_multiple_args():
4834
assert ret == os.path.join('foo', 'bar', 'baz')
4935

5036

51-
def test_exists_does_not_exist(tmpdir):
37+
def test_exists(tmpdir):
5238
assert not Prefix(str(tmpdir)).exists('foo')
53-
54-
55-
def test_exists_does_exist(tmpdir):
5639
tmpdir.ensure('foo')
5740
assert Prefix(str(tmpdir)).exists('foo')

0 commit comments

Comments
 (0)