def testAssertOutputs(self): output_dir = tempfile.mkdtemp() class DummyOpts(object): def __init__(self): self.input = util.PathFromRoot('grit/testdata/substitute.grd') self.verbose = False self.extra_verbose = False # Incomplete output file list should fail. builder_fail = build.RcBuilder() self.failUnlessEqual( 2, builder_fail.Run(DummyOpts(), [ '-o', output_dir, '-a', os.path.abspath( os.path.join(output_dir, 'en_generated_resources.rc')) ])) # Complete output file list should succeed. builder_ok = build.RcBuilder() self.failUnlessEqual( 0, builder_ok.Run(DummyOpts(), [ '-o', output_dir, '-a', os.path.abspath( os.path.join(output_dir, 'en_generated_resources.rc')), '-a', os.path.abspath( os.path.join(output_dir, 'sv_generated_resources.rc')), '-a', os.path.abspath(os.path.join(output_dir, 'resource.h')) ]))
def testAssertTemplateOutputs(self): output_dir = util.TempDir({}) class DummyOpts(object): def __init__(self): self.input = util.PathFromRoot( 'grit/testdata/substitute_tmpl.grd') self.verbose = False self.extra_verbose = False # Incomplete output file list should fail. builder_fail = build.RcBuilder() self.failUnlessEqual( 2, builder_fail.Run(DummyOpts(), [ '-o', output_dir.GetPath(), '-E', 'name=foo', '-a', os.path.abspath(output_dir.GetPath('en_foo_resources.rc')) ])) # Complete output file list should succeed. builder_ok = build.RcBuilder() self.failUnlessEqual( 0, builder_ok.Run(DummyOpts(), [ '-o', output_dir.GetPath(), '-E', 'name=foo', '-a', os.path.abspath( output_dir.GetPath('en_foo_resources.rc')), '-a', os.path.abspath(output_dir.GetPath('sv_foo_resources.rc')), '-a', os.path.abspath(output_dir.GetPath('resource.h')) ])) output_dir.CleanUp()
def testWhitelistStrings(self): output_dir = tempfile.mkdtemp() builder = build.RcBuilder() class DummyOpts(object): def __init__(self): self.input = util.PathFromRoot( 'grit/testdata/whitelist_strings.grd') self.verbose = False self.extra_verbose = False whitelist_file = util.PathFromRoot('grit/testdata/whitelist.txt') builder.Run(DummyOpts(), ['-o', output_dir, '-w', whitelist_file]) header = os.path.join(output_dir, 'whitelist_test_resources.h') rc = os.path.join(output_dir, 'en_whitelist_test_strings.rc') whitelisted_ids = ['IDS_MESSAGE_WHITELISTED'] non_whitelisted_ids = ['IDS_MESSAGE_NOT_WHITELISTED'] self._verifyWhitelistedOutput( header, whitelisted_ids, non_whitelisted_ids, ) self._verifyWhitelistedOutput(rc, whitelisted_ids, non_whitelisted_ids, encoding='utf16')
def testWriteOnlyNew(self): output_dir = util.TempDir({}) builder = build.RcBuilder() class DummyOpts(object): def __init__(self): self.input = util.PathFromRoot('grit/testdata/substitute.grd') self.verbose = False self.extra_verbose = False UNCHANGED = 10 header = output_dir.GetPath('resource.h') builder.Run(DummyOpts(), ['-o', output_dir.GetPath()]) self.failUnless(os.path.exists(header)) first_mtime = os.stat(header).st_mtime os.utime(header, (UNCHANGED, UNCHANGED)) builder.Run(DummyOpts(), ['-o', output_dir.GetPath(), '--write-only-new', '0']) self.failUnless(os.path.exists(header)) second_mtime = os.stat(header).st_mtime os.utime(header, (UNCHANGED, UNCHANGED)) builder.Run(DummyOpts(), ['-o', output_dir.GetPath(), '--write-only-new', '1']) self.failUnless(os.path.exists(header)) third_mtime = os.stat(header).st_mtime self.assertTrue(abs(second_mtime - UNCHANGED) > 5) self.assertTrue(abs(third_mtime - UNCHANGED) < 5) output_dir.CleanUp()
def testAllowlistStrings(self): output_dir = util.TempDir({}) builder = build.RcBuilder() class DummyOpts(object): def __init__(self): self.input = util.PathFromRoot( 'grit/testdata/allowlist_strings.grd') self.verbose = False self.extra_verbose = False allowlist_file = util.PathFromRoot('grit/testdata/allowlist.txt') builder.Run(DummyOpts(), ['-o', output_dir.GetPath(), '-w', allowlist_file]) header = output_dir.GetPath('allowlist_test_resources.h') rc = output_dir.GetPath('en_allowlist_test_strings.rc') allowlisted_ids = ['IDS_MESSAGE_ALLOWLISTED'] non_allowlisted_ids = ['IDS_MESSAGE_NOT_ALLOWLISTED'] self._verifyAllowlistedOutput( header, allowlisted_ids, non_allowlisted_ids, ) self._verifyAllowlistedOutput(rc, allowlisted_ids, non_allowlisted_ids, encoding='utf16') output_dir.CleanUp()
def testGenerateDepFile(self): output_dir = util.TempDir({}) builder = build.RcBuilder() class DummyOpts(object): def __init__(self): self.input = util.PathFromRoot('grit/testdata/depfile.grd') self.verbose = False self.extra_verbose = False expected_dep_file = output_dir.GetPath('substitute.grd.d') builder.Run(DummyOpts(), [ '-o', output_dir.GetPath(), '--depdir', output_dir.GetPath(), '--depfile', expected_dep_file ]) self.failUnless(os.path.isfile(expected_dep_file)) with open(expected_dep_file) as f: line = f.readline() (dep_output_file, deps_string) = line.split(': ') deps = deps_string.split(' ') self.failUnlessEqual("default_100_percent.pak", dep_output_file) self.failUnlessEqual(deps, [ util.PathFromRoot('grit/testdata/default_100_percent/a.png'), util.PathFromRoot('grit/testdata/grit_part.grdp'), util.PathFromRoot('grit/testdata/special_100_percent/a.png'), ]) output_dir.CleanUp()
def testGenerateDepFile(self): output_dir = tempfile.mkdtemp() builder = build.RcBuilder() class DummyOpts(object): def __init__(self): self.input = util.PathFromRoot('grit/testdata/substitute.grd') self.verbose = False self.extra_verbose = False expected_dep_file = os.path.join(output_dir, 'substitute.grd.d') builder.Run(DummyOpts(), [ '-o', output_dir, '--depdir', output_dir, '--depfile', expected_dep_file ]) self.failUnless(os.path.isfile(expected_dep_file)) with open(expected_dep_file) as f: line = f.readline() (dep_output_file, deps_string) = line.split(': ') deps = deps_string.split(' ') self.failUnlessEqual("resource.h", dep_output_file) self.failUnlessEqual(1, len(deps)) self.failUnlessEqual( deps[0], util.PathFromRoot('grit/testdata/substitute.xmb'))
def testGenerateDepFileWithResourceIds(self): output_dir = util.TempDir({}) builder = build.RcBuilder() class DummyOpts(object): def __init__(self): self.input = util.PathFromRoot( 'grit/testdata/substitute_no_ids.grd') self.verbose = False self.extra_verbose = False expected_dep_file = output_dir.GetPath('substitute_no_ids.grd.d') builder.Run(DummyOpts(), [ '-f', util.PathFromRoot('grit/testdata/resource_ids'), '-o', output_dir.GetPath(), '--depdir', output_dir.GetPath(), '--depfile', expected_dep_file ]) self.failUnless(os.path.isfile(expected_dep_file)) with open(expected_dep_file) as f: line = f.readline() (dep_output_file, deps_string) = line.split(': ') deps = deps_string.split(' ') self.failUnlessEqual("resource.h", dep_output_file) self.failUnlessEqual(2, len(deps)) self.failUnlessEqual( deps[0], util.PathFromRoot('grit/testdata/substitute.xmb')) self.failUnlessEqual( deps[1], util.PathFromRoot('grit/testdata/resource_ids')) output_dir.CleanUp()
def testFindTranslationsWithSubstitutions(self): # This is a regression test; we had a bug where GRIT would fail to find # messages with substitutions e.g. "Hello [IDS_USER]" where IDS_USER is # another <message>. output_dir = tempfile.mkdtemp() builder = build.RcBuilder() class DummyOpts(object): def __init__(self): self.input = util.PathFromRoot('grit/testdata/substitute.grd') self.verbose = False self.extra_verbose = False builder.Run(DummyOpts(), ['-o', output_dir])
def testGenerateDepFileWithDependOnStamp(self): output_dir = util.TempDir({}) builder = build.RcBuilder() class DummyOpts(object): def __init__(self): self.input = util.PathFromRoot('grit/testdata/substitute.grd') self.verbose = False self.extra_verbose = False expected_dep_file_name = 'substitute.grd.d' expected_stamp_file_name = expected_dep_file_name + '.stamp' expected_dep_file = output_dir.GetPath(expected_dep_file_name) expected_stamp_file = output_dir.GetPath(expected_stamp_file_name) if os.path.isfile(expected_stamp_file): os.remove(expected_stamp_file) builder.Run(DummyOpts(), [ '-o', output_dir.GetPath(), '--depdir', output_dir.GetPath(), '--depfile', expected_dep_file, '--depend-on-stamp' ]) self.failUnless(os.path.isfile(expected_stamp_file)) first_mtime = os.stat(expected_stamp_file).st_mtime # Reset mtime to very old. OLDTIME = 10 os.utime(expected_stamp_file, (OLDTIME, OLDTIME)) builder.Run(DummyOpts(), [ '-o', output_dir.GetPath(), '--depdir', output_dir.GetPath(), '--depfile', expected_dep_file, '--depend-on-stamp' ]) self.failUnless(os.path.isfile(expected_stamp_file)) second_mtime = os.stat(expected_stamp_file).st_mtime # Some OS have a 2s stat resolution window, so can't do a direct comparison. self.assertTrue((second_mtime - OLDTIME) > 5) self.assertTrue(abs(second_mtime - first_mtime) < 5) self.failUnless(os.path.isfile(expected_dep_file)) with open(expected_dep_file) as f: line = f.readline() (dep_output_file, deps_string) = line.split(': ') deps = deps_string.split(' ') self.failUnlessEqual(expected_stamp_file_name, dep_output_file) self.failUnlessEqual(deps, [ util.PathFromRoot('grit/testdata/substitute.xmb'), ]) output_dir.CleanUp()
def testFileIsOutput(self): grd = self.MakeGrd() dirname = util.TempDir({}) try: tool = build.RcBuilder() tool.o = grit_runner.Options() tool.output_directory = dirname.GetPath() tool.res = grd tool.Process() self.failUnless(os.path.isfile(dirname.GetPath('de_GoogleDesktop.adm'))) self.failUnless(os.path.isfile(dirname.GetPath('de_README.txt'))) finally: dirname.CleanUp()
def _RunBuildTest(self, structures, inputs, expected_outputs, skip_rc=False, layout_fallback=''): outputs = '\n'.join( '<output filename="out/%s%s" type="%s" context="%s"%s />' % (context, ext, type, context, layout_fallback) for ext, type in _OUTFILETYPES for context in expected_outputs) infiles = { 'in/in.grd': '''<?xml version="1.0" encoding="UTF-8"?> <grit latest_public_release="0" current_release="1"> <outputs> %s </outputs> <release seq="1"> %s </release> </grit> ''' % (outputs, structures), } for pngpath, pngdata in inputs.items(): normpath = os.path.normpath('in/' + pngpath) infiles[normpath] = pngdata class Options(object): pass with util.TempDir(infiles) as tmp_dir: with tmp_dir.AsCurrentDir(): options = Options() options.input = tmp_dir.GetPath('in/in.grd') options.verbose = False options.extra_verbose = False build.RcBuilder().Run(options, []) for context, expected_data in expected_outputs.items(): self.assertEquals( expected_data, _GetFilesInPak(tmp_dir.GetPath('out/%s.pak' % context))) if not skip_rc: self.assertEquals( expected_data, _GetFilesInRc(tmp_dir.GetPath('out/%s.rc' % context), tmp_dir, infiles))
def testFileIsOutput(self): grd = self.MakeGrd() dirname = tempfile.mkdtemp() try: tool = build.RcBuilder() tool.o = grit_runner.Options() tool.output_directory = dirname tool.res = grd tool.Process() self.failUnless(os.path.isfile( os.path.join(dirname, 'de_GoogleDesktop.adm'))) self.failUnless(os.path.isfile( os.path.join(dirname, 'de_README.txt'))) finally: for f in os.listdir(dirname): os.unlink(os.path.join(dirname, f)) os.rmdir(dirname)
def _Builder(target, source, env): # We fork GRIT into a separate process so we can use more processes between # scons and GRIT. This already runs as separate threads, but because of the # python GIL, all these threads have to share the same process. By using # fork, we can use multiple processes and processors. pid = os.fork() if pid != 0: pid, exit_code = os.waitpid(pid, 0) if exit_code != 0: raise SCons.Errors.BuildError(errstr="see grit error") return try: try: child_exit_code = 0 from grit import grit_runner from grit.tool import build options = grit_runner.Options() # This sets options to default values. options.ReadOptions(['-v']) options.input = _SourceToFile(source) # TODO(joi) Check if we can get the 'verbose' option from the environment. builder = build.RcBuilder() # Get the CPP defines from the environment. for flag in env.get('RCFLAGS', []): if flag.startswith('/D'): flag = flag[2:] name, val = build.ParseDefine(flag) # Only apply to first instance of a given define if name not in builder.defines: builder.defines[name] = val # To ensure that our output files match what we promised SCons, we # use the list of targets provided by SCons and update the file paths in # our .grd input file with the targets. builder.scons_targets = [str(t) for t in target] builder.Run(options, []) except: child_exit_code = -1 finally: # Exit the child process. os._exit(child_exit_code)
def testAllowlistResources(self): output_dir = util.TempDir({}) builder = build.RcBuilder() class DummyOpts(object): def __init__(self): self.input = util.PathFromRoot( 'grit/testdata/allowlist_resources.grd') self.verbose = False self.extra_verbose = False allowlist_file = util.PathFromRoot('grit/testdata/allowlist.txt') builder.Run(DummyOpts(), ['-o', output_dir.GetPath(), '-w', allowlist_file]) header = output_dir.GetPath('allowlist_test_resources.h') map_cc = output_dir.GetPath('allowlist_test_resources_map.cc') map_h = output_dir.GetPath('allowlist_test_resources_map.h') pak = output_dir.GetPath('allowlist_test_resources.pak') # Ensure the resource map header and .pak files exist, but don't verify # their content. self.failUnless(os.path.exists(map_h)) self.failUnless(os.path.exists(pak)) allowlisted_ids = [ 'IDR_STRUCTURE_ALLOWLISTED', 'IDR_STRUCTURE_IN_TRUE_IF_ALLOWLISTED', 'IDR_INCLUDE_ALLOWLISTED', ] non_allowlisted_ids = [ 'IDR_STRUCTURE_NOT_ALLOWLISTED', 'IDR_STRUCTURE_IN_TRUE_IF_NOT_ALLOWLISTED', 'IDR_STRUCTURE_IN_FALSE_IF_ALLOWLISTED', 'IDR_STRUCTURE_IN_FALSE_IF_NOT_ALLOWLISTED', 'IDR_INCLUDE_NOT_ALLOWLISTED', ] for output_file in (header, map_cc): self._verifyAllowlistedOutput( output_file, allowlisted_ids, non_allowlisted_ids, ) output_dir.CleanUp()
def _Builder(target, source, env): print _SourceToFile(source) from grit import grit_runner from grit.tool import build options = grit_runner.Options() # This sets options to default values options.ReadOptions([]) options.input = _SourceToFile(source) # TODO(joi) Check if we can get the 'verbose' option from the environment. builder = build.RcBuilder(defines=_ParseRcFlags(env['RCFLAGS'])[0]) # To ensure that our output files match what we promised SCons, we # use the list of targets provided by SCons and update the file paths in # our .grd input file with the targets. builder.scons_targets = [str(t) for t in target] builder.Run(options, []) return None # success
def testWhitelistResources(self): output_dir = tempfile.mkdtemp() builder = build.RcBuilder() class DummyOpts(object): def __init__(self): self.input = util.PathFromRoot( 'grit/testdata/whitelist_resources.grd') self.verbose = False self.extra_verbose = False whitelist_file = util.PathFromRoot('grit/testdata/whitelist.txt') builder.Run(DummyOpts(), ['-o', output_dir, '-w', whitelist_file]) header = os.path.join(output_dir, 'whitelist_test_resources.h') map_cc = os.path.join(output_dir, 'whitelist_test_resources_map.cc') map_h = os.path.join(output_dir, 'whitelist_test_resources_map.h') pak = os.path.join(output_dir, 'whitelist_test_resources.pak') # Ensure the resource map header and .pak files exist, but don't verify # their content. self.failUnless(os.path.exists(map_h)) self.failUnless(os.path.exists(pak)) whitelisted_ids = [ 'IDR_STRUCTURE_WHITELISTED', 'IDR_STRUCTURE_IN_TRUE_IF_WHITELISTED', 'IDR_INCLUDE_WHITELISTED', ] non_whitelisted_ids = [ 'IDR_STRUCTURE_NOT_WHITELISTED', 'IDR_STRUCTURE_IN_TRUE_IF_NOT_WHITELISTED', 'IDR_STRUCTURE_IN_FALSE_IF_WHITELISTED', 'IDR_STRUCTURE_IN_FALSE_IF_NOT_WHITELISTED', 'IDR_INCLUDE_NOT_WHITELISTED', ] for output_file in (header, map_cc): self._verifyWhitelistedOutput( output_file, whitelisted_ids, non_whitelisted_ids, )
def testOutputAllResourceDefinesFalse(self): output_dir = tempfile.mkdtemp() builder = build.RcBuilder() class DummyOpts(object): def __init__(self): self.input = util.PathFromRoot( 'grit/testdata/whitelist_resources.grd') self.verbose = False self.extra_verbose = False whitelist_file = util.PathFromRoot('grit/testdata/whitelist.txt') builder.Run(DummyOpts(), [ '-o', output_dir, '-w', whitelist_file, '--no-output-all-resource-defines', ]) header = os.path.join(output_dir, 'whitelist_test_resources.h') map_cc = os.path.join(output_dir, 'whitelist_test_resources_map.cc') whitelisted_ids = [ 'IDR_STRUCTURE_WHITELISTED', 'IDR_STRUCTURE_IN_TRUE_IF_WHITELISTED', 'IDR_INCLUDE_WHITELISTED', ] non_whitelisted_ids = [ 'IDR_STRUCTURE_NOT_WHITELISTED', 'IDR_STRUCTURE_IN_TRUE_IF_NOT_WHITELISTED', 'IDR_STRUCTURE_IN_FALSE_IF_WHITELISTED', 'IDR_STRUCTURE_IN_FALSE_IF_NOT_WHITELISTED', 'IDR_INCLUDE_NOT_WHITELISTED', ] for output_file in (header, map_cc): self._verifyWhitelistedOutput( output_file, whitelisted_ids, non_whitelisted_ids, )
def testGenerateDepFile(self): output_dir = tempfile.mkdtemp() builder = build.RcBuilder() class DummyOpts(object): def __init__(self): self.input = util.PathFromRoot('grit/testdata/substitute.grd') self.verbose = False self.extra_verbose = False builder.Run(DummyOpts(), ['-o', output_dir, '--dep-dir', output_dir]) expected_dep_file = os.path.join(output_dir, 'substitute.grd.d') self.failUnless(os.path.isfile(expected_dep_file)) with open(expected_dep_file) as f: line = f.readline() (dep_file_name, deps_string) = line.split(': ') deps = deps_string.split(' ') self.failUnlessEqual(os.path.abspath(expected_dep_file), os.path.abspath(os.path.join(output_dir, dep_file_name)), "depfile should refer to itself as the depended upon file") self.failUnlessEqual(1, len(deps)) self.failUnlessEqual(deps[0], util.PathFromRoot('grit/testdata/substitute.xmb'))
def _RunBuildTest(self, structures, pngfiles, contexts_and_results): outputs = '\n'.join( '<output filename="out/%s%s" type="%s" context="%s" />' % (context, ext, type, context) for ext, type in _OUTFILETYPES for context, expected_includes in contexts_and_results) infiles = { 'in/in.grd': '''<?xml version="1.0" encoding="UTF-8"?> <grit latest_public_release="0" current_release="1"> <outputs> %s </outputs> <release seq="1"> %s </release> </grit> ''' % (outputs, structures), } for pngpath in pngfiles: infiles['in/' + pngpath] = 'CONTENTS_OF(%s)' % pngpath class Options(object): pass with util.TempDir(infiles) as tmp_dir: with tmp_dir.AsCurrentDir(): options = Options() options.input = tmp_dir.GetPath('in/in.grd') options.verbose = False options.extra_verbose = False build.RcBuilder().Run(options, []) for context, expected_includes in contexts_and_results: self.assertEquals( _GetFilesInPak(tmp_dir.GetPath('out/%s.pak' % context)), expected_includes) self.assertEquals( _GetFilesInRc(tmp_dir.GetPath('out/%s.rc' % context)), [tmp_dir.GetPath('in/' + x) for x in expected_includes])