5

我可以将 ApprovalTests 与 PDF 一起使用吗?我尝试使用 FileLauncher,但似乎相同的 PDF 在文件(位)级别略有不同。还是我用错了?

[TestMethod]
[UseReporter(typeof(FileLauncherReporter))]
public void TestPdf()
{
    var createSomePdf = PdfCreate();

    ApprovalTests.Approvals.Verify(new FileInfo(createSomePdf.FileName));

}
4

3 回答 3

7

Pdf 很可能是使用时间戳创建的。根据用于创建 pdf 的方法,您也许可以模拟创建的时间。但我不得不擦洗它。

这是我用来执行此操作的代码。

    public static void VerifyPdf(string coverFile)
    {
        ScrubPdf(coverFile);
        Approvals.Verify(new ExistingFileWriter(coverFile));
    }

    private static void ScrubPdf(string coverFile)
    {
        long location;
        using (var pdf = File.OpenRead(coverFile))
        {
            location = Find("/CreationDate (", pdf);

        }
        using (var pdf = File.OpenWrite(coverFile))
        {
            pdf.Seek(location, SeekOrigin.Begin);

            var original = "/CreationDate (D:20110426104115-07'00')";
            var desired = new System.Text.ASCIIEncoding().GetBytes(original);

            pdf.Write(desired, 0, desired.Length);
            pdf.Flush();
        }
    }
于 2012-10-10T15:57:19.763 回答
5

我找到了一个命令行工具diff-pdf。比较 2 个 PDF,如果它们相同则返回退出代码 0,如果它们不同则返回 1。下载 + 提取 + 将其添加到您的 PATH 中。

缺点 - 它必须渲染两个 PDF 才能执行差异。如果它们很大,那么性能就很好。

批准人(主要基于ApprovalTests.Approvers.FileApprover):

public class DiffPdfApprover : IApprovalApprover
{
    public static void Verify(byte[] bytes)
    {
        var writer = new ApprovalTests.Writers.BinaryWriter(bytes, "pdf");
        var namer = ApprovalTests.Approvals.GetDefaultNamer();
        var reporter = ApprovalTests.Approvals.GetReporter();

        ApprovalTests.Core.Approvals.Verify(new DiffPdfApprover(writer, namer), reporter);
    }

    private DiffPdfApprover(IApprovalWriter writer, IApprovalNamer namer)
    {
        this.writer = writer;
        this.namer = namer;
    }

    private readonly IApprovalNamer namer;
    private readonly IApprovalWriter writer;
    private string approved;
    private ApprovalException failure;
    private string received;

    public virtual bool Approve()
    {
        string basename = string.Format(@"{0}\{1}", namer.SourcePath, namer.Name);
        approved = Path.GetFullPath(writer.GetApprovalFilename(basename));
        received = Path.GetFullPath(writer.GetReceivedFilename(basename));
        received = writer.WriteReceivedFile(received);

        failure = Approve(approved, received);
        return failure == null;
    }

    public static ApprovalException Approve(string approved, string received)
    {
        if (!File.Exists(approved))
        {
            return new ApprovalMissingException(received, approved);
        }

        var process = new Process();
        //settings up parameters for the install process
        process.StartInfo.FileName = "diff-pdf";
        process.StartInfo.Arguments = String.Format("\"{0}\" \"{1}\"", received, approved);

        process.Start();

        process.WaitForExit();

        if (process.ExitCode != 0)
        {
            return new ApprovalMismatchException(received, approved);
        }

        return null;
    }

    public void Fail()
    {
        throw failure;
    }

    public void ReportFailure(IApprovalFailureReporter reporter)
    {
        reporter.Report(approved, received);
    }

    public void CleanUpAfterSucess(IApprovalFailureReporter reporter)
    {
        File.Delete(received);
        if (reporter is IApprovalReporterWithCleanUp)
        {
            ((IApprovalReporterWithCleanUp)reporter).CleanUp(approved, received);
        }
    }
}

核实:

DiffPdfApprover.Verify(pdfBytes);

diff-pdf 也可以直观地显示差异。我为此滚动了一个 Reporter,但不要太多使用它。我认为如果在初始报告开发后出现回归(我现在所处的位置),它会派上用场。

public class DiffPdfReporter : GenericDiffReporter
{
    private static readonly string Path = FindFullPath("diff-pdf.exe");
    public DiffPdfReporter() : base(Path,
        GetArgs(),
        "Please put diff-pdf.exe in your %PATH%. https://github.com/vslavik/diff-pdf. And restart whatever's running the tests. Everything seems to cache the %PATH%.") { }

    private static string GetArgs()
    {
        return "--view \"{0}\" \"{1}\"";
    }

    private static string FindFullPath(string programInPath)
    {
        foreach (var path in from path in Environment.GetEnvironmentVariable("path").Split(';')
                             select path)
        {
            var fullPath = System.IO.Path.Combine(path, programInPath);
            if (File.Exists(fullPath))
                return fullPath;
        }
        return null;
    }
}
于 2013-06-27T04:32:49.747 回答
2

看起来这现在已内置到 ApprovalTests 中。

用法:

Approvals.VerifyPdfFile(pdfFileLocation);

查看源代码

public static void VerifyPdfFile(string pdfFilePath)
{
    PdfScrubber.ScrubPdf(pdfFilePath);
    Verify(new ExistingFileWriter(pdfFilePath));
}
于 2017-12-08T02:02:59.027 回答