-
Notifications
You must be signed in to change notification settings - Fork 836
Add project template build tests + CG reporting #6355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8cfa76d
Started on template build tests.
MackinnonBuck 90572ad
Working restore/build tests + helper script for debugging
MackinnonBuck 504d6c3
Clean up
MackinnonBuck 7876b03
Try updating CI pipeline
MackinnonBuck 965112d
Merge branch 'main' into mbuck/template-cg
MackinnonBuck d9ccb35
Only clear test output if exists
MackinnonBuck 4a3ddca
Add comments, update README, add more tests
MackinnonBuck ef5d2d7
PR feedback
MackinnonBuck 276f333
Add note to README
MackinnonBuck b2e902c
Update test/ProjectTemplates/Microsoft.Extensions.AI.Templates.Integr…
MackinnonBuck fccdf15
Update test/ProjectTemplates/Microsoft.Extensions.AI.Templates.Integr…
MackinnonBuck 57d058c
Merge branch 'main' into mbuck/template-cg
MackinnonBuck fa16de6
Fill out missing exception message
MackinnonBuck 7919312
Merge branch 'main' into mbuck/template-cg
MackinnonBuck 6d2b0d7
Fix test command logging
MackinnonBuck 0c54671
Treat warnings as errors + isolate .editorconfig
MackinnonBuck e66a00b
Generate all possible valid template combinations
MackinnonBuck 25916b9
Remove redundant Aspire args
MackinnonBuck d069189
Explicitly exclude template content from build, just in case
MackinnonBuck 3a6dcb3
Add comments about CG reporting
MackinnonBuck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
...ctTemplates/Microsoft.Extensions.AI.Templates.IntegrationTests/AIChatWebExecutionTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
// 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.Threading.Tasks; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.Extensions.AI.Templates.Tests; | ||
|
||
/// <summary> | ||
/// Contains execution tests for the "AI Chat Web" template. | ||
/// </summary> | ||
/// <remarks> | ||
/// In addition to validating that the templates build and restore correctly, | ||
/// these tests are also responsible for template component governance reporting. | ||
/// This is because the generated output is left on disk after tests complete, | ||
/// most importantly the project.assets.json file that gets created during restore. | ||
/// Therefore, it's *critical* that these tests remain in a working state, | ||
/// as disabling them will also disable CG reporting. | ||
/// </remarks> | ||
public class AIChatWebExecutionTests : TemplateExecutionTestBase<AIChatWebExecutionTests>, ITemplateExecutionTestConfigurationProvider | ||
{ | ||
public AIChatWebExecutionTests(TemplateExecutionTestFixture fixture, ITestOutputHelper outputHelper) | ||
: base(fixture, outputHelper) | ||
{ | ||
} | ||
|
||
public static TemplateExecutionTestConfiguration Configuration { get; } = new() | ||
{ | ||
TemplatePackageName = "Microsoft.Extensions.AI.Templates", | ||
TestOutputFolderPrefix = "AIChatWeb" | ||
}; | ||
|
||
public static IEnumerable<object[]> GetBasicTemplateOptions() | ||
=> GetFilteredTemplateOptions("--aspire", "false"); | ||
|
||
public static IEnumerable<object[]> GetAspireTemplateOptions() | ||
=> GetFilteredTemplateOptions("--aspire", "true"); | ||
|
||
// Do not skip. See XML docs for this test class. | ||
[Theory] | ||
[MemberData(nameof(GetBasicTemplateOptions))] | ||
public async Task CreateRestoreAndBuild_BasicTemplate(params string[] args) | ||
{ | ||
const string ProjectName = "BasicApp"; | ||
var project = await Fixture.CreateProjectAsync( | ||
templateName: "aichatweb", | ||
projectName: ProjectName, | ||
args); | ||
|
||
await Fixture.RestoreProjectAsync(project); | ||
await Fixture.BuildProjectAsync(project); | ||
} | ||
|
||
// Do not skip. See XML docs for this test class. | ||
[Theory] | ||
[MemberData(nameof(GetAspireTemplateOptions))] | ||
public async Task CreateRestoreAndBuild_AspireTemplate(params string[] args) | ||
{ | ||
const string ProjectName = "AspireApp"; | ||
var project = await Fixture.CreateProjectAsync( | ||
templateName: "aichatweb", | ||
ProjectName, | ||
args); | ||
|
||
project.StartupProjectRelativePath = $"{ProjectName}.AppHost"; | ||
|
||
await Fixture.RestoreProjectAsync(project); | ||
await Fixture.BuildProjectAsync(project); | ||
} | ||
|
||
private static readonly (string name, string[] values)[] _templateOptions = [ | ||
("--provider", ["azureopenai", "githubmodels", "ollama", "openai"]), | ||
("--vector-store", ["azureaisearch", "local", "qdrant"]), | ||
("--managed-identity", ["true", "false"]), | ||
("--aspire", ["true", "false"]), | ||
]; | ||
|
||
private static IEnumerable<object[]> GetFilteredTemplateOptions(params string[] filter) | ||
{ | ||
foreach (var options in GetAllPossibleOptions(_templateOptions)) | ||
{ | ||
if (!MatchesFilter()) | ||
{ | ||
continue; | ||
} | ||
|
||
if (HasOption("--managed-identity", "true")) | ||
{ | ||
if (HasOption("--aspire", "true")) | ||
{ | ||
// The managed identity option is disabled for the Aspire template. | ||
continue; | ||
} | ||
|
||
if (!HasOption("--vector-store", "azureaisearch") && | ||
!HasOption("--aspire", "false")) | ||
{ | ||
// Can only use managed identity when using Azure in the non-Aspire template. | ||
continue; | ||
} | ||
} | ||
|
||
if (HasOption("--vector-store", "qdrant") && | ||
HasOption("--aspire", "false")) | ||
{ | ||
// Can't use Qdrant without Aspire. | ||
continue; | ||
} | ||
|
||
yield return options; | ||
|
||
bool MatchesFilter() | ||
{ | ||
for (var i = 0; i < filter.Length; i += 2) | ||
{ | ||
if (!HasOption(filter[i], filter[i + 1])) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool HasOption(string name, string value) | ||
{ | ||
for (var i = 0; i < options.Length; i += 2) | ||
{ | ||
if (string.Equals(name, options[i], StringComparison.Ordinal) && | ||
string.Equals(value, options[i + 1], StringComparison.Ordinal)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} | ||
|
||
private static IEnumerable<string[]> GetAllPossibleOptions(ReadOnlyMemory<(string name, string[] values)> options) | ||
{ | ||
if (options.Length == 0) | ||
{ | ||
yield return []; | ||
yield break; | ||
} | ||
|
||
var first = options.Span[0]; | ||
foreach (var restSelection in GetAllPossibleOptions(options[1..])) | ||
{ | ||
foreach (var value in first.values) | ||
{ | ||
yield return [first.name, value, .. restSelection]; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...plates/Microsoft.Extensions.AI.Templates.IntegrationTests/Infrastructure/DotNetCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
|
||
namespace Microsoft.Extensions.AI.Templates.Tests; | ||
|
||
public class DotNetCommand : TestCommand | ||
{ | ||
public DotNetCommand(params ReadOnlySpan<string> args) | ||
{ | ||
FileName = WellKnownPaths.RepoDotNetExePath; | ||
|
||
foreach (var arg in args) | ||
{ | ||
Arguments.Add(arg); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...tes/Microsoft.Extensions.AI.Templates.IntegrationTests/Infrastructure/DotNetNewCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// 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.Threading.Tasks; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.Extensions.AI.Templates.Tests; | ||
|
||
public sealed class DotNetNewCommand : DotNetCommand | ||
{ | ||
private bool _customHiveSpecified; | ||
|
||
public DotNetNewCommand(params ReadOnlySpan<string> args) | ||
: base(["new", .. args]) | ||
{ | ||
} | ||
|
||
public DotNetNewCommand WithCustomHive(string path) | ||
{ | ||
Arguments.Add("--debug:custom-hive"); | ||
Arguments.Add(path); | ||
_customHiveSpecified = true; | ||
return this; | ||
} | ||
|
||
public override Task<TestCommandResult> ExecuteAsync(ITestOutputHelper outputHelper) | ||
{ | ||
if (!_customHiveSpecified) | ||
{ | ||
// If this exception starts getting thrown in cases where a custom hive is | ||
// legitimately undesirable, we can add a new 'WithoutCustomHive()' method that | ||
// just sets '_customHiveSpecified' to 'true'. | ||
throw new InvalidOperationException($"A {nameof(DotNetNewCommand)} should specify a custom hive with '{nameof(WithCustomHive)}()'."); | ||
} | ||
|
||
return base.ExecuteAsync(outputHelper); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
....Templates.IntegrationTests/Infrastructure/ITemplateExecutionTestConfigurationProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.Extensions.AI.Templates.Tests; | ||
|
||
public interface ITemplateExecutionTestConfigurationProvider | ||
{ | ||
static abstract TemplateExecutionTestConfiguration Configuration { get; } | ||
} |
27 changes: 27 additions & 0 deletions
27
...ft.Extensions.AI.Templates.IntegrationTests/Infrastructure/MessageSinkTestOutputHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
|
||
namespace Microsoft.Extensions.AI.Templates.Tests; | ||
|
||
public sealed class MessageSinkTestOutputHelper : ITestOutputHelper | ||
{ | ||
private readonly IMessageSink _messageSink; | ||
|
||
public MessageSinkTestOutputHelper(IMessageSink messageSink) | ||
{ | ||
_messageSink = messageSink; | ||
} | ||
|
||
public void WriteLine(string message) | ||
{ | ||
_messageSink.OnMessage(new DiagnosticMessage(message)); | ||
} | ||
|
||
public void WriteLine(string format, params object[] args) | ||
{ | ||
_messageSink.OnMessage(new DiagnosticMessage(format, args)); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...es/Microsoft.Extensions.AI.Templates.IntegrationTests/Infrastructure/ProcessExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Diagnostics; | ||
|
||
public static class ProcessExtensions | ||
{ | ||
public static bool TryGetHasExited(this Process process) | ||
{ | ||
try | ||
{ | ||
return process.HasExited; | ||
} | ||
catch (InvalidOperationException ex) when (ex.Message.Contains("No process is associated with this object")) | ||
{ | ||
return true; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.