Skip to content
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

Clean up and rationalize imports in the compiler #11409

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
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
@@ -1,10 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Xunit;

namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X;
Expand All @@ -15,21 +13,21 @@ public class MvcImportProjectFeatureTest
public void AddDefaultDirectivesImport_AddsSingleDynamicImport()
{
// Arrange
var imports = new List<RazorProjectItem>();
using var imports = new PooledArrayBuilder<RazorProjectItem>();

// Act
MvcImportProjectFeature.AddDefaultDirectivesImport(imports);
MvcImportProjectFeature.AddDefaultDirectivesImport(ref imports.AsRef());

// Assert
var import = Assert.Single(imports);
var import = Assert.Single(imports.ToImmutable());
Assert.Null(import.FilePath);
}

[Fact]
public void AddHierarchicalImports_AddsViewImportSourceDocumentsOnDisk()
{
// Arrange
var imports = new List<RazorProjectItem>();
using var imports = new PooledArrayBuilder<RazorProjectItem>();
var projectItem = new TestRazorProjectItem("/Contact/Index.cshtml");
var testFileSystem = new TestRazorProjectFileSystem(new[]
{
Expand All @@ -44,10 +42,10 @@ public void AddHierarchicalImports_AddsViewImportSourceDocumentsOnDisk()
};

// Act
mvcImportFeature.AddHierarchicalImports(projectItem, imports);
mvcImportFeature.AddHierarchicalImports(projectItem, ref imports.AsRef());

// Assert
Assert.Collection(imports,
Assert.Collection(imports.ToImmutable(),
import => Assert.Equal("/_ViewImports.cshtml", import.FilePath),
import => Assert.Equal("/Contact/_ViewImports.cshtml", import.FilePath));
}
Expand All @@ -56,7 +54,7 @@ public void AddHierarchicalImports_AddsViewImportSourceDocumentsOnDisk()
public void AddHierarchicalImports_AddsViewImportSourceDocumentsNotOnDisk()
{
// Arrange
var imports = new List<RazorProjectItem>();
using var imports = new PooledArrayBuilder<RazorProjectItem>();
var projectItem = new TestRazorProjectItem("/Pages/Contact/Index.cshtml");
var testFileSystem = new TestRazorProjectFileSystem(new[] { projectItem });
var mvcImportFeature = new MvcImportProjectFeature()
Expand All @@ -65,10 +63,10 @@ public void AddHierarchicalImports_AddsViewImportSourceDocumentsNotOnDisk()
};

// Act
mvcImportFeature.AddHierarchicalImports(projectItem, imports);
mvcImportFeature.AddHierarchicalImports(projectItem, ref imports.AsRef());

// Assert
Assert.Collection(imports,
Assert.Collection(imports.ToImmutable(),
import => Assert.Equal("/_ViewImports.cshtml", import.FilePath),
import => Assert.Equal("/Pages/_ViewImports.cshtml", import.FilePath),
import => Assert.Equal("/Pages/Contact/_ViewImports.cshtml", import.FilePath));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Xunit;

namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X;
Expand All @@ -15,21 +13,21 @@ public class MvcImportProjectFeatureTest
public void AddDefaultDirectivesImport_AddsSingleDynamicImport()
{
// Arrange
var imports = new List<RazorProjectItem>();
using var imports = new PooledArrayBuilder<RazorProjectItem>();

// Act
MvcImportProjectFeature.AddDefaultDirectivesImport(imports);
MvcImportProjectFeature.AddDefaultDirectivesImport(ref imports.AsRef());

// Assert
var import = Assert.Single(imports);
var import = Assert.Single(imports.ToImmutable());
Assert.Null(import.FilePath);
}

[Fact]
public void AddHierarchicalImports_AddsViewImportSourceDocumentsOnDisk()
{
// Arrange
var imports = new List<RazorProjectItem>();
using var imports = new PooledArrayBuilder<RazorProjectItem>();
var projectItem = new TestRazorProjectItem("/Contact/Index.cshtml");
var testFileSystem = new TestRazorProjectFileSystem(new[]
{
Expand All @@ -44,10 +42,10 @@ public void AddHierarchicalImports_AddsViewImportSourceDocumentsOnDisk()
};

// Act
mvcImportFeature.AddHierarchicalImports(projectItem, imports);
mvcImportFeature.AddHierarchicalImports(projectItem, ref imports.AsRef());

// Assert
Assert.Collection(imports,
Assert.Collection(imports.ToImmutable(),
import => Assert.Equal("/_ViewImports.cshtml", import.FilePath),
import => Assert.Equal("/Contact/_ViewImports.cshtml", import.FilePath));
}
Expand All @@ -56,7 +54,7 @@ public void AddHierarchicalImports_AddsViewImportSourceDocumentsOnDisk()
public void AddHierarchicalImports_AddsViewImportSourceDocumentsNotOnDisk()
{
// Arrange
var imports = new List<RazorProjectItem>();
using var imports = new PooledArrayBuilder<RazorProjectItem>();
var projectItem = new TestRazorProjectItem("/Pages/Contact/Index.cshtml");
var testFileSystem = new TestRazorProjectFileSystem(new[] { projectItem });
var mvcImportFeature = new MvcImportProjectFeature()
Expand All @@ -65,10 +63,10 @@ public void AddHierarchicalImports_AddsViewImportSourceDocumentsNotOnDisk()
};

// Act
mvcImportFeature.AddHierarchicalImports(projectItem, imports);
mvcImportFeature.AddHierarchicalImports(projectItem, ref imports.AsRef());

// Assert
Assert.Collection(imports,
Assert.Collection(imports.ToImmutable(),
import => Assert.Equal("/_ViewImports.cshtml", import.FilePath),
import => Assert.Equal("/Pages/_ViewImports.cshtml", import.FilePath),
import => Assert.Equal("/Pages/Contact/_ViewImports.cshtml", import.FilePath));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Xunit;

namespace Microsoft.AspNetCore.Mvc.Razor.Extensions;
Expand All @@ -15,21 +13,21 @@ public class MvcImportProjectFeatureTest
public void AddDefaultDirectivesImport_AddsSingleDynamicImport()
{
// Arrange
var imports = new List<RazorProjectItem>();
using var imports = new PooledArrayBuilder<RazorProjectItem>();

// Act
MvcImportProjectFeature.AddDefaultDirectivesImport(imports);
MvcImportProjectFeature.AddDefaultDirectivesImport(ref imports.AsRef());

// Assert
var import = Assert.Single(imports);
var import = Assert.Single(imports.ToImmutable());
Assert.Null(import.FilePath);
}

[Fact]
public void AddHierarchicalImports_AddsViewImportSourceDocumentsOnDisk()
{
// Arrange
var imports = new List<RazorProjectItem>();
using var imports = new PooledArrayBuilder<RazorProjectItem>();
var projectItem = new TestRazorProjectItem("/Contact/Index.cshtml");
var testFileSystem = new TestRazorProjectFileSystem(new[]
{
Expand All @@ -44,10 +42,10 @@ public void AddHierarchicalImports_AddsViewImportSourceDocumentsOnDisk()
};

// Act
mvcImportFeature.AddHierarchicalImports(projectItem, imports);
mvcImportFeature.AddHierarchicalImports(projectItem, ref imports.AsRef());

// Assert
Assert.Collection(imports,
Assert.Collection(imports.ToImmutable(),
import => Assert.Equal("/_ViewImports.cshtml", import.FilePath),
import => Assert.Equal("/Contact/_ViewImports.cshtml", import.FilePath));
}
Expand All @@ -56,7 +54,7 @@ public void AddHierarchicalImports_AddsViewImportSourceDocumentsOnDisk()
public void AddHierarchicalImports_AddsViewImportSourceDocumentsNotOnDisk()
{
// Arrange
var imports = new List<RazorProjectItem>();
using var imports = new PooledArrayBuilder<RazorProjectItem>();
var projectItem = new TestRazorProjectItem("/Pages/Contact/Index.cshtml");
var testFileSystem = new TestRazorProjectFileSystem(new[] { projectItem });
var mvcImportFeature = new MvcImportProjectFeature()
Expand All @@ -65,10 +63,10 @@ public void AddHierarchicalImports_AddsViewImportSourceDocumentsNotOnDisk()
};

// Act
mvcImportFeature.AddHierarchicalImports(projectItem, imports);
mvcImportFeature.AddHierarchicalImports(projectItem, ref imports.AsRef());

// Assert
Assert.Collection(imports,
Assert.Collection(imports.ToImmutable(),
import => Assert.Equal("/_ViewImports.cshtml", import.FilePath),
import => Assert.Equal("/Pages/_ViewImports.cshtml", import.FilePath),
import => Assert.Equal("/Pages/Contact/_ViewImports.cshtml", import.FilePath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,11 @@ public void Process_GetsImportsFromFeature()
var projectItem = new TestRazorProjectItem("Index.cshtml");

var testImport = new TestRazorProjectItem("testvalue");
var importFeature = new Mock<IImportProjectFeature>();
importFeature
.Setup(feature => feature.GetImports(It.IsAny<RazorProjectItem>()))
.Returns(new[] { testImport });
var importFeature = new TestImportProjectFeature(testImport);

var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, TestRazorProjectFileSystem.Empty, builder =>
{
builder.SetImportFeature(importFeature.Object);
builder.SetImportFeature(importFeature);
});

// Act
Expand All @@ -86,21 +83,15 @@ public void Process_GetsImportsFromFeature_MultipleFeatures()
var projectItem = new TestRazorProjectItem("Index.cshtml");

var testImport1 = new TestRazorProjectItem("testvalue1");
var importFeature1 = new Mock<IImportProjectFeature>();
importFeature1
.Setup(feature => feature.GetImports(It.IsAny<RazorProjectItem>()))
.Returns(new[] { testImport1 });
var importFeature1 = new TestImportProjectFeature(testImport1);

var testImport2 = new TestRazorProjectItem("testvalue2");
var importFeature2 = new Mock<IImportProjectFeature>();
importFeature2
.Setup(feature => feature.GetImports(It.IsAny<RazorProjectItem>()))
.Returns(new[] { testImport2 });
var importFeature2 = new TestImportProjectFeature(testImport2);

var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, TestRazorProjectFileSystem.Empty, builder =>
{
builder.Features.Add(importFeature1.Object);
builder.Features.Add(importFeature2.Object);
builder.Features.Add(importFeature1);
builder.Features.Add(importFeature2);
});

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ public void FindHierarchicalItems_FindsItemsWithMatchingNames()
items,
item =>
{
Assert.Equal("/Views/Home/_ViewImports.cshtml", item.FilePath);
Assert.Equal("/_ViewImports.cshtml", item.FilePath);
Assert.Equal("/", item.BasePath);
Assert.Equal(Path.Combine(TestFolder, "Views", "Home", "_ViewImports.cshtml"), item.PhysicalPath);
Assert.Equal(Path.Combine("Views", "Home", "_ViewImports.cshtml"), item.RelativePhysicalPath);
Assert.Equal(Path.Combine(TestFolder, "_ViewImports.cshtml"), item.PhysicalPath);
Assert.Equal("_ViewImports.cshtml", item.RelativePhysicalPath);
},
item =>
{
Expand All @@ -242,10 +242,10 @@ public void FindHierarchicalItems_FindsItemsWithMatchingNames()
},
item =>
{
Assert.Equal("/_ViewImports.cshtml", item.FilePath);
Assert.Equal("/Views/Home/_ViewImports.cshtml", item.FilePath);
Assert.Equal("/", item.BasePath);
Assert.Equal(Path.Combine(TestFolder, "_ViewImports.cshtml"), item.PhysicalPath);
Assert.Equal("_ViewImports.cshtml", item.RelativePhysicalPath);
Assert.Equal(Path.Combine(TestFolder, "Views", "Home", "_ViewImports.cshtml"), item.PhysicalPath);
Assert.Equal(Path.Combine("Views", "Home", "_ViewImports.cshtml"), item.RelativePhysicalPath);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Runtime.CompilerServices;
using Microsoft.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.AspNetCore.Razor.Test.Common;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -425,14 +426,14 @@ private void RunDesignTimeTagHelpersTest(IEnumerable<TagHelperDescriptor> descri

private static ImmutableArray<RazorSourceDocument> GetImports(RazorProjectEngine projectEngine, RazorProjectItem projectItem)
{
var importFeatures = projectEngine.GetFeatures<IImportProjectFeature>();
var importItems = importFeatures.SelectMany(f => f.GetImports(projectItem));
var importSourceDocuments = importItems
.Where(i => i.Exists)
.Select(RazorSourceDocument.ReadFrom)
.ToImmutableArray();

return importSourceDocuments;
using var result = new PooledArrayBuilder<RazorSourceDocument>();

foreach (var import in projectEngine.GetImports(projectItem, static i => i.Exists))
{
result.Add(RazorSourceDocument.ReadFrom(import));
}

return result.ToImmutable();
}

private void AddTagHelperStubs(IEnumerable<TagHelperDescriptor> descriptors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public void SetImportFeature_SetsTheImportFeature()
{
// Arrange
var builder = new RazorProjectEngineBuilder(RazorConfiguration.Default, Mock.Of<RazorProjectFileSystem>());
var testFeature1 = Mock.Of<IImportProjectFeature>();
var testFeature2 = Mock.Of<IImportProjectFeature>();
var testFeature1 = new TestImportProjectFeature();
var testFeature2 = new TestImportProjectFeature();
builder.Features.Add(testFeature1);
builder.Features.Add(testFeature2);
var newFeature = Mock.Of<IImportProjectFeature>();
var newFeature = new TestImportProjectFeature();

// Act
builder.SetImportFeature(newFeature);
Expand Down
Loading