Add tests for tox_ini

Change-Id: I3ad3d392b3741deaf8bb98c392214a65eaac5409
This commit is contained in:
Chmouel Boudjnah 2014-09-11 14:11:01 +02:00
parent b89d73a268
commit 022610fe86
6 changed files with 85 additions and 5 deletions

3
.gitignore vendored
View File

@ -26,6 +26,7 @@ pip-log.txt
.tox
nosetests.xml
.testrepository
cover
# Translations
*.mo
@ -48,4 +49,4 @@ ChangeLog
# Editors
*~
.*.swp
.*.swp

View File

@ -33,24 +33,25 @@ def get_tox_ini():
class ToxIni(object):
_ini = None
tox_ini_file = 'tox.ini'
def _open_tox_ini(self):
if self._ini is None:
self._ini = ConfigParser.ConfigParser()
self._ini.read('tox.ini')
self._ini.read(self.tox_ini_file)
return self._ini
def exists(self):
return os.path.exists('tox.ini')
return os.path.exists(self.tox_ini_file)
def get_images(self):
ini = self._open_tox_ini()
if ini.has_option('docker', 'images'):
return ini.get('docker', 'images', '').split(',')
def get_commands(self, extra_args):
def get_commands(self, extra_args, section='testenv'):
ini = self._open_tox_ini()
commands = ini.get('testenv', 'commands')
commands = ini.get(section, 'commands')
extra_args = " ".join(extra_args)
if '{posargs}' in commands:
commands = commands.replace('{posargs}', extra_args)

View File

@ -22,6 +22,9 @@ import testtools
_TRUE_VALUES = ('true', '1', 'yes')
TESTDIR = os.path.dirname(os.path.abspath(__file__))
SAMPLEDIR = os.path.join(TESTDIR, 'samples')
class TestCase(testtools.TestCase):

14
dox/tests/samples/tox.ini Normal file
View File

@ -0,0 +1,14 @@
[tox]
minversion = 1.6
[docker]
images = foo,bar
[testenv]
commands = foobar
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
install_command = pip install -U {opts} {packages}
[testenv2]
commands = foobar {posargs} blah

View File

@ -0,0 +1,60 @@
# -*- coding: utf-8 -*-
# Author: Chmouel Boudjnah <chmouel@enovance.com>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
import os
from dox.config import tox_ini
from dox.tests import base
class TestImages(base.TestCase):
def setUp(self):
super(TestImages, self).setUp()
self.toxini = tox_ini.ToxIni()
self.toxini.tox_ini_file = os.path.join(base.SAMPLEDIR,
'tox.ini')
def test_get_tox_ini(self):
tox_ini_new = tox_ini.ToxIni()
with mock.patch.object(tox_ini, '_tox_ini', tox_ini_new):
self.assertEqual(tox_ini.get_tox_ini(),
tox_ini_new)
def test_exists_ini_file(self):
self.assertTrue(self.toxini.exists())
def test_open_tox_ini(self):
self.assertIn('tox', self.toxini._open_tox_ini().sections())
def test_get_images(self):
self.assertEqual(['foo', 'bar'],
self.toxini.get_images())
def test_get_commands(self):
self.assertEqual('foobar -c',
self.toxini.get_commands(['-c']))
self.assertEqual('foobar -c blah',
self.toxini.get_commands(
['-c'], section='testenv2'))
def test_get_prep_commands(self):
cmd = ['pip install -U -r/dox/requirements.txt '
'-r/dox/test-requirements.txt']
self.assertEqual(
self.toxini.get_prep_commands(),
cmd)

View File

@ -9,3 +9,4 @@ oslo.sphinx
testrepository>=0.0.17
testscenarios>=0.4,<0.5
testtools>=0.9.32
mock