def test_identifiers(self): self.create_config_file(self.tempfile, { "Iron Man": {}, "Whiplash": {}, }) cfbs = ConfigFileBackingStore(self.tempfile) self.assertEquals(cfbs.identifiers(), ["Iron Man", "Whiplash"])
def __init__(self, config_override=None, consoles_override=None, emulators_override=None): self.validated_base_environment = False self.validated_configuration = False self.logger = IceLogger() self.logger.debug("Initializing Ice") config_data_path = _path_with_override(config_override, "config.txt") consoles_data_path = _path_with_override(consoles_override, "consoles.txt") emulators_data_path = _path_with_override(emulators_override, "emulators.txt") self.config = Configuration( ConfigFileBackingStore(config_data_path), ConfigFileBackingStore(consoles_data_path), ConfigFileBackingStore(emulators_data_path), ) self.steam = Steam() # TODO: Query the list of users some other way self.users = self.steam.local_users() filesystem = Filesystem() parser = ROMParser(self.logger) self.rom_finder = ROMFinder(self.config, filesystem, parser) archive_data_path = Configuration.path_for_data_file("archive.json") managed_rom_archive = ManagedROMArchive(archive_data_path) self.shortcut_synchronizer = SteamShortcutSynchronizer( managed_rom_archive, self.logger) provider = CombinedProvider( LocalProvider(self.logger), ConsoleGridProvider(self.logger), ) self.grid_updater = SteamGridUpdater(provider, self.logger)
def test_get(self): self.create_config_file(self.tempfile, {"Iron Man": { "identity": "Tony Stark", "alignment": "good", }}) cfbs = ConfigFileBackingStore(self.tempfile) self.assertEqual(cfbs.get("Iron Man", "identity", ""), "Tony Stark")
def test_get_keys_are_case_insensitive(self): self.create_config_file(self.tempfile, {"Iron Man": { "identity": "Tony Stark", "alignment": "good", }}) cfbs = ConfigFileBackingStore(self.tempfile) self.assertEqual(cfbs.get("Iron Man", "IDENTITY", ""), "Tony Stark")
def test_get(self): self.create_config_file( self.tempfile, {"Iron Man": { "identity": "Tony Stark", "alignment": "good", }}) cfbs = ConfigFileBackingStore(self.tempfile) self.assertEqual(cfbs.get("Iron Man", "identity", ""), "Tony Stark")
def test_get_keys_are_case_insensitive(self): self.create_config_file( self.tempfile, {"Iron Man": { "identity": "Tony Stark", "alignment": "good", }}) cfbs = ConfigFileBackingStore(self.tempfile) self.assertEqual(cfbs.get("Iron Man", "IDENTITY", ""), "Tony Stark")
def test_save_creates_new_file_when_path_originally_dne(self): self.assertFalse(os.path.isfile(self.tempfile)) cfbs = ConfigFileBackingStore(self.tempfile) cfbs.add_identifier("Iron Man") cfbs.set("Iron Man", "identity", "Tony Stark") cfbs.save() self.assertTrue(os.path.isfile(self.tempfile))
def test_save_raises_ioerror_when_cant_make_new_file(self): temppath = os.path.join(self.tempdir, "extra", "directories", "test.ini") cfbs = ConfigFileBackingStore(temppath) cfbs.add_identifier("Iron Man") cfbs.set("Iron Man", "identity", "Tony Stark") with self.assertRaises(IOError): cfbs.save()
def _load_roms_for_test(self, directory): """Takes the ROMs located in `directory/ROMs` and moves them into the ROMs directory specified in the provided config.txt file.""" config_path = self._test_config_path(directory, 'config') c = configuration.from_store(ConfigFileBackingStore(config_path)) target_roms_directory = self.sandbox.adjusted_path(c.roms_directory) source_roms_directory = os.path.join(directory, 'ROMs') shutil.copytree(source_roms_directory, target_roms_directory)
def test_raises_ioerror_when_permission_denied(self): self.create_config_file(self.tempfile, {}) mode = os.stat(self.tempfile)[stat.ST_MODE] new_mode = mode & ~stat.S_IWUSR & ~stat.S_IWGRP & ~stat.S_IWOTH os.chmod(self.tempfile, new_mode) cfbs = ConfigFileBackingStore(self.tempfile) cfbs.add_identifier("Iron Man") cfbs.set("Iron Man", "identity", "Tony Stark") with self.assertRaises(IOError): cfbs.save()
def test_save_modifies_contents_of_file(self): self.create_config_file(self.tempfile, {"Iron Man": { "identity": "Tony Stark" }}) old_contents = self.file_contents(self.tempfile) cfbs = ConfigFileBackingStore(self.tempfile) cfbs.set("Iron Man", "alignment", "good") cfbs.add_identifier("Whiplash") cfbs.set("Whiplash", "alignment", "evil") cfbs.save() new_contents = self.file_contents(self.tempfile) self.assertNotIn("Whiplash", old_contents) self.assertIn("Whiplash", new_contents) self.assertNotIn("alignment", old_contents) self.assertIn("alignment", new_contents) self.assertIn("good", new_contents) self.assertIn("evil", new_contents)
def __init__(self, steam, filesystem, options): """Valid options for creating an IceEngine are as follows: * config - The path to the config file to use. Searches the default paths for 'config.txt' otherwise * consoles - The path to the consoles file to use. Searches the default paths for 'consoles.txt' if none is provided * emulators - The path to the emulators file to use. Searches the default paths for 'emulators.txt' if none is provided """ self.validated_base_environment = False self.validated_configuration = False self.filesystem = filesystem logger.debug("Initializing Ice") config_data_path = _path_with_override(filesystem, options.config, "config.txt") consoles_data_path = _path_with_override(filesystem, options.consoles, "consoles.txt") emulators_data_path = _path_with_override(filesystem, options.emulators, "emulators.txt") self.config = Configuration( ConfigFileBackingStore(config_data_path), ConfigFileBackingStore(consoles_data_path), ConfigFileBackingStore(emulators_data_path), filesystem, ) self.steam = steam parser = ROMParser() self.rom_finder = ROMFinder(self.config, filesystem, parser) archive_data_path = paths.highest_precedent_data_file( filesystem, "archive.json") managed_rom_archive = ManagedROMArchive(archive_data_path) self.shortcut_synchronizer = SteamShortcutSynchronizer( managed_rom_archive) provider = CombinedProvider( LocalProvider(), ConsoleGridProvider(), ) self.grid_updater = SteamGridUpdater(provider)
def __init__(self, options): """Valid options for creating an IceEngine are as follows: * config - The path to the config file to use. Searches the default paths for 'config.txt' otherwise * consoles - The path to the consoles file to use. Searches the default paths for 'consoles.txt' if none is provided * emulators - The path to the emulators file to use. Searches the default paths for 'emulators.txt' if none is provided * verbose - Turn on debug logging. """ self.validated_base_environment = False self.validated_configuration = False self.logger = IceLogger(verbose=options.verbose) self.logger.debug("Initializing Ice") config_data_path = _path_with_override(options.config, "config.txt") consoles_data_path = _path_with_override(options.consoles, "consoles.txt") emulators_data_path = _path_with_override(options.emulators, "emulators.txt") self.config = Configuration( ConfigFileBackingStore(config_data_path), ConfigFileBackingStore(consoles_data_path), ConfigFileBackingStore(emulators_data_path), ) self.steam = Steam() # TODO: Query the list of users some other way self.users = self.steam.local_users() filesystem = Filesystem() parser = ROMParser(self.logger) self.rom_finder = ROMFinder(self.config, filesystem, parser) archive_data_path = Configuration.path_for_data_file("archive.json") managed_rom_archive = ManagedROMArchive(archive_data_path) self.shortcut_synchronizer = SteamShortcutSynchronizer( managed_rom_archive, self.logger) provider = CombinedProvider( LocalProvider(self.logger), ConsoleGridProvider(self.logger), ) self.grid_updater = SteamGridUpdater(provider, self.logger)
def test_save_modifies_contents_of_file(self): self.create_config_file(self.tempfile, { "Iron Man": {"identity": "Tony Stark"} }) old_contents = self.file_contents(self.tempfile) cfbs = ConfigFileBackingStore(self.tempfile) cfbs.set("Iron Man", "alignment", "good") cfbs.add_identifier("Whiplash") cfbs.set("Whiplash", "alignment", "evil") cfbs.save() new_contents = self.file_contents(self.tempfile) self.assertNotIn("Whiplash", old_contents) self.assertIn("Whiplash", new_contents) self.assertNotIn("alignment", old_contents) self.assertIn("alignment", new_contents) self.assertIn("good", new_contents) self.assertIn("evil", new_contents)
def create_backing_store(self, data): bs = ConfigFileBackingStore(self.tempfile) for ident in data.keys(): bs.add_identifier(ident) ident_data = data[ident] for key in ident_data.keys(): value = ident_data[key] bs.set(ident, key, value) bs.save() return bs
def _load_roms_for_test(self, directory): """Takes the ROMs located in `directory/ROMs` and moves them into the ROMs directory specified in the provided config.txt file.""" # TODO: This is extremely non-kosher. The fact that I have to do this # suggests that something is very wrong with my Configuration object. # # Knowing that object I'm tempted to agree. c = Configuration( ConfigFileBackingStore(self._test_config_path(directory, 'config')), None, None, None, ) target_roms_directory = self.sandbox.adjusted_path(c.roms_directory()) source_roms_directory = os.path.join(directory, 'ROMs') shutil.copytree(source_roms_directory, target_roms_directory)
def test_empty_file(self): self.create_config_file(self.tempfile, {}) cfbs = ConfigFileBackingStore(self.tempfile) self.assertEqual(cfbs.identifiers(), [])
class BackedObjectManagerTests(unittest.TestCase): def setUp(self): self.tempdir = tempfile.mkdtemp() self.tempfile = os.path.join(self.tempdir, "test.ini") self.backing_store = ConfigFileBackingStore(self.tempfile) self.manager = BackedObjectManager(self.backing_store, BackedObjectBackedObjectAdapter()) def tearDown(self): shutil.rmtree(self.tempdir) def populate_backing_store(self, data): for ident in data.keys(): self.backing_store.add_identifier(ident) ident_data = data[ident] for key in ident_data.keys(): value = ident_data[key] self.backing_store.set(ident, key, value) self.backing_store.save() def test_reading_data_from_store(self): self.populate_backing_store({ "Iron Man": { "identity": "Tony Stark", }, "War Machine": { "identity": "James Rhodes", "alias": "Rhodey", }, }) iron_man = self.manager.find("Iron Man") war_machine = self.manager.find("War Machine") self.assertIsNotNone(iron_man) self.assertEquals(iron_man.identifier, "Iron Man") self.assertEquals(iron_man.backed_value("identity"), "Tony Stark") self.assertIsNotNone(war_machine) self.assertEquals(war_machine.identifier, "War Machine") self.assertEquals(war_machine.backed_value("identity"), "James Rhodes") self.assertEquals(war_machine.backed_value("alias"), "Rhodey") def test_all(self): self.populate_backing_store({ "Iron Man": {}, "War Machine": {}, }) all_objects = self.manager.all() self.assertIs(len(all_objects), 2) obj1 = all_objects[0] obj2 = all_objects[1] self.assertFalse(obj1.identifier == obj2.identifier) self.assertIn(obj1.identifier, ["Iron Man", "War Machine"]) self.assertIn(obj2.identifier, ["Iron Man", "War Machine"]) def test_find_returns_none_with_invalid_identifier(self): im = self.manager.find("Iron Man") self.assertIsNone(self.manager.find("Iron Man")) def test_find_returns_non_none_with_valid_identifier(self): self.populate_backing_store({"Iron Man": {}}) self.assertIsNotNone(self.manager.find("Iron Man")) def test_find_returns_same_object_between_calls(self): self.populate_backing_store({"Iron Man": {}}) first_result = self.manager.find("Iron Man") self.assertIsNotNone(first_result) second_result = self.manager.find("Iron Man") self.assertIsNotNone(second_result) self.assertIs(first_result, second_result) def test_find_after_set_object_for_identifier_returns_same_object_that_was_set( self): war_machine = BackedObject(self.backing_store, "War Machine") self.manager.set_object_for_identifier(war_machine, "War Machine") self.assertEquals(self.manager.find("War Machine"), war_machine)
def test_empty_file(self): self.create_config_file(self.tempfile, {}) cfbs = ConfigFileBackingStore(self.tempfile) self.assertEquals(cfbs.identifiers(), [])
def setUp(self): self.tempdir = tempfile.mkdtemp() self.tempfile = os.path.join(self.tempdir, "test.ini") self.backing_store = ConfigFileBackingStore(self.tempfile) self.manager = BackedObjectManager(self.backing_store, BackedObjectBackedObjectAdapter())
def test_config_file_backing_store_can_read_saved_file(self): cfbs = ConfigFileBackingStore(self.tempfile) cfbs.add_identifier("Iron Man") cfbs.set("Iron Man", "alignment", "good") cfbs.add_identifier("Whiplash") cfbs.set("Whiplash", "alignment", "evil") cfbs.save() new_cfbs = ConfigFileBackingStore(self.tempfile) self.assertIn("Iron Man", new_cfbs.identifiers()) self.assertIn("Whiplash", new_cfbs.identifiers()) self.assertEquals("good", new_cfbs.get("Iron Man", "alignment")) self.assertEquals("evil", new_cfbs.get("Whiplash", "alignment"))
def test_get_returns_default_when_section_dne(self): self.create_config_file(self.tempfile, {"Iron Man": {}}) cfbs = ConfigFileBackingStore(self.tempfile) self.assertEqual(cfbs.get("Superman", "identity", "Unknown"), "Unknown")
def test_keys_raises_valueerror_when_section_dne(self): self.create_config_file(self.tempfile, {}) cfbs = ConfigFileBackingStore(self.tempfile) with self.assertRaises(ValueError): cfbs.keys("Iron Man")
def test_get_returns_empty_string_when_value_is_emptry_string(self): self.create_config_file(self.tempfile, {"Iron Man": { "identity": "" }}) cfbs = ConfigFileBackingStore(self.tempfile) self.assertEqual(cfbs.get("Iron Man", "identity", "Unknown"), "")
def test_get_returns_empty_string_when_value_is_emptry_string(self): self.create_config_file(self.tempfile, {"Iron Man": {"identity": ""}}) cfbs = ConfigFileBackingStore(self.tempfile) self.assertEqual(cfbs.get("Iron Man", "identity", "Unknown"), "")
def test_config_file_backing_store_can_read_saved_file(self): cfbs = ConfigFileBackingStore(self.tempfile) cfbs.add_identifier("Iron Man") cfbs.set("Iron Man", "alignment", "good") cfbs.add_identifier("Whiplash") cfbs.set("Whiplash", "alignment", "evil") cfbs.save() new_cfbs = ConfigFileBackingStore(self.tempfile) self.assertIn("Iron Man", new_cfbs.identifiers()) self.assertIn("Whiplash", new_cfbs.identifiers()) self.assertEqual("good", new_cfbs.get("Iron Man", "alignment")) self.assertEqual("evil", new_cfbs.get("Whiplash", "alignment"))
def test_added_identifiers_show_up_in_subsequent_calls(self): self.create_config_file(self.tempfile, {}) cfbs = ConfigFileBackingStore(self.tempfile) cfbs.add_identifier("Iron Man") self.assertIn("Iron Man", cfbs.identifiers())
def test_remove_identifier_does_nothing_when_identifer_dne(self): self.create_config_file(self.tempfile, {}) cfbs = ConfigFileBackingStore(self.tempfile) self.assertNotIn("Iron Man", cfbs.identifiers()) cfbs.remove_identifier("Iron Man") self.assertNotIn("Iron Man", cfbs.identifiers())
def setUp(self): self.tempdir = tempfile.mkdtemp() self.tempfile = os.path.join(self.tempdir, "test.ini") self.backing_store = ConfigFileBackingStore(self.tempfile) self.adapter = BackedObjectBackedObjectAdapter() self.manager = BackedObjectManager(self.backing_store, self.adapter)
def test_add_identifier_raises_valueerror_when_identifier_exists(self): self.create_config_file(self.tempfile, {"Iron Man": {}}) cfbs = ConfigFileBackingStore(self.tempfile) with self.assertRaises(ValueError): cfbs.add_identifier("Iron Man")
def test_removed_identifiers_dont_show_up_in_subsequent_calls(self): self.create_config_file(self.tempfile, {"Iron Man": {}}) cfbs = ConfigFileBackingStore(self.tempfile) cfbs.remove_identifier("Iron Man") self.assertNotIn("Iron Man", cfbs.identifiers())
class BackedObjectManagerTests(unittest.TestCase): def setUp(self): self.tempdir = tempfile.mkdtemp() self.tempfile = os.path.join(self.tempdir, "test.ini") self.backing_store = ConfigFileBackingStore(self.tempfile) self.adapter = BackedObjectBackedObjectAdapter() self.manager = BackedObjectManager(self.backing_store, self.adapter) def tearDown(self): shutil.rmtree(self.tempdir) def populate_backing_store(self, data): for ident in data.keys(): self.backing_store.add_identifier(ident) ident_data = data[ident] for key in ident_data.keys(): value = ident_data[key] self.backing_store.set(ident, key, value) self.backing_store.save() def test_reading_data_from_store(self): self.populate_backing_store({ "Iron Man": { "identity": "Tony Stark", }, "War Machine": { "identity": "James Rhodes", "alias": "Rhodey", }, }) iron_man = self.manager.find("Iron Man") war_machine = self.manager.find("War Machine") self.assertIsNotNone(iron_man) self.assertEquals(iron_man.identifier, "Iron Man") self.assertEquals(iron_man.backed_value("identity"), "Tony Stark") self.assertIsNotNone(war_machine) self.assertEquals(war_machine.identifier, "War Machine") self.assertEquals(war_machine.backed_value("identity"), "James Rhodes") self.assertEquals(war_machine.backed_value("alias"), "Rhodey") def test_all(self): self.populate_backing_store({ "Iron Man": {}, "War Machine": {}, }) all_objects = self.manager.all() self.assertIs(len(all_objects), 2) obj1 = all_objects[0] obj2 = all_objects[1] self.assertFalse(obj1.identifier == obj2.identifier) self.assertIn(obj1.identifier, ["Iron Man", "War Machine"]) self.assertIn(obj2.identifier, ["Iron Man", "War Machine"]) def test_find_returns_none_with_invalid_identifier(self): im = self.manager.find("Iron Man") self.assertIsNone(self.manager.find("Iron Man")) def test_find_returns_non_none_with_valid_identifier(self): self.populate_backing_store({"Iron Man": {}}) self.assertIsNotNone(self.manager.find("Iron Man")) def test_find_returns_same_object_between_calls(self): self.populate_backing_store({"Iron Man": {}}) first_result = self.manager.find("Iron Man") self.assertIsNotNone(first_result) second_result = self.manager.find("Iron Man") self.assertIsNotNone(second_result) self.assertIs(first_result, second_result) def test_find_after_set_object_for_identifier_returns_same_object_that_was_set(self): war_machine = BackedObject(self.backing_store, "War Machine") self.manager.set_object_for_identifier(war_machine, "War Machine") self.assertEquals(self.manager.find("War Machine"), war_machine) def test_all_doesnt_include_objects_where_verification_fails(self): self.populate_backing_store({ "Iron Man": {}, "War Machine": {}, "Whiplash": {}, }) self.adapter.verifier = lambda obj: obj.identifier.startswith("W") identifiers = map(lambda obj: obj.identifier, self.manager.all()) self.assertEqual(len(identifiers), 2) self.assertIn("War Machine", identifiers) self.assertIn("Whiplash", identifiers) def test_find_returns_none_for_object_where_verification_fails(self): self.populate_backing_store({"Iron Man": {}}) self.adapter.verifier = lambda obj: False self.assertIsNone(self.manager.find("Iron Man"))