Package qm :: Package test :: Package classes :: Module compilation_test
[hide private]
[frames] | no frames]

Source Code for Module qm.test.classes.compilation_test

  1  ######################################################################## 
  2  # 
  3  # File:   compilation_test.py 
  4  # Author: Stefan Seefeld 
  5  # Date:   2005-10-17 
  6  # 
  7  # Contents: 
  8  #   CompilationTest 
  9  #   CompiledResource 
 10  #   ExecutableTest 
 11  # 
 12  # Copyright (c) 2005 by CodeSourcery, LLC.  All rights reserved.  
 13  # 
 14  # For license terms see the file COPYING. 
 15  # 
 16  ######################################################################## 
 17   
 18  from compiler_test import CompilationStep, CompilerTest 
 19  from   qm.fields import * 
 20  from   qm.test.database import get_database 
 21  from   qm.test.result import * 
 22  from   qm.test.test import * 
 23  from   qm.test.resource import * 
 24  import qm.executable 
 25  from   qm.extension import parse_descriptor 
 26  from   qm.test.base import get_extension_class 
 27  from   compiler import Compiler 
 28  from   local_host import LocalHost 
 29   
 30   
31 -def _get_host(context, variable):
32 """Get a host instance according to a particular context variable. 33 Return a default 'LocalHost' host if the variable is undefined. 34 35 'context' -- The context to read the host descriptor from. 36 37 'variable' -- The name to which the host descriptor is bound. 38 39 returns -- A Host instance. 40 41 """ 42 43 target_desc = context.get(variable) 44 if target_desc is None: 45 target = LocalHost({}) 46 else: 47 f = lambda n: get_extension_class(n, "host", get_database()) 48 host_class, arguments = parse_descriptor(target_desc.strip(), f) 49 target = host_class(arguments) 50 return target
51 52 53 ######################################################################## 54 # Classes 55 ######################################################################## 56
57 -class CompilationTest(CompilerTest):
58 """A CompilationTest compiles and optionally runs an executable. 59 CompilationTest allows simple cross-testing. To run the executable on 60 anything other than localhost, specify a Host descriptor by means of the 61 context variable 'CompilationTest.target'.""" 62 63 options = SetField(TextField(description="""Test-specific options to pass to the compiler.""")) 64 ldflags = SetField(TextField(description="""Test-specific link flags to pass to the compiler.""")) 65 source_files = SetField(TextField(description="Source files to be compiled.")) 66 executable = TextField(description="The name of the executable to be compiled.") 67 execute = BooleanField(default_value = True, 68 description="Whether or not to run the compiled executable.") 69 70
71 - def Run(self, context, result):
77 78
79 - def _GetCompiler(self, context):
80 """The name of the compiler executable is taken from the context variable 81 'CompilationTest.compiler_path'.""" 82 83 name = context["CompilationTest.compiler_path"] 84 options = context.GetStringList("CompilationTest.compiler_options", []) 85 ldflags = context.GetStringList("CompilationTest.compiler_ldflags", []) 86 return Compiler(name, options, ldflags)
87 88
89 - def _GetCompilationSteps(self, context):
90 91 # Compile the executable in a single step so we can apply all 92 # options at once. 93 return [CompilationStep(self._GetCompiler(context), 94 Compiler.MODE_LINK, self.source_files, 95 self.options, self.ldflags, self.executable, [])]
96 97
98 - def _IsExecutionRequired(self):
99 100 return self.execute
101 102
103 - def _GetTarget(self, context):
104 105 return _get_host(context, "CompilationTest.target")
106 107
108 - def _CheckOutput(self, context, result, prefix, output, diagnostics):
109 110 if output: 111 result[prefix + "output"] = result.Quote(output) 112 113 return True
114 115
116 -class CompiledResource(Resource):
117 """A CompiledResource compiles an executable.""" 118 119 options = SetField(TextField(description="Resource-specific options to pass to the compiler.")) 120 source_files = SetField(TextField(description="Source files to be compiled.")) 121 executable = TextField(description="The name of the executable to be compiled.") 122 123
124 - def SetUp(self, context, result):
125 126 self._context = context 127 self._compiler = CompilationTest({'options':self.options, 128 'source_files':self.source_files, 129 'executable':self.executable, 130 'execute':False}, 131 qmtest_id = self.GetId(), 132 qmtest_database = self.GetDatabase()) 133 134 self._compiler.Run(context, result) 135 directory = self._compiler._GetDirectory(context) 136 self._executable = os.path.join(directory, self.executable) 137 context['CompiledResource.executable'] = self._executable
138 139
140 - def CleanUp(self, result):
141 142 self._compiler._RemoveDirectory(self._context, result)
143 144
145 -class ExecutableTest(Test):
146 """An ExecuableTest runs an executable from a CompiledResource. 147 ExecutableTest allows simple cross-testing. To run the executable on 148 anything other than localhost, specify a Host descriptor by means of the 149 context variable 'ExecutableTest.host'.""" 150 151 args = SetField(TextField(description="Arguments to pass to the executable.")) 152
153 - def Run(self, context, result):
154 155 executable = context['CompiledResource.executable'] 156 host = _get_host(context, 'ExecutableTest.host') 157 status, output = host.UploadAndRun(executable, self.args) 158 if not result.CheckExitStatus('ExecutableTest.', 'Program', status): 159 result.Fail('Unexpected exit_code') 160 if output: 161 result['ExecutableTest.output'] = result.Quote(output)
162