123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using UnityEditor.Networking.PlayerConnection;
- using UnityEngine;
- using UnityEngine.TestTools;
- namespace UnityEditor.TestTools.CodeCoverage.Utils
- {
- internal static class CoverageUtils
- {
- public static bool IsConnectedToPlayer
- {
- get
- {
- return EditorConnection.instance.ConnectedPlayers.Count > 0;
- }
- }
- public static string NormaliseFolderSeparators(string folderPath, bool stripTrailingSlash = false)
- {
- if (folderPath != null)
- {
- folderPath = folderPath.Replace('\\', '/');
- if (stripTrailingSlash)
- {
- folderPath = folderPath.TrimEnd('/');
- }
- }
- return folderPath;
- }
- public static bool EnsureFolderExists(string folderPath)
- {
- if (string.IsNullOrEmpty(folderPath))
- return false;
- if (!Directory.Exists(folderPath))
- {
- try
- {
- Directory.CreateDirectory(folderPath);
- }
- catch (Exception)
- {
- return false;
- }
- }
- return true;
- }
- public static string GetProjectFolderName()
- {
- string[] projectPathArray = GetProjectPath().Split('/');
- Debug.Assert(projectPathArray.Length > 0);
- string folderName = projectPathArray[projectPathArray.Length - 1];
- char[] invalidChars = Path.GetInvalidPathChars();
- StringBuilder folderNameStringBuilder = new StringBuilder();
- foreach (char c in folderName)
- {
- if (invalidChars.Contains(c))
- {
- folderNameStringBuilder.Append('_');
- }
- else
- {
- folderNameStringBuilder.Append(c);
- }
- }
- return folderNameStringBuilder.ToString();
- }
- public static string StripAssetsFolderIfExists(string folderPath)
- {
- if (folderPath != null)
- {
- string toTrim = "Assets";
- folderPath = folderPath.TrimEnd(toTrim.ToCharArray());
- }
- return folderPath;
- }
- public static string GetProjectPath()
- {
- return NormaliseFolderSeparators(StripAssetsFolderIfExists(Application.dataPath), true);
- }
- public static string GetRootFolderPath(CoverageSettings coverageSettings)
- {
- string rootFolderPath = string.Empty;
- string coverageFolderPath = string.Empty;
- if (CommandLineManager.instance.batchmode)
- {
- if (coverageSettings.resultsPathFromCommandLine.Length > 0)
- {
- coverageFolderPath = coverageSettings.resultsPathFromCommandLine;
- EnsureFolderExists(coverageFolderPath);
- }
- }
- else
- {
- if (CommandLineManager.instance.runFromCommandLine && coverageSettings.resultsPathFromCommandLine.Length > 0)
- {
- coverageFolderPath = coverageSettings.resultsPathFromCommandLine;
- EnsureFolderExists(coverageFolderPath);
- }
- else
- {
- coverageFolderPath = CoveragePreferences.instance.GetStringForPaths("Path", string.Empty);
- }
- }
- string projectPath = GetProjectPath();
- if (EnsureFolderExists(coverageFolderPath))
- {
- coverageFolderPath = NormaliseFolderSeparators(coverageFolderPath, true);
- // Add 'CodeCoverage' directory if coverageFolderPath is projectPath
- if (string.Equals(coverageFolderPath, projectPath, StringComparison.InvariantCultureIgnoreCase))
- rootFolderPath = JoinPaths(coverageFolderPath, coverageSettings.rootFolderName);
- // else user coverageFolderPath as the root folder
- else
- rootFolderPath = coverageFolderPath;
- }
- else
- {
- // Add 'CodeCoverage' directory to projectPath if coverageFolderPath is not valid
- rootFolderPath = JoinPaths(projectPath, coverageSettings.rootFolderName);
- }
- return rootFolderPath;
- }
- public static string GetHistoryFolderPath(CoverageSettings coverageSettings)
- {
- string historyFolderPath = string.Empty;
- string rootFolderPath = coverageSettings.rootFolderPath;
- if (CommandLineManager.instance.batchmode)
- {
- if (coverageSettings.historyPathFromCommandLine.Length > 0)
- {
- historyFolderPath = coverageSettings.historyPathFromCommandLine;
- EnsureFolderExists(historyFolderPath);
- }
- }
- else
- {
- if (CommandLineManager.instance.runFromCommandLine && coverageSettings.historyPathFromCommandLine.Length > 0)
- {
- historyFolderPath = coverageSettings.historyPathFromCommandLine;
- EnsureFolderExists(historyFolderPath);
- }
- else
- {
- historyFolderPath = CoveragePreferences.instance.GetStringForPaths("HistoryPath", string.Empty);
- }
- }
- bool addHistorySubDir = false;
- string projectPath = GetProjectPath();
- if (EnsureFolderExists(historyFolderPath))
- {
- historyFolderPath = NormaliseFolderSeparators(historyFolderPath, true);
-
- // If historyFolderPath == rootFolderPath, add 'Report-history' sub directory in rootFolderPath
- if (string.Equals(historyFolderPath, rootFolderPath, StringComparison.InvariantCultureIgnoreCase))
- {
- addHistorySubDir = true;
- }
- // If historyFolderPath == projectPath, add 'CodeCoverage' directory to projectPath
- // and add 'Report-history' sub directory in rootFolderPath
- else if (string.Equals(historyFolderPath, projectPath, StringComparison.InvariantCultureIgnoreCase))
- {
- rootFolderPath = JoinPaths(projectPath, coverageSettings.rootFolderName);
- addHistorySubDir = true;
- }
- // otherwise keep the original historyFolderPath
- }
- else
- {
- // If historyFolderPath is not valid, add 'CodeCoverage' directory to projectPath
- // and add 'Report-history' sub directory in rootFolderPath
- rootFolderPath = JoinPaths(projectPath, coverageSettings.rootFolderName);
- addHistorySubDir = true;
- }
- if (addHistorySubDir)
- {
- historyFolderPath = JoinPaths(rootFolderPath, CoverageSettings.ReportHistoryFolderName);
- }
- return historyFolderPath;
- }
- public static string JoinPaths(string pathLeft, string pathRight)
- {
- string[] pathsToJoin = new string[] { pathLeft, pathRight };
- return string.Join("/", pathsToJoin);
- }
- public static int GetNumberOfFilesInFolder(string folderPath, string filePattern, SearchOption searchOption)
- {
- if (folderPath == null)
- return 0;
- string[] files = Directory.GetFiles(folderPath, filePattern, searchOption);
- return files.Length;
- }
- public static void ClearFolderIfExists(string folderPath, string filePattern)
- {
- if (folderPath != null)
- {
- if (Directory.Exists(folderPath))
- {
- DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
- foreach (FileInfo file in dirInfo.GetFiles(filePattern))
- {
- try
- {
- file.Delete();
- }
- catch (Exception)
- {
- ResultsLogger.Log(ResultID.Warning_FailedToDeleteFile, file.FullName);
- }
- }
- foreach (DirectoryInfo dir in dirInfo.GetDirectories())
- {
- try
- {
- dir.Delete(true);
- }
- catch (Exception)
- {
- ResultsLogger.Log(ResultID.Warning_FailedToDeleteDir, dir.FullName);
- }
- }
- }
- }
- }
- public static bool DoesFolderExistAndNotEmpty(string folderPath)
- {
- if (folderPath != null)
- {
- if (Directory.Exists(folderPath))
- {
- DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
- return dirInfo.GetFiles().Length > 0 || dirInfo.GetDirectories().Length > 0;
- }
- }
- return false;
- }
- public static bool IsValidFolder(string folderPath)
- {
- return !string.IsNullOrEmpty(folderPath) && Directory.Exists(folderPath);
- }
- public static bool IsValidFile(string filePath)
- {
- return !string.IsNullOrEmpty(filePath) && File.Exists(filePath);
- }
- private static HashSet<char> regexSpecialChars = new HashSet<char>(new[] { '[', '\\', '^', '$', '.', '|', '?', '*', '+', '(', ')' });
- public static string GlobToRegex(string glob)
- {
- var regex = new StringBuilder();
- var characterClass = false;
- regex.Append("^");
- foreach (var c in glob)
- {
- if (characterClass)
- {
- if (c == ']')
- {
- characterClass = false;
- }
- regex.Append(c);
- continue;
- }
- switch (c)
- {
- case '*':
- regex.Append(".*");
- break;
- case '?':
- regex.Append(".");
- break;
- case '[':
- characterClass = true;
- regex.Append(c);
- break;
- default:
- if (regexSpecialChars.Contains(c))
- {
- regex.Append('\\');
- }
- regex.Append(c);
- break;
- }
- }
- regex.Append("$");
- return regex.ToString();
- }
- [ExcludeFromCoverage]
- public static string BrowseForDir(string directory, string title)
- {
- if (string.IsNullOrEmpty(directory))
- {
- string variable = "ProgramFiles";
- #if UNITY_EDITOR_OSX
- variable = "HOME";
- #endif
- string candidateDirectory = Environment.GetEnvironmentVariable(variable);
- if (IsValidFolder(candidateDirectory))
- directory = candidateDirectory;
- }
- directory = EditorUtility.OpenFolderPanel(title, directory, string.Empty);
- EditorWindow.FocusWindowIfItsOpen(typeof(CodeCoverageWindow));
- if (!IsValidFolder(directory))
- return string.Empty;
- return directory;
- }
- [ExcludeFromCoverage]
- public static string BrowseForFile(string directory, string title)
- {
- if (string.IsNullOrEmpty(directory))
- {
- string variable = "ProgramFiles";
- #if UNITY_EDITOR_OSX
- variable = "HOME";
- #endif
- string candidateDirectory = Environment.GetEnvironmentVariable(variable);
- if (IsValidFolder(candidateDirectory))
- directory = candidateDirectory;
- }
- string file = EditorUtility.OpenFilePanel(title, directory, "cs");
- EditorWindow.FocusWindowIfItsOpen(typeof(CodeCoverageWindow));
- if (!IsValidFile(file))
- return string.Empty;
- return file;
- }
- }
- }
|