def test_back_saving(self): filename = os.path.join(tempfile.gettempdir(), "SectionManagerTestFile") # We need to use a bad filename or this will parse coalas .coafile gather_configuration(lambda *args: True, self.log_printer, arg_list=[ '-S', "save=" + escape(filename, '\\'), "-c=some_bad_filename" ]) with open(filename, "r") as f: lines = f.readlines() self.assertEqual(["[Default]\n", "config = some_bad_filename\n"], lines) gather_configuration(lambda *args: True, self.log_printer, arg_list=[ '-S', "save=true", "config=" + escape(filename, '\\'), "test.value=5" ]) with open(filename, "r") as f: lines = f.readlines() os.remove(filename) if os.path.sep == '\\': filename = escape(filename, '\\') self.assertEqual([ "[Default]\n", "config = " + filename + "\n", "\n", "[test]\n", "value = 5\n" ], lines)
def test_gather_configuration(self): args = (lambda *args: True, self.log_printer) # Passing the default coafile name only triggers a warning. gather_configuration(*args, arg_list=["-c abcdefghi/invalid/.coafile"]) # Using a bad filename explicitly exits coala. with self.assertRaises(SystemExit): gather_configuration( *args, arg_list=["-S", "test=5", "-c", "some_bad_filename"]) with make_temp() as temporary: sections, local_bears, global_bears, targets = ( gather_configuration(*args, arg_list=[ "-S", "test=5", "-c", escape(temporary, "\\"), "-s" ])) self.assertEqual( str(sections["default"]), "Default {config : " + repr(temporary) + ", save : " "'True', test : '5'}") with make_temp() as temporary: sections, local_bears, global_bears, targets = ( gather_configuration(*args, arg_list=[ "-S test=5", "-c " + escape(temporary, "\\"), "-b LineCountBear -s" ])) self.assertEqual(len(local_bears["default"]), 0)
def test_back_saving(self): filename = os.path.join(tempfile.gettempdir(), "SectionManagerTestFile") # We need to use a bad filename or this will parse coalas .coafile gather_configuration( lambda *args: True, self.log_printer, arg_list=['-S', "save=" + escape(filename, '\\'), "-c=some_bad_filename"]) with open(filename, "r") as f: lines = f.readlines() self.assertEqual(["[Default]\n", "config = some_bad_filename\n"], lines) gather_configuration( lambda *args: True, self.log_printer, arg_list=['-S', "save=true", "config=" + escape(filename, '\\'), "test.value=5"]) with open(filename, "r") as f: lines = f.readlines() os.remove(filename) if os.path.sep == '\\': filename = escape(filename, '\\') self.assertEqual(["[Default]\n", "config = " + filename + "\n", "\n", "[test]\n", "value = 5\n"], lines)
def test_gather_configuration(self): args = (lambda *args: True, self.log_printer) # Passing the default coafile name only triggers a warning. gather_configuration(*args, arg_list=["-c abcdefghi/invalid/.coafile"]) # Using a bad filename explicitly exits coala. with self.assertRaises(SystemExit): gather_configuration( *args, arg_list=["-S", "test=5", "-c", "some_bad_filename"]) with make_temp() as temporary: sections, local_bears, global_bears, targets = ( gather_configuration( *args, arg_list=["-S", "test=5", "-c", escape(temporary, "\\"), "-s"])) self.assertEqual(str(sections["default"]), "Default {config : " + repr(temporary) + ", save : " "'True', test : '5'}") with make_temp() as temporary: sections, local_bears, global_bears, targets = ( gather_configuration(*args, arg_list=["-S test=5", "-c " + escape(temporary, "\\"), "-b LineCountBear -s"])) self.assertEqual(len(local_bears["default"]), 0)
def create_arguments(filename, file, config_file, r_keep_comments: bool=True, r_keep_blank_lines: bool=True, r_braces_on_next_line: bool=False, r_use_arrows: bool=False, tab_width: int=SpacingHelper.DEFAULT_TAB_WIDTH, r_max_expression_length: int=0): """ :param r_keep_comments: Determines whether comments are kept or not. :param r_keep_blank_lines: Determines whether blank lines are kept or not. :param r_braces_on_next_line: Determines whether a brace should be placed on the next line. Example: If ``True``, ``` if (...) { ``` changes to ``` if (...) { ``` If ``False`` the brace is placed on the same line. :param r_use_arrows: Determines whether the assignment operator ``=`` should be replaced by an arrow ``<-`` or not. Example: If ``True``, ``x = 1`` changes to ``x <- 1``. :param tab_width: Number of spaces for indentation. :param r_max_expression_length: Maximum number of characters for an expression. Example: If ``20`` then ``` 1 + 1 + 1 + 1 + 1 + 1 + 1 ``` changes to ``` 1 + 1 + 1 + 1 + 1 + 1 + 1 ``` """ options = {'source="' + escape(filename, '"\\') + '"', 'blank=' + _map_to_r_bool(r_keep_blank_lines), 'brace.newline=' + _map_to_r_bool(r_braces_on_next_line), 'comment=' + _map_to_r_bool(r_keep_comments), 'arrow=' + _map_to_r_bool(r_use_arrows), 'indent=' + str(tab_width)} if r_max_expression_length: options.add('width.cutoff=' + str(r_max_expression_length)) rcode = 'library(formatR);formatR::tidy_source({})'.format( ','.join(options)) return '-e', rcode
def test_merge_defaults(self): with make_temp() as temporary: sections, local_bears, global_bears, targets = gather_configuration( lambda *args: True, self.log_printer, arg_list=["-S", "value=1", "test.value=2", "-c", escape(temporary, "\\")], ) self.assertEqual(sections["default"], sections["test"].defaults)
def test_merge_defaults(self): with make_temp() as temporary: sections, local_bears, global_bears, targets = ( gather_configuration(lambda *args: True, self.log_printer, arg_list=[ "-S", "value=1", "test.value=2", "-c", escape(temporary, "\\") ])) self.assertEqual(sections["default"], sections["test"].defaults)
def escape_path_argument(path, shell=get_shell_type()): """ Makes a raw path ready for using as parameter in a shell command (escapes illegal characters, surrounds with quotes etc.). :param path: The path to make ready for shell. :param shell: The shell platform to escape the path argument for. Possible values are "sh", "powershell", and "cmd" (others will be ignored and return the given path without modification). :return: The escaped path argument. """ if shell == "cmd": # If a quote (") occurs in path (which is illegal for NTFS file # systems, but maybe for others), escape it by preceding it with # a caret (^). return '"' + escape(path, '"', '^') + '"' elif shell == "sh": return escape(path, " ") else: # Any other non-supported system doesn't get a path escape. return path
def escape_path_argument(path, os=platform.system()): """ Makes a raw path ready for using as parameter in a shell command (escapes illegal characters, surrounds with quotes etc.). :param path: The path to make ready for shell. :param os: The shell platform to escape the path argument for. Possible values are "Windows", "Linux" and "Darwin" (others will be ignored and return the given path without modification). :return: The escaped path argument. """ if os == "Windows": # If a quote (") occurs in path (which is illegal for NTFS file # systems, but maybe for others), escape it by preceding it with # a caret (^). return '"' + escape(path, '"', "^") + '"' elif os == "Linux" or os == "Darwin": return escape(path, " ") else: # Any other non-supported system doesn't get a path escape. return path
def __write_key_val(self, keys, val): assert not self.__closed if keys == []: return if all(self.is_comment(key) for key in keys): self.__file.write(val + "\n") self.__wrote_newline = val == "" return # Add escape characters as appropriate keys = [escape(key, chain(['\\'], self.__key_value_delimiters, self.__comment_seperators, self.__key_delimiters, self.__section_override_delimiters)) for key in keys] val = escape(val, chain(['\\'], self.__comment_seperators)) self.__file.write((self.__key_delimiter + " ").join(keys) + " " + self.__key_value_delimiter + " " + val + "\n") self.__wrote_newline = False
def prepare_string_argument(string, shell=get_shell_type()): """ Prepares a string argument for being passed as a parameter on shell. On ``sh`` this function effectively encloses the given string with quotes (either '' or "", depending on content). :param string: The string to prepare for shell. :param shell: The shell platform to prepare string argument for. If it is not "sh" it will be ignored and return the given string without modification. :return: The shell-prepared string. """ if shell == "sh": return '"' + escape(string, '"') + '"' else: return string
def prepare_string_argument(string, os=platform.system()): """ Prepares a string argument for being passed as a parameter on shell. On Linux and Darwin this function effectively encloses the given string with quotes (either '' or "", depending on content). :param string: The string to prepare for shell. :param os: The shell platform to prepare string argument for. Possible "Linux" and "Darwin" (others will be ignored and return the given string without modification). :return: The shell-prepared string. """ if os == "Linux" or os == "Darwin": return '"' + escape(string, '"') + '"' else: return string
def test_different_path(self): no_git_dir = mkdtemp() self.git_commit("Add a very long shortlog for a bad project history.") os.chdir(no_git_dir) # When section doesn't have a project_dir self.assertEqual(self.run_uut(), []) git_error = self.msg_queue.get().message self.assertEqual(git_error[:4], "git:") # when section does have a project_dir self.section.append(Setting("project_dir", escape(self.gitdir, '\\'))) self.assertEqual(self.run_uut(), ["Shortlog of HEAD commit is 1 character(s) longer " "than the limit (51 > 50)."]) self.assertEqual(get_config_directory(self.section), self.gitdir) os.chdir(self.gitdir) os.rmdir(no_git_dir)
def create_arguments(filename, file, config_file): lintcode = ('import Lint.lintfile; lintfile("' + escape(filename, '"\\') + '")') return '-e', lintcode
def create_arguments(filename, file, config_file, r_keep_comments: bool=True, r_keep_blank_lines: bool=True, r_braces_on_next_line: bool=False, r_use_arrows: bool=False, tab_width: int=SpacingHelper.DEFAULT_TAB_WIDTH, r_max_expression_length: int=20): """ :param r_keep_comments: Determines whether comments are kept or not. :param r_keep_blank_lines: Determines whether blank lines are kept or not. :param r_braces_on_next_line: Determines whether a brace should be placed on the next line. Example: If ``True``, ``` if (...) { ``` changes to ``` if (...) { ``` If ``False`` the brace is placed on the same line. :param r_use_arrows: Determines if either the assign operator ``=`` or the arrow ``<-`` should be used. Example: If ``True``, ``x = 1`` changes to ``x <- 1``. :param tab_width: Number of spaces for indentation. :param r_max_expression_length: Maximum number of characters for an expression. Example: If ``20`` then ``` 1 + 1 + 1 + 1 + 1 + 1 + 1 ``` changes to ``` 1 + 1 + 1 + 1 + 1 + 1 + 1 ``` """ rcode = ('library(formatR);' 'formatR::tidy_source(' 'source="' + escape(filename, '"\\') + '",' 'comment={r_keep_comments},' 'blank={r_keep_blank_lines},' 'arrow={r_use_arrows},' 'brace.newline={r_braces_on_next_line},' 'indent=' '{tab_width}'.format(tab_width=tab_width, r_keep_comments=_map_to_r_bool( r_keep_comments), r_keep_blank_lines=_map_to_r_bool( r_keep_blank_lines), r_use_arrows=_map_to_r_bool( r_use_arrows), r_braces_on_next_line=_map_to_r_bool( r_braces_on_next_line))) # Disable r_max_expression_length if it is equal to 0 rcode += ('' if r_max_expression_length == 0 else ',width.cutoff=' + str(r_max_expression_length)) + ')' return '-e', rcode
def create_arguments(filename, file, config_file): lintcode = ('import Lint.lintfile; display(lintfile("' + escape(filename, '"\\') + '"))') return '-e', lintcode