Skip to content

Commit

Permalink
fix: fix crash when TOML has no PSR section (#319)
Browse files Browse the repository at this point in the history
* test: reproduce issue with TOML without PSR section

* fix: crash when TOML has no PSR section

* chore: remove unused imports
  • Loading branch information
browniebroke committed Jan 29, 2021
1 parent 39acb68 commit 5f8ab99
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion semantic_release/settings.py
Expand Up @@ -67,7 +67,7 @@ def _config_from_pyproject(path):
with open(path, "r") as f:
pyproject = tomlkit.loads(f.read())
if pyproject:
return dict(pyproject.get("tool").get("semantic_release"))
return pyproject.get("tool", {}).get("semantic_release", {})
except TOMLKitError as e:
logger.debug(f"Could not decode pyproject.toml: {e}")

Expand Down
24 changes: 22 additions & 2 deletions tests/test_settings.py
@@ -1,9 +1,8 @@
import os
import platform
from textwrap import dedent
from unittest import TestCase

import tomlkit

from semantic_release.errors import ImproperConfigurationError
from semantic_release.history import parser_angular
from semantic_release.settings import _config, current_commit_parser
Expand Down Expand Up @@ -90,6 +89,27 @@ def test_no_raise_toml_error(self, mock_getcwd, mock_debug):
# delete temporary toml config file
os.remove(dummy_conf_path)

@mock.patch("semantic_release.settings.getcwd", return_value=temp_dir)
def test_toml_no_psr_section(self, mock_getcwd):
# create temporary toml config file
dummy_conf_path = os.path.join(temp_dir, "pyproject.toml")
toml_conf_content = dedent(
"""
[tool.foo]
bar = "baz"
"""
)
os.makedirs(os.path.dirname(dummy_conf_path), exist_ok=True)

with open(dummy_conf_path, "w") as dummy_conf_file:
dummy_conf_file.write(toml_conf_content)

config = _config()
mock_getcwd.assert_called_once_with()
self.assertEqual(config.get("hvcs"), "github")
# delete temporary toml config file
os.remove(dummy_conf_path)

@mock.patch("semantic_release.settings.config.get", lambda *x: "nonexistent.parser")
def test_current_commit_parser_should_raise_error_if_parser_module_do_not_exist(
self,
Expand Down

0 comments on commit 5f8ab99

Please sign in to comment.