Skip to content

Commit 668ac9e

Browse files
authored
Fix encoding warnings (#92)
1 parent a9788e9 commit 668ac9e

File tree

7 files changed

+31
-17
lines changed

7 files changed

+31
-17
lines changed

.github/workflows/docs.yml

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ on:
55
branches:
66
- main
77

8+
env:
9+
FORCE_COLOR: "1"
10+
811
jobs:
912
build:
1013
runs-on: ubuntu-latest

.github/workflows/lint.yml

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
- main
77
pull_request:
88

9+
env:
10+
FORCE_COLOR: "1"
11+
912
jobs:
1013
lint:
1114
runs-on: ubuntu-latest

.github/workflows/release.yml

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ name: release
77

88
permissions: {}
99

10+
env:
11+
FORCE_COLOR: "1"
12+
1013
jobs:
1114
build:
1215
name: Build distributions 📦

.github/workflows/tests.yml

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ on:
66
- main
77
pull_request:
88

9+
env:
10+
FORCE_COLOR: "1"
11+
PYTHONDEVMODE: "1" # -X dev
12+
PYTHONWARNDEFAULTENCODING: "1" # -X warn_default_encoding
13+
914
jobs:
1015
test:
1116
strategy:

src/pypi_attestations/_cli.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ def _inspect(args: argparse.Namespace) -> None:
393393
_validate_files(args.files, should_exist=True)
394394
for file_path in args.files:
395395
try:
396-
attestation = Attestation.model_validate_json(file_path.read_text())
396+
attestation = Attestation.model_validate_json(file_path.read_bytes())
397397
except ValidationError as validation_error:
398398
_die(f"Invalid attestation ({file_path}): {validation_error}")
399399

@@ -459,7 +459,7 @@ def _verify_attestation(args: argparse.Namespace) -> None:
459459
for file_path, attestations in files_with_attestations.items():
460460
for attestation_path in attestations:
461461
try:
462-
attestation = Attestation.model_validate_json(attestation_path.read_text())
462+
attestation = Attestation.model_validate_json(attestation_path.read_bytes())
463463
except ValidationError as validation_error:
464464
_die(f"Invalid attestation ({attestation_path}): {validation_error}")
465465

test/test_cli.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def test_sign_command(tmp_path: Path) -> None:
100100
copied_artifact_attestation = Path(f"{copied_artifact}.publish.attestation")
101101
assert copied_artifact_attestation.is_file()
102102

103-
attestation = Attestation.model_validate_json(copied_artifact_attestation.read_text())
103+
attestation = Attestation.model_validate_json(copied_artifact_attestation.read_bytes())
104104
assert attestation.version
105105

106106

@@ -544,7 +544,7 @@ def test_verify_pypi_validation_fails(
544544
) -> None:
545545
# Replace the actual wheel with another file
546546
def _download_file(url: str, dest: Path) -> None:
547-
with open(dest, "w") as f:
547+
with open(dest, "w", encoding="utf-8") as f:
548548
f.write("random wheel file")
549549

550550
monkeypatch.setattr(pypi_attestations._cli, "_download_file", _download_file)

test/test_impl.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def test_verify_from_gitlab_publisher(self) -> None:
156156
workflow_filepath=".gitlab-ci.yml",
157157
)
158158

159-
attestation = impl.Attestation.model_validate_json(gl_attestation_path.read_text())
159+
attestation = impl.Attestation.model_validate_json(gl_attestation_path.read_bytes())
160160
predicate_type, predicate = attestation.verify(publisher, gl_signed_dist)
161161
assert predicate_type == "https://docs.pypi.org/attestations/publish/v1"
162162
assert predicate is None
@@ -179,7 +179,7 @@ def test_verify_from_gitlab_publisher_wrong(self) -> None:
179179
workflow_filepath="wrong.yml",
180180
)
181181

182-
attestation = impl.Attestation.model_validate_json(gl_attestation_path.read_text())
182+
attestation = impl.Attestation.model_validate_json(gl_attestation_path.read_bytes())
183183
with pytest.raises(impl.VerificationError, match=r"Build Config URI .+ does not match"):
184184
attestation.verify(publisher, gl_signed_dist)
185185

@@ -189,7 +189,7 @@ def test_verify(self) -> None:
189189
identity="[email protected]", issuer="https://github.com/login/oauth"
190190
)
191191

192-
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_text())
192+
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_bytes())
193193
predicate_type, predicate = attestation.verify(pol, dist, staging=True)
194194

