Skip to content

Commit

Permalink
modules/hyprland/backend: handle empty json responses
Browse files Browse the repository at this point in the history
Fixes #3388
  • Loading branch information
khaneliman committed Jun 28, 2024
1 parent f6482c3 commit c08660d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/modules/hyprland/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,13 @@ std::string IPC::getSocket1Reply(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
10 changes: 8 additions & 2 deletions test/hyprland/backend.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include <cstdlib>
#if __has_include(<catch2/catch_test_macros.hpp>)
#include <catch2/catch_test_macros.hpp>
#else
#include <catch2/catch.hpp>
#endif

#include "fixtures/IPCTestFixture.hpp"
#include "modules/hyprland/backend.hpp"

namespace fs = std::filesystem;
namespace hyprland = waybar::modules::hyprland;
Expand Down Expand Up @@ -53,3 +51,11 @@ TEST_CASE_METHOD(IPCTestFixture, "XDGRuntimeDirExistsNoHyprDir", "[getSocketFold
// Assert expected result
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());
}
6 changes: 6 additions & 0 deletions test/hyprland/fixtures/IPCTestFixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ class IPCTestFixture : public hyprland::IPC {

private:
};

class IPCMock : public IPCTestFixture {
public:
// Mock getSocket1Reply to return an empty string
static std::string getSocket1Reply(const std::string& rq) { return ""; }
};

0 comments on commit c08660d

Please sign in to comment.