Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
Support custom base dir
Browse files Browse the repository at this point in the history
  • Loading branch information
bugale committed Jul 11, 2022
1 parent 192a716 commit b0a92c2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
3 changes: 3 additions & 0 deletions lintly/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
7 changes: 7 additions & 0 deletions lintly/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<!-- Automatically posted by Lintly -->'
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 {
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 2 additions & 4 deletions lintly/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@
import re

from .violations import Violation
from .config import Config


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
Expand Down
14 changes: 5 additions & 9 deletions tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()


Expand Down Expand Up @@ -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()


Expand Down

0 comments on commit b0a92c2

Please sign in to comment.