-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathTests.cs
More file actions
243 lines (204 loc) · 9.39 KB
/
Copy pathTests.cs
File metadata and controls
243 lines (204 loc) · 9.39 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
namespace SpacetimeDB.Codegen.Tests;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
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(string projectDir, CSharpCompilation 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 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 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);
}
}