|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- Coding: UTF-8 -*- |
| 3 | + |
| 4 | +__author__ = 'Benjamin Schubert, ben.c.schubert@gmail.com' |
| 5 | + |
| 6 | +from abc import abstractproperty |
| 7 | +import os |
| 8 | +import shutil |
| 9 | +import subprocess |
| 10 | +import unittest |
| 11 | + |
| 12 | + |
| 13 | +test_output_directory = "/tmp/test-wllvm" |
| 14 | +test_files_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_files") |
| 15 | +root_directory = os.path.join(os.path.dirname(os.path.dirname(__file__))) |
| 16 | + |
| 17 | + |
| 18 | +class BaseDriverTest(unittest.TestCase): |
| 19 | + """ |
| 20 | + This is a BaseDriverTest class. Can be used to generically test that every driver works correctly with different |
| 21 | + code examples without any problem. This class is meant to be overridden |
| 22 | + """ |
| 23 | + @classmethod |
| 24 | + def setUpClass(cls): |
| 25 | + """ |
| 26 | + This is a base class that should not be run in tests, skip it |
| 27 | + :return: |
| 28 | + """ |
| 29 | + if cls is BaseDriverTest: |
| 30 | + raise unittest.SkipTest("Skip BaseDriverTest, it's a base class") |
| 31 | + |
| 32 | + @abstractproperty |
| 33 | + def env(self): |
| 34 | + """ |
| 35 | + Defines all necessary environment variables to allow the driver to be tested, assembly seen in "Usage" in the README |
| 36 | + """ |
| 37 | + return None |
| 38 | + |
| 39 | + def setUp(self): |
| 40 | + """ |
| 41 | + Creates the test directory in /tmp |
| 42 | + :return: |
| 43 | + """ |
| 44 | + if not os.path.exists(test_output_directory): |
| 45 | + os.makedirs(test_output_directory) |
| 46 | + |
| 47 | + def tearDown(self): |
| 48 | + """ |
| 49 | + remove all temporary test files |
| 50 | + :return: |
| 51 | + """ |
| 52 | + shutil.rmtree(test_output_directory) |
| 53 | + |
| 54 | + def launch_proc(self, cmd): |
| 55 | + """ |
| 56 | + Launches cmd with environment and in test_output_directory |
| 57 | + :param cmd: command to launch |
| 58 | + :return: the subprocess instance |
| 59 | + """ |
| 60 | + return subprocess.Popen(cmd, shell=True, env=self.env, cwd=test_output_directory) |
| 61 | + |
| 62 | + def create_objects(self): |
| 63 | + """ |
| 64 | + Creates some objects used by tests |
| 65 | + :return: |
| 66 | + """ |
| 67 | + for f in ["foo.c", "bar.c", "baz.c", "main.c"]: |
| 68 | + self.assertEqual(self.launch_proc("${{CC}} {dir}/{f} -c".format(dir=test_files_directory, f=f)).wait(), 0) |
| 69 | + |
| 70 | + def create_archive(self): |
| 71 | + """ |
| 72 | + creates the libfoo.a archive |
| 73 | + :return: |
| 74 | + """ |
| 75 | + proc1 = self.launch_proc("ar cr libfoo.a foo.o bar.o baz.o") |
| 76 | + self.assertEqual(proc1.wait(), 0) |
| 77 | + |
| 78 | + def test_can_compile_simple_file(self): |
| 79 | + """ |
| 80 | + Checks that it is possible to compile a single simple file |
| 81 | + :return: |
| 82 | + """ |
| 83 | + proc = self.launch_proc("${{CXX}} -o hello {}/hello.cc".format(test_files_directory)) |
| 84 | + self.assertEqual(proc.wait(), 0) |
| 85 | + |
| 86 | + def test_can_compile_multiple_file_in_one_object(self): |
| 87 | + """ |
| 88 | + Checks that is is possible to compile multiple files into one executable |
| 89 | + :return: |
| 90 | + """ |
| 91 | + proc = self.launch_proc( |
| 92 | + "${{CC}} {dir}/foo.c {dir}/bar.c {dir}/baz.c {dir}/main.c -o main".format(dir=test_files_directory) |
| 93 | + ) |
| 94 | + self.assertEqual(proc.wait(), 0) |
| 95 | + |
| 96 | + def test_can_compile_and_link_multiple_object(self): |
| 97 | + """ |
| 98 | + Checks that is is possible to compile first then link the compiled objects together |
| 99 | + :return: |
| 100 | + """ |
| 101 | + proc1 = self.launch_proc( |
| 102 | + "${{CC}} {dir}/foo.c {dir}/bar.c {dir}/baz.c {dir}/main.c -c".format(dir=test_files_directory) |
| 103 | + ) |
| 104 | + self.assertEqual(proc1.wait(), 0) |
| 105 | + |
| 106 | + proc2 = self.launch_proc("${CC} foo.o bar.o baz.o main.o -o main") |
| 107 | + self.assertEqual(proc2.wait(), 0) |
| 108 | + |
| 109 | + def test_can_compile_and_link_object_and_source_object(self): |
| 110 | + """ |
| 111 | + Checks that is is possible to compile some objects first, then link them while compiling others |
| 112 | + :return: |
| 113 | + """ |
| 114 | + proc1 = self.launch_proc("${{CC}} {dir}/foo.c {dir}/bar.c -c".format(dir=test_files_directory)) |
| 115 | + self.assertEqual(proc1.wait(), 0) |
| 116 | + |
| 117 | + proc2 = self.launch_proc("${{CC}} foo.o bar.o {dir}/baz.c {dir}/main.c -o main".format(dir=test_files_directory)) |
| 118 | + self.assertEqual(proc2.wait(), 0) |
| 119 | + |
| 120 | + def test_can_link_multiple_objects_together(self): |
| 121 | + """ |
| 122 | + Checks that it is possible to link multiple objects together |
| 123 | + :return: |
| 124 | + """ |
| 125 | + self.create_objects() |
| 126 | + proc = self.launch_proc("${CC} foo.o bar.o baz.o main.o -o main") |
| 127 | + self.assertEqual(proc.wait(), 0) |
| 128 | + |
| 129 | + def test_can_create_archive_from_object_created(self): |
| 130 | + """ |
| 131 | + Checks that it is possible to create a valid archive from the created objects |
| 132 | + :return: |
| 133 | + """ |
| 134 | + self.create_objects() |
| 135 | + self.create_archive() |
| 136 | + |
| 137 | + proc2 = self.launch_proc("ranlib libfoo.a") |
| 138 | + self.assertEqual(proc2.wait(), 0) |
| 139 | + |
| 140 | + def test_can_create_dynamic_library_from_objects(self): |
| 141 | + """ |
| 142 | + Checks that is is possible to create a dynamic library from the objects |
| 143 | + :return: |
| 144 | + """ |
| 145 | + self.create_objects() |
| 146 | + proc = self.launch_proc("${CC} -dynamiclib foo.o bar.o baz.o main.o -o libfoo.dylib") |
| 147 | + self.assertEqual(proc.wait(), 0) |
| 148 | + |
| 149 | + def test_can_deadstrip_dynamic_library(self): |
| 150 | + """ |
| 151 | + Checks that is is possible to create a deadstripped dynamic library from the objects |
| 152 | + :return: |
| 153 | + """ |
| 154 | + self.create_objects() |
| 155 | + proc = self.launch_proc("${CC} -dynamiclib -Wl,-dead_strip foo.o bar.o baz.o main.o -o libfoo.dylib") |
| 156 | + self.assertEqual(proc.wait(), 0) |
| 157 | + |
| 158 | + def test_can_link_with_archive(self): |
| 159 | + """ |
| 160 | + Checks that is is possible to link with a created archive |
| 161 | + :return: |
| 162 | + """ |
| 163 | + self.create_objects() |
| 164 | + self.create_archive() |
| 165 | + |
| 166 | + proc = self.launch_proc("${CC} main.o libfoo.a -o main.arch") |
| 167 | + self.assertEqual(proc.wait(), 0) |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == '__main__': |
| 171 | + unittest.main() |
0 commit comments