195195
assert attestation.statement["_type"] == "https://in-toto.io/Statement/v1"
@@ -210,7 +210,7 @@ def test_verify_digest_mismatch(self, tmp_path: Path) -> None:
210210
identity="[email protected]", issuer="https://github.com/login/oauth"
211211
)
212212

213-
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_text())
213+
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_bytes())
214214

215215
modified_dist_path = tmp_path / dist_path.name
216216
modified_dist_path.write_bytes(b"nothing")
@@ -229,7 +229,7 @@ def test_verify_filename_mismatch(self, tmp_path: Path) -> None:
229229
identity="[email protected]", issuer="https://github.com/login/oauth"
230230
)
231231

232-
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_text())
232+
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_bytes())
233233

234234
modified_dist_path = tmp_path / "wrong_name-0.1.2-py3-none-any.whl"
235235
modified_dist_path.write_bytes(dist_path.read_bytes())
@@ -246,7 +246,7 @@ def test_verify_policy_mismatch(self) -> None:
246246
# Wrong identity.
247247
pol = policy.Identity(identity="[email protected]", issuer="https://github.com/login/oauth")
248248

249-
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_text())
249+
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_bytes())
250250

251251
with pytest.raises(impl.VerificationError, match=r"Certificate's SANs do not match"):
252252
attestation.verify(pol, dist, staging=True)
@@ -260,7 +260,7 @@ def test_verify_wrong_envelope(self, monkeypatch: pytest.MonkeyPatch) -> None:
260260
monkeypatch.setattr(impl.Verifier, "staging", staging)
261261
pol = pretend.stub()
262262

263-
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_text())
263+
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_bytes())
264264

265265
with pytest.raises(impl.VerificationError, match="expected JSON envelope, got fake-type"):
266266
attestation.verify(pol, dist, staging=True)
@@ -276,7 +276,7 @@ def test_verify_bad_payload(self, monkeypatch: pytest.MonkeyPatch) -> None:
276276
monkeypatch.setattr(impl.Verifier, "staging", staging)
277277
pol = pretend.stub()
278278

279-
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_text())
279+
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_bytes())
280280

281281
with pytest.raises(impl.VerificationError, match="invalid statement"):
282282
attestation.verify(pol, dist, staging=True)
@@ -308,7 +308,7 @@ def test_verify_too_many_subjects(self, monkeypatch: pytest.MonkeyPatch) -> None
308308
monkeypatch.setattr(impl.Verifier, "staging", staging)
309309
pol = pretend.stub()
310310

311-
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_text())
311+
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_bytes())
312312

313313
with pytest.raises(impl.VerificationError, match="too many subjects in statement"):
314314
attestation.verify(pol, dist, staging=True)
@@ -339,7 +339,7 @@ def test_verify_subject_missing_name(self, monkeypatch: pytest.MonkeyPatch) -> N
339339
monkeypatch.setattr(impl.Verifier, "staging", staging)
340340
pol = pretend.stub()
341341

342-
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_text())
342+
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_bytes())
343343

344344
with pytest.raises(impl.VerificationError, match="invalid subject: missing name"):
345345
attestation.verify(pol, dist, staging=True)
@@ -373,7 +373,7 @@ def test_verify_subject_invalid_name(self, monkeypatch: pytest.MonkeyPatch) -> N
373373
monkeypatch.setattr(impl.Verifier, "staging", staging)
374374
pol = pretend.stub()
375375

376-
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_text())
376+
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_bytes())
377377

378378
with pytest.raises(impl.VerificationError, match="invalid subject: Invalid wheel filename"):
379379
attestation.verify(pol, dist, staging=True)
@@ -413,14 +413,14 @@ def test_verify_unknown_attestation_type(self, monkeypatch: pytest.MonkeyPatch)
413413
monkeypatch.setattr(impl.Verifier, "staging", staging)
414414
pol = pretend.stub()
415415

416-
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_text())
416+
attestation = impl.Attestation.model_validate_json(dist_attestation_path.read_bytes())
417417

418418
with pytest.raises(impl.VerificationError, match="unknown attestation type: foo"):
419419
attestation.verify(pol, dist, staging=True)
420420

421421
def test_certificate_claims(self) -> None:
422422
attestation = impl.Attestation.model_validate_json(
423-
pypi_attestations_attestation.read_text()
423+
pypi_attestations_attestation.read_bytes()
424424
)
425425

426426
results = {

0 commit comments

Comments
 (0)