Skip to content

Commit 8281dcc

Browse files
committed
refactor(CustomisationGenerator): Consolidate Pi Connect configuration paths
- Moved Pi Connect configuration path and deployment key filename constants to the header file for better accessibility. - Updated the implementation to utilize these constants in script generation and testing, ensuring consistency across the codebase. - Removed hardcoded strings in favor of the new constants to enhance maintainability and clarity.
1 parent 96ba66e commit 8281dcc

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

src/customization_generator.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111

1212
namespace rpi_imager {
1313

14-
constexpr auto PI_CONNECT_CONFIG_PATH = ".config/com.raspberrypi.connect";
15-
constexpr auto PI_CONNECT_DEPLOY_KEY_FILENAME = "deploy.key";
16-
1714
QString CustomisationGenerator::shellQuote(const QString& value) {
1815
QString t = value;
1916
t.replace("'", "'\"'\"'");

src/customization_generator.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
namespace rpi_imager {
1414

15+
// Raspberry Pi Connect configuration paths
16+
constexpr auto PI_CONNECT_CONFIG_PATH = ".config/com.raspberrypi.connect";
17+
constexpr auto PI_CONNECT_DEPLOY_KEY_FILENAME = "auth.key";
18+
1519
/**
1620
* @brief Generates firstrun.sh and cloud-init customisation scripts for Raspberry Pi images
1721
*

src/test/customization_generator_test.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ TEST_CASE("CustomisationGenerator Raspberry Pi Connect", "[customization]") {
277277
QByteArray script = CustomisationGenerator::generateSystemdScript(settings, token);
278278
QString scriptStr = QString::fromUtf8(script);
279279

280-
REQUIRE_THAT(scriptStr.toStdString(), ContainsSubstring("com.raspberrypi.connect"));
281-
REQUIRE_THAT(scriptStr.toStdString(), ContainsSubstring("deploy.key"));
280+
REQUIRE_THAT(scriptStr.toStdString(), ContainsSubstring(PI_CONNECT_CONFIG_PATH));
281+
REQUIRE_THAT(scriptStr.toStdString(), ContainsSubstring(PI_CONNECT_DEPLOY_KEY_FILENAME));
282282
REQUIRE_THAT(scriptStr.toStdString(), ContainsSubstring("test-token-12345"));
283283
REQUIRE_THAT(scriptStr.toStdString(), ContainsSubstring("rpi-connect-signin.service"));
284284
}
@@ -448,8 +448,8 @@ TEST_CASE("CustomisationGenerator handles null/empty piConnect token", "[customi
448448
QString scriptStr = QString::fromUtf8(script);
449449

450450
// Should not include Pi Connect setup if token is empty
451-
REQUIRE_FALSE(scriptStr.contains("com.raspberrypi.connect"));
452-
REQUIRE_FALSE(scriptStr.contains("deploy.key"));
451+
REQUIRE_FALSE(scriptStr.contains(PI_CONNECT_CONFIG_PATH));
452+
REQUIRE_FALSE(scriptStr.contains(PI_CONNECT_DEPLOY_KEY_FILENAME));
453453
}
454454

455455
TEST_CASE("CustomisationGenerator handles invalid keyboard layout", "[customization][negative]") {
@@ -597,13 +597,15 @@ TEST_CASE("CustomisationGenerator generates cloud-init user-data with Pi Connect
597597
QString yaml = QString::fromUtf8(userdata);
598598

599599
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("write_files:"));
600-
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("- path: /home/testuser/.config/com.raspberrypi.connect/deploy.key"));
600+
QString expectedPath = QString("- path: /home/testuser/") + PI_CONNECT_CONFIG_PATH + "/" + PI_CONNECT_DEPLOY_KEY_FILENAME;
601+
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring(expectedPath.toStdString()));
601602
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("permissions: '0600'"));
602603
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("owner: testuser:testuser"));
603604
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("content: |"));
604605
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("test-token-abcd-1234"));
605606
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("runcmd:"));
606-
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring("install -o testuser -m 700 -d /home/testuser/.config/com.raspberrypi.connect"));
607+
QString expectedInstallDir = QString("install -o testuser -m 700 -d /home/testuser/") + PI_CONNECT_CONFIG_PATH;
608+
REQUIRE_THAT(yaml.toStdString(), ContainsSubstring(expectedInstallDir.toStdString()));
607609
}
608610

609611
TEST_CASE("CustomisationGenerator generates cloud-init network-config with WiFi", "[cloudinit][network]") {
@@ -701,6 +703,6 @@ TEST_CASE("CustomisationGenerator cloud-init handles empty Pi Connect token", "[
701703

702704
// Should not include write_files or runcmd for Pi Connect
703705
REQUIRE_FALSE(yaml.contains("write_files:"));
704-
REQUIRE_FALSE(yaml.contains("com.raspberrypi.connect"));
706+
REQUIRE_FALSE(yaml.contains(PI_CONNECT_CONFIG_PATH));
705707
}
706708

0 commit comments

Comments
 (0)