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

@ -55,14 +55,14 @@ def test_runner_fail(tempdir):
logfile = os.path.join(tempdir, "log.txt")
with BuildRoot("/", runner, libdir, var) as root, \
open(logfile, "w") as log:
open(logfile, "w", encoding="utf8") as log:
monitor = LogMonitor(log.fileno())
r = root.run(["/usr/bin/true"], monitor)
assert r.returncode == 1
with open(logfile) as f:
with open(logfile, encoding="utf8") as f:
log = f.read()
assert log
assert r.output
@ -208,7 +208,7 @@ def test_env_isolation(tempdir):
r = root.run(cmd, monitor, binds=[f"{ipc}:/ipc"])
assert r.returncode == 0
with open(os.path.join(ipc, "env.txt")) as f:
with open(os.path.join(ipc, "env.txt"), encoding="utf8") as f:
data = f.read().strip()
assert data
have = dict(map(lambda x: x.split("=", 1), data.split("\n")))
@ -247,7 +247,7 @@ def test_caps(tempdir):
r = root.run(cmd, monitor, binds=[f"{ipc}:/ipc"])
assert r.returncode == 0
with open(os.path.join(ipc, "status"), encoding="utf-8") as f:
with open(os.path.join(ipc, "status"), encoding="utf8") as f:
data = f.readlines()
assert data

View file

@ -67,13 +67,13 @@ class TestMonitor(unittest.TestCase):
logfile = os.path.join(tmpdir, "log.txt")
with open(logfile, "w") as log, ObjectStore(storedir) as store:
with open(logfile, "w", encoding="utf8") as log, ObjectStore(storedir) as store:
monitor = LogMonitor(log.fileno())
res = pipeline.run(store,
monitor,
libdir=os.path.abspath(os.curdir))
with open(logfile) as f:
with open(logfile, encoding="utf8") as f:
log = f.read()
assert res

View file

@ -122,7 +122,7 @@ class TestObjectStore(unittest.TestCase):
with object_store.new() as tree:
path = tree.write()
with tree.write() as path, \
open(os.path.join(path, "data"), "w") as f:
open(os.path.join(path, "data"), "w", encoding="utf8") as f:
f.write(data)
st = os.fstat(f.fileno())
data_inode = st.st_ino
@ -134,7 +134,7 @@ class TestObjectStore(unittest.TestCase):
# check that "data" is still the very
# same file after committing
with tree.read() as path:
with open(os.path.join(path, "data"), "r") as f:
with open(os.path.join(path, "data"), "r", encoding="utf8") as f:
st = os.fstat(f.fileno())
self.assertEqual(st.st_ino, data_inode)
data_read = f.read()
@ -147,7 +147,7 @@ class TestObjectStore(unittest.TestCase):
with object_store.new(base_id="x") as tree:
self.assertEqual(tree.base, "x")
with tree.read() as path:
with open(os.path.join(path, "data"), "r") as f:
with open(os.path.join(path, "data"), "r", encoding="utf8") as f:
# copy-on-write: since we have not written
# to the tree yet, "data" should be the
# very same file as that one of object "x"
@ -303,7 +303,7 @@ class TestObjectStore(unittest.TestCase):
assert Path(path) == mountpoint
filepath = Path(mountpoint, "file.txt")
assert filepath.exists()
txt = filepath.read_text()
txt = filepath.read_text(encoding="utf8")
assert txt == "osbuild"
# check we can mount subtrees via `read_tree_at`
@ -314,7 +314,7 @@ class TestObjectStore(unittest.TestCase):
path = client.read_tree_at("42", filemount, "/file.txt")
filepath = Path(path)
assert filepath.is_file()
txt = filepath.read_text()
txt = filepath.read_text(encoding="utf8")
assert txt == "osbuild"
dirmount = Path(tmpdir, "dir")

View file

@ -26,7 +26,7 @@ def test_ioctl_get_immutable(tmpdir):
# as intended.
#
with open(f"{tmpdir}/immutable", "x") as f:
with open(f"{tmpdir}/immutable", "x", encoding="utf8") as f:
assert not linux.ioctl_get_immutable(f.fileno())
@ -37,7 +37,7 @@ def test_ioctl_toggle_immutable(tmpdir):
# as intended.
#
with open(f"{tmpdir}/immutable", "x") as f:
with open(f"{tmpdir}/immutable", "x", encoding="utf8") as f:
# Check the file is mutable by default and if we clear it again.
assert not linux.ioctl_get_immutable(f.fileno())
linux.ioctl_toggle_immutable(f.fileno(), False)

