#!/usr/bin/env python

"""Run autopep8 against test file and check against expected output."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import os
import sys

import autopep8


def check(expected_filename, input_filename):
    """Test and compare output.

    Return True on success.

    """
    got = autopep8.fix_file(
        input_filename,
        options=autopep8.parse_args(['', '--aggressive'])[0])

    try:
        with autopep8.open_with_encoding(expected_filename) as expected_file:
            expected = expected_file.read()
    except IOError:
        expected = None

    if expected == got:
        return True
    else:
        got_filename = expected_filename + '.err'
        encoding = autopep8.detect_encoding(input_filename)

        with autopep8.open_with_encoding(got_filename,
                                         encoding=encoding,
                                         mode='w') as got_file:
            got_file.write(got)

        print('{} does not match expected {}'.format(got_filename,
                                                     expected_filename),
              file=sys.stderr)
        return False


def run(filename):
    """Test against a specific file.

    Return True on success.
    
    Expected output should have the same base filename, but live in an "out"
    directory:

        foo/bar.py
        foo/out/bar.py

    Failed output will go to:

        foo/out/bar.py.err

    """
    return check(
        expected_filename=os.path.join(
            os.path.dirname(filename),
            'out',
            os.path.basename(filename)
        ),
        input_filename=filename
    )


def suite():
    result = True
    path = os.path.join(os.path.dirname(__file__), 'suite')
    for filename in os.listdir(path):
        filename = os.path.join(path, filename)

        print(filename, file=sys.stderr)

        if filename.endswith('.py'):
            result = run(filename) and result

    return result


def main():
    return int(not suite())


if __name__ == '__main__':
    sys.exit(main())

@@ -279,16 +279,153 @@
 opep8%0A%0A%0A
+if sys.stdout.isatty():%0A    GREEN = '%5Cx1b%5B32m'%0A    RED = '%5Cx1b%5B31m'%0A    END = '%5Cx1b%5B0m'%0Aelse:%0A    GREEN = ''%0A    RED = ''%0A    END = ''%0A%0A%0A
 def chec
@@ -1219,10 +1219,33 @@
 int(
-'%7B
+%0A            '%7Bbegin%7D%7Bgot
 %7D do
@@ -1271,31 +1271,31 @@
 ed %7B
-%7D'.format(got_filename,
+expected%7D%7Bend%7D'.format(
 %0A   
@@ -1299,37 +1299,43 @@
                 
-     
+begin=RED,%0A
                 
@@ -1326,32 +1326,50 @@
                 
+got=got_filename,%0A
                 
@@ -1360,32 +1360,41 @@
                 
+expected=
 expected_filenam
@@ -1386,37 +1386,60 @@
 xpected_filename
-)
 ,%0A  
+              end=END),%0A
             file
@@ -1446,20 +1446,21 @@
 =sys.std
-err)
+out)%0A
 %0A       
@@ -2164,22 +2164,19 @@
     
-print(
+if 
 filename
 , fi
@@ -2175,110 +2175,168 @@
 name
-, file=sys.stderr)%0A%0A        if filename.endswith('.py'):%0A            result = run(filename) and result
+.endswith('.py'):%0A            print(filename, file=sys.stderr)%0A            result = run(filename) and result%0A%0A    if result:%0A        print(GREEN + 'Okay' + END)
 %0A%0A  

Add color