mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-28 18:24:38 -05:00
Captures the stdout and stderr of the pre/post scripts into the log
This commit is contained in:

committed by
Trenton H

parent
3d2f249e97
commit
81c98103a8
@@ -2,7 +2,9 @@ import datetime
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import stat
|
||||
import tempfile
|
||||
from subprocess import CalledProcessError
|
||||
from unittest import mock
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
@@ -801,7 +803,7 @@ class TestConsumerCreatedDate(DirectoriesMixin, TestCase):
|
||||
|
||||
|
||||
class PreConsumeTestCase(TestCase):
|
||||
@mock.patch("documents.consumer.Popen")
|
||||
@mock.patch("documents.consumer.run")
|
||||
@override_settings(PRE_CONSUME_SCRIPT=None)
|
||||
def test_no_pre_consume_script(self, m):
|
||||
c = Consumer()
|
||||
@@ -809,7 +811,7 @@ class PreConsumeTestCase(TestCase):
|
||||
c.run_pre_consume_script()
|
||||
m.assert_not_called()
|
||||
|
||||
@mock.patch("documents.consumer.Popen")
|
||||
@mock.patch("documents.consumer.run")
|
||||
@mock.patch("documents.consumer.Consumer._send_progress")
|
||||
@override_settings(PRE_CONSUME_SCRIPT="does-not-exist")
|
||||
def test_pre_consume_script_not_found(self, m, m2):
|
||||
@@ -818,7 +820,7 @@ class PreConsumeTestCase(TestCase):
|
||||
c.path = "path-to-file"
|
||||
self.assertRaises(ConsumerError, c.run_pre_consume_script)
|
||||
|
||||
@mock.patch("documents.consumer.Popen")
|
||||
@mock.patch("documents.consumer.run")
|
||||
def test_pre_consume_script(self, m):
|
||||
with tempfile.NamedTemporaryFile() as script:
|
||||
with override_settings(PRE_CONSUME_SCRIPT=script.name):
|
||||
@@ -830,14 +832,45 @@ class PreConsumeTestCase(TestCase):
|
||||
|
||||
args, kwargs = m.call_args
|
||||
|
||||
command = args[0]
|
||||
command = kwargs["args"]
|
||||
|
||||
self.assertEqual(command[0], script.name)
|
||||
self.assertEqual(command[1], "path-to-file")
|
||||
|
||||
@mock.patch("documents.consumer.Consumer.log")
|
||||
def test_script_with_output(self, mocked_log):
|
||||
"""
|
||||
GIVEN:
|
||||
- A script which outputs to stdout and stderr
|
||||
WHEN:
|
||||
- The script is executed as a consume script
|
||||
THEN:
|
||||
- The script's outputs are logged
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile(mode="w") as script:
|
||||
# Write up a little script
|
||||
with script.file as outfile:
|
||||
outfile.write("#!/usr/bin/env bash\n")
|
||||
outfile.write("echo This message goes to stdout\n")
|
||||
outfile.write("echo This message goes to stderr >&2")
|
||||
|
||||
# Make the file executable
|
||||
st = os.stat(script.name)
|
||||
os.chmod(script.name, st.st_mode | stat.S_IEXEC)
|
||||
|
||||
with override_settings(PRE_CONSUME_SCRIPT=script.name):
|
||||
c = Consumer()
|
||||
c.path = "path-to-file"
|
||||
c.run_pre_consume_script()
|
||||
|
||||
mocked_log.assert_called()
|
||||
|
||||
mocked_log.assert_any_call("info", "This message goes to stdout")
|
||||
mocked_log.assert_any_call("warning", "This message goes to stderr")
|
||||
|
||||
|
||||
class PostConsumeTestCase(TestCase):
|
||||
@mock.patch("documents.consumer.Popen")
|
||||
@mock.patch("documents.consumer.run")
|
||||
@override_settings(POST_CONSUME_SCRIPT=None)
|
||||
def test_no_post_consume_script(self, m):
|
||||
doc = Document.objects.create(title="Test", mime_type="application/pdf")
|
||||
@@ -858,7 +891,7 @@ class PostConsumeTestCase(TestCase):
|
||||
c.filename = "somefile.pdf"
|
||||
self.assertRaises(ConsumerError, c.run_post_consume_script, doc)
|
||||
|
||||
@mock.patch("documents.consumer.Popen")
|
||||
@mock.patch("documents.consumer.run")
|
||||
def test_post_consume_script_simple(self, m):
|
||||
with tempfile.NamedTemporaryFile() as script:
|
||||
with override_settings(POST_CONSUME_SCRIPT=script.name):
|
||||
@@ -868,7 +901,7 @@ class PostConsumeTestCase(TestCase):
|
||||
|
||||
m.assert_called_once()
|
||||
|
||||
@mock.patch("documents.consumer.Popen")
|
||||
@mock.patch("documents.consumer.run")
|
||||
def test_post_consume_script_with_correspondent(self, m):
|
||||
with tempfile.NamedTemporaryFile() as script:
|
||||
with override_settings(POST_CONSUME_SCRIPT=script.name):
|
||||
@@ -889,7 +922,7 @@ class PostConsumeTestCase(TestCase):
|
||||
|
||||
args, kwargs = m.call_args
|
||||
|
||||
command = args[0]
|
||||
command = kwargs["args"]
|
||||
|
||||
self.assertEqual(command[0], script.name)
|
||||
self.assertEqual(command[1], str(doc.pk))
|
||||
|
Reference in New Issue
Block a user