HTMLReport.py 11.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# HTMLReport.py
#
# Copyright (C) 2012 Carlos Garcia Campos <carlosgc@gnome.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
from __future__ import absolute_import, division, print_function
19 20 21 22

from backends import get_backend, get_all_backends
from Config import Config
import os
23
import errno
24 25
import subprocess

26 27
class HTMLPrettyDiff:

28
    def write(self, test, outdir, actual, expected, diff):
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
        raise NotImplementedError

    def _create_diff_for_test(self, outdir, test):
        diffdir = os.path.join(outdir, 'html', test)
        try:
            os.makedirs(diffdir)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise
        except:
            raise
        return diffdir

class HTMLPrettyDiffImage(HTMLPrettyDiff):

44 45 46 47
    def write(self, test, outdir, result, actual, expected, diff):
        def get_relative_path(path):
            return '../' * len(path.split('/')) + path

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        html = """
<html>
<head>
<title>%s</title>
<style>.label{font-weight:bold}</style>
</head>
<body>
Difference between images: <a href="%s">diff</a><br>
<div class=imageText></div>
<div class=imageContainer
     actual="%s"
     expected="%s">Loading...</div>
<script>
(function() {
    var preloadedImageCount = 0;
    function preloadComplete() {
        ++preloadedImageCount;
        if (preloadedImageCount < 2)
            return;
        toggleImages();
        setInterval(toggleImages, 2000)
    }

    function preloadImage(url) {
        image = new Image();
        image.addEventListener('load', preloadComplete);
        image.src = url;
        return image;
    }

    function toggleImages() {
        if (text.textContent == 'Expected Image') {
            text.textContent = 'Actual Image';
            container.replaceChild(actualImage, container.firstChild);
        } else {
            text.textContent = 'Expected Image';
            container.replaceChild(expectedImage, container.firstChild);
        }
    }

    var text = document.querySelector('.imageText');
    var container = document.querySelector('.imageContainer');
    var actualImage = preloadImage(container.getAttribute('actual'));
    var expectedImage = preloadImage(container.getAttribute('expected'));
})();
</script>
</body>
</html>
96
""" % (test, get_relative_path(diff), get_relative_path(actual), expected)
97 98

        diffdir = self._create_diff_for_test(outdir, test)
99 100
        pretty_diff_name = result + '-pretty-diff.html'
        pretty_diff = os.path.abspath(os.path.join(diffdir, pretty_diff_name))
101 102 103 104
        f = open(pretty_diff, 'w')
        f.write(html)
        f.close()

105
        return os.path.join(test, pretty_diff_name)
106 107

class HTMLPrettyDiffText(HTMLPrettyDiff):
108
    def write(self, test, outdir, result, actual, expected, diff):
109 110
        import difflib

111
        actual_file = open(os.path.join(outdir, actual), 'r')
112 113 114 115 116 117 118 119
        expected_file = open(expected, 'r')
        html = difflib.HtmlDiff().make_file(actual_file.readlines(),
                                            expected_file.readlines(),
                                            "Actual", "Expected", context=True)
        actual_file.close()
        expected_file.close()

        diffdir = self._create_diff_for_test(outdir, test)
120 121
        pretty_diff_name = result + '-pretty-diff.html'
        pretty_diff = os.path.abspath(os.path.join(diffdir, pretty_diff_name))
122 123 124 125
        f = open(pretty_diff, 'w')
        f.write(html)
        f.close()

126
        return os.path.join(test, pretty_diff_name)
127 128 129 130

def create_pretty_diff(backend):
    if backend.get_diff_ext() == '.diff.png':
        return HTMLPrettyDiffImage()
131 132 133 134 135
    # Disable pretty diff for Text files for now, since HtmlDiff().make_file() is
    # entering in an infinite loop with some files. We need to either fix that somehow or
    # find a different way to generate pretty diffs of text files.
    #if backend.get_diff_ext() == '.diff':
    #    return HTMLPrettyDiffText()
136 137
    return None

138 139 140 141 142 143 144
class BackendTestResult:

    def __init__(self, test, refsdir, outdir, backend, results):
        self._test = test
        self._refsdir = refsdir
        self._outdir = outdir
        self._backend = backend
145
        self.config = Config()
146 147 148 149

        self._results = []

        ref_path = os.path.join(self._refsdir, self._test)
150 151
        if not backend.has_md5(ref_path):
            return
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
        ref_names = backend.get_ref_names(ref_path)
        for result in results:
            basename = os.path.basename(result)
            if basename in ref_names:
                self._results.append(basename)

    def is_failed(self):
        return len(self._results) > 0

    def is_crashed(self):
        return self._backend.is_crashed(os.path.join(self._outdir, self._test))

    def is_failed_to_run(self):
        return self._backend.is_failed(os.path.join(self._outdir, self._test))

    def get_stderr(self):
        return self._backend.get_stderr(os.path.join(self._outdir, self._test))

    def get_failed_html(self):
        html = ""
        for result in self._results:
173 174
            actual = os.path.join(self._test, result)
            actual_path = os.path.join(self._outdir, actual)
175 176 177
            expected = os.path.join(self._refsdir, self._test, result)
            if self.config.abs_paths:
                expected = os.path.abspath(expected)
178 179
            html += "<li><a href='../%s'>actual</a> <a href='%s'>expected</a> " % (actual, expected)
            if self._backend.has_diff(actual_path):
180
                diff = actual + self._backend.get_diff_ext()
181
                html += "<a href='../%s'>diff</a> " % (diff)
182 183 184
                if self.config.pretty_diff:
                    pretty_diff = create_pretty_diff(self._backend)
                    if pretty_diff:
