Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/azure_devtools/scenario_tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ def create_random_name(self, prefix, length): # pylint: disable=no-self-use
return create_random_name(prefix=prefix, length=length)

def create_temp_file(self, size_kb, full_random=False):
""" Create a temporary file for testing. The test harness will delete the file during tearing down. """
"""
Create a temporary file for testing. The test harness will delete the file during tearing down.
:param float size_kb: specify the generated file size in kb.
"""
fd, path = tempfile.mkstemp()
os.close(fd)
self.addCleanup(lambda: os.remove(path))
Expand All @@ -40,8 +43,10 @@ def create_temp_file(self, size_kb, full_random=False):
chunk = os.urandom(1024)
else:
chunk = bytearray([0] * 1024)
for _ in range(size_kb):
for _ in range(int(size_kb)):
f.write(chunk)
chunk = os.urandom(int(1024 * (size_kb % 1)))
f.write(chunk)

return path

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,19 @@ def sample_test(self):
# the file is blank
self.assertTrue(any(b for b in fq.read(8 * 1024) if b != '\x00'))

random_file_3 = tb.create_temp_file(size_kb=8.5)
self.addCleanup(lambda: os.remove(random_file_3))
self.assertTrue(os.path.isfile(random_file_3))
self.assertEqual(os.path.getsize(random_file_3), 8.5 * 1024)
self.assertEqual(len(tb._cleanups), 3) # pylint: disable=protected-access
with open(random_file_3, 'rb') as fq:
# the file is blank
self.assertTrue(any(b for b in fq.read(int(8.5 * 1024)) if b != '\x00'))

random_dir = tb.create_temp_dir()
self.addCleanup(lambda: os.rmdir(random_dir))
self.assertTrue(os.path.isdir(random_dir))
self.assertEqual(len(tb._cleanups), 3) # pylint: disable=protected-access
self.assertEqual(len(tb._cleanups), 4) # pylint: disable=protected-access

def test_live_test_default_constructor(self):
class MockTest(LiveTest):
Expand Down