diff --git a/lintly/cli.py b/lintly/cli.py index c47716b..e50ced2 100644 --- a/lintly/cli.py +++ b/lintly/cli.py @@ -62,6 +62,9 @@ @click.option('--comment-tag', default='', help='A tag used to identify comments from a previous run that should be deleted') +@click.option('--base-dir', + default='.', + help='The base git directory') @click.option('--exit-zero/--no-exit-zero', default=False, help=('Whether Lintly should exit with error code indicating ' 'amount of violations or not. Default false')) diff --git a/lintly/config.py b/lintly/config.py index daf77c9..174646c 100644 --- a/lintly/config.py +++ b/lintly/config.py @@ -8,11 +8,13 @@ class Config(object): """A Config object that knows how to return configuration from the CLI or Continuous Integration services""" LINTLY_IDENTIFIER = '' + BASE_DIR = '.' def __init__(self, cli_config): self.cli_config = cli_config if self.comment_tag: type(self).LINTLY_IDENTIFIER = '(%s)%s' % (self.comment_tag, type(self).LINTLY_IDENTIFIER) + type(self).BASE_DIR = self.base_dir def as_dict(self): return { @@ -27,6 +29,7 @@ def as_dict(self): 'github_check_run_id': self.github_check_run_id, 'review_body': self.review_body, 'comment_tag': self.comment_tag, + 'base_dir': self.base_dir, } @property @@ -73,6 +76,10 @@ def review_body(self): def comment_tag(self): return self.cli_config['comment_tag'] + @property + def base_dir(self): + return self.cli_config['base_dir'] + @property def github_check_run_id(self): """The Check Run ID from GitHub Actions. diff --git a/lintly/parsers.py b/lintly/parsers.py index e474b1e..3ba3269 100644 --- a/lintly/parsers.py +++ b/lintly/parsers.py @@ -8,6 +8,7 @@ import re from .violations import Violation +from .config import Config class BaseLintParser(object): @@ -15,15 +16,12 @@ class BaseLintParser(object): def parse_violations(self, output): raise NotImplementedError - def _get_working_dir(self): - return os.getcwd() - def _normalize_path(self, path): """ Normalizes a file path so that it returns a path relative to the root repo directory. """ norm_path = os.path.normpath(path) - rel_path = os.path.relpath(norm_path, start=self._get_working_dir()) + rel_path = os.path.relpath(norm_path, start=Config.BASE_DIR) if os.name == 'nt': rel_path = rel_path.replace('\\', '/') return rel_path diff --git a/tests/test_parsers.py b/tests/test_parsers.py index 3e7a479..0a54400 100644 --- a/tests/test_parsers.py +++ b/tests/test_parsers.py @@ -2,12 +2,8 @@ import os import unittest -try: - from unittest.mock import patch -except ImportError: - from mock import patch - from lintly.parsers import PARSERS +from lintly.config import Config class ParserTestCaseMixin(object): @@ -137,8 +133,8 @@ class ESLintParserTestCase(ParserTestCaseMixin, unittest.TestCase): ] } - @patch('lintly.parsers.ESLintParser._get_working_dir', return_value='/Users/grant/project') - def test_parse_violations(self, _get_working_dir_mock): + def test_parse_violations(self): + Config.BASE_DIR = '/Users/grant/project' super(ESLintParserTestCase, self).test_parse_violations() @@ -168,8 +164,8 @@ class BlackParserTestCase(ParserTestCaseMixin, unittest.TestCase): for file_path in ['lintly/violations.py', 'lintly/parsers.py'] } - @patch('lintly.parsers.BlackParser._get_working_dir', return_value='/Users/jouyuy/Dev/workspace/Lintly') - def test_parse_violations(self, _get_working_dir_mock): + def test_parse_violations(self): + Config.BASE_DIR = '/Users/jouyuy/Dev/workspace/Lintly' super(BlackParserTestCase, self).test_parse_violations()