Skip to content

Commit

Permalink
Ensure that cli options specified with action=Highlander can only be …
Browse files Browse the repository at this point in the history
…set once, even if the set value is a default value. Add tests for action=Highlander. See borgbackup#7500 borgbackup#6269
  • Loading branch information
jorickert committed Apr 10, 2023
1 parent 9307f80 commit a032fad
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/borg/archiver/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,37 @@ def wrapper(self, args, repository, manifest, **kwargs):
class Highlander(argparse.Action):
"""make sure some option is only given once"""

def __init__(
self,
option_strings,
dest,
nargs=None,
const=None,
default=None,
type=None,
choices=None,
required=False,
help=None,
metavar=None,
):
self.called = False
super(Highlander, self).__init__(
option_strings=option_strings,
dest=dest,
nargs=nargs,
const=const,
default=default,
type=type,
choices=choices,
required=required,
help=help,
metavar=metavar,
)

def __call__(self, parser, namespace, values, option_string=None):
if getattr(namespace, self.dest, None) != self.default:
if self.called:
raise argparse.ArgumentError(self, "There can be only one.")
self.called = True
setattr(namespace, self.dest, values)


Expand Down
20 changes: 19 additions & 1 deletion src/borg/testsuite/archiver/argparsing.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import argparse
import pytest
from itertools import combinations_with_replacement

from ...helpers import parse_storage_quota
from . import ArchiverTestCaseBase, Archiver, RK_ENCRYPTION
from . import ArchiverTestCaseBase, Archiver, RK_ENCRYPTION, src_dir


class ArchiverTestCase(ArchiverTestCaseBase):
Expand All @@ -11,6 +12,23 @@ def test_bad_filters(self):
self.cmd(f"--repo={self.repository_location}", "create", "test", "input")
self.cmd(f"--repo={self.repository_location}", "delete", "--first", "1", "--last", "1", fork=True, exit_code=2)

def test_highlander(self):
self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
self.cmd(f"--repo={self.repository_location}", "create", "--comment", "comment 1", "test-1", src_dir)
error_msg = "There can be only one"
# Default permission is 077
# Test that it works with a oen time specified default or custom value
output_default = self.cmd(f"--repo={self.repository_location}", "--umask", "077", "rlist")
assert error_msg not in output_default
output_custom = self.cmd(f"--repo={self.repository_location}", "--umask", "007", "rlist")
assert error_msg not in output_custom
# Test that all combinations of custom and default permissions fail
for first, second in combinations_with_replacement(["007", "077"], 2):
output_custom = self.cmd(
f"--repo={self.repository_location}", "--umask", first, "--umask", second, "rlist", exit_code=2
)
assert error_msg in output_custom


def test_get_args():
archiver = Archiver()
Expand Down

0 comments on commit a032fad

Please sign in to comment.