~cr3/launchpad-results/trunk

« back to all changes in this revision

Viewing changes to doc/examples/subunit_to_launchpad.py

  • Committer: Marc Tardif
  • Date: 2010-10-19 00:59:16 UTC
  • Revision ID: marc.tardif@canonical.com-20101019005916-8fwzfni13vz4zi0e
Added TestResult which is contained within TestRuns and renamed Test to TestCase.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
import sys
 
4
 
 
5
import lazr.restful
 
6
 
 
7
from optparse import OptionParser
 
8
 
 
9
from subunit import ProtocolTestCase
 
10
 
 
11
from launchpadlib.launchpad import Launchpad
 
12
 
 
13
 
 
14
DEFAULT_CONSUMER_NAME = "Launchpad Results"
 
15
DEFAULT_SERVICE_ROOT = "http://localhost:8000/api/"
 
16
 
 
17
 
 
18
class Result(object):
 
19
 
 
20
    def __init__(self, test_run):
 
21
        self.test_run = test_run
 
22
 
 
23
    def startTest(self, test):
 
24
        pass
 
25
 
 
26
    def stopTest(self, test):
 
27
        pass
 
28
 
 
29
    def addFailure(self, test, details):
 
30
        self.test_run.addTestResult(test.id(), status="Fail")
 
31
 
 
32
    def addSuccess(self, test, details):
 
33
        self.test_run.addTestResult(test.id(), status="Pass")
 
34
 
 
35
 
 
36
def parse_stream(project, stream):
 
37
    test_run = project.createTestRun()
 
38
 
 
39
    result = Result(test_run)
 
40
    protocol = ProtocolTestCase(stream)
 
41
    protocol.run(result)
 
42
 
 
43
def parse_filename(project, filename):
 
44
    stream = open(filename)
 
45
    try:
 
46
        parse_stream(project, stream)
 
47
    finally:
 
48
        stream.close()
 
49
    
 
50
def parse_filenames(project, filenames):
 
51
    for filename in filenames:
 
52
        parse_filename(project, filename)
 
53
    
 
54
def main(args):
 
55
    usage = "Usage: %prog [OPTIONS] PROJECT [FILE...]"
 
56
    parser = OptionParser(usage=usage)
 
57
    parser.add_option("--consumer-name",
 
58
        default=DEFAULT_CONSUMER_NAME,
 
59
        help="The consumer appropriate for this application")
 
60
    parser.add_option("--service-root",
 
61
        default=DEFAULT_SERVICE_ROOT,
 
62
        help="The URL to the root of the web service")
 
63
    (options, args) = parser.parse_args(args)
 
64
 
 
65
    # Get project from the Launchpad service root
 
66
    if not args:
 
67
        parser.error("Must provide a project name")
 
68
 
 
69
    launchpad = Launchpad.login_with(options.consumer_name, options.service_root)
 
70
    project = launchpad.projects[args.pop(0)]
 
71
 
 
72
    # Read from sdin
 
73
    if not args or (len(args) == 1 and args[0] == "-"):
 
74
        parse_stream(project, sys.stdin)
 
75
 
 
76
    # Or from filenames given as arguments
 
77
    else:
 
78
        parse_filenames(project, args)
 
79
 
 
80
    return 0
 
81
 
 
82
 
 
83
if __name__ == "__main__":
 
84
    sys.exit(main(sys.argv[1:]))