Ejemplo n.º 1
0
def diffbehavior(args: argparse.Namespace, options: AnalysisOptions,
                 stdout: TextIO, stderr: TextIO) -> int:
    def checked_load(qualname: str) -> Optional[FunctionInfo]:
        try:
            objs = list(load_files_or_qualnames([qualname]))
        except Exception as exc:
            print(f'Unable to load "{qualname}": {exc}', file=stderr)
            return None
        obj = objs[0]
        if not isinstance(obj, FunctionInfo):
            print(f'"{qualname}" does not target a function.', file=stderr)
            return None
        return obj

    (fn_name1, fn_name2) = (args.fn1, args.fn2)
    fn1 = checked_load(fn_name1)
    fn2 = checked_load(fn_name2)
    if fn1 is None or fn2 is None:
        return 2
    options.stats = collections.Counter()
    diffs = diff_behavior(fn1, fn2, options)
    debug("stats", options.stats)
    if isinstance(diffs, str):
        print(diffs, file=stderr)
        return 2
    elif len(diffs) == 0:
        num_paths = options.stats["num_paths"]
        exhausted = options.stats["exhaustion"] > 0
        stdout.write(
            f"No differences found. (attempted {num_paths} iterations)\n")
        if exhausted:
            stdout.write(
                "All paths exhausted, functions are likely the same!\n")
        else:
            stdout.write(
                "Consider trying longer with: --per_condition_timeout=<seconds>\n"
            )
        return 0
    else:
        width = max(len(fn_name1), len(fn_name2)) + 2
        for diff in diffs:
            inputs = ", ".join(f"{k}={v}" for k, v in diff.args.items())
            stdout.write(f"Given: ({inputs}),\n")
            result1, result2 = diff.result1, diff.result2
            differing_args = result1.get_differing_arg_mutations(result2)
            stdout.write(
                f"{fn_name1.rjust(width)} : {result1.describe(differing_args)}\n"
            )
            stdout.write(
                f"{fn_name2.rjust(width)} : {result2.describe(differing_args)}\n"
            )
        return 1
Ejemplo n.º 2
0
def cover(args: argparse.Namespace, options: AnalysisOptions, stdout: TextIO,
          stderr: TextIO) -> int:
    ctxfn = checked_load(args.fn, stderr)
    if ctxfn is None:
        return 2
    options.stats = collections.Counter()
    paths = path_cover(ctxfn, options, args.coverage_type)
    fn, _ = ctxfn.callable()
    example_output_format = args.example_output_format
    if example_output_format == ExampleOutputFormat.ARGUMENT_DICTIONARY:
        return output_argument_dictionary_paths(fn, paths, stdout, stderr)
    elif example_output_format == ExampleOutputFormat.EVAL_EXPRESSION:
        return output_eval_exression_paths(fn, paths, stdout, stderr)
    if example_output_format == ExampleOutputFormat.PYTEST:
        return output_pytest_paths(fn, paths, stdout, stderr)
    assert False
Ejemplo n.º 3
0
def diffbehavior(args: argparse.Namespace, options: AnalysisOptions,
                 stdout: TextIO, stderr: TextIO) -> int:
    (fn_name1, fn_name2) = (args.fn1, args.fn2)
    fn1 = checked_load(fn_name1, stderr)
    fn2 = checked_load(fn_name2, stderr)
    if fn1 is None or fn2 is None:
        return 2
    options.stats = collections.Counter()
    diffs = diff_behavior(fn1, fn2, options)
    debug("stats", options.stats)
    if isinstance(diffs, str):
        print(diffs, file=stderr)
        return 2
    elif len(diffs) == 0:
        num_paths = options.stats["num_paths"]
        exhausted = options.stats["exhaustion"] > 0
        stdout.write(
            f"No differences found. (attempted {num_paths} iterations)\n")
        if exhausted:
            stdout.write(
                "All paths exhausted, functions are likely the same!\n")
        else:
            stdout.write(
                "Consider trying longer with: --per_condition_timeout=<seconds>\n"
            )
        return 0
    else:
        width = max(len(fn_name1), len(fn_name2)) + 2
        for diff in diffs:
            inputs = ", ".join(f"{k}={v}" for k, v in diff.args.items())
            stdout.write(f"Given: ({inputs}),\n")
            result1, result2 = diff.result1, diff.result2
            differing_args = result1.get_differing_arg_mutations(result2)
            stdout.write(
                f"{fn_name1.rjust(width)} : {result1.describe(differing_args)}\n"
            )
            stdout.write(
                f"{fn_name2.rjust(width)} : {result2.describe(differing_args)}\n"
            )
        return 1