Skip to content

Commit

Permalink
pypyr.steps.filereplace get_formatted X2
Browse files Browse the repository at this point in the history
get_formatted runs a redundant second time on filereplace
inputs.
Closes #303.
  • Loading branch information
yaythomas committed Oct 20, 2022
1 parent 5bb3bc5 commit 94cb270
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
5 changes: 1 addition & 4 deletions pypyr/steps/dsl/fileinoutrewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,8 @@ def __init__(self, name, root_key, context):

def run_step(self):
"""Write in to out, replacing strings per the replace_pairs."""
formatted_replacements = self.context.get_formatted_value(
self.replace_pairs)

iter = StreamReplacePairsRewriterStep.iter_replace_strings(
formatted_replacements)
self.replace_pairs)
rewriter = StreamRewriter(iter,
encoding_in=self.encoding_in,
encoding_out=self.encoding_out)
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/pypyr/steps/dsl/fileinoutrewriter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,49 @@ def test_streamreplacepairsrewriterstep_run_step(mock_rewriter):
out_path='outpathhere')


@patch('pypyr.steps.dsl.fileinoutrewriter.StreamRewriter', spec=StreamRewriter)
def test_streamreplacepairsrewriterstep_run_step_substitutions(mock_rewriter):
"""Stream replace pairs rewriter files_in_to_out with substitutions."""
context = Context({
'k1': 'b',
'k2': 'd',
'k3': '{k2}',
'root': {'in': 'inpathhere',
'out': 'outpathhere',
'replacePairs': {
'a': '{k1}',
'c': '{k3}'
},
'encodingIn': 'encIn',
'encodingOut': 'encOut'}})

obj = StreamReplacePairsRewriterStep('blah.name', 'root', context)
assert obj.path_in == 'inpathhere'
assert obj.path_out == 'outpathhere'
assert obj.context == context
assert obj.logger.name == 'blah.name'
assert obj.replace_pairs == {'a': 'b', 'c': 'd'}
assert obj.encoding_in == 'encIn'
assert obj.encoding_out == 'encOut'

iter_replace_strings_target = ('pypyr.steps.dsl.fileinoutrewriter.'
'StreamReplacePairsRewriterStep.'
'iter_replace_strings')
with patch(iter_replace_strings_target) as mock_iter:
obj.run_step()

# the rewriter should've been instantiated with the iter_replace_strings
# function.
mock_iter.assert_called_once_with({'a': 'b', 'c': 'd'})
assert mock_rewriter.mock_calls[0] == call(mock_iter.return_value,
encoding_in='encIn',
encoding_out='encOut')

mock_rewriter.return_value.files_in_to_out.assert_called_once_with(
in_path='inpathhere',
out_path='outpathhere')


@patch('pypyr.steps.dsl.fileinoutrewriter.StreamRewriter', spec=StreamRewriter)
def test_streamreplacepairsrewriterstep_run_step_no_out(mock_rewriter):
"""Stream replace pairs rewriter runs files_in_to_out with no out."""
Expand Down

0 comments on commit 94cb270

Please sign in to comment.