dom/browser-element/mochitest/createNewTest.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/browser-element/mochitest/createNewTest.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,121 @@
     1.4 +"""A script to generate the browser-element test boilerplate.
     1.5 +
     1.6 +This script requires Python 2.7."""
     1.7 +
     1.8 +from __future__ import print_function
     1.9 +
    1.10 +import sys
    1.11 +import os
    1.12 +import stat
    1.13 +import argparse
    1.14 +import textwrap
    1.15 +import subprocess
    1.16 +
    1.17 +html_template = textwrap.dedent("""\
    1.18 +    <!DOCTYPE HTML>
    1.19 +    <html>
    1.20 +    <head>
    1.21 +      <title>Test for Bug {bug}</title>
    1.22 +      <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.23 +      <script type="application/javascript" src="browserElementTestHelpers.js"></script>
    1.24 +      <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    1.25 +    </head>
    1.26 +    <body>
    1.27 +    <script type="application/javascript;version=1.7" src="browserElement_{test}.js">
    1.28 +    </script>
    1.29 +    </body>
    1.30 +    </html>""")
    1.31 +
    1.32 +# Note: Curly braces are escaped as "{{".
    1.33 +js_template = textwrap.dedent("""\
    1.34 +    /* Any copyright is dedicated to the public domain.
    1.35 +       http://creativecommons.org/publicdomain/zero/1.0/ */
    1.36 +
    1.37 +    // Bug {bug} - FILL IN TEST DESCRIPTION
    1.38 +    "use strict";
    1.39 +
    1.40 +    SimpleTest.waitForExplicitFinish();
    1.41 +    browserElementTestHelpers.setEnabledPref(true);
    1.42 +    browserElementTestHelpers.addPermission();
    1.43 +
    1.44 +    function runTest() {{
    1.45 +      var iframe = document.createElement('iframe');
    1.46 +      SpecialPowers.wrap(iframe).mozbrowser = true;
    1.47 +
    1.48 +      // FILL IN TEST
    1.49 +
    1.50 +      document.body.appendChild(iframe);
    1.51 +    }}
    1.52 +
    1.53 +    addEventListener('testready', runTest);
    1.54 +    """)
    1.55 +
    1.56 +def print_fill(s):
    1.57 +    print(textwrap.fill(textwrap.dedent(s)))
    1.58 +
    1.59 +def add_to_makefile(filenames):
    1.60 +    """Add a list of filenames to this directory's Makefile.in, then open
    1.61 +    $EDITOR and let the user move the filenames to their appropriate places in
    1.62 +    the file.
    1.63 +
    1.64 +    """
    1.65 +    lines_to_write = [''] + ['\t\t%s \\' % n for n in filenames]
    1.66 +    with open('Makefile.in', 'a') as f:
    1.67 +        f.write('\n'.join(lines_to_write))
    1.68 +
    1.69 +    if 'EDITOR' not in os.environ or not os.environ['EDITOR']:
    1.70 +        print_fill("""\
    1.71 +            Now open Makefile.in and move the filenames to their correct places.")
    1.72 +            (Define $EDITOR and I'll open your editor for you next time.)""")
    1.73 +        return
    1.74 +
    1.75 +    # Count the number of lines in Makefile.in.
    1.76 +    with open('Makefile.in', 'r') as f:
    1.77 +        num_lines = len(f.readlines())
    1.78 +
    1.79 +    try:
    1.80 +        subprocess.call([os.environ['EDITOR'],
    1.81 +                         '+%d' % (num_lines - len(lines_to_write) + 2),
    1.82 +                         'Makefile.in'])
    1.83 +    except Exception as e:
    1.84 +        print_fill("Error opening $EDITOR: %s." % str(e))
    1.85 +        print()
    1.86 +        print_fill("""\
    1.87 +            Please open Makefile.in and move the filenames at the bottom of the
    1.88 +            file to their correct places.""")
    1.89 +
    1.90 +def main(test_name, bug_number):
    1.91 +    global html_template, js_template
    1.92 +
    1.93 +    def format(str):
    1.94 +        return str.format(bug=bug_number, test=test_name)
    1.95 +
    1.96 +    def create_file(filename, template):
    1.97 +        path = os.path.join(os.path.dirname(sys.argv[0]), format(filename))
    1.98 +        # Create a new file, bailing with an error if the file exists.
    1.99 +        fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL)
   1.100 +
   1.101 +        try:
   1.102 +            # This file has 777 permission when created, for whatever reason.  Make it rw-rw-r---.
   1.103 +            os.fchmod(fd, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH)
   1.104 +        except:
   1.105 +            # fchmod doesn't work on Windows.
   1.106 +            pass
   1.107 +
   1.108 +        with os.fdopen(fd, 'w') as file:
   1.109 +            file.write(format(template))
   1.110 +
   1.111 +    create_file('browserElement_{test}.js', js_template)
   1.112 +    create_file('test_browserElement_inproc_{test}.html', html_template)
   1.113 +    create_file('test_browserElement_oop_{test}.html', html_template)
   1.114 +
   1.115 +    add_to_makefile([format(x) for x in ['browserElement_{test}.js',
   1.116 +                                         'test_browserElement_inproc_{test}.html',
   1.117 +                                         'test_browserElement_oop_{test}.html']])
   1.118 +
   1.119 +if __name__ == '__main__':
   1.120 +    parser = argparse.ArgumentParser(description="Create a new browser-element testcase.")
   1.121 +    parser.add_argument('test_name')
   1.122 +    parser.add_argument('bug_number', type=int)
   1.123 +    args = parser.parse_args()
   1.124 +    main(args.test_name, args.bug_number)

mercurial