Move to Python3
Most of the changes have been made by 2to3. Some manual fiddling was
needed to make sure we're calling python3 instead of just python in the
tests. Two of the helpers in the tests were reworked into proper methods
rather than partials because we had to convert their returned value to
str and this seems like the most straightforward/easy-to-read way.
Change-Id: I74fdc1eaade3c026ee0c3e6bcdf26f8840f259b3
diff --git a/tests/cli/testllvmprojects.py b/tests/cli/testllvmprojects.py
index c33ddb5..e8bb860 100644
--- a/tests/cli/testllvmprojects.py
+++ b/tests/cli/testllvmprojects.py
@@ -10,7 +10,6 @@
import subprocess
import unittest
-from functools import partial
from tempfile import mkdtemp
from uuid import uuid4
@@ -29,13 +28,14 @@
try:
test(*args, **kwargs)
except subprocess.CalledProcessError as exc:
- print "Error in {}:".format(test.__name__)
- print "Command {} exited with error code {}:\n{}".format(
- exc.cmd, exc.returncode, exc.output)
+ print("Error in {}:".format(test.__name__))
+ print("Command {} exited with error code {}:\n{}".format(
+ exc.cmd, exc.returncode, exc.output))
return wrapper
class Testllvmprojs(unittest.TestCase):
+ python = "python3"
script = os.path.join("scripts", "llvm.py")
@classmethod
@@ -64,6 +64,22 @@
def __get_subproj_repo(cls, subproj):
return os.path.join(cls.repos, subproj)
+ @staticmethod
+ def run_with_output(*args, **kwargs):
+ """Helper for running a command and capturing stdout and stderr"""
+ kwargs["stderr"] = subprocess.STDOUT
+ return str(subprocess.check_output(*args, **kwargs), 'utf-8')
+
+ @staticmethod
+ def run_quietly(*args, **kwargs):
+ """
+ Helper for running a command and ignoring stdout and stderr. Exceptions
+ are still thrown if something goes wrong
+ """
+ kwargs["stdout"] = subprocess.DEVNULL
+ kwargs["stderr"] = subprocess.DEVNULL
+ return subprocess.check_call(*args, **kwargs)
+
@classmethod
def setUpClass(cls):
"""Create the file structure and environment that llvmprojs expects"""
@@ -73,17 +89,6 @@
cls.all_repos = ("llvm", "clang", "compiler-rt", "lld", "lldb",
"libcxx", "libcxxabi", "libunwind", "test-suite")
- # Set up helper functions for running commands (this needs to be done
- # before creating the repos)
- # FIXME: In newer versions of Python (3.3+) we should be able to bind
- # the stdout to DEVNULL, as below:
- # cls.run_quietly = partial(subprocess.call, stdout=subprocess.DEVNULL)
- # This doesn't work in Python 2, so we'll just have to ignore the output
- # for now...
- cls.run_with_output = partial(subprocess.check_output,
- stderr=subprocess.STDOUT)
- cls.run_quietly = cls.run_with_output
-
# Create dummy repos
for reponame in cls.all_repos:
cls.__create_dummy_repo(cls.__get_subproj_repo(reponame))
@@ -128,38 +133,38 @@
Test that we're correctly dumping an empty configuration (i.e. no
projects linked) when running llvmprojs without arguments.
"""
- output = self.run_with_output(["python", self.script, "projects"])
- self.assertRegexpMatches(output, "Projects linked:.*\n.*none.*")
+ output = self.run_with_output([self.python, self.script, "projects"])
+ self.assertRegex(output, "Projects linked:.*\n.*none.*")
def test_add_remove_subprojects(self):
"""
Test that we can add and remove one or several subprojects.
"""
- output = self.run_with_output(["python", self.script, "projects",
+ output = self.run_with_output([self.python, self.script, "projects",
"--add", "clang"])
- self.assertRegexpMatches(output, "Projects linked:.*\n.*clang.*")
+ self.assertRegex(output, "Projects linked:.*\n.*clang.*")
- output = self.run_with_output(["python", self.script, "projects",
+ output = self.run_with_output([self.python, self.script, "projects",
"--add", "libcxx", "lldb"])
- self.assertRegexpMatches(
+ self.assertRegex(
output,
"Projects linked:.*\n" +
".*clang.*\n" +
".*libcxx.*\n" +
".*lldb.*")
- output = self.run_with_output(["python", self.script, "projects",
+ output = self.run_with_output([self.python, self.script, "projects",
"--remove", "libcxx"])
- self.assertRegexpMatches(output,
- "Projects linked:.*\n" +
- ".*clang.*\n" +
- ".*lldb.*")
+ self.assertRegex(output,
+ "Projects linked:.*\n" +
+ ".*clang.*\n" +
+ ".*lldb.*")
- output = self.run_with_output(["python", self.script, "projects",
+ output = self.run_with_output([self.python, self.script, "projects",
"--remove", "clang", "lldb"])
- self.assertRegexpMatches(output,
- "Projects linked:.*\n" +
- ".*none.*")
+ self.assertRegex(output,
+ "Projects linked:.*\n" +
+ ".*none.*")
def test_add_remove_invalid_subprojects(self):
"""
@@ -167,19 +172,19 @@
subprojects.
"""
with self.assertRaises(subprocess.CalledProcessError) as context:
- self.run_quietly(["python", self.script, "projects",
- "--add", "inventedsubproject"])
+ self.run_with_output([self.python, self.script, "projects",
+ "--add", "inventedsubproject"])
- self.assertRegexpMatches(
- context.exception.output,
+ self.assertRegex(
+ str(context.exception.output),
"(.*\n)*.*invalid choice:.*inventedsubproject(.*\n)*")
with self.assertRaises(subprocess.CalledProcessError) as context:
- self.run_quietly(["python", self.script, "projects",
- "--remove", "inventedsubproject"])
+ self.run_with_output([self.python, self.script, "projects",
+ "--remove", "inventedsubproject"])
- self.assertRegexpMatches(
- context.exception.output,
+ self.assertRegex(
+ str(context.exception.output),
"(.*\n)*.*invalid choice:.*inventedsubproject(.*\n)*")
def test_duplicate_add_remove(self):
@@ -187,18 +192,18 @@
Test that we don't crash when trying to add / remove the same subproject
twice with the same command.
"""
- output = self.run_with_output(["python", self.script, "projects",
+ output = self.run_with_output([self.python, self.script, "projects",
"--add", "clang", "lld", "clang"])
- self.assertRegexpMatches(output,
- "Projects linked:.*\n" +
- ".*clang.*\n" +
- ".*lld.*")
+ self.assertRegex(output,
+ "Projects linked:.*\n" +
+ ".*clang.*\n" +
+ ".*lld.*")
- output = self.run_with_output(["python", self.script, "projects",
+ output = self.run_with_output([self.python, self.script, "projects",
"--remove", "lld", "lld", "clang"])
- self.assertRegexpMatches(output,
- "Projects linked:.*\n" +
- ".*none.*")
+ self.assertRegex(output,
+ "Projects linked:.*\n" +
+ ".*none.*")
def test_redundant_add_remove(self):
"""
@@ -214,24 +219,24 @@
os.path.join(self.llvm_src, "projects", "compiler-rt"),
self.branch)
- output = self.run_with_output(["python", self.script, "projects",
+ output = self.run_with_output([self.python, self.script, "projects",
"--add", "clang"])
- self.assertRegexpMatches(output,
- "Projects linked:.*\n" +
- ".*clang.*")
+ self.assertRegex(output,
+ "Projects linked:.*\n" +
+ ".*clang.*")
- output = self.run_with_output(["python", self.script, "projects",
+ output = self.run_with_output([self.python, self.script, "projects",
"--add", "compiler-rt", "lld"])
- self.assertRegexpMatches(
+ self.assertRegex(
output,
"Projects linked:.*\n" +
".*clang.*\n" +
".*compiler-rt.*\n" +
".*lld.*")
- output = self.run_with_output(["python", self.script, "projects",
+ output = self.run_with_output([self.python, self.script, "projects",
"--remove", "lldb", "libcxx", "lld"])
- self.assertRegexpMatches(
+ self.assertRegex(
output,
"Projects linked:.*\n" +
".*clang.*\n" +
@@ -244,11 +249,11 @@
"""
# Try the really basic case and make sure we're not touching anything
with self.assertRaises(subprocess.CalledProcessError) as context:
- self.run_quietly(["python", self.script, "projects",
- "--add", "clang", "--remove", "clang"])
+ self.run_with_output([self.python, self.script, "projects",
+ "--add", "clang", "--remove", "clang"])
- self.assertRegexpMatches(
- context.exception.output,
+ self.assertRegex(
+ str(context.exception.output),
"(.*\n)*.*Can't add and remove clang at the same time(.*\n)*")
self.assertFalse(
@@ -263,11 +268,11 @@
self.branch)
with self.assertRaises(subprocess.CalledProcessError) as context:
- self.run_quietly(["python", self.script, "projects",
- "--add", "clang", "lld", "libcxx",
- "--remove", "lld", "libcxx"])
- self.assertRegexpMatches(
- context.exception.output,
+ self.run_with_output([self.python, self.script, "projects",
+ "--add", "clang", "lld", "libcxx",
+ "--remove", "lld", "libcxx"])
+ self.assertRegex(
+ str(context.exception.output),
"(.*\n)*" +
".*Can't add and remove (lld|libcxx) at the same time(.*\n)*")
@@ -287,32 +292,32 @@
Test that we can have multiple --add and --remove options in the same
command and that only the last one of each kind matters.
"""
- output = self.run_with_output(["python", self.script, "projects",
+ output = self.run_with_output([self.python, self.script, "projects",
"--add", "libcxxabi",
"--remove", "lld", "lldb",
"--add", "clang", "libcxx",
"--remove", "libunwind"])
- self.assertRegexpMatches(output,
- "Projects linked:.*\n" +
- ".*clang.*\n" +
- ".*libcxx.*\n")
+ self.assertRegex(output,
+ "Projects linked:.*\n" +
+ ".*clang.*\n" +
+ ".*libcxx.*\n")
- output = self.run_with_output(["python", self.script, "projects",
+ output = self.run_with_output([self.python, self.script, "projects",
"--add", "libunwind", "libcxxabi",
"--remove", "clang", "libcxx",
"--add", "compiler-rt",
"--remove", "libcxxabi"])
- self.assertRegexpMatches(output,
- "Projects linked:.*\n" +
- ".*clang.*\n" +
- ".*compiler-rt.*\n" +
- ".*libcxx.*\n")
+ self.assertRegex(output,
+ "Projects linked:.*\n" +
+ ".*clang.*\n" +
+ ".*compiler-rt.*\n" +
+ ".*libcxx.*\n")
- output = self.run_with_output(["python", self.script, "projects",
+ output = self.run_with_output([self.python, self.script, "projects",
"--add", "libcxx", "--remove", "lld",
"--add", "lld", "--remove", "libcxx"])
- self.assertRegexpMatches(output,
- "Projects linked:.*\n" +
- ".*clang.*\n" +
- ".*compiler-rt.*\n" +
- ".*lld.*\n")
+ self.assertRegex(output,
+ "Projects linked:.*\n" +
+ ".*clang.*\n" +
+ ".*compiler-rt.*\n" +
+ ".*lld.*\n")