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

Side by Side Diff: infra/tools/new_tool/test/new_tool_test.py

Issue 1172053003: Created infra/tools/new_tool (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Added tests Created 5 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
OLDNEW
(Empty)
1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import os
6 import unittest
7
8 from infra.tools.new_tool import new_tool
9 import infra_libs
10
11
12 DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')
13
14
15 class TestGenerateToolFiles(unittest.TestCase):
16 def test_generate(self):
17 with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir:
18 toolname = 'test_tool'
19 new_tool.generate_tool_files('test_tool', tempdir)
20 self.assertTrue(os.path.isdir(os.path.join(tempdir, toolname)))
21 self.assertTrue(os.path.isfile(os.path.join(tempdir, toolname,
22 '__init__.py')))
23 self.assertTrue(os.path.isfile(os.path.join(tempdir, toolname,
24 '__main__.py')))
25 self.assertTrue(os.path.isdir(os.path.join(tempdir, toolname, 'test')))
26 self.assertTrue(os.path.isfile(os.path.join(tempdir, toolname, 'test',
27 '__init__.py')))
28
29 def test_no_overwrite_tool(self):
30 with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir:
31 self.assertFalse(new_tool.generate_tool_files('test_tool', tempdir))
32 self.assertTrue(new_tool.generate_tool_files('test_tool', tempdir))
33
34 def test_missing_destination_dir(self):
35 with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir:
36 # When destination directory does not exist, just do nothing and return
37 # a non-zero value.
38 self.assertTrue(new_tool.generate_tool_files(
39 'test_tool', os.path.join(tempdir, 'missing-directory')))
40
41
42 class TestGeneratePythonFile(unittest.TestCase):
43 def test_generate_empty_file(self):
44 with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir:
45 filename = new_tool.generate_python_file(tempdir, 'test.py', None)
46 self.assertTrue(os.path.isfile(filename))
47 # Read the content and do some basic check
48 with open(filename, 'r') as f:
49 content = f.read()
50 self.assertTrue(content.startswith(new_tool.COPYRIGHT_NOTICE))
51
52 # Now make sure the file is not overwritten
53 new_content = 'other content'
54 with open(filename, 'w') as f:
55 f.write(new_content)
56 filename2 = new_tool.generate_python_file(tempdir, 'test.py', None)
57 self.assertEqual(filename, filename2)
58 with open(filename, 'r') as f:
59 content = f.read()
60 self.assertEqual(content, new_content)
61
62 def test_generate_file_from_template(self):
63 with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir:
64 filename = new_tool.generate_python_file(
65 tempdir, 'test.py', 'test_template', template_dir=DATA_DIR,
66 value='Passed string.')
67 self.assertTrue(os.path.isfile(filename))
68
69 # Read the content
70 with open(filename, 'r') as f:
71 content = f.read()
72
73 expected_content = (new_tool.COPYRIGHT_NOTICE
74 + 'This is a template used by the test suite.\n'
75 'Passed string.\n')
76 self.assertEqual(content, expected_content)
Sergey Berezin 2015/06/11 17:33:16 nit: this is effectively an encoded expectation. M
pgervais 2015/06/11 17:46:54 Several reasons: - this expectation is not suppose
Sergey Berezin 2015/06/11 17:55:06 Acknowledged.
OLDNEW
« infra/tools/new_tool/new_tool.py ('K') | « infra/tools/new_tool/test/data/test_template.template ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698