| OLD | NEW |
| (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 argparse |
| 6 import os |
| 7 import unittest |
| 8 |
| 9 from infra.tools.new_tool import new_tool |
| 10 import infra_libs |
| 11 |
| 12 |
| 13 DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data') |
| 14 |
| 15 |
| 16 class TestArgParseOptions(unittest.TestCase): |
| 17 def test_add_argparse_options_smoke(self): |
| 18 parser = argparse.ArgumentParser() |
| 19 new_tool.add_argparse_options(parser) |
| 20 |
| 21 |
| 22 class TestGenerateToolFiles(unittest.TestCase): |
| 23 def test_generate(self): |
| 24 with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir: |
| 25 toolname = 'test_tool' |
| 26 new_tool.generate_tool_files('test_tool', tempdir) |
| 27 self.assertTrue(os.path.isdir(os.path.join(tempdir, toolname))) |
| 28 self.assertTrue(os.path.isfile(os.path.join(tempdir, toolname, |
| 29 '__init__.py'))) |
| 30 self.assertTrue(os.path.isfile(os.path.join(tempdir, toolname, |
| 31 '__main__.py'))) |
| 32 self.assertTrue(os.path.isdir(os.path.join(tempdir, toolname, 'test'))) |
| 33 self.assertTrue(os.path.isfile(os.path.join(tempdir, toolname, 'test', |
| 34 '__init__.py'))) |
| 35 |
| 36 def test_no_overwrite_tool(self): |
| 37 with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir: |
| 38 self.assertFalse(new_tool.generate_tool_files('test_tool', tempdir)) |
| 39 self.assertTrue(new_tool.generate_tool_files('test_tool', tempdir)) |
| 40 |
| 41 def test_missing_destination_dir(self): |
| 42 with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir: |
| 43 # When destination directory does not exist, just do nothing and return |
| 44 # a non-zero value. |
| 45 self.assertTrue(new_tool.generate_tool_files( |
| 46 'test_tool', os.path.join(tempdir, 'missing-directory'))) |
| 47 |
| 48 |
| 49 class TestGeneratePythonFile(unittest.TestCase): |
| 50 def test_generate_empty_file(self): |
| 51 with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir: |
| 52 filename = new_tool.generate_python_file(tempdir, 'test.py', None) |
| 53 self.assertTrue(os.path.isfile(filename)) |
| 54 # Read the content and do some basic check |
| 55 with open(filename, 'r') as f: |
| 56 content = f.read() |
| 57 self.assertTrue(content.startswith(new_tool.COPYRIGHT_NOTICE)) |
| 58 |
| 59 # Now make sure the file is not overwritten |
| 60 new_content = 'other content' |
| 61 with open(filename, 'w') as f: |
| 62 f.write(new_content) |
| 63 filename2 = new_tool.generate_python_file(tempdir, 'test.py', None) |
| 64 self.assertEqual(filename, filename2) |
| 65 with open(filename, 'r') as f: |
| 66 content = f.read() |
| 67 self.assertEqual(content, new_content) |
| 68 |
| 69 def test_generate_file_from_template(self): |
| 70 with infra_libs.temporary_directory(prefix='new-tool-test-') as tempdir: |
| 71 filename = new_tool.generate_python_file( |
| 72 tempdir, 'test.py', 'test_template', template_dir=DATA_DIR, |
| 73 value='Passed string.') |
| 74 self.assertTrue(os.path.isfile(filename)) |
| 75 |
| 76 # Read the content |
| 77 with open(filename, 'r') as f: |
| 78 content = f.read() |
| 79 |
| 80 expected_content = (new_tool.COPYRIGHT_NOTICE |
| 81 + 'This is a template used by the test suite.\n' |
| 82 'Passed string.\n') |
| 83 self.assertEqual(content, expected_content) |
| OLD | NEW |