Beispiel #1
0
class FixJsStyleTest(googletest.TestCase):
    """Test case to for gjslint auto-fixing."""
    def testFixJsStyle(self):
        input_filename = None
        try:
            input_filename = '%s/fixjsstyle.in.js' % (_RESOURCE_PREFIX)

            golden_filename = '%s/fixjsstyle.out.js' % (_RESOURCE_PREFIX)
        except IOError, ex:
            raise IOError('Could not find testdata resource for %s: %s' %
                          (self._filename, ex))

        with open(input_filename) as f:
            for line in f:
                # Go to last line.
                pass
            self.assertTrue(line == line.rstrip(),
                            'fixjsstyle.js should not end '
                            'with a new line.')

        # Autofix the file, sending output to a fake file.
        actual = StringIO.StringIO()
        style_checker = checker.JavaScriptStyleChecker(
            error_fixer.ErrorFixer(actual))
        style_checker.Check(input_filename)

        # Now compare the files.
        actual.seek(0)
        expected = open(golden_filename, 'r')

        self.assertEqual(actual.readlines(), expected.readlines())
Beispiel #2
0
def main(argv=None):
    """Main function.

  Args:
    argv: Sequence of command line arguments.
  """
    if argv is None:
        argv = flags.FLAGS(sys.argv)

    suffixes = ['.js']
    if FLAGS.additional_extensions:
        suffixes += ['.%s' % ext for ext in FLAGS.additional_extensions]

    files = fileflags.GetFileList(argv, 'JavaScript', suffixes)

    output_buffer = None
    if FLAGS.dry_run:
        output_buffer = StringIO.StringIO()

    fixer = error_fixer.ErrorFixer(output_buffer)

    # Check the list of files.
    for filename in files:
        runner.Run(filename, fixer)
        if FLAGS.dry_run:
            print output_buffer.getvalue()
Beispiel #3
0
def main(argv=None):
    """Main function.

  Args:
    argv: Sequence of command line arguments.
  """
    if argv is None:
        argv = flags.FLAGS(sys.argv)

    suffixes = ['.js', '.k', '.kl', '.htm', '.html']
    if FLAGS.additional_extensions:
        suffixes += ['.%s' % ext for ext in FLAGS.additional_extensions]

    files = fileflags.GetFileList(argv, 'JavaScript', suffixes)

    style_checker = checker.JavaScriptStyleChecker(error_fixer.ErrorFixer())

    # Check the list of files.
    for filename in files:
        # if we have a js file
        if (filename.endswith('.js') or filename.endswith('.k')
                or filename.endswith('.kl')):
            style_checker.Check(filename)
        else:

            # HM: extract the js code from the html page,
            # run it through the checker, and composite the page again
            lines = open(filename).read().split('\n')
            insidejscode = 0
            htmlprev = []
            htmlpost = []
            jscode = []
            for line in lines:
                stripline = line.strip().lower()
                if (stripline.startswith('<')):
                    stripline = stripline[1:10000].strip()
                if (insidejscode == 0):
                    htmlprev.append(line)
                    if (stripline.startswith('script')
                            and stripline.find('src') == -1):
                        insidejscode = 1
                elif (insidejscode == 1):
                    if (stripline.startswith('/') and
                            stripline[1:10000].strip().startswith('script')):
                        insidejscode = 2
                        htmlpost.append(line)
                    else:
                        jscode.append(line)
                else:
                    htmlpost.append(line)

            jscode = '\n'.join(jscode)
            open(filename + '.tmp.js', 'w').write(jscode)
            style_checker.Check(filename + '.tmp.js')
            jscode = open(filename + '.tmp.js').read()
            htmlcode = '\n'.join(htmlprev) + '\n' + jscode + '\n' + '\n'.join(
                htmlpost)
            open(filename, 'w').write(htmlcode)
            os.remove(filename + '.tmp.js')
Beispiel #4
0
    def _AssertFixes(self, original, expected, include_header=True):
        """Asserts that the error fixer corrects original to expected."""
        if include_header:
            original = self._GetHeader() + original
            expected = self._GetHeader() + expected

        actual = StringIO.StringIO()
        runner.Run('testing.js', error_fixer.ErrorFixer(actual), original)
        actual.seek(0)

        expected = [x + '\n' for x in expected]

        self.assertListEqual(actual.readlines(), expected)
Beispiel #5
0
    def _AssertFixes(self, original, expected):
        """Asserts that the error fixer corrects original to expected."""
        original = self._GetHeader() + original
        expected = self._GetHeader() + expected

        actual = StringIO.StringIO()
        style_checker = checker.JavaScriptStyleChecker(
            error_fixer.ErrorFixer(actual))
        style_checker.CheckLines('testing.js', original, False)
        actual.seek(0)

        expected = [x + '\n' for x in expected]

        self.assertListEqual(actual.readlines(), expected)
