You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

115 lines
4.1 KiB

  1. #include <config.h>
  2. #include <apt-pkg/error.h>
  3. #include <apt-pkg/fileutl.h>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <gtest/gtest.h>
  8. #include "file-helpers.h"
  9. #define P(x) tempdir + "/" + x
  10. TEST(FileUtlTest,GetListOfFilesInDir)
  11. {
  12. std::string tempdir;
  13. createTemporaryDirectory("getlistoffiles", tempdir);
  14. createFile(tempdir, "anormalfile");
  15. createFile(tempdir, "01yet-anothernormalfile");
  16. createFile(tempdir, "anormalapt.conf");
  17. createFile(tempdir, "01yet-anotherapt.conf");
  18. createFile(tempdir, "anormalapt.list");
  19. createFile(tempdir, "01yet-anotherapt.list");
  20. createFile(tempdir, "wrongextension.wron");
  21. createFile(tempdir, "wrong-extension.wron");
  22. createFile(tempdir, "strangefile.");
  23. createFile(tempdir, "s.t.r.a.n.g.e.f.i.l.e");
  24. createFile(tempdir, ".hiddenfile");
  25. createFile(tempdir, ".hiddenfile.conf");
  26. createFile(tempdir, ".hiddenfile.list");
  27. createFile(tempdir, "multi..dot");
  28. createFile(tempdir, "multi.dot.conf");
  29. createFile(tempdir, "multi.dot.list");
  30. createFile(tempdir, "disabledfile.disabled");
  31. createFile(tempdir, "disabledfile.conf.disabled");
  32. createFile(tempdir, "disabledfile.list.disabled");
  33. createFile(tempdir, "invälid.conf");
  34. createFile(tempdir, "invalíd");
  35. createFile(tempdir, "01invalíd");
  36. createDirectory(tempdir, "invaliddir");
  37. createDirectory(tempdir, "directory.conf");
  38. createDirectory(tempdir, "directory.list");
  39. createDirectory(tempdir, "directory.wron");
  40. createDirectory(tempdir, "directory.list.disabled");
  41. createLink(tempdir, "anormalfile", "linkedfile.list");
  42. createLink(tempdir, "invaliddir", "linkeddir.list");
  43. createLink(tempdir, "non-existing-file", "brokenlink.list");
  44. // Files with no extension
  45. _error->PushToStack();
  46. std::vector<std::string> files = GetListOfFilesInDir(tempdir, "", true);
  47. ASSERT_EQ(2u, files.size());
  48. EXPECT_EQ(P("01yet-anothernormalfile"), files[0]);
  49. EXPECT_EQ(P("anormalfile"), files[1]);
  50. // Files with no extension - should be the same as above
  51. files = GetListOfFilesInDir(tempdir, "", true, true);
  52. ASSERT_EQ(2u, files.size());
  53. EXPECT_EQ(P("01yet-anothernormalfile"), files[0]);
  54. EXPECT_EQ(P("anormalfile"), files[1]);
  55. // Files with impossible extension
  56. files = GetListOfFilesInDir(tempdir, "impossible", true);
  57. EXPECT_TRUE(files.empty());
  58. // Files with impossible or no extension
  59. files = GetListOfFilesInDir(tempdir, "impossible", true, true);
  60. ASSERT_EQ(2u, files.size());
  61. EXPECT_EQ(P("01yet-anothernormalfile"), files[0]);
  62. EXPECT_EQ(P("anormalfile"), files[1]);
  63. // Files with list extension - nothing more
  64. files = GetListOfFilesInDir(tempdir, "list", true);
  65. ASSERT_EQ(4u, files.size());
  66. EXPECT_EQ(P("01yet-anotherapt.list"), files[0]);
  67. EXPECT_EQ(P("anormalapt.list"), files[1]);
  68. EXPECT_EQ(P("linkedfile.list"), files[2]);
  69. EXPECT_EQ(P("multi.dot.list"), files[3]);
  70. // Files with conf or no extension
  71. files = GetListOfFilesInDir(tempdir, "conf", true, true);
  72. ASSERT_EQ(5u, files.size());
  73. EXPECT_EQ(P("01yet-anotherapt.conf"), files[0]);
  74. EXPECT_EQ(P("01yet-anothernormalfile"), files[1]);
  75. EXPECT_EQ(P("anormalapt.conf"), files[2]);
  76. EXPECT_EQ(P("anormalfile"), files[3]);
  77. EXPECT_EQ(P("multi.dot.conf"), files[4]);
  78. // Files with disabled extension - nothing more
  79. files = GetListOfFilesInDir(tempdir, "disabled", true);
  80. ASSERT_EQ(3u, files.size());
  81. EXPECT_EQ(P("disabledfile.conf.disabled"), files[0]);
  82. EXPECT_EQ(P("disabledfile.disabled"), files[1]);
  83. EXPECT_EQ(P("disabledfile.list.disabled"), files[2]);
  84. // Files with disabled or no extension
  85. files = GetListOfFilesInDir(tempdir, "disabled", true, true);
  86. ASSERT_EQ(5u, files.size());
  87. EXPECT_EQ(P("01yet-anothernormalfile"), files[0]);
  88. EXPECT_EQ(P("anormalfile"), files[1]);
  89. EXPECT_EQ(P("disabledfile.conf.disabled"), files[2]);
  90. EXPECT_EQ(P("disabledfile.disabled"), files[3]);
  91. EXPECT_EQ(P("disabledfile.list.disabled"), files[4]);
  92. // discard the unknown file extension messages
  93. if (_error->PendingError())
  94. _error->MergeWithStack();
  95. else
  96. _error->RevertToStack();
  97. removeDirectory(tempdir);
  98. }