mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-13 12:30:08 -06:00
Simplify prefix a bit
This commit is contained in:
@@ -12,5 +12,5 @@ install_environment = helpers.no_install
|
||||
|
||||
def run_hook(prefix, hook, file_args):
|
||||
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)
|
||||
|
||||
@@ -5,16 +5,14 @@ import os.path
|
||||
|
||||
class Prefix(object):
|
||||
def __init__(self, prefix_dir):
|
||||
self.prefix_dir = prefix_dir.rstrip(os.sep) + os.sep
|
||||
self.prefix_dir = prefix_dir
|
||||
|
||||
def path(self, *parts):
|
||||
path = os.path.join(self.prefix_dir, *parts)
|
||||
return os.path.normpath(path)
|
||||
return os.path.normpath(os.path.join(self.prefix_dir, *parts))
|
||||
|
||||
def exists(self, *parts):
|
||||
return os.path.exists(self.path(*parts))
|
||||
|
||||
def star(self, end):
|
||||
return tuple(
|
||||
path for path in os.listdir(self.prefix_dir) if path.endswith(end)
|
||||
)
|
||||
paths = os.listdir(self.prefix_dir)
|
||||
return tuple(path for path in paths if path.endswith(end))
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import os
|
||||
import os.path
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -12,30 +12,16 @@ def norm_slash(*args):
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
('input', 'expected_prefix'), (
|
||||
norm_slash('.', './'),
|
||||
norm_slash('foo', 'foo/'),
|
||||
norm_slash('bar/', 'bar/'),
|
||||
norm_slash('foo/bar', 'foo/bar/'),
|
||||
norm_slash('foo/bar/', 'foo/bar/'),
|
||||
('prefix', 'path_end', 'expected_output'),
|
||||
(
|
||||
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'),
|
||||
),
|
||||
)
|
||||
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):
|
||||
instance = Prefix(prefix)
|
||||
ret = instance.path(path_end)
|
||||
@@ -48,10 +34,7 @@ def test_path_multiple_args():
|
||||
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')
|
||||
|
||||
|
||||
def test_exists_does_exist(tmpdir):
|
||||
tmpdir.ensure('foo')
|
||||
assert Prefix(str(tmpdir)).exists('foo')
|
||||
|
||||
Reference in New Issue
Block a user