def test_pip_error(mock_system, mock_virtualenv, monkeypatch):
    monkeypatch.setattr(
        subprocess, 'Popen', lambda *args, **kwargs: PopenStub(1))

    with pytest.raises(SystemExit):
        requirements.package(
            ['/path1/requirements.txt'], '/tmp')
def test_package_existing_target_file(mock_system, mock_virtualenv,
                                      monkeypatch):
    monkeypatch.setattr(os.path, 'isdir', lambda f: False)

    with pytest.raises(SystemExit):
        requirements.package(
            ['/path1/requirements.txt'], '/tmp')
def test_package_missing_deps(mock_system, mock_virtualenv, monkeypatch):
    monkeypatch.setattr(
        os.path, "isdir", lambda f: f != "/tmp/.venv/lib/dir1/site-packages"
    )

    with pytest.raises(SystemExit):
        requirements.package(["/path1/requirements.txt"], "/tmp")
Exemple #4
0
def test_package_windows(mock_system, mock_virtualenv, monkeypatch):
    monkeypatch.setattr(platform, 'system', lambda: 'Windows')

    requirements.package(['/path1/requirements.txt'], '/tmp')

    pip_calls = [c for c in mock_system if c[0] == 'subprocess.Popen']

    assert pip_calls[0] == ('subprocess.Popen', ([
        '/tmp/.venv/Scripts/pip.exe', 'install', '-r',
        '/path1/requirements.txt'
    ], ))
def test_package_windows(mock_system, mock_virtualenv, monkeypatch):
    monkeypatch.setattr(platform, "system", lambda: "Windows")

    requirements.package(["/path1/requirements.txt"], "/tmp")

    pip_calls = [c for c in mock_system if c[0] == "subprocess.Popen"]

    assert pip_calls[0] == (
        "subprocess.Popen",
        (["/tmp/.venv/Scripts/pip.exe", "install", "-r", "/path1/requirements.txt"],),
    )
def test_package_with_pip_args(mock_system, mock_virtualenv):
    requirements.package(
        ["/path1/requirements.txt"],
        "/tmp",
        "--no-deps --imaginary-arg 'imaginary \"value\"'",
    )

    # Invokes pip for package installation
    assert mock_system[15] == (
        "subprocess.Popen",
        ([
            "/tmp/.venv/bin/pip",
            "install",
            "-r",
            "/path1/requirements.txt",
            "--no-deps",
            "--imaginary-arg",
            'imaginary "value"',
        ], ),
    )
def test_package(mock_system, mock_virtualenv):
    requirements.package(
        ["/path1/requirements.txt", "/path2/requirements.txt"], "/tmp")

    assert len(mock_virtualenv) == 1
    assert mock_virtualenv[0] == [
        "", "/tmp/.venv", "--quiet", "-p", sys.executable
    ]

    # Checks that requirements files exist
    assert mock_system.pop(0) == ("os.path.isfile",
                                  ("/path1/requirements.txt", ))
    assert mock_system.pop(0) == ("os.path.isfile",
                                  ("/path2/requirements.txt", ))

    # Checks that output dir exists
    assert mock_system.pop(0) == ("os.path.exists", ("/tmp", ))
    assert mock_system.pop(0) == ("os.path.isdir", ("/tmp", ))
    assert mock_system.pop(0) == ("shutil.rmtree", ("/tmp", ))
    assert mock_system.pop(0) == ("os.mkdir", ("/tmp", ))

    # Checks and removes existing venv/tmp dirs
    assert mock_system.pop(0) == ("os.path.exists", ("/tmp/.venv", ))
    assert mock_system.pop(0) == ("shutil.rmtree", ("/tmp/.venv", ))
    assert mock_system.pop(0) == ("os.path.exists", ("/tmp/.tmp", ))
    assert mock_system.pop(0) == ("shutil.rmtree", ("/tmp/.tmp", ))

    # Looks up system type
    assert mock_system.pop(0) == ("platform.system", ())

    # Looks for pip installation
    assert mock_system.pop(0) == ("os.listdir", ("/tmp/.venv/lib", ))

    assert mock_system.pop(0) == ("os.path.isdir", ("/tmp/.venv/lib/dir1", ))
    assert mock_system.pop(0) == ("os.path.isdir", ("/tmp/.venv/lib/dir2", ))

    assert mock_system.pop(0) == ("os.path.isfile", ("/tmp/.venv/bin/pip", ))

    # Invokes pip for package installation
    assert mock_system.pop(0) == (
        "subprocess.Popen",
        (["/tmp/.venv/bin/pip", "install", "-r", "/path1/requirements.txt"], ),
    )
    assert mock_system.pop(0) == (
        "subprocess.Popen",
        (["/tmp/.venv/bin/pip", "install", "-r", "/path2/requirements.txt"], ),
    )

    # Copies installed packages to temporary directory
    assert mock_system.pop(0) == (
        "os.path.isdir",
        ("/tmp/.venv/lib/dir1/site-packages", ),
    )
    assert mock_system.pop(0) == (
        "shutil.copytree",
        ("/tmp/.venv/lib/dir1/site-packages", "/tmp/.tmp"),
    )

    # Lists installed packages
    assert mock_system.pop(0) == ("os.listdir", ("/tmp/.tmp", ))

    # Clears existing installation of package 1
    assert mock_system.pop(0) == ("os.path.isdir", ("/tmp/dir1", ))
    assert mock_system.pop(0) == ("shutil.rmtree", ("/tmp/dir1", ))

    # Moves package 1 into place
    assert mock_system.pop(0) == ("shutil.move", ("/tmp/.tmp/dir1", "/tmp"))

    # Clears existing installation of package 2
    assert mock_system.pop(0) == ("os.path.isdir", ("/tmp/dir2", ))
    assert mock_system.pop(0) == ("shutil.rmtree", ("/tmp/dir2", ))

    # Moves package 2 into place
    assert mock_system.pop(0) == ("shutil.move", ("/tmp/.tmp/dir2", "/tmp"))

    # Performs final cleanup
    assert mock_system.pop(0) == ("shutil.rmtree", ("/tmp/.venv", ))
    assert mock_system.pop(0) == ("shutil.rmtree", ("/tmp/.tmp", ))
