#pragma warning restore CA1009 /// <summary> /// Initialize the logging system for the application /// </summary> /// <param name="logfile">The log file to initialize</param> /// <param name="logfilePath">The custom path of where to initialize the file</param> /// <returns>True if successful initialization, false otherwise</returns> public static bool Init(Logfiles logfile, string logfilePath = null) { Logfile fileToWriteTo = null; //assign it here first to make sure it's null switch (logfile) { case Logfiles.Application: fileToWriteTo = ApplicationLogfile; logfilePath = ApplicationLogFilename; break; case Logfiles.Installer: fileToWriteTo = InstallLogfile; break; case Logfiles.Uninstaller: fileToWriteTo = UninstallLogfile; break; case Logfiles.Editor: fileToWriteTo = EditorLogfile; logfilePath = ApplicationEditorLogFilename; break; case Logfiles.PatchDesigner: fileToWriteTo = PatcherLogfile; logfilePath = ApplicationPatchDesignerLogFilename; break; case Logfiles.Updater: fileToWriteTo = UpdaterLogfile; logfilePath = ApplicationUpdaterLogFilename; break; } if (fileToWriteTo != null) { throw new BadMemeException("only do this once jackass"); } #pragma warning disable IDE0068 // Use recommended dispose pattern fileToWriteTo = new Logfile(logfilePath, ApplicationLogfileTimestamp); #pragma warning restore IDE0068 // Use recommended dispose pattern //now that it's newed, the reference needs to be reverse assigned switch (logfile) { case Logfiles.Application: ApplicationLogfile = fileToWriteTo; break; case Logfiles.Installer: InstallLogfile = fileToWriteTo; break; case Logfiles.Uninstaller: UninstallLogfile = fileToWriteTo; break; case Logfiles.Editor: EditorLogfile = fileToWriteTo; break; case Logfiles.PatchDesigner: PatcherLogfile = fileToWriteTo; break; case Logfiles.Updater: UpdaterLogfile = fileToWriteTo; break; } if (!fileToWriteTo.Init()) { MessageBox.Show(string.Format("Failed to initialize logfile {0}, check file permissions", logfilePath)); return(false); } return(true); }
/// <summary> /// Run a complete regression test based on the list of unit tests /// </summary> /// <returns>Returns false if a setup error occurred, true otherwise</returns> /// <remarks>The return value of the method does NOT related to the success of the Unit Tests</remarks> public bool RunRegressions() { //if from the editor, enable verbose logging (allows it to get debug log statements) bool tempVerboseLoggingSetting = ModpackSettings.VerboseLogging; if (!ModpackSettings.VerboseLogging) { Logging.Info("p.FromEditor=true and ModpackSettings.VerboseLogging=false, setting to true for duration of patch method"); ModpackSettings.VerboseLogging = true; } //init the logfile for regressions if (File.Exists(RegressionLogfile.Filepath)) { Logging.Warning("regression log file previously exists, deleting..."); File.Delete(RegressionLogfile.Filepath); } if (!RegressionLogfile.Init()) { Logging.Error("failed to initialize logfile"); return(false); } //make sure the files to test against exist first //and the start file if (!File.Exists(Path.Combine(RegressionFolderPath, Startfile))) { Logging.Error("regressions start file does not exist!"); Logging.Error(Path.Combine(RegressionFolderPath, Startfile)); return(false); } for (int i = 1; i < UnitTests.Count + 1; i++) { string checkfile = Path.Combine(RegressionFolderPath, string.Format("{0}{1}{2}", CheckFilenamePrefix, i.ToString("D2"), RegressionExtension)); if (!File.Exists(checkfile)) { Logging.Error("checkfile does not exist!"); Logging.Error(checkfile); return(false); } } //make a new file to be the one to make changes to //path get extension gets the dot string filenameToTest = "testfile" + RegressionExtension; string filenameToTestPath = Path.Combine(RegressionFolderPath, filenameToTest); if (File.Exists(filenameToTestPath)) { File.Delete(filenameToTestPath); } File.Copy(Path.Combine(RegressionFolderPath, Startfile), filenameToTestPath); WriteToLogfiles("----- Unit tests start -----"); bool breakOutEarly = false; foreach (UnitTest unitTest in UnitTests) { unitTest.Patch.CompletePath = filenameToTestPath; unitTest.Patch.File = filenameToTestPath; unitTest.Patch.Type = RegressionTypeString; WriteToLogfiles("Running test {0} of {1}: {2}", ++NumPassed, UnitTests.Count, unitTest.Description); unitTest.Patch.FromEditor = true; if (unitTest.Patch.FollowPath) { //delete testfile if (File.Exists(filenameToTestPath)) { File.Delete(filenameToTestPath); } if (NumPassed >= 5) { File.Copy(Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"check_04", RegressionExtension)), filenameToTestPath); if (NumPassed == 6) { //backup currentPlayersPanel and copy over new one if (File.Exists(Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"playersPanelBackup", RegressionExtension)))) { File.Delete(Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"playersPanelBackup", RegressionExtension))); } File.Copy(Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"playersPanel", RegressionExtension)), Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"playersPanelBackup", RegressionExtension))); if (File.Exists(Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"playersPanel", RegressionExtension)))) { File.Delete(Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"playersPanel", RegressionExtension))); } File.Copy(Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"check_05", RegressionExtension)), Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"playersPanel", RegressionExtension))); } else if (NumPassed == 7) { if (File.Exists(Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"playersPanel", RegressionExtension)))) { File.Delete(Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"playersPanel", RegressionExtension))); } File.Copy(Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"check_06", RegressionExtension)), Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"playersPanel", RegressionExtension))); } } else { File.Copy(Path.Combine(RegressionFolderPath, Startfile), filenameToTestPath); } } PatchUtils.RunPatch(unitTest.Patch); string checkfile = Path.Combine(RegressionFolderPath, string.Format("{0}{1}{2}", CheckFilenamePrefix, NumPassed.ToString("D2"), Path.GetExtension(Startfile))); WriteToLogfiles("Checking results against check file {0}...", Path.GetFileName(checkfile)); string patchRun = File.ReadAllText(filenameToTestPath); string patchTestAgainst = File.ReadAllText(checkfile); if (patchTestAgainst.Equals(patchRun)) { WriteToLogfiles("Success!"); } else { WriteToLogfiles("Failed!"); breakOutEarly = true; break; } } if (breakOutEarly) { WriteToLogfiles("----- Unit tests finish (fail)-----"); } else { WriteToLogfiles("----- Unit tests finish (pass)-----"); //delete the test file, we don't need it. (it's the same text as the last check file anyways) if (File.Exists(filenameToTestPath)) { File.Delete(filenameToTestPath); } if (UnitTests[0].Patch.FollowPath) { //delete not needed "escaped" files and put playersPanelBackup back if (File.Exists(Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"playersPanel", RegressionExtension)))) { File.Delete(Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"playersPanel", RegressionExtension))); } File.Copy(Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"playersPanelBackup", RegressionExtension)), Path.Combine(RegressionFolderPath, string.Format("{0}{1}", @"playersPanel", RegressionExtension))); foreach (string file in new string[] { "battleLabelsTemplates_escaped", "battleLabels_escaped", "damageLog_escaped", "playersPanel_escaped", "testfile_escaped", "playersPanelBackup" }) { if (File.Exists(Path.Combine(RegressionFolderPath, string.Format("{0}{1}", file, RegressionExtension)))) { File.Delete(Path.Combine(RegressionFolderPath, string.Format("{0}{1}", file, RegressionExtension))); } } } } //dispose log file RegressionLogfile.Dispose(); //set the verbose setting back Logging.Debug("temp logging setting={0}, ModpackSettings.VerboseLogging={1}, setting logging back to temp"); ModpackSettings.VerboseLogging = tempVerboseLoggingSetting; return(true); }