Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit encoding #100

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Next release

Default development branch is now called `main`.

Always use `utf-8` encoding explicitly

6.3.2 (2021-04-19)
------------------

Expand Down
137 changes: 136 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pytest = "^6.2.1"
pytest-cov = "^2.8.1"
pytest-mock = "^2.0.0"
safety = "^1.9.0"
semgrep = "^0.52.0"

[tool.poetry.scripts]
tbump = "tbump.main:main"
Expand Down
13 changes: 13 additions & 0 deletions semgrep.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
rules:
- id: python-no-implicit-encoding-when-reading
languages:
- python
message: Specify encoding when using read_text()
pattern: $OBJECT.read_text()
severity: ERROR
- id: python-no-implicit-encoding-when-writing
languages:
- python
message: Specify encoding when using write_text()
pattern: $OBJECT.write_text($X)
severity: ERROR
7 changes: 7 additions & 0 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def mypy(c, machine_readable=False):
c.run(cmd)


@task
def semgrep(c):
print("Running semgrep")
c.run("semgrep -c semgrep.yml")


@task
def test(c):
print("Running pytest")
Expand All @@ -47,6 +53,7 @@ def test(c):

@task(
pre=[
call(semgrep),
call(black, check=True),
call(isort, check=True),
call(flake8),
Expand Down
4 changes: 2 additions & 2 deletions tbump/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ def get_config_file(project_path: Path) -> ConfigFile:
def _get_config_file(project_path: Path) -> ConfigFile:
toml_path = project_path / "tbump.toml"
if toml_path.exists():
doc = tomlkit.loads(toml_path.read_text())
doc = tomlkit.loads(toml_path.read_text(encoding="utf-8"))
return TbumpTomlConfig(toml_path, doc)

pyproject_path = project_path / "pyproject.toml"
if pyproject_path.exists():
doc = tomlkit.loads(pyproject_path.read_text())
doc = tomlkit.loads(pyproject_path.read_text(encoding="utf-8"))
try:
doc["tool"]["tbump"]
except KeyError:
Expand Down
2 changes: 1 addition & 1 deletion tbump/file_bumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def compute_patches_for_change_request(
for file_path_str in glob.glob(str(file_path_glob), recursive=True):
file_path = Path(file_path_str)
expanded_src = file_path.relative_to(self.working_path)
old_lines = file_path.read_text().splitlines(keepends=False)
old_lines = file_path.read_text(encoding="utf-8").splitlines(keepends=False)

for i, old_line in enumerate(old_lines):
if should_replace(old_line, old_string, search):
Expand Down
2 changes: 1 addition & 1 deletion tbump/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def restore_cwd() -> Iterator[None]:


def file_contains(path: Path, text: str) -> bool:
for line in path.read_text().splitlines():
for line in path.read_text(encoding="utf-8").splitlines():
if text in line:
return True
return False
Expand Down
2 changes: 1 addition & 1 deletion tbump/test/data/after.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def main() -> None:
Path("after-hook.stamp").write_text("")
Path("after-hook.stamp").write_text("", encoding="utf-8")


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tbump/test/data/before.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

def main() -> None:
current, new = sys.argv[1:]
Path("before-hook.stamp").write_text(current + " -> " + new)
Path("before-hook.stamp").write_text(current + " -> " + new, encoding="utf-8")


if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions tbump/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_uses_pyproject_if_tbump_toml_is_missing(
to_write = tomlkit.dumps(pyproject_config)

pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(to_write)
pyproject_toml.write_text(to_write, encoding="utf-8")

actual_file = tbump.config.get_config_file(tmp_path)
assert actual_file.get_config() == expected_file.get_config()
Expand All @@ -71,7 +71,7 @@ def test_complain_if_pyproject_does_not_contain_tbump_config(tmp_path: Path) ->
profile = "black"
"""
)
pyproject_toml.write_text(to_write)
pyproject_toml.write_text(to_write, encoding="utf-8")

with pytest.raises(tbump.config.ConfigNotFound):
tbump.config.get_config_file(tmp_path)
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_validate_schema_in_pyrpoject_toml(tmp_path: Path) -> None:
'''
"""
)
pyproject_toml.write_text(to_write)
pyproject_toml.write_text(to_write, encoding="utf-8")

with pytest.raises(tbump.config.InvalidConfig) as e:
tbump.config.get_config_file(tmp_path)
Expand Down
Loading