Repository: sharkdp/bat
File: tests/syntax-tests/create_highlighted_versions.py (Python)

Code before:
                ["bat"] + get_options(source) + [source],
                stderr=subprocess.PIPE,
                env=env,
            )

            output_dir = path.join(output_basepath, source_dirname)
            output_path = path.join(output_dir, source_filename)

            os.makedirs(output_dir, exist_ok=True)

            with open(output_path, "wb") as output_file:
                output_file.write(bat_output)

            print("Created '{}'".format(output_path))
        except subprocess.CalledProcessError as err:
            print(
                "=== Error: Could not highlight source file '{}".format(source),
                file=sys.stderr,
            )
            print(
                "=== bat stdout:\n{}".format(err.stdout.decode("utf-8")),
                file=sys.stderr,
            )
            print(
                "=== bat stderr:\n{}".format(err.stderr.decode("utf-8")),
                file=sys.stderr,
            )
            sys.exit(1)
        except FileNotFoundError:
            print(
                "Error: Could not execute 'bat'. Please make sure that the executable "
                "is available on the PATH."
            )
            sys.exit(1)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="This script creates syntax-highlighted versions of all "
        "files in the 'source' directory."
    )
    parser.add_argument(
        "--output",
        "-O",
        metavar="PATH",
        help="Output directory",
        required=True,
        type=str,
    )

    args = parser.parse_args()

Review comment (bug):
@sharkdp `source` has the wrong value here, and gives misleading error messages :grimacing: 

Step-by-step:
1. `git fetch && git checkout create_highlighted_versions-wrong-path-repro`
2. `tests/syntax-tests/update.sh`

Expected:
`=== Error: Could not highlight source file '/home/martin/src/bat/tests/syntax-tests/source/Svelte/App.svelte`

Actual:
`=== Error: Could not highlight source file '/home/martin/src/bat/tests/syntax-tests/source/DotENV/.env`

You get the expected error message if you do `git revert` 44a332c1c4e904eada9fc53f2e6dfede43c83d7f first.

(I'm working on adding a LiveScript syntax to fix bat crashing when encountering this in .svelte files: https://github.com/sharkdp/bat/commit/271842d87c816cb80dc0a1c48eefee99c9439db6. It is this commit that is present in `create_highlighted_versions-wrong-path-repro`)

Code after:
    output_path = path.join(output_dir, source_filename)

    os.makedirs(output_dir, exist_ok=True)

    with open(output_path, "wb") as output_file:
        output_file.write(bat_output)

    print("Created '{}'".format(output_path))


def create_highlighted_versions(output_basepath):
    root = os.path.dirname(os.path.abspath(__file__))
    source_paths = path.join(root, "source", "*")

    sources = []
    for source in glob.glob(path.join(source_paths, "*")) + glob.glob(
        path.join(source_paths, ".*")
    ):
        sources.append((output_basepath, source))

    try:
        with Pool() as p:
            p.map(create_highlighted_version, sources)
    except subprocess.CalledProcessError as err:
        print(
            "=== Error: Could not highlight source file '{}".format(source),
            file=sys.stderr,
        )
        print(
            "=== bat stdout:\n{}".format(err.stdout.decode("utf-8")),
            file=sys.stderr,
        )
        print(
            "=== bat stderr:\n{}".format(err.stderr.decode("utf-8")),
            file=sys.stderr,
        )
        return False
    except FileNotFoundError:
        print(
            "Error: Could not execute 'bat'. Please make sure that the executable "
            "is available on the PATH."
        )
        return False

    return True


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="This script creates syntax-highlighted versions of all "
        "files in the 'source' directory."