-
Notifications
You must be signed in to change notification settings - Fork 481
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 a (b)ack option to 'Is this a valid secret?' Closes Issue #63 #72
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
244220b
Provide (b)ack option to user.
49235f7
First draft of what an iterator that can be told to step back once on
5405b34
removing from gitignore
fe723e2
iterator python2 compatibility hack: define that will call (I'll ne…
2d4c160
Unit tests for BidirectionalIterator
1134973
Audit loops only over secrets with decision, result is constructed an…
6e922b8
_handle_user_decision now deletes flag if skip decision 's' is passe…
2a6995c
Integration tests for going back and overwriting decisions
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class BidirectionalIterator(object): | ||
def __init__(self, collection): | ||
self.collection = collection | ||
self.index = -1 # starts on -1, as index is increased _before_ getting result | ||
self.step_back_once = False | ||
|
||
def __next__(self): | ||
if self.step_back_once: | ||
self.index -= 1 | ||
self.step_back_once = False | ||
else: | ||
self.index += 1 | ||
if self.index < 0: | ||
raise StopIteration | ||
try: | ||
result = self.collection[self.index] | ||
except IndexError: | ||
raise StopIteration | ||
return result | ||
|
||
def next(self): | ||
return self.__next__() | ||
|
||
def step_back_on_next_iteration(self): | ||
self.step_back_once = True | ||
|
||
def can_step_back(self): | ||
return self.index > 0 | ||
|
||
def __iter__(self): | ||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from __future__ import absolute_import | ||
|
||
import pytest | ||
|
||
from detect_secrets.core import bidirectional_iterator | ||
|
||
|
||
class TestBidirectionalIterator(object): | ||
|
||
def test_no_input(self): | ||
iterator = bidirectional_iterator.BidirectionalIterator([]) | ||
with pytest.raises(StopIteration): | ||
iterator.__next__() | ||
|
||
def test_cannot_step_back_too_far(self): | ||
iterator = bidirectional_iterator.BidirectionalIterator([0]) | ||
iterator.step_back_on_next_iteration() | ||
with pytest.raises(StopIteration): | ||
iterator.__next__() | ||
|
||
def test_cannot_step_back_too_far_after_stepping_in(self): | ||
iterator = bidirectional_iterator.BidirectionalIterator([0, 1, 2]) | ||
for _ in range(3): | ||
iterator.__next__() | ||
for _ in range(2): | ||
iterator.step_back_on_next_iteration() | ||
iterator.__next__() | ||
iterator.step_back_on_next_iteration() | ||
with pytest.raises(StopIteration): | ||
iterator.__next__() | ||
|
||
def test_works_correctly_in_loop(self): | ||
iterator = bidirectional_iterator.BidirectionalIterator([0, 1, 2, 3, 4, 5]) | ||
commands = [0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0] | ||
command_count = 0 | ||
results = [] | ||
for index in iterator: | ||
if commands[command_count]: | ||
iterator.step_back_on_next_iteration() | ||
results.append(index) | ||
command_count += 1 | ||
assert results == [0, 1, 0, 1, 2, 1, 0, 1, 2, 3, 4, 3, 2, 3, 4, 5] | ||
|
||
def test_normal_iterator_if_not_told_to_step_back(self): | ||
input_list = [0, 1, 2, 3, 4, 5] | ||
iterator = bidirectional_iterator.BidirectionalIterator(input_list) | ||
results = [] | ||
for index in iterator: | ||
results.append(index) | ||
assert results == input_list | ||
|
||
def test_knows_when_stepping_back_possible(self): | ||
iterator = bidirectional_iterator.BidirectionalIterator([0, 1, 2, 3]) | ||
commands = [0, 1, 0, 0, 1, 1, 0, 0, 0, 0] | ||
command_count = 0 | ||
results = [] | ||
for _ in iterator: | ||
if commands[command_count]: | ||
iterator.step_back_on_next_iteration() | ||
results.append(iterator.can_step_back()) | ||
command_count += 1 | ||
assert results == [False, True, False, True, True, True, False, True, True, True] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Looping should call the👍__next__
method directly, so no need for anext
methodThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added
next
to be python2 compatible (it was renamed formnext
to__next__
from python2 to 3).However, this does not feel very clean - perhaps you know of a better way? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha! Very good point, I only tested on Python 3. My bad. I am impressed by how thorough you are 👍