From 10fb8f6964ea4584a581aa909231b4ad53ea1834 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Doktor?= Date: Wed, 2 Mar 2016 06:19:34 +0100 Subject: [PATCH] avocado.core.result: Supply only "job" (and output for json/xunit/...) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both "stream" and "args" are present in the job. Let's only pass the "job" object and let the Result class to take the needed objects. Additionally allow overriding the output for json/xunit/html in order to allow users to specify different value than the one from args. Signed-off-by: Lukáš Doktor --- avocado/core/html.py | 18 +++++++++++------- avocado/core/job.py | 18 +++++------------- avocado/core/jsonresult.py | 19 +++++++++++++------ avocado/core/remote/result.py | 11 +++++------ avocado/core/result.py | 11 +++++------ avocado/core/xunit.py | 15 +++++++++------ avocado/plugins/journal.py | 7 +++---- selftests/unit/test_jsonresult.py | 4 +++- selftests/unit/test_xunit.py | 4 +++- 9 files changed, 57 insertions(+), 50 deletions(-) diff --git a/avocado/core/html.py b/avocado/core/html.py index 6e884cf4..4bf99684 100644 --- a/avocado/core/html.py +++ b/avocado/core/html.py @@ -24,7 +24,6 @@ import urllib import pystache -from . import output from .result import TestResult from ..utils import path as utils_path from ..utils import runtime @@ -189,11 +188,16 @@ class HTMLTestResult(TestResult): command_line_arg_name = '--html' - def __init__(self, stream=None, args=None): - TestResult.__init__(self, stream, args) - self.output = getattr(self.args, 'html_output') - self.args = args - self.view = output.View(app_args=args) + def __init__(self, job, force_html_file=None): + """ + :param job: Job which defines this result + :param force_html_file: Override the output html file location + """ + TestResult.__init__(self, job) + if force_html_file: + self.output = force_html_file + else: + self.output = self.args.html_output self.json = None def start_tests(self): @@ -288,7 +292,7 @@ class HTMLTestResult(TestResult): report_file.write(report_contents) if self.args is not None: - if getattr(self.args, 'open_browser'): + if getattr(self.args, 'open_browser', False): # if possible, put browser in separate process group, so # keyboard interrupts don't affect browser as well as Python setsid = getattr(os, 'setsid', None) diff --git a/avocado/core/job.py b/avocado/core/job.py index fd5daf6f..3e3ea6ff 100644 --- a/avocado/core/job.py +++ b/avocado/core/job.py @@ -195,7 +195,7 @@ class Job(object): def _set_output_plugins(self): if getattr(self.args, 'test_result_classes', None) is not None: for klass in self.args.test_result_classes: - test_result_instance = klass(self.view, self.args) + test_result_instance = klass(self) self.result_proxy.add_output_plugin(test_result_instance) def _make_test_result(self): @@ -216,26 +216,18 @@ class Job(object): # Setup the xunit plugin to output to the debug directory xunit_file = os.path.join(self.logdir, 'results.xml') - args = argparse.Namespace() - args.xunit_output = xunit_file - xunit_plugin = xunit.xUnitTestResult(self.view, args) + xunit_plugin = xunit.xUnitTestResult(self, xunit_file) self.result_proxy.add_output_plugin(xunit_plugin) # Setup the json plugin to output to the debug directory json_file = os.path.join(self.logdir, 'results.json') - args = argparse.Namespace() - args.json_output = json_file - json_plugin = jsonresult.JSONTestResult(self.view, args) + json_plugin = jsonresult.JSONTestResult(self, json_file) self.result_proxy.add_output_plugin(json_plugin) # Setup the html output to the results directory if HTML_REPORT_SUPPORT: html_file = os.path.join(self.logdir, 'html', 'results.html') - args = argparse.Namespace() - args.html_output = html_file - args.open_browser = getattr(self.args, 'open_browser', False) - args.relative_links = True - html_plugin = html.HTMLTestResult(self.view, args) + html_plugin = html.HTMLTestResult(self, html_file) self.result_proxy.add_output_plugin(html_plugin) op_set_stdout = self.result_proxy.output_plugins_using_stdout() @@ -249,7 +241,7 @@ class Job(object): sys.exit(exit_codes.AVOCADO_JOB_FAIL) if not op_set_stdout and not self.standalone: - human_plugin = result.HumanTestResult(self.view, self.args) + human_plugin = result.HumanTestResult(self) self.result_proxy.add_output_plugin(human_plugin) def _make_test_suite(self, urls=None): diff --git a/avocado/core/jsonresult.py b/avocado/core/jsonresult.py index e7b8ea8e..8aef730d 100644 --- a/avocado/core/jsonresult.py +++ b/avocado/core/jsonresult.py @@ -30,10 +30,17 @@ class JSONTestResult(TestResult): command_line_arg_name = '--json' - def __init__(self, stream=None, args=None): - TestResult.__init__(self, stream, args) - self.output = getattr(self.args, 'json_output', '-') - self.view = output.View(app_args=args) + def __init__(self, job, force_json_file=None): + """ + :param job: Job which defines this result + :param force_html_file: Override the json output file location + """ + TestResult.__init__(self, job) + if force_json_file: + self.output = force_json_file + else: + self.output = getattr(self.args, 'json_output', '-') + self.view = output.View(app_args=self.args) def start_tests(self): """ @@ -67,7 +74,7 @@ class JSONTestResult(TestResult): self.json['tests'].append(t) def _save_json(self): - with open(self.args.json_output, 'w') as j: + with open(self.output, 'w') as j: j.write(self.json) def end_tests(self): @@ -84,7 +91,7 @@ class JSONTestResult(TestResult): 'time': self.total_time }) self.json = json.dumps(self.json) - if self.args.json_output == '-': + if self.output == '-': self.view.notify(event='minor', msg=self.json) else: self._save_json() diff --git a/avocado/core/remote/result.py b/avocado/core/remote/result.py index db5e6df9..ca6cae71 100644 --- a/avocado/core/remote/result.py +++ b/avocado/core/remote/result.py @@ -27,14 +27,13 @@ class RemoteTestResult(HumanTestResult): command_line_arg_name = '--remote-hostname' - def __init__(self, stream, args): + def __init__(self, job): """ Creates an instance of RemoteTestResult. - :param stream: an instance of :class:`avocado.core.output.View`. - :param args: an instance of :class:`argparse.Namespace`. + :param job: an instance of :class:`avocado.core.job.Job`. """ - HumanTestResult.__init__(self, stream, args) + HumanTestResult.__init__(self, job) self.test_dir = os.getcwd() self.remote_test_dir = '~/avocado/tests' self.urls = self.args.url @@ -54,6 +53,6 @@ class VMTestResult(RemoteTestResult): command_line_arg_name = '--vm-domain' - def __init__(self, stream, args): - super(VMTestResult, self).__init__(stream, args) + def __init__(self, job): + super(VMTestResult, self).__init__(job) self.vm = None diff --git a/avocado/core/result.py b/avocado/core/result.py index 55fb85a2..68d2e1ef 100644 --- a/avocado/core/result.py +++ b/avocado/core/result.py @@ -118,16 +118,15 @@ class TestResult(object): #: inconsistencies come from. command_line_arg_name = None - def __init__(self, stream, args): + def __init__(self, job): """ Creates an instance of TestResult. - :param stream: an instance of :class:`avocado.core.output.View`. - :param args: an instance of :class:`argparse.Namespace`. + :param job: an instance of :class:`avocado.core.job.Job`. """ - self.stream = stream - self.args = args - self.tests_total = getattr(args, 'test_result_total', 1) + self.args = getattr(job, "args", None) + self.stream = getattr(job, "view", None) + self.tests_total = getattr(self.args, 'test_result_total', 1) self.tests_run = 0 self.total_time = 0.0 self.passed = [] diff --git a/avocado/core/xunit.py b/avocado/core/xunit.py index e5361ceb..2761c4bb 100644 --- a/avocado/core/xunit.py +++ b/avocado/core/xunit.py @@ -154,16 +154,19 @@ class xUnitTestResult(TestResult): command_line_arg_name = '--xunit' - def __init__(self, stream=None, args=None): + def __init__(self, job, force_xunit_file=None): """ Creates an instance of xUnitTestResult. - :param stream: an instance of :class:`avocado.core.output.View`. - :param args: an instance of :class:`argparse.Namespace`. + :param job: an instance of :class:`avocado.core.job.Job`. + :param force_xunit_file: Override the output file defined in job.args """ - TestResult.__init__(self, stream, args) - self.output = getattr(self.args, 'xunit_output', '-') - self.stream = output.View(app_args=args) + TestResult.__init__(self, job) + if force_xunit_file: + self.output = force_xunit_file + else: + self.output = getattr(self.args, 'xunit_output', '-') + self.stream = output.View(app_args=self.args) self.xml = XmlResult() def start_tests(self): diff --git a/avocado/plugins/journal.py b/avocado/plugins/journal.py index fdae5520..d4a4716e 100644 --- a/avocado/plugins/journal.py +++ b/avocado/plugins/journal.py @@ -45,14 +45,13 @@ class TestResultJournal(TestResult): command_line_arg_name = '--journal' - def __init__(self, stream=None, args=None): + def __init__(self, job=None): """ Creates an instance of TestResultJournal. - :param stream: an instance of :class:`avocado.core.output.View`. - :param args: an instance of :class:`argparse.Namespace`. + :param job: an instance of :class:`avocado.core.job.Job`. """ - TestResult.__init__(self, stream, args) + TestResult.__init__(self, job) self.journal_initialized = False def _init_journal(self, logdir): diff --git a/selftests/unit/test_jsonresult.py b/selftests/unit/test_jsonresult.py index 899ba61f..fb5bf0f6 100644 --- a/selftests/unit/test_jsonresult.py +++ b/selftests/unit/test_jsonresult.py @@ -1,3 +1,4 @@ +from flexmock import flexmock import unittest import os import json @@ -45,7 +46,8 @@ class JSONResultTest(unittest.TestCase): args = argparse.Namespace(json_output=self.tmpfile[1]) stream = _Stream() stream.logfile = 'debug.log' - self.test_result = jsonresult.JSONTestResult(stream, args) + dummyjob = flexmock(view=stream, args=args) + self.test_result = jsonresult.JSONTestResult(dummyjob) self.test_result.filename = self.tmpfile[1] self.test_result.start_tests() self.test1 = SimpleTest(job=job.Job(), base_logdir=self.tmpdir) diff --git a/selftests/unit/test_xunit.py b/selftests/unit/test_xunit.py index d3218ea9..c07e41f6 100644 --- a/selftests/unit/test_xunit.py +++ b/selftests/unit/test_xunit.py @@ -1,3 +1,4 @@ +from flexmock import flexmock import argparse import unittest import os @@ -48,7 +49,8 @@ class xUnitSucceedTest(unittest.TestCase): self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__) args = argparse.Namespace() args.xunit_output = self.tmpfile[1] - self.test_result = xunit.xUnitTestResult(stream=_Stream(), args=args) + dummy_job = flexmock(view=_Stream(), args=args) + self.test_result = xunit.xUnitTestResult(dummy_job) self.test_result.start_tests() self.test1 = SimpleTest(job=job.Job(), base_logdir=self.tmpdir) self.test1.status = 'PASS' -- GitLab