import io
import itertools
import getopt
+import locale
import os, signal, subprocess, sys
import re
import stat
return path, sorted(child_trees)
def compareTwoFiles(filepaths):
- compare_bytes = False
- encoding = None
filelines = []
for file in filepaths:
- try:
- with open(file, 'r') as f:
- filelines.append(f.readlines())
- except UnicodeDecodeError:
- try:
- with io.open(file, 'r', encoding="utf-8") as f:
- filelines.append(f.readlines())
- encoding = "utf-8"
- except:
- compare_bytes = True
-
- if compare_bytes:
- return compareTwoBinaryFiles(filepaths)
- else:
- return compareTwoTextFiles(filepaths, encoding)
+ with open(file, 'rb') as file_bin:
+ filelines.append(file_bin.readlines())
- def compareTwoBinaryFiles(filepaths):
- filelines = []
- for file in filepaths:
- with open(file, 'rb') as f:
- filelines.append(f.readlines())
+ try:
+ return compareTwoTextFiles(filepaths, filelines,
+ locale.getpreferredencoding(False))
+ except UnicodeDecodeError:
+ try:
+ return compareTwoTextFiles(filepaths, filelines, "utf-8")
+ except:
+ return compareTwoBinaryFiles(filepaths, filelines)
+ def compareTwoBinaryFiles(filepaths, filelines):
exitCode = 0
if hasattr(difflib, 'diff_bytes'):
# python 3.5 or newer
filelines[1], filepaths[0].encode(),
filepaths[1].encode(),
n = num_context_lines)
- diffs = [diff.decode() for diff in diffs]
+ diffs = [diff.decode(errors="backslashreplace") for diff in diffs]
else:
# python 2.7
func = difflib.unified_diff if unified_diff else difflib.context_diff
n = num_context_lines)
for diff in diffs:
- stdout.write(diff)
+ stdout.write(to_string(diff))
exitCode = 1
return exitCode
- def compareTwoTextFiles(filepaths, encoding):
+ def compareTwoTextFiles(filepaths, filelines_bin, encoding):
filelines = []
- for file in filepaths:
- if encoding is None:
- with open(file, 'r') as f:
- filelines.append(f.readlines())
- else:
- with io.open(file, 'r', encoding=encoding) as f:
- filelines.append(f.readlines())
+ for lines_bin in filelines_bin:
+ lines = []
+ for line_bin in lines_bin:
+ line = line_bin.decode(encoding=encoding)
+ lines.append(line)
+ filelines.append(lines)
exitCode = 0
def compose2(f, g):
func = difflib.unified_diff if unified_diff else difflib.context_diff
for diff in func(filelines[0], filelines[1], filepaths[0], filepaths[1],
n = num_context_lines):
- stdout.write(diff)
+ stdout.write(to_string(diff))
exitCode = 1
return exitCode
# CHECK: error: command failed with exit status: 127
# CHECK: ***
+
+# CHECK: FAIL: shtest-shell :: diff-encodings.txt
+# CHECK: *** TEST 'shtest-shell :: diff-encodings.txt' FAILED ***
+
+# CHECK: $ "diff" "-u" "diff-in.bin" "diff-in.bin"
+# CHECK-NOT: error
+
+# CHECK: $ "diff" "-u" "diff-in.utf16" "diff-in.bin"
+# CHECK: # command output:
+# CHECK-NEXT: ---
+# CHECK-NEXT: +++
+# CHECK-NEXT: @@
+# CHECK-NEXT: {{^ .f.o.o.$}}
+# CHECK-NEXT: {{^-.b.a.r.$}}
+# CHECK-NEXT: {{^\+.b.a.r..}}
+# CHECK-NEXT: {{^ .b.a.z.$}}
+# CHECK: error: command failed with exit status: 1
+# CHECK: $ "true"
+
+# CHECK: $ "diff" "-u" "diff-in.utf8" "diff-in.bin"
+# CHECK: # command output:
+# CHECK-NEXT: ---
+# CHECK-NEXT: +++
+# CHECK-NEXT: @@
+# CHECK-NEXT: -foo
+# CHECK-NEXT: -bar
+# CHECK-NEXT: -baz
+# CHECK-NEXT: {{^\+.f.o.o.$}}
+# CHECK-NEXT: {{^\+.b.a.r..}}
+# CHECK-NEXT: {{^\+.b.a.z.$}}
+# CHECK: error: command failed with exit status: 1
+# CHECK: $ "true"
+
+# CHECK: $ "diff" "-u" "diff-in.bin" "diff-in.utf8"
+# CHECK: # command output:
+# CHECK-NEXT: ---
+# CHECK-NEXT: +++
+# CHECK-NEXT: @@
+# CHECK-NEXT: {{^\-.f.o.o.$}}
+# CHECK-NEXT: {{^\-.b.a.r..}}
+# CHECK-NEXT: {{^\-.b.a.z.$}}
+# CHECK-NEXT: +foo
+# CHECK-NEXT: +bar
+# CHECK-NEXT: +baz
+# CHECK: error: command failed with exit status: 1
+# CHECK: $ "true"
+
+# CHECK: $ "false"
+
+# CHECK: ***
+
+
# CHECK: FAIL: shtest-shell :: diff-error-0.txt
# CHECK: *** TEST 'shtest-shell :: diff-error-0.txt' FAILED ***
# CHECK: $ "diff" "diff-error-0.txt" "diff-error-0.txt"
# CHECK: PASS: shtest-shell :: sequencing-0.txt
# CHECK: XFAIL: shtest-shell :: sequencing-1.txt
# CHECK: PASS: shtest-shell :: valid-shell.txt
-# CHECK: Failing Tests (30)
+# CHECK: Failing Tests (31)