michael@0: """A script to generate the browser-element test boilerplate.
michael@0:
michael@0: This script requires Python 2.7."""
michael@0:
michael@0: from __future__ import print_function
michael@0:
michael@0: import sys
michael@0: import os
michael@0: import stat
michael@0: import argparse
michael@0: import textwrap
michael@0: import subprocess
michael@0:
michael@0: html_template = textwrap.dedent("""\
michael@0:
michael@0:
michael@0:
michael@0: Test for Bug {bug}
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0:
michael@0: """)
michael@0:
michael@0: # Note: Curly braces are escaped as "{{".
michael@0: js_template = textwrap.dedent("""\
michael@0: /* Any copyright is dedicated to the public domain.
michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0:
michael@0: // Bug {bug} - FILL IN TEST DESCRIPTION
michael@0: "use strict";
michael@0:
michael@0: SimpleTest.waitForExplicitFinish();
michael@0: browserElementTestHelpers.setEnabledPref(true);
michael@0: browserElementTestHelpers.addPermission();
michael@0:
michael@0: function runTest() {{
michael@0: var iframe = document.createElement('iframe');
michael@0: SpecialPowers.wrap(iframe).mozbrowser = true;
michael@0:
michael@0: // FILL IN TEST
michael@0:
michael@0: document.body.appendChild(iframe);
michael@0: }}
michael@0:
michael@0: addEventListener('testready', runTest);
michael@0: """)
michael@0:
michael@0: def print_fill(s):
michael@0: print(textwrap.fill(textwrap.dedent(s)))
michael@0:
michael@0: def add_to_makefile(filenames):
michael@0: """Add a list of filenames to this directory's Makefile.in, then open
michael@0: $EDITOR and let the user move the filenames to their appropriate places in
michael@0: the file.
michael@0:
michael@0: """
michael@0: lines_to_write = [''] + ['\t\t%s \\' % n for n in filenames]
michael@0: with open('Makefile.in', 'a') as f:
michael@0: f.write('\n'.join(lines_to_write))
michael@0:
michael@0: if 'EDITOR' not in os.environ or not os.environ['EDITOR']:
michael@0: print_fill("""\
michael@0: Now open Makefile.in and move the filenames to their correct places.")
michael@0: (Define $EDITOR and I'll open your editor for you next time.)""")
michael@0: return
michael@0:
michael@0: # Count the number of lines in Makefile.in.
michael@0: with open('Makefile.in', 'r') as f:
michael@0: num_lines = len(f.readlines())
michael@0:
michael@0: try:
michael@0: subprocess.call([os.environ['EDITOR'],
michael@0: '+%d' % (num_lines - len(lines_to_write) + 2),
michael@0: 'Makefile.in'])
michael@0: except Exception as e:
michael@0: print_fill("Error opening $EDITOR: %s." % str(e))
michael@0: print()
michael@0: print_fill("""\
michael@0: Please open Makefile.in and move the filenames at the bottom of the
michael@0: file to their correct places.""")
michael@0:
michael@0: def main(test_name, bug_number):
michael@0: global html_template, js_template
michael@0:
michael@0: def format(str):
michael@0: return str.format(bug=bug_number, test=test_name)
michael@0:
michael@0: def create_file(filename, template):
michael@0: path = os.path.join(os.path.dirname(sys.argv[0]), format(filename))
michael@0: # Create a new file, bailing with an error if the file exists.
michael@0: fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL)
michael@0:
michael@0: try:
michael@0: # This file has 777 permission when created, for whatever reason. Make it rw-rw-r---.
michael@0: os.fchmod(fd, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH)
michael@0: except:
michael@0: # fchmod doesn't work on Windows.
michael@0: pass
michael@0:
michael@0: with os.fdopen(fd, 'w') as file:
michael@0: file.write(format(template))
michael@0:
michael@0: create_file('browserElement_{test}.js', js_template)
michael@0: create_file('test_browserElement_inproc_{test}.html', html_template)
michael@0: create_file('test_browserElement_oop_{test}.html', html_template)
michael@0:
michael@0: add_to_makefile([format(x) for x in ['browserElement_{test}.js',
michael@0: 'test_browserElement_inproc_{test}.html',
michael@0: 'test_browserElement_oop_{test}.html']])
michael@0:
michael@0: if __name__ == '__main__':
michael@0: parser = argparse.ArgumentParser(description="Create a new browser-element testcase.")
michael@0: parser.add_argument('test_name')
michael@0: parser.add_argument('bug_number', type=int)
michael@0: args = parser.parse_args()
michael@0: main(args.test_name, args.bug_number)