def test_fileproxy_init(self): with prepare_file([], None) as (_, file): url = os.path.normcase(os.getcwd()) fileproxy = FileProxy(file, url, 'coala') self.assertEqual(fileproxy.version, -1) self.assertEqual(fileproxy.contents(), 'coala') self.assertEqual(fileproxy.workspace, url)
def test_file_cache_proxy_integration(self, debug=False): with bear_test_module(), \ prepare_file(['disk-copy\n'], None) as (_, filename): memory_data = 'in-memory\n' proxy = FileProxy(filename, None, memory_data) proxymap = FileProxyMap([proxy]) self.cache.set_proxymap(proxymap) results, exitcode, file_dicts = run_coala( console_printer=ConsolePrinter(), log_printer=LogPrinter(), arg_list=( '-c', os.devnull, '-f', filename, '-b', 'TestBear', ), autoapply=False, debug=debug, cache=self.cache) self.assertEqual(exitcode, 0) self.assertEqual(len(results), 1) # run_coala() output's name is always lower case self.assertEqual(file_dicts['cli'][filename.lower()], (memory_data, ))
def test_fileproxy_str(self): with prepare_file([], None) as (_, file): empty_fileproxy = FileProxy(file) gen_str = (f'<FileProxy {empty_fileproxy.filename}, ' f'{empty_fileproxy.version}>') self.assertEqual(gen_str, str(empty_fileproxy))
def test_get_file_dict_with_proxy_map(self): with prepare_file([], None) as (_, file): proxy = FileProxy(file, None, 'coala\n') proxymap = FileProxyMap([proxy]) self.cache.set_proxymap(proxymap) file_dict = self.cache.get_file_dict([file]) self.assertEqual(file_dict, {file: ('coala\n', )})
def test_fileproxy_str(self): with prepare_file([], None) as (_, file): empty_fileproxy = FileProxy(file) gen_str = '<FileProxy {}, {}>'.format(empty_fileproxy.filename, empty_fileproxy.version) self.assertEqual(gen_str, str(empty_fileproxy))
def test_fileproxy_from_file(self): with prepare_file(['coala\n'], None) as (_, file): fileproxy = FileProxy.from_file(file, None) self.assertEqual(fileproxy.version, -1) self.assertEqual(fileproxy.workspace, None) self.assertEqual(fileproxy.contents(), 'coala\n') self.assertEqual(fileproxy.filename, os.path.normcase(file))
def test_fileproxy_relative_name(self): with prepare_file(['coala'], None) as (_, file): relative_url = os.path.relpath(file, __file__) with self.assertRaises(ValueError) as context: FileProxy(relative_url) self.assertEqual('expecting absolute filename', str(context.exception))
def test_fileproxy_binary_file(self): with make_temp() as filename: data = bytearray([120, 3, 255, 0, 100]) with open(filename, 'wb') as file: file.write(data) proxy = FileProxy.from_file(filename, None, binary=True) self.assertEqual(proxy.filename, os.path.normcase(filename)) self.assertEqual(proxy.contents(), data) self.assertEqual(proxy.lines(), (data, ))
def test_fileproxy_binary_file(self): with make_temp() as filename: data = bytearray([120, 3, 255, 0, 100]) with open(filename, 'wb') as file: file.write(data) proxy = FileProxy.from_file(filename, None, binary=True) self.assertEqual(proxy.filename, os.path.normcase(filename)) self.assertEqual(proxy.contents(), data) self.assertEqual(proxy.lines(), (data,))
def test_fileproxy_replace(self): with prepare_file(['coala\n'], None) as (_, file): fileproxy = FileProxy.from_file(file, '.') self.assertEqual(fileproxy.version, -1) self.assertEqual(fileproxy.contents(), 'coala\n') self.assertTrue(fileproxy.replace('coala-rocks', 1)) self.assertEqual(fileproxy.contents(), 'coala-rocks') self.assertFalse(fileproxy.replace('bears-rocks', 1)) self.assertEqual(fileproxy.contents(), 'coala-rocks') self.assertFalse(fileproxy.replace('coala-mountains', 0)) self.assertEqual(fileproxy.contents(), 'coala-rocks')
def test_proxymap_add(self): proxymap = self.empty_proxymap() with self.assertRaises(TypeError): proxymap.add(123) is False with self.assertRaises(TypeError): proxymap.add('coala') is False proxy_one = self.random_proxy() self.assertTrue(proxymap.add(proxy_one)) proxy_two = FileProxy(proxy_one.filename, '.', 'coala-rocks') self.assertFalse(proxymap.add(proxy_two, replace=False)) self.assertTrue(proxymap.add(proxy_two, replace=True))
def random_proxy(self, contents=['coala\n']): with prepare_file(contents, None) as (_, file): return FileProxy.from_file(file, None)
def test_fileproxy_hash(self): with prepare_file(['coala\n', 'bears\n'], None) as (_, file): proxy = FileProxy.from_file(file, None) self.assertEqual(hash(proxy), hash(os.path.normcase(file)))
def test_fileproxy_lines(self): with prepare_file(['coala\n', 'bears\n'], None) as (lines, file): proxy = FileProxy.from_file(file, None) self.assertEqual(proxy.lines(), tuple(lines))
def test_fileproxy_get_disk_contents(self): with prepare_file(['coala\n'], None) as (_, file): proxy = FileProxy(file) contents = proxy.get_disk_contents() self.assertEqual(contents, 'coala\n')
def test_fileproxy_clear(self): with prepare_file(['coala'], None) as (_, file): fileproxy = FileProxy.from_file(file, None) fileproxy.clear() self.assertEqual(fileproxy.contents(), '')
def test_file_from_file_missing_file(self): with prepare_file([], None) as (_, file): with self.assertRaises(OSError): FileProxy.from_file(file+'coala', '.')
def test_file_from_file_missing_file(self): with prepare_file([], None) as (_, file): with self.assertRaises(OSError): FileProxy.from_file(file + 'coala', '.')
def test_fileproxy_normcase(self): with prepare_file([], None) as (_, file): fileproxy = FileProxy(file, None, 'coala') self.assertEqual(fileproxy.filename, os.path.normcase(file))