test: explicit encodings for open()

This commit is contained in:
Simon de Vlieger 2022-09-09 11:40:27 +02:00
parent 3703328751
commit 38d2ab685c
14 changed files with 63 additions and 61 deletions

View file

@ -29,7 +29,8 @@ class TestAssemblers(test.TestBase):
@contextlib.contextmanager
def run_assembler(self, osb, name, options, output_path):
with open(os.path.join(self.locate_test_data(),
"manifests/filesystem.json")) as f:
"manifests/filesystem.json"),
encoding="utf8") as f:
manifest = json.load(f)
manifest["pipeline"] = dict(
manifest["pipeline"],
@ -51,7 +52,7 @@ class TestAssemblers(test.TestBase):
self.assertEqual(info["virtual-size"], expected_size)
def assertFilesystem(self, device, uuid, fstype, tree):
output = subprocess.check_output(["blkid", "--output", "export", device], encoding="utf-8")
output = subprocess.check_output(["blkid", "--output", "export", device], encoding="utf8")
blkid = dict(line.split("=") for line in output.strip().split("\n"))
self.assertEqual(blkid["UUID"], uuid)
self.assertEqual(blkid["TYPE"], fstype)
@ -113,7 +114,8 @@ class TestAssemblers(test.TestBase):
def test_ostree(self):
with self.osbuild as osb:
with open(os.path.join(self.locate_test_data(),
"manifests/fedora-ostree-commit.json")) as f:
"manifests/fedora-ostree-commit.json"),
encoding="utf8") as f:
manifest = json.load(f)
data = json.dumps(manifest)
@ -122,7 +124,7 @@ class TestAssemblers(test.TestBase):
compose_file = os.path.join(output_dir, "ostree-commit", "compose.json")
repo = os.path.join(output_dir, "ostree-commit", "repo")
with open(compose_file) as f:
with open(compose_file, encoding="utf8") as f:
compose = json.load(f)
commit_id = compose["ostree-commit"]
ref = compose["ref"]
@ -146,7 +148,7 @@ class TestAssemblers(test.TestBase):
"--repo", repo,
"--print-metadata-key=rpmostree.inputhash",
commit_id
], encoding="utf-8").strip()
], encoding="utf8").strip()
self.assertEqual(md, f"'{rpmostree_inputhash}'")
md = subprocess.check_output(
@ -156,7 +158,7 @@ class TestAssemblers(test.TestBase):
"--repo", repo,
"--print-metadata-key=version",
commit_id
], encoding="utf-8").strip()
], encoding="utf8").strip()
self.assertEqual(md, f"'{os_version}'")
@unittest.skipUnless(test.TestBase.have_tree_diff(), "tree-diff missing")
@ -223,7 +225,7 @@ class TestAssemblers(test.TestBase):
"org.osbuild.tar",
options,
filename) as (tree, image):
output = subprocess.check_output(["file", "--mime-type", image], encoding="utf-8")
output = subprocess.check_output(["file", "--mime-type", image], encoding="utf8")
_, mimetype = output.strip().split(": ") # "filename: mimetype"
self.assertIn(mimetype, expected_mimetypes)
@ -240,7 +242,7 @@ class TestAssemblers(test.TestBase):
"--xattrs", "--xattrs-include", "*",
"-xaf", image,
"-C", tmp]
subprocess.check_output(args, encoding="utf-8")
subprocess.check_output(args, encoding="utf8")
diff = self.tree_diff(tree, tmp)
self.assertEqual(diff["added_files"], [])
self.assertEqual(diff["deleted_files"], [])

View file

@ -46,7 +46,7 @@ class TestBoot(test.TestBase):
"-device", "virtserialport,chardev=stdio",
qcow2],
encoding="utf-8",
encoding="utf8",
check=True)
with open(output_file, "r") as f:
with open(output_file, "r", encoding="utf8") as f:
self.assertEqual(f.read().strip(), "running")

View file

@ -77,5 +77,5 @@ def test_loopback_basic(tmpdir):
client.call("close", None)
lo = loop.Loop(minor)
with open(filename, "r") as f:
with open(filename, "r", encoding="utf8") as f:
assert not lo.is_bound_to(f.fileno())

View file

