|
| 1 | +import os |
| 2 | + |
| 3 | +from e3.testsuite.driver.classic import TestAbortWithError |
| 4 | +from e3.testsuite.driver.diff import DiffTestDriver, OutputRefiner, Substitute |
| 5 | + |
| 6 | +from drivers import gprbuild |
| 7 | + |
| 8 | + |
| 9 | +class ToLower(OutputRefiner): |
| 10 | + """Output refiner to switch to lower case.""" |
| 11 | + |
| 12 | + def refine(self, output): |
| 13 | + return output.lower() |
| 14 | + |
| 15 | + |
| 16 | +class BuildRunDiffDriver(DiffTestDriver): |
| 17 | + """Build and run a project using GNATCOLL. |
| 18 | +
|
| 19 | + Put project and source files in the test directory, in particular |
| 20 | + "test.gpr" in the root directory, whose compilation is supposed to create a |
| 21 | + "test" executable in the same directory. |
| 22 | +
|
| 23 | + This test driver builds the "test.gpr" project file and then executes the |
| 24 | + "test" shell script (test.sh). This script should run the executable that |
| 25 | + was built. It can either pass through all output to stdout, or stdout can |
| 26 | + be piped to files to allow for post processesing first. The test succeeds |
| 27 | + if all of the following items are true: |
| 28 | +
|
| 29 | + * the compilation is successful; |
| 30 | + * the "test" program completes with status code 0. |
| 31 | + * the contents of test.out/regex_test.out match the stdout of the test.sh |
| 32 | + """ |
| 33 | + |
| 34 | + @property |
| 35 | + def baseline(self): |
| 36 | + # Allow a missing test.out or regex_test.out -- treat as empty |
| 37 | + test_out = self.test_dir("test.out") |
| 38 | + regex_test_out = self.test_dir("regex_test.out") |
| 39 | + regex = False |
| 40 | + if os.path.exists(test_out): |
| 41 | + with open(test_out, encoding=self.default_encoding) as f: |
| 42 | + baseline = f.read() |
| 43 | + elif os.path.exists(regex_test_out): |
| 44 | + with open(regex_test_out, encoding=self.default_encoding) as f: |
| 45 | + baseline = f.read() |
| 46 | + regex = True |
| 47 | + else: |
| 48 | + baseline = "" |
| 49 | + test_out = None |
| 50 | + |
| 51 | + return (test_out, baseline, regex) |
| 52 | + |
| 53 | + @property |
| 54 | + def output_refiners(self): |
| 55 | + result = super().output_refiners |
| 56 | + if self.test_env.get("fold_casing", False): |
| 57 | + result.append(ToLower()) |
| 58 | + if self.test_env.get("canonicalize_backslashes", False): |
| 59 | + result.append(Substitute("\\", "/")) |
| 60 | + return result |
| 61 | + |
| 62 | + def run(self): |
| 63 | + # Build the test project |
| 64 | + if self.test_env.get('no-coverage'): |
| 65 | + gpr_project_path = self.env.gnatcoll_debug_gpr_dir |
| 66 | + else: |
| 67 | + gpr_project_path = self.env.gnatcoll_gpr_dir |
| 68 | + gprbuild( |
| 69 | + self, |
| 70 | + project_file="test.gpr", |
| 71 | + gcov=self.env.gcov, |
| 72 | + gpr_project_path=gpr_project_path |
| 73 | + ) |
| 74 | + |
| 75 | + # Run the test program |
| 76 | + p = self.shell(["bash", "test.sh"], catch_error=False) |
| 77 | + |
| 78 | + if p.status: |
| 79 | + self.output += ">>>program returned status code {}\n".format( |
| 80 | + p.status |
| 81 | + ) |
0 commit comments