-
Notifications
You must be signed in to change notification settings - Fork 52
Fix duplicate extension config entries #867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xbmc4lyfe
wants to merge
4
commits into
nzbgetcom:develop
Choose a base branch
from
xbmc4lyfe:codex/fix-588-config-growth
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+199
−6
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4e2fde5
Fix duplicate extension config entries
xbmc4lyfe 2da9d5a
Remove existing duplicate config lines when saving config
xbmc4lyfe 0309227
Keep the last value when collapsing duplicate config lines
xbmc4lyfe a63573f
Merge branch 'develop' into codex/fix-588-config-growth
dnzbk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| /* | ||
| * This file is part of nzbget. See <https://nzbget.com>. | ||
| * | ||
| * Copyright (C) 2026 Denis <denis@nzbget.com> | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; either version 2 of the License, or | ||
| * (at your option) any later version. | ||
| */ | ||
|
|
||
| #include "nzbget.h" | ||
|
|
||
| #include <boost/test/unit_test.hpp> | ||
| #include <fstream> | ||
| #include <sstream> | ||
| #include "Options.h" | ||
| #include "ScriptConfig.h" | ||
|
|
||
| BOOST_AUTO_TEST_SUITE(ExtensionTest) | ||
|
|
||
| struct TempConfigFile | ||
| { | ||
| fs::path path = fs::temp_directory_path() / fs::make_unique_filename(); | ||
|
|
||
| ~TempConfigFile() | ||
| { | ||
| fs::error_code error; | ||
| fs::remove(path, error); | ||
| } | ||
| }; | ||
|
|
||
| struct OptionsGuard | ||
| { | ||
| Options* oldOptions = g_Options; | ||
|
|
||
| ~OptionsGuard() | ||
| { | ||
| g_Options = oldOptions; | ||
| } | ||
| }; | ||
|
|
||
| BOOST_AUTO_TEST_CASE(SaveConfigDoesNotAppendDuplicateOptionNames) | ||
| { | ||
| TempConfigFile configFile; | ||
| { | ||
| std::ofstream output(configFile.path); | ||
| output << "# existing config\n"; | ||
| } | ||
|
|
||
| OptionsGuard optionsGuard; | ||
| Options options("nzbget", configFile.path.string().c_str(), true, nullptr, nullptr); | ||
| g_Options = &options; | ||
|
|
||
| Options::OptEntries optEntries; | ||
| optEntries.emplace_back("Extension.Option", "first"); | ||
| optEntries.emplace_back("extension.option", "second"); | ||
| ScriptConfig scriptConfig; | ||
|
|
||
| BOOST_REQUIRE(scriptConfig.SaveConfig(&optEntries)); | ||
| std::string firstContents; | ||
| { | ||
| std::ifstream firstFile(configFile.path); | ||
| std::stringstream contents; | ||
| contents << firstFile.rdbuf(); | ||
| firstContents = contents.str(); | ||
| } | ||
| // one line only, carrying the last entry's value - Options::SetOption | ||
| // resolves duplicates the same way, so the file matches what nzbget loads | ||
| BOOST_CHECK_EQUAL(firstContents, "# existing config\nExtension.Option=second\n"); | ||
|
|
||
| BOOST_REQUIRE(scriptConfig.SaveConfig(&optEntries)); | ||
| std::string secondContents; | ||
| { | ||
| std::ifstream secondFile(configFile.path); | ||
| std::stringstream contents; | ||
| contents << secondFile.rdbuf(); | ||
| secondContents = contents.str(); | ||
| } | ||
| BOOST_CHECK_EQUAL(secondContents, firstContents); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_CASE(SaveConfigRemovesDuplicateLinesFromDamagedConfig) | ||
| { | ||
| TempConfigFile configFile; | ||
| { | ||
| std::ofstream output(configFile.path); | ||
| output << "# existing config\n" | ||
| << "Server1.Active=yes\n" | ||
| << "Server2.Active=yes\n" | ||
| << "Server2.Active=yes\n" | ||
| << "server2.active=yes\n" | ||
| << "Server2.Host=news.example.com\n"; | ||
| } | ||
|
|
||
| OptionsGuard optionsGuard; | ||
| Options options("nzbget", configFile.path.string().c_str(), true, nullptr, nullptr); | ||
| g_Options = &options; | ||
|
|
||
| Options::OptEntries optEntries; | ||
| optEntries.emplace_back("Server1.Active", "yes"); | ||
| optEntries.emplace_back("Server2.Active", "no"); | ||
| optEntries.emplace_back("Server2.Host", "news.example.com"); | ||
| ScriptConfig scriptConfig; | ||
|
|
||
| BOOST_REQUIRE(scriptConfig.SaveConfig(&optEntries)); | ||
| std::string contents; | ||
| { | ||
| std::ifstream file(configFile.path); | ||
| std::stringstream buffer; | ||
| buffer << file.rdbuf(); | ||
| contents = buffer.str(); | ||
| } | ||
| BOOST_CHECK_EQUAL(contents, | ||
| "# existing config\n" | ||
| "Server1.Active=yes\n" | ||
| "Server2.Active=no\n" | ||
| "Server2.Host=news.example.com\n"); | ||
| } | ||
|
|
||
| // When a config file carries duplicate lines for one option, Options::SetOption | ||
| // overwrites the same entry while parsing, so the LAST line is the value nzbget | ||
| // actually runs on. Collapsing the duplicates must preserve that value, | ||
| // otherwise saving a damaged config silently changes settings (issue #588). | ||
| BOOST_AUTO_TEST_CASE(SaveConfigKeepsLastValueWhenConfigHasDuplicateLines) | ||
| { | ||
| TempConfigFile configFile; | ||
| { | ||
| std::ofstream output(configFile.path); | ||
| output << "# existing config\n" | ||
| << "Server2.Active=yes\n" | ||
| << "Server2.Name=xsusenet\n" | ||
| << "server2.active=no\n"; | ||
| } | ||
|
|
||
| OptionsGuard optionsGuard; | ||
| Options options("nzbget", configFile.path.string().c_str(), true, nullptr, nullptr); | ||
| g_Options = &options; | ||
|
|
||
| // as loadconfig reports it: raw file lines, duplicates included | ||
| Options::OptEntries optEntries; | ||
| optEntries.emplace_back("Server2.Active", "yes"); | ||
| optEntries.emplace_back("Server2.Name", "xsusenet"); | ||
| optEntries.emplace_back("server2.active", "no"); | ||
| ScriptConfig scriptConfig; | ||
|
|
||
| BOOST_REQUIRE(scriptConfig.SaveConfig(&optEntries)); | ||
| std::string contents; | ||
| { | ||
| std::ifstream file(configFile.path); | ||
| std::stringstream buffer; | ||
| buffer << file.rdbuf(); | ||
| contents = buffer.str(); | ||
| } | ||
| // one line per option, keeping the first occurrence's name and position | ||
| // but the last occurrence's value - exactly what Options::SetOption does | ||
| BOOST_CHECK_EQUAL(contents, | ||
| "# existing config\n" | ||
| "Server2.Active=no\n" | ||
| "Server2.Name=xsusenet\n"); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_SUITE_END() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please drop the (issue #588) references from the codebase.