tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

mochi-to-testcase.py (3125B)


      1 #! /usr/bin/env python3
      2 import pathlib
      3 import re
      4 import sys
      5 
      6 assert len(sys.argv) == 2
      7 MOCHI_PATH = pathlib.Path(sys.argv[1])
      8 assert MOCHI_PATH.suffix == ".html"
      9 
     10 TEST_PATH = MOCHI_PATH.with_suffix(".solo.html")
     11 
     12 
     13 def read_local_file(include):
     14    inc_path = MOCHI_PATH.parent
     15    file_path = inc_path / include
     16 
     17    try:
     18        return file_path.read_bytes()
     19    except OSError:
     20        return b""
     21 
     22 
     23 SIMPLETEST_REPLACEMENT = b"""
     24 
     25 <script>
     26 // SimpleTest.js replacement
     27 
     28 function debug(text) {
     29  var elem = document.getElementById('mochi-to-testcase-output');
     30  elem.innerHTML += '\\n<br/>\\n' + text;
     31 }
     32 
     33 function ok(val, text) {
     34  var status = val ? 'Test <font color=\\'green\\'>passed</font>: '
     35                   : 'Test <font color=\\'red\\'  >FAILED</font>: ';
     36  debug(status + text);
     37 }
     38 
     39 function todo(val, text) {
     40  var status = val ? 'Test <font color=\\'orange\\'>UNEXPECTED PASS</font>: '
     41                   : 'Test <font color=\\'blue\\'  >todo</font>: ';
     42  debug(status + text);
     43 }
     44 
     45 function addLoadEvent(func) {
     46  window.addEventListener('load', func);
     47 }
     48 
     49 SimpleTest = {
     50  waitForExplicitFinish: () => {},
     51  finish: () => {},
     52  requestFlakyTimeout: () => {},
     53 };
     54 
     55 SpecialPowers = {
     56  pushPrefEnv: async (env, callback = null) => {
     57    console.log(`SpecialPowers.pushPrefEnv(${JSON.stringify(env)})`);
     58    await new Promise(res => {
     59        setTimeout(res, 0);
     60    });
     61    if (callback) {
     62        await callback();
     63    }
     64  },
     65  popPrefEnv: async (callback = null) => {
     66    console.log('SpecialPowers.popPrefEnv()');
     67    await new Promise(res => {
     68        setTimeout(res, 0);
     69    });
     70    if (callback) {
     71        await callback();
     72    }
     73  },
     74 };
     75 </script>
     76 <div id='mochi-to-testcase-output'></div>
     77 
     78 """
     79 
     80 INCLUDE_PATTERN = re.compile(b"<script\\s*src=['\"](.*)\\.js['\"]>\\s*</script>")
     81 CSS_PATTERN = re.compile(
     82    b"<link\\s*rel=['\"]stylesheet['\"]\\s*href=['\"]([^=>]*)['\"]>"
     83 )
     84 
     85 with open(TEST_PATH, "wb") as fout:
     86    with open(MOCHI_PATH, "rb") as fin:
     87        for line in fin:
     88            skip_line = False
     89            for css in CSS_PATTERN.findall(line):
     90                skip_line = True
     91                print("Ignoring stylesheet: " + css.decode())
     92 
     93            for inc in INCLUDE_PATTERN.findall(line):
     94                skip_line = True
     95                if inc == b"/MochiKit/MochiKit":
     96                    continue
     97 
     98                if inc == b"/tests/SimpleTest/SimpleTest":
     99                    print("Injecting SimpleTest replacement")
    100                    fout.write(SIMPLETEST_REPLACEMENT)
    101                    continue
    102 
    103                inc_js = inc.decode() + ".js"
    104                inc_data = read_local_file(inc_js)
    105                if not inc_data:
    106                    print("Warning: Unknown JS file ignored: " + inc_js)
    107                    continue
    108 
    109                print("Injecting include: " + inc_js)
    110                fout.write(b"\n<script>\n// Imported from: " + inc_js.encode() + b"\n")
    111                fout.write(inc_data)
    112                fout.write(b"\n</script>\n")
    113                continue
    114 
    115            if skip_line:
    116                continue
    117 
    118            fout.write(line)
    119            continue