From 1384a25a38d54ce3041fda297e295d5d07fd932f Mon Sep 17 00:00:00 2001 From: frostming Date: Tue, 14 May 2019 09:54:18 +0800 Subject: [PATCH 1/5] make check unused work --- pipenv/core.py | 6 +++--- tests/integration/test_cli.py | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pipenv/core.py b/pipenv/core.py index 76ce7e8b47..9e190343fb 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -242,7 +242,7 @@ def import_from_code(path="."): rs = [] try: - for r in pipreqs.get_all_imports(path): + for r in pipreqs.get_all_imports(path, encoding="utf-8"): if r not in BAD_PACKAGES: rs.append(r) pkg_names = pipreqs.get_pkg_names(rs) @@ -2534,8 +2534,8 @@ def do_check( if not args: args = [] if unused: - deps_required = [k for k in project.packages.keys()] - deps_needed = import_from_code(unused) + deps_required = [k.lower() for k in project.packages.keys()] + deps_needed = [k.lower() for k in import_from_code(unused)] for dep in deps_needed: try: deps_required.remove(dep) diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.py index a38883e0ed..ae1b13dde5 100644 --- a/tests/integration/test_cli.py +++ b/tests/integration/test_cli.py @@ -47,7 +47,7 @@ def test_pipenv_site_packages(PipenvInstance): c = p.pipenv('--python python --site-packages') assert c.return_code == 0 assert 'Making site-packages available' in c.err - + # no-global-site-packages.txt under stdlib dir should not exist. c = p.pipenv('run python -c "import sysconfig; print(sysconfig.get_path(\'stdlib\'))"') assert c.return_code == 0 @@ -215,20 +215,21 @@ def test_install_parse_error(PipenvInstance, pypi): @pytest.mark.code @pytest.mark.check @pytest.mark.unused -@pytest.mark.skip(reason="non-deterministic") def test_check_unused(PipenvInstance, pypi): with PipenvInstance(chdir=True, pypi=pypi) as p: with open('__init__.py', 'w') as f: contents = """ import tablib import records +import flask """.strip() f.write(contents) p.pipenv('install requests') p.pipenv('install tablib') - p.pipenv('install records') + p.pipenv('install flask') - assert all(pkg in p.pipfile['packages'] for pkg in ['requests', 'tablib', 'records']) + assert all(pkg in p.pipfile['packages'] for pkg in ['requests', 'tablib', 'flask']) c = p.pipenv('check --unused .') assert 'tablib' not in c.out + assert 'flask' not in c.out From e6b2f6463a905268641158564a23ccef0251586e Mon Sep 17 00:00:00 2001 From: frostming Date: Mon, 20 May 2019 12:45:09 +0800 Subject: [PATCH 2/5] add news entry --- news/3745.bugfix.rst | 1 + tests/integration/test_cli.py | 1 + 2 files changed, 2 insertions(+) create mode 100644 news/3745.bugfix.rst diff --git a/news/3745.bugfix.rst b/news/3745.bugfix.rst new file mode 100644 index 0000000000..229047a40d --- /dev/null +++ b/news/3745.bugfix.rst @@ -0,0 +1 @@ +Normalize the package names to lowercase when comparing used and in-Pipfile packages. diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.py index 0af5bedb8e..27314b7354 100644 --- a/tests/integration/test_cli.py +++ b/tests/integration/test_cli.py @@ -217,6 +217,7 @@ def test_install_parse_error(PipenvInstance, pypi): @pytest.mark.code @pytest.mark.check @pytest.mark.unused +@pytest.mark.needs_internet(reason='required by check') def test_check_unused(PipenvInstance, pypi): with PipenvInstance(chdir=True, pypi=pypi) as p: with open('__init__.py', 'w') as f: From 2dce8355e6ae3df727a35ddf95198de1bcb0a2b1 Mon Sep 17 00:00:00 2001 From: frostming Date: Mon, 20 May 2019 14:28:02 +0800 Subject: [PATCH 3/5] let's see what's happening --- pipenv/core.py | 3 +++ pipenv/vendor/pipreqs/pipreqs.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/pipenv/core.py b/pipenv/core.py index 3e62b19ca6..f90c60d1a0 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -243,12 +243,15 @@ def import_from_code(path="."): rs = [] try: for r in pipreqs.get_all_imports(path, encoding="utf-8"): + click.echo(r) if r not in BAD_PACKAGES: rs.append(r) pkg_names = pipreqs.get_pkg_names(rs) return [proper_case(r) for r in pkg_names] except Exception: + import traceback + traceback.print_exc() return [] diff --git a/pipenv/vendor/pipreqs/pipreqs.py b/pipenv/vendor/pipreqs/pipreqs.py index 791168a99d..c0466adf52 100644 --- a/pipenv/vendor/pipreqs/pipreqs.py +++ b/pipenv/vendor/pipreqs/pipreqs.py @@ -68,7 +68,7 @@ def get_all_imports(path, encoding=None, extra_ignore_dirs=None): candidates.append(os.path.basename(root)) files = [fn for fn in files if os.path.splitext(fn)[1] == ".py"] - + print(root, files) candidates += [os.path.splitext(fn)[0] for fn in files] for file_name in files: with open_func(os.path.join(root, file_name), "r", encoding=encoding) as f: From 457a42a69c990f8174c9c0848ed2f1a2b1a2b429 Mon Sep 17 00:00:00 2001 From: frostming Date: Mon, 20 May 2019 16:04:58 +0800 Subject: [PATCH 4/5] exclude venv folders --- tests/integration/test_cli.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.py index 27314b7354..ae5db17638 100644 --- a/tests/integration/test_cli.py +++ b/tests/integration/test_cli.py @@ -220,7 +220,8 @@ def test_install_parse_error(PipenvInstance, pypi): @pytest.mark.needs_internet(reason='required by check') def test_check_unused(PipenvInstance, pypi): with PipenvInstance(chdir=True, pypi=pypi) as p: - with open('__init__.py', 'w') as f: + os.makedirs('mypackage') + with open('mypackage/__init__.py', 'w') as f: contents = """ import tablib import records @@ -233,6 +234,6 @@ def test_check_unused(PipenvInstance, pypi): assert all(pkg in p.pipfile['packages'] for pkg in ['requests', 'tablib', 'flask']) - c = p.pipenv('check --unused .') + c = p.pipenv('check --unused mypackage') assert 'tablib' not in c.out assert 'flask' not in c.out From bc37beaef43de351a1a215bea1c6fad678774233 Mon Sep 17 00:00:00 2001 From: frostming Date: Mon, 20 May 2019 16:42:18 +0800 Subject: [PATCH 5/5] cleanup --- pipenv/core.py | 7 +++---- pipenv/vendor/pipreqs/pipreqs.py | 2 +- tests/integration/test_cli.py | 9 +++------ 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/pipenv/core.py b/pipenv/core.py index f90c60d1a0..7f04ac4999 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -242,16 +242,15 @@ def import_from_code(path="."): rs = [] try: - for r in pipreqs.get_all_imports(path, encoding="utf-8"): - click.echo(r) + for r in pipreqs.get_all_imports( + path, encoding="utf-8", extra_ignore_dirs=[".venv"] + ): if r not in BAD_PACKAGES: rs.append(r) pkg_names = pipreqs.get_pkg_names(rs) return [proper_case(r) for r in pkg_names] except Exception: - import traceback - traceback.print_exc() return [] diff --git a/pipenv/vendor/pipreqs/pipreqs.py b/pipenv/vendor/pipreqs/pipreqs.py index c0466adf52..791168a99d 100644 --- a/pipenv/vendor/pipreqs/pipreqs.py +++ b/pipenv/vendor/pipreqs/pipreqs.py @@ -68,7 +68,7 @@ def get_all_imports(path, encoding=None, extra_ignore_dirs=None): candidates.append(os.path.basename(root)) files = [fn for fn in files if os.path.splitext(fn)[1] == ".py"] - print(root, files) + candidates += [os.path.splitext(fn)[0] for fn in files] for file_name in files: with open_func(os.path.join(root, file_name), "r", encoding=encoding) as f: diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.py index ae5db17638..f131ae4440 100644 --- a/tests/integration/test_cli.py +++ b/tests/integration/test_cli.py @@ -220,20 +220,17 @@ def test_install_parse_error(PipenvInstance, pypi): @pytest.mark.needs_internet(reason='required by check') def test_check_unused(PipenvInstance, pypi): with PipenvInstance(chdir=True, pypi=pypi) as p: - os.makedirs('mypackage') - with open('mypackage/__init__.py', 'w') as f: + with open('__init__.py', 'w') as f: contents = """ import tablib import records import flask """.strip() f.write(contents) - p.pipenv('install requests') - p.pipenv('install tablib') - p.pipenv('install flask') + p.pipenv('install requests tablib flask') assert all(pkg in p.pipfile['packages'] for pkg in ['requests', 'tablib', 'flask']) - c = p.pipenv('check --unused mypackage') + c = p.pipenv('check --unused .') assert 'tablib' not in c.out assert 'flask' not in c.out