#
# Copyright (c) 2010 Testrepository Contributors
#
# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
# license at the users choice. A copy of both licenses are available in the
# project source as Apache-2.0 and BSD. You may not use this file except in
# compliance with one of these two licences.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# license you chose for the specific language governing permissions and
# limitations under that license.

"""Show the last run loaded into a repository."""

import os

from stestr.repository import file as file_repo


def get_cli_help():
    help_str = """Show the last run loaded into a repository.

    Failing tests are shown on the console and a summary of the run is printed
    at the end.

    Without --subunit, the process exit code will be non-zero if the test run
    was not successful. With --subunit, the process exit code is non-zero if
    the subunit stream could not be generated successfully.
    """
    return help_str


def set_cli_opts(parser):
    parser.add_argument(
        "--subunit", action="store_true",
        default=False, help="Show output as a subunit stream."),


def run(self):
    # TODO(mtreinish): Add a CLI opt to set repository type
    repo = file_repo.RepositoryFactory.open(os.getcwd())
    testcommand = self.command_factory(self.ui, repo)
    latest_run = repo.get_latest_run()
    if self.ui.options.subunit:
        stream = latest_run.get_subunit_stream()
        self.ui.output_stream(stream)
        # Exits 0 if we successfully wrote the stream.
        return 0
    case = latest_run.get_test()
    try:
        previous_run = repo.get_test_run(repo.latest_id() - 1)
    except KeyError:
        previous_run = None
    failed = False
    result, summary = self.ui.make_result(
        latest_run.get_id, testcommand, previous_run=previous_run)
    result.startTestRun()
    try:
        case.run(result)
    finally:
        result.stopTestRun()
    failed = not summary.wasSuccessful()
    if failed:
        return 1
    else:
        return 0

@@ -714,16 +714,53 @@
 port os%0A
+import sys%0A%0Afrom stestr import output
 %0Afrom st
@@ -800,16 +800,43 @@
 le_repo%0A
+from stestr import results%0A
 %0A%0Adef ge
@@ -1425,14 +1425,43 @@
 run(
-self):
+arguments):%0A    args = arguments%5B0%5D
 %0A   
@@ -1555,16 +1555,18 @@
 yFactory
+()
 .open(os
@@ -1580,62 +1580,8 @@
 ())%0A
-    testcommand = self.command_factory(self.ui, repo)%0A
     
@@ -1626,22 +1626,11 @@
  if 
-self.ui.option
+arg
 s.su
@@ -1693,23 +1693,22 @@
         
-self.ui
+output
 .output_
@@ -1975,118 +1975,186 @@
     
-result, summary = self.ui.make_result(%0A        latest_run.get_id, testcommand, previous_run=previous_run)%0A    
+output_result = results.CLITestResult(latest_run.get_id, sys.stdout,%0A                                          previous_run)%0A    summary = output_result.get_summary()%0A    output_
 resu
@@ -2197,16 +2197,23 @@
 ase.run(
+output_
 result)%0A
@@ -2233,16 +2233,23 @@
         
+output_
 result.s

Fix stestr last command