dom/browser-element/mochitest/createNewTest.py

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 """A script to generate the browser-element test boilerplate.
michael@0 2
michael@0 3 This script requires Python 2.7."""
michael@0 4
michael@0 5 from __future__ import print_function
michael@0 6
michael@0 7 import sys
michael@0 8 import os
michael@0 9 import stat
michael@0 10 import argparse
michael@0 11 import textwrap
michael@0 12 import subprocess
michael@0 13
michael@0 14 html_template = textwrap.dedent("""\
michael@0 15 <!DOCTYPE HTML>
michael@0 16 <html>
michael@0 17 <head>
michael@0 18 <title>Test for Bug {bug}</title>
michael@0 19 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 20 <script type="application/javascript" src="browserElementTestHelpers.js"></script>
michael@0 21 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
michael@0 22 </head>
michael@0 23 <body>
michael@0 24 <script type="application/javascript;version=1.7" src="browserElement_{test}.js">
michael@0 25 </script>
michael@0 26 </body>
michael@0 27 </html>""")
michael@0 28
michael@0 29 # Note: Curly braces are escaped as "{{".
michael@0 30 js_template = textwrap.dedent("""\
michael@0 31 /* Any copyright is dedicated to the public domain.
michael@0 32 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 33
michael@0 34 // Bug {bug} - FILL IN TEST DESCRIPTION
michael@0 35 "use strict";
michael@0 36
michael@0 37 SimpleTest.waitForExplicitFinish();
michael@0 38 browserElementTestHelpers.setEnabledPref(true);
michael@0 39 browserElementTestHelpers.addPermission();
michael@0 40
michael@0 41 function runTest() {{
michael@0 42 var iframe = document.createElement('iframe');
michael@0 43 SpecialPowers.wrap(iframe).mozbrowser = true;
michael@0 44
michael@0 45 // FILL IN TEST
michael@0 46
michael@0 47 document.body.appendChild(iframe);
michael@0 48 }}
michael@0 49
michael@0 50 addEventListener('testready', runTest);
michael@0 51 """)
michael@0 52
michael@0 53 def print_fill(s):
michael@0 54 print(textwrap.fill(textwrap.dedent(s)))
michael@0 55
michael@0 56 def add_to_makefile(filenames):
michael@0 57 """Add a list of filenames to this directory's Makefile.in, then open
michael@0 58 $EDITOR and let the user move the filenames to their appropriate places in
michael@0 59 the file.
michael@0 60
michael@0 61 """
michael@0 62 lines_to_write = [''] + ['\t\t%s \\' % n for n in filenames]
michael@0 63 with open('Makefile.in', 'a') as f:
michael@0 64 f.write('\n'.join(lines_to_write))
michael@0 65
michael@0 66 if 'EDITOR' not in os.environ or not os.environ['EDITOR']:
michael@0 67 print_fill("""\
michael@0 68 Now open Makefile.in and move the filenames to their correct places.")
michael@0 69 (Define $EDITOR and I'll open your editor for you next time.)""")
michael@0 70 return
michael@0 71
michael@0 72 # Count the number of lines in Makefile.in.
michael@0 73 with open('Makefile.in', 'r') as f:
michael@0 74 num_lines = len(f.readlines())
michael@0 75
michael@0 76 try:
michael@0 77 subprocess.call([os.environ['EDITOR'],
michael@0 78 '+%d' % (num_lines - len(lines_to_write) + 2),
michael@0 79 'Makefile.in'])
michael@0 80 except Exception as e:
michael@0 81 print_fill("Error opening $EDITOR: %s." % str(e))
michael@0 82 print()
michael@0 83 print_fill("""\
michael@0 84 Please open Makefile.in and move the filenames at the bottom of the
michael@0 85 file to their correct places.""")
michael@0 86
michael@0 87 def main(test_name, bug_number):
michael@0 88 global html_template, js_template
michael@0 89
michael@0 90 def format(str):
michael@0 91 return str.format(bug=bug_number, test=test_name)
michael@0 92
michael@0 93 def create_file(filename, template):
michael@0 94 path = os.path.join(os.path.dirname(sys.argv[0]), format(filename))
michael@0 95 # Create a new file, bailing with an error if the file exists.
michael@0 96 fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL)
michael@0 97
michael@0 98 try:
michael@0 99 # This file has 777 permission when created, for whatever reason. Make it rw-rw-r---.
michael@0 100 os.fchmod(fd, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH)
michael@0 101 except:
michael@0 102 # fchmod doesn't work on Windows.
michael@0 103 pass
michael@0 104
michael@0 105 with os.fdopen(fd, 'w') as file:
michael@0 106 file.write(format(template))
michael@0 107
michael@0 108 create_file('browserElement_{test}.js', js_template)
michael@0 109 create_file('test_browserElement_inproc_{test}.html', html_template)
michael@0 110 create_file('test_browserElement_oop_{test}.html', html_template)
michael@0 111
michael@0 112 add_to_makefile([format(x) for x in ['browserElement_{test}.js',
michael@0 113 'test_browserElement_inproc_{test}.html',
michael@0 114 'test_browserElement_oop_{test}.html']])
michael@0 115
michael@0 116 if __name__ == '__main__':
michael@0 117 parser = argparse.ArgumentParser(description="Create a new browser-element testcase.")
michael@0 118 parser.add_argument('test_name')
michael@0 119 parser.add_argument('bug_number', type=int)
michael@0 120 args = parser.parse_args()
michael@0 121 main(args.test_name, args.bug_number)

mercurial