Browse Source

Initialize Paths in unit tests to avoid having to do hack arounds

ManlyMarco 5 years ago
parent
commit
1357478c3d

+ 1 - 3
BepInEx/Configuration/ConfigFile.cs

@@ -13,9 +13,7 @@ namespace BepInEx.Configuration
 	/// </summary>
 	public class ConfigFile
 	{
-		// Need to be lazy evaluated to not cause problems for unit tests
-		private static ConfigFile _coreConfig;
-		internal static ConfigFile CoreConfig => _coreConfig ?? (_coreConfig = new ConfigFile(Paths.BepInExConfigPath, true));
+		internal static ConfigFile CoreConfig { get; } = new ConfigFile(Paths.BepInExConfigPath, true);
 
 		protected Dictionary<ConfigDefinition, ConfigEntry> Entries { get; } = new Dictionary<ConfigDefinition, ConfigEntry>();
 

+ 1 - 0
BepInEx/Properties/AssemblyInfo.cs

@@ -25,6 +25,7 @@ using BepInEx;
 [assembly: Guid("4ffba620-f5ed-47f9-b90c-dad1316fd9b9")]
 
 [assembly: InternalsVisibleTo("BepInEx.Preloader")]
+[assembly: InternalsVisibleTo("BepInExTests")]
 
 // Version information for an assembly consists of the following four values:
 //

+ 29 - 0
BepInExTests/AssemblyInit.cs

@@ -0,0 +1,29 @@
+using System.IO;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace BepInEx.Tests
+{
+	[TestClass]
+	public class AssemblyInit
+	{
+		private static string _testPath;
+
+		[AssemblyInitialize]
+		public static void InitAss(TestContext context)
+		{
+			_testPath = Path.Combine(Path.GetTempPath(), "BepinexTestDir");
+			Directory.CreateDirectory(_testPath);
+			
+			string exePath = Path.Combine(_testPath, "Text.exe");
+			File.WriteAllBytes(exePath, new byte[] { });
+
+			Paths.SetExecutablePath(_testPath);
+		}
+
+		[AssemblyCleanup]
+		public static void CleanupAss()
+		{
+			Directory.Delete(_testPath, true);
+		}
+	}
+}

+ 1 - 0
BepInExTests/BepInExTests.csproj

@@ -62,6 +62,7 @@
     <Otherwise />
   </Choose>
   <ItemGroup>
+    <Compile Include="AssemblyInit.cs" />
     <Compile Include="Configuration\ConfigFileTests.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>