import sys
import os
import settings
import filecmp

"""
This script compares the generated CSV coverage files with the ones in the codebase.
"""


def check_file_exists(file):
    if not os.path.exists(file):
        print("Expected file '" + file + "' doesn't exist.", file=sys.stderr)
        sys.exit(1)


def compare_files(file1, file2):
    filecmp.clear_cache()
    if not filecmp.cmp(file1, file2):
        print("Error: The generated files do not match the ones in the codebase. Please check and fix file '" +
              file1 + "'.", file=sys.stderr)
        sys.exit(1)


languages = ['java']

for lang in languages:
    repo_output_rst = settings.repo_output_rst.format(language=lang)
    repo_output_csv = settings.repo_output_csv.format(language=lang)

    generated_output_rst = settings.generated_output_rst.format(language=lang)
    generated_output_csv = settings.generated_output_csv.format(language=lang)

    check_file_exists(repo_output_rst)
    check_file_exists(repo_output_csv)
    check_file_exists(generated_output_rst)
    check_file_exists(generated_output_csv)

    compare_files(repo_output_rst, generated_output_rst)
    compare_files(repo_output_csv, generated_output_csv)

    print("The generated files for '" + lang +
          "' match the ones in the codebase.")

@@ -37,23 +37,23 @@
 %0Aimport 
-filecmp
+difflib
 %0A%0A%22%22%22%0ATh
@@ -311,99 +311,460 @@
 def 
-compare_files(file1, file2):%0A    filecmp.clear_cache
+ignore_line_ending(ch):%0A    return difflib.IS_CHARACTER_JUNK(ch, ws=%22 %5Cr%5Cn%22)%0A%0A%0Adef compare_files(file1, file2):%0A    has_differences = False%0A    diff = difflib.ndiff(open(file1).readlines
 ()
+,
 %0A    
-if not filecmp.cmp(file1, file2)
+                     open(file2).readlines(), None, ignore_line_ending)%0A    for line in diff:%0A        if line.startswith(%22+%22) or line.startswith(%22-%22):%0A            print(line, end=%22%22, file=sys.stderr)%0A            has_differences = True%0A%0A    if has_differences
 :%0A  
@@ -805,15 +805,15 @@
 file
-s
  do
- no
+esn'
 t ma
@@ -815,33 +815,32 @@
 't match the one
-s
  in the codebase

Rework file diff (show line differences)