Skip to content

Commit

Permalink
fix: include encoding for consistent behavior (default in Python 3.15…
Browse files Browse the repository at this point in the history
…+) (#891)

* fix: include encoding for consistent behavior (default in Python 3.15+)

Signed-off-by: Henry Schreiner <[email protected]>

* Apply suggestions from code review

---------

Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii authored Nov 23, 2024
1 parent a844fbf commit aa09595
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 18 deletions.
2 changes: 1 addition & 1 deletion nox/tox_to_nox.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def main() -> None:
args = parser.parse_args()

if TOX4:
output = check_output(["tox", "config"], text=True) # noqa: S607
output = check_output(["tox", "config"], text=True, encoding="utf-8") # noqa: S607
original_config = ConfigParser()
original_config.read_string(output)
config: dict[str, dict[str, Any]] = {}
Expand Down
1 change: 1 addition & 0 deletions nox/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def uv_version() -> version.Version:
check=False,
text=True,
capture_output=True,
encoding="utf-8",
)
except FileNotFoundError:
logger.info("uv binary not found.")
Expand Down
2 changes: 2 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def tests(session: nox.Session, tox_version: str) -> None:
session.install("-e.[tox_to_nox]")
if tox_version != "latest":
session.install(f"tox{tox_version}")
extra_env = {"PYTHONWARNDEFAULTENCODING": "1"} if tox_version == "latest" else {}
session.run(
"pytest",
"--cov",
Expand All @@ -69,6 +70,7 @@ def tests(session: nox.Session, tox_version: str) -> None:
*session.posargs,
env={
"COVERAGE_FILE": coverage_file,
**extra_env,
},
)

Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def generate_noxfile(**option_mapping: str | bool) -> str:
text = re.sub(rf"(# )?nox.options.{opt}", f"nox.options.{opt}", text)
text = Template(text).safe_substitute(**option_mapping)
path = tmp_path / "noxfile.py"
path.write_text(text)
path.write_text(text, encoding="utf8")
return str(path)

return generate_noxfile
2 changes: 1 addition & 1 deletion tests/test__version.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
def temp_noxfile(tmp_path: Path) -> Callable[[str], str]:
def make_temp_noxfile(content: str) -> str:
path = tmp_path / "noxfile.py"
path.write_text(content)
path.write_text(content, encoding="utf8")
return str(path)

return make_temp_noxfile
Expand Down
4 changes: 2 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,14 +694,14 @@ def generate_noxfile(
default_session: str, default_python: str, alternate_python: str
) -> str:
path = Path(RESOURCES) / "noxfile_options_pythons.py"
text = path.read_text()
text = path.read_text(encoding="utf-8")
text = text.format(
default_session=default_session,
default_python=default_python,
alternate_python=alternate_python,
)
path = tmp_path / "noxfile.py"
path.write_text(text)
path.write_text(text, encoding="utf-8")
return str(path)

return generate_noxfile
Expand Down
4 changes: 2 additions & 2 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def test_load_nox_module_needs_version_static(tmp_path: Path) -> None:
"""
)
noxfile = tmp_path / "noxfile.py"
noxfile.write_text(text)
noxfile.write_text(text, encoding="utf-8")
config = _options.options.namespace(noxfile=str(noxfile))
assert tasks.load_nox_module(config) == 2

Expand All @@ -162,7 +162,7 @@ def test_load_nox_module_needs_version_dynamic(tmp_path: Path) -> None:
"""
)
noxfile = tmp_path / "noxfile.py"
noxfile.write_text(text)
noxfile.write_text(text, encoding="utf-8")
config = _options.options.namespace(noxfile=str(noxfile))
tasks.load_nox_module(config)
# Dynamic version requirements are not checked.
Expand Down
12 changes: 8 additions & 4 deletions tests/test_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def test_load_pyproject(tmp_path: Path) -> None:
name = "hi"
version = "1.0"
dependencies = ["numpy", "requests"]
"""
""",
encoding="utf-8",
)

