tor-browser

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

build.py (3474B)


      1 import os
      2 import glob
      3 import shutil
      4 from os import path
      5 
      6 
      7 TEST_FILE_PATTERN = "support/**.test"
      8 TEST_OUTPUT_PATH = "tests"
      9 
     10 TEMPLATE = """\
     11 <!doctype html>
     12 <!-- DO NOT EDIT! This file and %vtt_file_rel_path are generated. -->
     13 <!-- See /webvtt/parsing/file-parsing/README.md -->
     14 <meta charset=utf-8>
     15 <title>WebVTT parser test: %test_name</title>
     16 %test_headers
     17 <script src="/resources/testharness.js"></script>
     18 <script src="/resources/testharnessreport.js"></script>
     19 <div id=log></div>
     20 <script>
     21 var t = async_test('%test_name');
     22 t.step(function(){
     23    var video = document.createElement('video');
     24    var track = document.createElement('track');
     25    assert_true('src' in track, 'track element not supported');
     26    track.src = '%vtt_file_rel_path';
     27    track['default'] = true;
     28    track.kind = 'subtitles';
     29    track.onload = this.step_func(trackLoaded);
     30    track.onerror = this.step_func(trackError);
     31    video.appendChild(track);
     32    document.body.appendChild(video);
     33 });
     34 
     35 function trackLoaded(event) {
     36    var track = event.target;
     37    var video = track.parentNode;
     38    var cues = video.textTracks[0].cues;
     39    {
     40 %test_js
     41    }
     42    this.done();
     43 }
     44 
     45 function trackError(e) {
     46    assert_unreached('got unexpected error event');
     47 }
     48 </script>
     49 """
     50 
     51 def generate_test(test_path, output_dir):
     52    # Read test file
     53    test_filename = path.basename(test_path)
     54    test_basefilename = path.splitext(test_filename)[0]
     55 
     56    with open(test_path, 'r') as test:
     57        test_source = test.read()
     58 
     59    # Split test header
     60    splits = test_source.split('\n\n', 1)
     61    if len(splits) != 2:
     62        raise ValueError("Leave an empty line between the test header and body")
     63 
     64    test_header, test_body = splits
     65 
     66    # Split header into name + html headers
     67    splits = test_header.split('\n', 1)
     68 
     69    test_name = splits[0]
     70    if len(splits) == 2:
     71        test_headers = splits[1]
     72 
     73    # Split body into js + vtt
     74    splits = test_body.split('\n===\n', 1)
     75    if len(splits) != 2:
     76        raise ValueError("Use === to separate the js and vtt parts")
     77 
     78    test_js, test_vtt = splits
     79 
     80    # Get output paths
     81    os.makedirs(output_dir, exist_ok=True)
     82    html_file_path = path.join(output_dir, test_basefilename + '.html')
     83 
     84    vtt_file_dir = path.join(output_dir, 'support')
     85    os.makedirs(vtt_file_dir, exist_ok=True)
     86 
     87    vtt_file_name = test_basefilename + '.vtt'
     88    vtt_file_path = path.join(vtt_file_dir, vtt_file_name)
     89    vtt_file_rel_path = path.join('support', vtt_file_name)
     90 
     91    # Write html file
     92    with open(html_file_path, 'w') as output:
     93        html = (TEMPLATE.replace('%test_name', test_name)
     94                        .replace('%test_headers', test_headers)
     95                        .replace('%test_js', test_js)
     96                        .replace('%vtt_file_rel_path', vtt_file_rel_path))
     97        output.write(html)
     98 
     99    # Write vtt file
    100    with open(vtt_file_path, 'w') as output:
    101        encoded = bytes(test_vtt, "utf-8").decode("unicode_escape")
    102        output.write(encoded)
    103 
    104 def main():
    105    file_parsing_path = path.normpath(path.join(path.dirname(__file__), ".."))
    106 
    107    test_output_path = path.join(file_parsing_path, TEST_OUTPUT_PATH)
    108 
    109    tests_pattern = path.join(file_parsing_path, TEST_FILE_PATTERN)
    110 
    111    # Clean test directory
    112    shutil.rmtree(test_output_path)
    113 
    114    # Generate tests
    115    for file in glob.glob(tests_pattern):
    116        print('Building test files for: ' + file)
    117        generate_test(file, test_output_path)
    118 
    119 if __name__ == '__main__':
    120    main()