Skip to content

Commit

Permalink
handle FileNotFoundError in parse_requirements function
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateus Latrova committed Oct 2, 2023
1 parent cebc3f7 commit d129e2d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pipreqs/pipreqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ def parse_requirements(file_):
delimiter, get module name by element index, create a dict consisting of
module:version, and add dict to list of parsed modules.
If file ´file_´ is not found in the system, the program will print a

Check warning on line 315 in pipreqs/pipreqs.py

View workflow job for this annotation

GitHub Actions / Lint

[flake8] reported by reviewdog 🐶 trailing whitespace Raw Output: ./pipreqs/pipreqs.py:315:73: W291 trailing whitespace
helpful message and end its execution immediately.
Args:
file_: File to parse.
Expand All @@ -328,9 +331,12 @@ def parse_requirements(file_):

try:
f = open(file_, "r")
except OSError:
logging.error("Failed on file: {}".format(file_))
raise
except FileNotFoundError:
print(f"File {file_} was not found. Please, fix it and run again.")
sys.exit(1)
except OSError as error:
logging.error(f"There was an error opening the file {file_}: {str(error)}")
raise error
else:
try:
data = [x.strip() for x in f.readlines() if x != "\n"]
Expand Down
23 changes: 23 additions & 0 deletions tests/test_pipreqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
Tests for `pipreqs` module.
"""

from io import StringIO
from unittest.mock import patch
import unittest
import os
import requests
import sys

from pipreqs import pipreqs

Expand Down Expand Up @@ -47,6 +50,7 @@ def setUp(self):
)
self.requirements_path = os.path.join(self.project, "requirements.txt")
self.alt_requirement_path = os.path.join(self.project, "requirements2.txt")
self.non_existing_filepath = "xpto"

def test_get_all_imports(self):
imports = pipreqs.get_all_imports(self.project)
Expand Down Expand Up @@ -427,6 +431,25 @@ def test_clean_with_imports_to_clean(self):
data = f.read().lower()
self.assertTrue(cleaned_module not in data)

@patch("sys.exit")
def test_parse_requirements_handles_file_not_found(self, exit_mock):
captured_output = StringIO()
sys.stdout = captured_output

# This assertion is needed, because since "sys.exit" is mocked, the program won't end,
# and the code that is after the except block will be run
with self.assertRaises(UnboundLocalError):
pipreqs.parse_requirements(self.non_existing_filepath)

exit_mock.assert_called_once_with(1)

printed_text = captured_output.getvalue().strip()
sys.stdout = sys.__stdout__

self.assertEqual(
printed_text, "File xpto was not found. Please, fix it and run again."
)

def tearDown(self):
"""
Remove requiremnts.txt files that were written
Expand Down

0 comments on commit d129e2d

Please sign in to comment.