view tests/test_ttw.txt @ 79:f42df1456565

add a giant example section
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 02 Mar 2012 16:25:05 -0800
parents 10f343c483ee
children
line wrap: on
line source

Test Through The Web
====================

Test ``simpypi`` through the web with a test server.

The obligatory imports::

    >>> import os
    >>> import shutil
    >>> import subprocess
    >>> import tempfile
    >>> import urllib2
    >>> from simpypi.factory import factory

Create a WSGI app::

    >>> app = factory(directory=directory)

Now wrap it in a server::

    >>> port = 64321
    >>> server = testserver(app, 'localhost', port)
    >>> server.start()
    >>> url = 'http://localhost:%d/' % port

Get the home page::

    >>> resource = urllib2.urlopen(url)
    >>> contents = resource.read()
    >>> 'Python Package Index' in contents
    True

Get the index page::

    >>> index = url + 'index/'
    >>> resource = urllib2.urlopen(index)
    >>> contents = resource.read()
    >>> 'Simple Index' in contents
    True


There should be no files to start out with::

    >>> os.listdir(directory)
    []

Upload a file::

    >>> path = os.path.join(here, 'HelloWorld-0.0.tar.gz')
    >>> upload = MultiPartForm()
    >>> upload.add_file('package', path)
    >>> response = upload.post(url)

Let's see if its in the right place::

    >>> os.listdir(directory)
    ['HelloWorld']
    >>> os.listdir(os.path.join(directory, 'HelloWorld'))
    ['HelloWorld-0.0.tar.gz']

Make a virtualenv to install it in::

    >>> tmpdir = tempfile.mkdtemp()
    >>> venv = create_virtualenv(tmpdir)
    >>> code = subprocess.call([venv.python, '-c', 'import helloworld'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    >>> code
    1

Install the package and make sure you can import it::

    >>> subprocess.call([venv.easy_install, '-i', index, 'HelloWorld'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    0
    >>> code = subprocess.call([venv.python, '-c', 'import helloworld'])
    >>> code
    0
    >>> process = subprocess.Popen([venv.python, '-c', 'import helloworld; print helloworld.hello'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    >>> stdout, stderr = process.communicate()
    >>> stdout
    'Hello, world!\n'
    >>> process.returncode
    0
    >>> shutil.rmtree(tmpdir)

Shut down the server::

    >>> server.stop()