Skip to content

(#387) Added support for Central Package Mangagement #388

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

Open
wants to merge 1 commit into
base: develop
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
5 changes: 5 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Generators/Generators.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
1 change: 1 addition & 0 deletions src/Guidelines/build/CakeInternalReferences.targets
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ProjectType="$(CakeContribGuidelinesProjectType)"
CakeVersion="$(CakeContribGuidelinesOverrideTargetFrameworkCakeVersion)"
References="@(PackageReference)"
PackageVersions="@(PackageVersion)"
NoWarn="$(NoWarn)"
WarningsAsErrors="$(WarningsAsErrors)"
/>
Expand Down
1 change: 1 addition & 0 deletions src/Guidelines/build/RecommendedCakeVersion.targets
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
ProjectFile="$(MSBuildProjectFullPath)"
RecommendedVersion="$(RecommendedCakeVersion)"
References="@(PackageReference)"
PackageVersions="@(PackageVersion)"
Omitted="@(CakeContribGuidelinesOmitRecommendedCakeVersion)"
ReferencesToCheck="@(CakeReferenceToCheck)"
ProjectType="$(CakeContribGuidelinesProjectType)"
Expand Down
1 change: 1 addition & 0 deletions src/Guidelines/build/TargetFrameworkVersions.targets
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<TargetFrameworkVersions
ProjectFile="$(MSBuildProjectFullPath)"
References="@(PackageReference)"
PackageVersions="@(PackageVersion)"
TargetFramework="$(TargetFramework)"
TargetFrameworks="$(TargetFrameworks)"
Omitted="@(CakeContribGuidelinesOmitTargetFramework)"
Expand Down
68 changes: 37 additions & 31 deletions src/Tasks.IntegrationTests/E2eTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Xunit;
using Xunit.Abstractions;

namespace CakeContrib.Guidelines.Tasks.IntegrationTests

Check warning on line 12 in src/Tasks.IntegrationTests/E2eTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Correct the spelling of 'Contrib' in namespace name 'CakeContrib' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1704)
{
// TODO: Writing things to disk is not deterministic...
// TODO: Running Code-Coverage on the integration-tests breaks all tests
Expand Down Expand Up @@ -121,7 +121,7 @@
{
// given
fixture.WithoutDefaultCakeReference();
fixture.WithPackageReference("Cake.Core", "0.38.5", "all");
fixture.WithPackageReference("Cake.Core", fixture.DefaultCakeVersion, "all");

// when
var result = fixture.Run();
Expand All @@ -136,7 +136,7 @@
{
// given
fixture.WithoutDefaultCakeReference();
fixture.WithPackageReference("Cake.Core", "0.38.5");
fixture.WithPackageReference("Cake.Core", fixture.DefaultCakeVersion);

// when
var result = fixture.Run();
Expand Down Expand Up @@ -221,22 +221,27 @@
// then
result.IsErrorExitCode.ShouldBeTrue();
result.ErrorLines.ShouldContain(l => l.IndexOf("CCG0007", StringComparison.Ordinal) > -1);
result.ErrorLines.ShouldContain(l => l.IndexOf("netstandard2.0", StringComparison.Ordinal) > -1);
result.ErrorLines.ShouldContain(l => l.IndexOf(fixture.DefaultTargetFrameworkForModules, StringComparison.Ordinal) > -1);
}

[Fact]
public void Missing_Suggested_Target_results_in_CCG0007_warning()
{
var missingTfm = fixture.DefaultTargetFrameworksForAddins
.Split(";", StringSplitOptions.RemoveEmptyEntries)

Check failure on line 231 in src/Tasks.IntegrationTests/E2eTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Argument 1: cannot convert from 'string' to 'char'

Check failure on line 231 in src/Tasks.IntegrationTests/E2eTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Argument 2: cannot convert from 'System.StringSplitOptions' to 'char'

Check failure on line 231 in src/Tasks.IntegrationTests/E2eTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Argument 1: cannot convert from 'string' to 'char'

Check failure on line 231 in src/Tasks.IntegrationTests/E2eTests.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Argument 2: cannot convert from 'System.StringSplitOptions' to 'char'
.First();
var allTfmButMissing = fixture.DefaultTargetFrameworksForAddins.Replace(missingTfm, string.Empty);

// given
fixture.WithTargetFrameworks("netstandard2.0");
fixture.WithTargetFrameworks(allTfmButMissing);

// when
var result = fixture.Run();

// then
result.IsErrorExitCode.ShouldBeFalse();
result.WarningLines.ShouldContain(l => l.IndexOf("CCG0007", StringComparison.Ordinal) > -1);
result.WarningLines.ShouldContain(l => l.IndexOf("net461", StringComparison.Ordinal) > -1);
result.IsErrorExitCode.ShouldBeTrue();
result.ErrorLines.ShouldContain(l => l.IndexOf("CCG0007", StringComparison.Ordinal) > -1);
result.ErrorLines.ShouldContain(l => l.IndexOf(missingTfm, StringComparison.Ordinal) > -1);
}

[Fact]
Expand All @@ -246,6 +251,7 @@
fixture.WithoutDefaultCakeReference();
fixture.WithPackageReference("Cake.Core", "1.0.0", "all");
fixture.WithTargetFrameworks("netstandard2.0;net461");
fixture.OmitRecommendedCakeVersion();

// when
var result = fixture.Run();
Expand Down Expand Up @@ -351,7 +357,7 @@
{
// given
fixture.WithAssemblyName("Cake.Buildsystems.Module");
fixture.WithTargetFrameworks("netstandard2.0");
fixture.WithTargetFrameworks(fixture.DefaultTargetFrameworkForModules);
fixture.WithCustomContent(@"
<Target Name=""ForTest""
AfterTargets=""BeforeBuild""
Expand All @@ -376,7 +382,7 @@
public void ProjectType_When_PackageId_Is_Module_Is_Module()
{
// given
fixture.WithTargetFrameworks("netstandard2.0");
fixture.WithTargetFrameworks(fixture.DefaultTargetFrameworkForModules);
fixture.WithCustomContent(@"
<PropertyGroup>
<PackageId>Cake.Buildsystems.Module</PackageId>
Expand Down Expand Up @@ -498,7 +504,7 @@
fixture.WithAssemblyName(assemblyName);
if (isModule)
{
fixture.WithTargetFrameworks("netstandard2.0");
fixture.WithTargetFrameworks(fixture.DefaultTargetFrameworkForModules);
}
fixture.WithCustomContent(@"
<PropertyGroup>
Expand Down Expand Up @@ -537,7 +543,7 @@
fixture.WithAssemblyName(assemblyName);
if (isModule)
{
fixture.WithTargetFrameworks("netstandard2.0");
fixture.WithTargetFrameworks(fixture.DefaultTargetFrameworkForModules);
}
fixture.WithCustomContent(@"
<PropertyGroup>
Expand Down Expand Up @@ -589,7 +595,7 @@
<PackageId>Cake.Buildsystems.Module</PackageId>
</PropertyGroup>");
fixture.WithTags("cake build cake-build script");
fixture.WithTargetFrameworks("netstandard2.0");
fixture.WithTargetFrameworks(fixture.DefaultTargetFrameworkForModules);

// when
var result = fixture.Run();
Expand Down Expand Up @@ -651,6 +657,7 @@
// given
fixture.WithoutDefaultCakeReference();
fixture.WithPackageReference("cake.core", "0.38.5", "All");
fixture.WithTargetFrameworks("netstandard2.0;net461;net5.0");

// when
var result = fixture.Run();
Expand All @@ -667,6 +674,7 @@
// given
fixture.WithoutDefaultCakeReference();
fixture.WithPackageReference("cake.core", "0.38.5", "All");
fixture.WithTargetFrameworks("netstandard2.0;net461;net5.0");
fixture.WithCustomContent(@"
<ItemGroup>
<CakeContribGuidelinesOmitRecommendedCakeVersion Include=""Cake.Core"" />
Expand All @@ -680,29 +688,11 @@
result.WarningLines.ShouldBeEmpty();
}

[Fact]
public void Missing_Suggested_Target_results_not_in_CCG0007_warning_if_NoWarn_is_set()
{
// given
fixture.WithTargetFrameworks("netstandard2.0");
fixture.WithCustomContent(@"
<PropertyGroup>
<NoWarn>1701;1702;ccg0007</NoWarn>
</PropertyGroup>");

// when
var result = fixture.Run();

// then
result.IsErrorExitCode.ShouldBeFalse();
result.WarningLines.ShouldNotContain(l => l.IndexOf("CCG0007", StringComparison.Ordinal) > -1);
}

[Fact]
public void Missing_Suggested_Target_results_in_CCG0007_error_if_WarningsAsErrors_is_set()
{
// given
fixture.WithTargetFrameworks("netstandard2.0");
fixture.WithTargetFrameworks(fixture.DefaultTargetFrameworkForModules);
fixture.WithCustomContent(@"
<PropertyGroup>
<WarningsAsErrors>NU1605;ccg0007</WarningsAsErrors >
Expand Down Expand Up @@ -738,5 +728,21 @@
err.ShouldNotBeNull();
err.ShouldContain("netstandard2.0");
}

[Fact]
public void Central_Package_Management_Correct_Cake_Reference_Should_Not_Raise_CCG0009()
{
// given
fixture.WithoutDefaultCakeReference();
fixture.WithPackageReference("cake.core", privateAssets: "All");
fixture.WithCpmPackageVersion("Cake.Core", fixture.DefaultCakeVersion);

// when
var result = fixture.Run();

// then
result.IsErrorExitCode.ShouldBeFalse();
result.WarningLines.ShouldBeEmpty();
}
}
}
67 changes: 59 additions & 8 deletions src/Tasks.IntegrationTests/Fixtures/E2eTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
private bool hasEditorConfig = true;
private bool omitRecommendedCakeVersion = false;
private bool hasDefaultCakeReference = true;
private readonly List<string> customContent = new List<string>();
private string targetFrameworks = "netstandard2.0;net461;net5.0";
private readonly List<string> references = new List<string>();
private readonly List<string> customContent = new();
private string targetFrameworks = "net8.0;net9.0";
private readonly List<string> references = new();
private readonly Dictionary<string, string> cpmPackageVersions = new();
private string tags = "cake;cake-build;build;script;addin;cake-addin;module;cake-module;recipe;cake-recipe";

public string DefaultCakeVersion => "5.0.0";
public string DefaultTargetFrameworkForModules => "net8.0";
public string DefaultTargetFrameworksForAddins => "net8.0;net9.0";

public E2eTestFixture(string tempFolder, ITestOutputHelper logger)
{
this.tempFolder = tempFolder;
Expand Down Expand Up @@ -78,6 +83,7 @@
{
properties.Add($"<PackageTags>{tags}</PackageTags>");
}

if (hasStylecopJson)
{
var stylecopJson = Path.Combine(tempFolder, "stylecop.json");
Expand All @@ -95,16 +101,22 @@
}
if (hasStylecopReference)
{
items.Add(@"
<PackageReference Include=""StyleCop.Analyzers"" Version=""1.1.118"">
var version = "Version=\"1.1.118\"";
if (cpmPackageVersions.Any())
{
version = string.Empty;
}

items.Add($@"
<PackageReference Include=""StyleCop.Analyzers"" {version}>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>");
}

if (hasDefaultCakeReference)
{
WithPackageReference("cake.core","1.0.0", "all");
WithPackageReference("cake.core", DefaultCakeVersion, "all");
}

if (omitRecommendedCakeVersion)
Expand All @@ -116,6 +128,32 @@
</ItemGroup>");
}

if (cpmPackageVersions.Any())
{
if (hasStylecopReference)
{
cpmPackageVersions.Add("StyleCop.Analyzers", "1.1.118");
}

var cpmFile = Path.Combine(tempFolder, "Directory.Packages.props");
File.WriteAllText(cpmFile,
$"""
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
{
string.Join(
Environment.NewLine,
cpmPackageVersions.Select(kvp => $"<PackageVersion Include=\"{kvp.Key}\" Version=\"{kvp.Value}\" />").ToArray()
)
}
</ItemGroup>
</Project>
""");
}

items.AddRange(references);

File.WriteAllText(csproj, string.Format(template,
Expand Down Expand Up @@ -157,12 +195,18 @@

internal void WithPackageReference(
string packageName,
string version,
string version = null,
string privateAssets = null,
params Tuple<string, string>[] additionalAttributes)
{
var reference = new StringBuilder();
reference.Append($@"<PackageReference Include=""{packageName}"" Version=""{version}""");
reference.Append($@"<PackageReference Include=""{packageName}""");

if (version != null)
{
reference.Append($@" Version=""{version}""");
}

if (privateAssets != null)
{
reference.Append($@" PrivateAssets=""{privateAssets}""");
Expand All @@ -177,6 +221,13 @@
references.Add(reference.ToString());
}

internal void WithCpmPackageVersion(
string packageName,
string version)
{
this.cpmPackageVersions.Add(packageName, version);
}

internal void WithoutStylecopReference()
{
hasStylecopReference = false;
Expand Down Expand Up @@ -204,7 +255,7 @@

private Tuple<string, string> GetTargetsToImport()
{
var codeBase = typeof(E2eTestFixture).Assembly.CodeBase;

Check warning on line 258 in src/Tasks.IntegrationTests/Fixtures/E2eTestFixture.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

'Assembly.CodeBase' is obsolete: 'Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility. Use Assembly.Location.' (https://aka.ms/dotnet-warnings/SYSLIB0012)
var assemblyPath = Uri.UnescapeDataString(new UriBuilder(codeBase).Path);
var folder = Path.GetDirectoryName(assemblyPath);

Expand Down
2 changes: 1 addition & 1 deletion src/Tasks.IntegrationTests/Tasks.IntegrationTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;</TargetFrameworks>
<TargetFrameworks>net8.0;net9.0;</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)'!='Unix'">$(TargetFrameworks);net472</TargetFrameworks>
<AssemblyName>CakeContrib.Guidelines.Tasks.IntegrationTests</AssemblyName>
<RootNamespace>CakeContrib.Guidelines.Tasks.IntegrationTests</RootNamespace>
Expand Down
Loading
Loading