-
Notifications
You must be signed in to change notification settings - Fork 863
Expand file tree
/
Copy pathMcpServerSnapshotTests.cs
More file actions
95 lines (82 loc) · 3.54 KB
/
McpServerSnapshotTests.cs
File metadata and controls
95 lines (82 loc) · 3.54 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.AI.Templates.Tests;
using Microsoft.Extensions.Logging;
using Microsoft.TemplateEngine.Authoring.TemplateVerifier;
using Microsoft.TemplateEngine.TestHelper;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.Extensions.AI.Templates.Tests;
public class McpServerSnapshotTests
{
// Keep the exclude patterns below in sync with those in Microsoft.Extensions.AI.Templates.csproj.
private static readonly string[] _verificationExcludePatterns = [
"**/bin/**",
"**/obj/**",
"**/.vs/**",
"**/*.sln",
"**/*.in",
];
private readonly ILogger _log;
public McpServerSnapshotTests(ITestOutputHelper log)
{
#pragma warning disable CA2000 // Dispose objects before losing scope
_log = new XunitLoggerProvider(log).CreateLogger("TestRun");
#pragma warning restore CA2000 // Dispose objects before losing scope
}
[Fact]
public async Task BasicTest()
{
await TestTemplateCoreAsync(scenarioName: "Basic");
}
private async Task TestTemplateCoreAsync(string scenarioName, IEnumerable<string>? templateArgs = null)
{
string workingDir = TestUtils.CreateTemporaryFolder();
string templateShortName = "mcpserver";
// Get the template location
string templateLocation = Path.Combine(WellKnownPaths.TemplateFeedLocation, "Microsoft.Extensions.AI.Templates", "src", "McpServer");
var verificationExcludePatterns = Path.DirectorySeparatorChar is '/'
? _verificationExcludePatterns
: _verificationExcludePatterns.Select(p => p.Replace('/', Path.DirectorySeparatorChar)).ToArray();
TemplateVerifierOptions options = new TemplateVerifierOptions(templateName: templateShortName)
{
TemplatePath = templateLocation,
TemplateSpecificArgs = templateArgs,
SnapshotsDirectory = "Snapshots",
OutputDirectory = workingDir,
DoNotPrependCallerMethodNameToScenarioName = true,
DoNotAppendTemplateArgsToScenarioName = true,
ScenarioName = scenarioName,
VerificationExcludePatterns = verificationExcludePatterns,
}
.WithCustomScrubbers(
ScrubbersDefinition.Empty.AddScrubber((path, content) =>
{
string filePath = path.UnixifyDirSeparators();
if (filePath.EndsWith(".csproj"))
{
// Scrub references to just-built packages and remove the suffix, if it exists.
// This allows the snapshots to remain the same regardless of where the repo is built (e.g., locally, public CI, internal CI).
var pattern = @"(?<=<PackageReference\s+Include=""Microsoft\.Extensions\..*""\s+Version="")(\d+\.\d+\.\d+)(?:-[^""]*)?(?=""\s*/>)";
content.ScrubByRegex(pattern, replacement: "$1");
}
}));
VerificationEngine engine = new VerificationEngine(_log);
await engine.Execute(options);
#pragma warning disable CA1031 // Do not catch general exception types
try
{
Directory.Delete(workingDir, recursive: true);
}
catch
{
/* don't care */
}
#pragma warning restore CA1031 // Do not catch general exception types
}
}