def test_venv_internal_tracked(appvenv_int):
    # Clean bill of health
    result = invoke(args=[
        appvenv_int.app_venv_deplint,
        'tracked',
    ],
                    cwd=appvenv_int.app_rootdir,
                    noisy=True)

    assert result.exit_code == 0
    assert result.stdout == ''
    assert result.stderr == ''

    # Install something that is not required
    appvenv_int.install_pypi_package('pytest==3.2.2')

    # Report unnecessary installed package
    result = invoke(args=[
        appvenv_int.app_venv_deplint,
        'tracked',
    ],
                    cwd=appvenv_int.app_rootdir,
                    noisy=True)

    assert result.exit_code == 1
    assert result.stdout.strip() == (
        "[IsTransitiveDep] warn: "
        "Installed non-transitive dependency 'pytest-3.2.2' is not required")
    assert result.stderr == ''
def test_venv_external_tracked(appvenv_ext):
    # Clean bill of health
    result = invoke(
        args=[
            'bin/deplint', 'tracked',
            '-r', appvenv_ext.app_requirements_txt,
            '--python', appvenv_ext.app_venv_python,
        ],
        noisy=True
    )

    assert result.exit_code == 0
    assert result.stdout == ''
    assert result.stderr == ''

    # Install something that is not required
    appvenv_ext.install_deplint()

    # Report unnecessary installed package
    result = invoke(
        args=[
            'bin/deplint', 'tracked',
            '-r', appvenv_ext.app_requirements_txt,
            '--python', appvenv_ext.app_venv_python,
        ],
        noisy=True
    )

    assert result.exit_code == 1
    assert result.stdout.strip() == (
        "[IsTransitiveDep] warn: "
        "Installed non-transitive dependency 'deplint-%s' is not required"
    ) % (deplint.__version__,)
    assert result.stderr == ''
Exemple #3
0
    def package_is_imported(self, package_name):
        # Using git grep is a bit dubious because git grep only matches content
        # in the git index/history - not content in files on disk not yet
        # tracked by git. Still, as a grep it's extremely widely available.
        # A more robust method would be to have a fallback on another grep as
        # plan B.
        args = [
            'git',
            'grep',
            '-E',
            ('^\s*'
             '(' + 'import\s+' + package_name + '|from\s+' + package_name +
             ')'),
        ]

        result = invoke(args, cwd=self.basedir)

        # code == 0 => found
        if result.exit_code == 0:
            return True

        # code 1 and no output => not found
        if result.exit_code == 1 and not any((result.stdout, result.stderr)):
            return False

        # code != 0 and output => error
        raise RuntimeError('git grep failed: %s' % result.stderr)
def test_venv_internal_app_works(appvenv_int):
    result = invoke(args=[
        appvenv_int.app_venv_python,
        'app.py',
    ],
                    cwd=appvenv_int.app_rootdir,
                    noisy=True)

    assert result.exit_code == 0
    assert 'Hello' in result.stdout
    assert 'Traceback' not in result.stderr
def test_venv_internal_vulnerable(appvenv_int):
    # Report requirement that is never used
    result = invoke(args=[
        appvenv_int.app_venv_deplint,
        'vulnerable',
    ],
                    cwd=appvenv_int.app_rootdir,
                    noisy=True)

    assert result.exit_code == 0
    assert result.stdout == ''
    assert result.stderr == ''
Exemple #6
0
def dummy_project(pkg, variant, git_init=True):
    direc = tempfile.mkdtemp(prefix='.deplint.tests-')

    # top level imports
    if variant == 'import-x':
        code = 'import %s' % pkg
    elif variant == 'import-x.y':
        code = 'import %s.hammer' % pkg

    elif variant == 'from-x-import-y':
        code = 'from %s import hammer' % pkg
    elif variant == 'from-x.y-import-z':
        code = 'from %s.hammer import rabbit' % pkg

    # nested imports
    elif variant == 'import-y.x':
        code = 'import hammer.%s' % pkg

    elif variant == 'from-y.x-import-z':
        code = 'from hammer.%s import rabbit' % pkg

    elif variant == 'from-y.z-import-x':
        code = 'from hammer.rabbit import %s' % pkg

    fd, file = tempfile.mkstemp(dir=direc, suffix='.py')
    os.write(fd, b'%s\n' % code.encode('utf-8'))
    os.close(fd)

    # create a git repo there and add the file - otherwise git grep won't work
    git_result = invoke(['git', 'init'], cwd=direc)
    assert git_result.exit_code == 0
    git_result = invoke(['git', 'add', '.'], cwd=direc)
    assert git_result.exit_code == 0

    try:
        yield direc

    finally:
        if os.path.exists(direc):
            shutil.rmtree(direc)
