Skip to content

Commit

Permalink
Revert "feat: add support for pyproject.toml configuration (#41)"
Browse files Browse the repository at this point in the history
This reverts commit aff681b.
  • Loading branch information
rubik committed Oct 21, 2024
1 parent f3d42f7 commit 2352b97
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 476 deletions.
305 changes: 0 additions & 305 deletions test_xenon.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import os
import sys
import unittest
import unittest.mock
import collections
import tempfile
import sys
import argparse

# Monkey-patch paramunittest for Python 3.10+
if sys.version_info[:2] >= (3, 10):
Expand All @@ -17,7 +13,6 @@
import httpretty
from paramunittest import parametrized

import xenon
from xenon import core, api, main


Expand Down Expand Up @@ -193,305 +188,5 @@ def test_api(self):
'message': 'Resource creation started'})


class TestCustomConfigParserGetStr(unittest.TestCase):
'''Test class for class CustomConfigParser - getstr method.'''

@staticmethod
def get_configuration(text):
'''Get CustomConfigParser object with loaded text.'''
with tempfile.TemporaryDirectory() as tmp_dir:
pyproject_path = tmp_dir + '/pyproject.toml'
with open(pyproject_path, "w") as toml_file:
toml_file.write(text)

configuration = xenon.CustomConfigParser()
configuration.read(pyproject_path)

return configuration

def test_missing_section_case(self):
'''Test missing section case.'''
configuration = self.get_configuration(
'[tool.xenon]\n'
'path = ["path_1", "path_2", "path_3"]\n'
'max_average = "A"\n'
'max_average_num = 1.2\n')

self.assertEqual(
configuration.getstr(xenon.PYPROJECT_SECTION, "maxaverage", "None"), "None")

def test_with_trim_value_case(self):
'''Test with trim value case.'''
configuration = self.get_configuration(
'[tool.xenon]\n'
'path = ["path_1", "path_2", "path_3"]\n'
'max_average = "A"\n'
'max_modules = \'B\'\n'
'max_average_num = 1.2\n')

self.assertEqual(
configuration.getstr(xenon.PYPROJECT_SECTION, "max_average", "None"), "A")

self.assertEqual(
configuration.getstr(xenon.PYPROJECT_SECTION, "max_modules", "None"), "B")

def test_without_trim_value_case(self):
'''Test without trim value case.'''
configuration = self.get_configuration(
'[tool.xenon]\n'
'path = ["path_1", "path_2", "path_3"]\n'
'max_average = A\n'
'max_average-num = 1.2\n')

self.assertEqual(
configuration.getstr(xenon.PYPROJECT_SECTION, "max_average", "None"), "A")


class TestCustomConfigParserGetListStr(unittest.TestCase):
'''Test class for class CustomConfigParser - getliststr method.'''

@staticmethod
def get_configuration(text):
'''Get CustomConfigParser object with loaded text.'''
with tempfile.TemporaryDirectory() as tmp_dir:
pyproject_path = tmp_dir + '/pyproject.toml'
with open(pyproject_path, "w") as toml_file:
toml_file.write(text)

configuration = xenon.CustomConfigParser()
configuration.read(pyproject_path)

return configuration

def test_missing_section_case(self):
'''Test missing section case.'''
configuration = self.get_configuration(
'[tool.xenon]\n'
'path = ["path_1", "path_2", "path_3"]\n'
'max_average = "A"\n'
'max_average-num = 1.2\n')

self.assertEqual(
configuration.getliststr(xenon.PYPROJECT_SECTION, "maxaverage", "None"), "None")

def test_parse_error_case(self):
'''Test parse error case.'''
configuration = self.get_configuration(
'[tool.xenon]\n'
'path = ["path_1", "path_2", "path_3"\n'
'max_average = "A"\n'
'max_average-num = 1.2\n')

self.assertRaisesRegex(
xenon.PyProjectParseError, "path", configuration.getliststr,
xenon.PYPROJECT_SECTION, "path")

def test_single_value_case(self):
'''Test single value case.'''
configuration = self.get_configuration(
'[tool.xenon]\n'
'path = "path_1"\n'
'max_average = "A"\n'
'max_average-num = 1.2\n')

self.assertListEqual(
configuration.getliststr(xenon.PYPROJECT_SECTION, "path", None), ["path_1"])

def test_invalid_format_case(self):
'''Test invalid format case'''
# Not a list case
configuration = self.get_configuration(
'[tool.xenon]\n'
'path = {"path_1": "path_2"}\n'
'max_average = "A"\n'
'max_average-num = 1.2\n')

self.assertRaisesRegex(
xenon.PyProjectParseError, "path", configuration.getliststr,
xenon.PYPROJECT_SECTION, "path", None)

# Not a list of str case
configuration = self.get_configuration(
'[tool.xenon]\n'
'path = ["path_1", "path_2", true]\n'
'max_average = "A"\n'
'max_average-num = 1.2\n')

self.assertRaisesRegex(
xenon.PyProjectParseError, "path", configuration.getliststr,
xenon.PYPROJECT_SECTION, "path", None)

def test_multiple_values_case(self):
'''Test multiple values case.'''
configuration = self.get_configuration(
'[tool.xenon]\n'
'path = ["path_1", "path_2", "path_3"]\n'
'max_average = "A"\n'
'max_average-num = 1.2\n')

self.assertListEqual(
configuration.getliststr(xenon.PYPROJECT_SECTION, "path", None),
["path_1", "path_2", "path_3"])


class TestParsePyproject(unittest.TestCase):
'''Test class for function parse_pyproject.'''

