Skip to content

Commit

Permalink
Refactor: specified encoding in read_text, write_text, open
Browse files Browse the repository at this point in the history
  • Loading branch information
y0urself committed Sep 20, 2021
1 parent 13967b0 commit a229c7c
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 43 deletions.
6 changes: 4 additions & 2 deletions pontos/changelog/conventional_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def __init__(
args: Namespace,
):
self.shell_cmd_runner: Callable = shell_cmd_runner
self.config: TOMLDocument = tomlkit.parse(args.config.read_text())
self.config: TOMLDocument = tomlkit.parse(
args.config.read_text(encoding='utf-8')
)
self.project: str = (
args.project
if args.project is not None
Expand Down Expand Up @@ -146,7 +148,7 @@ def build_changelog_file(self, commit_dict: Dict):
f'{self.project}/compare/<?>...HEAD'
)

self.output_file.write_text(changelog)
self.output_file.write_text(changelog, encoding='utf-8')


def initialize_default_parser() -> ArgumentParser:
Expand Down
10 changes: 5 additions & 5 deletions pontos/release/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def prepare(
)
changelog_bool = False
# Remove the header for the release text
changelog_text = output_file.read_text().replace(
changelog_text = output_file.read_text(encoding='utf-8').replace(
"# Changelog\n\n"
"All notable changes to this project "
"will be documented in this file.\n\n",
Expand All @@ -131,7 +131,7 @@ def prepare(

# Try to get the unreleased section of the specific version
updated, changelog_text = changelog_module.update(
change_log_path.read_text(),
change_log_path.read_text(encoding='utf-8'),
release_version,
git_tag_prefix=git_tag_prefix,
containing_version=release_version,
Expand All @@ -140,7 +140,7 @@ def prepare(
if not updated:
# Try to get unversioned unrelease section
updated, changelog_text = changelog_module.update(
change_log_path.read_text(),
change_log_path.read_text(encoding='utf-8'),
release_version,
git_tag_prefix=git_tag_prefix,
)
Expand All @@ -149,7 +149,7 @@ def prepare(
error("No unreleased text found in CHANGELOG.md")
sys.exit(1)

change_log_path.write_text(updated)
change_log_path.write_text(updated, encoding='utf-8')

ok("Updated CHANGELOG.md")

Expand All @@ -172,7 +172,7 @@ def prepare(
shell_cmd_runner(f"git tag {git_version} -m '{commit_msg}'")

release_text = path(RELEASE_TEXT_FILE)
release_text.write_text(changelog_text)
release_text.write_text(changelog_text, encoding='utf-8')

warning(
f"Please verify git tag {git_version}, "
Expand Down
6 changes: 3 additions & 3 deletions pontos/release/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def release(
shell_cmd_runner(f"git push --follow-tags {git_remote_name}")

info(f"Creating release for v{release_version}")
changelog_text: str = path(RELEASE_TEXT_FILE).read_text()
changelog_text: str = path(RELEASE_TEXT_FILE).read_text(encoding='utf-8')

headers = {'Accept': 'application/vnd.github.v3+json'}

Expand Down Expand Up @@ -126,13 +126,13 @@ def release(
warning(f"{tmp_path} is not a file.")

updated = changelog_module.add_skeleton(
markdown=change_log_path.read_text(),
markdown=change_log_path.read_text(encoding='utf-8'),
new_version=release_version,
project_name=project,
git_tag_prefix=git_tag_prefix,
git_space=space,
)
change_log_path.write_text(updated)
change_log_path.write_text(updated, encoding='utf-8')
changelog_bool = False

commit_msg += f'* Add empty changelog after {release_version}'
Expand Down
4 changes: 2 additions & 2 deletions pontos/updateheader/updateheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def _add_header(
licence_file = root / "templates" / licence / f"template{suffix}"
try:
return (
licence_file.read_text()
licence_file.read_text(encoding='utf-8')
.replace("<company>", company)
.replace("<year>", year)
)
Expand Down Expand Up @@ -214,7 +214,7 @@ def _get_exclude_list(
if exclude_file is None:
exclude_file = Path(".pontos-header-ignore")
try:
exclude_lines = exclude_file.read_text().split('\n')
exclude_lines = exclude_file.read_text(encoding='utf-8').split('\n')
except FileNotFoundError:
print("No exclude list file found.")
return []
Expand Down
8 changes: 4 additions & 4 deletions pontos/version/cmake_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,20 @@ def __print(self, *args):
print(*args)

def update_version(self, version: str, *, develop: bool = False):
content = self.__cmake_filepath.read_text()
content = self.__cmake_filepath.read_text(encoding='utf-8')
cmvp = CMakeVersionParser(content)
previous_version = cmvp.get_current_version()
new_content = cmvp.update_version(version, develop=develop)
self.__cmake_filepath.write_text(new_content)
self.__cmake_filepath.write_text(new_content, encoding='utf-8')
self.__print(f'Updated version from {previous_version} to {version}')

def print_current_version(self):
content = self.__cmake_filepath.read_text()
content = self.__cmake_filepath.read_text(encoding='utf-8')
cmvp = CMakeVersionParser(content)
self.__print(cmvp.get_current_version())

def get_current_version(self) -> str:
content = self.__cmake_filepath.read_text()
content = self.__cmake_filepath.read_text(encoding='utf-8')
cmvp = CMakeVersionParser(content)
return cmvp.get_current_version()

Expand Down
20 changes: 15 additions & 5 deletions pontos/version/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def get_version_from_pyproject_toml(pyproject_toml_path: Path = None) -> str:
if not pyproject_toml_path.exists():
raise VersionError(f'{str(pyproject_toml_path)} file not found.')

pyproject_toml = tomlkit.parse(pyproject_toml_path.read_text())
pyproject_toml = tomlkit.parse(
pyproject_toml_path.read_text(encoding='utf-8')
)
if (
'tool' in pyproject_toml
and 'poetry' in pyproject_toml['tool']
Expand Down Expand Up @@ -123,7 +125,9 @@ def _update_version_file(self, new_version: str) -> None:
Update the version file with the new version
"""
new_version = safe_version(new_version)
self.version_file_path.write_text(self.TEMPLATE.format(new_version))
self.version_file_path.write_text(
self.TEMPLATE.format(new_version), encoding='utf-8'
)

def _update_pyproject_version(
self,
Expand All @@ -134,7 +138,9 @@ def _update_pyproject_version(
"""

new_version = safe_version(new_version)
pyproject_toml = tomlkit.parse(self.pyproject_toml_path.read_text())
pyproject_toml = tomlkit.parse(
self.pyproject_toml_path.read_text(encoding='utf-8')
)

if 'tool' not in pyproject_toml:
tool_table = tomlkit.table()
Expand All @@ -146,7 +152,9 @@ def _update_pyproject_version(

pyproject_toml['tool']['poetry']['version'] = new_version

self.pyproject_toml_path.write_text(tomlkit.dumps(pyproject_toml))
self.pyproject_toml_path.write_text(
tomlkit.dumps(pyproject_toml), encoding='utf-8'
)

def update_version(
self, new_version: str, *, develop: bool = False, force: bool = False
Expand Down Expand Up @@ -248,7 +256,9 @@ def __init__(self, *, pyproject_toml_path=None):
if not pyproject_toml_path.exists():
raise VersionError(f'{str(pyproject_toml_path)} file not found.')

pyproject_toml = tomlkit.parse(pyproject_toml_path.read_text())
pyproject_toml = tomlkit.parse(
pyproject_toml_path.read_text(encoding='utf-8')
)

if (
'tool' not in pyproject_toml
Expand Down
1 change: 0 additions & 1 deletion tests/changelog/test_changelog_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def test_find_unreleased_information_before_another_version(self):
so little
### changed
I don't recognize it anymore
### security
[Unreleased]: https://github.com/greenbone/pontos/compare/v1.0.0...master
"""

Expand Down
6 changes: 4 additions & 2 deletions tests/changelog/test_conventional_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ def runner(cmd):
[0.0.2]: https://github.com/foo/bar/compare/0.0.1...0.0.2"""
output_file = changelog_builder.create_changelog_file()
print(output_file.read_text())
self.assertEqual(output_file.read_text(), expected_output)
print(output_file.read_text(encoding='utf-8'))
self.assertEqual(
output_file.read_text(encoding='utf-8'), expected_output
)

self.assertIn(
'git log "$(git describe --tags --abbrev=0)..HEAD" --oneline',
Expand Down
7 changes: 4 additions & 3 deletions tests/release/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,13 @@ def test_update_version_no_version_file(self):
'name = "testrepo"\n'
'version = "21.6.2.dev1"\n\n'
'[tool.pontos.version]\n'
'version-module-file = "testrepo/__version__.py"\n'
'version-module-file = "testrepo/__version__.py"\n',
encoding='utf-8',
)
executed, filename = update_version(
to='21.4.4', _version=version, develop=False
)
toml_text = toml.read_text()
toml_text = toml.read_text(encoding='utf-8')
print(toml_text)
self.assertEqual(filename, 'pyproject.toml')
self.assertTrue(executed)
Expand All @@ -168,7 +169,7 @@ def test_update_version_no_version_file(self):
'version-module-file = "testrepo/__version__.py"\n',
)
self.assertTrue(version_file.exists())
version_text = version_file.read_text()
version_text = version_file.read_text(encoding='utf-8')
self.assertEqual(
version_text,
'# pylint: disable=invalid-name\n\n'
Expand Down
4 changes: 3 additions & 1 deletion tests/release/test_prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ def test_prepare_coventional_commits(self, changelog_mock):
[21.8.1]: https://github.com/y0urself/test_workflows/compare/21.8.0...21.8.1"""

self.assertEqual(release_file.read_text(), expected_release_content)
self.assertEqual(
release_file.read_text(encoding='utf-8'), expected_release_content
)

release_file.unlink()
14 changes: 6 additions & 8 deletions tests/updateheader/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,6 @@ def test_update_create_header(self, mock_stdout):
test_file = self.path / "test.py"
test_file.touch()

# test_file.write_text(expected_header)

code = update_file(file=test_file, regex=self.regex, args=self.args)

ret = mock_stdout.getvalue()
Expand All @@ -308,7 +306,7 @@ def test_update_create_header(self, mock_stdout):
f"{test_file}: Added licence header.\n",
)
self.assertEqual(code, 0)
self.assertEqual(expected_header, test_file.read_text())
self.assertEqual(expected_header, test_file.read_text(encoding='utf-8'))
test_file.unlink()

@patch('sys.stdout', new_callable=StringIO)
Expand Down Expand Up @@ -340,7 +338,7 @@ def test_update_header_in_file(self, mock_stdout):
if test_file.exists():
test_file.unlink()

test_file.write_text(header)
test_file.write_text(header, encoding='utf-8')

code = update_file(file=test_file, regex=self.regex, args=self.args)

Expand All @@ -353,7 +351,7 @@ def test_update_header_in_file(self, mock_stdout):
)
self.assertIn(
'# Copyright (C) 2020-2021 Greenbone Networks GmbH',
test_file.read_text(),
test_file.read_text(encoding='utf-8'),
)

test_file.unlink()
Expand Down Expand Up @@ -387,7 +385,7 @@ def test_update_header_ok_in_file(self, mock_stdout):
if test_file.exists():
test_file.unlink()

test_file.write_text(header)
test_file.write_text(header, encoding='utf-8')

code = update_file(file=test_file, regex=self.regex, args=self.args)

Expand All @@ -399,7 +397,7 @@ def test_update_header_ok_in_file(self, mock_stdout):
)
self.assertIn(
'# Copyright (C) 2021 Greenbone Networks GmbH',
test_file.read_text(),
test_file.read_text(encoding='utf-8'),
)

test_file.unlink()
Expand Down Expand Up @@ -438,7 +436,7 @@ def test_get_exclude_list(self):
test_dirname = Path(__file__) / "../.."
# with a relative glob
test_ignore_file = Path('ignore.file')
test_ignore_file.write_text("*.py\n")
test_ignore_file.write_text("*.py\n", encoding='utf-8')

exclude_list = get_exclude_list(
test_ignore_file, [test_dirname.resolve()]
Expand Down
8 changes: 4 additions & 4 deletions tests/version/test_cmake_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_raise_exception_no_project(self):
fake_path.read_text.return_value = ""
with self.assertRaises(ValueError):
CMakeVersionCommand(cmake_lists_path=fake_path).run(args=['show'])
fake_path.read_text.assert_called_with()
fake_path.read_text.assert_called_with(encoding='utf-8')

def test_return_error_string_incorrect_version_on_verify(self):
fake_path_class = MagicMock(spec=Path)
Expand Down Expand Up @@ -73,7 +73,7 @@ def test_should_call_print_current_version_without_raising_exception(self):
fake_path.exists.return_value = True
fake_path.read_text.return_value = "project(VERSION 21)"
CMakeVersionCommand(cmake_lists_path=fake_path).run(args=['show'])
fake_path.read_text.assert_called_with()
fake_path.read_text.assert_called_with(encoding='utf-8')

def test_raise_update_version(self):
fake_path_class = MagicMock(spec=Path)
Expand All @@ -86,9 +86,9 @@ def test_raise_update_version(self):
CMakeVersionCommand(cmake_lists_path=fake_path).run(
args=['update', '22', '--develop']
)
fake_path.read_text.assert_called_with()
fake_path.read_text.assert_called_with(encoding='utf-8')
fake_path.write_text.assert_called_with(
'project(VERSION 22)\nset(PROJECT_DEV_VERSION 1)'
'project(VERSION 22)\nset(PROJECT_DEV_VERSION 1)', encoding='utf-8'
)


Expand Down
6 changes: 3 additions & 3 deletions tests/version/test_get_version_from_pyproject_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_no_poerty_section(self):
get_version_from_pyproject_toml(fake_path)

fake_path.exists.assert_called_with()
fake_path.read_text.assert_called_with()
fake_path.read_text.assert_called_with(encoding='utf-8')

def test_empty_poerty_section(self):
fake_path_class = MagicMock(spec=Path)
Expand All @@ -66,7 +66,7 @@ def test_empty_poerty_section(self):
get_version_from_pyproject_toml(fake_path)

fake_path.exists.assert_called_with()
fake_path.read_text.assert_called_with()
fake_path.read_text.assert_called_with(encoding='utf-8')

def test_get_version(self):
fake_path_class = MagicMock(spec=Path)
Expand All @@ -80,4 +80,4 @@ def test_get_version(self):
self.assertEqual(version, '1.2.3')

fake_path.exists.assert_called_with()
fake_path.read_text.assert_called_with()
fake_path.read_text.assert_called_with(encoding='utf-8')

0 comments on commit a229c7c

Please sign in to comment.