def pretty_compare(config, op, left, right): very_verbose = config.option.verbose >= 2 if not very_verbose: return None if op != "==": return None try: if abs(left + right) < 100: return None except TypeError: pass try: pretty_left = pformat(left, indent=4, width=80, sort_dict_keys=True).splitlines() pretty_right = pformat(right, indent=4, width=80, sort_dict_keys=True).splitlines() differ = icdiff.ConsoleDiff(cols=160, tabsize=4) icdiff_lines = list( differ.make_table(pretty_left, pretty_right, context=False)) return (["equals failed"] + ["<left>".center(79) + "|" + "<right>".center(80)] + ["-" * 160] + [icdiff.color_codes["none"] + l for l in icdiff_lines]) except Exception: return None
def diff_dicts(left, right, use_markup) -> Optional[List[str]]: half_cols = MAX_COLS / 2 - MARGINS pretty_left = pformat(left, indent=1, width=half_cols).splitlines() pretty_right = pformat(right, indent=1, width=half_cols).splitlines() diff_cols = MAX_COLS - MARGINS if len(pretty_left) < 3 or len(pretty_right) < 3: # avoid small diffs far apart by smooshing them up to the left smallest_left = pformat(left, indent=2, width=1).splitlines() smallest_right = pformat(right, indent=2, width=1).splitlines() max_side = max(len(line) + 1 for line in smallest_left + smallest_right) if (max_side * 2 + MARGIN_LEFT) < MAX_COLS: diff_cols = max_side * 2 + GUTTER pretty_left = pformat(left, indent=2, width=max_side).splitlines() pretty_right = pformat(right, indent=2, width=max_side).splitlines() differ = icdiff.ConsoleDiff(cols=diff_cols, tabsize=2) if not use_markup: # colorization is disabled in Pytest - either due to the terminal not # supporting it or the user disabling it. We should obey, but there is # no option in icdiff to disable it, so we replace its colorization # function with a no-op differ.colorize = lambda string: string color_off = "" else: color_off = icdiff.color_codes["none"] icdiff_lines = list(differ.make_table(pretty_left, pretty_right, context=True)) return ["equals failed"] + [color_off + line for line in icdiff_lines]
def create_icdiff(first, second, fromfile="first", tofile="second", indent=4, width=100): """ Based on: https://github.com/hjwp/pytest-icdiff/blob/master/pytest_icdiff.py """ if isinstance(first, str): first = first.splitlines() else: first = pformat(first, indent=indent, width=width) if isinstance(second, str): second = second.splitlines() else: second = pformat(second, indent=indent, width=width) icdiff_lines = list( icdiff.ConsoleDiff(tabsize=2, cols=width, highlight=True).make_table(fromlines=first, tolines=second, fromdesc=fromfile, todesc=tofile)) if len(icdiff_lines) == 1: # hacky whitespace reduce: icdiff_lines[0] = icdiff_lines[0].replace(" ", " ") # icdiff_lines = [f"{COLOR_OFF}{l}" for l in icdiff_lines] return "\n".join(icdiff_lines)
def pytest_assertrepr_compare(config, op, left, right): if op != '==': return return ['equals failed'] + list(icdiff.ConsoleDiff(tabsize=2).make_table( pformat(left, width=40).splitlines(), pformat(right, width=40).splitlines(), ))
def split_diff(old, new): """ Returns a generator yielding the side-by-side diff of `old` and `new`). """ return map( lambda l: l.rstrip(), icdiff.ConsoleDiff(cols=COLUMNS).make_table( old.splitlines(), new.splitlines()))
def test_long_dict(testdir): one = { 'currency': 'USD', 'default_UK_warehouse': 'xforce', 'default_incoterm': 'EXW', 'name': 'John Doe', 'payment_term': '30% deposit, 70% balance', 'reference': '42551456-a1b3-49bd-beed-b168d9a5ac83', 'website': 'http://megasofas.example.com', 'main_contact': { 'city': 'Madeira', 'country': 'PT', 'email': '*****@*****.**', 'fax': '012356 789039', 'mobile': '012356 789039', 'name': 'Almeida & Filhos - Example, S.A.', 'phone': '253444802010', 'postcode': '4815-123', 'street': "Senhora Test D'Ajuda, 432", 'street2': 'Moreira de Conegos' }, } two = { 'currency': 'USD', 'default_UK_warehouse': 'iforce', 'default_incoterm': 'EXW', 'freight_forwarder': 'flexport', 'name': 'John Doe', 'payment_term': '30% deposit, 70% balance', 'reference': '42551456-a1b3-49bd-beed-b168d9a5ac83', 'website': 'http://megasofas.example.com', 'main_contact': { 'name': 'Almeida & Filhos - Example, S.A.', 'email': '*****@*****.**', 'street': "Senhora Test D'Ajuda, 432", 'street2': 'Moreira de Conegos', 'postcode': '4815-123', 'city': 'Madeira', 'country': 'PT', 'phone': '253444802010', 'fax': '012356 789039', 'mobile': '012356 789039' } } testdir.makepyfile(f""" def test_two(): assert {one!r} == {two!r} """) output = testdir.runpytest('-vv', '--color=yes').stdout.str() expected_lines = icdiff.ConsoleDiff().make_table( pformat(one).splitlines(), pformat(two).splitlines(), ) for l in expected_lines: assert l.strip() in output
def pytest_assertrepr_compare(config, op, left, right): if op != '==': return icdiff_lines = list(icdiff.ConsoleDiff(tabsize=2).make_table( pformat(left, width=40).splitlines(), pformat(right, width=40).splitlines(), )) if len(icdiff_lines) == 1: icdiff_lines[0] = hacky_whitespace_reduce(icdiff_lines[0]) return ['equals failed'] + [f'{COLOR_OFF}{l}' for l in icdiff_lines]
def _overwrite_helper_show_diff(lines, new_lines, out_file, out_path, out_path_obj, show_diff_side_by_side): if out_path != "-" and out_path_obj.exists(): with out_path_obj.open("rt") as inputf: old_lines = inputf.read().splitlines(keepends=False) else: old_lines = [] if not show_diff_side_by_side: lines = list( difflib.unified_diff(old_lines, new_lines, fromfile=str(out_path), tofile=str(out_path))) for line in lines: if line.startswith(("+++", "---")): print(colored(line, color="white", attrs=("bold", )), end="", file=out_file) elif line.startswith("@@"): print(colored(line, color="cyan", attrs=("bold", )), end="", file=out_file) elif line.startswith("+"): print(colored(line, color="green", attrs=("bold", )), file=out_file) elif line.startswith("-"): print(colored(line, color="red", attrs=("bold", )), file=out_file) else: print(line, file=out_file) else: cd = icdiff.ConsoleDiff(cols=get_terminal_columns(), line_numbers=True) lines = list( cd.make_table( old_lines, new_lines, fromdesc=str(out_path), todesc=str(out_path), context=True, numlines=3, )) for line in lines: line = "%s\n" % line if hasattr(out_file, "buffer"): out_file.buffer.write(line.encode("utf-8")) # type: ignore else: out_file.write(line) out_file.flush() if not lines: logger.info("File %s not changed, no diff...", out_path) return lines
def test_prepends_icdiff_output_lines_with_color_off(testdir): one = ['hello', 'hello'] two = ['bello', 'hella'] testdir.makepyfile(f""" def test_thing(): assert {one!r} == {two!r} """) output = testdir.runpytest('--color=yes').stdout.str() expected = list(icdiff.ConsoleDiff().make_table( pformat(one, width=1).splitlines(), pformat(two, width=1).splitlines(), )) print('\n'.join(repr(l) for l in output.splitlines())) _assert_line_in_ignoring_whitespace(expected[0], output)
def pytest_assertrepr_compare(config, op, left, right): if op != '==': return try: if abs(left + right) < 100: return except TypeError: pass if isinstance(left, OrderedDict) and type(right) is dict: left = _convert_to_dict(left) if isinstance(right, OrderedDict) and type(left) is dict: right = _convert_to_dict(right) terminal_writer = config.get_terminal_writer() cols = terminal_writer.fullwidth - 12 wide_left = pformat(left, indent=2, width=cols / 2).splitlines() wide_right = pformat(right, indent=2, width=cols / 2).splitlines() if len(wide_left) < 3 or len(wide_right) < 3: shortest_left = pformat(left, indent=2, width=1).splitlines() shortest_right = pformat(right, indent=2, width=1).splitlines() cols = max(len(l) for l in shortest_left + shortest_right) * 2 else: cols = max(len(l) for l in wide_left + wide_right) * 2 pretty_left = pformat(left, indent=2, width=cols / 2).splitlines() pretty_right = pformat(right, indent=2, width=cols / 2).splitlines() differ = icdiff.ConsoleDiff(cols=cols + 12, tabsize=2) if not terminal_writer.hasmarkup: # colorization is disabled in Pytest - either due to the terminal not # supporting it or the user disabling it. We should obey, but there is # no option in icdiff to disable it, so we replace its colorization # function with a no-op differ.colorize = lambda string: string color_off = '' else: color_off = icdiff.color_codes['none'] icdiff_lines = list(differ.make_table(pretty_left, pretty_right)) return ['equals failed'] + [color_off + l for l in icdiff_lines]
def pytest_assertrepr_compare(config, op, left, right): if op != '==': return try: if abs(left + right) < 19999: return except TypeError: pass half_cols = COLS / 2 - MARGINS pretty_left = pformat(left, indent=2, width=half_cols).splitlines() pretty_right = pformat(right, indent=2, width=half_cols).splitlines() diff_cols = COLS - MARGINS if len(pretty_left) < 3 or len(pretty_right) < 3: # avoid small diffs far apart by smooshing them up to the left smallest_left = pformat(left, indent=2, width=1).splitlines() smallest_right = pformat(right, indent=2, width=1).splitlines() max_side = max(len(l) + 1 for l in smallest_left + smallest_right) if (max_side * 2 + MARGINS) < COLS: diff_cols = max_side * 2 + GUTTER pretty_left = pformat(left, indent=2, width=max_side).splitlines() pretty_right = pformat(right, indent=2, width=max_side).splitlines() differ = icdiff.ConsoleDiff(cols=diff_cols, tabsize=2) if not config.get_terminal_writer().hasmarkup: # colorization is disabled in Pytest - either due to the terminal not # supporting it or the user disabling it. We should obey, but there is # no option in icdiff to disable it, so we replace its colorization # function with a no-op differ.colorize = lambda string: string color_off = '' else: color_off = icdiff.color_codes['none'] icdiff_lines = list( differ.make_table(pretty_left, pretty_right, context=True)) return ['equals failed'] + [color_off + l for l in icdiff_lines]
def run(args, _parser: argparse.ArgumentParser, _subparser: argparse.ArgumentParser) -> typing.Optional[int]: """Run ``cubi-tk sea-snap write-sample-info``.""" res: typing.Optional[int] = check_args(args) if res: # pragma: nocover return res if args.project_uuid: logger.info("Starting to pull ISA files...") logger.info(" Args: %s", args) pull_isa(args) logger.info("Starting to write sample info...") logger.info(" Args: %s", args) with tempfile.NamedTemporaryFile(mode="w+t") as sample_info_file: # Write sample info to temporary file. res = write_sample_info(args, sample_info_file) if res: # pragma: nocover return res # Compare sample info with output if exists and --show-diff given. if args.show_diff: if os.path.exists(args.output_file.name): with open(args.output_file.name, "rt") as inputf: old_lines = inputf.read().splitlines(keepends=False) else: old_lines = [] sample_info_file.seek(0) new_lines = sample_info_file.read().splitlines(keepends=False) is_diff = False if not args.show_diff_side_by_side: lines = difflib.unified_diff( old_lines, new_lines, fromfile=args.output_file.name, tofile=args.output_file.name, ) for line in lines: is_diff = True line = line[:-1] if line.startswith(("+++", "---")): print(colored(line, color="white", attrs=("bold", )), file=sys.stdout) elif line.startswith("@@"): print(colored(line, color="cyan", attrs=("bold", )), file=sys.stdout) elif line.startswith("+"): print(colored(line, color="green", attrs=("bold", )), file=sys.stdout) elif line.startswith("-"): print(colored(line, color="red", attrs=("bold", )), file=sys.stdout) else: print(line, file=sys.stdout) else: cd = icdiff.ConsoleDiff(cols=get_terminal_columns(), line_numbers=True) lines = cd.make_table( old_lines, new_lines, fromdesc=args.output_file.name, todesc=args.output_file.name, context=True, numlines=3, ) heading = next(lines) def show_line(line): if hasattr(sys.stdout, "buffer"): sys.stdout.buffer.write(line.encode("utf-8")) else: sys.stdout.write(line) for line in lines: if not is_diff: show_line("%s\n" % heading) is_diff = True show_line("%s\n" % line) sys.stdout.flush() if not is_diff: logger.info("File %s not changed, no diff...", args.output_file.name) # Write to output file if not --dry-run is given if hasattr(args.output_file, "name") and args.dry_run: logger.warning("Not changing %s as we are in --dry-run mode", args.output_file.name) else: if hasattr(args.output_file, "name") and args.output_file.name != "<stdout>": action = ("Overwriting" if Path(args.output_file.name).stat().st_size != 0 else "Creating") logger.info("%s %s", action, args.output_file.name) if args.output_file != "<stdout>": sample_info_file.seek(0) if hasattr(args.output_file, "name") and args.output_file.name != "<stdout>": args.output_file.seek(0) args.output_file.truncate() shutil.copyfileobj(sample_info_file, args.output_file) logger.warning( "in_path_pattern: %s --> Use the same in your mapping_config.yaml!", args.in_path_pattern, ) return None
def overwrite_helper( out_path: typing.Union[str, pathlib.Path], contents: str, *, do_write: bool, show_diff: bool, show_diff_side_by_side: bool = False, answer_yes: bool = False, out_file: typing.IO = sys.stderr, ) -> None: out_path_obj = pathlib.Path(out_path) with tempfile.NamedTemporaryFile(mode="w+t") as sheet_file: lines = [] # Write sheet to temporary file. sheet_file.write(contents) sheet_file.flush() sheet_file.seek(0) new_lines = sheet_file.read().splitlines(keepends=False) # Compare sheet with output if exists and --show-diff given. if show_diff: if out_path != "-" and out_path_obj.exists(): with out_path_obj.open("rt") as inputf: old_lines = inputf.read().splitlines(keepends=False) else: old_lines = [] if not show_diff_side_by_side: lines = list( difflib.unified_diff(old_lines, new_lines, fromfile=str(out_path), tofile=str(out_path))) for line in lines: if line.startswith(("+++", "---")): print(colored(line, color="white", attrs=("bold", )), end="", file=out_file) elif line.startswith("@@"): print(colored(line, color="cyan", attrs=("bold", )), end="", file=out_file) elif line.startswith("+"): print(colored(line, color="green", attrs=("bold", )), file=out_file) elif line.startswith("-"): print(colored(line, color="red", attrs=("bold", )), file=out_file) else: print(line, file=out_file) else: cd = icdiff.ConsoleDiff(cols=get_terminal_columns(), line_numbers=True) lines = list( cd.make_table( old_lines, new_lines, fromdesc=str(out_path), todesc=str(out_path), context=True, numlines=3, )) for line in lines: line = "%s\n" % line if hasattr(out_file, "buffer"): out_file.buffer.write( line.encode("utf-8")) # type: ignore else: out_file.write(line) out_file.flush() if not lines: logger.info("File %s not changed, no diff...", out_path) # Actually copy the file contents. if (not show_diff or lines) and do_write: logger.info("About to write file contents to %s", out_path) sheet_file.seek(0) if out_path == "-": shutil.copyfileobj(sheet_file, sys.stdout) else: if show_diff: logger.info("See above for the diff that will be applied.") if answer_yes or input("Is this OK? [yN] ").lower().startswith( "y"): with out_path_obj.open("wt") as output_file: shutil.copyfileobj(sheet_file, output_file)