Skip to content

Commit

Permalink
binman: Clean up all output directories in tests
Browse files Browse the repository at this point in the history
At present some tests leave behind output directories. This happens
because some tests call binman, which sets up an output directory, then
call it again, which sets up another output directory and leaves the
original one behind.

Fix this by using a separate temporary directory when binman is called
twice, or by manually removing the output directory.

Signed-off-by: Simon Glass <[email protected]>
  • Loading branch information
sjg20 committed Jul 29, 2019
1 parent bf574f1 commit f86a736
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions tools/binman/ftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,28 @@ def tearDown(self):
"""Remove the temporary output directory"""
self._CleanupOutputDir()

def _SetupImageInTmpdir(self):
"""Set up the output image in a new temporary directory
This is used when an image has been generated in the output directory,
but we want to run binman again. This will create a new output
directory and fail to delete the original one.
This creates a new temporary directory, copies the image to it (with a
new name) and removes the old output directory.
Returns:
Tuple:
Temporary directory to use
New image filename
"""
image_fname = tools.GetOutputFilename('image.bin')
tmpdir = tempfile.mkdtemp(prefix='binman.')
updated_fname = os.path.join(tmpdir, 'image-updated.bin')
tools.WriteFile(updated_fname, tools.ReadFile(image_fname))
self._CleanupOutputDir()
return tmpdir, updated_fname

@classmethod
def _ResetDtbs(self):
TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
Expand Down Expand Up @@ -1563,6 +1585,7 @@ def testSelectImage(self):

self.assertFalse(os.path.exists(tools.GetOutputFilename('image1.bin')))
self.assertTrue(os.path.exists(tools.GetOutputFilename('image2.bin')))
self._CleanupOutputDir()

def testUpdateFdtAll(self):
"""Test that all device trees are updated with offset/size info"""
Expand Down Expand Up @@ -2364,9 +2387,12 @@ def testListCmd(self):
fdt_size = entries['section'].GetEntries()['u-boot-dtb'].size
fdtmap_offset = entries['fdtmap'].offset

image_fname = tools.GetOutputFilename('image.bin')
with test_util.capture_sys_output() as (stdout, stderr):
self._DoBinman('ls', '-i', image_fname)
try:
tmpdir, updated_fname = self._SetupImageInTmpdir()
with test_util.capture_sys_output() as (stdout, stderr):
self._DoBinman('ls', '-i', updated_fname)
finally:
shutil.rmtree(tmpdir)
lines = stdout.getvalue().splitlines()
expected = [
'Name Image-pos Size Entry-type Offset Uncomp-size',
Expand All @@ -2387,9 +2413,12 @@ def testListCmd(self):
def testListCmdFail(self):
"""Test failing to list an image"""
self._DoReadFile('005_simple.dts')
image_fname = tools.GetOutputFilename('image.bin')
with self.assertRaises(ValueError) as e:
self._DoBinman('ls', '-i', image_fname)
try:
tmpdir, updated_fname = self._SetupImageInTmpdir()
with self.assertRaises(ValueError) as e:
self._DoBinman('ls', '-i', updated_fname)
finally:
shutil.rmtree(tmpdir)
self.assertIn("Cannot find FDT map in image", str(e.exception))

def _RunListCmd(self, paths, expected):
Expand Down Expand Up @@ -2515,10 +2544,14 @@ def testExtractCmd(self):
"""Test extracting a file fron an image on the command line"""
self._CheckLz4()
self._DoReadFileRealDtb('130_list_fdtmap.dts')
image_fname = tools.GetOutputFilename('image.bin')
fname = os.path.join(self._indir, 'output.extact')
with test_util.capture_sys_output() as (stdout, stderr):
self._DoBinman('extract', '-i', image_fname, 'u-boot', '-f', fname)
try:
tmpdir, updated_fname = self._SetupImageInTmpdir()
with test_util.capture_sys_output() as (stdout, stderr):
self._DoBinman('extract', '-i', updated_fname, 'u-boot',
'-f', fname)
finally:
shutil.rmtree(tmpdir)
data = tools.ReadFile(fname)
self.assertEqual(U_BOOT_DATA, data)

Expand Down

0 comments on commit f86a736

Please sign in to comment.