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
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Nov 12, 2024
1 parent 7751763 commit 9e9175b
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion nox/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _parse_needs_version(source: str, filename: str = "<unknown>") -> str | None

def _read_needs_version(filename: str) -> str | None:
"""Read ``nox.needs_version`` from the user's Noxfile."""
with open(filename) as io:
with open(filename, encoding="utf-8") as io:
source = io.read()

return _parse_needs_version(source, filename=filename)
Expand Down
2 changes: 1 addition & 1 deletion nox/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def create_report(
return results

# Write the JSON report.
with open(global_config.report, "w") as report_file:
with open(global_config.report, "w", encoding="utf-8") as report_file:
json.dump(
{
"result": int(all(results)),
Expand Down
4 changes: 2 additions & 2 deletions nox/tox_to_nox.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def fixname(envname: str) -> str:

def write_output_to_file(output: str, filename: str) -> None:
"""Write output to a file."""
with open(filename, "w") as outfile:
with open(filename, "w", encoding="utf-8") as outfile:
outfile.write(output)


Expand All @@ -74,7 +74,7 @@ def main() -> None:
args = parser.parse_args()

if TOX4:
output = check_output(["tox", "config"], text=True)
output = check_output(["tox", "config"], text=True, encoding="utf-8")
original_config = ConfigParser()
original_config.read_string(output)
config: dict[str, dict[str, Any]] = {}
Expand Down
3 changes: 2 additions & 1 deletion nox/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,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 Expand Up @@ -452,7 +453,7 @@ def _clean_location(self) -> bool:
def _read_pyvenv_cfg(self) -> dict[str, str] | None:
"""Read a pyvenv.cfg file into dict, returns None if missing."""
path = os.path.join(self.location, "pyvenv.cfg")
with contextlib.suppress(FileNotFoundError), open(path) as fp:
with contextlib.suppress(FileNotFoundError), open(path, encoding="utf-8") as fp:
parts = (x.partition("=") for x in fp if "=" in x)
return {k.strip(): v.strip() for k, _, v in parts}
return None
Expand Down
1 change: 1 addition & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def tests(session: nox.Session, tox_version: str) -> None:
*session.posargs,
env={
"COVERAGE_FILE": coverage_file,
"PYTHONWARNDEFAULTENCODING": "1",
},
)

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 @@ -33,7 +33,7 @@
def temp_noxfile(tmp_path: Path):
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 @@ -639,14 +639,14 @@ def generate_noxfile_options_pythons(tmp_path):

def generate_noxfile(default_session, default_python, alternate_python):
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
6 changes: 3 additions & 3 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_load_nox_module_needs_version_static(reset_needs_version, tmp_path):
"""
)
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 @@ -138,7 +138,7 @@ def test_load_nox_module_needs_version_dynamic(reset_needs_version, tmp_path):
"""
)
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 Expand Up @@ -705,7 +705,7 @@ def test_create_report():
mock.ANY,
indent=2,
)
open_.assert_called_once_with("/path/to/report", "w")
open_.assert_called_once_with("/path/to/report", "w", encoding="utf-8")


def test_final_reduce():
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 @@ -29,12 +29,12 @@
@pytest.fixture
def makeconfig(tmpdir):
def makeconfig(toxini_content):
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()
return tmpdir.join("noxfile.py").read_text(encoding="utf8")
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 @@ -441,7 +441,7 @@ def test_create_reuse_stale_venv_environment(make_one):
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 @@ -574,7 +574,7 @@ def test_create_reuse_stale_virtualenv_environment(make_one, monkeypatch):
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 @@ -590,7 +590,9 @@ def test_create_reuse_uv_environment(make_one):

# 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 @@ -678,7 +680,10 @@ def test_create_reuse_venv_environment(make_one, monkeypatch):

# 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 @@ -717,7 +722,7 @@ def test_inner_functions_reusing_venv(make_one, monkeypatch):
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")

base_prefix = venv._read_pyvenv_cfg()["base-prefix"]
assert base_prefix == "foo"
Expand Down

0 comments on commit 9e9175b

Please sign in to comment.