1#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6'''Unit tests for the 'grit build' tool.
7'''
8
9import os
10import sys
11import tempfile
12if __name__ == '__main__':
13  sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
14
15import unittest
16
17from grit import util
18from grit.tool import build
19
20
21class BuildUnittest(unittest.TestCase):
22
23  def testFindTranslationsWithSubstitutions(self):
24    # This is a regression test; we had a bug where GRIT would fail to find
25    # messages with substitutions e.g. "Hello [IDS_USER]" where IDS_USER is
26    # another <message>.
27    output_dir = tempfile.mkdtemp()
28    builder = build.RcBuilder()
29    class DummyOpts(object):
30      def __init__(self):
31        self.input = util.PathFromRoot('grit/testdata/substitute.grd')
32        self.verbose = False
33        self.extra_verbose = False
34    builder.Run(DummyOpts(), ['-o', output_dir])
35
36  def testGenerateDepFile(self):
37    output_dir = tempfile.mkdtemp()
38    builder = build.RcBuilder()
39    class DummyOpts(object):
40      def __init__(self):
41        self.input = util.PathFromRoot('grit/testdata/substitute.grd')
42        self.verbose = False
43        self.extra_verbose = False
44    expected_dep_file = os.path.join(output_dir, 'substitute.grd.d')
45    builder.Run(DummyOpts(), ['-o', output_dir,
46                              '--depdir', output_dir,
47                              '--depfile', expected_dep_file])
48
49    self.failUnless(os.path.isfile(expected_dep_file))
50    with open(expected_dep_file) as f:
51      line = f.readline()
52      (dep_output_file, deps_string) = line.split(': ')
53      deps = deps_string.split(' ')
54
55      self.failUnlessEqual("resource.h", dep_output_file)
56      self.failUnlessEqual(1, len(deps))
57      self.failUnlessEqual(deps[0],
58          util.PathFromRoot('grit/testdata/substitute.xmb'))
59
60  def testAssertOutputs(self):
61    output_dir = tempfile.mkdtemp()
62    class DummyOpts(object):
63      def __init__(self):
64        self.input = util.PathFromRoot('grit/testdata/substitute.grd')
65        self.verbose = False
66        self.extra_verbose = False
67
68    # Incomplete output file list should fail.
69    builder_fail = build.RcBuilder()
70    self.failUnlessEqual(2,
71        builder_fail.Run(DummyOpts(), [
72            '-o', output_dir,
73            '-a', os.path.abspath(
74                os.path.join(output_dir, 'en_generated_resources.rc'))]))
75
76    # Complete output file list should succeed.
77    builder_ok = build.RcBuilder()
78    self.failUnlessEqual(0,
79        builder_ok.Run(DummyOpts(), [
80            '-o', output_dir,
81            '-a', os.path.abspath(
82                os.path.join(output_dir, 'en_generated_resources.rc')),
83            '-a', os.path.abspath(
84                os.path.join(output_dir, 'sv_generated_resources.rc')),
85            '-a', os.path.abspath(
86                os.path.join(output_dir, 'resource.h'))]))
87
88if __name__ == '__main__':
89  unittest.main()
90