on master help sign in
Add comparison script
commit02a89d8ee82865f50015ec19e675ba81b61d0960 authorsharkdp <davidpeter@web.de> dateMon 3 Aug 2020, 13:34 (6 years ago) parenta70efae
4 files changed+82−164
tests/syntax-tests/compare_highlighted_versions.py+65 tests/syntax-tests/create_highlighted_versions.py+2−3 tests/syntax-tests/highlighted/Rust/test.rs−161 tests/syntax-tests/regression_test.sh+15
tests/syntax-tests/compare_highlighted_versions.pynew file+65
@@ -0,0 +1,65 @@
1+#!/usr/bin/env python3
2+
3+import glob
4+import sys
5+import os.path as path
6+import difflib
7+import argparse
8+
9+
10+def compare_highlighted_versions(root_old, root_new):
11+ print("Comparing the following directories:")
12+ print(" -", root_old)
13+ print(" -", root_new)
14+ has_changes = False
15+ for path_old in glob.glob(path.join(root_old, "*", "*")):
16+ filename = path.basename(path_old)
17+ dirname = path.basename(path.dirname(path_old))
18+
19+ path_new = path.join(root_new, dirname, filename)
20+
21+ print("\n========== {}/{}".format(dirname, filename))
22+
23+ with open(path_old) as file_old:
24+ lines_old = file_old.readlines()
25+
26+ with open(path_new) as file_new:
27+ lines_new = file_new.readlines()
28+
29+ diff = difflib.unified_diff(
30+ lines_old, lines_new, fromfile=path_old, tofile=path_new
31+ )
32+
33+ file_has_changes = False
34+ for line in diff:
35+ print(line, end="")
36+ file_has_changes = True
37+
38+ if file_has_changes:
39+ has_changes = True
40+ else:
41+ print("No changes")
42+ print()
43+
44+ return has_changes
45+
46+
47+if __name__ == "__main__":
48+ parser = argparse.ArgumentParser(
49+ description="This script compares two directories that were created "
50+ "by 'create_highlighted_versions.py'."
51+ )
52+ parser.add_argument(
53+ "OLD", help="Path to the old (stored) version of the highlighted output",
54+ )
55+ parser.add_argument(
56+ "NEW", help="Path to the new version of the highlighted output",
57+ )
58+
59+ args = parser.parse_args()
60+
61+ if compare_highlighted_versions(args.OLD, args.NEW):
62+ print("Error: files with changes have been found")
63+ sys.exit(1)
64+ else:
65+ print("Directories are the same")
tests/syntax-tests/create_highlighted_versions.pymode 100644 → 100755+2−3
@@ -1,4 +1,4 @@
1−#!/usr/bin/env python
1+#!/usr/bin/env python3
22
33import subprocess
44import glob
@@ -37,8 +37,7 @@ def create_highlighted_versions(output_basepath):
3737 with open(output_path, "wb") as output_file:
3838 output_file.write(bat_output)
3939
40− relative_path = path.relpath(output_path, root)
41− print("Created '{}'".format(relative_path))
40+ print("Created '{}'".format(output_path))
4241 except subprocess.CalledProcessError as err:
4342 print(
4443 "=== Error: Could not highlight source file '{}".format(source),
tests/syntax-tests/highlighted/Rust/test.rsdeleted−161
@@ -1,161 +0,0 @@
1−use std::io::{self, Write};
2−#[cfg(feature = "paging")]
3−use std::process::Child;
4−
5−use crate::error::*;
6−#[cfg(feature = "paging")]
7−use crate::less::retrieve_less_version;
8−#[cfg(feature = "paging")]
9−use crate::paging::PagingMode;
10−
11−#[derive(Debug)]
12−pub enum OutputType {
13− #[cfg(feature = "paging")]
14− Pager(Child),
15− Stdout(io::Stdout),
16−}
17−
18−impl OutputType {
19− #[cfg(feature = "paging")]
20− pub fn from_mode(mode: PagingMode, pager: Option<&str>) -> Result<Self> {
21− use self::PagingMode::*;
22− Ok(match mode {
23− Always => OutputType::try_pager(false, pager)?,
24− QuitIfOneScreen => OutputType::try_pager(true, pager)?,
25− _ => OutputType::stdout(),
26− })
27− }
28−
29− /// Try to launch the pager. Fall back to stdout in case of errors.
30− #[cfg(feature = "paging")]
31− fn try_pager(quit_if_one_screen: bool, pager_from_config: Option<&str>) -> Result<Self> {
32− use std::env;
33− use std::ffi::OsString;
34− use std::path::PathBuf;
35− use std::process::{Command, Stdio};
36−
37− let mut replace_arguments_to_less = false;
38−
39− let pager_from_env = match (env::var("BAT_PAGER"), env::var("PAGER")) {
40− (Ok(bat_pager), _) => Some(bat_pager),
41− (_, Ok(pager)) => {
42− // less needs to be called with the '-R' option in order to properly interpret the
43− // ANSI color sequences printed by bat. If someone has set PAGER="less -F", we
44− // therefore need to overwrite the arguments and add '-R'.
45− //
46− // We only do this for PAGER (as it is not specific to 'bat'), not for BAT_PAGER
47− // or bats '--pager' command line option.
48− replace_arguments_to_less = true;
49− Some(pager)
50− }
51− _ => None,
52− };
53−
54− let pager_from_config = pager_from_config.map(|p| p.to_string());
55−
56− if pager_from_config.is_some() {
57− replace_arguments_to_less = false;
58− }
59−
60− let pager = pager_from_config
61− .or(pager_from_env)
62− .unwrap_or_else(|| String::from("less"));
63−
64− let pagerflags =
65− shell_words::split(&pager).chain_err(|| "Could not parse pager command.")?;
66−
67− match pagerflags.split_first() {
68− Some((pager_name, args)) => {
69− let mut pager_path = PathBuf::from(pager_name);
70−
71− if pager_path.file_stem() == Some(&OsString::from("bat")) {
72− pager_path = PathBuf::from("less");
73− }
74−
75− let is_less = pager_path.file_stem() == Some(&OsString::from("less"));
76−
77− let mut process = if is_less {
78− let mut p = Command::new(&pager_path);
79− if args.is_empty() || replace_arguments_to_less {
80− p.arg("--RAW-CONTROL-CHARS");
81− if quit_if_one_screen {
82− p.arg("--quit-if-one-screen");
83− }
84−
85− // Passing '--no-init' fixes a bug with '--quit-if-one-screen' in older
86− // versions of 'less'. Unfortunately, it also breaks mouse-wheel support.
87− //
88− // See: http://www.greenwoodsoftware.com/less/news.530.html
89− //
90− // For newer versions (530 or 558 on Windows), we omit '--no-init' as it
91− // is not needed anymore.
92− match retrieve_less_version() {
93− None => {
94− p.arg("--no-init");
95− }
96− Some(version)
97− if (version < 530 || (cfg!(windows) && version < 558)) =>
98− {
99− p.arg("--no-init");
100− }
101− _ => {}
102− }
103− } else {
104− p.args(args);
105− }
106− p.env("LESSCHARSET", "UTF-8");
107− p
108− } else {
109− let mut p = Command::new(&pager_path);
110− p.args(args);
111− p
112− };
113−
114− Ok(process
115− .stdin(Stdio::piped())
116− .spawn()
117− .map(OutputType::Pager)
118− .unwrap_or_else(|_| OutputType::stdout()))
119− }
120− None => Ok(OutputType::stdout()),
121− }
122− }
123−
124− pub(crate) fn stdout() -> Self {
125− OutputType::Stdout(io::stdout())
126− }
127−
128− #[cfg(feature = "paging")]
129− pub(crate) fn is_pager(&self) -> bool {
130− if let OutputType::Pager(_) = self {
131− true
132− } else {
133− false
134− }
135− }
136−
137− #[cfg(not(feature = "paging"))]
138− pub(crate) fn is_pager(&self) -> bool {
139− false
140− }
141−
142− pub fn handle(&mut self) -> Result<&mut dyn Write> {
143− Ok(match *self {
144− #[cfg(feature = "paging")]
145− OutputType::Pager(ref mut command) => command
146− .stdin
147− .as_mut()
148− .chain_err(|| "Could not open stdin for pager")?,
149− OutputType::Stdout(ref mut handle) => handle,
150− })
151− }
152−}
153−
154−#[cfg(feature = "paging")]
155−impl Drop for OutputType {
156− fn drop(&mut self) {
157− if let OutputType::Pager(ref mut command) = *self {
158− let _ = command.wait();
159− }
160− }
161−}
tests/syntax-tests/regression_test.shnew file+15
@@ -0,0 +1,15 @@
1+#!/bin/bash
2+
3+set -eou pipefail
4+
5+script_directory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
6+
7+output_directory=$(mktemp -d --suffix=.bat-syntax-regression-test)
8+
9+"$script_directory"/create_highlighted_versions.py --output="$output_directory"
10+
11+echo
12+
13+"$script_directory"/compare_highlighted_versions.py \
14+ "$script_directory/highlighted" \
15+ "$output_directory"