def test_package_missing_requirements_file(mock_system, mock_virtualenv,
                                           monkeypatch):
    monkeypatch.setattr(os.path, "isfile", lambda f: False)

    with pytest.raises(SystemExit):
        requirements.package(["/path1/requirements.txt"], "/tmp")
Exemple #9
0
def test_package(mock_system, mock_virtualenv):
    requirements.package(
        ['/path1/requirements.txt', '/path2/requirements.txt'], '/tmp')

    assert len(mock_virtualenv) == 1
    assert mock_virtualenv[0] == ['', '/tmp/.venv', '--quiet']

    # Checks that requirements files exist
    assert mock_system.pop(0) == ('os.path.isfile',
                                  ('/path1/requirements.txt', ))
    assert mock_system.pop(0) == ('os.path.isfile',
                                  ('/path2/requirements.txt', ))

    # Checks that output dir exists
    assert mock_system.pop(0) == ('os.path.exists', ('/tmp', ))
    assert mock_system.pop(0) == ('os.path.isdir', ('/tmp', ))

    # Checks and removes existing venv/tmp dirs
    assert mock_system.pop(0) == ('os.path.exists', ('/tmp/.venv', ))
    assert mock_system.pop(0) == ('shutil.rmtree', ('/tmp/.venv', ))
    assert mock_system.pop(0) == ('os.path.exists', ('/tmp/.tmp', ))
    assert mock_system.pop(0) == ('shutil.rmtree', ('/tmp/.tmp', ))

    # Looks up system type
    assert mock_system.pop(0) == ('platform.system', ())

    # Looks for pip installation
    assert mock_system.pop(0) == ('os.listdir', ('/tmp/.venv/lib', ))

    assert mock_system.pop(0) == ('os.path.isdir', ('/tmp/.venv/lib/dir1', ))
    assert mock_system.pop(0) == ('os.path.isdir', ('/tmp/.venv/lib/dir2', ))

    assert mock_system.pop(0) == ('os.path.isfile', ('/tmp/.venv/bin/pip', ))

    # Invokes pip for package installation
    assert mock_system.pop(0) == ('subprocess.Popen', ([
        '/tmp/.venv/bin/pip', 'install', '-r', '/path1/requirements.txt'
    ], ))
    assert mock_system.pop(0) == ('subprocess.Popen', ([
        '/tmp/.venv/bin/pip', 'install', '-r', '/path2/requirements.txt'
    ], ))

    # Copies installed packages to temporary directory
    assert mock_system.pop(0) == ('os.path.isdir',
                                  ('/tmp/.venv/lib/dir1/site-packages', ))
    assert mock_system.pop(0) == ('shutil.copytree',
                                  ('/tmp/.venv/lib/dir1/site-packages',
                                   '/tmp/.tmp'))

    # Lists installed packages
    assert mock_system.pop(0) == ('os.listdir', ('/tmp/.tmp', ))

    # Clears existing installation of package 1
    assert mock_system.pop(0) == ('os.path.isdir', ('/tmp/dir1', ))
    assert mock_system.pop(0) == ('shutil.rmtree', ('/tmp/dir1', ))

    # Moves package 1 into place
    assert mock_system.pop(0) == ('shutil.move', ('/tmp/.tmp/dir1', '/tmp'))

    # Clears existing installation of package 2
    assert mock_system.pop(0) == ('os.path.isdir', ('/tmp/dir2', ))
    assert mock_system.pop(0) == ('shutil.rmtree', ('/tmp/dir2', ))

    # Moves package 2 into place
    assert mock_system.pop(0) == ('shutil.move', ('/tmp/.tmp/dir2', '/tmp'))

    # Performs final cleanup
    assert mock_system.pop(0) == ('shutil.rmtree', ('/tmp/.venv', ))
    assert mock_system.pop(0) == ('shutil.rmtree', ('/tmp/.tmp', ))
def test_package_missing_python_dir(mock_system, mock_virtualenv, monkeypatch):
    monkeypatch.setattr(os, "listdir", lambda f: [])

    with pytest.raises(SystemExit):
        requirements.package(["/path1/requirements.txt"], "/tmp")