[qa] fix unclosed files problems in tests.
This commit is contained in:
@ -19,7 +19,8 @@ def test_write_csv_to_file(tmpdir):
|
|||||||
context.step()
|
context.step()
|
||||||
context.stop()
|
context.stop()
|
||||||
|
|
||||||
assert fs.open(filename).read() == 'foo\nbar\nbaz\n'
|
with fs.open(filename)as fp:
|
||||||
|
assert fp.read() == 'foo\nbar\nbaz\n'
|
||||||
|
|
||||||
with pytest.raises(AttributeError):
|
with pytest.raises(AttributeError):
|
||||||
getattr(context, 'file')
|
getattr(context, 'file')
|
||||||
@ -27,7 +28,8 @@ def test_write_csv_to_file(tmpdir):
|
|||||||
|
|
||||||
def test_read_csv_from_file(tmpdir):
|
def test_read_csv_from_file(tmpdir):
|
||||||
fs, filename = open_fs(tmpdir), 'input.csv'
|
fs, filename = open_fs(tmpdir), 'input.csv'
|
||||||
fs.open(filename, 'w').write('a,b,c\na foo,b foo,c foo\na bar,b bar,c bar')
|
with fs.open(filename, 'w') as fp:
|
||||||
|
fp.write('a,b,c\na foo,b foo,c foo\na bar,b bar,c bar')
|
||||||
|
|
||||||
reader = CsvReader(path=filename, delimiter=',')
|
reader = CsvReader(path=filename, delimiter=',')
|
||||||
|
|
||||||
@ -59,7 +61,8 @@ def test_read_csv_from_file(tmpdir):
|
|||||||
|
|
||||||
def test_read_csv_kwargs_output_formater(tmpdir):
|
def test_read_csv_kwargs_output_formater(tmpdir):
|
||||||
fs, filename = open_fs(tmpdir), 'input.csv'
|
fs, filename = open_fs(tmpdir), 'input.csv'
|
||||||
fs.open(filename, 'w').write('a,b,c\na foo,b foo,c foo\na bar,b bar,c bar')
|
with fs.open(filename, 'w') as fp:
|
||||||
|
fp.write('a,b,c\na foo,b foo,c foo\na bar,b bar,c bar')
|
||||||
|
|
||||||
reader = CsvReader(path=filename, delimiter=',', output_format='kwargs')
|
reader = CsvReader(path=filename, delimiter=',', output_format='kwargs')
|
||||||
|
|
||||||
|
|||||||
@ -25,7 +25,8 @@ def test_file_writer_in_context(tmpdir, lines, output):
|
|||||||
context.step()
|
context.step()
|
||||||
context.stop()
|
context.stop()
|
||||||
|
|
||||||
assert fs.open(filename).read() == output
|
with fs.open(filename) as fp:
|
||||||
|
assert fp.read() == output
|
||||||
|
|
||||||
|
|
||||||
def test_file_writer_out_of_context(tmpdir):
|
def test_file_writer_out_of_context(tmpdir):
|
||||||
@ -36,13 +37,15 @@ def test_file_writer_out_of_context(tmpdir):
|
|||||||
with writer.open(fs) as fp:
|
with writer.open(fs) as fp:
|
||||||
fp.write('Yosh!')
|
fp.write('Yosh!')
|
||||||
|
|
||||||
assert fs.open(filename).read() == 'Yosh!'
|
with fs.open(filename) as fp:
|
||||||
|
assert fp.read() == 'Yosh!'
|
||||||
|
|
||||||
|
|
||||||
def test_file_reader_in_context(tmpdir):
|
def test_file_reader_in_context(tmpdir):
|
||||||
fs, filename = open_fs(tmpdir), 'input.txt'
|
fs, filename = open_fs(tmpdir), 'input.txt'
|
||||||
|
|
||||||
fs.open(filename, 'w').write('Hello\nWorld\n')
|
with fs.open(filename, 'w') as fp:
|
||||||
|
fp.write('Hello\nWorld\n')
|
||||||
|
|
||||||
reader = FileReader(path=filename)
|
reader = FileReader(path=filename)
|
||||||
context = CapturingNodeExecutionContext(reader, services={'fs': fs})
|
context = CapturingNodeExecutionContext(reader, services={'fs': fs})
|
||||||
|
|||||||
@ -17,7 +17,8 @@ def test_write_json_to_file(tmpdir):
|
|||||||
context.step()
|
context.step()
|
||||||
context.stop()
|
context.stop()
|
||||||
|
|
||||||
assert fs.open(filename).read() == '[{"foo": "bar"}]'
|
with fs.open(filename) as fp:
|
||||||
|
assert fp.read() == '[{"foo": "bar"}]'
|
||||||
|
|
||||||
with pytest.raises(AttributeError):
|
with pytest.raises(AttributeError):
|
||||||
getattr(context, 'file')
|
getattr(context, 'file')
|
||||||
@ -28,7 +29,8 @@ def test_write_json_to_file(tmpdir):
|
|||||||
|
|
||||||
def test_read_json_from_file(tmpdir):
|
def test_read_json_from_file(tmpdir):
|
||||||
fs, filename = open_fs(tmpdir), 'input.json'
|
fs, filename = open_fs(tmpdir), 'input.json'
|
||||||
fs.open(filename, 'w').write('[{"x": "foo"},{"x": "bar"}]')
|
with fs.open(filename, 'w') as fp:
|
||||||
|
fp.write('[{"x": "foo"},{"x": "bar"}]')
|
||||||
reader = JsonReader(path=filename)
|
reader = JsonReader(path=filename)
|
||||||
|
|
||||||
context = CapturingNodeExecutionContext(reader, services={'fs': fs})
|
context = CapturingNodeExecutionContext(reader, services={'fs': fs})
|
||||||
|
|||||||
@ -20,7 +20,8 @@ def test_write_pickled_dict_to_file(tmpdir):
|
|||||||
context.step()
|
context.step()
|
||||||
context.stop()
|
context.stop()
|
||||||
|
|
||||||
assert pickle.loads(fs.open(filename, 'rb').read()) == {'foo': 'bar'}
|
with fs.open(filename, 'rb') as fp:
|
||||||
|
assert pickle.loads(fp.read()) == {'foo': 'bar'}
|
||||||
|
|
||||||
with pytest.raises(AttributeError):
|
with pytest.raises(AttributeError):
|
||||||
getattr(context, 'file')
|
getattr(context, 'file')
|
||||||
@ -28,8 +29,8 @@ def test_write_pickled_dict_to_file(tmpdir):
|
|||||||
|
|
||||||
def test_read_pickled_list_from_file(tmpdir):
|
def test_read_pickled_list_from_file(tmpdir):
|
||||||
fs, filename = open_fs(tmpdir), 'input.pkl'
|
fs, filename = open_fs(tmpdir), 'input.pkl'
|
||||||
fs.open(filename,
|
with fs.open(filename, 'wb') as fp:
|
||||||
'wb').write(pickle.dumps([['a', 'b', 'c'], ['a foo', 'b foo', 'c foo'], ['a bar', 'b bar', 'c bar']]))
|
fp.write(pickle.dumps([['a', 'b', 'c'], ['a foo', 'b foo', 'c foo'], ['a bar', 'b bar', 'c bar']]))
|
||||||
|
|
||||||
reader = PickleReader(path=filename)
|
reader = PickleReader(path=filename)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user