tests: Use sys.executable instead of hard-coded 'python'
To test yamllint as a module, tests run commands like `python -m yamllint`. But some environments (like continuous integration of Debian or CentOS) don't always include the `python` executable (they use `python3` instead). Let's dynamically detect the Python executable path.
This commit is contained in:
@@ -26,6 +26,9 @@ except AssertionError:
|
|||||||
import unittest2 as unittest
|
import unittest2 as unittest
|
||||||
|
|
||||||
|
|
||||||
|
PYTHON = sys.executable or 'python'
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipIf(sys.version_info < (2, 7), 'Python 2.6 not supported')
|
@unittest.skipIf(sys.version_info < (2, 7), 'Python 2.6 not supported')
|
||||||
class ModuleTestCase(unittest.TestCase):
|
class ModuleTestCase(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@@ -46,7 +49,7 @@ class ModuleTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
def test_run_module_no_args(self):
|
def test_run_module_no_args(self):
|
||||||
with self.assertRaises(subprocess.CalledProcessError) as ctx:
|
with self.assertRaises(subprocess.CalledProcessError) as ctx:
|
||||||
subprocess.check_output(['python', '-m', 'yamllint'],
|
subprocess.check_output([PYTHON, '-m', 'yamllint'],
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT)
|
||||||
self.assertEqual(ctx.exception.returncode, 2)
|
self.assertEqual(ctx.exception.returncode, 2)
|
||||||
self.assertRegexpMatches(ctx.exception.output.decode(),
|
self.assertRegexpMatches(ctx.exception.output.decode(),
|
||||||
@@ -54,7 +57,7 @@ class ModuleTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
def test_run_module_on_bad_dir(self):
|
def test_run_module_on_bad_dir(self):
|
||||||
with self.assertRaises(subprocess.CalledProcessError) as ctx:
|
with self.assertRaises(subprocess.CalledProcessError) as ctx:
|
||||||
subprocess.check_output(['python', '-m', 'yamllint',
|
subprocess.check_output([PYTHON, '-m', 'yamllint',
|
||||||
'/does/not/exist'],
|
'/does/not/exist'],
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT)
|
||||||
self.assertRegexpMatches(ctx.exception.output.decode(),
|
self.assertRegexpMatches(ctx.exception.output.decode(),
|
||||||
@@ -62,7 +65,7 @@ class ModuleTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
def test_run_module_on_file(self):
|
def test_run_module_on_file(self):
|
||||||
out = subprocess.check_output(
|
out = subprocess.check_output(
|
||||||
['python', '-m', 'yamllint', os.path.join(self.wd, 'warn.yaml')])
|
[PYTHON, '-m', 'yamllint', os.path.join(self.wd, 'warn.yaml')])
|
||||||
lines = out.decode().splitlines()
|
lines = out.decode().splitlines()
|
||||||
self.assertIn('/warn.yaml', lines[0])
|
self.assertIn('/warn.yaml', lines[0])
|
||||||
self.assertEqual('\n'.join(lines[1:]),
|
self.assertEqual('\n'.join(lines[1:]),
|
||||||
@@ -71,7 +74,7 @@ class ModuleTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
def test_run_module_on_dir(self):
|
def test_run_module_on_dir(self):
|
||||||
with self.assertRaises(subprocess.CalledProcessError) as ctx:
|
with self.assertRaises(subprocess.CalledProcessError) as ctx:
|
||||||
subprocess.check_output(['python', '-m', 'yamllint', self.wd])
|
subprocess.check_output([PYTHON, '-m', 'yamllint', self.wd])
|
||||||
self.assertEqual(ctx.exception.returncode, 1)
|
self.assertEqual(ctx.exception.returncode, 1)
|
||||||
|
|
||||||
files = ctx.exception.output.decode().split('\n\n')
|
files = ctx.exception.output.decode().split('\n\n')
|
||||||
|
|||||||
Reference in New Issue
Block a user