Skip to content
Permalink
Browse files

[tests] refactor and add new "unit" driver

  • Loading branch information...
geertj committed Feb 4, 2014
1 parent bad3565 commit 54eb9417046919e66f6461f3aa1af8e0c204b732
Showing with 146 additions and 70 deletions.
  1. +1 −0 .gitignore
  2. +127 −0 tests/support.py
  3. +4 −4 tests/test_database.py
  4. +1 −1 tests/test_parsing.py
  5. +1 −1 tests/test_validate.py
  6. +12 −64 tests/unit.py
@@ -14,3 +14,4 @@ MANIFEST.in
.*.swo
header
*~
tests/testcert.pem
@@ -0,0 +1,127 @@
#
# This file is part of Bluepass. Bluepass is Copyright (c) 2012-2014
# Geert Jansen.
#
# Bluepass is free software available under the GNU General Public License,
# version 3. See the file LICENSE distributed with this file for the exact
# licensing terms.

from __future__ import absolute_import, print_function

import os
import sys
import shutil
import tempfile
import logging
import subprocess
import functools

if sys.version_info[:2] >= (2,7):
import unittest
else:
import unittest2 as unittest

SkipTest = unittest.SkipTest

__all__ = ['UnitTest', 'SkipTest', 'unittest', 'unix_only']


def setup_logging():
"""Configure a logger to output to stdout."""
logger = logging.getLogger()
if logger.handlers:
return
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
template = '%(levelname)s %(message)s'
handler.setFormatter(logging.Formatter(template))
logger.addHandler(handler)


