def test_curdler_service_build_directory(rmtree, run_setup_script, mkdtemp):
    
    destination = mkdtemp.return_value

    # Given a curdler service instance
    service = curdler.Curdler(index=Mock())

    # When I execute the service
    service.handle('tests', {
        'requirement': 'pkg',
        'directory': '/tmp/pkg',
    })

    # And then I see that the `setup.py` script was run
    run_setup_script.assert_called_once_with(
        '/tmp/pkg/setup.py', 'bdist_wheel')

    # And then I see that the wheel file should indexed
    service.index.from_file.assert_called_once_with(
        run_setup_script.return_value)

    # And then I see that the temporary destination and the package
    # directory are removed afterwards
    list(rmtree.call_args_list).should.equal([
        call(destination),
        call('/tmp/pkg'),
    ])
def test_curdler_service(rmtree, run_setup_script, get_setup_from_package, mkdtemp):
    "Curdler.handle() Should unpack and build packages"

    destination = mkdtemp.return_value

    # Given a curdler service instance
    service = curdler.Curdler(index=Mock())

    # When I execute the service
    service.handle('tests', {
        'requirement': 'pkg',
        'tarball': 'pkg.tar.gz',
    })

    # Then I see that the `setup.py` script was retrieved using the
    # helper `get_setup_from_package()`.
    get_setup_from_package.assert_called_once_with('pkg.tar.gz', destination)

    # And then I see that the `setup.py` script was run
    run_setup_script.assert_called_once_with(
        get_setup_from_package.return_value, 'bdist_wheel')

    # And then I see that the wheel file should indexed
    service.index.from_file.assert_called_once_with(
        run_setup_script.return_value)

    # And then the temporary destination is removed afterwards
    rmtree.assert_called_once_with(destination)
def test_curdler_service_error(rmtree, run_setup_script, mkdtemp):

    destination = mkdtemp.return_value

    # Given a curdler service instance
    service = curdler.Curdler(index=Mock())

    # And then I create a problem in the `run_setup_script` to
    # simulate a build error
    run_setup_script.side_effect = Exception('P0wned!!1')

    # When I execute the service; Then I see that the right exception
    # is raised
    service.handle.when.called_with('tests', {
        'requirement': 'pkg',
        'directory': '/tmp/pkg',
    }).should.throw(Exception, 'P0wned!!1')

    # And then I see that the temporary destination and the package
    # directory are removed afterwards
    list(rmtree.call_args_list).should.equal([
        call(destination),
        call('/tmp/pkg'),
    ])