Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.TestUtilities;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -71,16 +72,32 @@ public async Task CreateRestoreAndBuild_AspireTemplate(params string[] args)
await Fixture.BuildProjectAsync(project);
}

/// <summary>
/// Runs a single test with --aspire true and a project name that will trigger the class
/// name normalization bug reported in https://github.com/dotnet/extensions/issues/6811.
/// </summary>
[Fact]
public async Task CreateRestoreAndBuild_AspireProjectName()
{
await CreateRestoreAndBuild_AspireProjectName_Variants("azureopenai", "mix.ed-dash_name 123");
}

/// <summary>
/// Tests build for various project name formats, including dots and other
/// separators, to trigger the class name normalization bug described
/// in https://github.com/dotnet/extensions/issues/6811
/// This runs for all provider combinations with --aspire true and different
/// project names to ensure the bug is caught in all scenarios.
/// </summary>
[Theory]
[MemberData(nameof(GetProjectNameVariants))]
public async Task CreateRestoreAndBuild_ProjectNameVariants(string provider, string projectName)
/// <remarks>
/// Because this test takes a long time to run, it is skipped by default. Set the
/// environment variable <c>AI_TEMPLATES_TEST_PROJECT_NAMES</c> to "true" or "1"
/// to enable it.
/// </remarks>
[ConditionalTheory]
[EnvironmentVariableSkipCondition("AI_TEMPLATES_TEST_PROJECT_NAMES", "true", "1")]
[MemberData(nameof(GetAspireProjectNameVariants))]
public async Task CreateRestoreAndBuild_AspireProjectName_Variants(string provider, string projectName)
{
var project = await Fixture.CreateProjectAsync(
templateName: "aichatweb",
Expand Down Expand Up @@ -181,22 +198,22 @@ private static IEnumerable<string[]> GetAllPossibleOptions(ReadOnlyMemory<(strin
}
}

public static IEnumerable<object[]> GetProjectNameVariants()
public static IEnumerable<object[]> GetAspireProjectNameVariants()
{
foreach (string provider in new[] { "ollama", "openai", "azureopenai", "githubmodels" })
{
foreach (string projectName in new[]
{
"dot.name",
"project.123",
"space name",
"mix.ed-dash_name 123",
".1My.Projec-",
"1Project123",
"11double",
"1",
"nomatch"
})
"mix.ed-dash_name 123",
"dot.name",
"project.123",
"space name",
".1My.Projec-",
"1Project123",
"11double",
"1",
"nomatch"
})
{
yield return new object[] { provider, projectName };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<PackageReference Include="Microsoft.TemplateEngine.TestHelper" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\TestUtilities\TestUtilities.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Snapshots\**\*.*" />
<None Include="Snapshots\**\*.*" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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.Linq;

namespace Microsoft.TestUtilities;

/// <summary>
/// Skips a test based on the value of an environment variable.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
public class EnvironmentVariableSkipConditionAttribute : Attribute, ITestCondition
{
private string? _currentValue;

/// <summary>
/// Initializes a new instance of the <see cref="EnvironmentVariableSkipConditionAttribute"/> class.
/// </summary>
/// <param name="variableName">Name of the environment variable.</param>
/// <param name="values">Value(s) of the environment variable to match for the condition.</param>
/// <remarks>
/// By default, the test will be run if the value of the variable matches any of the supplied values.
/// Set <see cref="RunOnMatch"/> to <c>False</c> to run the test only if the value does not match.
/// </remarks>
public EnvironmentVariableSkipConditionAttribute(string variableName, params string[] values)
{
if (string.IsNullOrEmpty(variableName))
{
throw new ArgumentException("Value cannot be null or empty.", nameof(variableName));
}

if (values == null || values.Length == 0)
{
throw new ArgumentException("You must supply at least one value to match.", nameof(values));
}

VariableName = variableName;
Values = values;
}

/// <summary>
/// Gets or sets a value indicating whether the test should run if the value of the variable matches any
/// of the supplied values. If <c>False</c>, the test runs only if the value does not match any of the
/// supplied values. Default is <c>True</c>.
/// </summary>
public bool RunOnMatch { get; set; } = true;

/// <summary>
/// Gets the name of the environment variable.
/// </summary>
public string VariableName { get; }

/// <summary>
/// Gets the value(s) of the environment variable to match for the condition.
/// </summary>
public string[] Values { get; }

/// <summary>
/// Gets a value indicating whether the condition is met for the configured environment variable and values.
/// </summary>
public bool IsMet
{
get
{
_currentValue ??= Environment.GetEnvironmentVariable(VariableName);
var hasMatched = Values.Any(value => string.Equals(value, _currentValue, StringComparison.OrdinalIgnoreCase));

return RunOnMatch ? hasMatched : !hasMatched;
}
}

/// <summary>
/// Gets a value indicating the reason the test was skipped.
/// </summary>
public string SkipReason
{
get
{
var value = _currentValue ?? "(null)";

return $"Test skipped on environment variable with name '{VariableName}' and value '{value}' " +
$"for the '{nameof(RunOnMatch)}' value of '{RunOnMatch}'.";
}
}
}