|
| 1 | +import os, subprocess |
| 2 | +from pathlib import Path |
| 3 | +from shlex import split |
| 4 | + |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +from PIL import Image, ImageChops |
| 8 | + |
| 9 | + |
| 10 | +def compare_images(path_gen, path_ref): |
| 11 | + """Compare a generated image against a reference image. |
| 12 | +
|
| 13 | + This is a simple pixel-by-pixel comparison, with a threshold for |
| 14 | + an allowable difference. |
| 15 | +
|
| 16 | + Parameters: file path to generated and reference image files |
| 17 | + Returns: True/ False |
| 18 | + """ |
| 19 | + if os.name == "nt": |
| 20 | + # Use Windows-specific reference files. |
| 21 | + path_ref = path_ref.with_name(path_ref.stem + "_windows" + path_ref.suffix) |
| 22 | + |
| 23 | + img_gen = Image.open(path_gen) |
| 24 | + img_ref = Image.open(path_ref) |
| 25 | + |
| 26 | + img_diff = ImageChops.difference(img_gen, img_ref) |
| 27 | + |
| 28 | + # We're only concerned with pixels that differ by a total of 20 or more |
| 29 | + # over all RGB values. |
| 30 | + # Convert the image data to a NumPy array for processing. |
| 31 | + data_diff = np.array(img_diff) |
| 32 | + |
| 33 | + # Calculate the sum along the color axis (axis 2) and then check |
| 34 | + # if the sum is greater than or equal to 20. This will return a 2D |
| 35 | + # boolean array where True represents pixels that differ significantly. |
| 36 | + pixels_diff = np.sum(data_diff, axis=2) >= 20 |
| 37 | + |
| 38 | + # Calculate the ratio of pixels that differ significantly. |
| 39 | + ratio_diff = np.mean(pixels_diff) |
| 40 | + |
| 41 | + # Images are similar if only a small % of pixels differ significantly. |
| 42 | + # This value can be increased if tests are failing when they shouldn't. |
| 43 | + # It can be decreased if tests are passing when they shouldn't. |
| 44 | + if ratio_diff < 0.0075: |
| 45 | + return True |
| 46 | + else: |
| 47 | + print("bad pixel ratio:", ratio_diff) |
| 48 | + return False |
| 49 | + |
| 50 | + |
| 51 | +def get_cmd_parts(raw_command): |
| 52 | + """ |
| 53 | + Convert a raw git-sim command to the full version we need to use |
| 54 | + when testing, then split the full command into parts for use in |
| 55 | + subprocess.run(). This allows test functions to explicitly state |
| 56 | + the actual command that users would run. |
| 57 | +
|
| 58 | + For example, the command: |
| 59 | + `git-sim log` |
| 60 | + becomes: |
| 61 | + `</path/to/git-sim> -d --output-only-path --img-format=png log` |
| 62 | +
|
| 63 | + This prevents images from auto-opening, simplifies parsing output to |
| 64 | + identify the images we need to check, and prefers png for test runs. |
| 65 | +
|
| 66 | + Returns: list of command parts, ready to be run with subprocess.run() |
| 67 | + """ |
| 68 | + # Add the global flags needed for testing. |
| 69 | + cmd = raw_command.replace( |
| 70 | + "git-sim", "git-sim -d --output-only-path --img-format=png" |
| 71 | + ) |
| 72 | + |
| 73 | + # Replace `git-sim` with the full path to the binary. |
| 74 | + # as_posix() is needed for Windows compatibility. |
| 75 | + git_sim_path = get_venv_path() / "git-sim" |
| 76 | + cmd = cmd.replace("git-sim", git_sim_path.as_posix()) |
| 77 | + |
| 78 | + return split(cmd) |
| 79 | + |
| 80 | + |
| 81 | +def run_git_reset(tmp_repo): |
| 82 | + """Run `git reset`, in order to induce a failure. |
| 83 | +
|
| 84 | + This is particularly useful when testing the image comparison algorithm. |
| 85 | + - Running `git reset` makes many of the generated images different. |
| 86 | + - For example, `git-sim log` then generates a valid image, but it doesn't |
| 87 | + match the reference image. |
| 88 | +
|
| 89 | + Note: tmp_repo is a required argument, to make sure this command is not |
| 90 | + accidentally called in a different directory. |
| 91 | + """ |
| 92 | + cmd = "git reset --hard 60bce95465a890960adcacdcd7fa726d6fad4cf3" |
| 93 | + cmd_parts = split(cmd) |
| 94 | + |
| 95 | + os.chdir(tmp_repo) |
| 96 | + subprocess.run(cmd_parts) |
| 97 | + |
| 98 | + |
| 99 | +def get_venv_path(): |
| 100 | + """Get the path to the active virtual environment. |
| 101 | +
|
| 102 | + We actually need the bin/ or Scripts/ dir, not just the path to venv/. |
| 103 | + """ |
| 104 | + if os.name == "nt": |
| 105 | + return Path(os.environ.get("VIRTUAL_ENV")) / "Scripts" |
| 106 | + else: |
| 107 | + return Path(os.environ.get("VIRTUAL_ENV")) / "bin" |
0 commit comments