~ubuntu-branches/ubuntu/vivid/mago/vivid

« back to all changes in this revision

Viewing changes to mago/cmd/main.py

  • Committer: Bazaar Package Importer
  • Author(s): Ara Pulido
  • Date: 2009-08-04 09:21:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090804092140-mnc0rm2tr7smo107
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
This module provides the main entry point for the execution automated
 
3
desktop test cases
 
4
"""
 
5
 
 
6
import os, re, sys, logging
 
7
import logging
 
8
from logging import StreamHandler, FileHandler, Formatter
 
9
from itertools import chain
 
10
import ldtp
 
11
 
 
12
from . import globals
 
13
from .runner import TestSuiteRunner
 
14
from .parser import parse_options
 
15
from .utils import safe_make_directory, safe_run_command
 
16
from .discovery import discover_applications
 
17
 
 
18
 
 
19
def process_application(app_data, target_directory):
 
20
    """
 
21
    Process all test suites in an application
 
22
    """
 
23
    logging.debug("Processing application '%s' (%s)"
 
24
                  % (app_data.name, app_data.path))
 
25
    application_target = app_data.get_target_directory(target_directory)
 
26
    safe_make_directory(application_target)
 
27
 
 
28
    for suite_data in app_data.suites():
 
29
        process_suite(suite_data, application_target)
 
30
 
 
31
 
 
32
def process_suite(suite_data, application_target):
 
33
    """
 
34
    Run the suite test cases, make sure that the logs are stored
 
35
    in the correct directory and generate html report from them
 
36
    """
 
37
    runner = TestSuiteRunner(suite_data)
 
38
    results = runner.run()
 
39
 
 
40
    log_file = suite_data.get_log_filename(application_target)
 
41
    with open(log_file, 'w') as f:
 
42
        f.write(results)
 
43
    convert_log_file(log_file)
 
44
 
 
45
 
 
46
def convert_log_file(log_file):
 
47
    """
 
48
    Transform xml log format into html report
 
49
    """
 
50
    if os.path.exists(globals.SCREENSHOTS_SHARE):
 
51
        screenshot_dir = os.path.join(os.path.dirname(log_file),
 
52
                                      "screenshots")
 
53
 
 
54
        if not os.path.exists(screenshot_dir):            
 
55
            safe_make_directory(screenshot_dir)
 
56
 
 
57
        if len(os.listdir(globals.SCREENSHOTS_SHARE)) > 0:
 
58
            command = "mv " + globals.SCREENSHOTS_SHARE + "/* " + screenshot_dir
 
59
            safe_run_command(command)
 
60
 
 
61
    log_file_tmp = log_file + ".tmp"
 
62
    o = open(log_file_tmp, "w")
 
63
    data = open(log_file).read()
 
64
    o.write(re.sub(globals.SCREENSHOTS_SHARE, "screenshots", data))
 
65
    o.flush()
 
66
    o.close()
 
67
    
 
68
    os.remove(log_file)
 
69
    os.rename(log_file_tmp, log_file)
 
70
 
 
71
    xsl_file = os.path.join(globals.MAGO_SHARE, "report.xsl")
 
72
    if not os.path.exists(xsl_file):
 
73
        logging.error("XSL file `%s' does not exist." % xsl_file)
 
74
        sys.exit(1)
 
75
 
 
76
    html_file = log_file.replace(".log", ".html")
 
77
 
 
78
    command = "xsltproc -o %s %s %s" \
 
79
        % (html_file, xsl_file, log_file)
 
80
    safe_run_command(command)
 
81
 
 
82
 
 
83
def configure_logging(log_level_str, log_filename):
 
84
    """
 
85
    Configure log handlers
 
86
    """
 
87
    log_level = logging.getLevelName(log_level_str.upper())
 
88
    log_handlers = []
 
89
    log_handlers.append(StreamHandler())
 
90
    if log_filename:
 
91
        log_handlers.append(FileHandler(log_filename))
 
92
 
 
93
    format = ("%(asctime)s %(levelname)-8s %(message)s")
 
94
    if log_handlers:
 
95
        for handler in log_handlers:
 
96
            handler.setFormatter(Formatter(format))
 
97
            logging.getLogger().addHandler(handler)
 
98
        if log_level:
 
99
            logging.getLogger().setLevel(log_level)
 
100
    elif not logging.getLogger().handlers:
 
101
        logging.disable(logging.CRITICAL)
 
102
 
 
103
 
 
104
def main(args=sys.argv):
 
105
    """
 
106
    Execute automated tests
 
107
    """
 
108
    # For the moment, all applications should be running in English
 
109
    ldtp.setlocale("C")
 
110
 
 
111
    # Get shared directory based on the directory in which binary is located
 
112
    options = parse_options(args)
 
113
    configure_logging(options.log_level, options.log)
 
114
 
 
115
    logging.debug('MAGO_SHARE: %s' % globals.MAGO_SHARE)
 
116
    logging.debug('MAGO_PATH: %s' % ':'.join(globals.MAGO_PATH))
 
117
    logging.debug('SCREENSHOTS_SHARE: %s' % globals.SCREENSHOTS_SHARE)
 
118
 
 
119
    apps = discover_applications(globals.MAGO_PATH,
 
120
                                 options.applications,
 
121
                                 options.suite_names,
 
122
                                 options.suite_files,
 
123
                                 options.cases)
 
124
 
 
125
    # Execute test cases
 
126
    if not apps:
 
127
        logging.warning("No test applications found")
 
128
    else:
 
129
        if not options.info:
 
130
            for app in apps:
 
131
                process_application(app, options.target)
 
132
        else:
 
133
            for app in apps:
 
134
                print "Application: %s - %s" % (app.name, app.path)
 
135
                for suite in app.suites():
 
136
                    print "- Suite: %s - %s" % (suite.name, suite.filename)
 
137
                    for case in suite.cases():
 
138
                        print " - Case: %s" % case.name
 
139
 
 
140
    return 0
 
141
 
 
142
 
 
143
if __name__ == "__main__":
 
144
    sys.exit(main())