Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax regression tests #1126

Merged
merged 7 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions .github/workflows/CICD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,22 @@ jobs:
args: --locked --path .
- name: Rebuild binary assets (syntaxes and themes)
run: bash assets/create.sh
- name: Run tests with new syntaxes and themes
- name: Build and install bat with updated assets
uses: actions-rs/cargo@v1
with:
command: install
args: --locked --path .
- name: Run unit tests with new syntaxes and themes
uses: actions-rs/cargo@v1
with:
command: test
args: --release
- name: Syntax highlighting regression test
run: tests/syntax-tests/regression_test.sh
- name: List of languages
uses: actions-rs/cargo@v1
with:
command: run
args: --release -- --list-languages
run: bat --list-languages
- name: List of themes
uses: actions-rs/cargo@v1
with:
command: run
args: --release -- --list-themes
run: bat --list-themes

build:
name: Build
Expand Down
1 change: 1 addition & 0 deletions tests/.gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ snapshots/* linguist-vendored
benchmarks/* linguist-vendored
benchmarks/test-src/* linguist-vendored
scripts/* linguist-vendored
syntax-tests/* linguist-vendored
65 changes: 65 additions & 0 deletions tests/syntax-tests/compare_highlighted_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python3

import glob
import sys
import os.path as path
import difflib
import argparse


def compare_highlighted_versions(root_old, root_new):
print("Comparing the following directories:")
print(" -", root_old)
print(" -", root_new)
has_changes = False
for path_old in glob.glob(path.join(root_old, "*", "*")):
filename = path.basename(path_old)
dirname = path.basename(path.dirname(path_old))

path_new = path.join(root_new, dirname, filename)

print("\n========== {}/{}".format(dirname, filename))

with open(path_old) as file_old:
lines_old = file_old.readlines()

with open(path_new) as file_new:
lines_new = file_new.readlines()

diff = difflib.unified_diff(
lines_old, lines_new, fromfile=path_old, tofile=path_new
)

file_has_changes = False
for line in diff:
print(line, end="")
file_has_changes = True

if file_has_changes:
has_changes = True
else:
print("No changes")
print()

return has_changes


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="This script compares two directories that were created "
"by 'create_highlighted_versions.py'."
)
parser.add_argument(
"OLD", help="Path to the old (stored) version of the highlighted output",
)
parser.add_argument(
"NEW", help="Path to the new version of the highlighted output",
)

args = parser.parse_args()

if compare_highlighted_versions(args.OLD, args.NEW):
print("Error: files with changes have been found")
sys.exit(1)
else:
print("Directories are the same")
82 changes: 82 additions & 0 deletions tests/syntax-tests/create_highlighted_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python3

import subprocess
import glob
import sys
import os.path as path
import os
import argparse


BAT_OPTIONS = [
"--no-config",
"--style=plain",
"--color=always",
"--theme='1337'",
"--italic-text=always",
]


def create_highlighted_versions(output_basepath):
root = os.path.dirname(os.path.abspath(__file__))

for source in glob.glob(path.join(root, "source", "*", "*")):
try:
env = os.environ.copy()
env.pop("PAGER", None)
env.pop("BAT_PAGER", None)
env.pop("BAT_CONFIG_PATH", None)
env.pop("BAT_STYLE", None)
env.pop("BAT_THEME", None)
env.pop("BAT_TABS", None)
env["COLORTERM"] = "truecolor" # make sure to output 24bit colors

bat_output = subprocess.check_output(
["bat"] + BAT_OPTIONS + [source], stderr=subprocess.PIPE, env=env,
)

source_dirname = path.basename(path.dirname(source))
source_filename = path.basename(source)

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)


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()

create_highlighted_versions(output_basepath=args.output)
Loading