mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-25 10:07:40 -05:00
test(reports): run the first pdftotext on PATH that accepts -bbox
The PDF report tests ran whichever pdftotext came first on PATH. Git for Windows ships Xpdf's, which has no -bbox, and shells that list Git first hid Poppler's: 21 tests failed with "Expected 0, Actual 99" on a machine that had Poppler installed. ReportPdf now takes the first pdftotext whose help offers -bbox, fails naming what it found when none does, and still skips when there is none at all. OPENNEST_PDFTOTEXT names one outright. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
24f70fc3f9
commit
9ffd02db73
3 files changed
+125
-8
No files matched your search
@@ -30,12 +30,12 @@ public static class ReportPdf
|
||||
|
||||
/// <summary>Visual-row text of one page; bbox coordinates avoid platform-specific -layout order.</summary>
|
||||
public static string Text(string path, int page) =>
|
||||
LayoutPage(Run("pdftotext", "-bbox", "-f", Page(page), "-l", Page(page), path, "-"));
|
||||
LayoutPage(Run("-bbox", "-f", Page(page), "-l", Page(page), path, "-"));
|
||||
|
||||
/// <summary>Every page's visual-row text, in order.</summary>
|
||||
public static string[] Pages(string path)
|
||||
{
|
||||
var html = Run("pdftotext", "-bbox", path, "-");
|
||||
var html = Run("-bbox", path, "-");
|
||||
return Regex.Matches(html, @"<page\b[^>]*>.*?</page>", RegexOptions.Singleline)
|
||||
.Select(match => LayoutPage(match.Value)).ToArray();
|
||||
}
|
||||
@@ -62,7 +62,7 @@ public static class ReportPdf
|
||||
/// </summary>
|
||||
public static List<string> PartCards(string path)
|
||||
{
|
||||
var html = Run("pdftotext", "-bbox", path, "-");
|
||||
var html = Run("-bbox", path, "-");
|
||||
var cards = new List<(int Page, double Top, int Column, string Text)>();
|
||||
var started = false;
|
||||
var pages = Regex.Matches(html, @"<page\b[^>]*>.*?</page>", RegexOptions.Singleline).Select(match => match.Value).ToList();
|
||||
@@ -128,7 +128,7 @@ public static class ReportPdf
|
||||
|
||||
/// <summary>Words with page-space boxes (points, Y down).</summary>
|
||||
public static List<(double Left, double Top, double Right, double Bottom, string Text)> Words(string path, int page) =>
|
||||
ParseWords(Run("pdftotext", "-bbox", "-f", Page(page), "-l", Page(page), path, "-"));
|
||||
ParseWords(Run("-bbox", "-f", Page(page), "-l", Page(page), path, "-"));
|
||||
|
||||
private static List<(double Left, double Top, double Right, double Bottom, string Text)> ParseWords(string xml)
|
||||
{
|
||||
@@ -142,8 +142,73 @@ public static class ReportPdf
|
||||
|
||||
private static double Number(string value) => double.Parse(value, CultureInfo.InvariantCulture);
|
||||
|
||||
private static string Run(string tool, params string[] arguments)
|
||||
/// <summary>Names the pdftotext to run, instead of the first on PATH that accepts -bbox.</summary>
|
||||
public const string ToolVariable = "OPENNEST_PDFTOTEXT";
|
||||
|
||||
private static readonly Lazy<(string Tool, string Problem)> PdfToText = new(FindPdfToText);
|
||||
|
||||
/// <summary>
|
||||
/// The pdftotext the text assertions run. PATH can hold more than one: Git for Windows
|
||||
/// ships Xpdf's, which has no -bbox, and a shell that puts Git first hides Poppler's.
|
||||
/// So the search takes the first that offers -bbox rather than the first it meets.
|
||||
/// </summary>
|
||||
private static (string Tool, string Problem) FindPdfToText()
|
||||
{
|
||||
var named = Environment.GetEnvironmentVariable(ToolVariable);
|
||||
if (!string.IsNullOrWhiteSpace(named))
|
||||
return (named, null);
|
||||
|
||||
var name = OperatingSystem.IsWindows() ? "pdftotext.exe" : "pdftotext";
|
||||
var found = (Environment.GetEnvironmentVariable("PATH") ?? string.Empty)
|
||||
.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.Select(folder => Path.Combine(folder.Trim('"'), name))
|
||||
.Where(File.Exists)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToList();
|
||||
|
||||
var tool = FirstWithBbox(found, Help);
|
||||
if (tool != null)
|
||||
return (tool, null);
|
||||
|
||||
return found.Count == 0
|
||||
? (null, null)
|
||||
: (null, $"No pdftotext on PATH accepts -bbox (found {string.Join(", ", found)}). "
|
||||
+ $"Install Poppler, or name its pdftotext in {ToolVariable}.");
|
||||
}
|
||||
|
||||
/// <summary>The first candidate whose help text offers -bbox, or null when none does.</summary>
|
||||
internal static string FirstWithBbox(IEnumerable<string> candidates, Func<string, string> help) =>
|
||||
candidates.FirstOrDefault(candidate => Regex.IsMatch(help(candidate), @"(?m)^\s*-bbox(?![\w-])"));
|
||||
|
||||
/// <summary>What a pdftotext prints for -h, on either stream; empty when it cannot run.</summary>
|
||||
private static string Help(string tool)
|
||||
{
|
||||
try
|
||||
{
|
||||
var info = new ProcessStartInfo(tool)
|
||||
{
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
};
|
||||
info.ArgumentList.Add("-h");
|
||||
using var process = Process.Start(info)!;
|
||||
var error = process.StandardError.ReadToEndAsync();
|
||||
var output = process.StandardOutput.ReadToEnd();
|
||||
process.WaitForExit();
|
||||
return output + error.Result;
|
||||
}
|
||||
catch (Exception exception) when (exception is System.ComponentModel.Win32Exception or IOException or InvalidOperationException)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
private static string Run(params string[] arguments)
|
||||
{
|
||||
var (tool, problem) = PdfToText.Value;
|
||||
Assert.True(problem == null, problem);
|
||||
Skip.If(tool == null, "pdftotext (poppler-utils) is not installed.");
|
||||
|
||||
var info = new ProcessStartInfo(tool)
|
||||
{
|
||||
RedirectStandardOutput = true,
|
||||
@@ -156,9 +221,10 @@ public static class ReportPdf
|
||||
{
|
||||
process = Process.Start(info)!;
|
||||
}
|
||||
catch (System.ComponentModel.Win32Exception)
|
||||
catch (System.ComponentModel.Win32Exception exception)
|
||||
{
|
||||
Skip.If(true, $"{tool} (poppler-utils) is not installed.");
|
||||
// Only a pdftotext named in the variable gets here: one found on PATH exists.
|
||||
Assert.Fail($"{tool} ({ToolVariable}) cannot start: {exception.Message}");
|
||||
throw;
|
||||
}
|
||||
using (process)
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
namespace OpenNest.Tests.Reporting;
|
||||
|
||||
/// <summary>
|
||||
/// The report tests need a pdftotext with -bbox. PATH can list one without it first (Git for
|
||||
/// Windows ships Xpdf's), so the search must pass over it.
|
||||
/// </summary>
|
||||
public class ReportPdfToolTests
|
||||
{
|
||||
private const string XpdfHelp = """
|
||||
pdftotext version 4.00
|
||||
Usage: pdftotext [options] <PDF-file> [<text-file>]
|
||||
-f <int> : first page to convert
|
||||
-layout : maintain original physical layout
|
||||
""";
|
||||
|
||||
private const string PopplerHelp = """
|
||||
pdftotext version 25.07.0
|
||||
Usage: pdftotext [options] <PDF-file> [<text-file>]
|
||||
-f <int> : first page to convert
|
||||
-bbox : output bounding box for each word and page size to html. Sets -htmlmeta
|
||||
-bbox-layout : like -bbox but with extra layout bounding box data. Sets -htmlmeta
|
||||
""";
|
||||
|
||||
[Fact]
|
||||
public void FirstWithBbox_PassesOverAToolWithoutTheOption()
|
||||
{
|
||||
var help = new Dictionary<string, string>
|
||||
{
|
||||
["git/pdftotext"] = XpdfHelp,
|
||||
["broken/pdftotext"] = string.Empty,
|
||||
["poppler/pdftotext"] = PopplerHelp,
|
||||
["later/pdftotext"] = PopplerHelp,
|
||||
};
|
||||
|
||||
Assert.Equal("poppler/pdftotext", ReportPdf.FirstWithBbox(help.Keys, tool => help[tool]));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FirstWithBbox_IsNullWhenNoToolHasTheOption()
|
||||
{
|
||||
// -bbox-layout alone is not the option the tests pass.
|
||||
var onlyLayout = " -bbox-layout : like -bbox but with extra layout bounding box data.";
|
||||
|
||||
Assert.Null(ReportPdf.FirstWithBbox(new[] { "a", "b" }, tool => tool == "a" ? XpdfHelp : onlyLayout));
|
||||
Assert.Null(ReportPdf.FirstWithBbox(Array.Empty<string>(), _ => PopplerHelp));
|
||||
}
|
||||
}
|
||||
@@ -284,7 +284,11 @@ The writer tests read page sizes and content streams with PDFsharp. Text
|
||||
assertions use Poppler's `pdftotext -bbox` coordinates to rebuild visual rows;
|
||||
`-layout` can return columns in a different order on Windows. Part cards are
|
||||
read column by column (`ReportPdf.PartCards`), each ending at its "Plates:" line. They are skipped,
|
||||
not passed, when poppler-utils is absent. The Windows release workflow installs
|
||||
not passed, when poppler-utils is absent. The tests run the first `pdftotext` on
|
||||
`PATH` whose help offers `-bbox`, so an older one listed first (Git for Windows
|
||||
ships Xpdf's) does not hide Poppler's; they fail, naming what they found, when
|
||||
every `pdftotext` on `PATH` lacks it. Set `OPENNEST_PDFTOTEXT` to a full path to
|
||||
choose one outright. The Windows release workflow installs
|
||||
an SHA-256-pinned Poppler binary because the runner's built-in `pdftotext` lacks
|
||||
`-bbox`; no PDF text test is skipped for that reason in a release candidate.
|
||||
For manual inspection also use `pdfinfo`,
|
||||
|
||||
Reference in new issue
Block a user