def test_load_with_invalid_plugin(self): def stub_exists(path): exists = [ os.path.abspath(path) for path in [ '/dummy/plugins/invalid1', '/dummy/plugins/invalid1/__init__.py', '/dummy/plugins/invalid1/command.py', '/dummy/plugins/invalid2', '/dummy/plugins/invalid2/__init__.py', '/dummy/plugins/invalid2/command.py', '/dummy/plugins/__init__.py' ] ] return os.path.abspath(path) in exists def stub_isdir(path): dirs = [ os.path.abspath(path) for path in ['/dummy/plugins/invalid1', '/dummy/plugins/invalid2'] ] return os.path.abspath(path) in dirs mock_tree = mock.MagicMock() mock_module = StubInvalidPlugin() dummy_plugin_dir = os.path.normcase('/dummy/plugins') with mock.patch('os.listdir', return_value=['invalid1', 'invalid2']), \ mock.patch('googkit.lib.path.plugin', return_value=dummy_plugin_dir), \ mock.patch('os.path.exists', side_effect=stub_exists), \ mock.patch('os.path.isdir', side_effect=stub_isdir), \ mock.patch('googkit.lib.plugin.__import__', create=True, return_value=mock_module): with self.assertRaises(GoogkitError): googkit.lib.plugin.load(mock_tree)
def test_load_without_init_file(self): def stub_exists(path): exists = [ os.path.abspath(path) for path in [ '/dummy/plugins/no_init1', '/dummy/plugins/no_init1/command.py', '/dummy/plugins/no_init2', '/dummy/plugins/no_init2/__init__.py', '/dummy/plugins/no_init2/command.py', '/dummy/plugins/__init__.py' ] ] return os.path.abspath(path) in exists def stub_isdir(path): dirs = [ os.path.abspath(path) for path in ['/dummy/plugins/no_init1', '/dummy/plugins/no_init2'] ] return os.path.abspath(path) in dirs mock_tree = mock.MagicMock() mock_module = StubPlugin() mock_module.register = mock.MagicMock() dummy_plugin_dir = os.path.normcase('/dummy/plugins') with mock.patch('os.listdir', return_value=['no_init1', 'no_init2']), \ mock.patch('googkit.lib.path.plugin', return_value=dummy_plugin_dir), \ mock.patch('os.path.exists', side_effect=stub_exists), \ mock.patch('os.path.isdir', side_effect=stub_isdir), \ mock.patch('googkit.lib.plugin.__import__', create=True, return_value=mock_module): # should not raise any error googkit.lib.plugin.load(mock_tree)
def test_load_config(self): class ConcreteCommandNeedsConfig(Command): @classmethod def needs_project_config(cls): return True with mock.patch('googkit.lib.path.user_config') as mock_usr_cfg, \ mock.patch('googkit.lib.path.default_config') as mock_def_cfg, \ mock.patch('googkit.lib.path.project_config') as mock_proj_cfg, \ mock.patch('googkit.commands.command.Config') as MockConfig: mock_usr_cfg.return_value = '/dummy/.googkit' mock_def_cfg.return_value = '/dummy/default.cfg' mock_proj_cfg.return_value = '/dummy/googkit.cfg' MockConfig.load.return_value = mock.MagicMock() env = StubEnvironment() cmd = ConcreteCommandNeedsConfig(env) result = cmd._load_config() self.assertIsNotNone(result) self.assertTrue(mock_usr_cfg.called) self.assertTrue(mock_def_cfg.called) self.assertTrue(mock_proj_cfg.called) self.assertTrue(MockConfig.return_value.load.called)
def test_run_internal(self): self.cmd.lint = mock.MagicMock() with mock.patch('googkit.lib.path.project_root'), \ mock.patch('googkit.commands.lint.working_directory'): self.cmd.run_internal() self.cmd.lint.assert_called_once_with()
def test_modify_source_map(self): # Data will be given by open() stub_source_map = { 'sourceRoot': 'change me', 'dummy': 'do not change me' } expected = { 'sourceRoot': os.path.relpath(StubConfig.PROJECT_DIR, StubConfig.DEBUG_DIR), 'dummy': 'do not change me' } mock_open = mock.mock_open() dummy_source_map_file = 'DUMMY' with mock.patch('googkit.commands.build.json') as mock_json, \ mock.patch('googkit.commands.build.open', new=mock_open, create=True): mock_open.return_value.__enter__.return_value = dummy_source_map_file mock_json.load.return_value = stub_source_map self.cmd.modify_source_map(dummy_source_map_file, StubConfig.PROJECT_DIR) mock_json.load.assert_called_once_with(dummy_source_map_file) self.assertEqual(mock_json.dump.call_args[0][0], expected) self.assertEqual(mock_json.dump.call_count, 1)
def test_run_with_no_target_dir(self): dirpath = os.path.join(os.sep, 'dir1', 'dir2') with mock.patch('os.path.exists', return_value=False), \ mock.patch('googkit.lib.clone._clone') as mock_clone: googkit.lib.clone.run('https://exmaple.com/example.git', dirpath) self.assertTrue(mock_clone.called)
def test_run(self): dirpath = os.path.join(os.sep, 'dir1', 'dir2') with mock.patch('os.path.exists', return_value=True), \ mock.patch('googkit.lib.clone._pull') as mock_pull: googkit.lib.clone.run('https://exmaple.com/example.git', dirpath) self.assertTrue(mock_pull.called)
def test_plugin_with_directory_missing(self): dummy_googkit_root = os.path.normcase('/dummy/usr/local/googkit') with mock.patch('googkit.lib.path.googkit_root', return_value=dummy_googkit_root), \ mock.patch('os.path.isdir', return_value=False): with self.assertRaises(GoogkitError): self.assertPathEqual(googkit.lib.path.plugin(), '/dummy/usr/local/googkit/plugins')
def test_run_internal(self): self.env.argument = self._arg('googkit.py _candidates deps') with mock.patch('sys.stdout', new_callable=StubStdout) as mock_stdout: self.cmd.run_internal() candidates = mock_stdout.getvalue().split('\n') self.assertFalse('deps' in candidates) self.assertTrue('update' in candidates) self.assertFalse('--verbose' in candidates) self.env.argument = self._arg('googkit.py _candidates deps update') with mock.patch('sys.stdout', new_callable=StubStdout) as mock_stdout: self.cmd.run_internal() candidates = mock_stdout.getvalue().split('\n') self.assertFalse('deps' in candidates) self.assertFalse('update' in candidates) self.assertTrue('--verbose' in candidates) self.env.argument = self._arg( 'googkit.py _candidates deps update --verbose') with mock.patch('sys.stdout', new_callable=StubStdout) as mock_stdout: self.cmd.run_internal() candidates = mock_stdout.getvalue().split('\n') self.assertFalse('deps' in candidates) self.assertFalse('update' in candidates) self.assertFalse('--verbose' in candidates)
def test_which(self): os_pathsep = ':' os_environ = { 'PATH': os_pathsep.join([ '/usr/local/bin', '/usr/bin' ]) } def os_path_exists(path): return os.path.abspath(path) in [ '/usr', '/usr/bin/cmd0', '/usr/local', '/usr/local/bin', '/usr/local/bin/cmd1', ] def executable(path): return path in [ '/usr/bin/cmd0', '/usr/local/bin/cmd1', ] with mock.patch('os.environ', new=os_environ), \ mock.patch('os.pathsep', new=os_pathsep), \ mock.patch('os.path.exists', side_effect=os_path_exists), \ mock.patch('googkit.lib.file.executable', side_effect=executable): self.assertTrue( googkit.lib.file.which('cmd0')) self.assertFalse( googkit.lib.file.which('bluerose'))
def test_load_without_init_file(self): def stub_exists(path): exists = [os.path.abspath(path) for path in [ '/dummy/plugins/no_init1', '/dummy/plugins/no_init1/command.py', '/dummy/plugins/no_init2', '/dummy/plugins/no_init2/__init__.py', '/dummy/plugins/no_init2/command.py', '/dummy/plugins/__init__.py' ]] return os.path.abspath(path) in exists def stub_isdir(path): dirs = [os.path.abspath(path) for path in [ '/dummy/plugins/no_init1', '/dummy/plugins/no_init2' ]] return os.path.abspath(path) in dirs mock_tree = mock.MagicMock() mock_module = StubPlugin() mock_module.register = mock.MagicMock() dummy_plugin_dir = os.path.normcase('/dummy/plugins') with mock.patch('os.listdir', return_value=['no_init1', 'no_init2']), \ mock.patch('googkit.lib.path.plugin', return_value=dummy_plugin_dir), \ mock.patch('os.path.exists', side_effect=stub_exists), \ mock.patch('os.path.isdir', side_effect=stub_isdir), \ mock.patch('googkit.lib.plugin.__import__', create=True, return_value=mock_module): # should not raise any error googkit.lib.plugin.load(mock_tree)
def test_load_with_invalid_plugin(self): def stub_exists(path): exists = [os.path.abspath(path) for path in [ '/dummy/plugins/invalid1', '/dummy/plugins/invalid1/__init__.py', '/dummy/plugins/invalid1/command.py', '/dummy/plugins/invalid2', '/dummy/plugins/invalid2/__init__.py', '/dummy/plugins/invalid2/command.py', '/dummy/plugins/__init__.py' ]] return os.path.abspath(path) in exists def stub_isdir(path): dirs = [os.path.abspath(path) for path in [ '/dummy/plugins/invalid1', '/dummy/plugins/invalid2' ]] return os.path.abspath(path) in dirs mock_tree = mock.MagicMock() mock_module = StubInvalidPlugin() dummy_plugin_dir = os.path.normcase('/dummy/plugins') with mock.patch('os.listdir', return_value=['invalid1', 'invalid2']), \ mock.patch('googkit.lib.path.plugin', return_value=dummy_plugin_dir), \ mock.patch('os.path.exists', side_effect=stub_exists), \ mock.patch('os.path.isdir', side_effect=stub_isdir), \ mock.patch('googkit.lib.plugin.__import__', create=True, return_value=mock_module): with self.assertRaises(GoogkitError): googkit.lib.plugin.load(mock_tree)
def test_template_with_directory_missing(self): dummy_googkit_root = os.path.normcase('/dummy/usr/local/googkit') with mock.patch('googkit.lib.path.googkit_root', return_value=dummy_googkit_root), \ mock.patch('os.path.isdir', return_value=False): with self.assertRaises(GoogkitError): self.assertPathEqual( googkit.lib.path.template(), '/dummy/usr/local/googkit/googkit_data/template')
def test_user_config_on_groundchild(self): def stub_expanduser(path): return re.sub(r'~', '/home/user', path) with mock.patch('os.path.expanduser', side_effect=stub_expanduser), \ mock.patch('os.path.exists'): self.assertPathEqual(googkit.lib.path.user_config(), '/home/user/.googkit')
def test_run_internal(self): dummy_project_root = os.path.normcase('/dir1/dir2') self.cmd.apply_config_all = mock.MagicMock() with mock.patch('sys.stdout', new_callable=StubStdout), \ mock.patch('googkit.lib.path.project_root', return_value=dummy_project_root), \ mock.patch('googkit.commands.apply_config.working_directory'): self.cmd.run_internal() self.cmd.apply_config_all.assert_called_once_with()
def test_print_help(self): help = self.help_with_args(['0_leaf']) with mock.patch('sys.stdout', new_callable=StubStdout) as mock_stdout: help.print_help() self.assertFalse(mock_stdout.getvalue().find('Invalid command') >= 0) help = self.help_with_args(['0_leaf', 'bluerose']) with mock.patch('sys.stdout', new_callable=StubStdout) as mock_stdout: help.print_help() self.assertTrue(mock_stdout.getvalue().find('Invalid command') >= 0)
def test_user_config_on_groundchild(self): def stub_expanduser(path): return re.sub(r'~', '/home/user', path) with mock.patch('os.path.expanduser', side_effect=stub_expanduser), \ mock.patch('os.path.exists'): self.assertPathEqual( googkit.lib.path.user_config(), '/home/user/.googkit')
def test_template(self): def stub_isdir(path): return is_path_equal(path, '/dummy/usr/local/googkit/googkit_data/template') dummy_googkit_root = os.path.normcase('/dummy/usr/local/googkit') with mock.patch('googkit.lib.path.googkit_root', return_value=dummy_googkit_root), \ mock.patch('os.path.isdir', side_effect=stub_isdir): self.assertPathEqual( googkit.lib.path.template(), '/dummy/usr/local/googkit/googkit_data/template')
def test_template(self): def stub_isdir(path): return is_path_equal( path, '/dummy/usr/local/googkit/googkit_data/template') dummy_googkit_root = os.path.normcase('/dummy/usr/local/googkit') with mock.patch('googkit.lib.path.googkit_root', return_value=dummy_googkit_root), \ mock.patch('os.path.isdir', side_effect=stub_isdir): self.assertPathEqual( googkit.lib.path.template(), '/dummy/usr/local/googkit/googkit_data/template')
def test_executable(self): with mock.patch('os.path.isfile', return_value=True), \ mock.patch('os.access', return_value=True): self.assertTrue( googkit.lib.file.executable('/foo/bar'), 'Executable file should be executable') with mock.patch('os.path.isfile', return_value=False): self.assertFalse( googkit.lib.file.executable('/foo/bar'), 'Non-existent file should not be executable')
def test_clone(self): dirpath = os.path.join(os.sep, 'dir1', 'dir2') MockPopen = mock.MagicMock() MockPopen.return_value.returncode = 0 with mock.patch('subprocess.Popen', new=MockPopen) as MockPopen, \ mock.patch('googkit.lib.clone._git_cmd', return_value='GIT'): googkit.lib.clone.run('https://example.com/example.git', dirpath) MockPopen.assert_called_once_with( ['GIT', 'clone', 'https://example.com/example.git', '/dir1/dir2'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def test_project_config(self): def stub_exists(path): if is_path_equal(path, '/dir1/dir2/googkit.cfg'): return True else: return False cwd = os.path.normcase('/dir1/dir2/dir3/dir4') dummy_project_root = os.path.normcase('/dir1/dir2') with mock.patch('os.path.exists', side_effect=stub_exists), \ mock.patch('googkit.lib.path.project_root', return_value=dummy_project_root): self.assertPathEqual(googkit.lib.path.project_config(cwd), '/dir1/dir2/googkit.cfg')
def test_run_internal(self): dummy_project_root = os.path.normcase('/dir1/dir2') self.cmd.update_deps = mock.MagicMock() self.cmd.update_testrunner = mock.MagicMock() with mock.patch('sys.stdout', new_callable=StubStdout), \ mock.patch('googkit.lib.path.project_root', return_value=dummy_project_root), \ mock.patch('googkit.commands.update_deps.working_directory'): self.cmd.run_internal() self.cmd.update_deps.assert_called_once_with() self.cmd.update_testrunner.assert_called_once_with()
def test_project_config(self): def stub_exists(path): if is_path_equal(path, '/dir1/dir2/googkit.cfg'): return True else: return False cwd = os.path.normcase('/dir1/dir2/dir3/dir4') dummy_project_root = os.path.normcase('/dir1/dir2') with mock.patch('os.path.exists', side_effect=stub_exists), \ mock.patch('googkit.lib.path.project_root', return_value=dummy_project_root): self.assertPathEqual( googkit.lib.path.project_config(cwd), '/dir1/dir2/googkit.cfg')
def test_update_testrunner(self): # Use stub config for stub project directories. self.cmd.config = StubConfigOnStubProject() self.cmd.update_tests = mock.MagicMock() self.cmd.update_tests.return_value = 'changed' # Data will be given by open with for-in statement read_data = '''\ DUMMY change me/*@test_files@*/ DUMMY''' # Expected data for write() expected_wrote = '''\ DUMMY changed/*@test_files@*/ DUMMY''' # Use mock_open mock_open = mock.mock_open(read_data=read_data) # Context Manager is a return value of the mock_open.__enter__ mock_fp = mock_open.return_value.__enter__.return_value # Read lines has "\n" at each last mock_fp.__iter__.return_value = iter([ (line + '\n') for line in read_data.split('\n') ]) with mock.patch('googkit.commands.update_deps.open', mock_open, create=True), \ mock.patch('os.path.exists') as mock_exists: mock_exists.return_value = True self.cmd.update_testrunner() # Expected the path is a related path from all_tests.html to js_dev/example_test.html expected_file = os.path.join('js_dev', 'example_test.html') self.cmd.update_tests.assert_called_once_with( ' change me/*@test_files@*/\n', [expected_file]) # Expected open was called twice (for reading and writing) mock_open.assert_any_call(StubConfigOnStubProject.TESTRUNNER) mock_open.assert_any_call(StubConfigOnStubProject.TESTRUNNER, 'w') self.assertEqual(mock_open.call_count, 2) # Expected correct data was wrote self.assertEqual( mock_fp.write.call_args_list, [mock.call(line + '\n', ) for line in expected_wrote.split('\n')])
def test_run_internal(self): self.env.argument = mock.MagicMock() self.env.argument.option.return_value = False dummy_project_root = os.path.normcase('/dir1/dir2') self.cmd.download_closure_compiler = mock.MagicMock() self.cmd.download_closure_library = mock.MagicMock() with mock.patch('sys.stdout', new_callable=StubStdout), \ mock.patch('googkit.lib.path.project_root', return_value=dummy_project_root), \ mock.patch('googkit.commands.download.working_directory'): self.cmd.run_internal() self.cmd.download_closure_compiler.assert_called_once_with() self.cmd.download_closure_library.assert_called_once_with()
def test_update_testrunner(self): # Use stub config for stub project directories. self.cmd.config = StubConfigOnStubProject() self.cmd.update_tests = mock.MagicMock() self.cmd.update_tests.return_value = 'changed' # Data will be given by open with for-in statement read_data = '''\ DUMMY change me/*@test_files@*/ DUMMY''' # Expected data for write() expected_wrote = '''\ DUMMY changed/*@test_files@*/ DUMMY''' # Use mock_open mock_open = mock.mock_open(read_data=read_data) # Context Manager is a return value of the mock_open.__enter__ mock_fp = mock_open.return_value.__enter__.return_value # Read lines has "\n" at each last mock_fp.__iter__.return_value = iter([(line + '\n') for line in read_data.split('\n')]) with mock.patch('googkit.commands.update_deps.open', mock_open, create=True), \ mock.patch('os.path.exists') as mock_exists: mock_exists.return_value = True self.cmd.update_testrunner() # Expected the path is a related path from all_tests.html to js_dev/example_test.html expected_file = os.path.join('js_dev', 'example_test.html') self.cmd.update_tests.assert_called_once_with( ' change me/*@test_files@*/\n', [expected_file]) # Expected open was called twice (for reading and writing) mock_open.assert_any_call(StubConfigOnStubProject.TESTRUNNER) mock_open.assert_any_call(StubConfigOnStubProject.TESTRUNNER, 'w') self.assertEqual(mock_open.call_count, 2) # Expected correct data was wrote self.assertEqual( mock_fp.write.call_args_list, [mock.call(line + '\n',) for line in expected_wrote.split('\n')])
def test_run_internal_with_debug_opt(self): self.cmd.build_debug = mock.MagicMock() self.cmd.build_production = mock.MagicMock() self.cmd.html_requiring_js = mock.MagicMock() self.cmd.html_requiring_js.side_effect = lambda: ['dummy.html'] self.env.argument = mock.MagicMock() self.env.argument.option.side_effect = lambda opt: opt == '--debug' dummy_project_root = os.path.normcase('/dir1/dir2') with mock.patch('googkit.lib.path.project_root', return_value=dummy_project_root), \ mock.patch('googkit.commands.build.working_directory'): self.cmd.run_internal() self.cmd.build_debug.assert_called_once_with('dummy.html', dummy_project_root, False)
def test_run(self): class DummySequenceCommand(SequenceCommand): @classmethod def _internal_commands(cls): return [DummyFooCommand, DummyBarCommand] env = StubEnvironment() command = DummySequenceCommand(env) with mock.patch('test.commands.test_sequence.DummyFooCommand') as MockFoo, \ mock.patch('test.commands.test_sequence.DummyBarCommand') as MockBar: command.run() self.assertTrue(MockFoo.return_value.run.called) self.assertTrue(MockBar.return_value.run.called)
def test_print_available_commands(self): help = self.help_with_args(['0_leaf']) with mock.patch('sys.stdout') as mock_stdout: help._print_available_commands(None) self.assertFalse(mock_stdout.write.called) help = self.help_with_args(['0_node']) with mock.patch('sys.stdout', new_callable=StubStdout) as mock_stdout: help._print_available_commands(None) self.assertTrue(mock_stdout.getvalue().find('Available commands') >= 0) help = self.help_with_args(['0_node', 'bluerose']) with mock.patch('sys.stdout', new_callable=StubStdout) as mock_stdout: help._print_available_commands(None) self.assertTrue( mock_stdout.getvalue().find('Did you mean one of these') >= 0)
def test_update_deps_js(self): MockPopen = mock.MagicMock() MockPopen.return_value.returncode = 0 with mock.patch('subprocess.Popen', new=MockPopen) as mock_popen: self.cmd.update_deps() arg_format_dict = { 'depswriter_path': StubConfig.DEPSWRITER, 'js_dev_path': StubConfig.JS_DEV_DIR, 'relpath_from_base_js_to_js_dev': os.path.relpath(StubConfig.JS_DEV_DIR, os.path.dirname(StubConfig.BASE_JS)), 'deps_js_path': StubConfig.DEPS_JS } expected = ' '.join([ 'python', '{depswriter_path}', '--root_with_prefix="{js_dev_path}', '{relpath_from_base_js_to_js_dev}"', '--output_file="{deps_js_path}"' ]).format(**arg_format_dict) mock_popen.assert_called_once_with(expected, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
def test_user_config_with_file_missing(self): def stub_expanduser(path): return re.sub(r'~', '/home/user', path) def stub_exists(path): exists = [os.path.abspath(exist) for exist in [ '/', '/home', '/home/user', '/home/user/.googkit']] return os.path.abspath(path) in exists with mock.patch('os.path.expanduser', side_effect=stub_expanduser), \ mock.patch('os.path.exists', side_effect=stub_exists): self.assertPathEqual(googkit.lib.path.user_config(), '/home/user/.googkit')
def test_user_config_with_file_missing(self): def stub_expanduser(path): return re.sub(r'~', '/home/user', path) def stub_exists(path): exists = [ os.path.abspath(exist) for exist in ['/', '/home', '/home/user', '/home/user/.googkit'] ] return os.path.abspath(path) in exists with mock.patch('os.path.expanduser', side_effect=stub_expanduser), \ mock.patch('os.path.exists', side_effect=stub_exists): self.assertPathEqual(googkit.lib.path.user_config(), '/home/user/.googkit')
def test_build_debug(self): self.cmd.setup_files = mock.MagicMock() self.cmd.debug_arguments = mock.MagicMock() self.cmd.debug_arguments.return_value = ['ARG'] self.cmd.compiled_js_path = mock.MagicMock() self.cmd.compiled_js_path.return_value = 'dummy.JS' self.cmd.modify_source_map = mock.MagicMock() MockPopen = mock.MagicMock() mock_popen = MockPopen.return_value # It simulates the command was succeeded mock_popen.returncode = 0 with mock.patch('subprocess.Popen', new=MockPopen): self.cmd.build_debug('dummy.html', StubConfig.PROJECT_DIR, False) self.cmd.setup_files.assert_called_once_with(StubConfig.DEBUG_DIR, False) MockPopen.assert_called_once_with( ['python', StubConfig.CLOSUREBUILDER, 'ARG'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.cmd.modify_source_map.assert_called_once_with( 'dummy.JS.map', StubConfig.PROJECT_DIR)
def test_debug_arguments_with_flagfile(self): expected = BuildCommand.BuilderArguments() expected.builder_arg( '--root', os.path.relpath(StubConfig.LIBRARRY_ROOT, StubConfig.PROJECT_DIR)) expected.builder_arg('--root', StubConfig.JS_DEV_DIR) expected.builder_arg('--namespace', 'googkit_dummy') expected.builder_arg('--output_mode', 'compiled') expected.builder_arg('--output_file', 'dummy.js') expected.builder_arg('--compiler_jar', StubConfig.COMPILER) expected.compiler_arg('--compilation_level', 'COMPILATION_LEVEL') expected.compiler_arg('--source_map_format', 'V3') expected.compiler_arg('--create_source_map', 'dummy.js.map') expected.compiler_arg( '--output_wrapper', '"%output%//# sourceMappingURL={path}"'.format( path='dummy.js.map')) expected.compiler_arg('--flagfile', StubConfig.COMPILER_FLAGFILE_FOR_DEBUG) self.cmd.compiled_js_path = mock.MagicMock() self.cmd.compiled_js_path.side_effect = lambda _: 'dummy.js' with mock.patch('os.path.exists', return_value=True): args = self.cmd.debug_arguments('dummy.html', StubConfig.PROJECT_DIR) self.assertEqual(args, expected)
def test_download_closure_library(self): with mock.patch('googkit.lib.clone') as mock_clone: self.cmd.download_closure_library() mock_clone.run.assert_called_once_with( StubConfig.LIBRARY_GIT_REPOS, StubConfig.LIBRARRY_ROOT)
def test_copy_templates_with_conflict(self): dst_path = '/tmp/foo/bar' template_dir = '/tmp/dummy' def listdir(path): if path == dst_path: return ['dummy1', 'dummy2', 'conflicted'] elif path == template_dir: return ['dummy3', 'conflicted'] else: self.failed('Unexpected path: ' + path) with mock.patch('os.listdir', side_effect=listdir) as mock_listdir, \ mock.patch('distutils.dir_util.copy_tree'), \ mock.patch('googkit.lib.path.template', return_value=template_dir): with self.assertRaises(GoogkitError): self.cmd.copy_template(dst_path)
def test_run(self): class DummySequenceCommand(SequenceCommand): @classmethod def _internal_commands(cls): return [ DummyFooCommand, DummyBarCommand ] env = StubEnvironment() command = DummySequenceCommand(env) with mock.patch('test.commands.test_sequence.DummyFooCommand') as MockFoo, \ mock.patch('test.commands.test_sequence.DummyBarCommand') as MockBar: command.run() self.assertTrue(MockFoo.return_value.run.called) self.assertTrue(MockBar.return_value.run.called)
def test_copy_templates(self): dst_path = '/tmp/foo/bar' template_dir = '/tmp/dummy' def listdir(path): if path == dst_path: return ['dummy1', 'dummy2'] elif path == template_dir: return ['dummy3'] else: self.failed('Unexpected path: ' + path) with mock.patch('os.listdir', side_effect=listdir) as mock_listdir, \ mock.patch('distutils.dir_util.copy_tree') as mock_copytree, \ mock.patch('googkit.lib.path.template', return_value=template_dir): self.cmd.copy_template(dst_path) mock_copytree.assert_called_once_with(template_dir, dst_path)
def test_load_with_two_plugins(self): def stub_exists(path): exists = [ os.path.abspath(path) for path in [ '/dummy/plugins/dummy1', '/dummy/plugins/dummy1/__init__.py', '/dummy/plugins/dummy1/command.py', '/dummy/plugins/dummy2', '/dummy/plugins/dummy2/__init__.py', '/dummy/plugins/dummy2/command.py', '/dummy/plugins/__init__.py' ] ] return os.path.abspath(path) in exists def stub_isdir(path): dirs = [ os.path.abspath(path) for path in ['/dummy/plugins/dummy1', '/dummy/plugins/dummy2'] ] return os.path.abspath(path) in dirs mock_tree = mock.MagicMock() mock_module = StubPlugin() mock_module.register = mock.MagicMock() dummy_plugin_dir = os.path.normcase('/dummy/plugins') with mock.patch('os.listdir', return_value=['dummy1', 'dummy2']), \ mock.patch('googkit.lib.path.plugin', return_value=dummy_plugin_dir), \ mock.patch('os.path.exists', side_effect=stub_exists), \ mock.patch('os.path.isdir', side_effect=stub_isdir), \ mock.patch('googkit.lib.plugin.__import__', create=True, return_value=mock_module) as mock_import: googkit.lib.plugin.load(mock_tree) mock_import.assert_any_call('plugins.dummy1.command', fromlist=['command']) mock_import.assert_any_call('plugins.dummy2.command', fromlist=['command']) self.assertEqual(mock_import.call_count, 2) mock_module.register.assert_any_call(mock_tree) self.assertEqual(mock_module.register.call_count, 2)
def test_default_config(self): def stub_exists(path): exists = [os.path.abspath(exist) for exist in [ '/', '/dummy', '/dummy/usr', '/dummy/usr/local', '/dummy/usr/local/googkit', '/dummy/usr/local/googkit/googkit_data', '/dummy/usr/local/googkit/googkit_data/default.cfg']] return os.path.abspath(path) in exists dummy_googkit_root = os.path.normcase('/dummy/usr/local/googkit') with mock.patch('os.path.exists', side_effect=stub_exists), \ mock.patch('googkit.lib.path.googkit_root', return_value=dummy_googkit_root): self.assertPathEqual( googkit.lib.path.default_config(), '/dummy/usr/local/googkit/googkit_data/default.cfg')
def test_load_with_no_plugins(self): def stub_exists(path): exists = [os.path.abspath(path) for path in [ '/dummy/plugins/__init__.py' ]] return os.path.abspath(path) in exists mock_tree = mock.MagicMock() mock_module = StubPlugin() mock_module.register = mock.MagicMock() dummy_plugin_dir = os.path.normcase('/dummy/plugins') with mock.patch('os.listdir', return_value=[]), \ mock.patch('googkit.lib.path.plugin', return_value=dummy_plugin_dir), \ mock.patch('os.path.exists', side_effect=stub_exists), \ mock.patch('os.path.isdir', return_value=False), \ mock.patch('googkit.lib.plugin.__import__', create=True, return_value=mock_module) as mock_import: googkit.lib.plugin.load(mock_tree) self.assertFalse(mock_import.called)
def test_compile_resource(self): tgt_path = '/tmp/foo/bar' # Data will be given by for-in statement with open() read_data = '''\ DUMMY <!--@base_js@--> <!--@deps_js@--> <!--@require_main@--> <!--@dummy_marker@-->''' # Expected data will be given by open.write() expected = '''\ DUMMY <script src="bar.JS"></script> <!--@dummy_marker@-->''' # Use mock_open mock_open = mock.mock_open(read_data=read_data) # Context Manager is a return value of the mock_open.__enter__ mock_fp = mock_open.return_value.__enter__.return_value # Read lines has "\n" at each last mock_fp.__iter__.return_value = iter([(line + '\n') for line in read_data.split('\n')]) # Switch to the mock_open from the original open with mock.patch('os.sep', new='/'), \ mock.patch('googkit.commands.build.open', mock_open, create=True): self.cmd.compile_resource(tgt_path) # Expected the target file was opened twice for reading and writing mock_open.assert_any_call(tgt_path) mock_open.assert_any_call(tgt_path, 'w') # Expected correct data was wrote self.assertEqual( mock_fp.write.call_args_list, [mock.call(line + '\n',) for line in expected.split('\n')])
def test_load_with_two_plugins(self): def stub_exists(path): exists = [os.path.abspath(path) for path in [ '/dummy/plugins/dummy1', '/dummy/plugins/dummy1/__init__.py', '/dummy/plugins/dummy1/command.py', '/dummy/plugins/dummy2', '/dummy/plugins/dummy2/__init__.py', '/dummy/plugins/dummy2/command.py', '/dummy/plugins/__init__.py' ]] return os.path.abspath(path) in exists def stub_isdir(path): dirs = [os.path.abspath(path) for path in [ '/dummy/plugins/dummy1', '/dummy/plugins/dummy2' ]] return os.path.abspath(path) in dirs mock_tree = mock.MagicMock() mock_module = StubPlugin() mock_module.register = mock.MagicMock() dummy_plugin_dir = os.path.normcase('/dummy/plugins') with mock.patch('os.listdir', return_value=['dummy1', 'dummy2']), \ mock.patch('googkit.lib.path.plugin', return_value=dummy_plugin_dir), \ mock.patch('os.path.exists', side_effect=stub_exists), \ mock.patch('os.path.isdir', side_effect=stub_isdir), \ mock.patch('googkit.lib.plugin.__import__', create=True, return_value=mock_module) as mock_import: googkit.lib.plugin.load(mock_tree) mock_import.assert_any_call('plugins.dummy1.command', fromlist=['command']) mock_import.assert_any_call('plugins.dummy2.command', fromlist=['command']) self.assertEqual(mock_import.call_count, 2) mock_module.register.assert_any_call(mock_tree) self.assertEqual(mock_module.register.call_count, 2)
def test_download_closure_compiler(self): tmp_path = '/tmp/dummy' MockZipFile = mock.MagicMock() mock_zip = MockZipFile.return_value.__enter__.return_value with mock.patch('googkit.commands.download.request.urlretrieve') as mock_urlretrive, \ mock.patch('zipfile.ZipFile', new=MockZipFile), \ mock.patch('tempfile.mkdtemp', return_value=tmp_path), \ mock.patch('shutil.rmtree') as mock_rmtree: self.cmd.download_closure_compiler() # Expected temporary directory was created and removed mock_rmtree.assert_called_once_with(tmp_path) MockZipFile.assert_called_once_with( os.path.join(tmp_path, 'compiler.zip')) mock_zip.extractall.assert_called_once_with( StubConfig.COMPILER_ROOT) mock_urlretrive.assert_called_once_with( StubConfig.COMPILER_LATEST_ZIP, os.path.join(tmp_path, 'compiler.zip'))
def test_setup(self): class DummyCommand(Command): def _load_config_if_needed(self): pass env = StubEnvironment() env.cwd = '/cwd' cmd = DummyCommand(env) cmd._load_config_if_needed = mock.MagicMock() with mock.patch('os.chdir') as mock_chdir: cmd._setup() mock_chdir.assert_called_once_with('/cwd')