-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathTests.cs
More file actions
403 lines (335 loc) · 14.7 KB
/
Tests.cs
File metadata and controls
403 lines (335 loc) · 14.7 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
namespace SpacetimeDB.Codegen.Tests;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.Text;
public static class GeneratorSnapshotTests
{
// Note that we can't use assembly path here because it will be put in some deep nested folder.
// Instead, to get the test project directory, we can use the `CallerFilePath` attribute which will magically give us path to the current file.
static string GetProjectDir([CallerFilePath] string path = "") => Path.GetDirectoryName(path)!;
record struct StepOutput(string Key, IncrementalStepRunReason Reason, object Value);
class Fixture
{
private readonly string projectDir;
private readonly CSharpCompilation sampleCompilation;
public Fixture(string projectDir, CSharpCompilation sampleCompilation)
{
this.projectDir = projectDir;
this.sampleCompilation = sampleCompilation;
}
public CSharpCompilation SampleCompilation => sampleCompilation;
public static async Task<Fixture> Compile(string name)
{
var projectDir = Path.Combine(GetProjectDir(), "fixtures", name);
using var workspace = MSBuildWorkspace.Create();
var sampleProject = await workspace.OpenProjectAsync($"{projectDir}/{name}.csproj");
var compilation = await sampleProject.GetCompilationAsync();
return new(projectDir, (CSharpCompilation)compilation!);
}
public Task Verify(string fileName, object target) =>
Verifier.Verify(target).UseDirectory($"{projectDir}/snapshots").UseFileName(fileName);
private static CSharpGeneratorDriver CreateDriver(
IIncrementalGenerator generator,
LanguageVersion languageVersion
)
{
return CSharpGeneratorDriver.Create(
[generator.AsSourceGenerator()],
driverOptions: new(
disabledOutputs: IncrementalGeneratorOutputKind.None,
trackIncrementalGeneratorSteps: true
),
// Make sure that generated files are parsed with the same language version.
parseOptions: new(languageVersion)
);
}
private async Task<IEnumerable<SyntaxTree>> RunAndCheckGenerator(
IIncrementalGenerator generator
)
{
var driver = CreateDriver(generator, sampleCompilation.LanguageVersion);
// Store the new driver instance - it contains the results and the cache.
var driverAfterGen = driver.RunGenerators(sampleCompilation);
var genResult = driverAfterGen.GetRunResult();
// Verify the generated code against the snapshots.
await Verify(generator.GetType().Name, genResult);
CheckCacheWorking(sampleCompilation, driverAfterGen);
return genResult.GeneratedTrees;
}
public GeneratorDriverRunResult RunGeneratorAndGetResult(IIncrementalGenerator generator)
{
var driver = CreateDriver(generator, sampleCompilation.LanguageVersion);
return driver.RunGenerators(sampleCompilation).GetRunResult();
}
public async Task<CSharpCompilation> RunAndCheckGenerators(
params IIncrementalGenerator[] generators
) =>
sampleCompilation.AddSyntaxTrees(
(await Task.WhenAll(generators.Select(RunAndCheckGenerator))).SelectMany(output =>
output
)
);
}
private static void CheckCacheWorking(
CSharpCompilation sampleCompilation,
GeneratorDriver driverAfterGen
)
{
// Run again with a driver containing the cache and a trivially modified code to verify that the cache is working.
var modifiedCompilation = sampleCompilation
.RemoveAllSyntaxTrees()
.AddSyntaxTrees(
sampleCompilation.SyntaxTrees.Select(tree =>
tree.WithChangedText(
SourceText.From(
string.Join(
"\n",
tree.GetText().Lines.Select(line => $"{line} // Modified")
)
)
)
)
);
var driverAfterRegen = driverAfterGen.RunGenerators(modifiedCompilation);
var regenSteps = driverAfterRegen
.GetRunResult()
.Results.SelectMany(result => result.TrackedSteps)
.Where(step => step.Key.StartsWith("SpacetimeDB."))
.SelectMany(step =>
step.Value.SelectMany(value => value.Outputs)
.Select(output => new StepOutput(step.Key, output.Reason, output.Value))
)
.ToImmutableArray();
// Ensure that we have tracked steps at all.
Assert.NotEmpty(regenSteps);
// Ensure that all steps were cached.
Assert.Empty(
regenSteps.Where(step =>
step.Reason
is not (IncrementalStepRunReason.Cached or IncrementalStepRunReason.Unchanged)
)
);
}
static IEnumerable<Diagnostic> GetCompilationErrors(Compilation compilation)
{
return compilation
.Emit(Stream.Null)
.Diagnostics.Where(diag => diag.Severity != DiagnosticSeverity.Hidden)
// The order of diagnostics is not predictable, sort them by location to make the test deterministic.
.OrderBy(diag => diag.GetMessage() + diag.Location.ToString());
}
static void AssertGeneratedCodeDoesNotUseInternalBound(CSharpCompilation compilation)
{
var generatedText = string.Join(
"\n\n",
compilation.SyntaxTrees.Select(tree => tree.GetText().ToString())
);
Assert.DoesNotContain("global::SpacetimeDB.Internal.Bound<", generatedText);
Assert.Contains("global::SpacetimeDB.Bound<", generatedText);
}
static void AssertPublicBoundIsAvailableInRuntime(Compilation compilation)
{
var bound = compilation.GetTypeByMetadataName("SpacetimeDB.Bound`1");
Assert.NotNull(bound);
Assert.Equal(Accessibility.Public, bound!.DeclaredAccessibility);
}
static void AssertRuntimeDoesNotDefineLocal(Compilation compilation)
{
var runtimeAssembly = compilation
.References.Select(r => compilation.GetAssemblyOrModuleSymbol(r))
.OfType<IAssemblySymbol>()
.FirstOrDefault(a => a.Name == "SpacetimeDB.Runtime");
Assert.NotNull(runtimeAssembly);
// These types are generated per-module by SpacetimeDB.Codegen.Module.
// If Runtime defines any of them too, user projects can hit CS0436 warnings.
var codegenOwnedTypes = new[]
{
"SpacetimeDB.Local",
"SpacetimeDB.ProcedureContext",
"SpacetimeDB.ProcedureTxContext",
"SpacetimeDB.ReducerContext",
"SpacetimeDB.ViewContext",
"SpacetimeDB.AnonymousViewContext",
};
foreach (var name in codegenOwnedTypes)
{
Assert.Null(runtimeAssembly!.GetTypeByMetadataName(name));
}
}
static void AssertNoCs0436Diagnostics(Compilation compilation)
{
var diagnostics = compilation
.Emit(Stream.Null)
.Diagnostics.Where(diag => diag.Severity != DiagnosticSeverity.Hidden);
Assert.DoesNotContain(diagnostics, d => d.Id == "CS0436");
}
[Fact]
public static async Task TypeGeneratorOnClient()
{
var fixture = await Fixture.Compile("client");
var compilationAfterGen = await fixture.RunAndCheckGenerators(
new SpacetimeDB.Codegen.Type()
);
Assert.Empty(GetCompilationErrors(compilationAfterGen));
}
[Fact]
public static async Task TypeAndModuleGeneratorsOnServer()
{
var fixture = await Fixture.Compile("server");
var compilationAfterGen = await fixture.RunAndCheckGenerators(
new SpacetimeDB.Codegen.Type(),
new SpacetimeDB.Codegen.Module()
);
Assert.Empty(GetCompilationErrors(compilationAfterGen));
AssertPublicBoundIsAvailableInRuntime(compilationAfterGen);
AssertRuntimeDoesNotDefineLocal(compilationAfterGen);
AssertGeneratedCodeDoesNotUseInternalBound(compilationAfterGen);
// Regression guard for user-reported warning spam:
// make sure a downstream "user" file that references SpacetimeDB.Local doesn't trigger CS0436.
var userCode =
"namespace User; public sealed class UseLocal { public SpacetimeDB.Local Db; }";
var userTree = CSharpSyntaxTree.ParseText(
userCode,
new CSharpParseOptions(compilationAfterGen.LanguageVersion)
);
var compilationWithUserCode = compilationAfterGen.AddSyntaxTrees(userTree);
AssertNoCs0436Diagnostics(compilationWithUserCode);
}
[Fact]
public static async Task SettingsAndExplicitNames()
{
var fixture = await Fixture.Compile("explicitnames");
var compilationAfterGen = await fixture.RunAndCheckGenerators(
new SpacetimeDB.Codegen.Type(),
new SpacetimeDB.Codegen.Module()
);
Assert.Empty(GetCompilationErrors(compilationAfterGen));
AssertPublicBoundIsAvailableInRuntime(compilationAfterGen);
AssertRuntimeDoesNotDefineLocal(compilationAfterGen);
AssertGeneratedCodeDoesNotUseInternalBound(compilationAfterGen);
}
[Fact]
public static async Task CSharpKeywordIdentifiersAreEscapedInGeneratedCode()
{
var fixture = await Fixture.Compile("server");
const string source =
"""
using SpacetimeDB;
[SpacetimeDB.Table]
public partial struct KeywordTable
{
[SpacetimeDB.PrimaryKey]
public ulong @class;
public int @params;
}
[SpacetimeDB.Table(Accessor = "class")]
public partial struct AccessorKeywordTable
{
[SpacetimeDB.PrimaryKey]
[SpacetimeDB.Index.BTree(Accessor = "class")]
public int Id;
}
[SpacetimeDB.Table]
public partial struct @class
{
[SpacetimeDB.PrimaryKey]
public int Id;
}
public static partial class KeywordApis
{
[SpacetimeDB.Reducer]
public static void KeywordReducer(ReducerContext ctx, int @params, string @class)
{
_ = @params;
_ = @class;
}
[SpacetimeDB.Reducer]
public static void @class(ReducerContext ctx)
{
}
[SpacetimeDB.Procedure]
public static int KeywordProcedure(ProcedureContext ctx, int @params, int @class)
{
return @params + @class;
}
[SpacetimeDB.Procedure]
public static void @params(ProcedureContext ctx)
{
}
}
""";
var parseOptions = new CSharpParseOptions(fixture.SampleCompilation.LanguageVersion);
var tree = CSharpSyntaxTree.ParseText(source, parseOptions, path: "KeywordNames.cs");
var compilation = fixture.SampleCompilation.AddSyntaxTrees(tree);
var driver = CSharpGeneratorDriver.Create(
[new SpacetimeDB.Codegen.Type().AsSourceGenerator(), new SpacetimeDB.Codegen.Module().AsSourceGenerator()],
driverOptions: new(
disabledOutputs: IncrementalGeneratorOutputKind.None,
trackIncrementalGeneratorSteps: true
),
parseOptions: parseOptions
);
var runResult = driver.RunGenerators(compilation).GetRunResult();
var compilationAfterGen = compilation.AddSyntaxTrees(runResult.GeneratedTrees);
Assert.Empty(GetCompilationErrors(compilationAfterGen));
}
[Fact]
public static async Task TestDiagnostics()
{
var fixture = await Fixture.Compile("diag");
var compilationAfterGen = await fixture.RunAndCheckGenerators(
new SpacetimeDB.Codegen.Type(),
new SpacetimeDB.Codegen.Module()
);
// Unlike in regular tests, we don't expect this compilation to succeed - it's supposed to be full of errors.
// We already reported the useful ones from the generator, but let's snapshot those emitted by the compiler as well.
// This way we can notice when they get particularly noisy and improve our codegen for the case of a broken code.
await fixture.Verify("ExtraCompilationErrors", GetCompilationErrors(compilationAfterGen));
AssertPublicBoundIsAvailableInRuntime(compilationAfterGen);
AssertRuntimeDoesNotDefineLocal(compilationAfterGen);
AssertGeneratedCodeDoesNotUseInternalBound(compilationAfterGen);
}
[Fact]
public static async Task ViewInvalidReturnHighlightsReturnType()
{
var fixture = await Fixture.Compile("diag");
var runResult = fixture.RunGeneratorAndGetResult(new SpacetimeDB.Codegen.Module());
var method = fixture
.SampleCompilation.SyntaxTrees.Select(tree => new
{
Tree = tree,
Root = tree.GetRoot(),
})
.SelectMany(entry =>
entry
.Root.DescendantNodes()
.OfType<MethodDeclarationSyntax>()
.Select(method => new
{
entry.Tree,
entry.Root,
Method = method,
})
)
.Single(entry => entry.Method.Identifier.Text == "ViewDefIEnumerableReturnFromIter");
var returnTypeSpan = method.Method.ReturnType.Span;
var diagnostics = runResult
.Results.SelectMany(result => result.Diagnostics)
.Where(d => d.Id == "STDB0024")
.ToList();
var diagnostic = diagnostics.FirstOrDefault(d =>
d.GetMessage().Contains("ViewDefIEnumerableReturnFromIter")
&& d.Location.SourceTree == method.Tree
);
Assert.NotNull(diagnostic);
Assert.Equal(returnTypeSpan, diagnostic!.Location.SourceSpan);
var returnTypeText = method
.Root.ToFullString()
.Substring(returnTypeSpan.Start, returnTypeSpan.Length);
Assert.Contains("IEnumerable", returnTypeText);
}
}