Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option -m/--max #67

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ Once installed, use ``--help`` to list all available options::
--no-caps Turn off capitalization.
-s NUM, --specials NUM
Insert NUM special chars into generated word.
-m NUM, --max NUM
Truncate to NUM length.
-d DELIMITER, --delimiter DELIMITER
Separate words by DELIMITER. Empty string by default.
-r SOURCE, --randomsource SOURCE
Expand Down
5 changes: 5 additions & 0 deletions diceware/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ def handle_options(args):
parser.add_argument(
'-s', '--specials', default=0, type=int, metavar='NUM',
help="Insert NUM special chars into generated word.")
parser.add_argument(
'-m', '--max', default=0, type=int, metavar='NUM',
Copy link
Owner

Choose a reason for hiding this comment

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

Default -1 or None would be more appropriate, I think.

help="Truncate to NUM length.")
parser.add_argument(
'-d', '--delimiter', default='',
help="Separate words by DELIMITER. Empty string by default.")
Expand Down Expand Up @@ -193,6 +196,8 @@ def get_passphrase(options=None):
result = options.delimiter.join(words)
for _ in range(options.specials):
result = insert_special_char(result, rnd=rnd)
if options.max > 0:
result = result[:options.max]
Comment on lines +199 to +200
Copy link
Owner

Choose a reason for hiding this comment

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

Shouldn't truncating happen before inserting special chars (that might get lost this way round)?

return result


Expand Down
1 change: 1 addition & 0 deletions diceware/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
num=6,
caps=True,
specials=0,
max=0,
delimiter="",
randomsource="system",
verbose=0,
Expand Down
1 change: 1 addition & 0 deletions tests/sample_dot_diceware.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ num = 6
caps = on
specials = 0
delimiter = ""
max = 0
randomsource = "system"
verbose = 0
wordlist = "en_securedrop"
Expand Down
17 changes: 16 additions & 1 deletion tests/test_diceware.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def test_handle_options_defaults(self):
assert options.randomsource == "system"
assert options.wordlist == "en_eff"
assert options.verbose == 0
assert options.max == 0

def test_handle_options_infile(self, tmpdir):
# we can give an infile
Expand All @@ -69,6 +70,14 @@ def test_handle_options_caps(self):
assert options.caps is True
options = handle_options(['--no-caps', ])
assert options.caps is False

def test_handle_options_max(self):
options = handle_options([])
assert options.max == 0
options = handle_options(['-m', '0'])
assert options.max == 0
options = handle_options(['--max', '0'])
assert options.max == 0
Comment on lines +77 to +80
Copy link
Owner

Choose a reason for hiding this comment

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

Values apart from 0 would be more interesting here.


def test_handle_options_caps_conflicting_raises_exc(self):
# conflicting caps-settings raise an exception
Expand Down Expand Up @@ -243,6 +252,12 @@ def test_get_passphrase_delimiters(self):
options.delimiter = " "
phrase = get_passphrase(options)
assert " " in phrase

def test_get_passphrase_max(self):
options = handle_options(args=[])
options.max = 15
phrase = get_passphrase(options)
assert len(phrase) == 15
Comment on lines +256 to +260
Copy link
Owner

Choose a reason for hiding this comment

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

This test would be more meaningful if it could ensure, that the original phrase was longer or shorter than 15 chars.


def test_print_version(self, capsys):
# we can print version infos
Expand Down Expand Up @@ -362,4 +377,4 @@ def test_main_wordlist(self, argv_handler, capsys, wordlists_dir):
sys.argv = ['diceware', '-w', 'foo']
main()
out, err = capsys.readouterr()
assert out == 'FooFooFooFooFooFoo\n'
assert out == 'FooFooFooFooFooFoo\n'
Copy link
Owner

Choose a reason for hiding this comment

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

The trailing newline is nitpicking, I know.