3
3
import string
4
4
import textwrap
5
5
from contextlib import contextmanager
6
+ from copy import deepcopy
6
7
7
8
import mock
8
9
import pytest
@@ -33,29 +34,39 @@ def test_quit_before_making_decision(self, mock_printer):
33
34
)
34
35
35
36
def test_nothing_to_audit (self , mock_printer ):
36
- modified_baseline = self .baseline . copy ( )
37
+ modified_baseline = deepcopy ( self .baseline )
37
38
modified_baseline ['results' ]['filenameA' ][0 ]['is_secret' ] = True
38
39
modified_baseline ['results' ]['filenameA' ][1 ]['is_secret' ] = False
40
+ modified_baseline ['results' ]['filenameB' ][0 ]['is_secret' ] = False
39
41
40
42
with self .mock_env (baseline = modified_baseline ):
41
43
audit .audit_baseline ('will_be_mocked' )
42
44
43
45
assert mock_printer .message == 'Nothing to audit!\n '
44
46
45
47
def test_making_decisions (self , mock_printer ):
46
- modified_baseline = self .baseline .copy ()
47
- modified_baseline ['results' ]['filenameA' ][0 ]['is_secret' ] = True
48
- modified_baseline ['results' ]['filenameA' ][1 ]['is_secret' ] = False
48
+ modified_baseline = deepcopy (self .baseline )
49
+
50
+ # Need to do it this way, because dictionaries are not ordered:
51
+ # meaning, that if we hard-code results to certain filenames, it's
52
+ # going to be a flakey test.
53
+ values_to_inject = [True , False , False ]
54
+ for secrets in modified_baseline ['results' ].values ():
55
+ for secret in secrets :
56
+ secret ['is_secret' ] = values_to_inject .pop (0 )
49
57
50
- self .run_logic (['y' , 'n' ], modified_baseline )
58
+ self .run_logic (['y' , 'n' , 'n' ], modified_baseline )
51
59
52
60
assert mock_printer .message == (
53
61
'Saving progress...\n '
54
62
)
55
63
56
64
def test_quit_half_way (self , mock_printer ):
57
- modified_baseline = self .baseline .copy ()
58
- modified_baseline ['results' ]['filenameA' ][0 ]['is_secret' ] = False
65
+ modified_baseline = deepcopy (self .baseline )
66
+
67
+ for secrets in modified_baseline ['results' ].values ():
68
+ secrets [0 ]['is_secret' ] = False
69
+ break
59
70
60
71
self .run_logic (['n' , 'q' ], modified_baseline )
61
72
@@ -65,10 +76,16 @@ def test_quit_half_way(self, mock_printer):
65
76
)
66
77
67
78
def test_skip_decision (self , mock_printer ):
68
- modified_baseline = self .baseline .copy ()
69
- modified_baseline ['results' ]['filenameA' ][1 ]['is_secret' ] = True
79
+ modified_baseline = deepcopy (self .baseline )
80
+
81
+ values_to_inject = [None , True , True ]
82
+ for secrets in modified_baseline ['results' ].values ():
83
+ for secret in secrets :
84
+ value = values_to_inject .pop (0 )
85
+ if value :
86
+ secret ['is_secret' ] = value
70
87
71
- self .run_logic (['s' , 'y' ], modified_baseline )
88
+ self .run_logic (['s' , 'y' , 'y' ], modified_baseline )
72
89
73
90
assert mock_printer .message == (
74
91
'Saving progress...\n '
@@ -439,17 +456,16 @@ def mock_user_input(inputs):
439
456
:type inputs: list
440
457
:param inputs: list of user choices
441
458
"""
442
- current_case = {'index' : 0 } # needed, because py2 doesn't have nonlocal
443
-
444
459
class InputShim (object ):
445
460
def __init__ (self ):
446
461
self .message = ''
462
+ self .index = 0
447
463
448
464
def get_user_input (self , * args , ** kwargs ):
449
465
self .message += args [0 ]
450
466
451
- output = inputs [current_case [ ' index' ] ]
452
- current_case [ ' index' ] += 1
467
+ output = inputs [self . index ]
468
+ self . index += 1
453
469
454
470
return output
455
471
0 commit comments