sbsv2/raptor/test/unit_suite/raptor_unit.py
branchwip
changeset 5 593a8820b912
parent 3 e1eecf4d390d
equal deleted inserted replaced
3:e1eecf4d390d 5:593a8820b912
    15 #
    15 #
    16 # Unit tests for the raptor module
    16 # Unit tests for the raptor module
    17 
    17 
    18 import raptor
    18 import raptor
    19 import raptor_version
    19 import raptor_version
       
    20 import raptor_meta
       
    21 import raptor_utilities
    20 import re
    22 import re
    21 import unittest
    23 import unittest
    22 import generic_path
    24 import generic_path
    23 import tempfile
    25 import tempfile
    24 import os
    26 import os
    36 		r = raptor.Raptor("dirname")
    38 		r = raptor.Raptor("dirname")
    37 		self.failUnless(r.errorCode == 1) # picked up that dirname doesn't exist
    39 		self.failUnless(r.errorCode == 1) # picked up that dirname doesn't exist
    38 		
    40 		
    39 
    41 
    40 	def testVersion(self):
    42 	def testVersion(self):
    41 		self.failUnless(re.match("^\d+\.\d+\.", raptor_version.Version()))
    43 		self.failUnless(re.match("^\d+\.\d+\.", raptor_version.fullversion()))
    42 
    44 
    43 
    45 
    44 	def testCLISupport(self):
    46 	def testCLISupport(self):
    45 		r = raptor.Raptor()
    47 		r = raptor.Raptor()
    46 		r.RunQuietly(True)
    48 		r.RunQuietly(True)
    54 		r.AddProject("bar.mmp")
    56 		r.AddProject("bar.mmp")
    55 		r.SetSysDefFile("SysDef.xml")
    57 		r.SetSysDefFile("SysDef.xml")
    56 		r.SetSysDefBase("C:\\mysysdef")
    58 		r.SetSysDefBase("C:\\mysysdef")
    57 		r.AddBuildInfoFile("build.info")
    59 		r.AddBuildInfoFile("build.info")
    58 		r.SetTopMakefile("E:\\epoc32\\build\\Makefile")
    60 		r.SetTopMakefile("E:\\epoc32\\build\\Makefile")
       
    61 		
       
    62 		
       
    63 	def testComponentListParsing(self):
       
    64 		expected_spec_output = [
       
    65 				'test/smoke_suite/test_resources/simple/bld.inf',
       
    66 				'test/smoke_suite/test_resources/simple_export/bld.inf',
       
    67 				'test/smoke_suite/test_resources/simple_dll/bld.inf',
       
    68 				'test/smoke_suite/test_resources/simple_extension/bld.inf',
       
    69 				'test/smoke_suite/test_resources/simple_gui/Bld.inf',
       
    70 				'TOOLS2 SHOULD NOT APPEAR IN THE OUTPUT']
       
    71 		
       
    72 		r = raptor.Raptor()
       
    73 		null_log_instance = raptor_utilities.NullLog()
       
    74 		r.Info = null_log_instance.Info 
       
    75 		r.Debug = null_log_instance.Debug
       
    76 		r.Warn = null_log_instance.Warn
       
    77 		r.ConfigFile()
       
    78 		r.ProcessConfig()
       
    79 		# Note that tools2/bld.inf specifies tools2 as the only supported
       
    80 		# platform, so it should not appear in the component list at the end
       
    81 		r.CommandLine([
       
    82 				'-b', 'smoke_suite/test_resources/simple/bld.inf',
       
    83 				'-b', 'smoke_suite/test_resources/simple_dll/bld.inf',
       
    84 				'-b', 'smoke_suite/test_resources/simple_export/bld.inf',
       
    85 				'-b', 'smoke_suite/test_resources/simple_extension/bld.inf',
       
    86 				'-b', 'smoke_suite/test_resources/simple_gui/Bld.inf',
       
    87 				'-b', 'smoke_suite/test_resources/tools2/bld.inf',
       
    88 				'-c', 'armv5'])
       
    89 		# establish an object cache
       
    90 		r.LoadCache()
       
    91 		buildUnitsToBuild = r.GetBuildUnitsToBuild(r.configNames)
       
    92 		# find out what components to build, and in what way
       
    93 		layers = []
       
    94 		layers = r.GetLayersFromCLI()
       
    95 		
       
    96 		generic_specs = r.GenerateGenericSpecs(buildUnitsToBuild)
       
    97 		
       
    98 		specs = []
       
    99 		specs.extend(generic_specs)
       
   100 		metaReader = raptor_meta.MetaReader(r, buildUnitsToBuild)
       
   101 		specs.extend(metaReader.ReadBldInfFiles(layers[0].children,
       
   102 				False))
       
   103 
       
   104 		# See what components are actually built for the given configs
       
   105 		# should be only 5 since 1 is a tools component and we're building armv5
       
   106 		hits = 0
       
   107 		for c in layers[0].children:
       
   108 			if len(c.specs) > 0: 
       
   109 				# something will be built from this component because
       
   110 				# it has at least one spec
       
   111 				shortname = str(c.bldinf_filename)[len(os.environ['SBS_HOME'])+1:]
       
   112 				self.assertTrue(shortname in expected_spec_output)
       
   113 				hits += 1
       
   114 
       
   115 		# Ensure there actually are 5 build specs
       
   116 		self.assertEqual(hits, len(expected_spec_output) - 1)
       
   117 
    59 
   118 
    60 	def setUp(self):
   119 	def setUp(self):
    61 		self.r = raptor.Raptor()
   120 		self.r = raptor.Raptor()
    62 		self.r.out = OutputMock()
       
    63 		
   121 		
    64 		self.cwd = generic_path.CurrentDir()
   122 		self.cwd = generic_path.CurrentDir()
    65 		self.isFileReturningFalse = lambda: False
   123 		self.isFileReturningFalse = lambda: False
    66 		self.isFileReturningTrue = lambda: True
   124 		self.isFileReturningTrue = lambda: True
    67 		
   125 		
    68 		self.sysDef = self.cwd.Append(self.r.systemDefinition)
   126 		self.sysDef = self.cwd.Append(self.r.systemDefinition)
    69 		self.bldInf = self.cwd.Append(self.r.buildInformation)
   127 		self.bldInf = self.cwd.Append(self.r.buildInformation)
    70 
   128 
    71 	def testCreateWarningForNonexistingBldInfAndSystemDefinitionFile(self): 
   129 	def testWarningIfSystemDefinitionFileDoesNotExist(self): 
    72 		"""Test if sbs creates warning if executed without specified 
   130 		"""Test if sbs creates warning if executed without specified 
    73 		component to build i.e. default bld.inf (bld.inf in current 
   131 		component to build i.e. default bld.inf (bld.inf in current 
    74 		directory) or system definition file.
   132 		directory) or system definition file.
    75 
   133 
    76 		Uses an empty temporary directory for this."""
   134 		Uses an empty temporary directory for this."""
       
   135 		self.r.out = OutputMock()
    77 
   136 
    78 		d = tempfile.mkdtemp(prefix='raptor_test') 
   137 		d = tempfile.mkdtemp(prefix='raptor_test') 
    79 		cdir = os.getcwd()
   138 		cdir = os.getcwd()
    80 		os.chdir(d) 
   139 		os.chdir(d) 
    81 		self.r.GetComponentGroupsFromCLI()
   140 		layers = self.r.GetLayersFromCLI()
    82 		os.chdir(cdir) # go back
   141 		os.chdir(cdir) # go back
    83 		os.rmdir(d)
   142 		os.rmdir(d)
    84 		
   143 		
    85 		self.assertTrue(self.r.out.warningWritten())
   144 		self.assertTrue(self.r.out.warningWritten())
    86 
   145 
    87 	def testCreateWarningForExistingBldInf(self):
       
    88 		d = tempfile.mkdtemp(prefix='raptor_test') 
   146 		d = tempfile.mkdtemp(prefix='raptor_test') 
    89 		cdir = os.getcwd()
   147 		cdir = os.getcwd()
    90 		os.chdir(d)
   148 		os.chdir(d)
    91 		f = open("bld.inf","w")
   149 		f = open("bld.inf","w")
    92 		f.close()
   150 		f.close()
    93 		self.r.GetComponentGroupsFromCLI()
   151 		layers = self.r.GetLayersFromCLI()
    94 		os.unlink("bld.inf")
   152 		os.unlink("bld.inf")
    95 		os.chdir(cdir) # go back
   153 		os.chdir(cdir) # go back
    96 		os.rmdir(d)
   154 		os.rmdir(d)
    97 
   155 
    98 		self.assertFalse(self.r.out.warningWritten())
   156 		self.assertTrue(self.r.out.warningWritten())
    99 
   157 
   100 	def testCreateWarningForExistingSystemDefinitionFile(self): 
   158 	def testNoWarningIfSystemDefinitionFileExists(self): 
       
   159 		self.r.out = OutputMock()
       
   160 
   101 		d = tempfile.mkdtemp(prefix='raptor_test') 
   161 		d = tempfile.mkdtemp(prefix='raptor_test') 
   102 		cdir = os.getcwd()
   162 		cdir = os.getcwd()
   103 		os.chdir(d)
   163 		os.chdir(d)
   104 		f = open("System_Definition.xml","w")
   164 		f = open("System_Definition.xml","w")
   105 		f.close()
   165 		f.close()
   106 		self.r.GetComponentGroupsFromCLI()
   166 		layers = self.r.GetLayersFromCLI()
   107 		os.unlink("System_Definition.xml")
   167 		os.unlink("System_Definition.xml")
   108 		os.chdir(cdir) # go back
   168 		os.chdir(cdir) # go back
   109 		os.rmdir(d)
   169 		os.rmdir(d)
   110 
   170 
   111 		self.assertFalse(self.r.out.warningWritten())
   171 		self.assertFalse(self.r.out.warningWritten())
   112 	
   172 	
   113 	# Test Info, Warn & Error functions can handle attributes
   173 	# Test Info, Warn & Error functions can handle attributes
   114 	def testInfoAttributes(self):
   174 	def testInfoAttributes(self):
       
   175 		self.r.out = OutputMock()
   115 		self.r.Info("hello %s", "world", planet="earth")
   176 		self.r.Info("hello %s", "world", planet="earth")
   116 		expected = "<info planet='earth'>hello world</info>\n"
   177 		expected = "<info planet='earth'>hello world</info>\n"
   117 		self.assertEquals(self.r.out.actual, expected)
   178 		self.assertEquals(self.r.out.actual, expected)
   118 		
   179 		
   119 	def testWarnAttributes(self):
   180 	def testWarnAttributes(self):
       
   181 		self.r.out = OutputMock()
   120 		self.r.Warn("look out", where="behind you")
   182 		self.r.Warn("look out", where="behind you")
   121 		expected = "<warning where='behind you'>look out</warning>\n"
   183 		expected = "<warning where='behind you'>look out</warning>\n"
   122 		self.assertEquals(self.r.out.actual, expected)
   184 		self.assertEquals(self.r.out.actual, expected)
   123 		
   185 		
   124 	def testErrorAttributes(self):
   186 	def testErrorAttributes(self):
       
   187 		self.r.out = OutputMock()
   125 		self.r.Error("messed up %s and %s", "all", "sundry", bldinf="bld.inf")
   188 		self.r.Error("messed up %s and %s", "all", "sundry", bldinf="bld.inf")
   126 		expected = "<error bldinf='bld.inf'>messed up all and sundry</error>\n"
   189 		expected = "<error bldinf='bld.inf'>messed up all and sundry</error>\n"
   127 		self.assertEquals(self.r.out.actual, expected)	
   190 		self.assertEquals(self.r.out.actual, expected)	
   128 		
   191 		
   129 	# Test Info, Warn & Error functions to ensure XML control chars are escaped
   192 	# Test Info, Warn & Error functions to ensure XML control chars are escaped
   130 	def testInfoXMLEscaped(self):
   193 	def testInfoXMLEscaped(self):
       
   194 		self.r.out = OutputMock()
   131 		self.r.Info("h&l>o<&amp;")
   195 		self.r.Info("h&l>o<&amp;")
   132 		expected = "<info>h&amp;l&gt;o&lt;&amp;amp;</info>\n"
   196 		expected = "<info>h&amp;l&gt;o&lt;&amp;amp;</info>\n"
   133 		self.assertEquals(self.r.out.actual, expected)
   197 		self.assertEquals(self.r.out.actual, expected)
   134 		
   198 		
   135 	def testWarnXMLEscaped(self):
   199 	def testWarnXMLEscaped(self):
       
   200 		self.r.out = OutputMock()
   136 		self.r.Warn("h&l>o<&amp;")
   201 		self.r.Warn("h&l>o<&amp;")
   137 		expected = "<warning>h&amp;l&gt;o&lt;&amp;amp;</warning>\n"
   202 		expected = "<warning>h&amp;l&gt;o&lt;&amp;amp;</warning>\n"
   138 		self.assertEquals(self.r.out.actual, expected)
   203 		self.assertEquals(self.r.out.actual, expected)
   139 		
   204 		
   140 	def testErrorXMLEscaped(self):
   205 	def testErrorXMLEscaped(self):
       
   206 		self.r.out = OutputMock()
   141 		self.r.Error("h&l>o<&amp;")
   207 		self.r.Error("h&l>o<&amp;")
   142 		expected = "<error>h&amp;l&gt;o&lt;&amp;amp;</error>\n"
   208 		expected = "<error>h&amp;l&gt;o&lt;&amp;amp;</error>\n"
   143 		self.assertEquals(self.r.out.actual, expected)
   209 		self.assertEquals(self.r.out.actual, expected)
   144 	
   210 	
   145 		
   211 		
   146 # Mock output class preserving output for checking
   212 # Mock output class preserving output for checking
   147 # Can also check if any warning has been written
   213 # Can also check if any warning has been written
   148 class OutputMock(object):
   214 class OutputMock(object):
   149 	actual = ""
   215 	warningRegExp = re.compile(".*warning.*")
       
   216 
       
   217 	def __init__(self):
       
   218 		self.actual = ""
   150 	
   219 	
   151 	def write(self, text):
   220 	def write(self, text):
   152 		self.actual = text
   221 		self.actual += text
   153 		
   222 		
   154 	def warningWritten(self):
   223 	def warningWritten(self):
   155 		regExp = re.compile(".*warning.*")
   224 		if OutputMock.warningRegExp.match(self.actual):
   156 		if regExp.match(self.actual):
       
   157 			return True
   225 			return True
   158 		return False
   226 		return False
   159 			
   227 			
   160 # run all the tests
   228 # run all the tests
   161 
   229