forked from drath/libratbag
tools: ratbagctl tests: do not spawn a new process for each call
This reduces the time to run the tests from (now with previous commit) 10s to a mere 2 seconds. The --subprocess argument is voluntarily masked from the help message, because I couldn't figure out how to add it to the unittest parser. And OTOH, we should not run in subprocess unless debugging... Signed-off-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
This commit is contained in:
committed by
Peter Hutterer
parent
c61782f1a4
commit
de4bd4a2d2
@@ -1639,6 +1639,10 @@ class RatbagParserRoot(object):
|
||||
self.parser.print_help()
|
||||
|
||||
|
||||
def get_parser():
|
||||
return RatbagParserRoot(parser_def)
|
||||
|
||||
|
||||
def open_ratbagd():
|
||||
try:
|
||||
r = Ratbagd()
|
||||
@@ -1655,7 +1659,7 @@ def main(argv):
|
||||
if not argv:
|
||||
argv = ["list"]
|
||||
|
||||
parser = RatbagParserRoot(parser_def)
|
||||
parser = get_parser()
|
||||
cmd = parser.parse(argv)
|
||||
|
||||
r = open_ratbagd()
|
||||
|
||||
@@ -25,7 +25,9 @@
|
||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
# DEALINGS IN THE SOFTWARE.
|
||||
|
||||
import argparse
|
||||
import imp
|
||||
import io
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
@@ -39,6 +41,8 @@ DBUS_CONF_DIR = '/etc/dbus-1/system.d'
|
||||
DBUS_CONF_NAME = 'org.freedesktop.ratbag_devel1.conf'
|
||||
DBUS_CONF_PATH = os.path.join(DBUS_CONF_DIR, DBUS_CONF_NAME)
|
||||
|
||||
run_ratbagctl_in_subprocess = False
|
||||
|
||||
|
||||
def import_non_standard_path(name, path):
|
||||
# Fast path: see if the module has already been imported.
|
||||
@@ -65,7 +69,7 @@ class TestRatbagCtl(unittest.TestCase):
|
||||
cls.reset_test_device()
|
||||
|
||||
@classmethod
|
||||
def run_ratbagctl(cls, params):
|
||||
def run_ratbagctl_subprocess(cls, params):
|
||||
ratbagctl = subprocess.Popen(" ".join([RATBAGCTL_PATH, params]),
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
@@ -77,6 +81,32 @@ class TestRatbagCtl(unittest.TestCase):
|
||||
stdout.decode('ascii').rstrip('\n'),
|
||||
stderr.decode('ascii').rstrip('\n'))
|
||||
|
||||
@classmethod
|
||||
def run_ratbagctl_fast(cls, params):
|
||||
global parser, ratbagd
|
||||
stdout = sys.stdout
|
||||
stderr = sys.stderr
|
||||
returncode = 0
|
||||
|
||||
sys.stdout = io.StringIO()
|
||||
sys.stderr = io.StringIO()
|
||||
try:
|
||||
cmd = parser.parse(params.split())
|
||||
cmd.func(ratbagd, cmd)
|
||||
except SystemExit as e:
|
||||
returncode = e.code
|
||||
output = sys.stdout.getvalue()
|
||||
error = sys.stderr.getvalue()
|
||||
sys.stdout = stdout
|
||||
sys.stderr = stderr
|
||||
return returncode, output.rstrip('\n'), error.rstrip('\n')
|
||||
|
||||
@classmethod
|
||||
def run_ratbagctl(cls, params):
|
||||
if run_ratbagctl_in_subprocess:
|
||||
return cls.run_ratbagctl_subprocess(params)
|
||||
return cls.run_ratbagctl_fast(params)
|
||||
|
||||
@classmethod
|
||||
def reset_test_device(cls):
|
||||
global ratbagd
|
||||
@@ -422,7 +452,7 @@ class TestRatbgagCtlLED(TestRatbagCtl):
|
||||
|
||||
|
||||
def setUpModule():
|
||||
global ratbagd_process, ratbagd
|
||||
global ratbagd_process, ratbagd, parser
|
||||
# first copy the policy for the ratbagd daemon to be allowed to run
|
||||
shutil.copy(os.path.join('@MESON_BUILD_ROOT@', DBUS_CONF_NAME), DBUS_CONF_PATH)
|
||||
|
||||
@@ -435,6 +465,8 @@ def setUpModule():
|
||||
ratbagd = ratbagctl.open_ratbagd()
|
||||
assert ratbagd is not None
|
||||
|
||||
parser = ratbagctl.get_parser()
|
||||
|
||||
|
||||
def tearDownModule():
|
||||
global ratbagd_process
|
||||
@@ -447,11 +479,22 @@ def tearDownModule():
|
||||
os.unlink(DBUS_CONF_PATH)
|
||||
|
||||
|
||||
def parse(input_string):
|
||||
global run_ratbagctl_in_subprocess
|
||||
parser_test = argparse.ArgumentParser(description="Testsuite for ratbagd/ratbagctl", add_help=False)
|
||||
parser_test.add_argument("--subprocess", action='store_true', default=False)
|
||||
ns, rest = parser_test.parse_known_args(input_string)
|
||||
run_ratbagctl_in_subprocess = ns.subprocess
|
||||
return rest
|
||||
|
||||
|
||||
def main(argv):
|
||||
if not os.geteuid() == 0:
|
||||
sys.exit('Script must be run as root')
|
||||
|
||||
unittest.main()
|
||||
args = parse(argv)
|
||||
|
||||
unittest.main(argv=[sys.argv[0], *args])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user