View file

@ -42,13 +42,13 @@ class TestUtilLorax(test.TestBase):
os.makedirs(root)
os.makedirs(tree)
with open(os.path.join(root, "hello.txt"), "w") as f:
with open(os.path.join(root, "hello.txt"), "w", encoding="utf8") as f:
f.write("Hello World\n")
self.assertExists(root, "hello.txt")
template = os.path.join(tmp, "template.tmpl")
with open(os.path.join(tmp, template), "w") as f:
with open(os.path.join(tmp, template), "w", encoding="utf8") as f:
f.write(BASIC_TEMPLATE)
# parse the template and render it
@ -76,7 +76,7 @@ class TestUtilLorax(test.TestBase):
self.assertExists(tree, "foo.txt")
for fn in ["a.txt", "b.txt"]:
with open(os.path.join(tree, fn), "r") as f:
with open(os.path.join(tree, fn), "r", encoding="utf8") as f:
data = f.read().strip()
self.assertEqual(data, "osbuild-42")

View file

@ -24,7 +24,7 @@ def have_lvm() -> bool:
try:
r = subprocess.run(
["vgs"],
encoding="utf-8",
encoding="utf8",
stdout=subprocess.PIPE,
check=False
)

View file

@ -15,7 +15,7 @@ from osbuild.util import ostree
from .. import test
def run(*args, check=True, encoding="utf-8", **kwargs):
def run(*args, check=True, encoding="utf8", **kwargs):
res = subprocess.run(*args,
encoding=encoding,
check=check,
@ -56,7 +56,7 @@ class TestObjectStore(test.TestBase):
tf["ref"] = test_ref
with tf.as_tmp_file() as path:
with open(path, "r") as f:
with open(path, "r", encoding="utf8") as f:
js = json.load(f)
self.assertEqual(js["ref"], test_ref)
self.assertEqual(tf["ref"], test_ref)
@ -118,16 +118,16 @@ class TestPasswdLike(unittest.TestCase):
"lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\n",
"sync:x:5:0:sync:/sbin:/bin/sync\n"
]
with open(os.path.join(tmpdir, "primary"), "w") as f:
with open(os.path.join(tmpdir, "primary"), "w", encoding="utf8") as f:
f.writelines(primary_file_lines)
with open(os.path.join(tmpdir, "secondary"), "w") as f:
with open(os.path.join(tmpdir, "secondary"), "w", encoding="utf8") as f:
f.writelines(secondary_file_lines)
passwd = ostree.PasswdLike.from_file(os.path.join(tmpdir, "primary"))
passwd.merge_with_file(os.path.join(tmpdir, "secondary"))
passwd.dump_to_file(os.path.join(tmpdir, "result"))
with open(os.path.join(tmpdir, "result"), "r") as f:
with open(os.path.join(tmpdir, "result"), "r", encoding="utf8") as f:
self.assertEqual(sorted(f.readlines()), sorted(result_file_lines))
def test_merge_group(self):
@ -145,16 +145,16 @@ class TestPasswdLike(unittest.TestCase):
"bin:x:1:\n",
"daemon:x:2:\n"
]
with open(os.path.join(tmpdir, "primary"), "w") as f:
with open(os.path.join(tmpdir, "primary"), "w", encoding="utf8") as f:
f.writelines(primary_file_lines)
with open(os.path.join(tmpdir, "secondary"), "w") as f:
with open(os.path.join(tmpdir, "secondary"), "w", encoding="utf8") as f:
f.writelines(secondary_file_lines)
passwd = ostree.PasswdLike.from_file(os.path.join(tmpdir, "primary"))
passwd.merge_with_file(os.path.join(tmpdir, "secondary"))
passwd.dump_to_file(os.path.join(tmpdir, "result"))
with open(os.path.join(tmpdir, "result"), "r") as f:
with open(os.path.join(tmpdir, "result"), "r", encoding="utf8") as f:
self.assertEqual(sorted(f.readlines()), sorted(result_file_lines))
#pylint: disable=no-self-use