Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_load_config_dict_from_toml_file(self):
config_path = get_test_config_path('pyproject.test2.toml')
self.assertTrue(config_path.is_file())
autohooksconfig = load_config_from_pyproject_toml(config_path)
self.assertTrue(autohooksconfig.has_config())
config = autohooksconfig.get_config()
self.assertEqual(
config.get('tool')
.get('autohooks')
.get('plugins')
.get('foo')
.get_value('bar'),
'ipsum',
)
self.assertEqual(
config.get('tool')
.get('autohooks')
.get('plugins')
.get('foo')
def test_load_from_toml_file(self):
config_path = get_test_config_path('pyproject.test1.toml')
self.assertTrue(config_path.is_file())
config = load_config_from_pyproject_toml(config_path)
self.assertTrue(config.has_config())
self.assertTrue(config.has_autohooks_config())
self.assertTrue(config.is_autohooks_enabled())
self.assertListEqual(
config.get_pre_commit_script_names(), ['foo', 'bar']
)
def test_load_from_non_existing_toml_file(self):
config_path = Path('foo')
self.assertFalse(config_path.exists())
config = load_config_from_pyproject_toml(config_path)
self.assertFalse(config.has_config())
self.assertFalse(config.has_autohooks_config())
self.assertFalse(config.is_autohooks_enabled())
self.assertEqual(config.get_mode(), Mode.UNDEFINED)
self.assertEqual(len(config.get_pre_commit_script_names()), 0)
def check_config(
term: Terminal, pyproject_toml: Path, pre_commit_hook: PreCommitHook,
) -> None:
if not pyproject_toml.exists():
term.error(
'Missing {} file. Please add a pyproject.toml file and include '
'a "{}" section.'.format(str(pyproject_toml), AUTOHOOKS_SECTION)
)
else:
config = load_config_from_pyproject_toml(pyproject_toml)
if not config.is_autohooks_enabled():
term.error(
'autohooks is not enabled in your {} file. Please add '
'a "{}" section.'.format(str(pyproject_toml), AUTOHOOKS_SECTION)
)
elif pre_commit_hook.exists():
config_mode = config.get_mode()
hook_mode = pre_commit_hook.read_mode()
if config_mode == Mode.UNDEFINED:
term.warning(
'autohooks mode is not defined in {}.'.format(
str(pyproject_toml)
)
)
elif config_mode == Mode.UNKNOWN:
def run() -> int:
print('autohooks => pre-commit')
config = load_config_from_pyproject_toml()
pre_commit_hook = PreCommitHook()
check_hook_is_current(pre_commit_hook)
check_hook_mode(config.get_mode(), pre_commit_hook.read_mode())
plugins = get_project_autohooks_plugins_path()
plugins_dir_name = str(plugins)
if plugins.is_dir():
sys.path.append(plugins_dir_name)
with autohooks_module_path():
for name in config.get_pre_commit_script_names():
try:
plugin = load_plugin(name)
def install_hooks(term: Terminal, args: Namespace) -> None:
pre_commit_hook = PreCommitHook()
pyproject_toml = get_pyproject_toml_path()
config = load_config_from_pyproject_toml(pyproject_toml)
if pre_commit_hook.exists() and not args.force:
term.ok(
'autohooks pre-commit hook is already installed at {}.'.format(
str(pre_commit_hook)
)
)
with term.indent():
term.print()
term.info(
"Run 'autohooks activate --force' to override the current "
"installed pre-commit hook."
)
term.info(
"Run 'autohooks check' to validate the current status of "
"the installed pre-commit hook."
def install_git_hook(self) -> None:
try:
pre_commit_hook_path = get_pre_commit_hook_path()
if not pre_commit_hook_path.exists():
config = load_config_from_pyproject_toml()
mode = config.get_mode()
autohooks_pre_commit_hook = get_autohooks_pre_commit_hook(mode)
install_pre_commit_hook(
autohooks_pre_commit_hook, pre_commit_hook_path
)
except Exception: # pylint: disable=broad-except
pass