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 __write_key_val(self, keys, val, defaults): assert not self.__closed if keys == []: return if all(self.is_comment(key) for key in keys): self.__file.write(val + '\n') return # Add escape characters as appropriate keys = [escape(key, chain(['\\'], self.__key_value_delimiters, self.__comment_separators, self.__key_delimiters, self.__section_override_delimiters)) for key in keys] val = escape(val, chain(['\\'], self.__comment_separators)) append_keys = [] other_keys = [] for key in keys: if (defaults and (key in defaults and val.startswith(str(defaults[key])+','))): append_keys.append(key) else: other_keys.append(key) self.__write_keys_val_to_file(append_keys, other_keys, val, defaults)
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_back_saving(self): filename = os.path.join(tempfile.gettempdir(), 'SectionManagerTestFile') # We need to use a bad filename or this will parse coalas .coafile # Despite missing settings (coala isn't run) the file is saved with self.assertRaises(SystemExit): 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(['[cli]\n', 'config = some_bad_filename\n'], lines) with self.assertRaises(SystemExit): 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([ '[cli]\n', 'config = ' + filename + '\n', '[test]\n', 'value = 5\n' ], lines)
def test_gather_configuration(self): args = (lambda *args: True, self.log_printer) # Using incomplete settings (e.g. an invalid coafile) will error with self.assertRaises(SystemExit): 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.min_args)) self.assertEqual( str(sections['default']), "Default {bears : 'JavaTestBear', config : " + repr(temporary) + ", files : '*.java', save : 'True', test : '5'}") with make_temp() as temporary: sections, local_bears, global_bears, targets = ( gather_configuration(*args, arg_list=['-S test=5', '-f *.java', '-c ' + escape(temporary, '\\'), '-b LineCountBear -s'])) self.assertEqual(len(local_bears['default']), 0)
def test_gather_configuration(self): args = (lambda *args: True, self.log_printer) # Using incomplete settings (e.g. an invalid coafile) will error with self.assertRaises(SystemExit): 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.min_args ) self.assertEqual( str(sections["cli"]), "cli {bears : 'JavaTestBear', config : " + repr(temporary) + ", files : '*.java', save : 'True', test : '5'}", ) with make_temp() as temporary: sections, local_bears, global_bears, targets = gather_configuration( *args, arg_list=["-S test=5", "-f *.java", "-c " + escape(temporary, "\\"), "-b LineCountBear -s"] ) self.assertEqual(len(local_bears["cli"]), 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 # Despite missing settings (coala isn't run) the file is saved with self.assertRaises(SystemExit): 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(["[cli]\n", "config = some_bad_filename\n"], lines) with self.assertRaises(SystemExit): 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(["[cli]\n", "config = " + filename + "\n", "[test]\n", "value = 5\n"], lines)
def __write_key_val(self, keys, val, defaults): assert not self.__closed if keys == []: return if all(self.is_comment(key) for key in keys): self.__file.write(val + '\n') return # Add escape characters as appropriate keys = [ escape( key, chain(['\\'], self.__key_value_delimiters, self.__comment_separators, self.__key_delimiters, self.__section_override_delimiters)) for key in keys ] val = escape(val, chain(['\\'], self.__comment_separators)) append_keys = [] other_keys = [] for key in keys: if (defaults and (key in defaults and val.startswith(str(defaults[key]) + ','))): append_keys.append(key) else: other_keys.append(key) self.__write_keys_val_to_file(append_keys, other_keys, val, defaults)
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) # Using incomplete settings (e.g. an invalid coafile) will error with self.assertRaises(SystemExit): 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.min_args)) self.assertEqual( str(sections['cli']), "cli {bears : 'JavaTestBear', config : " + repr(temporary) + ", files : '*.java', save : 'True', test : '5'}") with make_temp() as temporary: sections, local_bears, global_bears, targets = ( gather_configuration(*args, arg_list=[ '-S test=5', '-f *.java', '-c ' + escape(temporary, '\\'), '-b LineCountBear -s' ])) self.assertEqual(len(local_bears['cli']), 0)
def __getitem__(self, item): value = getattr(self.lang, item) if isinstance(value, (list, tuple)): value = Setting(item, ', '.join(escape(val, ',') for val in value)) elif isinstance(value, dict): value = Setting(item, ', '.join( escape(key, ':,') + ': ' + escape(val, ':,') for key, val in value.items())) else: value = Setting(item, str(value)) return value
def __getitem__(self, item): value = getattr(self.lang, item) if isinstance(value, (list, tuple)): value = Setting(item, ', '.join(escape(val, ',') for val in value)) elif isinstance(value, dict): value = Setting( item, ', '.join( escape(key, ':,') + ': ' + escape(val, ':,') for key, val in value.items())) else: value = Setting(item, str(value)) return value
def test_append(self): with open(self.file, 'w', encoding='utf-8') as file: file.write(self.append_example_file) result_file = ['[defaults]\n', 'a = 4\n', 'b = 4,5,6\n', 'c = 4,5\n', 'd = 4\n', '[defaults.new]\n', 'b += 7\n', 'c += 6, 7\n', 'a, d += 5, 6, 7\n', '[cli]\n'] sections = load_configuration(['-c', escape(self.file, '\\')], self.log_printer)[0] del sections['cli'].contents['config'] self.uut.write_sections(sections) self.uut.close() with open(self.write_file_name, 'r') as f: lines = f.readlines() self.assertEqual(result_file, lines)
def test_write(self): result_file = ['[Section]\n', '[MakeFiles]\n', 'j, ANother = a\n', 'multiline\n', 'value\n', '; just a omment\n', '; just a omment\n', 'key\\ space = value space\n', 'key\\=equal = value=equal\n', 'key\\\\backslash = value\\\\backslash\n', 'key\\,comma = value,comma\n', 'key\\#hash = value\\#hash\n', 'key\\.dot = value.dot\n', 'a_default += val2\n', '[cli]\n', 'save = true\n', 'a_default, another = val\n', '# thats a comment\n', 'test = push\n', 't = \n'] sections = load_configuration(['-c', escape(self.file, '\\')], self.log_printer)[0] del sections['cli'].contents['config'] self.uut.write_sections(sections) self.uut.close() with open(self.write_file_name, 'r') as f: lines = f.readlines() self.assertEqual(result_file, lines)
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.min_args, ) self.assertEqual(sections["cli"], 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.min_args)) self.assertEqual(sections['cli'], 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 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.min_args)) self.assertEqual(sections['cli'], sections['test'].defaults)
def install_command(self): """ Creates the installation command for the instance of the class. >>> JuliaRequirement('Lint').install_command() 'julia -e \\'Pkg.add("Lint")\\'' :return: A string with the installation command. """ code = 'Pkg.add("{}")'.format(escape(self.package, '\\"')) args = ('julia', '-e', shlex.quote(code)) return ' '.join(args)
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') return # Add escape characters as appropriate keys = [escape(key, chain(['\\'], self.__key_value_delimiters, self.__comment_separators, self.__key_delimiters, self.__section_override_delimiters)) for key in keys] val = escape(val, chain(['\\'], self.__comment_separators)) self.__file.write((self.__key_delimiter + ' ').join(keys) + ' ' + self.__key_value_delimiter + ' ' + val + '\n')
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') return # Add escape characters as appropriate keys = [ escape( key, chain(['\\'], self.__key_value_delimiters, self.__comment_separators, self.__key_delimiters, self.__section_override_delimiters)) for key in keys ] val = escape(val, chain(['\\'], self.__comment_separators)) self.__file.write((self.__key_delimiter + ' ').join(keys) + ' ' + self.__key_value_delimiter + ' ' + val + '\n')
def is_installed(self): """ Checks if the dependency is installed. :return: ``True`` if dependency is installed, ``False`` otherwise. """ # We need to check explicitly if `nothing` is returned, as this happens # when the package is *registered*, but *not installed*. If it's not # even registered, julia will throw an exception which lets julia exit # with an error code different from 0. code = 'Pkg.installed("{}")==nothing?exit(1):exit(0)'.format( escape(self.package, '\\"')) args = ('julia', '-e', code) return not call_without_output(args)
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 test_back_saving(self): filename = os.path.join(tempfile.gettempdir(), 'SectionManagerTestFile') # We need to use a bad filename or this will parse coalas .coafile # Despite missing settings (coala isn't run) the file is saved with self.assertRaises(SystemExit): 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(['[cli]\n', 'config = some_bad_filename\n'], lines) with self.assertRaises(SystemExit): 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(['[cli]\n', 'config = ' + filename + '\n', '[test]\n', 'value = 5\n'], lines)
def test_append(self): with open(self.file, 'w', encoding='utf-8') as file: file.write(self.append_example_file) result_file = [ '[defaults]\n', 'a = 4\n', 'b = 4,5,6\n', 'c = 4,5\n', 'd = 4\n', '[defaults.new]\n', 'b += 7\n', 'c += 6, 7\n', 'a, d += 5, 6, 7\n', '[cli]\n' ] sections = load_configuration(['-c', escape(self.file, '\\')], self.log_printer)[0] del sections['cli'].contents['config'] self.uut.write_sections(sections) self.uut.close() with open(self.write_file_name, 'r') as f: lines = f.readlines() self.assertEqual(result_file, lines)
def test_write(self): result_file = [ '[Section]\n', '[MakeFiles]\n', 'j, ANother = a\n', 'multiline\n', 'value\n', '; just a omment\n', '; just a omment\n', 'key\\ space = value space\n', 'key\\=equal = value=equal\n', 'key\\\\backslash = value\\\\backslash\n', 'key\\,comma = value,comma\n', 'key\\#hash = value\\#hash\n', 'key\\.dot = value.dot\n', 'a_default += val2\n', '[cli]\n', 'save = true\n', 'a_default, another = val\n', '# thats a comment\n', 'test = push\n', 't = \n' ] sections = load_configuration(['-c', escape(self.file, '\\')], self.log_printer)[0] del sections['cli'].contents['config'] self.uut.write_sections(sections) self.uut.close() with open(self.write_file_name, 'r') as f: lines = f.readlines() self.assertEqual(result_file, lines)
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 shlex.quote(path) else: # Any other non-supported system doesn't get a path escape. return path
invalid_xml_url = load_testdata('concept-invalid.xml') dtd_url = 'http://docs.oasis-open.org/dita/v1.0.1/dtd/concept.dtd' XMLBearCorrectedTest = verify_local_bear(XMLBear, valid_files=(valid_xml_file, valid_xml_chars), invalid_files=(invalid_xml_file, invalid_xml_chars), tempfile_kwargs={'suffix': '.xml'}) XMLBearSchemaTest = verify_local_bear( XMLBear, valid_files=(valid_xml_path, ), invalid_files=(invalid_xml_schema, ), settings={'xml_schema': escape(schema_file_path, '\\')}, tempfile_kwargs={'suffix': '.xml'}) XMLBearDTDPathTest = verify_local_bear( XMLBear, valid_files=(valid_xml_path, ), invalid_files=(invalid_xml_dtd, ), settings={'xml_dtd': escape(dtd_file_path, '\\')}, tempfile_kwargs={'suffix': '.xml'}) XMLBearDTDUrlTest = verify_local_bear(XMLBear, valid_files=(valid_xml_url, ), invalid_files=(invalid_xml_url, ), settings={'xml_dtd': dtd_url}, tempfile_kwargs={'suffix': '.xml'})
test_directory) CPPCleanBearTest = verify_local_bear(CPPCleanBear, valid_files=(good_file, ), invalid_files=( bad_file, test_include_paths_file, ), tempfile_kwargs={'suffix': '.cpp'}) CPPCleanBearValidIncludeMultiplePathTest = verify_local_bear( CPPCleanBear, valid_files=(test_include_paths_file, ), invalid_files=(), settings={ 'include_paths': escape(get_testdirectory_name('headers1'), '\\') + ',' + escape(get_testdirectory_name('headers2'), '\\') }, tempfile_kwargs={'suffix': '.cpp'}) CPPCleanBearInvalidIncludeSinglePathTest = verify_local_bear( CPPCleanBear, valid_files=(), invalid_files=(test_include_paths_file, ), settings={ 'include_paths': escape(get_testdirectory_name('headers1'), '\\') }, tempfile_kwargs={'suffix': '.cpp'})
def get_testdirectory_name(test_directory): return os.path.join(os.path.dirname(__file__), 'test_files', test_directory) CPPCleanBearTest = verify_local_bear(CPPCleanBear, valid_files=(good_file,), invalid_files=(bad_file, test_include_paths_file,), tempfile_kwargs={'suffix': '.cpp'}) CPPCleanBearValidIncludeMultiplePathTest = verify_local_bear( CPPCleanBear, valid_files=(test_include_paths_file,), invalid_files=(), settings={'include_paths': escape(get_testdirectory_name('headers1'), '\\') + ',' + escape(get_testdirectory_name('headers2'), '\\')}, tempfile_kwargs={'suffix': '.cpp'}) CPPCleanBearInvalidIncludeSinglePathTest = verify_local_bear( CPPCleanBear, valid_files=(), invalid_files=(test_include_paths_file,), settings={'include_paths': escape(get_testdirectory_name('headers1'), '\\')}, tempfile_kwargs={'suffix': '.cpp'})
invalid_xml_relaxng = load_testdata('rng-error.xml') invalid_xml_url = load_testdata('concept-invalid.xml') dtd_url = 'http://docs.oasis-open.org/dita/v1.0.1/dtd/concept.dtd' XMLBearCorrectedTest = verify_local_bear( XMLBear, valid_files=(valid_xml_file, valid_xml_chars), invalid_files=(invalid_xml_file, invalid_xml_chars), tempfile_kwargs={'suffix': '.xml'}) XMLBearSchemaTest = verify_local_bear( XMLBear, valid_files=(valid_xml_path,), invalid_files=(invalid_xml_schema,), settings={'xml_schema': escape(schema_file_path, '\\')}, tempfile_kwargs={'suffix': '.xml'}) XMLBearDTDPathTest = verify_local_bear( XMLBear, valid_files=(valid_xml_path,), invalid_files=(invalid_xml_dtd,), settings={'xml_dtd': escape(dtd_file_path, '\\')}, tempfile_kwargs={'suffix': '.xml'}) XMLBearDTDUrlTest = verify_local_bear( XMLBear, valid_files=(valid_xml_url,), invalid_files=(invalid_xml_url,), settings={'xml_dtd': dtd_url}, tempfile_kwargs={'suffix': '.xml'})
def create_arguments(filename, file, config_file): return ('-e', 'library(lintr)', '-e', 'lintr::lint("' + escape(filename, '\\"') + '")')