Add include and exclude attributes to EnvironmentTagHelper

Addresses #5671
This commit is contained in:
Jass Bagga 2017-01-27 14:46:53 -08:00 committed by GitHub
parent d44c9aee1e
commit 7449ffad74
2 changed files with 163 additions and 15 deletions

View File

@ -11,6 +11,8 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
/// <summary> /// <summary>
/// <see cref="ITagHelper"/> implementation targeting &lt;environment&gt; elements that conditionally renders /// <see cref="ITagHelper"/> implementation targeting &lt;environment&gt; elements that conditionally renders
/// content based on the current value of <see cref="IHostingEnvironment.EnvironmentName"/>. /// content based on the current value of <see cref="IHostingEnvironment.EnvironmentName"/>.
/// If the environment is not listed in the specified <see cref="Names"/> or <see cref="Include"/>,
/// or if it is in <see cref="Exclude"/>, the content will not be rendered.
/// </summary> /// </summary>
public class EnvironmentTagHelper : TagHelper public class EnvironmentTagHelper : TagHelper
{ {
@ -30,6 +32,7 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
/// <summary> /// <summary>
/// A comma separated list of environment names in which the content should be rendered. /// A comma separated list of environment names in which the content should be rendered.
/// If the current environment is also in the <see cref="Exclude"/> list, the content will not be rendered.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// The specified environment names are compared case insensitively to the current value of /// The specified environment names are compared case insensitively to the current value of
@ -37,6 +40,25 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
/// </remarks> /// </remarks>
public string Names { get; set; } public string Names { get; set; }
/// <summary>
/// A comma separated list of environment names in which the content should be rendered.
/// If the current environment is also in the <see cref="Exclude"/> list, the content will not be rendered.
/// </summary>
/// <remarks>
/// The specified environment names are compared case insensitively to the current value of
/// <see cref="IHostingEnvironment.EnvironmentName"/>.
/// </remarks>
public string Include { get; set; }
/// <summary>
/// A comma separated list of environment names in which the content will not be rendered.
/// </summary>
/// <remarks>
/// The specified environment names are compared case insensitively to the current value of
/// <see cref="IHostingEnvironment.EnvironmentName"/>.
/// </remarks>
public string Exclude { get; set; }
protected IHostingEnvironment HostingEnvironment { get; } protected IHostingEnvironment HostingEnvironment { get; }
/// <inheritdoc /> /// <inheritdoc />
@ -55,7 +77,7 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
// Always strip the outer tag name as we never want <environment> to render // Always strip the outer tag name as we never want <environment> to render
output.TagName = null; output.TagName = null;
if (string.IsNullOrWhiteSpace(Names)) if (string.IsNullOrWhiteSpace(Names) && string.IsNullOrWhiteSpace(Include) && string.IsNullOrWhiteSpace(Exclude))
{ {
// No names specified, do nothing // No names specified, do nothing
return; return;
@ -68,27 +90,67 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
return; return;
} }
var tokenizer = new StringTokenizer(Names, NameSeparator); if (Exclude != null)
var hasEnvironments = false;
foreach (var item in tokenizer)
{ {
var environment = item.Trim(); var tokenizer = new StringTokenizer(Exclude, NameSeparator);
if (environment.HasValue && environment.Length > 0) foreach (var item in tokenizer)
{ {
hasEnvironments = true; var environment = item.Trim();
if (environment.Equals(currentEnvironmentName, StringComparison.OrdinalIgnoreCase)) if (environment.HasValue && environment.Length > 0)
{ {
// Matching environment name found, do nothing if (environment.Equals(currentEnvironmentName, StringComparison.OrdinalIgnoreCase))
return; {
// Matching environment name found, suppress output
output.SuppressOutput();
return;
}
}
}
}
var hasEnvironments = false;
if (Names != null)
{
var tokenizer = new StringTokenizer(Names, NameSeparator);
foreach (var item in tokenizer)
{
var environment = item.Trim();
if (environment.HasValue && environment.Length > 0)
{
hasEnvironments = true;
if (environment.Equals(currentEnvironmentName, StringComparison.OrdinalIgnoreCase))
{
// Matching environment name found, do nothing
return;
}
}
}
}
if (Include != null)
{
var tokenizer = new StringTokenizer(Include, NameSeparator);
foreach (var item in tokenizer)
{
var environment = item.Trim();
if (environment.HasValue && environment.Length > 0)
{
hasEnvironments = true;
if (environment.Equals(currentEnvironmentName, StringComparison.OrdinalIgnoreCase))
{
// Matching environment name found, do nothing
return;
}
} }
} }
} }
if (hasEnvironments) if (hasEnvironments)
{ {
// This instance had at least one non-empty environment specified but none of these // This instance had at least one non-empty environment (names or include) specified but none of these
// environments matched the current environment. Suppress the output in this case. // environments matched the current environment. Suppress the output in this case.
output.SuppressOutput(); output.SuppressOutput();
return;
} }
} }
} }

View File

