|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
| 5 | +using Basic.Reference.Assemblies; |
| 6 | +using Microsoft.CodeAnalysis.CSharp; |
| 7 | +using Microsoft.CodeAnalysis.Test.Utilities; |
| 8 | +using Microsoft.CodeAnalysis.Text; |
| 9 | +using Roslyn.Test.Utilities; |
5 | 10 | using Xunit.Abstractions; |
6 | 11 |
|
7 | 12 | namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests |
@@ -49,6 +54,101 @@ public async Task MultipleMefCompositionsAreCached() |
49 | 54 | AssertCachedCompositionCountEquals(expectedCount: 2); |
50 | 55 | } |
51 | 56 |
|
| 57 | + [Theory, WorkItem("https://github.com/microsoft/vscode-dotnettools/issues/1686")] |
| 58 | + // gen-delims |
| 59 | + [InlineData("#"), |
| 60 | + InlineData(":"), |
| 61 | + InlineData("?"), |
| 62 | + InlineData("["), |
| 63 | + InlineData("]"), |
| 64 | + InlineData("@"), |
| 65 | + // sub-delims |
| 66 | + InlineData("!"), |
| 67 | + InlineData("$"), |
| 68 | + InlineData("&"), |
| 69 | + InlineData("'"), |
| 70 | + InlineData("("), |
| 71 | + InlineData(")"), |
| 72 | + InlineData("*"), |
| 73 | + InlineData("+"), |
| 74 | + InlineData(","), |
| 75 | + InlineData(";"), |
| 76 | + InlineData("=")] |
| 77 | + public async Task CanFindCodeBaseWhenReservedCharactersInPath(string reservedCharacter) |
| 78 | + { |
| 79 | + // Test that given an unescaped code base URI (as vs-mef gives us), we can resolve the file path even if it contains reserved characters. |
| 80 | + |
| 81 | + // Certain characters aren't valid file paths on different file systems and so can't be in the path. |
| 82 | + if (ExecutionConditionUtil.IsWindows && reservedCharacter is "*" or ":" or "?") |
| 83 | + { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + var dllPath = GenerateDll(reservedCharacter, out var assemblyName); |
| 88 | + |
| 89 | + var firstServer = await TestLspServer.CreateAsync(new Roslyn.LanguageServer.Protocol.ClientCapabilities(), TestOutputLogger, MefCacheDirectory.Path, includeDevKitComponents: true, [dllPath]); |
| 90 | + await firstServer.DisposeAsync(); |
| 91 | + |
| 92 | + await using var testServer = await TestLspServer.CreateAsync(new Roslyn.LanguageServer.Protocol.ClientCapabilities(), TestOutputLogger, MefCacheDirectory.Path, includeDevKitComponents: true, [dllPath]); |
| 93 | + |
| 94 | + var assemblies = AppDomain.CurrentDomain.GetAssemblies(); |
| 95 | + var assembly = Assert.Single(assemblies, a => a.GetName().Name == assemblyName); |
| 96 | + var type = Assert.Single(assembly.GetTypes(), t => t.FullName?.Contains("ExportedType") == true); |
| 97 | + var values = testServer.ExportProvider.GetExportedValues(type, contractName: null); |
| 98 | + Assert.Single(values); |
| 99 | + } |
| 100 | + |
| 101 | + private string GenerateDll(string reservedCharacter, out string assemblyName) |
| 102 | + { |
| 103 | + var directory = TempRoot.CreateDirectory(); |
| 104 | + CSharpCompilationOptions options = new(OutputKind.DynamicallyLinkedLibrary); |
| 105 | + |
| 106 | + // Create a dll that exports and imports a mef type to ensure that MEF attempts to load and create a MEF graph |
| 107 | + // using this assembly. |
| 108 | + var source = """ |
| 109 | + namespace MyTestExportNamespace |
| 110 | + { |
| 111 | + [System.ComponentModel.Composition.Export(typeof(ExportedType))] |
| 112 | + public class ExportedType { } |
| 113 | +
|
| 114 | + public class ImportType |
| 115 | + { |
| 116 | + [System.ComponentModel.Composition.ImportingConstructorAttribute] |
| 117 | + public ImportType(ExportedType t) { } |
| 118 | + } |
| 119 | + } |
| 120 | + """; |
| 121 | + |
| 122 | + // Generate an assembly name associated with the character we're testing - this ensures |
| 123 | + // that if multiple of these tests run in the same process we're making sure that the correct expected assembly is loaded. |
| 124 | + assemblyName = "MyAssembly" + reservedCharacter.GetHashCode(); |
| 125 | +#pragma warning disable RS0030 // Do not use banned APIs - intentionally using System.ComponentModel.Composition to verify mef construction. |
| 126 | + var compilation = CSharpCompilation.Create( |
| 127 | + assemblyName, |
| 128 | + syntaxTrees: [SyntaxFactory.ParseSyntaxTree(SourceText.From(source, encoding: null, SourceHashAlgorithms.Default))], |
| 129 | + references: |
| 130 | + [ |
| 131 | + NetStandard20.References.mscorlib, |
| 132 | + NetStandard20.References.netstandard, |
| 133 | + NetStandard20.References.SystemRuntime, |
| 134 | + MetadataReference.CreateFromFile(typeof(System.ComponentModel.Composition.ExportAttribute).Assembly.Location) |
| 135 | + ], |
| 136 | + options: options); |
| 137 | +#pragma warning restore RS0030 // Do not use banned APIs |
| 138 | + |
| 139 | + // Write a dll to a subdir with a reserved character in the name. |
| 140 | + var tempSubDir = directory.CreateDirectory(reservedCharacter); |
| 141 | + var tempFile = tempSubDir.CreateFile($"{assemblyName}.dll"); |
| 142 | + var dllData = compilation.EmitToStream(); |
| 143 | + tempFile.WriteAllBytes(dllData.ToArray()); |
| 144 | + |
| 145 | + // Mark the file as read only to prevent mutations. |
| 146 | + var fileInfo = new FileInfo(tempFile.Path); |
| 147 | + fileInfo.IsReadOnly = true; |
| 148 | + |
| 149 | + return tempFile.Path; |
| 150 | + } |
| 151 | + |
52 | 152 | private async Task AssertCacheWriteWasAttemptedAsync() |
53 | 153 | { |
54 | 154 | var cacheWriteTask = ExportProviderBuilder.TestAccessor.GetCacheWriteTask(); |
|
0 commit comments