Changeset a45fc7b for tests


Ignore:
Timestamp:
Mar 27, 2019, 2:44:34 PM (6 years ago)
Author:
tdelisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
1241851, ce3d305
Parents:
86fb8f2
Message:

Tests no longer use subprocess.run(shell=True), which solves leak process problems

Location:
tests
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • tests/pybin/settings.py

    r86fb8f2 ra45fc7b  
    11import os
     2import subprocess
    23import sys
    34from . import tools
     
    8283        def __init__(self, value):
    8384                self.string = "debug" if value else "no debug"
    84                 self.flags  = """DEBUG_FLAGS="%s" """ % ("-debug -O0" if value else "-nodebug -O2")
     85                self.flags  = """DEBUG_FLAGS=%s""" % ("-debug -O0" if value else "-nodebug -O2")
    8586
    8687class Install:
    8788        def __init__(self, value):
    8889                self.string = "installed" if value else "in-tree"
    89                 self.flags  = """INSTALL_FLAGS="%s" """ % ("" if value else "-in-tree")
     90                self.flags  = """INSTALL_FLAGS=%s""" % ("" if value else "-in-tree")
    9091
    9192class Timeouts:
     
    114115        dry_run      = options.dry_run
    115116        generating   = options.regenerate_expected
    116         make         = 'make'
     117        make         = ['make']
    117118        debug        = Debug(options.debug)
    118119        install      = Install(options.install)
     
    125126        global make
    126127
    127         make = "make" if not force else ("make -j%i" % jobs)
     128        make = ['make'] if not force else ['make', "-j%i" % jobs]
    128129
    129130def validate():
    130131        errf = os.path.join(BUILDDIR, ".validate.err")
    131         make_ret, _ = tools.make( ".validate", error_file = errf, redirects  = "2> /dev/null 1> /dev/null", )
     132        make_ret, out = tools.make( ".validate", error_file = errf, output=subprocess.DEVNULL, error=subprocess.DEVNULL )
    132133        if make_ret != 0:
    133134                with open (errf, "r") as myfile:
  • tests/pybin/tools.py

    r86fb8f2 ra45fc7b  
    11import __main__
    22import argparse
     3import contextlib
    34import fileinput
    45import multiprocessing
     
    2122
    2223# helper functions to run terminal commands
    23 def sh(cmd, print2stdout = True, timeout = False, output = None, input = None):
    24         # add input redirection if needed
    25         if input and os.path.isfile(input):
    26                 cmd += " < %s" % input
    27 
    28         # add output redirection if needed
    29         if output:
    30                 cmd += " > %s" % output
     24def sh(*cmd, timeout = False, output = None, input = None, error = subprocess.STDOUT):
     25        cmd = list(cmd)
    3126
    3227        # if this is a dry_run, only print the commands that would be ran
    3328        if settings.dry_run :
    34                 print("cmd: %s" % cmd)
     29                print("cmd: %s" % ' '.join(cmd))
    3530                return 0, None
    3631
    37         # otherwise create a pipe and run the desired command
    38         else :
     32        with contextlib.ExitStack() as onexit:
     33                # add input redirection if needed
     34                if input and input != subprocess.DEVNULL:
     35                        if os.path.isfile(input):
     36                                input = open(input)
     37                                onexit.push(input)
     38                        else:
     39                                input = None
     40
     41                # add output redirection if needed
     42                if output and output != subprocess.DEVNULL and output != subprocess.PIPE:
     43                        output = open(output, 'w')
     44                        onexit.push(output)
     45
     46                # run the desired command
    3947                try:
    4048                        proc = subprocess.run(
    4149                                cmd,
    42                                 stdout=None if print2stdout else PIPE,
     50                                stdin =input,
     51                                stdout=output,
    4352                                stderr=STDOUT,
    44                                 shell=True,
    4553                                timeout=settings.timeout.single if timeout else None
    4654                        )
     
    5765                return False
    5866
    59         code, out = sh("file %s" % fname, print2stdout = False)
     67        code, out = sh("file %s" % fname, output=subprocess.PIPE)
    6068        if code != 0:
    6169                return False
     
    7583        if isinstance(files, str ): files = [ files ]
    7684        for file in files:
    77                 sh("rm -f %s > /dev/null 2>&1" % file )
     85                sh( 'rm', '-f', file, output=subprocess.DEVNULL, error=subprocess.DEVNULL )
    7886
    7987# Create 1 or more directory
     
    8391                p = os.path.normpath( file )
    8492                d = os.path.dirname ( p )
    85                 sh( "mkdir -p {}".format(d) )
     93                sh( 'mkdir', '-p', d, output=subprocess.DEVNULL, error=subprocess.DEVNULL )
    8694
    8795
     
    93101# diff two files
    94102def diff( lhs, rhs ):
    95         # diff the output of the files
    96         diff_cmd = ("diff --text "
    97                         "--old-group-format='\t\tmissing lines :\n"
    98                         "%%<' \\\n"
    99                         "--new-group-format='\t\tnew lines :\n"
    100                         "%%>' \\\n"
    101                         "--unchanged-group-format='%%=' \\"
    102                         "--changed-group-format='\t\texpected :\n"
    103                         "%%<"
    104                         "\t\tgot :\n"
    105                         "%%>\n' \\\n"
    106                         "--new-line-format='\t\t%%dn\t%%L' \\\n"
    107                         "--old-line-format='\t\t%%dn\t%%L' \\\n"
    108                         "--unchanged-line-format='' \\\n"
    109                         "%s %s")
    110 
    111103        # fetch return code and error from the diff command
    112         return sh(diff_cmd % (lhs, rhs), False)
     104        return sh(
     105                '''diff''',
     106                '''--text''',
     107                '''--old-group-format=\t\tmissing lines :\n%<''',
     108                '''--new-line-format=\t\t%dn\t%L''',
     109                '''--new-group-format=\t\tnew lines : \n%>''',
     110                '''--old-line-format=\t\t%dn\t%L''',
     111                '''--unchanged-group-format=%=''',
     112                '''--changed-group-format=\t\texpected :\n%<\t\tgot :\n%>''',
     113                '''--unchanged-line-format=''',
     114                lhs,
     115                rhs,
     116                output=subprocess.PIPE
     117        )
    113118
    114119# call make
    115 def make(target, flags = '', redirects = '', error_file = None, silent = False):
    116         test_param = """test="%s" """ % (error_file) if error_file else ''
    117         cmd = ' '.join([
    118                 settings.make,
    119                 '-s' if silent else '',
     120def make(target, *, flags = '', output = None, error = None, error_file = None, silent = False):
     121        test_param = """test="%s" """ % (error_file) if error_file else None
     122        cmd = [
     123                *settings.make,
     124                '-s' if silent else None,
    120125                test_param,
    121126                settings.arch.flags,
     
    123128                settings.install.flags,
    124129                flags,
    125                 target,
    126                 redirects
    127         ])
    128         return sh(cmd)
     130                target
     131        ]
     132        cmd = [s for s in cmd if s]
     133        return sh(*cmd, output=output, error=error)
    129134
    130135def which(program):
     
    146151# move a file
    147152def mv(source, dest):
    148         ret, _ = sh("mv %s %s" % (source, dest))
     153        ret, _ = sh("mv", source, dest)
    149154        return ret
    150155
    151156# cat one file into the other
    152157def cat(source, dest):
    153         ret, _ = sh("cat %s > %s" % (source, dest))
     158        ret, _ = sh("cat", source, output=dest)
    154159        return ret
    155160
     
    253258                return 1, "ERR No core dump"
    254259
    255         return sh("gdb -n %s %s -batch -x %s" % (path, core, cmd), print2stdout=False)
     260        return sh('gdb', '-n', path, core, '-batch', '-x', cmd, output=subprocess.PIPE)
    256261
    257262class Timed:
  • tests/test.py

    r86fb8f2 ra45fc7b  
    141141        # build, skipping to next test on error
    142142        with Timed() as comp_dur:
    143                 make_ret, _ = make( test.target(),      redirects  = ("2> %s 1> /dev/null" % out_file), error_file = err_file )
     143                make_ret, _ = make( test.target(), output=subprocess.DEVNULL, error=out_file, error_file = err_file )
    144144
    145145        # if the make command succeds continue otherwise skip to diff
     
    195195
    196196                #print result with error if needed
    197                 text = name_txt + result_txt
     197                text = '\t' + name_txt + result_txt
    198198                out = sys.stdout
    199199                if error :
     
    212212def run_tests(tests, jobs) :
    213213        # clean the sandbox from previous commands
    214         make('clean', redirects = '> /dev/null 2>&1')
     214        make('clean', output=subprocess.DEVNULL, error=subprocess.DEVNULL)
    215215
    216216        # create the executor for our jobs and handle the signal properly
     
    230230
    231231        # clean the workspace
    232         make('clean', redirects = '> /dev/null 2>&1')
     232        make('clean', output=subprocess.DEVNULL, error=subprocess.DEVNULL)
    233233
    234234        for failed in results:
Note: See TracChangeset for help on using the changeset viewer.