@ -66,6 +66,94 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers.Test
ShouldShowContent(namesAttribute, environmentName); ShouldShowContent(namesAttribute, environmentName);
} }
[Theory]
[InlineData("", "", "")]
[InlineData("", null, "Test")]
[InlineData("Development", "", "")]
[InlineData("", "Development, Test", "")]
[InlineData(null, "development, TEST", "Test")]
[InlineData("Development", "", "Test")]
[InlineData("Development", "Test, Development", "")]
[InlineData("Test", "DEVELOPMENT", null)]
[InlineData("Development", "Test", "")]
[InlineData("Development", null, "Test")]
[InlineData("Development", "Test", "Test")]
[InlineData("Test", "Development", "Test")]
public void ShouldShowContent_IncludeExcludeSpecified(string namesAttribute, string includeAttribute, string excludeAttribute)
{
// Arrange
var content = "content";
var context = MakeTagHelperContext(
attributes: new TagHelperAttributeList {
{ "names", namesAttribute },
{ "include", includeAttribute },
{ "exclude", excludeAttribute },
});
var output = MakeTagHelperOutput("environment", childContent: content);
var hostingEnvironment = new Mock<IHostingEnvironment>();
hostingEnvironment.SetupProperty(h => h.EnvironmentName, "Development");
// Act
var helper = new EnvironmentTagHelper(hostingEnvironment.Object)
{
Names = namesAttribute,
Include = includeAttribute,
Exclude = excludeAttribute,
};
helper.Process(context, output);
// Assert
Assert.Null(output.TagName);
Assert.False(output.IsContentModified);
}
[Theory]
[InlineData(null, "", "Development")]
[InlineData("", "Development", "development")]
[InlineData("", "Test", "Development, test")]
[InlineData("Development", "", "Development")]
[InlineData("Test", "", "Development")]
[InlineData("Development", "Development", "DEVELOPMENT, TEST")]
[InlineData("Development", "Test", "Development")]
[InlineData("Test", "Development", "Development")]
[InlineData("Test", "Test", "Development")]
[InlineData("", "Test", "Test")]
[InlineData("Test", null, "Test")]
[InlineData("Test", "Test", "Test")]
[InlineData("", "Test", null)]
[InlineData("Test", "", "")]
[InlineData("Test", "Test", null)]
public void DoesNotShowContent_IncludeExcludeSpecified(string namesAttribute, string includeAttribute, string excludeAttribute)
{
// Arrange
var content = "content";
var context = MakeTagHelperContext(
attributes: new TagHelperAttributeList {
{ "names", namesAttribute },
{ "include", includeAttribute },
{ "exclude", excludeAttribute },
});
var output = MakeTagHelperOutput("environment", childContent: content);
var hostingEnvironment = new Mock<IHostingEnvironment>();
hostingEnvironment.SetupProperty(h => h.EnvironmentName, "Development");
// Act
var helper = new EnvironmentTagHelper(hostingEnvironment.Object)
{
Names = namesAttribute,
Include = includeAttribute,
Exclude = excludeAttribute,
};
helper.Process(context, output);
// Assert
Assert.Null(output.TagName);
Assert.Empty(output.PreContent.GetContent());
Assert.True(output.Content.GetContent().Length == 0);
Assert.Empty(output.PostContent.GetContent());
Assert.True(output.IsContentModified);
}
[Theory] [Theory]
[InlineData("NotDevelopment", "Development")] [InlineData("NotDevelopment", "Development")]
[InlineData("NOTDEVELOPMENT", "Development")] [InlineData("NOTDEVELOPMENT", "Development")]
@ -82,8 +170,7 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers.Test
var context = MakeTagHelperContext(attributes: new TagHelperAttributeList { { "names", namesAttribute } }); var context = MakeTagHelperContext(attributes: new TagHelperAttributeList { { "names", namesAttribute } });
var output = MakeTagHelperOutput("environment", childContent: content); var output = MakeTagHelperOutput("environment", childContent: content);
var hostingEnvironment = new Mock<IHostingEnvironment>(); var hostingEnvironment = new Mock<IHostingEnvironment>();
hostingEnvironment.SetupProperty(h => h.EnvironmentName); hostingEnvironment.SetupProperty(h => h.EnvironmentName, environmentName);
hostingEnvironment.Object.EnvironmentName = environmentName;
// Act // Act
var helper = new EnvironmentTagHelper(hostingEnvironment.Object) var helper = new EnvironmentTagHelper(hostingEnvironment.Object)
@ -108,8 +195,7 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers.Test
attributes: new TagHelperAttributeList { { "names", namesAttribute } }); attributes: new TagHelperAttributeList { { "names", namesAttribute } });
var output = MakeTagHelperOutput("environment", childContent: content); var output = MakeTagHelperOutput("environment", childContent: content);
var hostingEnvironment = new Mock<IHostingEnvironment>(); var hostingEnvironment = new Mock<IHostingEnvironment>();
hostingEnvironment.SetupProperty(h => h.EnvironmentName); hostingEnvironment.SetupProperty(h => h.EnvironmentName, environmentName);
hostingEnvironment.Object.EnvironmentName = environmentName;
// Act // Act
var helper = new EnvironmentTagHelper(hostingEnvironment.Object) var helper = new EnvironmentTagHelper(hostingEnvironment.Object)