PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/EditorFeatures/Test/Workspaces/TestHostProject.cs

https://gitlab.com/sharadag/Roslyn
C# | 373 lines | 324 code | 44 blank | 5 comment | 12 complexity | 8c9cfd9fc09bfad7b81f97ddcba7f5a0 MD5 | raw file
  1. // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using Microsoft.CodeAnalysis;
  7. using Microsoft.CodeAnalysis.Diagnostics;
  8. using Microsoft.CodeAnalysis.Host;
  9. using Microsoft.CodeAnalysis.LanguageServices;
  10. using Microsoft.CodeAnalysis.Text;
  11. using Roslyn.Utilities;
  12. namespace Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
  13. {
  14. public class TestHostProject
  15. {
  16. private readonly HostLanguageServices _languageServices;
  17. private readonly ProjectId _id;
  18. private readonly string _name;
  19. private readonly IEnumerable<ProjectReference> _projectReferences;
  20. private readonly IEnumerable<MetadataReference> _metadataReferences;
  21. private readonly IEnumerable<AnalyzerReference> _analyzerReferences;
  22. private readonly CompilationOptions _compilationOptions;
  23. private readonly ParseOptions _parseOptions;
  24. private readonly bool _isSubmission;
  25. private readonly string _assemblyName;
  26. private readonly Type _hostObjectType;
  27. private readonly VersionStamp _version;
  28. private readonly string _filePath;
  29. private readonly string _outputFilePath;
  30. public IEnumerable<TestHostDocument> Documents;
  31. public IEnumerable<TestHostDocument> AdditionalDocuments;
  32. public string Name
  33. {
  34. get
  35. {
  36. return _name;
  37. }
  38. }
  39. public IEnumerable<ProjectReference> ProjectReferences
  40. {
  41. get
  42. {
  43. return _projectReferences;
  44. }
  45. }
  46. public IEnumerable<MetadataReference> MetadataReferences
  47. {
  48. get
  49. {
  50. return _metadataReferences;
  51. }
  52. }
  53. public IEnumerable<AnalyzerReference> AnalyzerReferences
  54. {
  55. get
  56. {
  57. return _analyzerReferences;
  58. }
  59. }
  60. public CompilationOptions CompilationOptions
  61. {
  62. get
  63. {
  64. return _compilationOptions;
  65. }
  66. }
  67. public ParseOptions ParseOptions
  68. {
  69. get
  70. {
  71. return _parseOptions;
  72. }
  73. }
  74. public ProjectId Id
  75. {
  76. get
  77. {
  78. return _id;
  79. }
  80. }
  81. public bool IsSubmission
  82. {
  83. get
  84. {
  85. return _isSubmission;
  86. }
  87. }
  88. public string AssemblyName
  89. {
  90. get
  91. {
  92. return _assemblyName;
  93. }
  94. }
  95. public Type HostObjectType
  96. {
  97. get
  98. {
  99. return _hostObjectType;
  100. }
  101. }
  102. public VersionStamp Version
  103. {
  104. get
  105. {
  106. return _version;
  107. }
  108. }
  109. public string FilePath
  110. {
  111. get
  112. {
  113. return _filePath;
  114. }
  115. }
  116. public string OutputFilePath
  117. {
  118. get { return _outputFilePath; }
  119. }
  120. internal TestHostProject(
  121. HostLanguageServices languageServices,
  122. CompilationOptions compilationOptions,
  123. ParseOptions parseOptions,
  124. params MetadataReference[] references)
  125. : this(languageServices, compilationOptions, parseOptions, "Test", references)
  126. {
  127. }
  128. internal TestHostProject(
  129. HostLanguageServices languageServices,
  130. CompilationOptions compilationOptions,
  131. ParseOptions parseOptions,
  132. string assemblyName,
  133. params MetadataReference[] references)
  134. : this(languageServices, compilationOptions, parseOptions, assemblyName: assemblyName, projectName: assemblyName, references: references, documents: SpecializedCollections.EmptyArray<TestHostDocument>())
  135. {
  136. }
  137. internal TestHostProject(
  138. HostLanguageServices languageServices,
  139. CompilationOptions compilationOptions,
  140. ParseOptions parseOptions,
  141. string assemblyName,
  142. string projectName,
  143. IList<MetadataReference> references,
  144. IList<TestHostDocument> documents,
  145. IList<TestHostDocument> additionalDocuments = null,
  146. Type hostObjectType = null,
  147. bool isSubmission = false,
  148. string filePath = null,
  149. IList<AnalyzerReference> analyzerReferences = null)
  150. {
  151. _assemblyName = assemblyName;
  152. _name = projectName;
  153. _id = ProjectId.CreateNewId(debugName: this.AssemblyName);
  154. _languageServices = languageServices;
  155. _compilationOptions = compilationOptions;
  156. _parseOptions = parseOptions;
  157. _metadataReferences = references;
  158. _analyzerReferences = analyzerReferences ?? SpecializedCollections.EmptyEnumerable<AnalyzerReference>();
  159. this.Documents = documents;
  160. this.AdditionalDocuments = additionalDocuments ?? SpecializedCollections.EmptyEnumerable<TestHostDocument>();
  161. _projectReferences = SpecializedCollections.EmptyEnumerable<ProjectReference>();
  162. _isSubmission = isSubmission;
  163. _hostObjectType = hostObjectType;
  164. _version = VersionStamp.Create();
  165. _filePath = filePath;
  166. _outputFilePath = GetTestOutputFilePath(filePath);
  167. }
  168. public TestHostProject(
  169. TestWorkspace workspace,
  170. TestHostDocument document,
  171. string name = null,
  172. string language = null,
  173. CompilationOptions compilationOptions = null,
  174. ParseOptions parseOptions = null,
  175. IEnumerable<TestHostProject> projectReferences = null,
  176. IEnumerable<MetadataReference> metadataReferences = null,
  177. IEnumerable<AnalyzerReference> analyzerReferences = null,
  178. string assemblyName = null)
  179. : this(workspace, name, language, compilationOptions, parseOptions, SpecializedCollections.SingletonEnumerable(document), SpecializedCollections.EmptyEnumerable<TestHostDocument>(), projectReferences, metadataReferences, analyzerReferences, assemblyName)
  180. {
  181. }
  182. public TestHostProject(
  183. TestWorkspace workspace,
  184. string name = null,
  185. string language = null,
  186. CompilationOptions compilationOptions = null,
  187. ParseOptions parseOptions = null,
  188. IEnumerable<TestHostDocument> documents = null,
  189. IEnumerable<TestHostDocument> additionalDocuments = null,
  190. IEnumerable<TestHostProject> projectReferences = null,
  191. IEnumerable<MetadataReference> metadataReferences = null,
  192. IEnumerable<AnalyzerReference> analyzerReferences = null,
  193. string assemblyName = null)
  194. {
  195. _name = name ?? "TestProject";
  196. _id = ProjectId.CreateNewId(debugName: this.Name);
  197. language = language ?? LanguageNames.CSharp;
  198. _languageServices = workspace.Services.GetLanguageServices(language);
  199. _compilationOptions = compilationOptions ?? this.LanguageServiceProvider.GetService<ICompilationFactoryService>().GetDefaultCompilationOptions();
  200. _parseOptions = parseOptions ?? this.LanguageServiceProvider.GetService<ISyntaxTreeFactoryService>().GetDefaultParseOptions();
  201. this.Documents = documents ?? SpecializedCollections.EmptyEnumerable<TestHostDocument>();
  202. this.AdditionalDocuments = additionalDocuments ?? SpecializedCollections.EmptyEnumerable<TestHostDocument>();
  203. _projectReferences = projectReferences != null ? projectReferences.Select(p => new ProjectReference(p.Id)) : SpecializedCollections.EmptyEnumerable<ProjectReference>();
  204. _metadataReferences = metadataReferences ?? new MetadataReference[] { TestReferences.NetFx.v4_0_30319.mscorlib };
  205. _analyzerReferences = analyzerReferences ?? SpecializedCollections.EmptyEnumerable<AnalyzerReference>();
  206. _assemblyName = assemblyName ?? "TestProject";
  207. _version = VersionStamp.Create();
  208. _outputFilePath = GetTestOutputFilePath(_filePath);
  209. if (documents != null)
  210. {
  211. foreach (var doc in documents)
  212. {
  213. doc.SetProject(this);
  214. }
  215. }
  216. if (additionalDocuments != null)
  217. {
  218. foreach (var doc in additionalDocuments)
  219. {
  220. doc.SetProject(this);
  221. }
  222. }
  223. }
  224. internal void SetSolution(TestHostSolution solution)
  225. {
  226. // set up back pointer to this project.
  227. if (this.Documents != null)
  228. {
  229. foreach (var doc in this.Documents)
  230. {
  231. doc.SetProject(this);
  232. }
  233. foreach (var doc in this.AdditionalDocuments)
  234. {
  235. doc.SetProject(this);
  236. }
  237. }
  238. }
  239. internal void AddDocument(TestHostDocument document)
  240. {
  241. this.Documents = this.Documents.Concat(new TestHostDocument[] { document });
  242. document.SetProject(this);
  243. }
  244. internal void RemoveDocument(TestHostDocument document)
  245. {
  246. this.Documents = this.Documents.Where(d => d != document);
  247. }
  248. internal void AddAdditionalDocument(TestHostDocument document)
  249. {
  250. this.AdditionalDocuments = this.AdditionalDocuments.Concat(new TestHostDocument[] { document });
  251. document.SetProject(this);
  252. }
  253. internal void RemoveAdditionalDocument(TestHostDocument document)
  254. {
  255. this.AdditionalDocuments = this.AdditionalDocuments.Where(d => d != document);
  256. }
  257. public string Language
  258. {
  259. get
  260. {
  261. return _languageServices.Language;
  262. }
  263. }
  264. internal HostLanguageServices LanguageServiceProvider
  265. {
  266. get
  267. {
  268. return _languageServices;
  269. }
  270. }
  271. public ProjectInfo ToProjectInfo()
  272. {
  273. return ProjectInfo.Create(
  274. this.Id,
  275. this.Version,
  276. this.Name,
  277. this.AssemblyName,
  278. this.Language,
  279. this.FilePath,
  280. this.OutputFilePath,
  281. this.CompilationOptions,
  282. this.ParseOptions,
  283. this.Documents.Select(d => d.ToDocumentInfo()),
  284. this.ProjectReferences,
  285. this.MetadataReferences,
  286. this.AnalyzerReferences,
  287. this.AdditionalDocuments.Select(d => d.ToDocumentInfo()),
  288. this.IsSubmission,
  289. this.HostObjectType);
  290. }
  291. // It is identical with the internal extension method 'GetDefaultExtension' defined in OutputKind.cs.
  292. // However, we could not apply for InternalVisibleToTest due to other parts of this assembly
  293. // complaining about CS0507: "cannot change access modifiers when overriding 'access' inherited member".
  294. private static string GetDefaultExtension(OutputKind kind)
  295. {
  296. switch (kind)
  297. {
  298. case OutputKind.ConsoleApplication:
  299. case OutputKind.WindowsApplication:
  300. case OutputKind.WindowsRuntimeApplication:
  301. return ".exe";
  302. case OutputKind.DynamicallyLinkedLibrary:
  303. return ".dll";
  304. case OutputKind.NetModule:
  305. return ".netmodule";
  306. case OutputKind.WindowsRuntimeMetadata:
  307. return ".winmdobj";
  308. default:
  309. return ".dll";
  310. }
  311. }
  312. private string GetTestOutputFilePath(string filepath)
  313. {
  314. string outputFilePath = @"Z:\";
  315. try
  316. {
  317. outputFilePath = Path.GetDirectoryName(_filePath);
  318. }
  319. catch (ArgumentException)
  320. {
  321. }
  322. if (string.IsNullOrEmpty(outputFilePath))
  323. {
  324. outputFilePath = @"Z:\";
  325. }
  326. return this.CompilationOptions == null ? "" : Path.Combine(outputFilePath, this.AssemblyName + GetDefaultExtension(this.CompilationOptions.OutputKind));
  327. }
  328. }
  329. }