def test_parse_error_case(self):
'''Parse error case.'''
with tempfile.TemporaryDirectory() as tmp_dir:
pyproject_path = tmp_dir + '/pyproject.toml'
with open(pyproject_path, "w") as toml_file:
toml_file.write('parameter = value')

self.assertRaisesRegex(
xenon.PyProjectParseError, "Unable", xenon.parse_pyproject, pyproject_path)

def test_duplicate_parameteres_case(self):
'''Test duplicate parameters case'''
with tempfile.TemporaryDirectory() as tmp_dir:
pyproject_path = tmp_dir + '/pyproject.toml'
with open(pyproject_path, "w") as toml_file:
toml_file.write(
'[tool.xenon]\n'
'max_average_num = value_1\n'
'max_average_num = value_2')

self.assertRaisesRegex(
xenon.PyProjectParseError, "duplicate parameters", xenon.parse_pyproject, pyproject_path)

def test_missing_file_case(self):
'''Test missing file path case.'''
with tempfile.TemporaryDirectory() as tmp_dir:
self.assertDictEqual(xenon.parse_pyproject(tmp_dir), {})

def test_invalid_max_average_num_type_case(self):
'''Test invalid max-average-num parameter type case'''
with tempfile.TemporaryDirectory() as tmp_dir:
pyproject_path = tmp_dir + '/pyproject.toml'
with open(pyproject_path, "w") as toml_file:
toml_file.write('[tool.xenon]\nmax_average_num = value')

self.assertRaisesRegex(
xenon.PyProjectParseError, "max_average_num", xenon.parse_pyproject, pyproject_path)

def test_invalid_no_assert_type_case(self):
'''Test invalid no_assert parameter type case'''
with tempfile.TemporaryDirectory() as tmp_dir:
pyproject_path = tmp_dir + '/pyproject.toml'
with open(pyproject_path, "w") as toml_file:
toml_file.write('[tool.xenon]\nno_assert = next')

self.assertRaisesRegex(
xenon.PyProjectParseError, "no_assert", xenon.parse_pyproject, pyproject_path)

def test_all_parameters_case(self):
'''Test all parameters case.'''
with tempfile.TemporaryDirectory() as tmp_dir:
pyproject_path = tmp_dir + '/pyproject.toml'
with open(pyproject_path, "w") as toml_file:
toml_file.write(
'[tool.xenon]\n'
'path = ["path_1", "path_2", "path_3"]\n'
'max_average = "A"\n'
'max_average_num = 1.2\n'
'max_modules = "B"\n'
'max_absolute = "C"\n'
'exclude = ["path_4", "path_5"]\n'
'ignore = ["path_6", "path_7"]\n'
'no_assert = true')

result = xenon.parse_pyproject(pyproject_path)

self.assertDictEqual(
xenon.parse_pyproject(pyproject_path), {
"path": ["path_1", "path_2", "path_3"],
"average": 'A',
"averagenum": 1.2,
"modules": 'B',
"absolute": 'C',
"url": None,
"config": None,
"exclude": 'path_4,path_5',
"ignore": 'path_6,path_7',
"no_assert": True})


class TestGetParserDefaultsAndDeleteDefaults(unittest.TestCase):
'''Test class for function get_parser_defaults_and_delete_defaults.'''

def test_valid_case(self):
'''Test valid case'''
parser = argparse.ArgumentParser(add_help=False)

parser.add_argument("-t", "--test", default="test")
parser.add_argument("-v", "--values", default="values", dest="val")
parser.add_argument("-w", "--without")

args_defualt = xenon.get_parser_defaults_and_delete_defaults(parser)

self.assertEqual(args_defualt, {"test": "test", "val": "values"})

sys.argv = ["file.py"]

parser.parse_args()

self.assertEqual(
vars(parser.parse_args()), {"test": None, "val": None, "without": None})


class TestParseArgs(unittest.TestCase):
'''Test class for function parse_args.'''

def test_cmd_pyproject_case(self):
'''Test parameter set from cmd and pyproject case.'''
with tempfile.TemporaryDirectory() as tmp_dir:
pyproject_path = tmp_dir + '/pyproject.toml'
with open(pyproject_path, "w") as toml_file:
toml_file.write(
'[tool.xenon]\n'
'max_average = "A"\n')

sys.argv = ["file", "-a", "B"]

args = xenon.parse_args(pyproject_path)
self.assertEqual(args.average, "B")

def test_cmd_default_value_case(self):
'''Test parameter not set from cmd (with default value) and pyproject case.'''
with tempfile.TemporaryDirectory() as tmp_dir:
pyproject_path = tmp_dir + '/pyproject.toml'
with open(pyproject_path, "w") as toml_file:
toml_file.write(
'[tool.xenon]\n'
'config_file = config_file_path\n')

sys.argv = ["file"]

args = xenon.parse_args(pyproject_path)
self.assertEqual(args.config, "config_file_path")

def test_cmd_non_default_value_case(self):
'''Test parameter set from cmd (with default value) and pyproject case.'''
with tempfile.TemporaryDirectory() as tmp_dir:
pyproject_path = tmp_dir + '/pyproject.toml'
with open(pyproject_path, "w") as toml_file:
toml_file.write(
'[tool.xenon]\n'
'config_file = config_file_path\n')

# Set different value in cmd then default
sys.argv = ["file", "--config-file", "cfg_file_path"]

args = xenon.parse_args(pyproject_path)
self.assertEqual(args.config, "cfg_file_path")

# Set same value in cmd as default
sys.argv = ["file", "--config-file", ".xenon.yml"]

args = xenon.parse_args(pyproject_path)
self.assertEqual(args.config, ".xenon.yml")


if __name__ == '__main__':
unittest.main()
Loading

0 comments on commit 2352b97

Please # to comment.