forked from dotnet/try
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCSharpFileCompilationTests.cs
More file actions
253 lines (223 loc) · 10.4 KB
/
CSharpFileCompilationTests.cs
File metadata and controls
253 lines (223 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Net.Http.Json;
using System.Reflection;
using System.Text.Json;
using System.Text.RegularExpressions;
using AwesomeAssertions;
using AwesomeAssertions.Execution;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.DotNet.Interactive.Connection;
using Microsoft.DotNet.Interactive.CSharpProject.Events;
using Microsoft.DotNet.Interactive.Events;
using Xunit;
namespace Microsoft.TryDotNet.FileIntegration.Tests;
/// <summary>
/// Integration tests that compile C# source files through the Try .NET API and verify
/// compilation outcomes against optional chapter-listings.json metadata.
/// </summary>
/// <remarks>
/// <para>Test data comes from two sources:</para>
/// <list type="bullet">
/// <item><strong>Embedded samples</strong> — <c>.cs</c> files in the <c>Samples/</c> directory
/// deployed alongside the test assembly. These have no chapter metadata and are always
/// expected to compile successfully.</item>
/// <item><strong>External listings</strong> — files under the path specified by the
/// <c>TRYDOTNET_LISTINGS_PATH</c> environment variable, following a
/// <c>Chapter##/##.##.cs</c> naming convention. Compilation expectations are driven by
/// the <see cref="ChapterListingsFixture"/> metadata.</item>
/// </list>
/// </remarks>
public class CSharpFileCompilationTests : IClassFixture<WebApplicationFactory<Program>>, IClassFixture<ChapterListingsFixture>
{
private readonly WebApplicationFactory<Program> _factory;
private readonly ChapterListingsFixture _listingsFixture;
public CSharpFileCompilationTests(WebApplicationFactory<Program> factory, ChapterListingsFixture listingsFixture)
{
_factory = factory;
_listingsFixture = listingsFixture;
}
/// <summary>
/// Provides test data by enumerating embedded sample files and, when configured,
/// external chapter listing files. Each row contains the full path, a display name,
/// and an optional chapter name.
/// </summary>
public static IEnumerable<object[]> SampleFiles()
{
// Always include embedded Samples/ directory
var samplesDir = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!,
"Samples");
if (Directory.Exists(samplesDir))
{
foreach (var file in Directory.EnumerateFiles(samplesDir, "*.cs"))
{
// Embedded samples have no chapter
yield return new object[] { file, Path.GetFileName(file), null };
}
}
// Additionally include external listings if configured
var externalPath = Environment.GetEnvironmentVariable(ChapterListingsFixture.ListingsPathEnvVar);
if (!string.IsNullOrEmpty(externalPath) && Directory.Exists(externalPath))
{
foreach (var file in Directory.EnumerateFiles(externalPath, "*.cs", SearchOption.AllDirectories))
{
var relativePath = Path.GetRelativePath(externalPath, file);
var segments = relativePath.Split(Path.DirectorySeparatorChar);
// Only include Chapter##/##.##.cs — exactly two segments, matching the numbering pattern
if (segments.Length != 2)
continue;
if (!Regex.IsMatch(segments[0], @"^Chapter\d+$"))
continue;
if (!Regex.IsMatch(segments[1], @"^\d+\.\d+\.cs$"))
continue;
yield return new object[] { file, relativePath, segments[0] };
}
}
}
/// <summary>
/// Compiles a single C# source file through the Try .NET API and asserts the outcome
/// matches the expectation from chapter-listings.json (if available), or that embedded
/// samples compile successfully.
/// </summary>
[Theory]
[MemberData(nameof(SampleFiles))]
public async Task File_compilation_matches_expectation(string fullPath, string displayName, string? chapterName)
{
var fileName = Path.GetFileName(fullPath);
var fileContent = await File.ReadAllTextAsync(fullPath);
var client = _factory.CreateDefaultClient();
var requestJson = BuildCommandPayload(fileName, fileContent);
var requestBody = JsonContent.Create(JsonDocument.Parse(requestJson).RootElement);
var response = await client.PostAsync("commands", requestBody);
var responseJson = JsonDocument.Parse(
await response.Content.ReadAsStringAsync(CancellationToken.None)).RootElement;
var events = responseJson
.GetProperty("events")
.EnumerateArray()
.Select(KernelEventEnvelope.Deserialize)
.Select(ee => ee.Event)
.ToList();
response.EnsureSuccessStatusCode();
var assemblyProduced = events.OfType<AssemblyProduced>().SingleOrDefault();
bool actualCompiled = assemblyProduced is not null;
// Record result for fixture update logic
if (chapterName is not null)
{
_listingsFixture.ActualResults[(chapterName, fileName)] = actualCompiled;
}
var expectedCompile = _listingsFixture.GetExpectedCanCompile(chapterName, fileName);
// Chapter files require the metadata JSON to be present. Fail early with a clear
// message rather than silently asserting "all chapter files compile", which is wrong
// for listings that intentionally reference types defined in other listing files.
if (chapterName is not null && _listingsFixture.Model is null)
{
var resolvedPath = _listingsFixture.JsonFilePath ?? "(unresolved — set TRYDOTNET_LISTINGS_PATH or TRYDOTNET_LISTINGS_METADATA)";
false.Should().BeTrue(
$"chapter-listings.json could not be loaded, so compilation expectations for " +
$"chapter files are unavailable. Resolved path: {resolvedPath}");
return;
}
if (expectedCompile is null)
{
// No metadata entry (embedded samples, unknown files): assert compilation succeeds
AssertCompilationSucceeded(assemblyProduced, events, displayName);
}
else if (_listingsFixture.UpdateEnabled && expectedCompile.Value != actualCompiled)
{
// Update mode: mismatch is noted but test passes — fixture writes the JSON on dispose
Console.WriteLine(
$"[UPDATE] '{displayName}': expected can_compile={expectedCompile.Value}, actual={actualCompiled}. Will update JSON on dispose.");
}
else if (expectedCompile.Value)
{
// Expected to compile successfully per chapter-listings.json
AssertCompilationSucceeded(assemblyProduced, events, displayName,
"per chapter-listings.json");
}
else
{
// Expected to fail compilation per chapter-listings.json
actualCompiled.Should().BeFalse(
$"Expected '{displayName}' to fail compilation per chapter-listings.json, but it succeeded.");
}
}
/// <summary>
/// Asserts that compilation produced a non-empty assembly, collecting diagnostic details
/// on failure for a descriptive assertion message.
/// </summary>
private static void AssertCompilationSucceeded(
AssemblyProduced? assemblyProduced,
List<KernelEvent> events,
string displayName,
string? context = null)
{
using var scope = new AssertionScope();
if (assemblyProduced is null)
{
var details = FormatCompilationFailureDetails(events);
var contextSuffix = context is not null ? $" {context}" : "";
assemblyProduced.Should().NotBeNull(
$"Expected compilation of '{displayName}' to produce an assembly{contextSuffix}, but it failed:{Environment.NewLine}{details}");
}
else
{
assemblyProduced.Assembly.Value.Should().NotBeNullOrWhiteSpace(
$"AssemblyProduced for '{displayName}' had an empty assembly value.");
}
}
/// <summary>
/// Extracts <see cref="CommandFailed"/> messages and <see cref="DiagnosticsProduced"/>
/// entries from the event stream and formats them into a human-readable string.
/// </summary>
private static string FormatCompilationFailureDetails(List<KernelEvent> events)
{
var failures = events.OfType<CommandFailed>()
.Select(f => f.Message);
var diagnostics = events.OfType<DiagnosticsProduced>()
.SelectMany(d => d.Diagnostics)
.Select(d => $"{d.Severity} {d.Code}: {d.Message} ({d.LinePositionSpan})");
return string.Join(Environment.NewLine,
failures.Concat(diagnostics).DefaultIfEmpty("No AssemblyProduced event and no diagnostics found."));
}
/// <summary>
/// Builds the JSON command payload that opens a project with a single file,
/// opens that file as the active document, and compiles the project.
/// </summary>
private static string BuildCommandPayload(string fileName, string fileContent)
{
var escapedContent = JsonEncodedText.Encode(fileContent).ToString();
return $$"""
{
"commands": [
{
"commandType": "OpenProject",
"command": {
"project": {
"files": [
{
"relativeFilePath": "{{fileName}}",
"content": "{{escapedContent}}"
}
]
}
},
"token": "file-test::1"
},
{
"commandType": "OpenDocument",
"command": {
"relativeFilePath": "{{fileName}}"
},
"token": "file-test::2"
},
{
"commandType": "CompileProject",
"command": {},
"token": "file-test::3"
}
]
}
""";
}
}