-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suports discovery of static web assets from referenced projects (#605)
* Define static web assets in the current project * These are assets under the wwwroot folder. * By convention, the base path for these assets is `_content/<<PackageId>>` with spaces and dots removed and all characters lower-cased. * Retrieve static web assets from referenced projects
- Loading branch information
Showing
9 changed files
with
257 additions
and
27 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/Razor/src/Microsoft.NET.Sdk.Razor/GetDefaultStaticWebAssetsBasePath.cs
This file contains 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,40 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Tasks | ||
{ | ||
public class GetDefaultStaticWebAssetsBasePath : Task | ||
{ | ||
[Required] | ||
public string BasePath { get; set; } | ||
|
||
[Output] | ||
public string SafeBasePath { get; set; } | ||
|
||
public override bool Execute() | ||
{ | ||
if (string.IsNullOrWhiteSpace(BasePath)) | ||
{ | ||
Log.LogError($"Base path '{BasePath ?? "(null)"}' must contain non-whitespace characters."); | ||
return !Log.HasLoggedErrors; | ||
} | ||
|
||
var safeBasePath = BasePath | ||
.Replace(" ", "") | ||
.Replace(".", "") | ||
.ToLowerInvariant(); | ||
|
||
if (safeBasePath == "") | ||
{ | ||
Log.LogError($"Base path '{BasePath}' must contain non '.' characters."); | ||
return !Log.HasLoggedErrors; | ||
} | ||
SafeBasePath = safeBasePath; | ||
|
||
return !Log.HasLoggedErrors; | ||
} | ||
} | ||
} |
This file contains 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
95 changes: 95 additions & 0 deletions
95
src/Razor/test/Microsoft.NET.Sdk.Razor.Test/GetDefaultStaticWebAssetsBasePathTest.cs
This file contains 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,95 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.Build.Framework; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Tasks | ||
{ | ||
public class GetDefaultStaticWebAssetsBasePathTest | ||
{ | ||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
[InlineData(" ")] | ||
[InlineData(" ")] | ||
public void ReturnsError_WhenBasePath_DoesNotContainNonWhitespaceCharacters(string basePath) | ||
{ | ||
// Arrange | ||
var expectedError = $"Base path '{basePath ?? "(null)"}' must contain non-whitespace characters."; | ||
|
||
var errorMessages = new List<string>(); | ||
var buildEngine = new Mock<IBuildEngine>(); | ||
buildEngine.Setup(e => e.LogErrorEvent(It.IsAny<BuildErrorEventArgs>())) | ||
.Callback<BuildErrorEventArgs>(args => errorMessages.Add(args.Message)); | ||
|
||
var task = new GetDefaultStaticWebAssetsBasePath | ||
{ | ||
BuildEngine = buildEngine.Object, | ||
BasePath = basePath | ||
}; | ||
|
||
// Act | ||
var result = task.Execute(); | ||
|
||
// Assert | ||
Assert.False(result); | ||
var message = Assert.Single(errorMessages); | ||
Assert.Equal(expectedError, message); | ||
} | ||
|
||
[Theory] | ||
[InlineData(".")] | ||
[InlineData("..")] | ||
[InlineData(". ")] | ||
[InlineData(" .")] | ||
[InlineData(" . ")] | ||
[InlineData(". .")] | ||
public void ReturnsError_WhenSafeBasePath_MapsToTheEmptyString(string basePath) | ||
{ | ||
// Arrange | ||
var expectedError = $"Base path '{basePath}' must contain non '.' characters."; | ||
|
||
var errorMessages = new List<string>(); | ||
var buildEngine = new Mock<IBuildEngine>(); | ||
buildEngine.Setup(e => e.LogErrorEvent(It.IsAny<BuildErrorEventArgs>())) | ||
.Callback<BuildErrorEventArgs>(args => errorMessages.Add(args.Message)); | ||
|
||
var task = new GetDefaultStaticWebAssetsBasePath | ||
{ | ||
BuildEngine = buildEngine.Object, | ||
BasePath = basePath | ||
}; | ||
|
||
// Act | ||
var result = task.Execute(); | ||
|
||
// Assert | ||
Assert.False(result); | ||
var message = Assert.Single(errorMessages); | ||
Assert.Equal(expectedError, message); | ||
} | ||
|
||
[Theory] | ||
[InlineData("Identity", "identity")] | ||
[InlineData("Microsoft.AspNetCore.Identity", "microsoftaspnetcoreidentity")] | ||
public void ReturnsSafeBasePath_WhenBasePath_ContainsUnsafeCharacters(string basePath, string expectedSafeBasePath) | ||
{ | ||
// Arrange | ||
var task = new GetDefaultStaticWebAssetsBasePath | ||
{ | ||
BuildEngine = Mock.Of<IBuildEngine>(), | ||
BasePath = basePath | ||
}; | ||
|
||
// Act | ||
var result = task.Execute(); | ||
|
||
// Assert | ||
Assert.True(result); | ||
Assert.Equal(expectedSafeBasePath, task.SafeBasePath); | ||
} | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.