tor-browser

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

make_tests.py (2682B)


      1 # This tool creates .html test files for the WPT harness from corresponding .test
      2 # files that it finds in the tree for this test collection.
      3 
      4 import re
      5 import time
      6 import json
      7 import fnmatch
      8 import os
      9 import shutil
     10 import sys
     11 import argparse
     12 
     13 TESTTREE = '..'
     14 DEFDIR   = '../definitions'
     15 MANUAL_TEMPLATE = 'template_manual'
     16 JS_TEMPLATE = 'template_js'
     17 
     18 parser = argparse.ArgumentParser()
     19 
     20 parser.add_argument('--examples', action="store_const", const=1)
     21 
     22 args = parser.parse_args()
     23 
     24 # pull in the template
     25 
     26 manualTemplate = open(MANUAL_TEMPLATE, "r").read()
     27 autoTemplate = open(JS_TEMPLATE, "r").read()
     28 
     29 defList = []
     30 defnames = ""
     31 
     32 # find all of the definitions
     33 for curdir, subdirList, fileList in os.walk(DEFDIR, topdown=True):
     34  for file in fnmatch.filter(fileList, "*.json"):
     35    theFile = os.path.join(curdir, file)
     36    try:
     37      testJSON = json.load(open(theFile, "r"))
     38    except ValueError as e:
     39      print("parse of " + theFile + " failed: " + e[0])
     40    else:
     41      theFile = re.sub(r"\.\./", "", theFile)
     42      defList.append(theFile)
     43 
     44 if (len(defList)):
     45    defNames = '"' + '",\n  "'.join(defList) + '"'
     46 
     47 
     48 # iterate over the folders looking for .test files
     49 
     50 for curdir, subdirList, fileList in os.walk(TESTTREE, topdown=True):
     51  # skip the definitions directory
     52  subdirList[:] = [d for d in subdirList if d != "definitions"]
     53  # skip the examples directory
     54  if args.examples != 1:
     55    subdirList[:] = [d for d in subdirList if d != "examples"]
     56 
     57  for file in fnmatch.filter(fileList, "*.test"):
     58 # for each .test file, create a corresponding .html file using the appropriate
     59 # template
     60    theFile = os.path.join(curdir, file)
     61    try:
     62      testJSON = json.load(open(theFile, "r"))
     63    except ValueError as e:
     64      print("parse of " + theFile + " failed: " + e[0])
     65    else:
     66      try:
     67        testType = testJSON['testType']
     68      except:
     69        testType = "manual"
     70 
     71      templateFile = manualTemplate
     72      suffix = "-manual.html"
     73 
     74      if testType == "automated":
     75        templateFile = autoTemplate
     76        suffix = ".html"
     77 
     78      rfile = re.sub(r"\.\./", "", file)
     79      # interesting pattern is {{TESTFILE}}
     80      tcopy = re.sub("{{TESTFILE}}", rfile, templateFile)
     81 
     82      tcopy = re.sub("{{SCHEMADEFS}}", defNames, tcopy)
     83 
     84      try:
     85        title = testJSON['name']
     86      except:
     87        title = file
     88      tcopy = re.sub("{{TESTTITLE}}", title, tcopy)
     89 
     90      # target file is basename of theFile + '-manual.html'
     91      target = re.sub(r"\.test",suffix, theFile)
     92 
     93      try:
     94        out = open(target, "w")
     95        out.write(tcopy)
     96        out.close()
     97      except:
     98        print("Failed to create "+target)
     99      else:
    100        print("Created " + target)