summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-07-19 13:21:08 +0300
committerLars Wirzenius <liw@liw.fi>2021-07-19 13:21:08 +0300
commitbf92322f7eafdae5b7ae5a8da4486a277d01307d (patch)
treecc33ffc039c999ef5c15d24b54a13391477a38db
parent891e1155495e5c42beafa6cfd9b518c804e9978c (diff)
downloadobnam2-bf92322f7eafdae5b7ae5a8da4486a277d01307d.tar.gz
test: use better way to write files in Python
"open(...).write(...)" does not necessarily close the file, and thus flush buffered writes to disk. "with open(...)" does. Sponsored-by: author
-rwxr-xr-xlist_new_release_tags3
-rw-r--r--subplot/data.py9
2 files changed, 8 insertions, 4 deletions
diff --git a/list_new_release_tags b/list_new_release_tags
index 90994ac..7b82050 100755
--- a/list_new_release_tags
+++ b/list_new_release_tags
@@ -41,7 +41,8 @@ def built_tags(filename):
def save_built_tags(filename, tags):
- return open(filename, "w").write("".join(f"{tag}\n" for tag in tags))
+ with open(filename, "w") as f:
+ f.write("".join(f"{tag}\n" for tag in tags))
tags_filename = sys.argv[1]
diff --git a/subplot/data.py b/subplot/data.py
index 2e2802e..583e52d 100644
--- a/subplot/data.py
+++ b/subplot/data.py
@@ -11,7 +11,8 @@ def create_file_with_given_data(ctx, filename=None, data=None):
logging.debug(f"creating file {filename} with {data!r}")
dirname = os.path.dirname(filename) or "."
os.makedirs(dirname, exist_ok=True)
- open(filename, "wb").write(data.encode("UTF-8"))
+ with open(filename, "wb") as f:
+ f.write(data.encode("UTF-8"))
def create_file_with_random_data(ctx, filename=None):
@@ -33,7 +34,8 @@ def create_cachedir_tag_in(ctx, dirpath=None):
filepath = f"{dirpath}/CACHEDIR.TAG"
logging.debug(f"creating {filepath}")
os.makedirs(dirpath, exist_ok=True)
- open(filepath, "w").write("Signature: 8a477f597d28d172789f06886806bc55")
+ with open(filepath, "w") as f:
+ f.write("Signature: 8a477f597d28d172789f06886806bc55")
def create_nonutf8_filename(ctx, dirname=None):
@@ -69,7 +71,8 @@ def _create_manifest_of_directory(ctx, dirname=None, manifest=None):
runcmd_run(ctx, ["find", "-exec", "summain", "{}", "+"], cwd=dirname)
assert runcmd_get_exit_code(ctx) == 0
stdout = runcmd_get_stdout(ctx)
- open(manifest, "w").write(stdout)
+ with open(manifest, "w") as f:
+ f.write(stdout)
def file_is_restored(ctx, filename=None, restored=None):