Exemple #7
0
def test_run_action_vulnerable():
    result = invoke([
        'bin/deplint',
        'vulnerable',
        '-r',
        'requirements.txt',
        '--python',
        sys.executable,
        '-v',
    ],
                    noisy=True)

    assert result.exit_code == 0
    assert 'Traceback' not in result.stderr
def test_venv_external_vulnerable(appvenv_ext):
    # Clean bill of health
    result = invoke(
        args=[
            'bin/deplint', 'vulnerable',
            '-r', appvenv_ext.app_requirements_txt,
            '--python', appvenv_ext.app_venv_python,
        ],
        noisy=True
    )

    assert result.exit_code == 0
    assert result.stdout == ''
    assert result.stderr == ''
Exemple #9
0
def test_invoke_noisy(capsys):
    result = invoke(['echo', 'hello'], cwd='/tmp', noisy=True)

    assert result.exit_code == 0
    assert result.stdout == 'hello\n'
    assert result.stderr == ''

    stdout, stderr = capsys.readouterr()
    assert stdout.strip() == '\n'.join([
        '{invoke} [cwd: /tmp] Invoking: echo hello',
        '{invoke} Returned: exit_code 0',
        '{invoke} stdout: hello\n',
        '{invoke} stderr:',
    ])
    assert stderr == ''
Exemple #10
0
def test_run_action_tracked():
    result = invoke([
        'bin/deplint',
        'tracked',
        '-r',
        'requirements.txt',
        '--python',
        sys.executable,
        '-v',
    ],
                    noisy=True)

    # warning about dev dependencies being installed
    assert result.exit_code == 1
    assert 'Traceback' not in result.stderr
def test_venv_external_unused(appvenv_ext):
    # Clean bill of health
    result = invoke(
        args=[
            'bin/deplint', 'unused',
            '-r', appvenv_ext.app_requirements_txt,
            '--python', appvenv_ext.app_venv_python,
        ],
        noisy=True
    )

    assert result.exit_code == 0
    assert result.stdout == ''
    assert result.stderr == ''

    # Install and require something that is not used
    appvenv_ext.install_deplint()
    appvenv_ext.inject_installed_deplint_into_reqs()

    # Report requirement that is never used
    result = invoke(
        args=[
            'bin/deplint', 'unused',
            '-r', appvenv_ext.app_requirements_txt,
            '--python', appvenv_ext.app_venv_python,
        ],
        noisy=True
    )

    assert result.exit_code == 0
    assert result.stdout.strip() == (
        "[IsUnused] info: "
        "Required dependency 'deplint==%s' "
        "is never imported (deplint)"
    ) % (deplint.__version__,)
    assert result.stderr == ''
Exemple #12
0
def test_venv_internal_unused(appvenv_int):
    # Report requirement that is never used
    result = invoke(args=[
        appvenv_int.app_venv_deplint,
        'unused',
    ],
                    cwd=appvenv_int.app_rootdir,
                    noisy=True)

    assert result.exit_code == 0
    assert result.stdout.strip() == ("[IsUnused] info: "
                                     "Required dependency 'deplint==%s' "
                                     "is never imported (deplint)") % (
                                         deplint.__version__, )
    assert result.stderr == ''
def test_venv_external_installed(appvenv_ext):
    result = invoke(
        args=[
            'bin/deplint', 'installed',
            '-r', appvenv_ext.app_requirements_txt,
            '--python', appvenv_ext.app_venv_python,
        ],
        noisy=True
    )

    assert result.exit_code == 0
    assert result.stdout.strip() == (
        "[RequiredInstalled] debug: "
        "Required dependency 'ansicolor==0.2.4' "
        "is satisfied by installed 'ansicolor-0.2.4'"
    )
    assert result.stderr == ''
Exemple #14
0
def test_venv_internal_installed(appvenv_int):
    result = invoke(args=[
        appvenv_int.app_venv_deplint,
        'installed',
    ],
                    cwd=appvenv_int.app_rootdir,
                    noisy=True)

    assert result.exit_code == 0
    assert result.stdout.strip() == '\n'.join([
        ("[RequiredInstalled] debug: "
         "Required dependency 'ansicolor==0.2.4' "
         "is satisfied by installed 'ansicolor-0.2.4'"),
        ("[RequiredInstalled] debug: "
         "Required dependency 'deplint==%s' "
         "is satisfied by installed 'deplint-%s'") %
        (deplint.__version__, deplint.__version__),
    ])
    assert result.stderr == ''
Exemple #15
0
def test_invoke():
    result = invoke(['echo', 'hello'])

    assert result.exit_code == 0
    assert result.stdout == 'hello\n'
    assert result.stderr == ''
Exemple #16
0
def test_run_missing_action():
    result = invoke(['bin/deplint'], noisy=True)

    assert result.exit_code == 2
    assert 'Traceback' not in result.stderr