mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-08 22:04:17 +00:00
Merge branch 'config-appdirs' into 'master'
config: Determine path to user configuration immediately See merge request pycqa/flake8!409
This commit is contained in:
commit
9ed2d8d742
3 changed files with 24 additions and 38 deletions
|
|
@ -3,7 +3,6 @@ import collections
|
||||||
import configparser
|
import configparser
|
||||||
import logging
|
import logging
|
||||||
import os.path
|
import os.path
|
||||||
import sys
|
|
||||||
from typing import List, Optional, Tuple
|
from typing import List, Optional, Tuple
|
||||||
|
|
||||||
from flake8 import utils
|
from flake8 import utils
|
||||||
|
|
@ -46,21 +45,29 @@ class ConfigFileFinder(object):
|
||||||
# The value of --isolated from the CLI.
|
# The value of --isolated from the CLI.
|
||||||
self.ignore_config_files = ignore_config_files
|
self.ignore_config_files = ignore_config_files
|
||||||
|
|
||||||
# Platform specific settings
|
# User configuration file.
|
||||||
self.is_windows = sys.platform == "win32"
|
|
||||||
self.xdg_home = os.environ.get(
|
|
||||||
"XDG_CONFIG_HOME", os.path.expanduser("~/.config")
|
|
||||||
)
|
|
||||||
|
|
||||||
# Look for '.<program_name>' files
|
|
||||||
self.program_config = "." + program_name
|
|
||||||
self.program_name = program_name
|
self.program_name = program_name
|
||||||
|
self.user_config_file = self._user_config_file(program_name)
|
||||||
|
|
||||||
# List of filenames to find in the local/project directory
|
# List of filenames to find in the local/project directory
|
||||||
self.project_filenames = ("setup.cfg", "tox.ini", self.program_config)
|
self.project_filenames = ("setup.cfg", "tox.ini", "." + program_name)
|
||||||
|
|
||||||
self.local_directory = os.path.abspath(os.curdir)
|
self.local_directory = os.path.abspath(os.curdir)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _user_config_file(program_name):
|
||||||
|
# type: (str) -> str
|
||||||
|
if utils.is_windows():
|
||||||
|
home_dir = os.path.expanduser("~")
|
||||||
|
config_file_basename = "." + program_name
|
||||||
|
else:
|
||||||
|
home_dir = os.environ.get(
|
||||||
|
"XDG_CONFIG_HOME", os.path.expanduser("~/.config")
|
||||||
|
)
|
||||||
|
config_file_basename = program_name
|
||||||
|
|
||||||
|
return os.path.join(home_dir, config_file_basename)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _read_config(*files):
|
def _read_config(*files):
|
||||||
# type: (*str) -> Tuple[configparser.RawConfigParser, List[str]]
|
# type: (*str) -> Tuple[configparser.RawConfigParser, List[str]]
|
||||||
|
|
@ -139,15 +146,9 @@ class ConfigFileFinder(object):
|
||||||
"""Parse all local config files into one config object."""
|
"""Parse all local config files into one config object."""
|
||||||
return self.local_configs_with_files()[0]
|
return self.local_configs_with_files()[0]
|
||||||
|
|
||||||
def user_config_file(self):
|
|
||||||
"""Find the user-level config file."""
|
|
||||||
if self.is_windows:
|
|
||||||
return os.path.expanduser("~\\" + self.program_config)
|
|
||||||
return os.path.join(self.xdg_home, self.program_name)
|
|
||||||
|
|
||||||
def user_config(self):
|
def user_config(self):
|
||||||
"""Parse the user config file into a config object."""
|
"""Parse the user config file into a config object."""
|
||||||
config, found_files = self._read_config(self.user_config_file())
|
config, found_files = self._read_config(self.user_config_file)
|
||||||
if found_files:
|
if found_files:
|
||||||
LOG.debug("Found user configuration files: %s", found_files)
|
LOG.debug("Found user configuration files: %s", found_files)
|
||||||
return config
|
return config
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
"""Tests for the ConfigFileFinder."""
|
"""Tests for the ConfigFileFinder."""
|
||||||
import configparser
|
import configparser
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
import pytest
|
import pytest
|
||||||
|
|
@ -13,18 +12,6 @@ CLI_SPECIFIED_FILEPATH = 'tests/fixtures/config_files/cli-specified.ini'
|
||||||
BROKEN_CONFIG_PATH = 'tests/fixtures/config_files/broken.ini'
|
BROKEN_CONFIG_PATH = 'tests/fixtures/config_files/broken.ini'
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('platform,is_windows', [
|
|
||||||
('win32', True),
|
|
||||||
('linux', False),
|
|
||||||
('darwin', False),
|
|
||||||
])
|
|
||||||
def test_windows_detection(platform, is_windows):
|
|
||||||
"""Verify we detect Windows to the best of our knowledge."""
|
|
||||||
with mock.patch.object(sys, 'platform', platform):
|
|
||||||
finder = config.ConfigFileFinder('flake8')
|
|
||||||
assert finder.is_windows is is_windows
|
|
||||||
|
|
||||||
|
|
||||||
def test_cli_config():
|
def test_cli_config():
|
||||||
"""Verify opening and reading the file specified via the cli."""
|
"""Verify opening and reading the file specified via the cli."""
|
||||||
cli_filepath = CLI_SPECIFIED_FILEPATH
|
cli_filepath = CLI_SPECIFIED_FILEPATH
|
||||||
|
|
|
||||||
|
|
@ -69,9 +69,9 @@ def test_parse_user_config(optmanager, config_finder):
|
||||||
action='count')
|
action='count')
|
||||||
parser = config.MergedConfigParser(optmanager, config_finder)
|
parser = config.MergedConfigParser(optmanager, config_finder)
|
||||||
|
|
||||||
with mock.patch.object(parser.config_finder, 'user_config_file') as usercf:
|
config_finder.user_config_file = ('tests/fixtures/config_files/'
|
||||||
usercf.return_value = 'tests/fixtures/config_files/cli-specified.ini'
|
'cli-specified.ini')
|
||||||
parsed_config = parser.parse_user_config()
|
parsed_config = parser.parse_user_config()
|
||||||
|
|
||||||
assert parsed_config == {
|
assert parsed_config == {
|
||||||
'ignore': ['E123', 'W234', 'E111'],
|
'ignore': ['E123', 'W234', 'E111'],
|
||||||
|
|
@ -127,11 +127,9 @@ def test_merge_user_and_local_config(optmanager, config_finder):
|
||||||
localcfs.return_value = [
|
localcfs.return_value = [
|
||||||
'tests/fixtures/config_files/local-config.ini'
|
'tests/fixtures/config_files/local-config.ini'
|
||||||
]
|
]
|
||||||
with mock.patch.object(config_finder,
|
config_finder.user_config_file = ('tests/fixtures/config_files/'
|
||||||
'user_config_file') as usercf:
|
'user-config.ini')
|
||||||
usercf.return_value = ('tests/fixtures/config_files/'
|
parsed_config = parser.merge_user_and_local_config()
|
||||||
'user-config.ini')
|
|
||||||
parsed_config = parser.merge_user_and_local_config()
|
|
||||||
|
|
||||||
assert parsed_config == {
|
assert parsed_config == {
|
||||||
'exclude': [
|
'exclude': [
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue