def test_restore_file_bad_permission_dest( fs, # pylint: disable=invalid-name caplog, dir_name): ''' Test the restore_file function where user data directory (and later the "sets" directory) cannot be created because it's parent directory is unwritable. ''' # path to the directory under test target_dir = os.path.join(USER_DATA_DIR, dir_name) assert not os.path.exists(target_dir) # create the parent with restricted permissions parent_dir = os.path.dirname(target_dir) assert not os.path.exists(parent_dir) fs.CreateDirectory(parent_dir, 0o444) assert os.path.exists(parent_dir) # test we raise an exception caplog.set_level(logging.WARNING) with pytest.raises(InitializationError): util.restore_file("doesn't_matter") assert "Directory '{}' is inaccessible because of a permissions error".format( parent_dir) in caplog.text
def test_restore_file_success( fs, # pylint: disable=invalid-name caplog): ''' Test the restore_file function, expecting a successful copy. ''' # the source config file import pkg_resources real_set = "buffy" config = pkg_resources.resource_filename( "legendary", os.path.join("data", "{}.config".format(real_set))) assert not os.path.exists(config) fs.CreateFile(config) assert os.path.exists(config) # test copying the file when it doesn't previously exist caplog.set_level(logging.DEBUG) util.restore_file(real_set) # assert the success is logged and the file is copied assert "Configuration file for Legendary set '{}' restored!".format( real_set) in caplog.text assert os.path.exists(os.path.join(SETS_DIR, "{}.config".format(real_set))) # test copying the file again, overwriting util.restore_file(real_set) # assert the success is logged assert "Configuration file for Legendary set '{}' restored!".format( real_set) in caplog.text
def test_restore_file_bad_dst_filesystem_structure( fs, # pylint: disable=invalid-name monkeypatch): ''' Test the restore_file function where copying the config file cannot be done for a reason *other* than the two errors tested above - here we use that the sets "directory" already exists as a *file*. ''' # monkeypatch the create_directory function to get past monkeypatch.setattr(util, "create_directory", lambda directory: None) # create the "sets" directory as a *file* assert not os.path.exists(SETS_DIR) fs.CreateFile(SETS_DIR) assert os.path.exists(SETS_DIR) # the source config file import pkg_resources real_set = "buffy" config = pkg_resources.resource_filename( "legendary", os.path.join("data", "{}.config".format(real_set))) assert not os.path.exists(config) fs.CreateFile(config) assert os.path.exists(config) # test we raise an exception with pytest.raises(NotADirectoryError): util.restore_file(real_set)
def test_restore_file_cant_overwrite( fs, # pylint: disable=invalid-name caplog): ''' Test the restore_file function where the destination config file location is not writable. ''' # the source config file import pkg_resources real_set = "buffy" config = pkg_resources.resource_filename( "legendary", os.path.join("data", "{}.config".format(real_set))) assert not os.path.exists(config) fs.CreateFile(config) assert os.path.exists(config) # create the file in the sets sub-directory of the user's data directory, # but make it unwritable dst_file = os.path.join(SETS_DIR, "buffy.config") assert not os.path.exists(dst_file) fs.CreateFile(dst_file, 0o444) assert os.path.exists(dst_file) # test that we raise an exception when destination file is unwritable caplog.set_level(logging.WARNING) with pytest.raises(InitializationError): util.restore_file(real_set) # assert the logging of the error assert "Destination configuraion file '{}' for Legendary set '{}' inaccessible because of a permissions " \ "error".format(dst_file, real_set) in caplog.text
def test_restore_file_bad_src_filesystem_structure(fs): # pylint: disable=invalid-name ''' Test the restore_file function where the user data directory cannot be created for a reason *other* than the permissions error tested above - here we use that the parent "directory" already exists as a file. ''' # create the parent directory as a *file* parent_dir = os.path.dirname(USER_DATA_DIR) assert not os.path.exists(parent_dir) fs.CreateFile(parent_dir) assert os.path.exists(parent_dir) # test we raise an exception with pytest.raises(NotADirectoryError): util.restore_file("doesn't_matter")
def test_restore_file_no_orig( fs, # pylint: disable=invalid-name, unused-argument caplog): ''' Test the restore_file function where the source config file does not exist for copying to the user data directory. ''' # the dummy (source) config file import pkg_resources dummy_set = "foobar" config = pkg_resources.resource_filename( "legendary", os.path.join("data", "{}.config".format(dummy_set))) # test that we raise an exception when that file does not exist caplog.set_level(logging.WARNING) with pytest.raises(InitializationError): util.restore_file(dummy_set) # assert the logging of the error assert "Source configuration file '{}' for Legendary set '{}' does not " \ "exist!".format(config, dummy_set) in caplog.text