Skip to content

Commit

Permalink
use the correct sep. chars between req and comment, closes #183
Browse files Browse the repository at this point in the history
  • Loading branch information
jayfk committed Jan 19, 2017
1 parent 6c5bcc2 commit 8390df4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
13 changes: 12 additions & 1 deletion pyup/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,18 @@ def update_content(self, content, update_hashes=True):
new_line += ";" + self.line.split(";", 1)[1].split("#")[0].rstrip()
# add the comment
if "#" in self.line:
new_line += " #" + "#".join(self.line.splitlines()[0].split("#")[1:])
# split the line into parts: requirement and comment
parts = self.line.splitlines()[0].split("#")
requirement, comment = parts[0], "#".join(parts[1:])
# find all whitespaces between the requirement and the comment
whitespaces = (hex(ord('\t')), hex(ord(' ')))
trailing_whitespace = ''
for c in requirement[::-1]:
if hex(ord(c)) in whitespaces:
trailing_whitespace += c
else:
break
new_line += trailing_whitespace + "#" + comment
# if this is a hashed requirement, add a multiline break before the comment
if self.hashes and not new_line.endswith("\\"):
new_line += " \\"
Expand Down
19 changes: 17 additions & 2 deletions tests/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@

class RequirementUpdateContent(TestCase):

def test_update_contains_correct_sep_char(self):

with patch('pyup.requirements.Requirement.latest_version_within_specs',
new_callable=PropertyMock,
return_value="2.9.5"):
content = "Jinja2==2.9.4 # via flask"
req_file = RequirementFile("req.txt", content)
req = list(req_file.requirements)[0]
self.assertEqual(
Requirement.parse("Jinja2==2.9.4 # via flask", 1),
req
)

self.assertEqual(req.update_content(content), "Jinja2==2.9.5 # via flask")

@patch("pyup.requirements.hashin.get_package_hashes")
def test_update_with_hashes(self, get_hashes_mock):
get_hashes_mock.return_value = {
Expand Down Expand Up @@ -111,12 +126,12 @@ def test_update_content_tabbed(self):
content = "bla==1.4.1\t\t# some foo"
req = Requirement.parse(content, 0)

self.assertEqual(req.update_content(content), "bla==1.4.2 # some foo")
self.assertEqual(req.update_content(content), "bla==1.4.2\t\t# some foo")

content = "bla==1.4.1\t\t# pyup: <1.4.2"
req = Requirement.parse(content, 0)

self.assertEqual(req.update_content(content), "bla==1.4.2 # pyup: <1.4.2")
self.assertEqual(req.update_content(content), "bla==1.4.2\t\t# pyup: <1.4.2")

def test_something_else(self):
with patch('pyup.requirements.Requirement.latest_version', new_callable=PropertyMock,
Expand Down

0 comments on commit 8390df4

Please sign in to comment.