|
| 1 | +from pathlib import Path |
| 2 | +from typing import List, Tuple, Union |
| 3 | + |
| 4 | +import typer |
| 5 | +from tabulate import tabulate |
| 6 | + |
| 7 | +app = typer.Typer() |
| 8 | + |
| 9 | + |
| 10 | +@app.callback(invoke_without_command=True) |
| 11 | +def subsets( |
| 12 | + ctx: typer.Context, |
| 13 | + file_before: Path = typer.Argument(None, help="First subset file to compare"), |
| 14 | + file_after: Path = typer.Argument(None, help="Second subset file to compare") |
| 15 | +): |
| 16 | + """ |
| 17 | + Compare two subset files and display changes in test order positions |
| 18 | + """ |
| 19 | + if ctx.invoked_subcommand is None: |
| 20 | + if file_before is None or file_after is None: |
| 21 | + typer.echo("Error: Both file_before and file_after arguments are required") |
| 22 | + raise typer.Exit(1) |
| 23 | + |
| 24 | + if not file_before.exists(): |
| 25 | + typer.echo(f"Error: File {file_before} does not exist", err=True) |
| 26 | + raise typer.Exit(1) |
| 27 | + |
| 28 | + if not file_after.exists(): |
| 29 | + typer.echo(f"Error: File {file_after} does not exist", err=True) |
| 30 | + raise typer.Exit(1) |
| 31 | + |
| 32 | + # Read files and map test paths to their indices |
| 33 | + with open(file_before, 'r') as f: |
| 34 | + before_tests = f.read().splitlines() |
| 35 | + before_index_map = {test: idx for idx, test in enumerate(before_tests)} |
| 36 | + |
| 37 | + with open(file_after, 'r') as f: |
| 38 | + after_tests = f.read().splitlines() |
| 39 | + after_index_map = {test: idx for idx, test in enumerate(after_tests)} |
| 40 | + |
| 41 | + # List of tuples representing test order changes (before, after, diff, test) |
| 42 | + rows: List[Tuple[Union[int, str], Union[int, str], Union[int, str], str]] = [] |
| 43 | + |
| 44 | + # Calculate order difference and add each test in file_after to changes |
| 45 | + for after_idx, test in enumerate(after_tests): |
| 46 | + if test in before_index_map: |
| 47 | + before_idx = before_index_map[test] |
| 48 | + diff = after_idx - before_idx |
| 49 | + rows.append((before_idx + 1, after_idx + 1, diff, test)) |
| 50 | + else: |
| 51 | + rows.append(('-', after_idx + 1, 'NEW', test)) |
| 52 | + |
| 53 | + # Add all deleted tests to changes |
| 54 | + for before_idx, test in enumerate(before_tests): |
| 55 | + if test not in after_index_map: |
| 56 | + rows.append((before_idx + 1, '-', 'DELETED', test)) |
| 57 | + |
| 58 | + # Sort changes by the order diff |
| 59 | + rows.sort(key=lambda x: (0 if isinstance(x[2], str) else 1, x[2])) |
| 60 | + |
| 61 | + # Display results in a tabular format |
| 62 | + headers = ["Before", "After", "After - Before", "Test"] |
| 63 | + tabular_data = [ |
| 64 | + (before, after, f"{diff:+}" if isinstance(diff, int) else diff, test) |
| 65 | + for before, after, diff, test in rows |
| 66 | + ] |
| 67 | + typer.echo(tabulate(tabular_data, headers=headers, tablefmt="github")) |
0 commit comments