Skip to content

Commit ce4ab79

Browse files
committed
fix(dry-run): show diff with dry-run option
1 parent 63a92a9 commit ce4ab79

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

README.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,22 @@ this as a fallback in the case a suitable wheel cannot be found.
124124
Dry run mode
125125
============
126126

127-
There are some use cases, when you maybe don't want to edit your requirements.txt
128-
right away. You can use the ``-dry-run`` argument to just print the lines, as if they
129-
would be added to your requirements.txt file.
127+
There are some use cases, when you maybe don't want to edit your ``requirements.txt``
128+
right away. You can use the ``--dry-run`` argument to show the diff, so you
129+
can preview the changes to your ``requirements.txt`` file.
130130

131131
Example::
132132

133133
hashin --dry-run requests==2.19.1
134134

135135
Would result in a printout on the command line::
136136

137-
requests==2.19.1 \
138-
--hash=sha256:63b52e3c866428a224f97cab011de738c36aec0185aa91cfacd418b5d58911d1 \
139-
--hash=sha256:ec22d826a36ed72a7358ff3fe56cbd4ba69dd7a6718ffd450ff0e9df7a47ce6a
140-
141-
137+
--- Old
138+
+++ New
139+
@@ -0,0 +1,3 @@
140+
+requests==2.19.1 \
141+
+ --hash=sha256:63b52e3c866428a224f97cab011de738c36aec0185aa91cfacd418b5d58911d1 \
142+
+ --hash=sha256:ec22d826a36ed72a7358ff3fe56cbd4ba69dd7a6718ffd450ff0e9df7a47ce6a
142143

143144
PEP-0496 Environment Markers
144145
============================

hashin.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import pip_api
1717
from packaging.version import parse
18+
import difflib
1819

1920
if sys.version_info >= (3,):
2021
from urllib.request import urlopen
@@ -77,7 +78,7 @@
7778
)
7879
parser.add_argument(
7980
'--dry-run',
80-
help='Don\'t touch requirements.txt and just show hashes',
81+
help="Don't touch requirements.txt and just show the diff",
8182
action='store_true',
8283
default=False,
8384
)
@@ -181,22 +182,31 @@ def run_single_package(
181182
new_lines += ' \\'
182183
new_lines += '\n'
183184

184-
if dry_run:
185-
if verbose:
186-
_verbose('Dry run, not editing ', file)
187-
print(new_lines)
188-
return
189-
if verbose:
190-
_verbose('Editing', file)
191185
with open(file) as f:
192-
requirements = f.read()
186+
old_requirements = f.read()
193187
requirements = amend_requirements_content(
194-
requirements,
188+
old_requirements,
195189
package,
196190
new_lines
197191
)
198-
with open(file, 'w') as f:
199-
f.write(requirements)
192+
if dry_run:
193+
if verbose:
194+
_verbose('Dry run, not editing ', file)
195+
print(
196+
"".join(
197+
difflib.unified_diff(
198+
old_requirements.splitlines(True),
199+
requirements.splitlines(True),
200+
fromfile="Old",
201+
tofile="New",
202+
)
203+
)
204+
)
205+
else:
206+
with open(file, 'w') as f:
207+
f.write(requirements)
208+
if verbose:
209+
_verbose('Editing', file)
200210

201211

202212
def amend_requirements_content(requirements, package, new_lines):

tests/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,10 +745,10 @@ def mocked_get(url, **options):
745745
# Check dry run output
746746
out_lines = my_stdout.getvalue().splitlines()
747747
self.assertTrue(
748-
'hashin==0.10' in out_lines[0]
748+
'+hashin==0.10' in out_lines[3]
749749
)
750750
self.assertTrue(
751-
'--hash=sha256:aaaaa' in out_lines[1]
751+
'+--hash=sha256:aaaaa' in out_lines[4].replace(" ", "")
752752
)
753753

754754
@cleanup_tmpdir('hashin*')

0 commit comments

Comments
 (0)