Skip to content

Commit 1dc5da3

Browse files
authored
Use context managers for file writes in tests (#618)
This removes the need to explicitly close files and avoids accidental unclosed files
1 parent 142bb2b commit 1dc5da3

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

tests/test_cli.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,11 +2082,10 @@ def test_schema(tmpdir, options, expected):
20822082
def test_long_csv_column_value(tmpdir):
20832083
db_path = str(tmpdir / "test.db")
20842084
csv_path = str(tmpdir / "test.csv")
2085-
csv_file = open(csv_path, "w")
2086-
long_string = "a" * 131073
2087-
csv_file.write("id,text\n")
2088-
csv_file.write("1,{}\n".format(long_string))
2089-
csv_file.close()
2085+
with open(csv_path, "w") as csv_file:
2086+
long_string = "a" * 131073
2087+
csv_file.write("id,text\n")
2088+
csv_file.write("1,{}\n".format(long_string))
20902089
result = CliRunner().invoke(
20912090
cli.cli,
20922091
["insert", db_path, "bigtable", csv_path, "--csv"],
@@ -2110,11 +2109,10 @@ def test_long_csv_column_value(tmpdir):
21102109
def test_import_no_headers(tmpdir, args, tsv):
21112110
db_path = str(tmpdir / "test.db")
21122111
csv_path = str(tmpdir / "test.csv")
2113-
csv_file = open(csv_path, "w")
2114-
sep = "\t" if tsv else ","
2115-
csv_file.write("Cleo{sep}Dog{sep}5\n".format(sep=sep))
2116-
csv_file.write("Tracy{sep}Spider{sep}7\n".format(sep=sep))
2117-
csv_file.close()
2112+
with open(csv_path, "w") as csv_file:
2113+
sep = "\t" if tsv else ","
2114+
csv_file.write("Cleo{sep}Dog{sep}5\n".format(sep=sep))
2115+
csv_file.write("Tracy{sep}Spider{sep}7\n".format(sep=sep))
21182116
result = CliRunner().invoke(
21192117
cli.cli,
21202118
["insert", db_path, "creatures", csv_path] + args,

0 commit comments

Comments
 (0)