Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ install:
- pip install pep8
before_script:
- "pep8 mozdownload"
script: ./run_tests.sh
script: ./run_tests.py
notifications:
email:
- [email protected]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ Run `mozdownload --help` for detailed information on the command line options.

## Running the tests

To run the tests, run `./run_tests.sh`.
To run the tests, run `./run_tests.py`.
95 changes: 95 additions & 0 deletions run_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import shutil
from subprocess import check_call
import sys
import urllib2
import zipfile

# Link to the folder which contains the zip archives of virtualenv
URL_VIRTUALENV = 'https://codeload.github.com/pypa/virtualenv/zip/'
VERSION_VIRTUALENV = '1.9.1'

DIR_BASE = os.path.abspath(os.path.dirname(__file__))
DIR_ENV = os.path.join(DIR_BASE, 'tmp', 'venv')
DIR_TMP = os.path.join(DIR_BASE, 'tmp')

REQ_TXT = os.path.join('tests', 'requirements.txt')


def download(url, target):
"""Downloads the specified url to the given target."""
response = urllib2.urlopen(url)
with open(target, 'wb') as f:
f.write(response.read())
return target


# see http://stackoverflow.com/questions/12332975/installing-python-module-within-code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we don't need 2 blank lines between global functions. That only applies to classes, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Separate top-level function and class definitions with two blank lines." - http://www.python.org/dev/peps/pep-0008/#blank-lines

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks!

def install(package, version):
package_arg = "%s==%s" % (package, version)
check_call(['pip', 'install', '--upgrade', package_arg])


def python(*args):
pypath = [os.path.join(DIR_ENV, 'bin', 'python')]
check_call(pypath + list(args))


try:
# Remove potentially pre-existing tmp_dir
shutil.rmtree(DIR_TMP, True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the discussion came up a couple of times but I'm still behind that we should make sure that our applicatoin works and would not fail if e.g. permissions for files/folders to delete are not correct. So I'm highly in favor of using mozfile.remove(), which will make our code way more cleaner. I think we can live with the small extra depenency to mozfile very well.

# Start out clean
os.makedirs(DIR_TMP)

print 'Downloading virtualenv %s' % VERSION_VIRTUALENV
virtualenv_file = download(URL_VIRTUALENV + VERSION_VIRTUALENV,
os.path.join(DIR_TMP, 'virtualenv.zip'))
virtualenv_zip = zipfile.ZipFile(virtualenv_file)
virtualenv_zip.extractall(DIR_TMP)
virtualenv_zip.close()

print 'Creating new virtual environment'
virtualenv_script = os.path.join(DIR_TMP,
'virtualenv-%s' % VERSION_VIRTUALENV,
'virtualenv.py')
check_call(['python', virtualenv_script, DIR_ENV])

print 'Activating virtual environment'
# for more info see:
# http://www.virtualenv.org/en/latest/#using-virtualenv-without-bin-python
if sys.platform == 'win32':
activate_this_file = os.path.join(DIR_ENV, 'Scripts',
'activate_this.py')
else:
activate_this_file = os.path.join(DIR_ENV, 'bin',
'activate_this.py')

if not os.path.isfile(activate_this_file):
# create venv
check_call(['virtualenv', '--no-site-packages', DIR_ENV])

execfile(activate_this_file, dict(__file__=activate_this_file))
print "Virtual environment activated successfully."

except:
print "Could not activate virtual environment."
print "Exiting."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put this except higher in the code right after the code which could fail. Then you can exit with the right exit code via sys.exit()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will have to call sys.exit() with an appropriate error code here, so that we really exit the script at this point.

sys.exit(1)


# Install dependent packages
check_call(['pip', 'install', '--upgrade', '-r', REQ_TXT])

# Install mozdownload
python('setup.py', 'develop')

# run the tests
python(os.path.join('tests', 'test.py'))

# Clean up
shutil.rmtree(DIR_TMP)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here for mozfile.remove()

32 changes: 0 additions & 32 deletions run_tests.sh

This file was deleted.

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'mozlog>=1.3',
'progressbar==2.2',
'requests==1.2.2'
]
]

setup(name='mozdownload',
version=version,
Expand Down
5 changes: 5 additions & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ManifestDestiny==0.5.6
mozfile==1.1
mozhttpd==0.6
mozlog==1.3
moztest==0.1