|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +// Uncomment this to test run-api locally. |
| 6 | +// Eventually when a new enough SDK is adopted in-repo we can remove this |
| 7 | +//#define RoslynTestRunApi |
| 8 | + |
| 9 | +using System.Text; |
| 10 | +using System.Text.RegularExpressions; |
| 11 | +using Microsoft.CodeAnalysis.LanguageServer.FileBasedPrograms; |
| 12 | +using Microsoft.Extensions.Logging; |
| 13 | +using Roslyn.LanguageServer.Protocol; |
| 14 | +using Roslyn.Test.Utilities; |
| 15 | +using Xunit.Abstractions; |
| 16 | + |
| 17 | +namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests; |
| 18 | + |
| 19 | +/// <summary> |
| 20 | +/// Goal of these tests: |
| 21 | +/// - Ensure that the various request/response forms work as expected in basic scenarios. |
| 22 | +/// - Ensure that various properties on the response are populated in a reasonable way. |
| 23 | +/// Non-goals: |
| 24 | +/// - Thorough behavioral testing. |
| 25 | +/// - Testing of more intricate behaviors which are subject to change. |
| 26 | +/// </summary> |
| 27 | +public sealed class VirtualProjectXmlProviderTests : AbstractLanguageServerHostTests |
| 28 | +{ |
| 29 | + public VirtualProjectXmlProviderTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + private class EnableRunApiTests : ExecutionCondition |
| 34 | + { |
| 35 | + public override bool ShouldSkip => |
| 36 | +#if RoslynTestRunApi |
| 37 | + false; |
| 38 | +#else |
| 39 | + true; |
| 40 | +#endif |
| 41 | + |
| 42 | + public override string SkipReason => $"Compilation symbol 'RoslynTestRunApi' is not defined."; |
| 43 | + } |
| 44 | + |
| 45 | + private async Task<VirtualProjectXmlProvider> GetProjectXmlProviderAsync() |
| 46 | + { |
| 47 | + var (exportProvider, _) = await LanguageServerTestComposition.CreateExportProviderAsync( |
| 48 | + LoggerFactory, includeDevKitComponents: false, MefCacheDirectory.Path, extensionPaths: null); |
| 49 | + return exportProvider.GetExportedValue<VirtualProjectXmlProvider>(); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public async Task GetProjectXml_FileBasedProgram_SdkTooOld_01() |
| 54 | + { |
| 55 | + var projectProvider = await GetProjectXmlProviderAsync(); |
| 56 | + |
| 57 | + var tempDir = TempRoot.CreateDirectory(); |
| 58 | + var appFile = tempDir.CreateFile("app.cs"); |
| 59 | + await appFile.WriteAllTextAsync(""" |
| 60 | + Console.WriteLine("Hello, world!"); |
| 61 | + """); |
| 62 | + |
| 63 | + var globalJsonFile = tempDir.CreateFile("global.json"); |
| 64 | + globalJsonFile.WriteAllBytes(Encoding.UTF8.GetBytes(""" |
| 65 | + { |
| 66 | + "sdk": { |
| 67 | + "version": "9.0.105" |
| 68 | + } |
| 69 | + } |
| 70 | + """)); |
| 71 | + |
| 72 | + var contentNullable = await projectProvider.GetVirtualProjectContentAsync(appFile.Path, CancellationToken.None); |
| 73 | + Assert.Null(contentNullable); |
| 74 | + } |
| 75 | + |
| 76 | + [ConditionalFact(typeof(EnableRunApiTests))] |
| 77 | + public async Task GetProjectXml_FileBasedProgram_01() |
| 78 | + { |
| 79 | + var projectProvider = await GetProjectXmlProviderAsync(); |
| 80 | + |
| 81 | + var tempDir = TempRoot.CreateDirectory(); |
| 82 | + var appFile = tempDir.CreateFile("app.cs"); |
| 83 | + await appFile.WriteAllTextAsync(""" |
| 84 | + Console.WriteLine("Hello, world!"); |
| 85 | + """); |
| 86 | + |
| 87 | + var globalJsonFile = tempDir.CreateFile("global.json"); |
| 88 | + await globalJsonFile.WriteAllTextAsync(""" |
| 89 | + { |
| 90 | + "sdk": { |
| 91 | + "version": "10.0.100-preview.5.25265.12" |
| 92 | + } |
| 93 | + } |
| 94 | + """); |
| 95 | + |
| 96 | + var contentNullable = await projectProvider.GetVirtualProjectContentAsync(appFile.Path, CancellationToken.None); |
| 97 | + var content = contentNullable.Value; |
| 98 | + var virtualProjectXml = content.VirtualProjectXml; |
| 99 | + LoggerFactory.CreateLogger<VirtualProjectXmlProviderTests>().LogTrace(virtualProjectXml); |
| 100 | + |
| 101 | + Assert.Contains("<TargetFramework>net10.0</TargetFramework>", virtualProjectXml); |
| 102 | + Assert.Contains("<ArtifactsPath>", virtualProjectXml); |
| 103 | + Assert.Empty(content.Diagnostics); |
| 104 | + } |
| 105 | + |
| 106 | + [ConditionalFact(typeof(EnableRunApiTests))] |
| 107 | + public async Task GetProjectXml_NonFileBasedProgram_01() |
| 108 | + { |
| 109 | + var projectProvider = await GetProjectXmlProviderAsync(); |
| 110 | + |
| 111 | + var tempDir = TempRoot.CreateDirectory(); |
| 112 | + var appFile = tempDir.CreateFile("app.cs"); |
| 113 | + await appFile.WriteAllTextAsync(""" |
| 114 | + public class C |
| 115 | + { |
| 116 | + } |
| 117 | + """); |
| 118 | + |
| 119 | + var globalJsonFile = tempDir.CreateFile("global.json"); |
| 120 | + await globalJsonFile.WriteAllTextAsync(""" |
| 121 | + { |
| 122 | + "sdk": { |
| 123 | + "version": "10.0.100-preview.5.25265.12" |
| 124 | + } |
| 125 | + } |
| 126 | + """); |
| 127 | + |
| 128 | + var contentNullable = await projectProvider.GetVirtualProjectContentAsync(appFile.Path, CancellationToken.None); |
| 129 | + var content = contentNullable.Value; |
| 130 | + LoggerFactory.CreateLogger<VirtualProjectXmlProviderTests>().LogTrace(content.VirtualProjectXml); |
| 131 | + |
| 132 | + Assert.Contains("<TargetFramework>net10.0</TargetFramework>", content.VirtualProjectXml); |
| 133 | + Assert.Contains("<ArtifactsPath>", content.VirtualProjectXml); |
| 134 | + Assert.Empty(content.Diagnostics); |
| 135 | + } |
| 136 | + |
| 137 | + [ConditionalFact(typeof(EnableRunApiTests))] |
| 138 | + public async Task GetProjectXml_BadPath_01() |
| 139 | + { |
| 140 | + var projectProvider = await GetProjectXmlProviderAsync(); |
| 141 | + |
| 142 | + var tempDir = TempRoot.CreateDirectory(); |
| 143 | + |
| 144 | + var globalJsonFile = tempDir.CreateFile("global.json"); |
| 145 | + await globalJsonFile.WriteAllTextAsync(""" |
| 146 | + { |
| 147 | + "sdk": { |
| 148 | + "version": "10.0.100-preview.5.25265.12" |
| 149 | + } |
| 150 | + } |
| 151 | + """); |
| 152 | + |
| 153 | + var content = await projectProvider.GetVirtualProjectContentAsync(Path.Combine(tempDir.Path, "BAD"), CancellationToken.None); |
| 154 | + Assert.Null(content); |
| 155 | + } |
| 156 | + |
| 157 | + [ConditionalFact(typeof(EnableRunApiTests))] |
| 158 | + public async Task GetProjectXml_BadDirective_01() |
| 159 | + { |
| 160 | + var projectProvider = await GetProjectXmlProviderAsync(); |
| 161 | + |
| 162 | + var tempDir = TempRoot.CreateDirectory(); |
| 163 | + var appFile = tempDir.CreateFile("app.cs"); |
| 164 | + await appFile.WriteAllTextAsync(""" |
| 165 | + #:package Newtonsoft.Json@13.0.3 |
| 166 | + #:BAD |
| 167 | + Console.WriteLine("Hello, world!"); |
| 168 | + """); |
| 169 | + |
| 170 | + var globalJsonFile = tempDir.CreateFile("global.json"); |
| 171 | + await globalJsonFile.WriteAllTextAsync(""" |
| 172 | + { |
| 173 | + "sdk": { |
| 174 | + "version": "10.0.100-preview.5.25265.12" |
| 175 | + } |
| 176 | + } |
| 177 | + """); |
| 178 | + |
| 179 | + var contentNullable = await projectProvider.GetVirtualProjectContentAsync(appFile.Path, CancellationToken.None); |
| 180 | + var content = contentNullable.Value; |
| 181 | + var diagnostic = content.Diagnostics.Single(); |
| 182 | + Assert.Contains("Unrecognized directive 'BAD'", diagnostic.Message); |
| 183 | + Assert.Equal(appFile.Path, diagnostic.Location.Path); |
| 184 | + |
| 185 | + // LinePositionSpan is not deserializing properly. |
| 186 | + // Address when implementing editor squiggles. https://github.com/dotnet/roslyn/issues/78688 |
| 187 | + Assert.Equal("(0,0)-(0,0)", diagnostic.Location.Span.ToString()); |
| 188 | + } |
| 189 | +} |
0 commit comments