diff --git a/data/settings.gd b/data/settings.gd index 484d27b..35536c4 100644 --- a/data/settings.gd +++ b/data/settings.gd @@ -181,10 +181,19 @@ func save_settings() -> void: GameLog.warning("Could not save the settings to %s: %s" % [path, error_string(error)]) -## Sets the play queue ID for [param server] and saves it. +## Sets the play queue ID for [param server] and saves it. Only the ID is changed in +## the file; the other settings in it stay as they were saved. func set_queue_id(server: String, id: String) -> void: queue_ids[server] = id - save_settings() + var path: String = saved_settings_path() + var file: ConfigFile = ConfigFile.new() + # A missing file is fine: it is written with only the queue ID + file.load(path) + file.set_value("queues", server, id) + DirAccess.make_dir_recursive_absolute(path.get_base_dir()) + var error: Error = file.save(path) + if error != OK: + GameLog.warning("Could not save the queue ID to %s: %s" % [path, error_string(error)]) ## Turns fullscreen on or off and saves it. diff --git a/tests/unit/test_settings_files.gd b/tests/unit/test_settings_files.gd index fb8f647..2f253d9 100644 --- a/tests/unit/test_settings_files.gd +++ b/tests/unit/test_settings_files.gd @@ -107,6 +107,36 @@ func test_queue_ids_are_saved_and_load_back() -> void: assert_dict(Settings.queue_ids).is_equal({"https://music.example.com": "queue-1"}) +func test_saving_a_queue_id_leaves_the_other_settings_as_saved() -> void: + Settings.winning_score = 10 + Settings.save_settings() + Settings.winning_score = 12 + + Settings.set_queue_id("https://music.example.com", "queue-1") + + var file: ConfigFile = _saved_file() + assert_int(file.get_value("quiz", "winning_score")).is_equal(10) + assert_str(file.get_value("queues", "https://music.example.com")).is_equal("queue-1") + + +func test_saving_a_queue_id_without_a_file_saves_only_the_queue_id() -> void: + Settings.set_queue_id("https://music.example.com", "queue-1") + + var file: ConfigFile = _saved_file() + assert_array(file.get_sections()).contains_exactly(["queues"]) + assert_str(file.get_value("queues", "https://music.example.com")).is_equal("queue-1") + + +func test_new_queue_id_replaces_only_its_server() -> void: + Settings.set_queue_id("https://music.example.com", "queue-1") + Settings.set_queue_id("http://localhost:8080", "local-1") + Settings.set_queue_id("https://music.example.com", "queue-2") + + var file: ConfigFile = _saved_file() + assert_str(file.get_value("queues", "https://music.example.com")).is_equal("queue-2") + assert_str(file.get_value("queues", "http://localhost:8080")).is_equal("local-1") + + func test_file_without_queues_keeps_the_current_queue_ids() -> void: Settings.save_settings() Settings.queue_ids["https://music.example.com"] = "queue-1"