From d56d5feb299f1aad02a6642d86fa3e3b424bd5eb Mon Sep 17 00:00:00 2001 From: mateuslatrova Date: Mon, 18 Sep 2023 17:38:23 -0300 Subject: [PATCH 1/3] delete unused function "filter_line" --- pipreqs/pipreqs.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pipreqs/pipreqs.py b/pipreqs/pipreqs.py index 8182115..a0e9bee 100644 --- a/pipreqs/pipreqs.py +++ b/pipreqs/pipreqs.py @@ -149,10 +149,6 @@ def get_all_imports( return list(packages - data) -def filter_line(line): - return len(line) > 0 and line[0] != "#" - - def generate_requirements_file(path, imports, symbol): with _open(path, "w") as out_file: logging.debug('Writing {num} requirements: {imports} to {file}'.format( From 6ac4357cf451478bad1103bd2bbe516806ad1a5e Mon Sep 17 00:00:00 2001 From: mateuslatrova Date: Mon, 18 Sep 2023 17:38:34 -0300 Subject: [PATCH 2/3] fix return type on docs --- pipreqs/pipreqs.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pipreqs/pipreqs.py b/pipreqs/pipreqs.py index a0e9bee..a84f39b 100644 --- a/pipreqs/pipreqs.py +++ b/pipreqs/pipreqs.py @@ -315,7 +315,7 @@ def parse_requirements(file_): OSerror: If there's any issues accessing the file. Returns: - tuple: The contents of the file, excluding comments. + list: The contents of the file, excluding comments. """ modules = [] # For the dependency identifier specification, see @@ -353,7 +353,6 @@ def parse_requirements(file_): return modules - def compare_modules(file_, imports): """Compare modules in a file to imported modules in a project. @@ -362,8 +361,8 @@ def compare_modules(file_, imports): imports (tuple): Modules being imported in the project. Returns: - tuple: The modules not imported in the project, but do exist in the - specified file. + set: The modules not imported in the project, but do exist in the + specified file. """ modules = parse_requirements(file_) From ed46d270e9fb4050612a6f3cb9ba53875626453e Mon Sep 17 00:00:00 2001 From: Mateus Latrova Date: Thu, 28 Sep 2023 20:28:32 -0300 Subject: [PATCH 3/3] add test for "compare_modules" function --- tests/_data/empty.txt | 0 tests/_data/imports.txt | 3 +++ tests/test_pipreqs.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 tests/_data/empty.txt create mode 100644 tests/_data/imports.txt diff --git a/tests/_data/empty.txt b/tests/_data/empty.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/imports.txt b/tests/_data/imports.txt new file mode 100644 index 0000000..32eea8a --- /dev/null +++ b/tests/_data/imports.txt @@ -0,0 +1,3 @@ +pandas==2.0.0 +numpy>=1.2.3 +torch<4.0.0 \ No newline at end of file diff --git a/tests/test_pipreqs.py b/tests/test_pipreqs.py index 039d4fd..6d90b6e 100644 --- a/tests/test_pipreqs.py +++ b/tests/test_pipreqs.py @@ -39,6 +39,13 @@ def setUp(self): self.project = os.path.join(os.path.dirname(__file__), "_data") self.project_clean = os.path.join(os.path.dirname(__file__), "_data_clean") self.project_invalid = os.path.join(os.path.dirname(__file__), "_invalid_data") + self.parsed_packages = [ + {"name": "pandas", "version": "2.0.0"}, + {"name": "numpy", "version": "1.2.3"}, + {"name": "torch", "version": "4.0.0"}, + ] + self.empty_filepath = os.path.join(self.project, "empty.txt") + self.imports_filepath = os.path.join(self.project, "imports.txt") self.project_with_ignore_directory = os.path.join( os.path.dirname(__file__), "_data_ignore" ) @@ -427,6 +434,31 @@ def test_clean_with_imports_to_clean(self): data = f.read().lower() self.assertTrue(cleaned_module not in data) + def test_compare_modules(self): + test_cases = [ + (self.empty_filepath, [], set()), # both empty + (self.empty_filepath, self.parsed_packages, set()), # only file empty + ( + self.imports_filepath, + [], + set(package["name"] for package in self.parsed_packages), + ), # only imports empty + (self.imports_filepath, self.parsed_packages, set()), # no difference + ( + self.imports_filepath, + self.parsed_packages[1:], + set([self.parsed_packages[0]["name"]]), + ), # common case + ] + + for test_case in test_cases: + with self.subTest(test_case): + filename, imports, expected_modules_not_imported = test_case + + modules_not_imported = pipreqs.compare_modules(filename, imports) + + self.assertSetEqual(modules_not_imported, expected_modules_not_imported) + def tearDown(self): """ Remove requiremnts.txt files that were written