summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-mochitest/mochi-to-testcase.py
blob: c2e84eec07144140ea7fb99f1fb0f2ccd826b4c3 (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
#! /usr/bin/env python3
import sys
import pathlib
import re

assert len(sys.argv) == 2
MOCHI_PATH = pathlib.Path(sys.argv[1])
assert MOCHI_PATH.suffix == ".html"

TEST_PATH = MOCHI_PATH.with_suffix(".solo.html")


def read_local_file(include):
    inc_path = MOCHI_PATH.parent
    file_path = inc_path / include

    try:
        return file_path.read_bytes()
    except IOError:
        return b""


SIMPLETEST_REPLACEMENT = b"""

<script>
// SimpleTest.js replacement

function debug(text) {
  var elem = document.getElementById('mochi-to-testcase-output');
  elem.innerHTML += '\\n<br/>\\n' + text;
}

function ok(val, text) {
  var status = val ? 'Test <font color=\\'green\\'>passed</font>: '
                   : 'Test <font color=\\'red\\'  >FAILED</font>: ';
  debug(status + text);
}

function todo(val, text) {
  var status = val ? 'Test <font color=\\'orange\\'>UNEXPECTED PASS</font>: '
                   : 'Test <font color=\\'blue\\'  >todo</font>: ';
  debug(status + text);
}

function addLoadEvent(func) {
  window.addEventListener('load', func, false);
}

SimpleTest = {
  waitForExplicitFinish: function() {},
  finish: function() {},
  requestFlakyTimeout: function() {},
};

SpecialPowers = {
  pushPrefEnv: function(env, func) {
    console.log('SpecialPowers.pushPrefEnv: ' + JSON.stringify(env));
    setTimeout(func, 0);
  },
};
</script>
<div id='mochi-to-testcase-output'></div>

"""

INCLUDE_PATTERN = re.compile(b"<script\\s*src=['\"](.*)\\.js['\"]>\\s*</script>")
CSS_PATTERN = re.compile(
    b"<link\\s*rel=['\"]stylesheet['\"]\\s*href=['\"]([^=>]*)['\"]>"
)

with open(TEST_PATH, "wb") as fout:
    with open(MOCHI_PATH, "rb") as fin:
        for line in fin:
            skip_line = False
            for css in CSS_PATTERN.findall(line):
                skip_line = True
                print("Ignoring stylesheet: " + css.decode())

            for inc in INCLUDE_PATTERN.findall(line):
                skip_line = True
                if inc == b"/MochiKit/MochiKit":
                    continue

                if inc == b"/tests/SimpleTest/SimpleTest":
                    print("Injecting SimpleTest replacement")
                    fout.write(SIMPLETEST_REPLACEMENT)
                    continue

                inc_js = inc.decode() + ".js"
                inc_data = read_local_file(inc_js)
                if not inc_data:
                    print("Warning: Unknown JS file ignored: " + inc_js)
                    continue

                print("Injecting include: " + inc_js)
                fout.write(b"\n<script>\n// Imported from: " + inc_js.encode() + b"\n")
                fout.write(inc_data)
                fout.write(b"\n</script>\n")
                continue

            if skip_line:
                continue

            fout.write(line)
            continue