From 501def327deae9716497031c9485227349eaf97b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Verg=C3=A9?= Date: Sun, 5 Nov 2017 10:06:46 +0100 Subject: [PATCH] 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. --- tests/test_module.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/test_module.py b/tests/test_module.py index e7c78b3..8bdffcc 100644 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -26,6 +26,9 @@ except AssertionError: import unittest2 as unittest +PYTHON = sys.executable or 'python' + + @unittest.skipIf(sys.version_info < (2, 7), 'Python 2.6 not supported') class ModuleTestCase(unittest.TestCase): def setUp(self): @@ -46,7 +49,7 @@ class ModuleTestCase(unittest.TestCase): def test_run_module_no_args(self): with self.assertRaises(subprocess.CalledProcessError) as ctx: - subprocess.check_output(['python', '-m', 'yamllint'], + subprocess.check_output([PYTHON, '-m', 'yamllint'], stderr=subprocess.STDOUT) self.assertEqual(ctx.exception.returncode, 2) self.assertRegexpMatches(ctx.exception.output.decode(), @@ -54,7 +57,7 @@ class ModuleTestCase(unittest.TestCase): def test_run_module_on_bad_dir(self): with self.assertRaises(subprocess.CalledProcessError) as ctx: - subprocess.check_output(['python', '-m', 'yamllint', + subprocess.check_output([PYTHON, '-m', 'yamllint', '/does/not/exist'], stderr=subprocess.STDOUT) self.assertRegexpMatches(ctx.exception.output.decode(), @@ -62,7 +65,7 @@ class ModuleTestCase(unittest.TestCase): def test_run_module_on_file(self): 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() self.assertIn('/warn.yaml', lines[0]) self.assertEqual('\n'.join(lines[1:]), @@ -71,7 +74,7 @@ class ModuleTestCase(unittest.TestCase): def test_run_module_on_dir(self): 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) files = ctx.exception.output.decode().split('\n\n')