CoverageResultWriterBase.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.IO;
  2. using NUnit.Framework;
  3. using UnityEditor.TestTools.CodeCoverage.Utils;
  4. using UnityEngine;
  5. namespace UnityEditor.TestTools.CodeCoverage
  6. {
  7. internal abstract class CoverageResultWriterBase<T> where T : class
  8. {
  9. protected CoverageSettings m_CoverageSettings;
  10. public T CoverageSession { get; set; }
  11. protected CoverageResultWriterBase(CoverageSettings coverageSettings)
  12. {
  13. m_CoverageSettings = coverageSettings;
  14. }
  15. public virtual void WriteCoverageSession()
  16. {
  17. #if UNITY_2020_1_OR_NEWER
  18. if (Compilation.CompilationPipeline.codeOptimization == Compilation.CodeOptimization.Release)
  19. {
  20. ResultsLogger.Log(ResultID.Warning_DebugCodeOptimization);
  21. if (!CommandLineManager.instance.batchmode)
  22. {
  23. if (EditorUtility.DisplayDialog(
  24. L10n.Tr("Code Coverage"),
  25. L10n.Tr($"Code Coverage requires Code Optimization to be set to debug mode in order to obtain accurate coverage information. Do you want to switch to debug mode?\n\nNote that you would need to rerun {(CoverageRunData.instance.isRecording ? "the Coverage Recording session." : "the tests.")}"),
  26. L10n.Tr("Switch to debug mode"),
  27. L10n.Tr("Cancel")))
  28. {
  29. Compilation.CompilationPipeline.codeOptimization = Compilation.CodeOptimization.Debug;
  30. EditorPrefs.SetBool("ScriptDebugInfoEnabled", true);
  31. }
  32. }
  33. }
  34. #endif
  35. #if BURST_INSTALLED
  36. if (EditorPrefs.GetBool("BurstCompilation", true) && !CommandLineManager.instance.burstDisabled)
  37. {
  38. ResultsLogger.Log(ResultID.Warning_BurstCompilationEnabled);
  39. }
  40. #endif
  41. }
  42. public void SetupCoveragePaths()
  43. {
  44. string folderName = CoverageUtils.GetProjectFolderName();
  45. string resultsRootDirectoryName = string.Concat(folderName, m_CoverageSettings.resultsFolderSuffix);
  46. bool isRecording = CoverageRunData.instance.isRecording;
  47. // We want to save in the 'Recording' subdirectory of the results folder when recording
  48. #if TEST_FRAMEWORK_1_2_OR_NEWER
  49. string testSuite = isRecording ? "Recording" : "Automated";
  50. #else
  51. string testSuite = isRecording ? "Recording" : TestContext.Parameters.Get("platform");
  52. #endif
  53. string directoryName = CoverageUtils.JoinPaths(resultsRootDirectoryName, testSuite != null ? testSuite : "");
  54. m_CoverageSettings.rootFolderPath = CoverageUtils.GetRootFolderPath(m_CoverageSettings);
  55. m_CoverageSettings.historyFolderPath = CoverageUtils.GetHistoryFolderPath(m_CoverageSettings);
  56. string filePath = CoverageUtils.JoinPaths(directoryName, m_CoverageSettings.resultsFileName);
  57. filePath = CoverageUtils.JoinPaths(m_CoverageSettings.rootFolderPath, filePath);
  58. filePath = CoverageUtils.NormaliseFolderSeparators(filePath);
  59. CoverageUtils.EnsureFolderExists(Path.GetDirectoryName(filePath));
  60. m_CoverageSettings.resultsRootFolderPath = CoverageUtils.NormaliseFolderSeparators(CoverageUtils.JoinPaths(m_CoverageSettings.rootFolderPath, resultsRootDirectoryName));
  61. m_CoverageSettings.resultsFolderPath = CoverageUtils.NormaliseFolderSeparators(CoverageUtils.JoinPaths(m_CoverageSettings.rootFolderPath, directoryName));
  62. m_CoverageSettings.resultsFilePath = filePath;
  63. }
  64. public void ClearCoverageFolderIfExists()
  65. {
  66. CoverageUtils.ClearFolderIfExists(m_CoverageSettings.resultsFolderPath, "*.xml");
  67. }
  68. protected string GetNextFullFilePath()
  69. {
  70. int nextFileIndex = m_CoverageSettings.hasPersistentRunData ? CoverageRunData.instance.GetNextFileIndex() : 0;
  71. string fullFilePath = m_CoverageSettings.resultsFilePath + "_" + nextFileIndex.ToString("D4") + "." + m_CoverageSettings.resultsFileExtension;
  72. return fullFilePath;
  73. }
  74. }
  75. }