@ -35,7 +35,7 @@ libc.setns.errcheck = errcheck
@contextlib.contextmanager
def netns():
# Grab a reference to the current namespace.
with open("/proc/self/ns/net") as oldnet:
with open("/proc/self/ns/net", encoding="utf8") as oldnet:
# Create a new namespace and enter it.
libc.unshare(CLONE_NEWNET)
try:
@ -76,7 +76,7 @@ def runFileServer(barrier, directory):
def guess_type(self, path):
try:
with open(path + ".mimetype", "r") as f:
with open(path + ".mimetype", "r", encoding="utf8") as f:
return f.read().strip()
except FileNotFoundError:
pass
@ -119,7 +119,7 @@ def test_sources(source, case, tmpdir):
index = osbuild.meta.Index(os.curdir)
sources = os.path.join(test.TestBase.locate_test_data(), "sources")
with open(f"{sources}/{source}/cases/{case}") as f:
with open(f"{sources}/{source}/cases/{case}", encoding="utf8") as f:
case_options = json.load(f)
info = index.get_module_info("Source", source)

View file

@ -25,7 +25,7 @@ def have_sfdisk_with_json():
r = subprocess.run(["sfdisk", "--version"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
encoding="utf8",
check=False)
if r.returncode != 0:
@ -187,7 +187,7 @@ class TestStages(test.TestBase):
pprint.pformat(want).splitlines())
txt = "\n".join(diff)
path = f"/tmp/osbuild.metadata.{stageid}.json"
with open(path, "w") as f:
with open(path, "w", encoding="utf8") as f:
json.dump(have, f, indent=2)
self.fail(f"metadata for {stageid} differs:\n{txt}\n{path}")
@ -224,14 +224,14 @@ class TestStages(test.TestBase):
actual_diff = self.tree_diff(tree1, tree2)
with open(os.path.join(test_dir, "diff.json")) as f:
with open(os.path.join(test_dir, "diff.json"), encoding="utf8") as f:
expected_diff = json.load(f)
self.assertTreeDiffsEqual(expected_diff, actual_diff)
md_path = os.path.join(test_dir, "metadata.json")
if os.path.exists(md_path):
with open(md_path, "r") as f:
with open(md_path, "r", encoding="utf8") as f:
metadata = json.load(f)
self.assertMetadata(metadata, res)
@ -245,7 +245,7 @@ class TestStages(test.TestBase):
datadir = self.locate_test_data()
base = os.path.join(datadir, "stages/dracut")
with open(f"{base}/vanilla.json", "r") as f:
with open(f"{base}/vanilla.json", "r", encoding="utf8") as f:
refs = json.load(f)
with self.osbuild as osb, tempfile.TemporaryDirectory(dir="/var/tmp") as outdir:
@ -273,7 +273,7 @@ class TestStages(test.TestBase):
testdir = os.path.join(datadir, "stages", "selinux")
def load_manifest(manifest_name):
with open(os.path.join(datadir, f"manifests/{manifest_name}")) as f:
with open(os.path.join(datadir, f"manifests/{manifest_name}"), encoding="utf8") as f:
manifest = json.load(f)
return manifest
@ -281,7 +281,7 @@ class TestStages(test.TestBase):
for t in glob.glob(f"{testdir}/test_*.json"):
manifest = load_manifest("f34-base.json")
with open(t) as f:
with open(t, encoding="utf8") as f:
check = json.load(f)
manifest["pipeline"]["stages"].append({
"name": "org.osbuild.selinux",
@ -308,7 +308,7 @@ class TestStages(test.TestBase):
checks_path = os.path.join(testdir, "checks.json")
checks = {}
with open(checks_path) as f:
with open(checks_path, encoding="utf8") as f:
checks = json.load(f)
for image_name, test_data in checks.items():
@ -326,7 +326,7 @@ class TestStages(test.TestBase):
["qemu-img", "info", "--output=json", ip],
capture_output=True,
check=True,
encoding="utf-8"
encoding="utf8"
)
qemu_img_out = json.loads(qemu_img_run.stdout)
@ -371,7 +371,7 @@ class TestStages(test.TestBase):
imgname = "disk.img"
with open(os.path.join(testdir, f"{imgname}.json"), "r") as f:
with open(os.path.join(testdir, f"{imgname}.json"), "r", encoding="utf8") as f:
want = json.load(f)
with self.osbuild as osb, tempfile.TemporaryDirectory(dir="/var/tmp") as outdir:
@ -388,7 +388,7 @@ class TestStages(test.TestBase):
r = subprocess.run(["sfdisk", "--json", target],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
encoding="utf8",
check=False)
have = json.loads(r.stdout)
@ -419,7 +419,7 @@ class TestStages(test.TestBase):
imgname = "disk.img"
with open(os.path.join(testdir, f"{imgname}.json"), "r") as f:
with open(os.path.join(testdir, f"{imgname}.json"), "r", encoding="utf8") as f:
want = json.load(f)
with self.osbuild as osb, tempfile.TemporaryDirectory(dir="/var/tmp") as outdir:
@ -436,7 +436,7 @@ class TestStages(test.TestBase):
r = subprocess.run(["sfdisk", "--json", target],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
encoding="utf8",
check=False)
have = json.loads(r.stdout)