Welcome to mirror list, hosted at ThFree Co, Russian Federation.

diff-viewer.js « support « screenshot-testing « lib « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 92180e47305caed5fc80902c4aa1133f4fa939e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*!
 * Piwik - free/libre analytics platform
 *
 * Image diff & HTML diff viewer generation.
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

var fs = require('fs'),
    path = require('./path');

var DiffViewerGenerator = function (diffDir) {
    this.diffDir = diffDir;
    this.outputPath = path.join(diffDir, 'diffviewer.html');
    this.failures = [];
};

DiffViewerGenerator.prototype.getDiffPath = function (testInfo) {
    var baseDir = path.join(PIWIK_INCLUDE_PATH, 'tests/UI');
    return path.resolve(path.join(baseDir, config.screenshotDiffDir, testInfo.name));
};

// TODO: diff output path shouldn't be stored in piwik repo
DiffViewerGenerator.prototype.getUrlForPath = function (path) {
    return fs.relpath(path, this.diffDir);
};

DiffViewerGenerator.prototype.generate = function (callback) {
    if (this.failures.length == 0) {
        return callback();
    }

    console.log("Generating diff file");

    var diffViewerContent = "<html>\
<head></head>\
<body>\
<h1>Screenshot Test Failures</h1>\
<table>\
    <tr>\
        <th>Name</th>\
        <th>Expected</th>\
        <th>Expected Latest (Master)</th>\
        <th>Processed</th>\
        <th>Difference</th>\
    </tr>\n\n";

        for (var i = 0; i != this.failures.length; ++i) {
            var entry = this.failures[i];
            var expectedUrl = null;
            var githubUrl   = '';

            if (entry.expected) {
                if (options['assume-artifacts']) {
                    require('child_process').spawn('cp', [entry.expected, this.getDiffPath(entry)]);
                }

                var filename       = entry.name,
                    expectedUrl    = filename,
                    screenshotRepo = options['screenshot-repo'] || 'piwik/piwik',
                    pathPrefix     = options['screenshot-repo'] ? '/Test/UI' : '/tests/UI',
                    expectedUrlGithub = 'https://raw.githubusercontent.com/' + screenshotRepo + '/master' + pathPrefix
                                      + '/expected-screenshots/' + filename;

                var expectedHtml = '';

                if (!options['assume-artifacts']) {
                    expectedUrl = this.getUrlForPath(entry.expected);
                }

                expectedHtml += '<a href="' + expectedUrl + '">Expected</a>&nbsp;';
                githubUrl     = '<a href="' + expectedUrlGithub + '">GitHub</a>';
            } else {
                var expectedHtml = '<em>Not found</em>';
            }

            if (entry.processed) {
                if (options['assume-artifacts']) {
                    entry.processedUrl = path.join("../processed-ui-screenshots", path.basename(entry.processed));
                } else {
                    entry.processedUrl = this.getUrlForPath(entry.processed);
                }
            }

            var entryLocationHint = '',
                hintSource = entry.expected || entry.processed,
                m = hintSource ? hintSource.match(/\/plugins\/([^\/]*)\//) : null;
            if (m) {
                entryLocationHint = ' <em>(for ' + m[1] + ' plugin)</em>';
            }

            var processedEntryPath = '';
            if (entry.processed) {
                processedEntryPath = path.basename(entry.processed);
            }

            diffViewerContent += "\n\
    <tr>\n\
        <td>" + entry.name + entryLocationHint + "</td>\n\
        <td>" + expectedHtml + "</td>\n\
        <td>" + githubUrl + "</td>\n\
        <td>" + (entry.processed ? ("<a href='" + entry.processedUrl + "'>Processed</a>") : "<em>Not found</em>") + "</td>\n\
        <td>" + (expectedUrl ? ("<a href='singlediff.html?processed=" + entry.processedUrl + "&expected=" + expectedUrl + "&github=" + processedEntryPath + "'>Difference</a>") : "<em>Could not create diff.</em>") + "</td>\n\
    </tr>\n";
        }

        diffViewerContent += '\
</table>\
</body>\
</html>';

        fs.write(this.outputPath, diffViewerContent, "w");

        console.log("Failures encountered. View all diffs at: " + this.outputPath);
        console.log();
        console.log("If processed screenshots are correct, you can copy the generated screenshots to the expected "
                  + "screenshot folder.");
        console.log();
        console.log("*** IMPORTANT *** In your commit message, explain the cause of the difference in rendering so other "
                  + "Piwik developers will be aware of it.");

        callback();
};

exports.DiffViewerGenerator = DiffViewerGenerator;