Skip to content

Commit

Permalink
Merge pull request #951 from RonnyPfannschmidt/fix-950-encoding-safe
Browse files Browse the repository at this point in the history
fix #950 - add encoding args to io interaction
  • Loading branch information
RonnyPfannschmidt authored Oct 10, 2023
2 parents b877fb8 + 83dd018 commit 4e8e40a
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 21 deletions.
4 changes: 4 additions & 0 deletions changelog.d/20231003_203751_ronny_fix_950_encoding_safe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

### Changed

- fix #950: ensure to pass encodings to io usage
1 change: 1 addition & 0 deletions src/setuptools_scm/_run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def run(
HGPLAIN="1",
),
text=True,
encoding="utf-8",
timeout=timeout,
)

Expand Down
18 changes: 11 additions & 7 deletions testing/test_basic_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_run_plain(tmp_path: Path) -> None:

def test_data_from_mime(tmp_path: Path) -> None:
tmpfile = tmp_path.joinpath("test.archival")
tmpfile.write_text("name: test\nrevision: 1")
tmpfile.write_bytes(b"name: test\nrevision: 1")

res = data_from_mime(str(tmpfile))
assert res == {"name": "test", "revision": "1"}
Expand Down Expand Up @@ -81,7 +81,8 @@ def test_parentdir_prefix(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> No
p.joinpath("setup.py").write_text(
"""from setuptools import setup
setup(use_scm_version={"parentdir_prefix_version": "projectname-"})
"""
""",
encoding="utf-8",
)
res = run([sys.executable, "setup.py", "--version"], p)
assert res.stdout == "12.34"
Expand All @@ -94,7 +95,8 @@ def test_fallback(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
p.joinpath("setup.py").write_text(
"""from setuptools import setup
setup(use_scm_version={"fallback_version": "12.34"})
"""
""",
encoding="utf-8",
)
res = run([sys.executable, "setup.py", "--version"], p)
assert res.stdout == "12.34"
Expand All @@ -108,7 +110,8 @@ def test_empty_pretend_version(tmp_path: Path, monkeypatch: pytest.MonkeyPatch)
p.joinpath("setup.py").write_text(
"""from setuptools import setup
setup(use_scm_version={"fallback_version": "12.34"})
"""
""",
encoding="utf-8",
)
res = run([sys.executable, "setup.py", "--version"], p)
assert res.stdout == "12.34"
Expand All @@ -126,7 +129,8 @@ def test_empty_pretend_version_named(
p.joinpath("setup.py").write_text(
"""from setuptools import setup
setup(name="myscm", use_scm_version={"fallback_version": "12.34"})
"""
""",
encoding="utf-8",
)
res = run([sys.executable, "setup.py", "--version"], p)
assert res.stdout == "12.34"
Expand Down Expand Up @@ -171,7 +175,7 @@ def test_dump_version(tmp_path: Path) -> None:
dump_version(tmp_path, version, "first.txt", scm_version=scm_version)

def read(name: str) -> str:
return tmp_path.joinpath(name).read_text()
return tmp_path.joinpath(name).read_text(encoding="utf-8")

assert read("first.txt") == "1.0"

Expand Down Expand Up @@ -248,7 +252,7 @@ def __repr__(self) -> str:


def test_internal_get_version_warns_for_version_files(tmp_path: Path) -> None:
tmp_path.joinpath("PKG-INFO").write_text("Version: 0.1")
tmp_path.joinpath("PKG-INFO").write_bytes(b"Version: 0.1")
c = Configuration(root=tmp_path, fallback_root=tmp_path)
with pytest.warns(
DeprecationWarning,
Expand Down
4 changes: 2 additions & 2 deletions testing/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_dump_version_works_with_pretend(
name = "VERSION.txt"
target = tmp_path.joinpath(name)
get_version(root=tmp_path, write_to=name)
assert target.read_text() == version
assert target.read_text(encoding="utf-8") == version


def test_dump_version_modern(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
Expand All @@ -107,7 +107,7 @@ def test_dump_version_modern(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) ->
project.mkdir()

get_version(root="..", relative_to=target, version_file=name)
assert target.read_text() == version
assert target.read_text(encoding="utf-8") == version


def dump_a_version(tmp_path: Path) -> None:
Expand Down
8 changes: 5 additions & 3 deletions testing/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def test_root_relative_to(wd: WorkDir, monkeypatch: pytest.MonkeyPatch) -> None:
"""from setuptools import setup
setup(use_scm_version={"root": "../..",
"relative_to": __file__})
"""
""",
encoding="utf-8",
)
res = run([sys.executable, "setup.py", "--version"], p)
assert res.stdout == "0.1.dev0+d20090213"
Expand All @@ -86,7 +87,8 @@ def test_root_search_parent_directories(
p.joinpath("setup.py").write_text(
"""from setuptools import setup
setup(use_scm_version={"search_parent_directories": True})
"""
""",
encoding="utf-8",
)
res = run([sys.executable, "setup.py", "--version"], p)
assert res.stdout == "0.1.dev0+d20090213"
Expand Down Expand Up @@ -249,7 +251,7 @@ def test_git_version_unnormalized_setuptools(
assert res == "17.33.0rc1"

# but the version tag in the file is non-normalized (with the dash)
assert wd.cwd.joinpath("VERSION.txt").read_text() == "17.33.0-rc1"
assert wd.cwd.joinpath("VERSION.txt").read_text(encoding="utf-8") == "17.33.0-rc1"


@pytest.mark.issue(179)
Expand Down
6 changes: 4 additions & 2 deletions testing/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def test_pyproject_support(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> N
),
encoding="utf-8",
)
pkg.joinpath("setup.py").write_text("__import__('setuptools').setup()")
pkg.joinpath("setup.py").write_text(
"__import__('setuptools').setup()", encoding="utf-8"
)
res = run([sys.executable, "setup.py", "--version"], pkg)
assert res.stdout == "12.34"

Expand Down Expand Up @@ -238,7 +240,7 @@ def test_setuptools_version_keyword_ensures_regex(
"ep_name", ["setuptools_scm.parse_scm", "setuptools_scm.parse_scm_fallback"]
)
def test_git_archival_plugin_ignored(tmp_path: Path, ep_name: str) -> None:
tmp_path.joinpath(".git_archival.txt").write_text("broken")
tmp_path.joinpath(".git_archival.txt").write_text("broken", encoding="utf-8")
try:
dist = importlib.metadata.distribution("setuptools_scm_git_archive")
except importlib.metadata.PackageNotFoundError:
Expand Down
2 changes: 1 addition & 1 deletion testing/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_main() -> None:
os.path.dirname(__file__), "..", "src", "setuptools_scm", "__main__.py"
)
ns = {"__package__": "setuptools_scm"}
with open(mainfile) as f:
with open(mainfile, encoding="utf-8") as f:
code = compile(f.read(), "__main__.py", "exec")
exec(code, ns)

Expand Down
10 changes: 6 additions & 4 deletions testing/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ def test_pkginfo_noscmroot(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> N

tmp_path.joinpath(".git").mkdir()
p.joinpath("setup.py").write_text(
"from setuptools import setup;" 'setup(use_scm_version={"root": ".."})'
"from setuptools import setup;" 'setup(use_scm_version={"root": ".."})',
encoding="utf-8",
)

res = run([sys.executable, "setup.py", "--version"], p)
assert "setuptools-scm was unable to detect version for" in res.stderr
assert res.returncode == 1

p.joinpath("PKG-INFO").write_text("Version: 1.0")
p.joinpath("PKG-INFO").write_text("Version: 1.0", encoding="utf-8")
res = run([sys.executable, "setup.py", "--version"], p)
assert res.stdout == "1.0"

Expand Down Expand Up @@ -76,9 +77,10 @@ def vs(v):
return guess_next_dev_version(v)
return {"version_scheme": vs}
setup(use_scm_version=vcfg)
"""
""",
encoding="utf-8",
)
p.joinpath("PKG-INFO").write_text("Version: 1.0")
p.joinpath("PKG-INFO").write_text("Version: 1.0", encoding="utf-8")

res = run([sys.executable, "setup.py", "--version"], p)
assert res.stdout == "1.0"
Expand Down
2 changes: 1 addition & 1 deletion testing/wd_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def write(self, name: str, content: str | bytes) -> Path:
if isinstance(content, bytes):
path.write_bytes(content)
else:
path.write_text(content)
path.write_text(content, encoding="utf-8")
return path

def _reason(self, given_reason: str | None) -> str:
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ deps=
rich
build
commands=
pytest {posargs}
python -X warn_default_encoding -m pytest {posargs}



Expand Down

0 comments on commit 4e8e40a

Please sign in to comment.