KWinScriptFiles.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <catch2/catch_test_macros.hpp>
  2. #include <algorithm>
  3. #include <chrono>
  4. #include <filesystem>
  5. #include <fstream>
  6. #include <string>
  7. #include <cerrno>
  8. #include <csignal>
  9. #include <fcntl.h>
  10. #include <sys/file.h>
  11. #include <unistd.h>
  12. #include "WallpaperEngine/Desktop/KWinScript.h"
  13. using WallpaperEngine::Desktop::KWinScript;
  14. using namespace std::chrono_literals;
  15. namespace {
  16. struct Directory {
  17. std::filesystem::path path;
  18. Directory () {
  19. path = std::filesystem::temp_directory_path () / ("lwe-kwin-test-" + std::to_string (getpid ()));
  20. std::filesystem::create_directories (path);
  21. }
  22. ~Directory () { std::filesystem::remove_all (path); }
  23. std::filesystem::path make (const std::string& name, std::chrono::seconds age = 60s) const {
  24. const auto file = path / name;
  25. std::ofstream (file) << "// script\n";
  26. std::filesystem::last_write_time (file, std::filesystem::file_time_type::clock::now () - age);
  27. return file;
  28. }
  29. };
  30. // a pid nothing runs under: engines' file names end in one, and the sweep spares a script whose pid is alive
  31. std::string deadPid (int offset = 0) {
  32. for (int pid = 4000000 - offset; pid > 1000; pid--) {
  33. if (kill (pid, 0) != 0 && errno == ESRCH) {
  34. return std::to_string (pid);
  35. }
  36. }
  37. FAIL ("no free pid");
  38. return {};
  39. }
  40. bool contains (const std::vector<std::string>& names, const std::string& name) {
  41. return std::ranges::find (names, name) != names.end ();
  42. }
  43. } // namespace
  44. TEST_CASE ("A script file nobody holds belongs to an engine that is gone") {
  45. const Directory dir;
  46. const auto pid = deadPid ();
  47. dir.make ("lwe-cursor-input-" + pid + ".js");
  48. dir.make ("lwe-window-states-" + pid + ".js");
  49. const auto stale = KWinScript::findStale (dir.path);
  50. CHECK (stale.size () == 2);
  51. CHECK (contains (stale, "lwe-cursor-input-" + pid));
  52. CHECK (contains (stale, "lwe-window-states-" + pid));
  53. }
  54. TEST_CASE ("A script file a running engine holds is left alone") {
  55. const Directory dir;
  56. const auto pid = deadPid ();
  57. const auto held = dir.make ("lwe-cursor-input-" + pid + ".js");
  58. // what KWinScript does for as long as the engine lives
  59. const int lock = open (held.c_str (), O_RDONLY);
  60. REQUIRE (lock >= 0);
  61. REQUIRE (flock (lock, LOCK_SH) == 0);
  62. CHECK_FALSE (contains (KWinScript::findStale (dir.path), "lwe-cursor-input-" + pid));
  63. // the engine ends, however it does
  64. close (lock);
  65. CHECK (contains (KWinScript::findStale (dir.path), "lwe-cursor-input-" + pid));
  66. }
  67. TEST_CASE ("A file an engine is still starting up with is not taken for stale") {
  68. const Directory dir;
  69. const auto pid = deadPid ();
  70. dir.make ("lwe-cursor-input-" + pid + ".js", 0s);
  71. CHECK (KWinScript::findStale (dir.path, 2s).empty ());
  72. CHECK (contains (KWinScript::findStale (dir.path, 0s), "lwe-cursor-input-" + pid));
  73. }
  74. TEST_CASE ("The script of an older engine that still runs is left alone") {
  75. const Directory dir;
  76. // no lock to be seen, the pid is what says it lives (this process stands in for it)
  77. dir.make ("lwe-cursor-input-" + std::to_string (getpid ()) + ".js");
  78. CHECK (KWinScript::findStale (dir.path).empty ());
  79. // a Flatpak's tag is not a pid and falls back on the lock alone
  80. dir.make ("lwe-window-states-abc123_" + std::to_string (getpid ()) + ".js");
  81. CHECK (contains (KWinScript::findStale (dir.path), "lwe-window-states-abc123_" + std::to_string (getpid ())));
  82. }
  83. TEST_CASE ("Only the engine's own script files count") {
  84. const Directory dir;
  85. dir.make ("other-script.js");
  86. dir.make ("lwe-cursor-input-9.txt");
  87. std::filesystem::create_directories (dir.path / "lwe-folder.js");
  88. CHECK (KWinScript::findStale (dir.path).empty ());
  89. CHECK (KWinScript::findStale (dir.path / "missing").empty ());
  90. }