Chromium Code Reviews| Index: infra/tools/new_tool/test/new_tool_test.py |
| diff --git a/infra/tools/new_tool/test/new_tool_test.py b/infra/tools/new_tool/test/new_tool_test.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2274341e4243b3d69f044b82506954f0ae051dd6 |
| --- /dev/null |
| +++ b/infra/tools/new_tool/test/new_tool_test.py |
| @@ -0,0 +1,76 @@ |
| +# Copyright 2015 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import os |
| +import unittest |
| + |
| +from infra.tools.new_tool import new_tool |
| +import infra_libs |
| + |
| + |
| +DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data') |
| + |
| + |
| +class TestGenerateToolFiles(unittest.TestCase): |
| + def test_generate(self): |
| + with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir: |
| + toolname = 'test_tool' |
| + new_tool.generate_tool_files('test_tool', tempdir) |
| + self.assertTrue(os.path.isdir(os.path.join(tempdir, toolname))) |
| + self.assertTrue(os.path.isfile(os.path.join(tempdir, toolname, |
| + '__init__.py'))) |
| + self.assertTrue(os.path.isfile(os.path.join(tempdir, toolname, |
| + '__main__.py'))) |
| + self.assertTrue(os.path.isdir(os.path.join(tempdir, toolname, 'test'))) |
| + self.assertTrue(os.path.isfile(os.path.join(tempdir, toolname, 'test', |
| + '__init__.py'))) |
| + |
| + def test_no_overwrite_tool(self): |
| + with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir: |
| + self.assertFalse(new_tool.generate_tool_files('test_tool', tempdir)) |
| + self.assertTrue(new_tool.generate_tool_files('test_tool', tempdir)) |
| + |
| + def test_missing_destination_dir(self): |
| + with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir: |
| + # When destination directory does not exist, just do nothing and return |
| + # a non-zero value. |
| + self.assertTrue(new_tool.generate_tool_files( |
| + 'test_tool', os.path.join(tempdir, 'missing-directory'))) |
| + |
| + |
| +class TestGeneratePythonFile(unittest.TestCase): |
| + def test_generate_empty_file(self): |
| + with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir: |
| + filename = new_tool.generate_python_file(tempdir, 'test.py', None) |
| + self.assertTrue(os.path.isfile(filename)) |
| + # Read the content and do some basic check |
| + with open(filename, 'r') as f: |
| + content = f.read() |
| + self.assertTrue(content.startswith(new_tool.COPYRIGHT_NOTICE)) |
| + |
| + # Now make sure the file is not overwritten |
| + new_content = 'other content' |
| + with open(filename, 'w') as f: |
| + f.write(new_content) |
| + filename2 = new_tool.generate_python_file(tempdir, 'test.py', None) |
| + self.assertEqual(filename, filename2) |
| + with open(filename, 'r') as f: |
| + content = f.read() |
| + self.assertEqual(content, new_content) |
| + |
| + def test_generate_file_from_template(self): |
| + with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir: |
| + filename = new_tool.generate_python_file( |
| + tempdir, 'test.py', 'test_template', template_dir=DATA_DIR, |
| + value='Passed string.') |
| + self.assertTrue(os.path.isfile(filename)) |
| + |
| + # Read the content |
| + with open(filename, 'r') as f: |
| + content = f.read() |
| + |
| + expected_content = (new_tool.COPYRIGHT_NOTICE |
| + + 'This is a template used by the test suite.\n' |
| + 'Passed string.\n') |
| + 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.
|