#!/usr/bin/python

"""cpplint_wrapper_test.py -- Unit test for cpplint_wrapper.py.
"""

import os
import subprocess
import sys
import unittest


class TestStringMethods(unittest.TestCase):

    def get_absolute_filename(self, relative_filename):
        """Return an absolute filename, relative to this test program."""
        mydir = os.path.dirname(os.path.abspath(__file__))
        return os.path.join(mydir, relative_filename)

    def get_valid_header_filename(self):
        """This header should always exist, and pass cpplint."""
        return self.get_absolute_filename("../eigen_types.h")

    def run_and_expect(self, args, expected_exitcode, expected_regexps=None):
        try:
            cpplint_wrapper = self.get_absolute_filename("cpplint_wrapper.py")
            output = subprocess.check_output(
                [sys.executable, cpplint_wrapper] + args,
                stderr=subprocess.STDOUT)
            returncode = 0
        except subprocess.CalledProcessError as e:
            output = e.output
            returncode = e.returncode
        self.assertEqual(returncode, expected_exitcode,
                         "output was:\n" + output)
        if expected_regexps is not None:
            for item in expected_regexps:
                self.assertRegexpMatches(output, item)

    def test_help(self):
        self.run_and_expect(
            ['--help'],
            0,
            ["usage: cpplint_wrapper"])

    def test_summarize(self):
        filename = self.get_valid_header_filename()
        self.run_and_expect(
            [filename],
            0,
            [r"Checking \.+\n",
             r"TOTAL 1 files passed"])
        self.run_and_expect(
            ["--no-summarize", filename],
            0,
            [r"Done processing .*/eigen_types.h",
             r"TOTAL 1 files passed"])

    def test_true_positive(self):
        filename = self.get_absolute_filename(
            "../../../"
            "externals/google_styleguide/cpplint/cpplint_test_header.h")
        self.run_and_expect(
            [filename],
            1,
            [r"Checking \.+\n",
             r"TOTAL 1 files checked",
             r"found 2 warnings"])
        # Test that pass-through arguments works, along with warning filtering.
        self.run_and_expect(
            [filename, "--", "--filter=-build/header_guard"],
            1,
            [r"Checking \.+\n",
             r"TOTAL 1 files checked",
             r"found 1 warnings"])
        self.run_and_expect(
            [filename, "--", "--filter=-build/header_guard,-legal/copyright"],
            0,
            [r"Checking \.+\n",
             r"TOTAL 1 files passed"])

    def test_ignored_extension(self):
        self.run_and_expect(
            [self.get_absolute_filename('cpplint_wrapper_test.py')],
            0,
            [r"Ignoring .*/cpplint_wrapper_test.py",
             r"TOTAL 0 files"])

    def test_num_processes(self):
        filename = self.get_valid_header_filename()
        self.run_and_expect(
            [filename] * 2,
            0,
            [r"TOTAL 2 files passed"])
        self.run_and_expect(
            ["--num-processes=1"] + [filename] * 2,
            0,
            [r"TOTAL 2 files passed"])
        self.run_and_expect(
            ["--num-processes=0"] + [filename] * 2,
            1)

    def test_huge(self):
        filename = self.get_valid_header_filename()
        self.run_and_expect(
            ["--num-processes=2"] + [filename] * 25,
            0,
            [r"Checking \.+\n",
             r"TOTAL 25 files passed"])


if __name__ == '__main__':
    unittest.main()

@@ -1861,32 +1861,84 @@
 positive(self):%0A
+        # Test that the header is clean by default.%0A
         filename
@@ -2122,33 +2122,33 @@
 e%5D,%0A            
-1
+0
 ,%0A            %5Br
@@ -2198,49 +2198,14 @@
 les 
-checked%22,%0A             r%22found 2 warnings
+passed
 %22%5D)%0A
@@ -2256,38 +2256,8 @@
 orks
-, along with warning filtering
 .%0A  
@@ -2319,34 +2319,21 @@
  %22--
-filter=-build/header_guard
+linelength=40
 %22%5D,%0A
@@ -2439,17 +2439,21 @@
 r%22found 
-1
+%5B123%5D
  warning
@@ -2454,18 +2454,17 @@
 arnings%22
-%5D)
+,
 %0A       
@@ -2468,190 +2468,37 @@
     
-self.run_and_expect(%0A            %5Bfilename, %22--%22, %22--filter=-build/header_guard,-legal/copyright%22%5D,%0A            0,%0A            %5Br%22Checking %5C.+%5Cn%22,%0A             r%22TOTAL 1 files passed
+     r%22whitespace/line_length
 %22%5D)%0A

Rework true-positive test to account for our suppressions.