Skip to content

Commit

Permalink
Fixed platform-dependent encoding.
Browse files Browse the repository at this point in the history
- Added `encoding="utf-8"` to all writes.
  • Loading branch information
coordt committed Mar 23, 2024
1 parent 455c9bc commit f8b4d65
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 59 deletions.
2 changes: 1 addition & 1 deletion bumpversion/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ def sample_config(prompt: bool, destination: str) -> None:
if destination == "stdout":
print_info(dumps(destination_config))
else:
Path(destination).write_text(dumps(destination_config))
Path(destination).write_text(dumps(destination_config), encoding="utf-8")

Check warning on line 551 in bumpversion/cli.py

View check run for this annotation

Codecov / codecov/patch

bumpversion/cli.py#L551

Added line #L551 was not covered by tests


@cli.command()
Expand Down
2 changes: 1 addition & 1 deletion bumpversion/config/files_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,4 @@ def update_ini_config_file(
)

if not dry_run:
config_path.write_text(new_config)
config_path.write_text(new_config, encoding="utf-8")
2 changes: 1 addition & 1 deletion bumpversion/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,4 @@ def _update_toml_file(

set_nested_value(toml_data, new_value, self.file_change.key_path)

self.path.write_text(tomlkit.dumps(toml_data))
self.path.write_text(tomlkit.dumps(toml_data), encoding="utf-8")
3 changes: 2 additions & 1 deletion tests/test_bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ def test_key_path_required_for_toml_change(tmp_path: Path, caplog):
bake_cookies = true
ignore-words-list = "sugar, salt, flour"
"""
)
),
encoding="utf-8",
)

conf = config.get_configuration(config_file=config_path)
Expand Down
13 changes: 7 additions & 6 deletions tests/test_cli/test_bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ def test_bump_nested_regex(tmp_path: Path, fixtures_path: Path, caplog, runner):
Assert: There is no configured files specified to modify
"""
cff_path = tmp_path / "citation.cff"
cff_path.write_text("cff-version: 1.2.0\ndate-released: 2023-09-19\n")
cff_path.write_text("cff-version: 1.2.0\ndate-released: 2023-09-19\n", encoding="utf-8")
content = fixtures_path.joinpath("regex_test_config.toml").read_text()
config_path = tmp_path / ".bumpversion.toml"
config_path.write_text(content)
config_path.write_text(content, encoding="utf-8")

with inside_dir(tmp_path):
result: Result = runner.invoke(cli.cli, ["bump", "-vv", "patch"])
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_dirty_work_dir_raises_error(repo: str, scm_command: str, request, runne
repo_path: Path = request.getfixturevalue(repo)
with inside_dir(repo_path):
# Arrange
repo_path.joinpath("dirty2").write_text("i'm dirty! 1.1.1")
repo_path.joinpath("dirty2").write_text("i'm dirty! 1.1.1", encoding="utf-8")
subprocess.run([scm_command, "add", "dirty2"], check=True)

# Act
Expand All @@ -182,7 +182,7 @@ def test_non_scm_operations_if_scm_not_installed(tmp_path: Path, monkeypatch, ru

with inside_dir(tmp_path):
version_path = tmp_path / "VERSION"
version_path.write_text("31.0.3")
version_path.write_text("31.0.3", encoding="utf-8")

# Act
runner.invoke(cli.cli, ["bump", "major", "--current-version", "31.0.3", "VERSION"])
Expand All @@ -205,7 +205,7 @@ def test_detects_bad_or_missing_version_part(version_part: str, tmp_path: Path,

with inside_dir(tmp_path):
version_path = tmp_path / "VERSION"
version_path.write_text("31.0.3")
version_path.write_text("31.0.3", encoding="utf-8")

args = ["bump", "--current-version", "31.0.3"]
if version_part:
Expand All @@ -229,7 +229,8 @@ def test_ignores_missing_files_with_option(tmp_path, fixtures_path, runner):
"allow_dirty = true\n\n"
"[[tool.bumpversion.files]]\n"
'filename = "VERSION"\n'
"regex = false\n"
"regex = false\n",
encoding="utf-8",
)

# Act
Expand Down
25 changes: 16 additions & 9 deletions tests/test_cli/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ def test_ignores_missing_files_with_option(self, mocker, tmp_path, fixtures_path
"allow_dirty = true\n\n"
"[[tool.bumpversion.files]]\n"
'filename = "VERSION"\n'
"regex = false\n"
"regex = false\n",
encoding="utf-8",
)

# Act
Expand Down Expand Up @@ -156,9 +157,11 @@ def test_accepts_plain_string(self, tmp_path, fixtures_path, runner):

# Arrange
config_path = tmp_path / "pyproject.toml"
config_path.write_text(dumps(TEST_REPLACE_CONFIG))
config_path.write_text(dumps(TEST_REPLACE_CONFIG), encoding="utf-8")
doc_path = tmp_path / "docs.yaml"
doc_path.write_text("url: https://github.com/sampleuser/workflows/main/.github/update_mailmap.py")
doc_path.write_text(
"url: https://github.com/sampleuser/workflows/main/.github/update_mailmap.py", encoding="utf-8"
)

with inside_dir(tmp_path):
result: Result = runner.invoke(
Expand Down Expand Up @@ -189,7 +192,8 @@ def test_unintentional_valid_regex_still_found(self, tmp_path: Path, caplog, run

version_path = tmp_path / "VERSION"
version_path.write_text(
"# Changelog\n\n## [0.0.1 (unreleased)](https://cool.url)\n\n- Test unreleased package.\n"
"# Changelog\n\n## [0.0.1 (unreleased)](https://cool.url)\n\n- Test unreleased package.\n",
encoding="utf-8",
)
config_file = tmp_path / ".bumpversion.toml"
config_file.write_text(
Expand All @@ -200,7 +204,8 @@ def test_unintentional_valid_regex_still_found(self, tmp_path: Path, caplog, run
'filename = "VERSION"\n'
"regex = false\n"
f'search = "{search}"\n'
f'replace = "{replace}"\n'
f'replace = "{replace}"\n',
encoding="utf-8",
)

# Act
Expand Down Expand Up @@ -241,9 +246,9 @@ def test_accepts_empty_string(self, tmp_path, fixtures_path, runner):

# Arrange
config_path = tmp_path / "pyproject.toml"
config_path.write_text(dumps(TEST_REPLACE_CONFIG))
config_path.write_text(dumps(TEST_REPLACE_CONFIG), encoding="utf-8")
doc_path = tmp_path / "docs.yaml"
doc_path.write_text("We should censor profanity\n\n")
doc_path.write_text("We should censor profanity\n\n", encoding="utf-8")

with inside_dir(tmp_path):
result: Result = runner.invoke(
Expand Down Expand Up @@ -274,9 +279,11 @@ def test_accepts_plain_string(self, tmp_path, fixtures_path, runner):

# Arrange
config_path = tmp_path / "pyproject.toml"
config_path.write_text(dumps(TEST_REPLACE_CONFIG))
config_path.write_text(dumps(TEST_REPLACE_CONFIG), encoding="utf-8")
doc_path = tmp_path / "docs.yaml"
doc_path.write_text("url: https://github.com/sampleuser/workflows/v2.17.7/.github/update_mailmap.py")
doc_path.write_text(
"url: https://github.com/sampleuser/workflows/v2.17.7/.github/update_mailmap.py", encoding="utf-8"
)

with inside_dir(tmp_path):
result: Result = runner.invoke(
Expand Down
6 changes: 3 additions & 3 deletions tests/test_config/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class TestGetDefaultsFromDest:
def test_existing_path_returns_existing_config(self, tmp_path: Path, default_config: dict):
"""If the destination exists, the existing config should be returned."""
dest_path = tmp_path / "pyproject-1.toml"
dest_path.write_text(dumps(BASIC_CONFIG))
dest_path.write_text(dumps(BASIC_CONFIG), encoding="utf-8")

with inside_dir(tmp_path):
config, destination_config = get_defaults_from_dest("pyproject-1.toml")
Expand All @@ -73,7 +73,7 @@ def test_missing_path_returns_default_config(self, tmp_path: Path, default_confi
def test_existing_path_with_no_config_returns_default_config(self, tmp_path: Path, default_config: dict):
"""If the destination exists but has no config, the default config should be returned."""
dest_path = tmp_path / "pyproject-3.toml"
dest_path.write_text(dumps({"tool": {"poetry": {}}}))
dest_path.write_text(dumps({"tool": {"poetry": {}}}), encoding="utf-8")

with inside_dir(tmp_path):
config, destination_config = get_defaults_from_dest("pyproject-3.toml")
Expand All @@ -85,7 +85,7 @@ def test_existing_path_with_no_config_returns_default_config(self, tmp_path: Pat
def test_existing_path_with_project_version_uses_project_version(self, tmp_path: Path, default_config: dict):
"""If the destination exists and has a project version, the project version should be used as current_version."""
dest_path = tmp_path / "pyproject-4.toml"
dest_path.write_text(dumps({"tool": {"poetry": {}}, "project": {"version": "1.2.3"}}))
dest_path.write_text(dumps({"tool": {"poetry": {}}, "project": {"version": "1.2.3"}}), encoding="utf-8")

with inside_dir(tmp_path):
config, destination_config = get_defaults_from_dest("pyproject-4.toml")
Expand Down
23 changes: 12 additions & 11 deletions tests/test_config/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TestWhenExplictConfigFileIsPassed:
def test_returns_path_to_existing_file(self, tmp_path: Path) -> None:
"""If an explicit config file is passed, it should be returned."""
cfg_file = tmp_path / "bump.toml"
cfg_file.write_text('[tool.bumpversion]\ncurrent_version = "1.0.0"')
cfg_file.write_text('[tool.bumpversion]\ncurrent_version = "1.0.0"', encoding="utf-8")
assert find_config_file(cfg_file) == cfg_file

def test_returns_none_when_missing_file(self, tmp_path: Path) -> None:
Expand All @@ -43,7 +43,7 @@ class TestWhenNoExplicitConfigFileIsPassed:
def test_returns_path_to_existing_default_file(self, tmp_path: Path, cfg_file_name: str) -> None:
"""If no explicit config file is passed, it returns the path to an existing expected file."""
cfg_file = tmp_path / cfg_file_name
cfg_file.write_text('[tool.bumpversion]\ncurrent_version = "1.0.0"')
cfg_file.write_text('[tool.bumpversion]\ncurrent_version = "1.0.0"', encoding="utf-8")
with inside_dir(tmp_path):
assert find_config_file() == cfg_file

Expand All @@ -57,7 +57,7 @@ def test_returns_path_to_existing_file_in_correct_order(self, tmp_path: Path) ->
expected_order = list(CONFIG_FILE_SEARCH_ORDER)[:] # make a copy so we can mutate it
cfg_file_paths = [tmp_path / cfg_file_name for cfg_file_name in expected_order]
for cfg_file in cfg_file_paths: # create all the files
cfg_file.write_text('[tool.bumpversion]\ncurrent_version = "1.0.0"')
cfg_file.write_text('[tool.bumpversion]\ncurrent_version = "1.0.0"', encoding="utf-8")

with inside_dir(tmp_path):
while expected_order:
Expand Down Expand Up @@ -101,7 +101,7 @@ def test_returns_empty_dict_with_unknown_suffix(
caplog.set_level("INFO")
tmp_path = tmp_path_factory.mktemp("explicit-file-passed-")
cfg_file = tmp_path / "basic_cfg.unknown"
cfg_file.write_text('[tool.bumpversion]\ncurrent_version = "1.0.0"')
cfg_file.write_text('[tool.bumpversion]\ncurrent_version = "1.0.0"', encoding="utf-8")
with inside_dir(tmp_path):
assert config.read_config_file(cfg_file) == {}
assert "Unknown config file suffix" in caplog.text
Expand Down Expand Up @@ -132,9 +132,9 @@ def test_read_toml_file(conf_file: str, expected_file: str, fixtures_path: Path)
def test_multiple_config_files(tmp_path: Path):
"""If there are multiple config files, the first one with content wins."""
setup_cfg = tmp_path / "setup.cfg"
setup_cfg.write_text("[metadata]\nname: just-a-name\n")
setup_cfg.write_text("[metadata]\nname: just-a-name\n", encoding="utf-8")
bumpversion_cfg = tmp_path / ".bumpversion.cfg"
bumpversion_cfg.write_text("\n")
bumpversion_cfg.write_text("\n", encoding="utf-8")
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
"[tool.bumpversion]\n"
Expand All @@ -143,7 +143,8 @@ def test_multiple_config_files(tmp_path: Path):
"serialize = [\n"
' "{major}.{minor}.{patch}-{release}",\n'
' "{major}.{minor}.{patch}"\n'
"]\n"
"]\n",
encoding="utf-8",
)
with inside_dir(tmp_path):
cfg_file = bumpversion.config.files.find_config_file()
Expand Down Expand Up @@ -180,7 +181,7 @@ def test_update_config_file(tmp_path: Path, cfg_file_name: str, fixtures_path: P
expected_diff = TOML_EXPECTED_DIFF
cfg_path = tmp_path / cfg_file_name
orig_path = fixtures_path / f"basic_cfg{cfg_path.suffix}"
cfg_path.write_text(orig_path.read_text())
cfg_path.write_text(orig_path.read_text(), encoding="utf-8")
original_content = orig_path.read_text().splitlines(keepends=True)
with inside_dir(tmp_path):
cfg = config.get_configuration(cfg_path)
Expand All @@ -206,9 +207,9 @@ def test_pep440_config(git_repo: Path, fixtures_path: Path):

cfg_path = git_repo / "pyproject.toml"
orig_path = fixtures_path / "pep440.toml"
cfg_path.write_text(orig_path.read_text())
cfg_path.write_text(orig_path.read_text(), encoding="utf-8")
version_path = git_repo / "VERSION"
version_path.write_text("1.0.0")
version_path.write_text("1.0.0", encoding="utf-8")
readme_path = git_repo / "README.md"
runner: CliRunner = CliRunner()

Expand All @@ -225,7 +226,7 @@ def test_pep440_config(git_repo: Path, fixtures_path: Path):
assert next_version_str == "1.0.1"

subprocess.run(["git", "checkout", "-b", "my-really-LONG-branch_name"], check=True, capture_output=True)
readme_path.write_text("This is my branch!")
readme_path.write_text("This is my branch!", encoding="utf-8")
result: Result = runner.invoke(cli.cli, ["bump", "dev_label", "--no-tag"])
assert result.exit_code == 0
cfg = config.get_configuration(cfg_path)
Expand Down
8 changes: 5 additions & 3 deletions tests/test_config/test_files_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def test_independent_falsy_value_in_config_does_not_bump_independently(tmp_path:
[bumpversion:part:build]
independent = 0
"""
)
),
encoding="utf-8",
)

conf = config.get_configuration(config_file)
Expand Down Expand Up @@ -113,7 +114,7 @@ def test_update_ini_config_file(tmp_path: Path, cfg_file_name: str, fixtures_pat
expected_diff = CFG_EXPECTED_DIFF
cfg_path = tmp_path / cfg_file_name
orig_path = fixtures_path / f"basic_cfg{cfg_path.suffix}"
cfg_path.write_text(orig_path.read_text())
cfg_path.write_text(orig_path.read_text(), encoding="utf-8")
original_content = orig_path.read_text().splitlines(keepends=True)

update_ini_config_file(cfg_path, "1.0.0", "1.0.1")
Expand All @@ -133,7 +134,8 @@ def test_file_keyword_with_suffix_is_accepted(tmp_path: Path, cfg_file: str, cfg
"replace = version {new_version}\n"
f"[bumpversion:{cfg_file_keyword}:file2]\n"
"search = The current version is {current_version}\n"
"replace = The current version is {new_version}\n"
"replace = The current version is {new_version}\n",
encoding="utf-8",
)
setup_cfg = config.get_configuration(cfg_file_path)
assert len(setup_cfg.files) == 2
Expand Down
2 changes: 1 addition & 1 deletion tests/test_config/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def write_config(tmp_path: Path, overrides: dict) -> Path:
defaults["current_version"] = defaults["current_version"] or "1.2.3"
defaults["commit_args"] = ""
config = {"tool": {"bumpversion": {**defaults, **overrides}}}
config_file.write_text(tomlkit.dumps(config))
config_file.write_text(tomlkit.dumps(config), encoding="utf-8")
return config_file


Expand Down
Loading

0 comments on commit f8b4d65

Please sign in to comment.