modules/hyprland/backend: handle empty json responses

Fixes https://github.com/Alexays/Waybar/issues/3388
This commit is contained in:
Austin Horstman 2024-06-28 13:11:50 -05:00
parent f6482c36dc
commit c08660d837
No known key found for this signature in database
3 changed files with 21 additions and 3 deletions

View File

@ -218,7 +218,13 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
} }
Json::Value IPC::getSocket1JsonReply(const std::string& rq) { Json::Value IPC::getSocket1JsonReply(const std::string& rq) {
return parser_.parse(getSocket1Reply("j/" + rq)); std::string reply = getSocket1Reply("j/" + rq);
if (reply.empty()) {
return {};
}
return parser_.parse(reply);
} }
} // namespace waybar::modules::hyprland } // namespace waybar::modules::hyprland

View File

@ -1,4 +1,3 @@
#include <cstdlib>
#if __has_include(<catch2/catch_test_macros.hpp>) #if __has_include(<catch2/catch_test_macros.hpp>)
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#else #else
@ -6,7 +5,6 @@
#endif #endif
#include "fixtures/IPCTestFixture.hpp" #include "fixtures/IPCTestFixture.hpp"
#include "modules/hyprland/backend.hpp"
namespace fs = std::filesystem; namespace fs = std::filesystem;
namespace hyprland = waybar::modules::hyprland; namespace hyprland = waybar::modules::hyprland;
@ -53,3 +51,11 @@ TEST_CASE_METHOD(IPCTestFixture, "XDGRuntimeDirExistsNoHyprDir", "[getSocketFold
// Assert expected result // Assert expected result
REQUIRE(actualPath == expectedPath); REQUIRE(actualPath == expectedPath);
} }
TEST_CASE_METHOD(IPCMock, "getSocket1JsonReply handles empty response", "[getSocket1JsonReply]") {
std::string request = "test_request";
Json::Value jsonResponse = getSocket1JsonReply(request);
REQUIRE(jsonResponse.isNull());
}

View File

@ -14,3 +14,9 @@ class IPCTestFixture : public hyprland::IPC {
private: private:
}; };
class IPCMock : public IPCTestFixture {
public:
// Mock getSocket1Reply to return an empty string
static std::string getSocket1Reply(const std::string& rq) { return ""; }
};