Beispiel #6
0
    def testFixJsStyle(self):
        test_cases = [['fixjsstyle.in.js', 'fixjsstyle.out.js'],
                      ['indentation.js', 'fixjsstyle.indentation.out.js'],
                      ['fixjsstyle.html.in.html', 'fixjsstyle.html.out.html'],
                      [
                          'fixjsstyle.oplineend.in.js',
                          'fixjsstyle.oplineend.out.js'
                      ]]
        for [running_input_file, running_output_file] in test_cases:
            print 'Checking %s vs %s' % (running_input_file,
                                         running_output_file)
            input_filename = None
            golden_filename = None
            current_filename = None
            try:
                input_filename = '%s/%s' % (_RESOURCE_PREFIX,
                                            running_input_file)
                current_filename = input_filename

                golden_filename = '%s/%s' % (_RESOURCE_PREFIX,
                                             running_output_file)
                current_filename = golden_filename
            except IOError as ex:
                raise IOError('Could not find testdata resource for %s: %s' %
                              (current_filename, ex))

            if running_input_file == 'fixjsstyle.in.js':
                with open(input_filename) as f:
                    for line in f:
                        # Go to last line.
                        pass
                    self.assertTrue(
                        line == line.rstrip(), '%s file should not end '
                        'with a new line.' % (input_filename))

            # Autofix the file, sending output to a fake file.
            actual = StringIO.StringIO()
            runner.Run(input_filename, error_fixer.ErrorFixer(actual))

            # Now compare the files.
            actual.seek(0)
            expected = open(golden_filename, 'r')

            # Uncomment to generate new golden files and run
            # open('/'.join(golden_filename.split('/')[4:]), 'w').write(actual.read())
            # actual.seek(0)

            self.assertEqual(actual.readlines(), expected.readlines())
def main(argv = None):
  """Main function.

  Args:
    argv: Sequence of command line arguments.
  """
  if argv is None:
    argv = flags.FLAGS(sys.argv)

  files = fileflags.GetFileList(argv, 'JavaScript', ['.js'])

  style_checker = checker.JavaScriptStyleChecker(error_fixer.ErrorFixer())

  # Check the list of files.
  for filename in files:
    style_checker.Check(filename)
Beispiel #8
0
def main(argv=None):
    """Main function.

  Args:
    argv: Sequence of command line arguments.
  """
    if argv is None:
        argv = flags.FLAGS(sys.argv)

    suffixes = ['.js']
    if FLAGS.additional_extensions:
        suffixes += ['.%s' % ext for ext in FLAGS.additional_extensions]

    files = fileflags.GetFileList(argv, 'JavaScript', suffixes)

    style_checker = checker.JavaScriptStyleChecker(error_fixer.ErrorFixer())

    # Check the list of files.
    for filename in files:
        style_checker.Check(filename)
Beispiel #9
0
    def testFixJsStyle(self):
        test_cases = [['fixjsstyle.in.js', 'fixjsstyle.out.js'],
                      ['indentation.js', 'fixjsstyle.indentation.out.js']]
        for [running_input_file, running_output_file] in test_cases:
            input_filename = None
            golden_filename = None
            current_filename = None
            try:
                input_filename = '%s/%s' % (_RESOURCE_PREFIX,
                                            running_input_file)
                current_filename = input_filename

                golden_filename = '%s/%s' % (_RESOURCE_PREFIX,
                                             running_output_file)
                current_filename = golden_filename
            except IOError, ex:
                raise IOError('Could not find testdata resource for %s: %s' %
                              (current_filename, ex))

            if running_input_file == 'fixjsstyle.in.js':
                with open(input_filename) as f:
                    for line in f:
                        # Go to last line.
                        pass
                    self.assertTrue(
                        line == line.rstrip(), '%s file should not end '
                        'with a new line.' % (input_filename))

            # Autofix the file, sending output to a fake file.
            actual = StringIO.StringIO()
            style_checker = checker.JavaScriptStyleChecker(
                error_fixer.ErrorFixer(actual))
            style_checker.Check(input_filename)

            # Now compare the files.
            actual.seek(0)
            expected = open(golden_filename, 'r')

            self.assertEqual(actual.readlines(), expected.readlines())
Beispiel #10
0
 def setUp(self):
   self.error_fixer = error_fixer.ErrorFixer()
Beispiel #11
0
def fix_files(filenames):
    style_checker = checker.JavaScriptStyleChecker(error_fixer.ErrorFixer())

    for filename in filenames:
        style_checker.Check(filename)
    return 0
#!/usr/bin/env python
from settings import *
import os

try:
    from closure_linter import checker
    from closure_linter import error_fixer
    from closure_linter.common import simplefileflags as fileflags
except:
    raise Exception('Please install the closure linter first')

style_checker = checker.JavaScriptStyleChecker(error_fixer.ErrorFixer())
style_checker.Check(os.environ['TM_FILEPATH'])