Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(214)

Side by Side Diff: native_client_sdk/src/project_templates/init_project_test.py

Issue 16972010: Dropping nacl sdk dead code (documentation, project_template). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 init_project.py."""
7
8 __author__ = 'mlinck@google.com (Michael Linck)'
9
10 import fileinput
11 import os
12 import shutil
13 import sys
14 import unittest
15 import mox
16 import init_project
17
18
19 def TestMock(file_path, open_func):
20 temp_file = open_func(file_path)
21 temp_file.close()
22
23
24 class TestGlobalFunctions(unittest.TestCase):
25 """Class for test cases to cover globally declared helper functions."""
26
27 def testGetCamelCaseName(self):
28 output = init_project.GetCamelCaseName('camel_case_name')
29 self.assertEqual(output, 'CamelCaseName')
30 output = init_project.GetCamelCaseName('print_42')
31 self.assertEqual(output, 'Print42')
32
33 def testGetCodeDirectory(self):
34 output = init_project.GetCodeDirectory(True, '')
35 self.assertEqual(output, 'c')
36 output = init_project.GetCodeDirectory(False, '')
37 self.assertEqual(output, 'cc')
38 output = init_project.GetCodeDirectory(True, 'test')
39 self.assertEqual(output, 'test/c')
40 output = init_project.GetCodeDirectory(False, 'test')
41 self.assertEqual(output, 'test/cc')
42
43 def testGetCodeSourceFiles(self):
44 output = init_project.GetCodeSourceFiles(False)
45 self.assertEqual(output, init_project.CC_SOURCE_FILES)
46 output = init_project.GetCodeSourceFiles(True)
47 self.assertEqual(output, init_project.C_SOURCE_FILES)
48
49 def testGetCommonSourceFiles(self):
50 output = init_project.GetCommonSourceFiles()
51 expected_output_linux = init_project.COMMON_PROJECT_FILES
52 expected_output_windows = init_project.COMMON_PROJECT_FILES
53 expected_output_windows.extend(['scons.bat'])
54 linux_match = (output == expected_output_linux)
55 windows_match = (output == expected_output_windows)
56 passed = (linux_match | windows_match)
57 self.assertTrue(passed)
58
59 def testGetHTMLDirectory(self):
60 output = init_project.GetHTMLDirectory('')
61 self.assertEqual(output, 'html')
62 output = init_project.GetHTMLDirectory('test')
63 self.assertEqual(output, 'test/html')
64
65 def testGetHTMLSourceFiles(self):
66 output = init_project.GetHTMLSourceFiles()
67 self.assertEqual(output, init_project.HTML_FILES)
68
69 def testGetTargetFileName(self):
70 output = init_project.GetTargetFileName('project_file.cc', 'bonkers')
71 self.assertEqual(output, 'bonkers.cc')
72 output = init_project.GetTargetFileName('constant.html', 'bonkers')
73 self.assertEqual(output, 'constant.html')
74
75 def testParseArguments(self):
76 output = init_project.ParseArguments(['-n', 'test_name', '-d', 'test/dir'])
77 self.assertEqual(output.is_c_project, False)
78 self.assertEqual(output.project_name, 'test_name')
79 self.assertEqual(output.project_directory, 'test/dir')
80 output = init_project.ParseArguments(['-n', 'test_name_2', '-c'])
81 self.assertEqual(output.is_c_project, True)
82 self.assertEqual(output.project_name, 'test_name_2')
83 self.assertEqual(output.project_directory,
84 init_project.GetDefaultProjectDir())
85
86
87 class TestProjectInitializer(unittest.TestCase):
88 """Class for test cases to cover public interface of ProjectInitializer."""
89
90 def __init__(self):
91 unittest.TestCase.__init__(self)
92 self.os_mock = None
93 self.fileinput_mock = None
94 self.sys_mock = None
95 self.shutil_mock = None
96
97 def setUp(self):
98 self.script_dir = os.path.abspath(os.path.dirname(__file__))
99 self.nacl_src_dir = os.getenv('NACL_SDK_ROOT', None)
100 self.mock_factory = mox.Mox()
101 # This mock is only valid for initialization and will be overwritten
102 # after ward by self.os_mock.
103 init_path_mock = self.mock_factory.CreateMock(os.path)
104 init_path_mock.join('test/dir', 'test_project').AndReturn(
105 'test/dir/test_project')
106 init_path_mock.exists('test/dir/test_project').AndReturn(False)
107 init_os_mock = self.mock_factory.CreateMock(os)
108 init_os_mock.path = init_path_mock
109 init_os_mock.makedirs('test/dir/test_project')
110 self.mock_factory.ReplayAll()
111 self.test_subject = init_project.ProjectInitializer(
112 # True => is C project, False => is vs project
113 True, False, 'test_project', 'test/dir', 'pepper_14', self.script_dir,
114 self.nacl_src_dir, os_resource=init_os_mock)
115 self.mock_factory.VerifyAll()
116 self.InitializeResourceMocks()
117
118 def InitializeResourceMocks(self):
119 """Can be called multiple times if multiple functions need to be tested."""
120 self.fileinput_mock = self.mock_factory.CreateMock(fileinput)
121 self.test_subject.fileinput = self.fileinput_mock
122 self.os_mock = self.mock_factory.CreateMock(os)
123 self.test_subject.os = self.os_mock
124 self.shutil_mock = self.mock_factory.CreateMock(shutil)
125 self.test_subject.shutil = self.shutil_mock
126 self.sys_mock = self.mock_factory.CreateMock(sys)
127 self.test_subject.sys = self.sys_mock
128
129 def testCopyAndRenameFiles(self):
130 self.shutil_mock.copy('source/dir/normal_name.txt',
131 'test/dir/test_project/normal_name.txt')
132 self.shutil_mock.copy('source/dir/project_file.txt',
133 'test/dir/test_project/test_project.txt')
134 self.os_mock.path = os.path
135 self.mock_factory.ReplayAll()
136 self.test_subject.CopyAndRenameFiles(
137 'source/dir', ['normal_name.txt', 'project_file.txt'])
138 self.mock_factory.VerifyAll()
139
140 def testPrepareDirectoryContent(self):
141 self.shutil_mock.copy(
142 '%s/c/build.scons' % self.script_dir,
143 'test/dir/test_project/build.scons')
144 self.shutil_mock.copy(
145 '%s/c/project_file.c' % self.script_dir,
146 'test/dir/test_project/test_project.c')
147 self.shutil_mock.copy(
148 '%s/html/project_file.html' % self.script_dir,
149 'test/dir/test_project/test_project.html')
150 self.shutil_mock.copy(
151 '%s/scons' % self.script_dir,
152 'test/dir/test_project/scons')
153 self.shutil_mock.copy(
154 '%s/scons.bat' % self.script_dir,
155 'test/dir/test_project/scons.bat')
156 self.os_mock.path = os.path
157 self.mock_factory.ReplayAll()
158 self.test_subject.PrepareDirectoryContent()
159 self.mock_factory.VerifyAll()
160
161 def testPrepareFileContent(self):
162 self.testCopyAndRenameFiles()
163 # We need a new set of resource mocks since the old ones have already been
164 # used.
165 self.InitializeResourceMocks()
166 path_mock = self.mock_factory.CreateMock(os.path)
167 stdout_mock = self.mock_factory.CreateMock(sys.stdout)
168 self.os_mock.path = path_mock
169 path_mock.abspath(self.nacl_src_dir).AndReturn(self.nacl_src_dir)
170 self.fileinput_mock.input(
171 'test/dir/test_project/normal_name.txt',
172 inplace=1, mode='U').AndReturn(
173 ['A line with <PROJECT_NAME>.',
174 'A line with <ProjectName>.',
175 'A line with <NACL_SDK_ROOT>.',
176 'A line with <NACL_PLATFORM>.'])
177 stdout_mock.write('A line with test_project.')
178 stdout_mock.write('A line with <ProjectName>.')
179 stdout_mock.write('A line with <NACL_SDK_ROOT>.')
180 stdout_mock.write('A line with <NACL_PLATFORM>.')
181 self.fileinput_mock.input(
182 'test/dir/test_project/normal_name.txt',
183 inplace=1, mode='U').AndReturn(
184 ['A line with test_project.',
185 'A line with <ProjectName>.',
186 'A line with <NACL_SDK_ROOT>.',
187 'A line with <NACL_PLATFORM>.'])
188 stdout_mock.write('A line with test_project.')
189 stdout_mock.write('A line with TestProject.')
190 stdout_mock.write('A line with <NACL_SDK_ROOT>.')
191 stdout_mock.write('A line with <NACL_PLATFORM>.')
192 self.fileinput_mock.input(
193 'test/dir/test_project/normal_name.txt',
194 inplace=1, mode='U').AndReturn(
195 ['A line with test_project.',
196 'A line with TestProject.',
197 'A line with <NACL_SDK_ROOT>.',
198 'A line with <NACL_PLATFORM>.'])
199 stdout_mock.write('A line with test_project.')
200 stdout_mock.write('A line with TestProject.')
201 stdout_mock.write('A line with %s.' % self.nacl_src_dir)
202 stdout_mock.write('A line with <NACL_PLATFORM>.')
203 self.fileinput_mock.input(
204 'test/dir/test_project/normal_name.txt',
205 inplace=1, mode='U').AndReturn(
206 ['A line with test_project.',
207 'A line with TestProject.',
208 'A line with some/dir.',
209 'A line with <NACL_PLATFORM>.'])
210 stdout_mock.write('A line with test_project.')
211 stdout_mock.write('A line with TestProject.')
212 stdout_mock.write('A line with some/dir.')
213 stdout_mock.write('A line with pepper_14.')
214 # One multi-line file with different replacements has already been mocked
215 # so we make this next test simpler.
216 self.fileinput_mock.input(
217 'test/dir/test_project/test_project.txt',
218 inplace=1, mode='U').AndReturn(['A line with no replaceable text.'])
219 stdout_mock.write('A line with no replaceable text.')
220 self.fileinput_mock.input(
221 'test/dir/test_project/test_project.txt',
222 inplace=1, mode='U').AndReturn(['A line with no replaceable text.'])
223 stdout_mock.write('A line with no replaceable text.')
224 self.fileinput_mock.input(
225 'test/dir/test_project/test_project.txt',
226 inplace=1, mode='U').AndReturn(['A line with no replaceable text.'])
227 stdout_mock.write('A line with no replaceable text.')
228 self.fileinput_mock.input(
229 'test/dir/test_project/test_project.txt',
230 inplace=1, mode='U').AndReturn(['A line with no replaceable text.'])
231 stdout_mock.write('A line with no replaceable text.')
232 self.sys_mock.stdout = stdout_mock
233 self.mock_factory.ReplayAll()
234 self.test_subject.PrepareFileContent()
235 self.mock_factory.VerifyAll()
236
237 def testReplaceInFile(self):
238 self.fileinput_mock.input('test/path', inplace=1, mode='U').AndReturn(
239 ['A sentence replace_me.'])
240 stdout_mock = self.mock_factory.CreateMock(sys.stdout)
241 stdout_mock.write('A sentence with_this.')
242 self.sys_mock.stdout = stdout_mock
243 self.mock_factory.ReplayAll()
244 self.test_subject.ReplaceInFile('test/path', 'replace_me', 'with_this')
245 self.mock_factory.VerifyAll()
246
247
248 def RunTests():
249 # It's possible to do this with one suite instead of two, but then it's
250 # harder to read the test output.
251 return_value = 1
252 suite_one = unittest.TestLoader().loadTestsFromTestCase(TestGlobalFunctions)
253 result_one = unittest.TextTestRunner(verbosity=2).run(suite_one)
254 suite_two = unittest.TestLoader().loadTestsFromTestCase(
255 TestProjectInitializer)
256 result_two = unittest.TextTestRunner(verbosity=2).run(suite_two)
257 if result_one.wasSuccessful() and result_two.wasSuccessful():
258 return_value = 0
259 return return_value
260
261
262 if __name__ == '__main__':
263 sys.exit(RunTests())
OLDNEW
« no previous file with comments | « native_client_sdk/src/project_templates/init_project.py ('k') | native_client_sdk/src/project_templates/scons » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698