From 8fb644e7c0d738fcd09cc1a61d035c888bc40005 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 13 Jan 2018 15:53:22 -0800 Subject: [PATCH] Simplify prefix a bit --- pre_commit/languages/script.py | 2 +- pre_commit/prefix.py | 10 ++++----- tests/prefix_test.py | 37 +++++++++------------------------- 3 files changed, 15 insertions(+), 34 deletions(-) diff --git a/pre_commit/languages/script.py b/pre_commit/languages/script.py index 8186e77a..551b4d80 100644 --- a/pre_commit/languages/script.py +++ b/pre_commit/languages/script.py @@ -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) diff --git a/pre_commit/prefix.py b/pre_commit/prefix.py index 128bd861..073b3f54 100644 --- a/pre_commit/prefix.py +++ b/pre_commit/prefix.py @@ -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)) diff --git a/tests/prefix_test.py b/tests/prefix_test.py index 05f3f8a4..728b5df4 100644 --- a/tests/prefix_test.py +++ b/tests/prefix_test.py @@ -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')