ConfigFileTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Concurrent;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading;
  7. namespace BepInEx.Configuration.Tests
  8. {
  9. [TestClass]
  10. public class ConfigFileTests
  11. {
  12. private static ConcurrentBag<ConfigFile> _toRemove;
  13. [ClassInitialize]
  14. public static void Init(TestContext context)
  15. {
  16. _toRemove = new ConcurrentBag<ConfigFile>();
  17. }
  18. [ClassCleanup]
  19. public static void Cleanup()
  20. {
  21. foreach (var configFile in _toRemove)
  22. {
  23. configFile.StopWatching();
  24. File.Delete(configFile.ConfigFilePath);
  25. }
  26. }
  27. private static ConfigFile MakeConfig()
  28. {
  29. string configPath = Path.GetTempFileName();
  30. if (configPath == null) throw new InvalidOperationException("Wtf");
  31. var config = new ConfigFile(configPath, true);
  32. _toRemove.Add(config);
  33. return config;
  34. }
  35. [TestMethod]
  36. public void SaveTest()
  37. {
  38. MakeConfig().Save();
  39. }
  40. [TestMethod]
  41. public void SaveTestValueChange()
  42. {
  43. var c = MakeConfig();
  44. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
  45. var lines = File.ReadAllLines(c.ConfigFilePath);
  46. Assert.AreEqual(0, lines.Count(x => x.Equals("[Cat]")));
  47. Assert.AreEqual(0, lines.Count(x => x.Equals("# Test")));
  48. Assert.AreEqual(0, lines.Count(x => x.Equals("Key = 0")));
  49. c.Save();
  50. lines = File.ReadAllLines(c.ConfigFilePath);
  51. Assert.AreEqual(1, lines.Count(x => x.Equals("[Cat]")));
  52. Assert.AreEqual(1, lines.Count(x => x.Equals("# Test")));
  53. Assert.AreEqual(1, lines.Count(x => x.Equals("Key = 0")));
  54. w.Value = 69;
  55. lines = File.ReadAllLines(c.ConfigFilePath);
  56. Assert.AreEqual(1, lines.Count(x => x.Equals("[Cat]")));
  57. Assert.AreEqual(1, lines.Count(x => x.Equals("# Test")));
  58. Assert.AreEqual(1, lines.Count(x => x.Equals("Key = 69")));
  59. }
  60. [TestMethod]
  61. public void AutoSaveTest()
  62. {
  63. var c = MakeConfig();
  64. c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
  65. var eventFired = new AutoResetEvent(false);
  66. c.ConfigReloaded += (sender, args) => eventFired.Set();
  67. c.Save();
  68. Assert.IsFalse(eventFired.WaitOne(200));
  69. }
  70. [TestMethod]
  71. public void ReadTest()
  72. {
  73. var c = MakeConfig();
  74. File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\n");
  75. c.Reload();
  76. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
  77. Assert.AreEqual(w.Value, 1);
  78. var w2 = c.Wrap("Cat", "Key2", 0, new ConfigDescription("Test"));
  79. Assert.AreEqual(w2.Value, 0);
  80. }
  81. [TestMethod]
  82. public void ReadTest2()
  83. {
  84. var c = MakeConfig();
  85. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
  86. Assert.AreEqual(w.Value, 0);
  87. File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey = 1 \n");
  88. c.Reload();
  89. Assert.AreEqual(w.Value, 1);
  90. }
  91. [TestMethod]
  92. public void FileWatchTest()
  93. {
  94. var c = MakeConfig();
  95. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
  96. Assert.AreEqual(w.Value, 0);
  97. var eventFired = new AutoResetEvent(false);
  98. w.SettingChanged += (sender, args) => eventFired.Set();
  99. File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey = 1 \n");
  100. Assert.IsTrue(eventFired.WaitOne(500));
  101. Assert.AreEqual(w.Value, 1);
  102. }
  103. [TestMethod]
  104. public void FileWatchTestNoSelfReload()
  105. {
  106. var c = MakeConfig();
  107. var eventFired = new AutoResetEvent(false);
  108. c.ConfigReloaded += (sender, args) => eventFired.Set();
  109. c.Save();
  110. Assert.IsFalse(eventFired.WaitOne(200));
  111. }
  112. [TestMethod]
  113. public void EventTestWrapper()
  114. {
  115. var c = MakeConfig();
  116. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
  117. File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\n");
  118. var eventFired = false;
  119. w.SettingChanged += (sender, args) => eventFired = true;
  120. c.Reload();
  121. Assert.IsTrue(eventFired);
  122. }
  123. [TestMethod]
  124. public void EventTestReload()
  125. {
  126. var c = MakeConfig();
  127. var eventFired = false;
  128. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
  129. w.SettingChanged += (sender, args) => eventFired = true;
  130. Assert.IsFalse(eventFired);
  131. File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\n");
  132. c.Reload();
  133. Assert.IsTrue(eventFired);
  134. }
  135. }
  136. }