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_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_run(self): self.cmd._validate_options = mock.MagicMock() self.cmd.run_internal = mock.MagicMock() self.cmd.run() self.assertFalse(self.cmd._validate_options.called) self.assertTrue(self.cmd.run_internal.called)
def test_register(self): cmd100 = mock.MagicMock() cmd101 = mock.MagicMock() self.tree.register(['sub', 'subsub', 'subsubsub'], [cmd100, cmd101]) self.assertTrue('sub' in self.tree._tree) self.assertTrue('subsub' in self.tree._tree['sub']) self.assertTrue('subsubsub' in self.tree._tree['sub']['subsub']) self.assertEqual(self.tree._tree['sub']['subsub']['subsubsub'], [cmd100, cmd101])
def test_init(self): cwd = '/cwd' mock_argument = mock.MagicMock() mock_tree = mock.MagicMock() env = Environment(cwd, mock_argument, mock_tree) self.assertEqual(env.cwd, cwd) self.assertEqual(env.argument, mock_argument) self.assertEqual(env.tree, mock_tree)
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_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_run(self): class DummyCommand(Command): pass env = StubEnvironment() env.argument = Argument() cmd = DummyCommand(env) cmd._setup = mock.MagicMock() cmd.run_internal = mock.MagicMock() with mock.patch('sys.stdout', new_callable=StubStdout): cmd.run() self.assertTrue(cmd._setup.called) self.assertTrue(cmd.run_internal.called)
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_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_update_base_js(self): self.cmd.config = mock.MagicMock() self.cmd.config.base_js.return_value = 'dummy.js' line = '<script src="change me"></script>' expected = '<script src="dummy.js"></script>' self.assertEqual(self.cmd.update_base_js(line, 'dummy.html'), expected)
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_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_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.env.cwd = 'dummy' self.cmd.copy_template = mock.MagicMock() self.cmd.run_internal() self.cmd.copy_template.assert_called_once_with('dummy')
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_multitestrunner_css(self): self.cmd.config = mock.MagicMock() self.cmd.config.multitestrunner_css.return_value = 'dummy.css' line = '<link rel="stylesheet" href="change me">' expected = '<link rel="stylesheet" href="dummy.css">' self.assertEqual( self.cmd.update_multitestrunner_css(line, 'dummy.html'), expected)
def test_build_production(self): self.cmd.setup_files = mock.MagicMock() self.cmd.production_arguments = mock.MagicMock() self.cmd.production_arguments.return_value = ['ARG'] 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_production(StubConfig.PROJECT_DIR, False) self.cmd.setup_files.assert_called_once_with(StubConfig.PRODUCTION_DIR, False) MockPopen.assert_called_once_with( ['python', StubConfig.CLOSUREBUILDER, 'ARG'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
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 setUp(self): CommandTree.DEFAULT_TREE = { '0_leaf': TestHelp.NoOptionCommand, '0_node': { '1_leaf': TestHelp.OptionCommand, '1_node': { '2_leaf': mock.MagicMock() } } } self.tree = CommandTree()
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_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_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_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')
def test_setup_files(self): self.cmd.config = StubConfigOnStubProject() self.cmd.compile_resource = mock.MagicMock() with mock.patch.object(BuildCommand, 'ignore_dirs') as mock_ignore_dirs, \ mock.patch('googkit.lib.file.copytree') as mock_copytree: mock_ignore_dirs.return_value = 'IGNORE' self.cmd.setup_files(StubConfigOnStubProject.PRODUCTION_DIR, False) mock_copytree.assert_called_once_with( StubConfigOnStubProject.DEVELOPMENT_DIR, StubConfigOnStubProject.PRODUCTION_DIR, ignore='IGNORE') self.cmd.compile_resource.assert_any_call( os.path.join(StubConfigOnStubProject.PRODUCTION_DIR, 'index.html'))
def test_apply_config_all(self): self.cmd.apply_config = mock.MagicMock() self.cmd.config = StubConfigOnStubProject() self.cmd.apply_config_all() expected_paths = [ os.path.normcase(path) for path in [ 'index.html', 'all_tests.html', 'style.css', 'js_dev/example.js', 'js_dev/example_test.html', 'js_dev/main.js' ] ] for expected_path in expected_paths: path = os.path.join(StubConfigOnStubProject.DEVELOPMENT_DIR, expected_path) self.cmd.apply_config.assert_any_call(path)
def test_run(self): MockCmd = mock.MagicMock() MockCmd.needs_project_config.return_value = False mock_cmd = MockCmd.return_value with mock.patch('os.chdir'), \ mock.patch('sys.argv', new=['/DUMMY.py', 'dummy1', 'dummy2']), \ mock.patch('googkit.lib.path.project_root', return_value='/dir1/dir2'), \ mock.patch('googkit.Help'), \ mock.patch('googkit.Environment', return_value='dummy_env'), \ mock.patch('googkit.CommandTree') as MockTree, \ mock.patch('googkit.lib.plugin.load') as mock_load, \ mock.patch('logging.basicConfig'): MockTree.return_value.command_class.return_value = MockCmd googkit.main() mock_load.assert_called_once_with(MockTree.return_value) mock_cmd.run.assert_called_once_with()
def test_production_arguments(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('--define', 'goog.DEBUG=false') 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=False): args = self.cmd.production_arguments('dummy.html', StubConfig.PROJECT_DIR) self.assertEqual(args, expected)
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_lint(self): 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), \ mock.patch('googkit.lib.file.which', return_value='/usr/local/bin/gjslint'): self.cmd.lint() call_1 = mock.call([ 'gjslint', os.path.join(StubConfigOnStubProject.JS_DEV_DIR, 'example.js'), os.path.join(StubConfigOnStubProject.JS_DEV_DIR, 'main.js'), ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) call_2 = mock.call([ 'gjslint', os.path.join(StubConfigOnStubProject.JS_DEV_DIR, 'main.js'), os.path.join(StubConfigOnStubProject.JS_DEV_DIR, 'example.js'), ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) if call_1 not in MockPopen.call_args_list and call_2 not in MockPopen.call_args_list: self.fail('Assertion Error: Arguments for gjslint is invalid.')