185
                        html += "<a href='%s'>pretty diff</a> " % (pretty_diff.write (self._test, self._outdir, result, actual, expected, diff))
186 187 188 189 190 191 192 193 194 195 196 197
            html += "</li>\n"

        if html:
            return "<ul>%s</ul>\n" % (html)
        return ""


class TestResult:

    def __init__(self, docsdir, refsdir, outdir, resultdir, results, backends):
        self._refsdir = refsdir
        self._outdir = outdir
198
        self.config = Config()
199

200
        self._test = resultdir[len(self._outdir):].lstrip('/')
201 202 203
        self._doc = os.path.join(docsdir, self._test)
        self._results = {}
        for backend in backends:
204 205 206
            ref_path = os.path.join(self._refsdir, self._test)
            if not backend.has_md5(ref_path) and not backend.is_crashed(ref_path) and not backend.is_failed(ref_path):
                continue
207 208 209 210 211 212 213 214 215 216 217 218 219 220
            self._results[backend] = BackendTestResult(self._test, refsdir, outdir, backend, results)

    def get_test(self):
        return self._test

    def is_failed(self):
        for backend in self._results:
            if self._results[backend].is_failed():
                return True
        return False;

    def get_failed_html(self):
        html = ""
        for backend in self._results:
221 222
            if self._results[backend].is_crashed() or self._results[backend].is_failed_to_run():
                continue
223 224 225 226 227 228 229
            backend_html = self._results[backend].get_failed_html()
            if not backend_html:
                continue

            html += "<li>%s " % (backend.get_name())
            stderr = self._results[backend].get_stderr()
            if os.path.exists(stderr):
230 231
                stderr_name = stderr[len(self._outdir):].lstrip('/')
                html += "<a href='../%s'>stderr</a>" % (stderr_name)
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
            html += "</li>\n%s" % (backend_html)

        if html:
            return "<h2><a name='%s'><a href='%s'>%s</a></a></h2>\n<ul>%s</ul><a href='#top'>Top</a>\n" % (self._test, self._doc, self._test, html)
        return ""

    def get_crashed_html(self):
        html = ""
        for backend in self._results:
            if not self._results[backend].is_crashed():
                continue

            html += "<li><a href='%s'>%s</a> (%s)</li>\n" % (self._doc, self._test, backend.get_name())

        if html:
            return "<ul>%s</ul>\n" % (html)
        return ""

    def get_failed_to_run_html(self):
        html = ""
        for backend in self._results:
            status = self._results[backend].is_failed_to_run()
            if not status:
                continue

            html += "<li><a href='%s'>%s</a> [Status: %d] (%s)</li>\n" % (self._doc, self._test, status, backend.get_name())

        if html:
            return "<ul>%s</ul>\n" % (html)
        return ""

class HTMLReport:

    def __init__(self, docsdir, refsdir, outdir):
        self._docsdir = docsdir
        self._refsdir = refsdir
        self._outdir = outdir
269
        self._htmldir = os.path.join(outdir, 'html')
270 271
        self.config = Config()

272 273 274 275 276 277 278 279
        try:
            os.makedirs(self._htmldir)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise
        except:
            raise

280
    def create(self, launch_browser):
281
        html = "<html><body><a name='top'></a>"
282 283
        if os.path.exists(os.path.join(self._outdir, '.exited_early')):
            html += "<p style='border: 3px solid red; font-weight: bold; display: inline-block; padding: 3px'>Testing exited early</p>"
284 285 286 287 288 289 290 291 292 293 294
        if self.config.backends:
            backends = [get_backend(name) for name in self.config.backends]
        else:
            backends = get_all_backends()

        results = {}
        for root, dirs, files in os.walk(self._outdir, False):
            if not files:
                continue
            if not root.lower().endswith('.pdf'):
                continue
295 296
            if root.startswith(self._htmldir):
                continue
297 298 299 300 301 302 303

            results[root] = TestResult(self._docsdir, self._refsdir, self._outdir, root, files, backends)

        failed_anchors = []
        failed = ""
        crashed = ""
        failed_to_run = ""
304
        for test_name, test in sorted(results.items()):
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
            if test.is_failed():
                failed_anchors.append(test.get_test())
                failed += test.get_failed_html()
            crashed += test.get_crashed_html()
            failed_to_run += test.get_failed_to_run_html()

        if failed:
            failed = "<h1><a name='failed'>Tests Failed (differences were found)</name></h1>\n%s" % (failed)
        if crashed:
            crashed = "<h1><a name='crashed'>Tests Crashed</a></h1>\n%s" % (crashed)
        if failed_to_run:
            failed_to_run = "<h1><a name='failed_to_run'>Tests that failed to run (command returned an error status)</a></h1>\n%s" % (failed_to_run)

        if failed or crashed or failed_to_run:
            html += "<ul>\n"
            if failed:
                html += "<li><a href='#failed'>Tests Failed (differences were found)</a></li>\n<ul>"
                for anchor in failed_anchors:
                    html += "<li><a href='#%s'>%s</a></li>" % (anchor, anchor)
                html += "</ul>\n"
            if crashed:
326
                html += "<li><a href='#crashed'>Tests Crashed</a></li>\n"
327 328 329 330 331 332
            if failed_to_run:
                html += "<li><a href='#failed_to_run'>Tests that failed to run (command returned an error status)</a></li>\n"
            html += "</ul>\n"

        html += failed + crashed + failed_to_run + "</body></html>"

333
        report_index = os.path.join(self._htmldir, 'index.html')
334 335
        with open(report_index, 'w') as f:
            f.write(html)
336

337 338
        if launch_browser:
            subprocess.Popen(['xdg-open', report_index])