toml = nox.project.load_toml(filepath)
Expand Down Expand Up @@ -43,7 +44,8 @@ def test_load_script_block(tmp_path: Path, example: str) -> None:
data = resp.json()
pprint([(k, v["title"]) for k, v in data.items()][:10])
"""
)
),
encoding="utf-8",
)

toml = nox.project.load_toml(filepath)
Expand All @@ -64,7 +66,8 @@ def test_load_no_script_block(tmp_path: Path) -> None:
data = resp.json()
pprint([(k, v["title"]) for k, v in data.items()][:10])
"""
)
),
encoding="utf-8",
)

with pytest.raises(ValueError, match="No script block found"):
Expand Down Expand Up @@ -94,7 +97,8 @@ def test_load_multiple_script_block(tmp_path: Path) -> None:
data = resp.json()
pprint([(k, v["title"]) for k, v in data.items()][:10])
"""
)
),
encoding="utf-8",
)

with pytest.raises(ValueError, match="Multiple script blocks found"):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_tox_to_nox.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
@pytest.fixture
def makeconfig(tmpdir: LEGACY_PATH) -> Callable[[str], str]:
def makeconfig(toxini_content: str) -> str:
tmpdir.join("tox.ini").write(toxini_content)
tmpdir.join("tox.ini").write_text(toxini_content, encoding="utf8")
old = tmpdir.chdir()
try:
sys.argv = [sys.executable]
tox_to_nox.main()
return tmpdir.join("noxfile.py").read() # type: ignore[no-any-return]
return tmpdir.join("noxfile.py").read_text(encoding="utf8") # type: ignore[no-any-return]
finally:
old.chdir()

Expand Down
15 changes: 10 additions & 5 deletions tests/test_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def test_create_reuse_stale_venv_environment(
version = 3.9.6
uv = 0.1.9
"""
location.join("pyvenv.cfg").write(dedent(pyvenv_cfg))
location.join("pyvenv.cfg").write_text(dedent(pyvenv_cfg), encoding="utf-8")

reused = not venv.create()

Expand Down Expand Up @@ -670,7 +670,7 @@ def test_create_reuse_stale_virtualenv_environment(
base-exec-prefix = /usr
base-executable = /usr/bin/python3.9
"""
location.join("pyvenv.cfg").write(dedent(pyvenv_cfg))
location.join("pyvenv.cfg").write_text(dedent(pyvenv_cfg), encoding="utf-8")

reused = not venv.create()

Expand All @@ -688,7 +688,9 @@ def test_create_reuse_uv_environment(

# Place a spurious occurrence of "uv" in the pyvenv.cfg.
pyvenv_cfg = location.join("pyvenv.cfg")
pyvenv_cfg.write(pyvenv_cfg.read() + "bogus = uv\n")
pyvenv_cfg.write_text(
pyvenv_cfg.read_text(encoding="utf-8") + "bogus = uv\n", encoding="utf-8"
)

reused = not venv.create()

Expand Down Expand Up @@ -790,7 +792,10 @@ def test_create_reuse_venv_environment(

# Place a spurious occurrence of "virtualenv" in the pyvenv.cfg.
pyvenv_cfg = location.join("pyvenv.cfg")
pyvenv_cfg.write(pyvenv_cfg.read() + "bogus = virtualenv\n")
pyvenv_cfg.write_text(
pyvenv_cfg.read_text(encoding="utf-8") + "bogus = virtualenv\n",
encoding="utf-8",
)

reused = not venv.create()

Expand Down Expand Up @@ -834,7 +839,7 @@ def test_inner_functions_reusing_venv(
version = 3.10
base-prefix = foo
"""
location.join("pyvenv.cfg").write(dedent(pyvenv_cfg))
location.join("pyvenv.cfg").write_text(dedent(pyvenv_cfg), encoding="utf-8")

config = venv._read_pyvenv_cfg()
assert config
Expand Down

0 comments on commit aa09595

Please sign in to comment.