Skip to content

Commit 024a5de

Browse files
annejanclaude
andauthored
test: address five reviewer nits on tst_settings and tst_util (#1502)
Five of seven CodeRabbit findings on tst_settings.cpp and tst_util.cpp were valid; applying them and skipping two with a rationale. tst_settings.cpp: - getPassStore: tighten the "plausible path" predicate. The old `startsWith('/') || contains('/')` was redundant and Unix-only; switch to `QDir::isAbsolutePath || contains('/') || contains('\\')` so Windows paths with backslashes also satisfy the check. - setAndGetSavestate (profile-keys check): loop over both profile1 and profile2 instead of asserting just on profile1. Same git-options assertions now cover both entries. tst_util.cpp: - CHI_SQUARE_PERMISSIVE_THRESHOLD_DF9: add a comment explaining the threshold derivation (above p=0.995 critical value ~23.59 for df=9 to reduce false failures without losing real bias detection). - isValidKeyIdInvalid: collapse the 6-line ASSUMED_MAX_KEY_ID_LENGTH / TOO_LONG_KEY_ID_LENGTH dance into a single `kTooLongKeyIdLength = 41` constexpr with a one-line comment. - SshAuthSockGuard: move from `namespace {}` to a named `namespace testutils`. Qualifies all seven usage sites. Skipped: - tst_settings::getPasswordConfigurationDefault: CodeRabbit wanted to compare against a default-constructed PasswordConfiguration. Verified: this fails when the developer's persistent QSettings hold a non-default config (the test class itself warns "Non-portable mode detected: tests may modify persistent user settings"). The current `>= 0` check is intentionally permissive for non-isolated test runs. Keep it. - tst_settings isUseGit assertion flip: CodeRabbit's suggested rewrite changes the assertions to claim isUseGit auto-detects .git regardless of the default arg. Verified against qtpasssettings.cpp:707 — the auto-detect branch is guarded by `&& defaultValue`, so it only fires when default=true. The current assertions match the production behaviour. Suggested rewrite would break them. Build clean, 113/113 settings + 136/136 util tests pass. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 13f7782 commit 024a5de

2 files changed

Lines changed: 34 additions & 34 deletions

File tree

tests/auto/settings/tst_settings.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,9 @@ void tst_settings::setAndGetGeometry() {
158158

159159
void tst_settings::getPassStore() {
160160
QString store = QtPassSettings::getPassStore();
161-
QVERIFY2(store.isEmpty() || store.startsWith("/") || store.contains("/"),
162-
"Pass store should be empty or a plausible path");
161+
const bool plausiblePath = store.isEmpty() || QDir::isAbsolutePath(store) ||
162+
store.contains('/') || store.contains('\\');
163+
QVERIFY2(plausiblePath, "Pass store should be empty or a plausible path");
163164
}
164165

165166
void tst_settings::setAndGetPassStore() {
@@ -512,18 +513,21 @@ void tst_settings::setAndGetMultipleProfiles() {
512513
QVERIFY(readProfiles.contains("profile2"));
513514
QVERIFY(readProfiles["profile1"].contains("path"));
514515
QVERIFY(readProfiles["profile2"].contains("path"));
515-
// Verify new git options are in profile (issue #112)
516-
QVERIFY(readProfiles["profile1"].contains("useGit"));
517-
QVERIFY(readProfiles["profile1"].contains("autoPush"));
518-
QVERIFY(readProfiles["profile1"].contains("autoPull"));
519-
// Verify git options default to empty (unset) - compare QString directly
520-
// instead of toInt() which treats "true" as 0
521-
QVERIFY2(readProfiles["profile1"].value("useGit").isEmpty(),
522-
"useGit should default to empty");
523-
QVERIFY2(readProfiles["profile1"].value("autoPush").isEmpty(),
524-
"autoPush should default to empty");
525-
QVERIFY2(readProfiles["profile1"].value("autoPull").isEmpty(),
526-
"autoPull should default to empty");
516+
// Verify new git options are in all profiles (issue #112)
517+
const QStringList profileNames = {"profile1", "profile2"};
518+
for (const QString &profileName : profileNames) {
519+
QVERIFY(readProfiles[profileName].contains("useGit"));
520+
QVERIFY(readProfiles[profileName].contains("autoPush"));
521+
QVERIFY(readProfiles[profileName].contains("autoPull"));
522+
// Verify git options default to empty (unset) - compare QString directly
523+
// instead of toInt() which treats "true" as 0
524+
QVERIFY2(readProfiles[profileName].value("useGit").isEmpty(),
525+
"useGit should default to empty");
526+
QVERIFY2(readProfiles[profileName].value("autoPush").isEmpty(),
527+
"autoPush should default to empty");
528+
QVERIFY2(readProfiles[profileName].value("autoPull").isEmpty(),
529+
"autoPull should default to empty");
530+
}
527531
}
528532

529533
void tst_settings::profileGitOptions() {

tests/auto/util/tst_util.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ static constexpr int DISTRIBUTION_MAX_PERCENT = 120;
4040
static constexpr int PERCENT_BASE = 100;
4141
static constexpr int RANDOMNESS_TEST_SAMPLE_COUNT = 200;
4242
static constexpr int RANDOMNESS_TEST_PASSWORD_LENGTH = 32;
43+
// Permissive chi-square cutoff for df=9: set above the p=0.995 critical
44+
// value (~23.59) to reduce false test failures while still catching
45+
// meaningful distribution bias.
4346
static constexpr double CHI_SQUARE_PERMISSIVE_THRESHOLD_DF9 = 30.0;
4447

4548
/**
@@ -1221,19 +1224,12 @@ void tst_util::isValidKeyIdWithEmail() {
12211224
}
12221225

12231226
void tst_util::isValidKeyIdInvalid() {
1224-
// NOTE: 40 is intentional and derived from the current Util::isValidKeyId
1225-
// implementation detail: its key-id regex accepts 8-40 hexadecimal
1226-
// characters (optionally with a 0x prefix). This test verifies that a
1227-
// value just above that upper bound is rejected.
1228-
//
1229-
// Keep boundary expectations aligned with production validation rules.
1230-
// This test checks that one character above the configured max is rejected.
1231-
// If Util::isValidKeyId's accepted range changes, update this constant.
1232-
constexpr int ASSUMED_MAX_KEY_ID_LENGTH = 40;
1233-
constexpr int TOO_LONG_KEY_ID_LENGTH = ASSUMED_MAX_KEY_ID_LENGTH + 1;
1227+
// Boundary check: one character above the currently accepted maximum
1228+
// hexadecimal key-id length should be rejected.
1229+
constexpr int kTooLongKeyIdLength = 41; // current max (40) + 1
12341230
QVERIFY(!Util::isValidKeyId(""));
12351231
QVERIFY(!Util::isValidKeyId("short"));
1236-
QVERIFY(!Util::isValidKeyId(QString(TOO_LONG_KEY_ID_LENGTH, 'a')));
1232+
QVERIFY(!Util::isValidKeyId(QString(kTooLongKeyIdLength, 'a')));
12371233
QVERIFY(!Util::isValidKeyId("invalidchars!"));
12381234
QVERIFY(!Util::isValidKeyId("space in key"));
12391235
}
@@ -2080,7 +2076,7 @@ void tst_util::grepImitatePassInvalidRegexEmitsEmpty() {
20802076
// RAII helper restores SSH_AUTH_SOCK + the override setting around each test
20812077
// so we don't pollute the developer's environment or each other's state.
20822078
// ---------------------------------------------------------------------------
2083-
namespace {
2079+
namespace testutils {
20842080
class SshAuthSockGuard {
20852081
public:
20862082
SshAuthSockGuard()
@@ -2103,13 +2099,13 @@ class SshAuthSockGuard {
21032099
QByteArray prevEnv_;
21042100
QString prevOverride_;
21052101
};
2106-
} // namespace
2102+
} // namespace testutils
21072103

21082104
/**
21092105
* @brief getter/setter roundtrip for sshAuthSockOverride.
21102106
*/
21112107
void tst_util::sshAuthSockOverrideRoundtrip() {
2112-
SshAuthSockGuard guard;
2108+
testutils::SshAuthSockGuard guard;
21132109
const QString sentinel =
21142110
QStringLiteral("/tmp/qtpass-test-sock-") + QUuid::createUuid().toString();
21152111
QtPassSettings::setSshAuthSockOverride(sentinel);
@@ -2122,7 +2118,7 @@ void tst_util::sshAuthSockOverrideRoundtrip() {
21222118
* @brief default value of getSshAuthSockOverride is empty.
21232119
*/
21242120
void tst_util::sshAuthSockOverrideEmptyByDefault() {
2125-
SshAuthSockGuard guard;
2121+
testutils::SshAuthSockGuard guard;
21262122
QtPassSettings::setSshAuthSockOverride(QString{});
21272123
QCOMPARE(QtPassSettings::getSshAuthSockOverride(), QString{});
21282124
}
@@ -2132,7 +2128,7 @@ void tst_util::sshAuthSockOverrideEmptyByDefault() {
21322128
* already set.
21332129
*/
21342130
void tst_util::initialiseSshAuthSockHonoursExistingEnv() {
2135-
SshAuthSockGuard guard;
2131+
testutils::SshAuthSockGuard guard;
21362132
const QByteArray existing("/tmp/qtpass-existing-sock");
21372133
qputenv("SSH_AUTH_SOCK", existing);
21382134
// Even if an override is configured, the existing env wins.
@@ -2147,7 +2143,7 @@ void tst_util::initialiseSshAuthSockHonoursExistingEnv() {
21472143
* unset.
21482144
*/
21492145
void tst_util::initialiseSshAuthSockUsesOverride() {
2150-
SshAuthSockGuard guard;
2146+
testutils::SshAuthSockGuard guard;
21512147
qunsetenv("SSH_AUTH_SOCK");
21522148
const QString override = QStringLiteral("/tmp/qtpass-override-sock");
21532149
QtPassSettings::setSshAuthSockOverride(override);
@@ -2162,7 +2158,7 @@ void tst_util::initialiseSshAuthSockUsesOverride() {
21622158
* the resulting state is consistent.
21632159
*/
21642160
void tst_util::initialiseSshAuthSockNoOverrideNoEnvProbes() {
2165-
SshAuthSockGuard guard;
2161+
testutils::SshAuthSockGuard guard;
21662162
qunsetenv("SSH_AUTH_SOCK");
21672163
QtPassSettings::setSshAuthSockOverride(QString{});
21682164
Util::initialiseSshAuthSock();
@@ -2184,7 +2180,7 @@ void tst_util::initialiseSshAuthSockNoOverrideNoEnvProbes() {
21842180
* the user starts their agent after launching QtPass).
21852181
*/
21862182
void tst_util::initialiseSshAuthSockOverrideSkipsAgentValidation() {
2187-
SshAuthSockGuard guard;
2183+
testutils::SshAuthSockGuard guard;
21882184
qunsetenv("SSH_AUTH_SOCK");
21892185
// A path that almost certainly has no listener — validation would fail.
21902186
// Override should win regardless.
@@ -2201,7 +2197,7 @@ void tst_util::initialiseSshAuthSockOverrideSkipsAgentValidation() {
22012197
* it should fall through to the auto-probe.
22022198
*/
22032199
void tst_util::initialiseSshAuthSockEmptyOverrideFallsThrough() {
2204-
SshAuthSockGuard guard;
2200+
testutils::SshAuthSockGuard guard;
22052201
qunsetenv("SSH_AUTH_SOCK");
22062202
QtPassSettings::setSshAuthSockOverride(QString{});
22072203
Util::initialiseSshAuthSock();

0 commit comments

Comments
 (0)