Skip to content

Commit

Permalink
Allow test cases to run without installing first
Browse files Browse the repository at this point in the history
As @spwhitton found:

The test suite needs to call "python3 -m ocrmypdf.main" instead of
just "ocrmypdf" because this /usr/bin/ocrmypdf script has not yet been
generated when dh runs the test suite.

---

Seems reasonable to perform in-place testing independent of installation.

Source:
https://sources.debian.net/src/ocrmypdf/4.2.1%2Bgit.20160824.1.5d67cc7-1/debian/patches/0001-patch-test-suite-executable.patch/
  • Loading branch information
James R. Barlow committed Aug 26, 2016
1 parent 1a9f09c commit 325cc0b
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions tests/test_main.py
Expand Up @@ -27,6 +27,7 @@
TEST_OUTPUT = os.environ.get(
'OCRMYPDF_TEST_OUTPUT',
default=os.path.join(PROJECT_ROOT, 'tests', 'output', 'main'))
OCRMYPDF = [sys.executable, '-m', 'ocrmypdf.main']


def running_in_docker():
Expand Down Expand Up @@ -54,6 +55,7 @@ def _outfile(output_basename):


def check_ocrmypdf(input_basename, output_basename, *args, env=None):
"Run ocrmypdf and confirmed that a valid file was created"
input_file = _infile(input_basename)
output_file = _outfile(output_basename)

Expand All @@ -70,13 +72,14 @@ def check_ocrmypdf(input_basename, output_basename, *args, env=None):


def run_ocrmypdf(input_basename, output_basename, *args, env=None):
"Run ocrmypdf and let caller deal with results"
input_file = _infile(input_basename)
output_file = _outfile(output_basename)

if env is None:
env = os.environ

p_args = ['ocrmypdf'] + list(args) + [input_file, output_file]
p_args = OCRMYPDF + list(args) + [input_file, output_file]
p = Popen(
p_args, close_fds=True, stdout=PIPE, stderr=PIPE,
universal_newlines=True, env=env)
Expand Down Expand Up @@ -617,7 +620,7 @@ def test_stdin(spoof_tesseract_noop):
p1_args = ['cat', input_file]
p1 = Popen(p1_args, close_fds=True, stdin=DEVNULL, stdout=PIPE)

p2_args = ['ocrmypdf', '-', output_file]
p2_args = OCRMYPDF + ['-', output_file]
p2 = Popen(
p2_args, close_fds=True, stdout=PIPE, stderr=PIPE,
stdin=p1.stdout, env=spoof_tesseract_noop)
Expand Down

6 comments on commit 325cc0b

@spwhitton
Copy link
Contributor

@spwhitton spwhitton commented on 325cc0b Aug 31, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbarlow83
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should work fine. This is really a fix to the test suite in that test_pageinfo.py and test_hocrtransform.py use import ocrmypdf.module and test_main.py did not necessarily locate the same installation if ocrmypdf is installed. By calling Python via sys.executable -m package the same import resolution order as import should be followed so all parts of the test suite are testing whatever Python finds in response to import ocrmypdf - meaning it follows sys.path, the first entry of which is the current working directory.

So as written, it will run tests on a new version prior to installation or on the installed version for CI.

That said, there could be reasons you might want to explicitly test /usr/[local/]bin/ocrmypdf. I can't think of anything that should be needed other than a basic sanity check:

test "$(ocrmypdf --version)" == "$(python3 -m ocrmypdf --version)"

Is there anything beyond that you'd need to check?

@spwhitton
Copy link
Contributor

@spwhitton spwhitton commented on 325cc0b Sep 1, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbarlow83
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I experimented and it seems that py.test is designed to take care of this automatically (provided a package has tests outside its package source tree, as with ocrmypdf). If running via python3 setup.py test (to test the package before installation) py.test inserts the uninstalled package into sys.path (I think by way of a temporary installation). If run as py.test it will take the system package because it changes PWD and runs each test in its directory.

So, with your patch applied, it did all work properly (no crossover of uninstalled/installed versions) before, but I now I understand why it works, which makes me feel a lot better about it. With your patch applied python3 setup.py test should work on Debian and other platforms as expected without patching.

@spwhitton
Copy link
Contributor

@spwhitton spwhitton commented on 325cc0b Sep 2, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spwhitton
Copy link
Contributor

@spwhitton spwhitton commented on 325cc0b Sep 15, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.