[gentoo-portage-dev] [PATCH v2] runtests: create a global tempdir to hold subtest files
A lot of unittests currently leak content in /tmp when they run. Rather than explicitly track down every failing test (which we can do regardless of this), have the runtest runner create a global tempdir and use that as a base for children tests. Then when the runtest script finishes, it takes care of nuking everything. --- v2 - make tempdir setup more robust per Zac runtests | 48 +--- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/runtests b/runtests index 36243c6..89c6e14 100755 --- a/runtests +++ b/runtests @@ -15,8 +15,10 @@ from __future__ import print_function import argparse import os -import sys +import shutil import subprocess +import sys +import tempfile # These are the versions we fully support and require to pass tests. @@ -91,6 +93,8 @@ $ %(prog)s pym/portage/tests/xpak/test_decodeint.py description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, epilog=epilog) + parser.add_argument('--keep-temp', default=False, action='store_true', + help='Do not delete the temporary directory when exiting') parser.add_argument('--color', type=str, default=None, help='Whether to use colorized output (default is auto)') parser.add_argument('--python-versions', action='append', @@ -116,20 +120,34 @@ def main(argv): else: pyversions.extend(ver.split()) - # Actually test those versions now. - statuses = [] - for ver in pyversions: - prog = get_python_executable(ver) - cmd = [prog, '-b', '-Wd', 'pym/portage/tests/runTests.py'] + args - if os.access(prog, os.X_OK): - print('%sTesting with Python %s...%s' % - (colors.GOOD, ver, colors.NORMAL)) - statuses.append(subprocess.call(cmd)) - elif not ignore_missing: - print('%sCould not find requested Python %s%s' % - (colors.BAD, ver, colors.NORMAL)) - statuses.append(1) - print() + tempdir = None + try: + # Set up a single tempdir for all the tests to use. + # This way we know the tests won't leak things on us. + tempdir = tempfile.mkdtemp(prefix='portage.runtests.') + os.environ['TMPDIR'] = tempdir + + # Actually test those versions now. + statuses = [] + for ver in pyversions: + prog = get_python_executable(ver) + cmd = [prog, '-b', '-Wd', 'pym/portage/tests/runTests.py'] + args + if os.access(prog, os.X_OK): + print('%sTesting with Python %s...%s' % + (colors.GOOD, ver, colors.NORMAL)) + statuses.append(subprocess.call(cmd)) + elif not ignore_missing: + print('%sCould not find requested Python %s%s' % + (colors.BAD, ver, colors.NORMAL)) + statuses.append(1) + print() + finally: + if tempdir is not None: + if opts.keep_temp: + print('Temporary directory left behind:\n%s' % tempdir) + else: + # Nuke our tempdir and anything that might be under it. + shutil.rmtree(tempdir, True) # Then summarize it all. print('\nSummary:\n') -- 2.5.2
Re: [gentoo-portage-dev] [PATCH 2/2] _argparse: punt the module
-BEGIN PGP SIGNED MESSAGE- Hash: SHA512 On 30/10/15 06:24, Zac Medico wrote: > Both patches look good. Yeah, this is great stuff. Thanks Mike! - -- Alexander berna...@gentoo.org https://secure.plaimi.net/~alexander -BEGIN PGP SIGNATURE- Version: GnuPG v2 iQIcBAEBCgAGBQJWM19CAAoJENQqWdRUGk8B9pMP/2UYc7nq71Rcog7V/g5xPkWT Twk6TSzgxRDD+v6w+qh2f7sk1CuNTptf+lhhD02Cxiv2WtzUANuTTKdNf8Bt3BuV zlgZ4Od2aJyai0FXS+eSk+TOE4U8aoU/jvG9LNZePEpBl0FVnJsvEJpqNJlMh8XV 8MVoyY2ezw5aIe1h96fCAZ5pi+PkqmdM59CUNZJsMOABAkn4MFXersSejoFefO1z IckNFRFQ1XF0qRszuUUum6iRyvIUQER67g0ABZ5OhLfNYHRAHQUU0pcooZMnRub4 eCoAis8yqHJMydv1NHY74Bc9S4aUt5mIxkczWqgSuBb4q3d3LBYu6Hlv5MnHqIm6 P+/XBwdwLXe43QFl1lG2S4AeIbU0TxVe4wHDxT2H1/ZlckKeat6fcynPeLayp6rP hKm9nBBJDP8FmN2wPULruLT+o2ENj3Rz2PzOQon3MCMuIWyi8Ax9I21uUNbM2gBv t/h3A2sgy4YJhgjFK1ITeZotptO9U6XGP8HeuA/7mcBG2DGI9r+yNRRZuMoXqTZW wIYs3j2vVf9CHQns44X72NIMIATKGnDMrx+4nJgeyvCsgJCkI3VmHGoaoNNbeQ/+ BOHz/3eWTMMfJVZzzah5cdJQEGC0KV5ouq4zWsUEsj1oc548rC/ZPUbf57WoW03v zBpalKiy3CCNTaloBPCr =KztW -END PGP SIGNATURE-
Re: [gentoo-portage-dev] [PATCH] runtests: create a global tempdir to hold subtest files
On 10/30/2015 11:52 AM, Zac Medico wrote: > On 10/30/2015 11:00 AM, Mike Frysinger wrote: > >> +try: >> +# Set up a single tempdir for all the tests to use. >> +# This way we know the tests won't leak things on us. >> +tempdir = tempfile.mkdtemp(prefix='portage.runtests.') > [snip] >> +finally: >> +if opts.keep_temp: >> +print('Temporary directory left behind:\n%s' % tempdir) >> +else: >> +# Nuke our tempdir and anything that might be under it. >> +shutil.rmtree(tempdir, True) >> >> # Then summarize it all. >> print('\nSummary:\n') >> > > This will raise NameError if mkdtemp for some reason. For absolute > correctness, you need to call mkdtemp before try, or set tempdir = None > before the try and check that it's not None before calling rmtree. > I mean, "if mkdtemp fails for some reason." Other then that issue, the patch looks good. -- Thanks, Zac
Re: [gentoo-portage-dev] [PATCH] runtests: create a global tempdir to hold subtest files
On 10/30/2015 11:00 AM, Mike Frysinger wrote: > + try: > + # Set up a single tempdir for all the tests to use. > + # This way we know the tests won't leak things on us. > + tempdir = tempfile.mkdtemp(prefix='portage.runtests.') [snip] > + finally: > + if opts.keep_temp: > + print('Temporary directory left behind:\n%s' % tempdir) > + else: > + # Nuke our tempdir and anything that might be under it. > + shutil.rmtree(tempdir, True) > > # Then summarize it all. > print('\nSummary:\n') > This will raise NameError if mkdtemp for some reason. For absolute correctness, you need to call mkdtemp before try, or set tempdir = None before the try and check that it's not None before calling rmtree. -- Thanks, Zac
[gentoo-portage-dev] [PATCH] runtests: create a global tempdir to hold subtest files
A lot of unittests currently leak content in /tmp when they run. Rather than explicitly track down every failing test (which we can do regardless of this), have the runtest runner create a global tempdir and use that as a base for children tests. Then when the runtest script finishes, it takes care of nuking everything. --- runtests | 46 +++--- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/runtests b/runtests index 36243c6..fc6de0a 100755 --- a/runtests +++ b/runtests @@ -15,8 +15,10 @@ from __future__ import print_function import argparse import os -import sys +import shutil import subprocess +import sys +import tempfile # These are the versions we fully support and require to pass tests. @@ -91,6 +93,8 @@ $ %(prog)s pym/portage/tests/xpak/test_decodeint.py description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, epilog=epilog) + parser.add_argument('--keep-temp', default=False, action='store_true', + help='Do not delete the temporary directory when exiting') parser.add_argument('--color', type=str, default=None, help='Whether to use colorized output (default is auto)') parser.add_argument('--python-versions', action='append', @@ -116,20 +120,32 @@ def main(argv): else: pyversions.extend(ver.split()) - # Actually test those versions now. - statuses = [] - for ver in pyversions: - prog = get_python_executable(ver) - cmd = [prog, '-b', '-Wd', 'pym/portage/tests/runTests.py'] + args - if os.access(prog, os.X_OK): - print('%sTesting with Python %s...%s' % - (colors.GOOD, ver, colors.NORMAL)) - statuses.append(subprocess.call(cmd)) - elif not ignore_missing: - print('%sCould not find requested Python %s%s' % - (colors.BAD, ver, colors.NORMAL)) - statuses.append(1) - print() + try: + # Set up a single tempdir for all the tests to use. + # This way we know the tests won't leak things on us. + tempdir = tempfile.mkdtemp(prefix='portage.runtests.') + os.environ['TMPDIR'] = tempdir + + # Actually test those versions now. + statuses = [] + for ver in pyversions: + prog = get_python_executable(ver) + cmd = [prog, '-b', '-Wd', 'pym/portage/tests/runTests.py'] + args + if os.access(prog, os.X_OK): + print('%sTesting with Python %s...%s' % + (colors.GOOD, ver, colors.NORMAL)) + statuses.append(subprocess.call(cmd)) + elif not ignore_missing: + print('%sCould not find requested Python %s%s' % + (colors.BAD, ver, colors.NORMAL)) + statuses.append(1) + print() + finally: + if opts.keep_temp: + print('Temporary directory left behind:\n%s' % tempdir) + else: + # Nuke our tempdir and anything that might be under it. + shutil.rmtree(tempdir, True) # Then summarize it all. print('\nSummary:\n') -- 2.5.2