1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright © 2018-19 Qtrac Ltd. All rights reserved.

use crate::util::PathExt;
use crate::xerr;
use crate::xerror::{xerror, XResult};
use crate::Test;
use image::{self, GenericImageView};
use json;
use std::io::{BufRead, BufReader, Read};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

/// The kind of comparison to do when retesting.
///
/// Retest will work this out for itself unless you override it.
///
/// For files with no suffix it assumes `DiffKind::Binary`, and for files
/// with suffix `.jsn` or `.json` it assumes `DiffKind::Json` (and compares
/// the JSON data, ignoring irrelevant whitespace). For common image
/// formats it assumes `DiffKind::Image` and compares pixel for pixel;
/// otherwise it assumes `DiffKind::Text` and compares text (but ignores
/// line-endings).
///
/// You can force the kind of comparison to use by setting the `DiffKind`.
///
/// Or you can force retest to use an external tool by using
/// [`DiffKind::custom(Path)`](enum.DiffKind.html#method.custom) giving it
/// the tool's name.
///
/// Or you can force retest _not_ to compare output (i.e., if you only want
/// to check the exit status/error level), by setting `DiffKind::No`.
///
/// The `DiffKind` is set per-test using the `DIFF` entry in
/// [retest plan files
/// (`.rt`)](http://www.qtrac.eu/retest.html#retestplanfile), or using the
/// [`Test::diff()`](struct.Test.html#method.diff)
/// method.
#[derive(Clone, Debug, PartialEq)]
pub enum DiffKind {
    No,
    Binary,
    Image,
    Json,
    Text,
    Custom(PathBuf),
}

impl DiffKind {
    /// Returns a `DiffKind::Custom` with the given external tool.
    ///
    /// For example, on Unix: `DiffKind::custom("diff")`.
    pub fn custom<P: AsRef<Path>>(diff: P) -> DiffKind {
        DiffKind::Custom(diff.as_ref().to_path_buf())
    }
}

pub(crate) fn is_same(
    test: &Test,
    expected_filename: &Path,
    actual_filename: &Path,
) -> XResult<bool> {
    let kind = match &test.diff {
        Some(kind) => kind.clone(),
        None => guess_kind(&expected_filename),
    };
    Ok(match kind {
        DiffKind::No => true, // do nothing; correct exit code is sufficient
        DiffKind::Binary => {
            is_same_binary(&expected_filename, &actual_filename)?
        }
        DiffKind::Image => {
            is_same_image(&expected_filename, &actual_filename)?
        }
        DiffKind::Json => {
            is_same_json(&expected_filename, &actual_filename)?
        }
        DiffKind::Text => {
            is_same_text(&expected_filename, &actual_filename)?
        }
        DiffKind::Custom(diff) => is_same_custom(&diff, &test.diff_args)?,
    })
}

pub(crate) fn diff_kind_for(diff: &str) -> Option<DiffKind> {
    Some(match diff.to_lowercase().as_str() {
        "0" | "no" | "false" => DiffKind::No,
        "rt-binary" => DiffKind::Binary,
        "rt-json" => DiffKind::Json,
        "rt-image" => DiffKind::Image,
        "rt-text" => DiffKind::Text,
        _ => DiffKind::Custom(Path::new(diff).to_path_buf()),
    })
}

fn guess_kind(filename: &Path) -> DiffKind {
    match filename.extension() {
        None => DiffKind::Binary,
        Some(ext) => {
            match ext.to_string_lossy().to_lowercase().as_str() {
                "json" | "jsn" => DiffKind::Json,
                "png" | "jpg" | "jpeg" | "gif" | "bmp" | "ico"
                | "tif" | "tiff" | "pbm" | "pgm" | "ppm" | "pam" => {
                    DiffKind::Image
                }
                _ => DiffKind::Text,
            }
        }
    }
}

fn is_same_text(
    expected_filename: &Path,
    actual_filename: &Path,
) -> XResult<bool> {
    let expected = &expected_filename.open()?;
    let actual = &actual_filename.open()?;
    let expected_iter = BufReader::new(expected);
    let actual_iter = BufReader::new(actual);
    for (left, right) in expected_iter.lines().zip(actual_iter.lines()) {
        let left = left?;
        let right = right?;
        if left != right {
            return Ok(false);
        }
    }
    Ok(true)
}

fn is_same_binary(
    expected_filename: &Path,
    actual_filename: &Path,
) -> XResult<bool> {
    if &expected_filename.len()? != &actual_filename.len()? {
        return Ok(false);
    }
    let expected = &expected_filename.open()?;
    let actual = &actual_filename.open()?;
    for (left, right) in expected.bytes().zip(actual.bytes()) {
        let left = left?;
        let right = right?;
        if left != right {
            return Ok(false);
        }
    }
    Ok(true)
}

fn is_same_image(
    expected_filename: &Path,
    actual_filename: &Path,
) -> XResult<bool> {
    let expected = image::open(&expected_filename).or_else(|err| {
        xerr!(
            "Failed to open \"{}\": {}",
            expected_filename.display(),
            err
        )
    })?;
    let actual = image::open(&actual_filename).or_else(|err| {
        xerr!("Failed to open \"{}\": {}", actual_filename.display(), err)
    })?;
    if expected.dimensions() != actual.dimensions() {
        return Ok(false);
    }
    for (left, right) in expected.pixels().zip(actual.pixels()) {
        if left.2 != right.2 {
            // (x, y, pixel)
            return Ok(false);
        }
    }
    Ok(true)
}

fn is_same_json(
    expected_filename: &Path,
    actual_filename: &Path,
) -> XResult<bool> {
    let mut expected = &expected_filename.open()?;
    let mut expected_json = String::new();
    expected.read_to_string(&mut expected_json)?;
    let expected = json::parse(&expected_json)?;
    let mut actual = &actual_filename.open()?;
    let mut actual_json = String::new();
    actual.read_to_string(&mut actual_json)?;
    let actual = json::parse(&actual_json)?;
    Ok(expected == actual)
}

fn is_same_custom(command: &Path, args: &[String]) -> XResult<bool> {
    let mut exe = Command::new(&command);
    exe.args(args).stdout(Stdio::null()).stderr(Stdio::piped());
    let message = format!("{} {}", command.display(), args.join(" "));
    let child = exe
        .spawn()
        .or_else(|err| xerr!("Failed to run {}: {}", &message, err))?;
    let output = child.wait_with_output().or_else(|err| {
        xerr!("Failed to complete {}: {}", &message, err)
    })?;
    if !output.stderr.is_empty() {
        let stderr =
            String::from_utf8_lossy(&output.stderr).trim_end().to_owned();
        let mut out = String::new();
        for c in stderr.chars() {
            if c == '\n' || c == '\r' || out.len() >= 150 {
                break;
            }
            out.push(c);
        }
        if out.len() < stderr.len() {
            out.push_str("...");
        }
        xerr!("{} -> stderr: {}", &message, out);
    }
    match output.status.code() {
        Some(0) => Ok(true), // 0 implies same
        _ => Ok(false),
    }
}