Skip to content

Commit c3586cc

Browse files
authored
Added NuGet source mapping for unit tests. (#35)
* Ensures that unit tests are using ONLY the locally built packages. - This wasn't a real issue until the first pre-release was published to public NuGet feeds Co-authored-by: smaillet <25911635+smaillet@users.noreply.github.com>
1 parent 63e8d08 commit c3586cc

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

src/Ubiquity.Versioning.Build.Tasks.UT/ProjectCreatorLibraryExtensions.cs

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using System;
88
using System.Collections.Generic;
99
using System.Diagnostics.CodeAnalysis;
10+
using System.Linq;
11+
using System.Xml.Linq;
1012

1113
using Microsoft.Build.Evaluation;
1214
using Microsoft.Build.Utilities.ProjectCreation;
@@ -15,6 +17,17 @@ namespace Ubiquity.Versioning.Build.Tasks.UT
1517
{
1618
internal static class ProjectCreatorLibraryExtensions
1719
{
20+
public static PackageRepository SourceMapping( this PackageRepository pkgRepo, string pkgSourceKey, string pattern )
21+
{
22+
var nugetConfig = XDocument.Load(pkgRepo.NuGetConfigPath);
23+
XElement configuration = GetOrCreateConfigurationElement( nugetConfig );
24+
XElement sourceMapping = GetOrCreateSourceMappingElement( configuration );
25+
XElement pkgSrcElement = GetOrCreatePackageSource( sourceMapping, pkgSourceKey );
26+
_ = GetOrCreatePackageElement(pkgSrcElement, pattern);
27+
nugetConfig.Save(pkgRepo.NuGetConfigPath);
28+
return pkgRepo;
29+
}
30+
1831
[SuppressMessage( "Style", "IDE0060:Remove unused parameter", Justification = "Syntactical sugar" )]
1932
[SuppressMessage( "Style", "IDE0046:Convert to conditional expression", Justification = "Result is Less than 'simplified'" )]
2033
public static ProjectCreator VersioningProject(
@@ -30,9 +43,9 @@ public static ProjectCreator VersioningProject(
3043
string? treatAsLocalProperty = null,
3144
ProjectCollection? projectCollection = null,
3245
IDictionary<string, string>? globalProperties = null,
33-
NewProjectFileOptions? projectFileOptions = NewProjectFileOptions.None)
46+
NewProjectFileOptions? projectFileOptions = NewProjectFileOptions.None )
3447
{
35-
ArgumentException.ThrowIfNullOrWhiteSpace(targetFramework);
48+
ArgumentException.ThrowIfNullOrWhiteSpace( targetFramework );
3649

3750
return templates.SdkCsproj(
3851
path,
@@ -46,13 +59,71 @@ public static ProjectCreator VersioningProject(
4659
projectCollection,
4760
projectFileOptions,
4861
globalProperties
49-
).ItemPackageReference(PackageUnderTestId, version: packageVersion, privateAssets: "All")
50-
.Property("Nullable", "disable")
51-
.Property("ManagePackageVersionsCentrally", "false")
52-
.Property("ImplicitUsings","disable")
62+
).ItemPackageReference( PackageUnderTestId, version: packageVersion, privateAssets: "All" )
63+
.Property( "Nullable", "disable" )
64+
.Property( "ManagePackageVersionsCentrally", "false" )
65+
.Property( "ImplicitUsings", "disable" )
5366
;
5467
}
5568

69+
private static XElement GetOrCreateSourceMappingElement( XElement configuration )
70+
{
71+
XElement? sourceMapping = configuration.Element("packageSourceMapping");
72+
if(sourceMapping is null)
73+
{
74+
sourceMapping = new XElement( "packageSourceMapping" );
75+
configuration.Add( sourceMapping );
76+
}
77+
78+
return sourceMapping;
79+
}
80+
81+
private static XElement GetOrCreateConfigurationElement( XDocument nugetConfig )
82+
{
83+
XElement? configuration = nugetConfig.Element("configuration");
84+
if(configuration is null)
85+
{
86+
configuration = new XElement( "configuration" );
87+
nugetConfig.Add( configuration );
88+
}
89+
90+
return configuration;
91+
}
92+
93+
private static XElement GetOrCreatePackageSource( XElement sourceMapping, string pkgSource )
94+
{
95+
XElement? packageSource = ( from e in sourceMapping.Elements("packageSource")
96+
from a in e.Attributes()
97+
where a.Name == "key" && a.Value == pkgSource
98+
select e
99+
).FirstOrDefault();
100+
101+
if(packageSource is null)
102+
{
103+
packageSource = new XElement( "packageSource", new XAttribute( "key", pkgSource ) );
104+
sourceMapping.Add( packageSource );
105+
}
106+
107+
return packageSource;
108+
}
109+
110+
private static XElement GetOrCreatePackageElement( XElement pkgSrc, string pattern )
111+
{
112+
XElement? package = ( from e in pkgSrc.Elements("package")
113+
from a in e.Attributes()
114+
where a.Name == "pattern" && a.Value == pattern
115+
select e
116+
).FirstOrDefault();
117+
118+
if(package is null)
119+
{
120+
package = new XElement( "package", new XAttribute( "pattern", pattern) );
121+
pkgSrc.Add( package );
122+
}
123+
124+
return package;
125+
}
126+
56127
private const string PackageUnderTestId = @"Ubiquity.NET.Versioning.Build.Tasks";
57128
}
58129
}

src/Ubiquity.Versioning.Build.Tasks.UT/TestModuleFixtures.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public static void AssemblyInitialize( TestContext ctx )
6363
ctx.TestRunDirectory, // '.nuget/packages' repo folder goes here
6464
new Uri(Path.Combine(BuildOutputPath, "NuGet")), // Local feed (Contains location of the build of the package under test)
6565
new Uri("https://api.nuget.org/v3/index.json") // standard NuGet Feed
66-
);
66+
).SourceMapping("Local1", "Ubiquity.NET.Versioning*") // force all fetches of built package to locally built source only
67+
.SourceMapping("api.nuget.org", "*"); // Everything else goes to NuGet public feed.
6768
}
6869

6970
[AssemblyCleanup]

0 commit comments

Comments
 (0)