Debian Codegraph @

hyprtester/src/main.cpp

raw
1
2 // This is a tester for Hyprland. It will launch the built binary in ./build/Hyprland
3 // in headless mode and test various things.
4 // for now it's quite basic and limited, but will be expanded in the future.
5
6 // NOTE: This tester has to be ran from its directory!!
7
8 // Some TODO:
9 // - Add a plugin built alongside so that we can do more detailed tests (e.g. simulating keystrokes)
10 // - test coverage
11 // - maybe figure out a way to do some visual tests too?
12
13 // Required runtime deps for checks:
14 // - kitty
15 // - xeyes
16
17 #define INCLUDED_FROM_MAIN 1 // Prevent macro redefinition warnings from includes of "tests/*/tests.hpp"
18
19 #include "shared.hpp"
20 #include "hyprctlCompat.hpp"
21 #include "tests/main/tests.hpp"
22 #include "tests/clients/tests.hpp"
23 #include "tests/misc/tests.hpp"
24 #include "tests/shared.hpp"
25
26 #include <hyprutils/os/Process.hpp>
27 #include <hyprutils/memory/WeakPtr.hpp>
28 #include <hyprutils/memory/Casts.hpp>
29
30 #include <algorithm>
31 #include <cerrno>
32 #include <chrono>
33 #include <csignal>
34 #include <filesystem>
35 #include <memory>
36 #include <print>
37 #include <string>
38 #include <string_view>
39 #include <span>
40 #include <thread>
41 #include <vector>
42
43 #include "Log.hpp"
44
45 using namespace Hyprutils::OS;
46 using namespace Hyprutils::Memory;
47 using Path = std::filesystem::path;
48
49 #define SP CSharedPointer
50
51 namespace {
52 struct SSettings {
53 Path configPath;
54 Path binaryPath;
55 Path pluginPath;
56 std::vector<std::string> requestedTests;
57 };
58
59 struct STestsRunResult {
60 unsigned long long total;
61 std::vector<std::string> failedNames;
62 };
63 }
64
65 static SP<CProcess> hyprlandProc;
66
67 static bool launchHyprland(Path configPath, Path binaryPath) {
68 NLog::yellow("Launching Hyprland");
69 hyprlandProc = makeShared<CProcess>(binaryPath, std::vector<std::string>{"--config", configPath});
70 hyprlandProc->addEnv("HYPRLAND_HEADLESS_ONLY", "1");
71
72 NLog::yellow("Launched async process");
73
74 return hyprlandProc->runAsync();
75 }
76
77 static bool hyprlandAlive() {
78 NLog::yellow("hyprlandAlive");
79 return kill(hyprlandProc->pid(), 0) == 0 || errno != ESRCH;
80 }
81
82 [[noreturn]] static void helpAndDie(int exit_code) {
83 NLog::log("usage: hyprtester [--OPTION [VALUE]]... [TEST_NAMES].\n");
84 NLog::log(R"(Arguments:
85 --help -h - Show this message again
86 --config FILE -c FILE - Specify config file to use (default: './test.lua')
87 --binary FILE -b FILE - Specify Hyprland binary to use (default: '../build/Hyprland')
88 --plugin FILE -p FILE - Specify the location of the test plugin (default: './')
89 [TEST_NAMES] - Specify list of tests to run (separated by spaces).
90 If omitted, all tests will run.)");
91
92 std::exit(exit_code);
93 }
94
95 static Path validatePathOrDie(Path path) {
96 try {
97 if (!std::filesystem::is_regular_file(path)) {
98 throw std::exception();
99 }
100 } catch (...) {
101 std::println(stderr, "[ ERROR ] File '{}' is not accessible or not a regular file", path.string());
102 helpAndDie(EXIT_FAILURE);
103 }
104 return path;
105 }
106
107 static SSettings parseSettings(const std::span<const char*> args) {
108 static const auto cwd = std::filesystem::current_path();
109 SSettings settings{};
110
111 for (auto it = args.begin(); it < args.end(); it++) {
112 std::string_view value = *it;
113 if (value == "--config" || value == "-c") {
114 if (std::next(it) == args.end()) {
115 helpAndDie(EXIT_FAILURE);
116 }
117
118 settings.configPath = validatePathOrDie(*std::next(it));
119 it++;
120 } else if (value == "--binary" || value == "-b") {
121 if (std::next(it) == args.end()) {
122 helpAndDie(EXIT_FAILURE);
123 }
124
125 settings.binaryPath = validatePathOrDie(*std::next(it));
126 it++;
127 } else if (value == "--plugin" || value == "-p") {
128 if (std::next(it) == args.end()) {
129 helpAndDie(EXIT_FAILURE);
130 }
131
132 settings.pluginPath = validatePathOrDie(*std::next(it));
133 it++;
134 } else if (value == "--help" || value == "-h") {
135 helpAndDie(EXIT_SUCCESS);
136 } else if (!value.starts_with("-")) {
137 settings.requestedTests.emplace_back(value);
138 } else {
139 std::println(stderr, "[ ERROR ] Unknown option '{}' !", *it);
140 helpAndDie(EXIT_FAILURE);
141 }
142 }
143
144 // Default options
145 if (settings.configPath.empty())
146 settings.configPath = validatePathOrDie(cwd / "test.lua");
147 if (settings.binaryPath.empty())
148 settings.binaryPath = validatePathOrDie(cwd / "../build/Hyprland");
149 if (settings.pluginPath.empty())
150 settings.pluginPath = cwd;
151
152 return settings;
153 }
154
155 static bool preTestCleanup() {
156 bool failed = false;
157
158 if (!Tests::killAllWindows()) {
159 NLog::red("Internal failure: failed to kill all windows");
160 failed = true;
161 }
162 if (!Tests::killAllLayers()) {
163 NLog::red("Internal failure: failed to kill all layers");
164 failed = true;
165 }
166 if (getFromSocket("/reload") != "ok") {
167 NLog::red("Internal failure: failed to reload");
168 failed = true;
169 }
170 if (!getFromSocket("/activeworkspace").contains("workspace ID 1 (1)")) {
171 if (getFromSocket("/dispatch hl.dsp.focus({ workspace = '1' })") != "ok") {
172 NLog::red("Internal failure: failed to switch to workspace 1");
173 failed = true;
174 }
175 }
176 if (getFromSocket("/dispatch hl.dsp.cursor.move({ x = 960, y = 540 })") != "ok") {
177 NLog::red("Internal failure: failed to reset cursor position");
178 failed = true;
179 }
180
181 return !failed;
182 }
183
184 static STestsRunResult runTests(std::vector<std::shared_ptr<CTestCase>>& testCases) {
185 struct STestsRunResult res{.total = testCases.size(), .failedNames = {}};
186
187 for (auto& tc : testCases) {
188 // Clean up before every test
189 NLog::yellow("Cleaning up");
190
191 if (!preTestCleanup()) // damn it, something really went wrong
192 std::exit(1);
193
194 NLog::log("{}Running test {}", Colors::BLUE, tc->name());
195 tc->test();
196
197 if (tc->failed) {
198 NLog::red("Test failed!: {}", tc->name());
199 res.failedNames.emplace_back(std::format("{}:{}", tc->groupName(), tc->name()));
200 } else
201 NLog::green("Test passed: {}", tc->name());
202 }
203
204 return res;
205 }
206
207 static void cleanupAndReport(const STestsRunResult& tInfo) {
208 NLog::green("dispatching exit");
209 getFromSocket("/dispatch hl.dsp.exit()");
210
211 NLog::log("\nSummary:\n\tPASSED: {}{}{}/{}", Colors::GREEN, tInfo.total - tInfo.failedNames.size(), Colors::RESET, tInfo.total);
212 NLog::log("\tFAILED: {}{}{}/{}", Colors::RED, tInfo.failedNames.size(), Colors::RESET, tInfo.total);
213 if (!tInfo.failedNames.empty()) {
214 NLog::red("Failed tests:");
215 for (const auto& name : tInfo.failedNames) {
216 NLog::red("\t- {}", name);
217 }
218 }
219
220 kill(hyprlandProc->pid(), SIGKILL);
221 hyprlandProc.reset();
222 }
223
224 int main(int argc, char** argv, char** envp) {
225
226 std::span<const char*> args{const_cast<const char**>(argv + 1), sc<std::size_t>(argc - 1)};
227 const SSettings settings = parseSettings(args);
228
229 std::vector<std::shared_ptr<CTestCase>> requestedTestCases;
230 for (auto& test : settings.requestedTests) {
231 if (testCases.contains(test)) {
232 requestedTestCases.push_back(testCases.at(test));
233 } else {
234 NLog::red("ERROR: Unknown test name '{}'", Colors::RED, test);
235 return EXIT_FAILURE;
236 }
237 }
238 if (requestedTestCases.empty()) {
239 // When no tests are explicitly requested, run all tests.
240 // For convenience of log inspection, run tests group by group.
241 requestedTestCases = miscTestCases;
242 std::ranges::copy(clientTestCases, std::back_inserter(requestedTestCases));
243 std::ranges::copy(mainTestCases, std::back_inserter(requestedTestCases));
244 }
245
246 NLog::yellow("launching hl");
247 if (!launchHyprland(settings.configPath, settings.binaryPath)) {
248 NLog::red("well it failed");
249 return 1;
250 }
251
252 // hyprland has launched, let's check if it's alive after 10s
253 std::this_thread::sleep_for(std::chrono::milliseconds(10000));
254 NLog::yellow("slept for 10s");
255 if (!hyprlandAlive()) {
256 NLog::red("Hyprland failed to launch!");
257 return 1;
258 }
259
260 // wonderful, we are in. Let's get the instance signature.
261 NLog::yellow("trying to get INSTANCES");
262 const auto INSTANCES = instances();
263 if (INSTANCES.empty()) {
264 NLog::red("Hyprland failed to launch (2)");
265 return 1;
266 }
267
268 HIS = INSTANCES.back().id;
269 WLDISPLAY = INSTANCES.back().wlSocket;
270
271 NLog::yellow("trying to get create headless output");
272 const auto CREATE_HEADLESS_2 = getFromSocket("/output create headless HEADLESS-2");
273 if (CREATE_HEADLESS_2 != "ok" && CREATE_HEADLESS_2 != "Name already taken") {
274 NLog::red("Failed to create HEADLESS-2: {}", CREATE_HEADLESS_2);
275 getFromSocket("/dispatch hl.dsp.exit()");
276 return 1;
277 }
278
279 NLog::yellow("trying to load plugin");
280 if (const auto R = getFromSocket(std::format("/plugin load {}", settings.pluginPath.string())); R != "ok") {
281 NLog::red("Failed to load the test plugin: {}", R);
282 getFromSocket("/dispatch hl.dsp.exit()");
283 return 1;
284 }
285
286 NLog::yellow("Loaded plugin");
287
288 STestsRunResult result = runTests(requestedTestCases);
289
290 cleanupAndReport(result);
291
292 return result.failedNames.size() > 0;
293 }