def create_ssl_certificate(fname):
"""Create a new SSL private key and self-signed certificate, and store
them both in the file *fname*."""
try:
openssl = subprocess.Popen(['openssl', 'req', '-new',
'-newkey', 'rsa:1024', '-x509', '-subj', '/CN=test/',
'-days', '365', '-nodes', '-batch',
'-out', fname, '-keyout', fname],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError:
sys.stderr.write('Error: openssl not found. SSL tests disabled.\n')
return
stdout, stderr = openssl.communicate()
if openssl.returncode:
sys.stderr.write('Error: key generation failed\n')
sys.stderr.write('openssl stdout: {0}\n'.format(stdout))
sys.stderr.write('openssl stderr: {0}\n'.format(stderr))


def unix_only(func):
"""Decorator to mark a test as Unix-only."""
@functools.wraps(func)
def wrapped(self):
if not hasattr(os, 'fork'):
raise SkipTest('this test only works on Unix')
return func(self)
return wrapped


class UnitTest(unittest.TestCase):
"""Base class for unit tests."""

@classmethod
def setUpClass(cls):
cls.__tmpdir = tempfile.mkdtemp('bluepass-test')
cls._tmpindex = 1
setup_logging()
testdir = os.path.abspath(os.path.split(__file__)[0])
os.chdir(testdir)
if not os.access('testcert.pem', os.R_OK):
create_ssl_certificate('testcert.pem')
cls.certname = 'testcert.pem'

@classmethod
def tearDownClass(cls):
# Some paranoia checks to make me feel better before calling
# shutil.rmtree()..
assert '/..' not in cls.__tmpdir and '\\..' not in cls.__tmpdir
if '/tmp/' not in cls.__tmpdir and '\\temp\\' not in cls.__tmpdir:
return
try:
shutil.rmtree(cls.__tmpdir)
except OSError:
# On Windows a WindowsError is raised when files are
# still open (WindowsError inherits from OSError).
pass
cls.__tmpdir = None

@property
def tempdir(self):
return self.__tmpdir

@classmethod
def tempname(cls, name=None):
if name is None:
name = 'tmpfile-{0}'.format(cls._tmpindex)
cls._tmpindex += 1
return os.path.join(cls.__tmpdir, name)

@classmethod
def pipename(cls, name):
if sys.platform.startswith('win'):
return r'\\.\pipe\{0}-{1}'.format(name, os.getpid())
else:
return cls.tempname(name)

def assertRaises(self, exc, func, *args, **kwargs):
# Like unittest.assertRaises, but returns the exception.
try:
func(*args, **kwargs)
except exc as e:
exc = e
except Exception as e:
self.fail('Wrong exception raised: {0!s}'.format(e))
else:
self.fail('Exception not raised: {0!s}'.format(exc))
return exc
@@ -1,5 +1,5 @@
#
# This file is part of Bluepass. Bluepass is Copyright (c) 2012-2013
# This file is part of Bluepass. Bluepass is Copyright (c) 2012-2014
# Geert Jansen.
#
# Bluepass is free software available under the GNU General Public License,
@@ -8,15 +8,15 @@

from __future__ import absolute_import, print_function

from unit import *
from bluepass.database import Database
from tests.support import *
from bluepass.database import *


class TestDatabase(UnitTest):
"""Unit test suite for Database."""

def setUp(self):
self.filename = self.tempfile()
self.filename = self.tempname()
self.database = Database(self.filename)
self.database.create_table('items')

@@ -10,7 +10,7 @@

import re

from unit import *
from tests.support import *
from bluepass.parsing import *


@@ -9,7 +9,7 @@
from __future__ import absolute_import, print_function

import six
from unit import *
from tests.support import *

from bluepass import validate as vmod
from bluepass.validate import *
@@ -1,78 +1,26 @@
#
# This file is part of Bluepass. Bluepass is Copyright (c) 2012-2013
# This file is part of Bluepass. Bluepass is Copyright (c) 2012-2014
# Geert Jansen.
#
# Bluepass is free software available under the GNU General Public License,
# version 3. See the file LICENSE distributed with this file for the exact
# licensing terms.

from __future__ import absolute_import, print_function

import os
import sys
import shutil
import tempfile
import unittest

if sys.version_info[:2] >= (2,7):
import unittest
else:
import unittest2 as unittest

SkipTest = unittest.SkipTest

__all__ = ['UnitTest', 'SkipTest', 'unittest']


def assert_raises(exc, func, *args):
"""Like nose.tools.assert_raises but returns the exception."""
try:
func(*args)
except Exception as e:
if isinstance(e, exc):
return e
raise
raise AssertionError('%s not raised' % exc.__name__)


class UnitTest(unittest.TestCase):
"""Base class for unit tests."""

@classmethod
def setUpClass(cls):
cls.tmpdir = tempfile.mkdtemp()
cls.prevdir = os.getcwd()
os.chdir(cls.tmpdir)
cls.tmpdirs = []

@classmethod
def tearDownClass(cls):
os.chdir(cls.prevdir)
shutil.rmtree(cls.tmpdir)
for tmpdir in cls.tmpdirs:
shutil.rmtree(tmpdir)
if sys.version_info[:2] < (3,3):
sys.stderr.write('This driver requires Python >= 3.3\n')
sys.stderr.write('Please use "nosetests" instead.\n')
sys.exit(1)

def tempfile(self):
return tempfile.mkstemp(dir=self.tmpdir)[1]
from unittest import TestLoader, TextTestRunner

def tempdir(self):
tmpdir = tempfile.mkdtemp()
self.tmpdirs.append(tmpdir)
return tmpdir
testdir = os.path.split(os.path.abspath(__file__))[0]
os.chdir(testdir)

def write_file(self, fname, contents):
fout = file(fname, 'w')
fout.write(contents)
fout.close()
loader = TestLoader()
tests = loader.discover('.', 'test_*.py')

def assertRaises(self, exc, func, *args, **kwargs):
# Like unittest.assertRaises, but returns the exception.
try:
func(*args, **kwargs)
except exc as e:
exc = e
except Exception as e:
self.fail('Wrong exception raised: {0!s}'.format(e))
else:
self.fail('Exception not raised: {0!s}'.format(exc))
return exc
runner = TextTestRunner(verbosity=1, buffer=True)
runner.run(tests)

0 comments on commit 54eb941

Please sign in to comment.
You can’t perform that action at this time.