mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-20 01:24:42 +04:00
Merge pull request #686 from pre-commit/prefix_simplfiy
Simplify prefix a bit
This commit is contained in:
commit
65f60e2593
3 changed files with 15 additions and 34 deletions
|
|
@ -12,5 +12,5 @@ install_environment = helpers.no_install
|
||||||
|
|
||||||
def run_hook(prefix, hook, file_args):
|
def run_hook(prefix, hook, file_args):
|
||||||
cmd = helpers.to_cmd(hook)
|
cmd = helpers.to_cmd(hook)
|
||||||
cmd = (prefix.prefix_dir + cmd[0],) + cmd[1:]
|
cmd = (prefix.path(cmd[0]),) + cmd[1:]
|
||||||
return xargs(cmd, file_args)
|
return xargs(cmd, file_args)
|
||||||
|
|
|
||||||
|
|
@ -5,16 +5,14 @@ import os.path
|
||||||
|
|
||||||
class Prefix(object):
|
class Prefix(object):
|
||||||
def __init__(self, prefix_dir):
|
def __init__(self, prefix_dir):
|
||||||
self.prefix_dir = prefix_dir.rstrip(os.sep) + os.sep
|
self.prefix_dir = prefix_dir
|
||||||
|
|
||||||
def path(self, *parts):
|
def path(self, *parts):
|
||||||
path = os.path.join(self.prefix_dir, *parts)
|
return os.path.normpath(os.path.join(self.prefix_dir, *parts))
|
||||||
return os.path.normpath(path)
|
|
||||||
|
|
||||||
def exists(self, *parts):
|
def exists(self, *parts):
|
||||||
return os.path.exists(self.path(*parts))
|
return os.path.exists(self.path(*parts))
|
||||||
|
|
||||||
def star(self, end):
|
def star(self, end):
|
||||||
return tuple(
|
paths = os.listdir(self.prefix_dir)
|
||||||
path for path in os.listdir(self.prefix_dir) if path.endswith(end)
|
return tuple(path for path in paths if path.endswith(end))
|
||||||
)
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import os
|
import os.path
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
@ -12,30 +12,16 @@ def norm_slash(*args):
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
('input', 'expected_prefix'), (
|
('prefix', 'path_end', 'expected_output'),
|
||||||
norm_slash('.', './'),
|
(
|
||||||
norm_slash('foo', 'foo/'),
|
norm_slash('foo', '', 'foo'),
|
||||||
norm_slash('bar/', 'bar/'),
|
norm_slash('foo', 'bar', 'foo/bar'),
|
||||||
norm_slash('foo/bar', 'foo/bar/'),
|
norm_slash('foo/bar', '../baz', 'foo/baz'),
|
||||||
norm_slash('foo/bar/', 'foo/bar/'),
|
norm_slash('./', 'bar', 'bar'),
|
||||||
|
norm_slash('./', '', '.'),
|
||||||
|
norm_slash('/tmp/foo', '/tmp/bar', '/tmp/bar'),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
def test_init_normalizes_path_endings(input, expected_prefix):
|
|
||||||
instance = Prefix(input)
|
|
||||||
assert instance.prefix_dir == expected_prefix
|
|
||||||
|
|
||||||
|
|
||||||
PATH_TESTS = (
|
|
||||||
norm_slash('foo', '', 'foo'),
|
|
||||||
norm_slash('foo', 'bar', 'foo/bar'),
|
|
||||||
norm_slash('foo/bar', '../baz', 'foo/baz'),
|
|
||||||
norm_slash('./', 'bar', 'bar'),
|
|
||||||
norm_slash('./', '', '.'),
|
|
||||||
norm_slash('/tmp/foo', '/tmp/bar', '/tmp/bar'),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(('prefix', 'path_end', 'expected_output'), PATH_TESTS)
|
|
||||||
def test_path(prefix, path_end, expected_output):
|
def test_path(prefix, path_end, expected_output):
|
||||||
instance = Prefix(prefix)
|
instance = Prefix(prefix)
|
||||||
ret = instance.path(path_end)
|
ret = instance.path(path_end)
|
||||||
|
|
@ -48,10 +34,7 @@ def test_path_multiple_args():
|
||||||
assert ret == os.path.join('foo', 'bar', 'baz')
|
assert ret == os.path.join('foo', 'bar', 'baz')
|
||||||
|
|
||||||
|
|
||||||
def test_exists_does_not_exist(tmpdir):
|
def test_exists(tmpdir):
|
||||||
assert not Prefix(str(tmpdir)).exists('foo')
|
assert not Prefix(str(tmpdir)).exists('foo')
|
||||||
|
|
||||||
|
|
||||||
def test_exists_does_exist(tmpdir):
|
|
||||||
tmpdir.ensure('foo')
|
tmpdir.ensure('foo')
|
||||||
assert Prefix(str(tmpdir)).exists('foo')
|
assert Prefix(str(tmpdir)).exists('foo')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue