Browse Source

Fix a traceback when an invalid command-line flag or action is used (#768).

Dan Helfman 1 year ago
parent
commit
c687dafdd2
3 changed files with 15 additions and 3 deletions
  1. 2 1
      NEWS
  2. 2 2
      borgmatic/logger.py
  3. 11 0
      tests/end-to-end/test_invalid_flag.py

+ 2 - 1
NEWS

@@ -1,7 +1,8 @@
 1.8.4.dev0
- * Add a monitoring hook for sending backup status to a variety of monitoring services via the
+ * #715: Add a monitoring hook for sending backup status to a variety of monitoring services via the
    Apprise library. See the documentation for more information:
    https://torsion.org/borgmatic/docs/how-to/monitor-your-backups/#apprise-hook
+ * #768: Fix a traceback when an invalid command-line flag or action is used.
 
 1.8.3
  * #665: BREAKING: Simplify logging logic as follows: Syslog verbosity is now disabled by

+ 2 - 2
borgmatic/logger.py

@@ -166,6 +166,8 @@ def configure_logging(
 
     Raise FileNotFoundError or PermissionError if the log file could not be opened for writing.
     '''
+    add_custom_log_levels()
+
     if syslog_log_level is None:
         syslog_log_level = logging.DISABLED
     if log_file_log_level is None:
@@ -173,8 +175,6 @@ def configure_logging(
     if monitoring_log_level is None:
         monitoring_log_level = console_log_level
 
-    add_custom_log_levels()
-
     # Log certain log levels to console stderr and others to stdout. This supports use cases like
     # grepping (non-error) output.
     console_disabled = logging.NullHandler()

+ 11 - 0
tests/end-to-end/test_invalid_flag.py

@@ -0,0 +1,11 @@
+import subprocess
+import sys
+
+
+def test_borgmatic_command_with_invalid_flag_shows_error_but_not_traceback():
+    output = subprocess.run(
+        'borgmatic -v 2 --invalid'.split(' '), stdout=subprocess.PIPE, stderr=subprocess.STDOUT
+    ).stdout.decode(sys.stdout.encoding)
+
+    assert 'Unrecognized argument' in output
+    assert 'Traceback' not in output