-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add support for reading the extern alias #11451
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
dsplaisted
merged 7 commits into
dotnet:master
from
nkolev92:master-nkolev92-externAliases
May 29, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bda0b16
Add support for reading the extern alias
nkolev92 7acbdcd
weird
nkolev92 2ae201f
use net5.0
nkolev92 fc2159a
work
nkolev92 f6e2517
dianostic assert
nkolev92 d95c0f1
fix tests
nkolev92 e82c23f
use msbuild version fact
nkolev92 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
249 changes: 249 additions & 0 deletions
249
src/Tests/Microsoft.NET.Build.Tests/GivenThatWeHaveAPackageReferenceWithAliases.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,249 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using FluentAssertions; | ||
using Microsoft.NET.TestFramework; | ||
using Microsoft.NET.TestFramework.Assertions; | ||
using Microsoft.NET.TestFramework.Commands; | ||
using Microsoft.NET.TestFramework.ProjectConstruction; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.NET.Build.Tests | ||
{ | ||
public class GivenThatWeHaveAPackageReferenceWithAliases : SdkTest | ||
{ | ||
|
||
public GivenThatWeHaveAPackageReferenceWithAliases(ITestOutputHelper log) : base(log) | ||
{ } | ||
|
||
[RequiresMSBuildVersionFact("16.7.0")] | ||
public void CanBuildProjectWithPackageReferencesWithConflictingTypes() | ||
{ | ||
var targetFramework = "netcoreapp5.0"; | ||
var packageReferences = GetPackageReferencesWithConflictingTypes(targetFramework, "A", "B"); | ||
|
||
TestProject testProject = new TestProject() | ||
{ | ||
Name = "Project", | ||
IsSdkProject = true, | ||
IsExe = false, | ||
TargetFrameworks = targetFramework, | ||
}; | ||
|
||
testProject.PackageReferences.Add(packageReferences.First()); | ||
testProject.PackageReferences.Add( | ||
new TestPackageReference( | ||
packageReferences.Last().ID, | ||
packageReferences.Last().Version, | ||
packageReferences.Last().NupkgPath, | ||
packageReferences.Last().PrivateAssets, | ||
aliases: "Special")); | ||
var packagesPaths = packageReferences.Select(e => Path.GetDirectoryName(e.NupkgPath)); | ||
|
||
testProject.AdditionalProperties.Add("RestoreSources", | ||
"$(RestoreSources);" + string.Join(";", packagesPaths)); | ||
|
||
// Use a test-specific packages folder | ||
testProject.AdditionalProperties["RestorePackagesPath"] = @"$(MSBuildProjectDirectory)\..\pkg"; | ||
nkolev92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
testProject.SourceFiles[$"{testProject.Name}.cs"] = ConflictingClassLibUsage; | ||
var testAsset = _testAssetsManager.CreateTestProject(testProject); | ||
|
||
var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); | ||
buildCommand.Execute() | ||
.Should() | ||
.Pass(); | ||
} | ||
|
||
[RequiresMSBuildVersionFact("16.7.0")] | ||
public void CanBuildProjectWithMultiplePackageReferencesWithAliases() | ||
{ | ||
var targetFramework = "netcoreapp5.0"; | ||
|
||
var packageReferenceA = GetPackageReference(targetFramework, "A", ClassLibClassA); | ||
var packageReferenceB = GetPackageReference(targetFramework, "B", ClassLibClassB); | ||
|
||
TestProject testProject = new TestProject() | ||
{ | ||
Name = "Project", | ||
IsSdkProject = true, | ||
IsExe = false, | ||
TargetFrameworks = targetFramework, | ||
}; | ||
|
||
testProject.PackageReferences.Add( | ||
new TestPackageReference( | ||
packageReferenceA.ID, | ||
packageReferenceA.Version, | ||
packageReferenceA.NupkgPath, | ||
packageReferenceA.PrivateAssets, | ||
aliases: "First")); | ||
testProject.PackageReferences.Add( | ||
new TestPackageReference( | ||
packageReferenceB.ID, | ||
packageReferenceB.Version, | ||
packageReferenceB.NupkgPath, | ||
packageReferenceB.PrivateAssets, | ||
aliases: "Second")); | ||
|
||
testProject.AdditionalProperties.Add("RestoreSources", | ||
"$(RestoreSources);" + Path.GetDirectoryName(packageReferenceA.NupkgPath) + ";" + Path.GetDirectoryName(packageReferenceB.NupkgPath)); | ||
|
||
// Use a test-specific packages folder | ||
testProject.AdditionalProperties["RestorePackagesPath"] = @"$(MSBuildProjectDirectory)\..\pkg"; | ||
testProject.SourceFiles[$"{testProject.Name}.cs"] = ClassLibAandBUsage; | ||
var testAsset = _testAssetsManager.CreateTestProject(testProject); | ||
|
||
var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); | ||
buildCommand.Execute() | ||
.Should() | ||
.Pass(); | ||
} | ||
|
||
[RequiresMSBuildVersionFact("16.7.0")] | ||
public void CanBuildProjectWithAPackageReferenceWithMultipleAliases() | ||
{ | ||
var targetFramework = "netcoreapp5.0"; | ||
|
||
var packageReferenceA = GetPackageReference(targetFramework, "A", ClassLibMultipleClasses); | ||
|
||
TestProject testProject = new TestProject() | ||
{ | ||
Name = "Project", | ||
IsSdkProject = true, | ||
IsExe = false, | ||
TargetFrameworks = targetFramework, | ||
}; | ||
|
||
testProject.PackageReferences.Add( | ||
new TestPackageReference( | ||
packageReferenceA.ID, | ||
packageReferenceA.Version, | ||
packageReferenceA.NupkgPath, | ||
packageReferenceA.PrivateAssets, | ||
aliases: "First,Second")); | ||
|
||
testProject.AdditionalProperties.Add("RestoreSources", | ||
"$(RestoreSources);" + Path.GetDirectoryName(packageReferenceA.NupkgPath)); | ||
|
||
// Use a test-specific packages folder | ||
testProject.AdditionalProperties["RestorePackagesPath"] = @"$(MSBuildProjectDirectory)\..\pkg"; | ||
testProject.SourceFiles[$"{testProject.Name}.cs"] = ClassLibAandBUsage; | ||
var testAsset = _testAssetsManager.CreateTestProject(testProject); | ||
|
||
var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); | ||
buildCommand.Execute() | ||
.Should() | ||
.Pass(); | ||
} | ||
|
||
private IEnumerable<TestPackageReference> GetPackageReferencesWithConflictingTypes(string targetFramework, params string[] packageNames) | ||
{ | ||
foreach (var packageName in packageNames) | ||
{ | ||
yield return GetPackageReference(targetFramework, packageName, ClassLibConflictingMethod); | ||
} | ||
} | ||
|
||
private TestPackageReference GetPackageReference(string targetFramework, string packageName, string projectFileContent) | ||
{ | ||
var project = GetProject(targetFramework, packageName, projectFileContent); | ||
var packCommand = new PackCommand(Log, _testAssetsManager.CreateTestProject(project).TestRoot, packageName); | ||
|
||
packCommand | ||
.Execute() | ||
.Should() | ||
.Pass(); | ||
return new TestPackageReference(packageName, "1.0.0", packCommand.GetNuGetPackage(packageName)); | ||
} | ||
|
||
private static TestProject GetProject(string targetFramework, string referenceProjectName, string projectFileContent) | ||
{ | ||
var project = new TestProject() | ||
{ | ||
Name = referenceProjectName, | ||
TargetFrameworks = targetFramework, | ||
IsSdkProject = true | ||
}; | ||
project.SourceFiles[$"{referenceProjectName}.cs"] = projectFileContent; | ||
return project; | ||
} | ||
|
||
private static string ClassLibConflictingMethod = @" | ||
using System; | ||
public class ClassLib | ||
{ | ||
public void ConflictingMethod() | ||
{ | ||
} | ||
} | ||
"; | ||
|
||
private static string ClassLibClassA = @" | ||
using System; | ||
public class A | ||
{ | ||
public void AMethod() | ||
{ | ||
} | ||
} | ||
"; | ||
|
||
private static string ClassLibMultipleClasses = @" | ||
using System; | ||
public class A | ||
{ | ||
public void AMethod() | ||
{ | ||
} | ||
} | ||
|
||
public class B | ||
{ | ||
public void BMethod() | ||
{ | ||
} | ||
} | ||
"; | ||
|
||
private static string ClassLibClassB = @" | ||
using System; | ||
public class B | ||
{ | ||
public void BMethod() | ||
{ | ||
} | ||
} | ||
"; | ||
|
||
private static string ClassLibAandBUsage = @" | ||
extern alias First; | ||
extern alias Second; | ||
using System; | ||
public class ClassLibUsage | ||
{ | ||
public void UsageMethod() | ||
{ | ||
new First.A().AMethod(); | ||
new Second.B().BMethod(); | ||
} | ||
} | ||
"; | ||
|
||
private static string ConflictingClassLibUsage = @" | ||
extern alias Special; | ||
using System; | ||
public class ClassLibUsage | ||
{ | ||
public void UsageMethod() | ||
{ | ||
new Special.ClassLib().ConflictingMethod(); | ||
nkolev92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
"; | ||
} | ||
} |
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
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.