aboutsummaryrefslogtreecommitdiff
path: root/run_unittests.py
diff options
context:
space:
mode:
authorNirbheek Chauhan <nirbheek@centricular.com>2017-01-21 11:15:14 +0530
committerNirbheek Chauhan <nirbheek@centricular.com>2017-01-27 23:42:22 +0530
commitdbcbf19ecea9d07d264dbbc1cd87ab22393be8a7 (patch)
treee00d94bf17cb98e7b61dab6afca8b1bd65cb6fa8 /run_unittests.py
parent0e078adf5a7d47d5ad168f75e39d4a044032b197 (diff)
downloadmeson-dbcbf19ecea9d07d264dbbc1cd87ab22393be8a7.zip
meson-dbcbf19ecea9d07d264dbbc1cd87ab22393be8a7.tar.gz
meson-dbcbf19ecea9d07d264dbbc1cd87ab22393be8a7.tar.bz2
compilers: New class CompilerArgs derived from list()
The purpose of this class is to make it possible to sanely generate compiler command-lines by ensuring that new arguments appended or added to a list of arguments properly override previous arguments. For instance: >>> a = CompilerArgs(['-Lfoo', '-DBAR']) >>> a += ['-Lgah', '-DTAZ'] >>> print(a) ['-Lgah', '-Lfoo', '-DBAR', '-DTAZ'] Arguments will be de-duped if it is safe to do so. Currently, this is only done for -I and -L arguments (previous occurances are removed when a new one is added) and arguments that once added cannot be overriden such as -pipe are removed completely.
Diffstat (limited to 'run_unittests.py')
-rwxr-xr-xrun_unittests.py59
1 files changed, 58 insertions, 1 deletions
diff --git a/run_unittests.py b/run_unittests.py
index 5eba222..a90e46b 100755
--- a/run_unittests.py
+++ b/run_unittests.py
@@ -19,6 +19,7 @@ import subprocess
import re, json
import tempfile
from glob import glob
+import mesonbuild.compilers
import mesonbuild.environment
import mesonbuild.mesonlib
from mesonbuild.environment import detect_ninja, Environment
@@ -95,6 +96,62 @@ class InternalTests(unittest.TestCase):
stat.S_IRWXU | stat.S_ISUID |
stat.S_IRGRP | stat.S_IXGRP)
+ def test_compiler_args_class(self):
+ cargsfunc = mesonbuild.compilers.CompilerArgs
+ c = mesonbuild.environment.CCompiler([], 'fake', False)
+ # Test that bad initialization fails
+ self.assertRaises(TypeError, cargsfunc, [])
+ self.assertRaises(TypeError, cargsfunc, [], [])
+ self.assertRaises(TypeError, cargsfunc, c, [], [])
+ # Test that empty initialization works
+ a = cargsfunc(c)
+ self.assertEqual(a, [])
+ # Test that list initialization works
+ a = cargsfunc(['-I.', '-I..'], c)
+ self.assertEqual(a, ['-I.', '-I..'])
+ # Test that there is no de-dup on initialization
+ self.assertEqual(cargsfunc(['-I.', '-I.'], c), ['-I.', '-I.'])
+
+ ## Test that appending works
+ a.append('-I..')
+ self.assertEqual(a, ['-I..', '-I.'])
+ a.append('-O3')
+ self.assertEqual(a, ['-I..', '-I.', '-O3'])
+
+ ## Test that in-place addition works
+ a += ['-O2', '-O2']
+ self.assertEqual(a, ['-I..', '-I.', '-O3', '-O2', '-O2'])
+ # Test that removal works
+ a.remove('-O2')
+ self.assertEqual(a, ['-I..', '-I.', '-O3', '-O2'])
+ # Test that de-dup happens on addition
+ a += ['-Ifoo', '-Ifoo']
+ self.assertEqual(a, ['-Ifoo', '-I..', '-I.', '-O3', '-O2'])
+
+ # .extend() is just +=, so we don't test it
+
+ ## Test that addition works
+ # Test that adding a list with just one old arg works and yields the same array
+ a = a + ['-Ifoo']
+ self.assertEqual(a, ['-Ifoo', '-I..', '-I.', '-O3', '-O2'])
+ # Test that adding a list with one arg new and one old works
+ a = a + ['-Ifoo', '-Ibaz']
+ self.assertEqual(a, ['-Ifoo', '-Ibaz', '-I..', '-I.', '-O3', '-O2'])
+ # Test that adding args that must be prepended and appended works
+ a = a + ['-Ibar', '-Wall']
+ self.assertEqual(a, ['-Ibar', '-Ifoo', '-Ibaz', '-I..', '-I.', '-O3', '-O2', '-Wall'])
+
+ ## Test that reflected addition works
+ # Test that adding to a list with just one old arg works and DOES NOT yield the same array
+ a = ['-Ifoo'] + a
+ self.assertEqual(a, ['-Ibar', '-Ifoo', '-Ibaz', '-I..', '-I.', '-O3', '-O2', '-Wall'])
+ # Test that adding to a list with just one new arg that is not pre-pended works
+ a = ['-Werror'] + a
+ self.assertEqual(a, ['-Ibar', '-Ifoo', '-Ibaz', '-I..', '-I.', '-Werror', '-O3', '-O2', '-Wall'])
+ # Test that adding to a list with two new args preserves the order
+ a = ['-Ldir', '-Lbah'] + a
+ self.assertEqual(a, ['-Ibar', '-Ifoo', '-Ibaz', '-I..', '-I.', '-Ldir', '-Lbah', '-Werror', '-O3', '-O2', '-Wall'])
+
class LinuxlikeTests(unittest.TestCase):
def setUp(self):
@@ -416,7 +473,7 @@ class LinuxlikeTests(unittest.TestCase):
cmd = cmd[1:]
# Verify that -I flags from the `args` kwarg are first
# This is set in the '43 has function' test case
- self.assertEqual(cmd[2], '-I/tmp')
+ self.assertEqual(cmd[1], '-I/tmp')
# Verify that -O3 set via the environment is overriden by -O0
Oargs = [arg for arg in cmd if arg.startswith('-O')]
self.assertEqual(Oargs, [Oflag, '-O0'])