diff --git a/.gitignore b/.gitignore index 1399c98..c17df2b 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ pip-log.txt .tox nosetests.xml .testrepository +cover # Translations *.mo @@ -48,4 +49,4 @@ ChangeLog # Editors *~ -.*.swp \ No newline at end of file +.*.swp diff --git a/dox/config/tox_ini.py b/dox/config/tox_ini.py index 29d4244..0698c5e 100644 --- a/dox/config/tox_ini.py +++ b/dox/config/tox_ini.py @@ -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) diff --git a/dox/tests/base.py b/dox/tests/base.py index 253ef6e..7311049 100644 --- a/dox/tests/base.py +++ b/dox/tests/base.py @@ -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): diff --git a/dox/tests/samples/tox.ini b/dox/tests/samples/tox.ini new file mode 100644 index 0000000..9e7b4a5 --- /dev/null +++ b/dox/tests/samples/tox.ini @@ -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 diff --git a/dox/tests/test_config_tox_ini.py b/dox/tests/test_config_tox_ini.py new file mode 100644 index 0000000..6f9a3fc --- /dev/null +++ b/dox/tests/test_config_tox_ini.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +# Author: Chmouel Boudjnah +# +# 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) diff --git a/test-requirements.txt b/test-requirements.txt index 9dab35b..52aac41 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -9,3 +9,4 @@ oslo.sphinx testrepository>=0.0.17 testscenarios>=0.4,<0.5 testtools>=0.9.32 +mock