123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- using NUnit.Framework.Interfaces;
- using UnityEditor.TestTools.TestRunner.GUI;
- using UnityEngine;
-
- namespace UnityEditor.TestRunner.TestLaunchers
- {
- internal static class FilePathMetaInfo
- {
- [Serializable]
- private struct FileReference
- {
- public string FilePath;
- public int LineNumber;
- }
-
- private enum PathType
- {
- ProjectRepositoryPath,
- ProjectPath,
- }
-
- public static void TryCreateFile(ITest runnerLoadedTest, BuildPlayerOptions playerBuildOptions)
- {
- try
- {
- var metaFileDestinationPath = GetMetaDestinationPath(playerBuildOptions);
- var repositoryPath = GetPathFromArgs(PathType.ProjectRepositoryPath);
- // if no path is given, early out so we do not pollute the build player folder with the file path file.
- if (string.IsNullOrEmpty(repositoryPath))
- {
- return;
- }
-
- // Create a dictionary for the test names and their file paths
- var testFilePaths = new Dictionary<string, FileReference>();
- RecursivelyPopulateFileReferences(runnerLoadedTest, testFilePaths, repositoryPath, new GuiHelper(new MonoCecilHelper(), new AssetsDatabaseHelper()));
- SaveToJsonFile(testFilePaths, metaFileDestinationPath);
- }
- catch (Exception e)
- {
- Debug.LogWarning("Saving test file path meta info failed: " + e.Message);
- }
- }
-
- // This function serializes dictionary to json file, all the logic would not be necessary if Unity was able to serialize Dictionaries, or if we could use Newtonsoft.Json.
- // This function could be changed later on, or we can use different data structure than Dictionary.
- private static void SaveToJsonFile(Dictionary<string, FileReference> testFilePaths, string metaFileDestinationPath)
- {
- using (var fileStream = File.CreateText(Path.Combine(metaFileDestinationPath, "TestFileReferences.json")))
- {
- fileStream.WriteLine("{");
-
- foreach (var testFilePath in testFilePaths)
- {
- fileStream.WriteLine($" \"{JavaScriptStringEncode(testFilePath.Key)}\": {{");
- fileStream.WriteLine($" \"filePath\": \"{JavaScriptStringEncode(testFilePath.Value.FilePath)}\",");
- fileStream.WriteLine($" \"lineNumber\": {testFilePath.Value.LineNumber}");
- // check if it is the last element in the dictionary
- if (testFilePath.Key != testFilePaths.Keys.Last())
- {
- fileStream.WriteLine(" },");
- }
- else
- {
- fileStream.WriteLine(" }");
- }
- }
-
- fileStream.WriteLine("}");
- }
- }
-
- private static string GetMetaDestinationPath(BuildPlayerOptions playerBuildOptions)
- {
- // If we are Auto-Running the player, use project path instead of player build path because it will be wiped out after successful run.
- if ((playerBuildOptions.options & BuildOptions.AutoRunPlayer) != 0)
- {
- return Path.Combine(GetPathFromArgs(PathType.ProjectPath));
- }
-
- // if the buildOutputPath is for a file, then get the directory of it
- return File.Exists(playerBuildOptions.locationPathName) ? Path.GetDirectoryName(playerBuildOptions.locationPathName) : playerBuildOptions.locationPathName;
- }
-
- private static void RecursivelyPopulateFileReferences(ITest test, Dictionary<string, FileReference> testFilePaths, string repositoryPath, IGuiHelper guiHelper)
- {
- if (test.HasChildren)
- {
- foreach (var child in test.Tests)
- {
- RecursivelyPopulateFileReferences(child, testFilePaths, repositoryPath, guiHelper);
- }
-
- return;
- }
-
- var testMethod = test.Method;
- if (testMethod == null)
- {
- testMethod = test.Parent.Method;
- if (testMethod == null)
- {
- return;
- }
- }
-
- var methodInfo = test.Method.MethodInfo;
- var type = test.TypeInfo.Type;
- var fileOpenInfo = guiHelper.GetFileOpenInfo(type, methodInfo);
- var filePathString = Path.Combine(repositoryPath, fileOpenInfo.FilePath);
- var lineNumber = fileOpenInfo.LineNumber;
- var fileReference = new FileReference
- {
- FilePath = filePathString,
- LineNumber = lineNumber
- };
- // Cannot be simplified with .TryAdd because Unity 2020.3 and below does not have it.
- if (!testFilePaths.ContainsKey(test.FullName))
- {
- testFilePaths.Add(test.FullName, fileReference);
- }
- }
-
- private static string GetPathFromArgs(PathType type)
- {
- var commandLineArgs = Environment.GetCommandLineArgs();
-
- string lookFor;
- switch (type)
- {
- case PathType.ProjectRepositoryPath:
- lookFor = "-projectRepositoryPath";
- break;
- case PathType.ProjectPath:
- lookFor = "-projectPath";
- break;
- default:
- throw new ArgumentException("Invalid PathType");
- }
-
- for (var i = 0; i < commandLineArgs.Length; i++)
- {
- if (commandLineArgs[i].Equals(lookFor))
- {
- return commandLineArgs[i + 1];
- }
- }
-
- return string.Empty;
- }
-
- // Below implementation is copy-paste from HttpUtility.JavaScriptStringEncode
- private static string JavaScriptStringEncode(string value) {
- if (String.IsNullOrEmpty(value)) {
- return String.Empty;
- }
-
- StringBuilder b = null;
- int startIndex = 0;
- int count = 0;
- for (int i = 0; i < value.Length; i++) {
- char c = value[i];
-
- // Append the unhandled characters (that do not require special treament)
- // to the string builder when special characters are detected.
- if (CharRequiresJavaScriptEncoding(c)) {
- if (b == null) {
- b = new StringBuilder(value.Length + 5);
- }
-
- if (count > 0) {
- b.Append(value, startIndex, count);
- }
-
- startIndex = i + 1;
- count = 0;
- }
-
- switch (c) {
- case '\r':
- b.Append("\\r");
- break;
- case '\t':
- b.Append("\\t");
- break;
- case '\"':
- b.Append("\\\"");
- break;
- case '\\':
- b.Append("\\\\");
- break;
- case '\n':
- b.Append("\\n");
- break;
- case '\b':
- b.Append("\\b");
- break;
- case '\f':
- b.Append("\\f");
- break;
- default:
- if (CharRequiresJavaScriptEncoding(c)) {
- AppendCharAsUnicodeJavaScript(b, c);
- }
- else {
- count++;
- }
- break;
- }
- }
-
- if (b == null) {
- return value;
- }
-
- if (count > 0) {
- b.Append(value, startIndex, count);
- }
-
- return b.ToString();
- }
-
- private static bool CharRequiresJavaScriptEncoding(char c) {
- return c < 0x20 // control chars always have to be encoded
- || c == '\"' // chars which must be encoded per JSON spec
- || c == '\\'
- || c == '\'' // HTML-sensitive chars encoded for safety
- || c == '<'
- || c == '>'
- || c == '&'
- || c == '\u0085' // newline chars (see Unicode 6.2, Table 5-1 [http://www.unicode.org/versions/Unicode6.2.0/ch05.pdf]) have to be encoded (DevDiv #663531)
- || c == '\u2028'
- || c == '\u2029';
- }
-
- private static void AppendCharAsUnicodeJavaScript(StringBuilder builder, char c) {
- builder.Append("\\u");
- builder.Append(((int)c).ToString("x4", CultureInfo.InvariantCulture));
- }
- }
- }
|