| 1 | using CascadeIDE.Services; |
| 2 | using Xunit; |
| 3 | |
| 4 | namespace CascadeIDE.Tests.MonacoForward; |
| 5 | |
| 6 | [Trait("Category", "MonacoForward")] |
| 7 | public sealed class CSharpProjectGlobalUsingsResolverTests |
| 8 | { |
| 9 | [Fact] |
| 10 | public void Resolve_uses_sdk_implicit_usings_from_csproj() |
| 11 | { |
| 12 | var csproj = FindRepoFile(@"CasaField.Core\CasaField.Core.csproj"); |
| 13 | var source = Path.Combine(Path.GetDirectoryName(csproj)!, "Sample.cs"); |
| 14 | var text = CSharpProjectGlobalUsingsResolver.ResolveGlobalUsingsTree(source).ToString(); |
| 15 | |
| 16 | Assert.Contains("global using System;", text); |
| 17 | Assert.Contains("global using System.Linq;", text); |
| 18 | Assert.DoesNotContain("global using System.Text;", text); |
| 19 | } |
| 20 | |
| 21 | [Fact] |
| 22 | public void Profile_merges_using_include_and_remove() |
| 23 | { |
| 24 | var dir = Path.Combine(Path.GetTempPath(), "cide-usings-" + Guid.NewGuid().ToString("N")); |
| 25 | Directory.CreateDirectory(dir); |
| 26 | try |
| 27 | { |
| 28 | var csproj = Path.Combine(dir, "App.csproj"); |
| 29 | File.WriteAllText(csproj, """ |
| 30 | <Project Sdk="Microsoft.NET.Sdk"> |
| 31 | <PropertyGroup> |
| 32 | <ImplicitUsings>enable</ImplicitUsings> |
| 33 | </PropertyGroup> |
| 34 | <ItemGroup> |
| 35 | <Using Include="System.Text" /> |
| 36 | <Using Remove="System.Net.Http" /> |
| 37 | </ItemGroup> |
| 38 | </Project> |
| 39 | """); |
| 40 | |
| 41 | var profile = CSharpProjectUsingsProfile.TryLoad(csproj); |
| 42 | Assert.NotNull(profile); |
| 43 | var ns = profile!.ResolveNamespaces(); |
| 44 | Assert.Contains("System.Text", ns); |
| 45 | Assert.Contains("System.Linq", ns); |
| 46 | Assert.DoesNotContain("System.Net.Http", ns); |
| 47 | } |
| 48 | finally |
| 49 | { |
| 50 | try { Directory.Delete(dir, recursive: true); } catch { /* best effort */ } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | [Fact] |
| 55 | public void Resolve_prefers_generated_GlobalUsings_g_cs() |
| 56 | { |
| 57 | var dir = Path.Combine(Path.GetTempPath(), "cide-usings-" + Guid.NewGuid().ToString("N")); |
| 58 | var objDir = Path.Combine(dir, "obj", "Debug", "net10.0"); |
| 59 | Directory.CreateDirectory(objDir); |
| 60 | try |
| 61 | { |
| 62 | var csproj = Path.Combine(dir, "App.csproj"); |
| 63 | File.WriteAllText(csproj, """<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><ImplicitUsings>disable</ImplicitUsings></PropertyGroup></Project>"""); |
| 64 | var generated = Path.Combine(objDir, "GlobalUsings.g.cs"); |
| 65 | File.WriteAllText(generated, "global using System.Text;\n"); |
| 66 | |
| 67 | var source = Path.Combine(dir, "Program.cs"); |
| 68 | File.WriteAllText(source, "class Program { }"); |
| 69 | |
| 70 | var text = CSharpProjectGlobalUsingsResolver.ResolveGlobalUsingsTree(source).ToString(); |
| 71 | Assert.Contains("global using System.Text;", text); |
| 72 | Assert.DoesNotContain("global using System.Linq;", text); |
| 73 | } |
| 74 | finally |
| 75 | { |
| 76 | try { Directory.Delete(dir, recursive: true); } catch { /* best effort */ } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | [Fact] |
| 81 | public void FindOwningProjectFile_picks_nested_csproj() |
| 82 | { |
| 83 | var csproj = FindRepoFile(@"CasaField.Core\CasaField.Core.csproj"); |
| 84 | var source = Path.Combine(Path.GetDirectoryName(csproj)!, "Grid", "Decoder.cs"); |
| 85 | var found = CSharpProjectGlobalUsingsResolver.FindOwningProjectFile(source); |
| 86 | Assert.Equal(csproj, found); |
| 87 | } |
| 88 | |
| 89 | private static string FindRepoFile(string relativePath) |
| 90 | { |
| 91 | var dir = AppContext.BaseDirectory; |
| 92 | for (var i = 0; i < 8; i++) |
| 93 | { |
| 94 | var candidate = Path.GetFullPath(Path.Combine(dir, relativePath)); |
| 95 | if (File.Exists(candidate)) |
| 96 | return candidate; |
| 97 | dir = Path.GetDirectoryName(dir)!; |
| 98 | } |
| 99 | |
| 100 | throw new FileNotFoundException(relativePath); |
| 101 | } |
| 102 | } |
| 103 | |