Issue159Tests.inc 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Issue #159: searching folders for a project's missing file (GenesiSF.sf2) on a slow HDD froze LYNX and it closed.
  2. // The window's search hands its matches over as they are found, and a later, better match replaces an earlier one; the
  3. // SoundFont index's folder walk stops when asked, at once, and passes over folders it can't read.
  4. #include <filesystem>
  5. #include <fstream>
  6. #if ! defined(_WIN32)
  7. #include <unistd.h>
  8. #endif
  9. #include "core/MissingFiles.h"
  10. #include "core/SoundFontIndex.h"
  11. namespace issue159
  12. {
  13. // a fake folder tree: folder → (name, isFolder)
  14. using Tree = std::map<std::string, std::vector<std::pair<std::string, bool>>>;
  15. auto lister (const Tree& tree, int* calls = nullptr)
  16. {
  17. return [&tree, calls] (const std::string& folder, const std::function<bool (const std::string&, bool)>& onEntry) {
  18. if (calls != nullptr)
  19. ++*calls;
  20. if (const auto it = tree.find (folder); it != tree.end())
  21. for (const auto& [name, isFolder] : it->second)
  22. if (! onEntry (folder + "/" + name, isFolder))
  23. return;
  24. };
  25. }
  26. } // namespace issue159
  27. TEST (issue159_missing_file_search_streams_its_matches)
  28. {
  29. using namespace lynx;
  30. std::vector<MissingFile> missing;
  31. addMissingFile (missing, "C:\\Sounds\\Genesis\\GenesiSF.sf2", "Soundfont player");
  32. addMissingFile (missing, "C:\\Drums\\kick.wav", "Kick");
  33. const issue159::Tree tree {
  34. { "/d", { { "Genesis", true }, { "other", true } } }, // (walked last folder first: "other", then "Genesis")
  35. { "/d/other", { { "GenesiSF.sf2", false }, { "snare.wav", false } } },
  36. { "/d/Genesis", { { "GenesiSF.sf2", false } } },
  37. };
  38. std::vector<std::string> streamed;
  39. const auto hits = searchFolderForMissing (missing, "/d", issue159::lister (tree), 0, {}, [&] (const std::string& h) { streamed.push_back (h); });
  40. CHECK (streamed == hits && hits.size() == 2); // each match told as it was found, the same as the result
  41. // picked again from the matches so far: the first one now, the better one (same parent folder) once it comes
  42. std::vector<bool> placed (missing.size(), true);
  43. auto files = missing;
  44. CHECK (relocateFromSearch (files, placed, { hits[0] }) == 1);
  45. CHECK (files[0].found == "/d/other/GenesiSF.sf2");
  46. CHECK (relocateFromSearch (files, placed, hits) == 1);
  47. CHECK (files[0].found == "/d/Genesis/GenesiSF.sf2" && files[1].found.empty());
  48. // a file the search didn't begin with (located or skipped by hand) is left alone
  49. files = missing;
  50. files[0].found = "/x/GenesiSF.sf2";
  51. placed[0] = false;
  52. relocateFromSearch (files, placed, hits);
  53. CHECK (files[0].found == "/x/GenesiSF.sf2");
  54. // cancelled: the walk ends at the next entry
  55. int calls = 0;
  56. const auto none = searchFolderForMissing (missing, "/d", issue159::lister (tree, &calls), 0, [] { return true; });
  57. CHECK (none.empty() && calls == 1);
  58. }
  59. TEST (issue159_soundfont_folder_walk_stops_and_skips_unreadable_folders)
  60. {
  61. namespace fs = std::filesystem;
  62. const auto root = fs::temp_directory_path() / ("lynx-iss159-" + std::to_string ((long long) std::chrono::steady_clock::now().time_since_epoch().count()));
  63. fs::create_directories (root / "a" / "deep");
  64. fs::create_directories (root / "locked" / "inside");
  65. for (const auto& p : { root / "a" / "one.sf2", root / "a" / "deep" / "two.SF2", root / "a" / "not.wav", root / "locked" / "inside" / "three.sf2" })
  66. std::ofstream (p) << "x";
  67. auto all = lynx::sf::findFontFiles (root.string());
  68. CHECK (all.size() == 3);
  69. #if ! defined(_WIN32)
  70. // a folder the user can't read (as on another user's or the system's folders of a big drive): passed over, no throw
  71. fs::permissions (root / "locked", fs::perms::none);
  72. const auto readable = lynx::sf::findFontFiles (root.string());
  73. CHECK ((readable.size() == 2 || getuid() == 0)); // (root reads everything)
  74. fs::permissions (root / "locked", fs::perms::owner_all);
  75. #endif
  76. // asked to stop: nothing more is walked
  77. std::atomic<bool> stop { true };
  78. CHECK (lynx::sf::findFontFiles (root.string(), 20000, &stop).empty());
  79. // a bound on the entries looked at
  80. CHECK (lynx::sf::findFontFiles (root.string(), 20000, nullptr, 1).size() <= 1);
  81. std::error_code ec;
  82. fs::remove_all (root, ec);
  83. }