def test_build_ctags__invalid_custom_command(self): """Checks for failure for invalid custom command to execute ctags""" # build_ctags requires a real path, so we create a temporary file as a # cross-platform way to get the temp directory with tempfile.NamedTemporaryFile() as temp: with self.assertRaises(CalledProcessError): ctags.build_ctags(path=temp.name, cmd='ccttaaggss')
def test_build_ctags__invalid_custom_command(self): """Checks for failure for invalid custom command to execute ctags""" # build_ctags requires a real path, so we create a temporary file as a # cross-platform way to get the temp directory with tempfile.NamedTemporaryFile() as temp: with self.assertRaises(EnvironmentError): ctags.build_ctags(path=temp.name, cmd='ccttaaggss')
def test_build_ctags__ctags_on_path(self): """Checks that ``ctags`` is in ``PATH``""" # build_ctags requires a real path, so we create a temporary file as a # cross-platform way to get the temp directory with tempfile.NamedTemporaryFile() as temp: try: ctags.build_ctags(path=temp.name) except EnvironmentError: self.fail("build_ctags() raised EnvironmentError. ctags not" " on path")
def test_build_ctags__custom_command(self): """Checks for support of simple custom command to execute ctags""" # build_ctags requires a real path, so we create a temporary file as a # cross-platform way to get the temp directory with tempfile.NamedTemporaryFile() as temp: try: ctags.build_ctags(path=temp.name, cmd="ctags") except EnvironmentError: self.fail("build_ctags() raised EnvironmentError. ctags not" " on path")
def test_build_ctags__ctags_on_path(self): """Checks that ``ctags`` is in ``PATH``""" # build_ctags requires a real path, so we create a temporary file as a # cross-platform way to get the temp directory with tempfile.NamedTemporaryFile() as temp: try: ctags.build_ctags(path=temp.name) except EnvironmentError: self.fail('build_ctags() raised EnvironmentError. ctags not' ' on path')
def test_build_ctags__custom_command(self): """Checks for support of simple custom command to execute ctags""" # build_ctags requires a real path, so we create a temporary file as a # cross-platform way to get the temp directory with tempfile.NamedTemporaryFile() as temp: try: ctags.build_ctags(path=temp.name, cmd='ctags') except EnvironmentError: self.fail('build_ctags() raised EnvironmentError. ctags not' ' on path')
def build_ctags(self, cmd, tag_files): def tags_built(tag_file): print "Finished building %s" % tag_file in_main(lambda: status_message("Finished building %s" % tag_file))() in_main(lambda: tags_cache[dirname(tag_file)].clear())() for tag_file in tag_files: print "Re/Building CTags for %s: Please be patient" % tag_file in_main(lambda: status_message("Re/Building CTags for %s: Please be patient" % tag_file))() ctags.build_ctags(cmd, tag_file) tags_built(tag_file)
def build_ctags(self, cmd, tag_files): def tags_built(tag_file): print(('Finished building %s' % tag_file)) in_main(lambda: status_message('Finished building %s' % tag_file))() in_main(lambda: tags_cache[dirname(tag_file)].clear())() for tag_file in tag_files: print(('Re/Building CTags for %s: Please be patient' % tag_file)) in_main(lambda: status_message('Re/Building CTags for %s: Please be patient' % tag_file))() ctags.build_ctags(cmd, tag_file) tags_built(tag_file)
def test_parse_tag_lines__c(self): """ Test ``parse_tag_lines`` with a sample C file. """ path = self.build_c_file() tag_file = ctags.build_ctags(path=path) with codecs.open(tag_file, encoding='utf-8') as output: try: content = output.readlines() filename = os.path.basename(path) except IOError: self.fail("Setup of files for test failed") finally: output.close() os.remove(path) # clean up os.remove(tag_file) expected_outputs = { 'bar': [{ 'symbol': 'bar', 'filename': filename, 'ex_command': 'void bar()', 'tag_path': (filename, 'bar'), 'type': 'f', 'fields': None }], 'foo': [{ 'symbol': 'foo', 'filename': filename, 'ex_command': '1', 'tag_path': (filename, 'foo'), 'type': 'd', 'fields': 'file:', 'field_keys': ['file'], 'file': '' }], 'foobar': [{ 'symbol': 'foobar', 'filename': filename, 'ex_command': '2', 'tag_path': (filename, 'foobar'), 'type': 'd', 'fields': 'file:', 'field_keys': ['file'], 'file': '' }] } result = ctags.parse_tag_lines(content) for key in expected_outputs: self.assertEqual(result[key], expected_outputs[key]) for key in result: # don't forget - we might have missed something! self.assertEqual(expected_outputs[key], result[key])
def build_ctags(self, paths, command, tag_file, recursive, opts): """ Build tags for the open file or folder(s). :param paths: paths to build ctags for :param command: ctags command :param tag_file: filename to use for the tag file. Defaults to ``tags`` :param recursive: specify if search should be recursive in directory given by path. This overrides filename specified by ``path`` :param opts: list of additional parameters to pass to the ``ctags`` executable :returns: None """ def tags_building(tag_file): """Display 'Building CTags' message in all views""" print(('Building CTags for %s: Please be patient' % tag_file)) in_main(lambda: status_message('Building CTags for {0}: Please be' ' patient'.format(tag_file)))() def tags_built(tag_file): """Display 'Finished Building CTags' message in all views""" print(('Finished building %s' % tag_file)) in_main(lambda: status_message('Finished building {0}'.format( tag_file)))() in_main(lambda: tags_cache[os.path.dirname(tag_file)].clear())() for path in paths: tags_building(path) try: result = ctags.build_ctags(path=path, tag_file=tag_file, recursive=recursive, opts=opts, cmd=command) except IOError as e: error_message(e.strerror) return except subprocess.CalledProcessError as e: if sublime.platform() == 'windows': str_err = ' '.join( e.output.decode('windows-1252').splitlines()) else: str_err = e.output.decode( locale.getpreferredencoding()).rstrip() error_message(str_err) return except Exception as e: error_message( "An unknown error occured.\nCheck the console for info.") raise e tags_built(result) GetAllCTagsList.ctags_list = [] # clear the cached ctags list
def build_ctags(self, paths, command, tag_file, recursive, opts, stdout): """Build tags for the open file or folder(s) :param paths: paths to build ctags for :param command: ctags command :param tag_file: filename to use for the tag file. Defaults to ``tags`` :param recursive: specify if search should be recursive in directory given by path. This overrides filename specified by ``path`` :param opts: list of additional parameters to pass to the ``ctags`` executable :param stdout: use command output to stdout instead of -f :returns: None """ def tags_building(tag_file): """Display 'Building CTags' message in all views""" print(('Building CTags for %s: Please be patient' % tag_file)) in_main(lambda: status_message('Building CTags for {0}: Please be' ' patient'.format(tag_file)))() def tags_built(tag_file): """Display 'Finished Building CTags' message in all views""" print(('Finished building %s' % tag_file)) in_main(lambda: status_message('Finished building {0}' .format(tag_file)))() in_main(lambda: tags_cache[os.path.dirname(tag_file)].clear())() for path in paths: tags_building(path) try: result = ctags.build_ctags(path=path, tag_file=tag_file, recursive=recursive, opts=opts, cmd=command, stdout=stdout) except IOError as e: error_message(e.strerror) return except subprocess.CalledProcessError as e: if sublime.platform() == 'windows': str_err = ' '.join( e.output.decode('windows-1252').splitlines()) else: str_err = e.output.decode(locale.getpreferredencoding()).rstrip() error_message(str_err) return except Exception as e: error_message("An unknown error occured.\nCheck the console for info.") raise e tags_built(result) # clear the cached ctags list self.view.run_command('ctags_access_merge') GetAllCTagsList.set_ctags_list(self.view, None)
def test_parse_tag_lines__c(self): """ Test ``parse_tag_lines`` with a sample C file. """ path = self.build_c_file() tag_file = ctags.build_ctags(path=path) with codecs.open(tag_file, encoding='utf-8') as output: try: content = output.readlines() filename = os.path.basename(path) except IOError: self.fail("Setup of files for test failed") finally: output.close() os.remove(path) # clean up os.remove(tag_file) expected_outputs = { 'bar': [{ 'symbol': 'bar', 'filename': filename, 'ex_command': 'void bar()', 'tag_path': (filename, 'bar'), 'type': 'f', 'fields': None}], 'foo': [{ 'symbol': 'foo', 'filename': filename, 'ex_command': '1', 'tag_path': (filename, 'foo'), 'type': 'd', 'fields': 'file:', 'field_keys': ['file'], 'file': ''}], 'foobar': [{ 'symbol': 'foobar', 'filename': filename, 'ex_command': '2', 'tag_path': (filename, 'foobar'), 'type': 'd', 'fields': 'file:', 'field_keys': ['file'], 'file': ''}] } result = ctags.parse_tag_lines(content) for key in expected_outputs: self.assertEqual(result[key], expected_outputs[key]) for key in result: # don't forget - we might have missed something! self.assertEqual(expected_outputs[key], result[key])
def test_find_tags_relative_to__find_tags_in_current_directory(self): TAG_FILE = "example_tags" current_path = self.build_python_file() tag_file = ctags.build_ctags(path=current_path, tag_file=TAG_FILE) # should find tag file in current directory self.assertEqual(ctagsplugin.find_tags_relative_to(current_path, TAG_FILE), tag_file) # cleanup self.remove_tmp_files([current_path, tag_file])
def test_find_tags_relative_to__find_tags_in_current_directory(self): TAG_FILE = 'example_tags' current_path = self.build_python_file() tag_file = ctags.build_ctags(path=current_path, tag_file=TAG_FILE) # should find tag file in current directory self.assertEqual( ctagsplugin.find_tags_relative_to(current_path, TAG_FILE), tag_file) # cleanup self.remove_tmp_files([current_path, tag_file])
def test_find_tags_relative_to__find_tags_in_parent_directory(self): TAG_FILE = "example_tags" parent_path = self.build_python_file() parent_tag_file = ctags.build_ctags(path=parent_path, tag_file=TAG_FILE) child_dir = self.make_tmp_directory() child_path = self.build_python_file(pwd=child_dir) # should find tag file in parent directory self.assertEqual(ctagsplugin.find_tags_relative_to(child_path, TAG_FILE), parent_tag_file) # cleanup self.remove_tmp_files([parent_path, parent_tag_file]) self.remove_tmp_directory(child_dir)
def build_ctags(self, paths, command, tag_file, recursive, opts): """Build tags for the open file or folder(s) :param paths: paths to build ctags for :param command: ctags command :param tag_file: filename to use for the tag file. Defaults to ``tags`` :param recursive: specify if search should be recursive in directory given by path. This overrides filename specified by ``path`` :param opts: additional parameters to pass to the ``ctags`` executable :returns: None """ def tags_building(tag_file): """Display 'Building CTags' message in all views""" print(('Building CTags for %s: Please be patient' % tag_file)) in_main(lambda: status_message('Building CTags for {0}: Please be' ' patient'.format(tag_file)))() def tags_built(tag_file): """Display 'Finished Building CTags' message in all views""" print(('Finished building %s' % tag_file)) in_main(lambda: status_message('Finished building {0}'.format( tag_file)))() in_main(lambda: tags_cache[os.path.dirname(tag_file)].clear())() for path in paths: tags_building(path) try: result = ctags.build_ctags(path=path, tag_file=tag_file, recursive=recursive, opts=opts, cmd=command) except IOError as e: error_message(str(e).rstrip()) return except EnvironmentError as e: if not isinstance(e.strerror, str): str_err = ' '.join(e.strerror.decode('utf-8').splitlines()) else: str_err = str(e).rstrip() error_message(str_err) # show error_message return tags_built(result) GetAllCTagsList.ctags_list = [] # clear the cached ctags list
def test_build_ctags__additional_options(self): """Test execution of ctags using additional ctags options""" path = self.build_python_file() tag_file = ctags.build_ctags(path=path, tag_file="my_tag_file", opts="--language-force=java") with codecs.open(tag_file, encoding="utf-8") as output: try: content = output.readlines() # there should be nothing in the file but headers (due to the # Java 'language-force' option on a Python file) self.assertEqual(content[-1][:2], "!_") # all comments start with '!_' - confirm finally: output.close() os.remove(path) # clean up os.remove(tag_file)
def build_ctags(self, paths, command, tag_file, recursive, opts): """Build tags for the open file or folder(s) :param paths: paths to build ctags for :param command: ctags command :param tag_file: filename to use for the tag file. Defaults to ``tags`` :param recursive: specify if search should be recursive in directory given by path. This overrides filename specified by ``path`` :param opts: list of additional parameters to pass to the ``ctags`` executable :returns: None """ def tags_building(tag_file): """Display 'Building CTags' message in all views""" print(('Building CTags for %s: Please be patient' % tag_file)) in_main(lambda: status_message('Building CTags for {0}: Please be' ' patient'.format(tag_file)))() def tags_built(tag_file): """Display 'Finished Building CTags' message in all views""" print(('Finished building %s' % tag_file)) in_main(lambda: status_message('Finished building {0}' .format(tag_file)))() in_main(lambda: tags_cache[os.path.dirname(tag_file)].clear())() for path in paths: tags_building(path) try: result = ctags.build_ctags(path=path, tag_file=tag_file, recursive=recursive, opts=opts, cmd=command) except IOError as e: error_message(str(e).rstrip()) return except EnvironmentError as e: if not isinstance(e.strerror, str): str_err = ' '.join(e.strerror.decode('utf-8').splitlines()) else: str_err = str(e).rstrip() error_message(str_err) # show error_message return tags_built(result) GetAllCTagsList.ctags_list = [] # clear the cached ctags list
def test_build_ctags__custom_tag_file(self): """Test execution of ctags using a custom tag file""" path = self.build_python_file() tag_file = ctags.build_ctags(path=path, tag_file="my_tag_file") with codecs.open(tag_file, encoding="utf-8") as output: try: content = output.readlines() filename = os.path.basename(path) self.assertEqual( content[-1], "my_definition\t{0}\t/^def my_definition()" ':$/;"\tf\r\n'.format(filename) ) finally: output.close() os.remove(path) # clean up os.remove(tag_file)
def test_build_ctags__custom_tag_file(self): """Test execution of ctags using a custom tag file""" path = self.build_python_file() tag_file = ctags.build_ctags(path=path, tag_file='my_tag_file') with codecs.open(tag_file, encoding='utf-8') as output: try: content = output.readlines() filename = os.path.basename(path) self.assertEqual( content[-1], 'my_definition\t{0}\t/^def my_definition()' ':$/;"\tf\r\n'.format(filename)) finally: output.close() os.remove(path) # clean up os.remove(tag_file)
def test_find_tags_relative_to__find_tags_in_parent_directory(self): TAG_FILE = 'example_tags' parent_path = self.build_python_file() parent_tag_file = ctags.build_ctags(path=parent_path, tag_file=TAG_FILE) child_dir = self.make_tmp_directory() child_path = self.build_python_file(pwd=child_dir) # should find tag file in parent directory self.assertEqual( ctagsplugin.find_tags_relative_to(child_path, TAG_FILE), parent_tag_file) # cleanup self.remove_tmp_files([parent_path, parent_tag_file]) self.remove_tmp_directory(child_dir)
def test_build_ctags__single_file(self): """Test execution of ctags using a single temporary file""" path = self.build_python_file() tag_file = ctags.build_ctags(path=path) with codecs.open(tag_file, encoding='utf-8') as output: try: content = output.readlines() filename = os.path.basename(path) self.assertEqual( content[-1], 'my_definition\t{0}\t/^def my_definition()' ':$/;"\tf{1}'.format(filename, os.linesep)) finally: output.close() os.remove(path) # clean up os.remove(tag_file)
def test_build_ctags__additional_options(self): """Test execution of ctags using additional ctags options""" path = self.build_python_file() tag_file = ctags.build_ctags(path=path, tag_file='my_tag_file', opts="--language-force=java") with codecs.open(tag_file, encoding='utf-8') as output: try: content = output.readlines() # there should be nothing in the file but headers (due to the # Java 'language-force' option on a Python file) self.assertEqual( content[-1][:2], # all comments start with '!_' - confirm '!_') finally: output.close() os.remove(path) # clean up os.remove(tag_file)
def test_build_ctags__single_file(self): """ Test execution of ctags using a single temporary file. """ path = self.build_python_file() tag_file = ctags.build_ctags(path=path) with codecs.open(tag_file, encoding='utf-8') as output: try: content = output.readlines() filename = os.path.basename(path) self.assertEqual( content[-1], 'my_definition\t{0}\t/^def my_definition()' ':$/;"\tf{1}'.format(filename, os.linesep)) finally: output.close() os.remove(path) # clean up os.remove(tag_file)
def build_ctags(self, cmd, tag_file): in_main( lambda: status_message('Re/Building CTags: Please be patient'))() ctags.build_ctags(cmd, tag_file) return tag_file
def build_ctags(self, cmd, tag_file): in_main(lambda: status_message('Re/Building CTags: Please be patient'))() ctags.build_ctags(cmd, tag_file) return tag_file
def test_parse_tag_lines__python(self): """Test ``parse_tag_lines`` with a sample Python file""" path = self.build_python_file__extended() tag_file = ctags.build_ctags(path=path, opts=['--python-kinds=-i']) with codecs.open(tag_file, encoding='utf-8') as output: try: content = output.readlines() filename = os.path.basename(path) except: self.fail("Setup of files for test failed") finally: output.close() os.remove(path) # clean up os.remove(tag_file) expected_outputs = { 'MyClass': [{ 'symbol': 'MyClass', 'filename': filename, 'ex_command': 'class MyClass(object):', 'tag_path': (filename, 'MyClass'), 'type': 'c', 'fields': None }], 'address': [{ 'symbol': 'address', 'filename': filename, 'ex_command': '\taddress = None\t# comment preceded by a tab', 'tag_path': (filename, 'MyClass', 'address'), 'type': 'v', 'fields': 'class:MyClass', 'field_keys': ['class'], 'class': 'MyClass' }], 'last_name': [{ 'symbol': 'last_name', 'filename': filename, 'ex_command': '\tlast_name = None', 'tag_path': (filename, 'MyClass', 'last_name'), 'type': 'v', 'fields': 'class:MyClass', 'field_keys': ['class'], 'class': 'MyClass' }], 'my_function': [{ 'symbol': 'my_function', 'filename': filename, 'ex_command': 'def my_function(first_name):', 'tag_path': (filename, 'my_function'), 'type': 'f', 'fields': None }], 'my_method': [{ 'symbol': 'my_method', 'filename': filename, 'ex_command': '\tdef my_method(self, last_name):', 'tag_path': (filename, 'MyClass', 'my_method'), 'type': 'm', 'fields': 'class:MyClass', 'field_keys': ['class'], 'class': 'MyClass' }], 'COLOR_RED': [{ 'symbol': 'COLOR_RED', 'filename': filename, 'ex_command': 'COLOR_RED = "\\c800080FF;"\t#red', 'tag_path': (filename, 'COLOR_RED'), 'type': 'v', 'fields': None }], } result = ctags.parse_tag_lines(content) for key in expected_outputs: self.assertEqual(result[key], expected_outputs[key]) for key in result: # don't forget - we might have missed something! self.assertEqual(expected_outputs[key], result[key])
def test_parse_tag_lines__python(self): """Test ``parse_tag_lines`` with a sample Python file""" path = self.build_python_file__extended() tag_file = ctags.build_ctags(path=path, opts=['--python-kinds=-i']) with codecs.open(tag_file, encoding='utf-8') as output: try: content = output.readlines() filename = os.path.basename(path) except: self.fail("Setup of files for test failed") finally: output.close() os.remove(path) # clean up os.remove(tag_file) expected_outputs = { 'MyClass': [{ 'symbol': 'MyClass', 'filename': filename, 'ex_command': 'class MyClass(object):', 'tag_path': (filename, 'MyClass'), 'type': 'c', 'fields': None}], 'address': [{ 'symbol': 'address', 'filename': filename, 'ex_command': '\taddress = None\t# comment preceded by a tab', 'tag_path': (filename, 'MyClass', 'address'), 'type': 'v', 'fields': 'class:MyClass', 'field_keys': ['class'], 'class': 'MyClass'}], 'last_name': [{ 'symbol': 'last_name', 'filename': filename, 'ex_command': '\tlast_name = None', 'tag_path': (filename, 'MyClass', 'last_name'), 'type': 'v', 'fields': 'class:MyClass', 'field_keys': ['class'], 'class': 'MyClass'}], 'my_function': [{ 'symbol': 'my_function', 'filename': filename, 'ex_command': 'def my_function(first_name):', 'tag_path': (filename, 'my_function'), 'type': 'f', 'fields': None}], 'my_method': [{ 'symbol': 'my_method', 'filename': filename, 'ex_command': '\tdef my_method(self, last_name):', 'tag_path': (filename, 'MyClass', 'my_method'), 'type': 'm', 'fields': 'class:MyClass', 'field_keys': ['class'], 'class': 'MyClass'}], 'COLOR_RED': [{ 'symbol': 'COLOR_RED', 'filename': filename, 'ex_command': 'COLOR_RED = "\\c800080FF;"\t#red', 'tag_path': (filename, 'COLOR_RED'), 'type': 'v', 'fields': None}], } result = ctags.parse_tag_lines(content) for key in expected_outputs: self.assertEqual(result[key], expected_outputs[key]) for key in result: # don't forget - we might have missed something! self.assertEqual(expected_outputs[key], result[key])