Repository: chezou/tabula-py
Pull request #254 (2020-08-02 07:40:42)

Fixed the issue when passing the silent argument
<!--- Provide a general summary of your changes in the Title above -->
The silent argument behaves as intended after this bug fix.

## Description
<!--- Describe your changes in detail -->
Behaviour Now :=
* missing option: messages about font errors are outputted.
* `silent=True:` messages about font errors are NOT outputted.
* `silent=False:` messages about font errors are outputted.
* `silent=None:` messages about font errors are outputted.

## Motivation and Context
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->
This fixes the issue #246 .

## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, tests ran to see how -->
<!--- your change affects other areas of the code, etc. -->
`nox`

## Screenshots (if appropriate):

## Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)

## Checklist:
<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->
- [x] I read [the contributing document](https://tabula-py.readthedocs.io/en/latest/contributing.html).
- [x] My code follows the code style of this project with running linter.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.



Patch:
diff --git a/tabula/io.py b/tabula/io.py
index 1b9f1da..414cd7f 100644
--- a/tabula/io.py
+++ b/tabula/io.py
@@ -62,7 +62,7 @@ def _run(java_options, options, path=None, encoding="utf-8"):
     """
     # Workaround to enforce the silent option. See:
     # https://github.com/tabulapdf/tabula-java/issues/231#issuecomment-397281157
-    if "silent" in options:
+    if options.get("silent"):
         java_options.extend(
             (
                 "-Dorg.slf4j.simpleLogger.defaultLogLevel=off",


Test patch:
diff --git a/tests/test_read_pdf_table.py b/tests/test_read_pdf_table.py
index 367e063..bd454bd 100644
--- a/tests/test_read_pdf_table.py
+++ b/tests/test_read_pdf_table.py
@@ -339,6 +339,63 @@ class TestReadPdfTable(unittest.TestCase):
             dfs_template[0].equals(pd.read_csv(template_expected_csv, **pandas_options))
         )
 
+    @patch("subprocess.run")
+    @patch("tabula.io._jar_path")
+    def test_read_pdf_with_silent_false(self, jar_func, mock_fun):
+        jar_func.return_value = "/tmp/tabula-java.jar"
+
+        tabula.read_pdf(self.pdf_path, encoding="utf-8", silent=False)
+
+        target_args = ["java"]
+        if platform.system() == "Darwin":
+            target_args += ["-Djava.awt.headless=true"]
+        target_args += [
+            "-Dfile.encoding=UTF8",
+            "-jar",
+            "/tmp/tabula-java.jar",
+            "--guess",
+            "--format",
+            "JSON",
+            "tests/resources/data.pdf",
+        ]
+        subp_args = {
+            "stdout": subprocess.PIPE,
+            "stderr": subprocess.PIPE,
+            "stdin": subprocess.DEVNULL,
+            "check": True,
+        }
+        mock_fun.assert_called_with(target_args, **subp_args)
+
+    @patch("subprocess.run")
+    @patch("tabula.io._jar_path")
+    def test_read_pdf_with_silent_true(self, jar_func, mock_fun):
+        jar_func.return_value = "/tmp/tabula-java.jar"
+
+        tabula.read_pdf(self.pdf_path, encoding="utf-8", silent=True)
+
+        target_args = ["java"]
+        if platform.system() == "Darwin":
+            target_args += ["-Djava.awt.headless=true"]
+        target_args += [
+            "-Dfile.encoding=UTF8",
+            "-Dorg.slf4j.simpleLogger.defaultLogLevel=off",
+            "-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog",
+            "-jar",
+            "/tmp/tabula-java.jar",
+            "--guess",
+            "--format",
+            "JSON",
+            "--silent",
+            "tests/resources/data.pdf",
+        ]
+        subp_args = {
+            "stdout": subprocess.PIPE,
+            "stderr": subprocess.PIPE,
+            "stdin": subprocess.DEVNULL,
+            "check": True,
+        }
+        mock_fun.assert_called_with(target_args, **subp_args)
+
 
 if __name__ == "__main__":
     unittest.main()