From 12cc1e5b74a6469876a32d0105913010cb51b868 Mon Sep 17 00:00:00 2001 From: fernandocrz Date: Thu, 21 Sep 2023 16:24:48 -0300 Subject: [PATCH] add test for parse_requirements function --- tests/_data/empty.txt | 0 tests/_data/imports.txt | 3 ++ tests/_data/imports_any_version.txt | 4 ++ tests/_data/imports_no_version.txt | 3 ++ tests/test_pipreqs.py | 82 ++++++++++++++++++++--------- 5 files changed, 68 insertions(+), 24 deletions(-) create mode 100644 tests/_data/empty.txt create mode 100644 tests/_data/imports.txt create mode 100644 tests/_data/imports_any_version.txt create mode 100644 tests/_data/imports_no_version.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..7180017 --- /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/_data/imports_any_version.txt b/tests/_data/imports_any_version.txt new file mode 100644 index 0000000..575ff64 --- /dev/null +++ b/tests/_data/imports_any_version.txt @@ -0,0 +1,4 @@ +numpy +pandas==2.0.0 +tensorflow +torch<4.0.0 \ No newline at end of file diff --git a/tests/_data/imports_no_version.txt b/tests/_data/imports_no_version.txt new file mode 100644 index 0000000..571d829 --- /dev/null +++ b/tests/_data/imports_no_version.txt @@ -0,0 +1,3 @@ +pandas +tensorflow +torch \ No newline at end of file diff --git a/tests/test_pipreqs.py b/tests/test_pipreqs.py index 7cb347d..520766b 100644 --- a/tests/test_pipreqs.py +++ b/tests/test_pipreqs.py @@ -39,14 +39,35 @@ def setUp(self): self.modules2 = ["beautifulsoup4"] self.local = ["docopt", "requests", "nose", "pyflakes"] self.project = os.path.join(os.path.dirname(__file__), "_data") + self.empty_filepath = os.path.join(self.project, "empty.txt") + self.imports_filepath = os.path.join(self.project, "imports.txt") + self.imports_no_version_filepath = os.path.join(self.project, "imports_no_version.txt") + self.imports_any_version_filepath = os.path.join(self.project, "imports_any_version.txt") + self.non_existent_filepath = os.path.join(self.project, "non_existent_file.txt") + + self.parsed_packages = [ + {"name": "pandas", "version": "2.0.0"}, + {"name": "numpy", "version": "1.2.3"}, + {"name": "torch", "version": "4.0.0"}, + ] + + self.parsed_packages_no_version = [ + {"name": "pandas", "version": None}, + {"name": "tensorflow", "version": None}, + {"name": "torch", "version": None}, + ] + + self.parsed_packages_any_version = [ + {"name": "numpy", "version": None}, + {"name": "pandas", "version": "2.0.0"}, + {"name": "tensorflow", "version": None}, + {"name": "torch", "version": "4.0.0"}, + ] + 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.project_with_ignore_directory = os.path.join( - os.path.dirname(__file__), "_data_ignore" - ) - self.project_with_duplicated_deps = os.path.join( - os.path.dirname(__file__), "_data_duplicated_deps" - ) + self.project_with_ignore_directory = os.path.join(os.path.dirname(__file__), "_data_ignore") + self.project_with_duplicated_deps = os.path.join(os.path.dirname(__file__), "_data_duplicated_deps") self.requirements_path = os.path.join(self.project, "requirements.txt") self.alt_requirement_path = os.path.join(self.project, "requirements2.txt") @@ -223,12 +244,8 @@ def test_get_import_name_without_alias(self): """ import_name_with_alias = "requests as R" expected_import_name_without_alias = "requests" - import_name_without_aliases = pipreqs.get_name_without_alias( - import_name_with_alias - ) - self.assertEqual( - import_name_without_aliases, expected_import_name_without_alias - ) + import_name_without_aliases = pipreqs.get_name_without_alias(import_name_with_alias) + self.assertEqual(import_name_without_aliases, expected_import_name_without_alias) def test_custom_pypi_server(self): """ @@ -267,9 +284,7 @@ def test_ignored_directory(self): "--mode": None, } ) - with open( - os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r" - ) as f: + with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f: data = f.read().lower() for item in ["click", "getpass"]: self.assertFalse(item.lower() in data) @@ -292,9 +307,7 @@ def test_dynamic_version_no_pin_scheme(self): "--mode": "no-pin", } ) - with open( - os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r" - ) as f: + with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f: data = f.read().lower() for item in ["beautifulsoup4", "boto"]: self.assertTrue(item.lower() in data) @@ -317,9 +330,7 @@ def test_dynamic_version_gt_scheme(self): "--mode": "gt", } ) - with open( - os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r" - ) as f: + with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f: data = f.readlines() for item in data: symbol = ">=" @@ -344,9 +355,7 @@ def test_dynamic_version_compat_scheme(self): "--mode": "compat", } ) - with open( - os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r" - ) as f: + with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f: data = f.readlines() for item in data: symbol = "~=" @@ -472,6 +481,31 @@ def test_output_requirements(self): stdout_content = capturedOutput.getvalue().lower() self.assertTrue(file_content == stdout_content) + def test_parse_requirements(self): + """ + Test parse_requirements function + """ + test_cases = [ + (self.empty_filepath, []), # empty file + (self.imports_filepath, self.parsed_packages), # imports with versions + ( + self.imports_no_version_filepath, + self.parsed_packages_no_version, + ), # imports without versions + ( + self.imports_any_version_filepath, + self.parsed_packages_any_version, + ), # imports with and without versions + ] + + for test in test_cases: + with self.subTest(test): + filename, expected_parsed_requirements = test + + parsed_requirements = pipreqs.parse_requirements(filename) + + self.assertListEqual(parsed_requirements, expected_parsed_requirements) + def tearDown(self): """ Remove requiremnts.txt files that were written