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

Issue #628: Add support for packages.config NuGet manifests #631

Open
wants to merge 3 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
8 changes: 7 additions & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ checks:
method-lines:
config:
threshold: 30

engines:
duplication:
enabled: true
config:
languages:
csharp:
mass_threshold: 70 # default is 60
exclude_patterns:
- "**/obj/"
- "Corgibytes.Freshli.Lib.Test/"
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
<Folder Include="fixtures\php\small" />
</ItemGroup>

<ItemGroup>
<Content Include="fixtures\csharp\csproj\Project.csproj">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<Target Name="UnzipArchive" AfterTargets="Build">
<Exec Condition="'$(OS)' != 'Windows_NT'" Command="unzip -o nokotest.zip" WorkingDirectory="$(OutputPath)/fixtures/ruby" />
<Exec Condition="'$(OS)' == 'Windows_NT'" Command="PowerShell -NonInteractive -Command &quot;&amp;{Expand-Archive -Force -Path nokotest.zip -DestinationPath .}&quot;" WorkingDirectory="$(OutputPath)/fixtures/ruby" />
Expand Down
51 changes: 51 additions & 0 deletions Corgibytes.Freshli.Lib.Test/Integration/ManifestServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using Xunit;

Expand Down Expand Up @@ -88,5 +89,55 @@ public void PerlCpanfile()

Assert.Equal("cpanfile", finders.First().GetManifestFilenames(fixturePath).First());
}

[Fact]
public void CSharpProjectFile()
{
string fixturePath = Fixtures.Path(
"csharp",
"csproj"
);
AssertManifest(fixturePath, "Project.csproj");
}

[Fact]
public void CSharpPackagesFile()
{
string fixturePath = Fixtures.Path(
"csharp",
"config"
);
AssertManifest(fixturePath, "packages.config");
}

[Fact]
public void CSharpMultipleFiles()
{
string fixturePath = Fixtures.Path(
"csharp"
);
var historyService = new FileHistoryService(FileHistoryFinderRegistry);
var fileFinder = historyService.SelectFinderFor(fixturePath);
var manifestService = new ManifestService();

var finders = manifestService.SelectFindersFor(fixturePath, fileFinder);
List<string> manifestFilenames = finders.SelectMany(finder => finder.GetManifestFilenames(fixturePath)).ToList();

// When https://github.com/corgibytes/freshli-lib/issues/630 is resolved this assertion
// should be Assert.Equal(8, manifestFilenames.Count);
Assert.InRange(manifestFilenames.Count, 2, 8);
Assert.Contains("Project.csproj", manifestFilenames);
Assert.Contains("packages.config", manifestFilenames);
}

private void AssertManifest(string fixturePath, string expectedManifest)
{
var historyService = new FileHistoryService(FileHistoryFinderRegistry);
var fileFinder = historyService.SelectFinderFor(fixturePath);
var manifestService = new ManifestService();
var finders = manifestService.SelectFindersFor(fixturePath, fileFinder);

Assert.Equal(expectedManifest, finders.First().GetManifestFilenames(fixturePath).First());
}
}
}
27 changes: 27 additions & 0 deletions Corgibytes.Freshli.Lib.Test/Unit/CSharp/PackagesManifestTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Corgibytes.Freshli.Lib.Languages.CSharp;
using Xunit;

namespace Corgibytes.Freshli.Lib.Test.Unit.CSharp;

public class PackagesManifestTest
{
[Fact]
public void ParsesFile()
{
var manifest = new PackagesManifest();
var testContent = @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
<package id=""DotNetEnv"" version=""1.4.0"" />
<package id=""Elasticsearch.Net"" version=""7.10"" />
<package id=""HtmlAgilityPack"" version=""1.11.30"" />
<package id=""LibGit2Sharp"" version=""0.27.0"" />
<package id=""NLog"" version=""4.7.7"" />
<package id=""RestSharp"" version=""106.11.7"" />
</packages>";

manifest.Parse(testContent);

Assert.Equal(6, manifest.Count);
}

}
21 changes: 21 additions & 0 deletions Corgibytes.Freshli.Lib.Test/Unit/RunnerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using Xunit;

namespace Corgibytes.Freshli.Lib.Test.Unit;

public class RunnerTest
{
[Fact]
public void RunCSharp()
{
string fixturePath = Fixtures.Path(
"csharp"
);
Runner runner = new();
IList<ScanResult> scanResults = runner.Run(fixturePath);
Assert.Equal(2, scanResults.Count);
Assert.Contains(scanResults, result => result.Filename.Equals("csproj/Project.csproj"));
Assert.Contains(scanResults, result => result.Filename.Equals("config/packages.config"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.4.4" targetFramework="net48" />
<package id="WebActivator" version="1.0.0.0" targetFramework="net48" />
<package id="NLog" version="4.7.7" targetFramework="net48" />
<package id="RestSharp" version="110.2.0" targetFramework="net48" />
</packages>
21 changes: 21 additions & 0 deletions Corgibytes.Freshli.Lib.Test/fixtures/csharp/csproj/Project.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>dev-$([System.DateTime]::Now.ToString("yyyyMMddTHHmm"))</VersionSuffix>
<LangVersion>9</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DotNetEnv" Version="1.4.0" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.30" />
<PackageReference Include="LibGit2Sharp" Version="0.27.0-preview-0096" />
<PackageReference Include="NLog" Version="4.7.7" />
<PackageReference Include="RestSharp" Version="106.11.7" />
</ItemGroup>
<ItemGroup>
<Content Include="*.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
23 changes: 23 additions & 0 deletions Corgibytes.Freshli.Lib/Languages/CSharp/PackagesManifest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Xml;

namespace Corgibytes.Freshli.Lib.Languages.CSharp;

public class PackagesManifest : AbstractManifest
{
public override void Parse(string contents)
{
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(contents);

var packages = xmlDoc.GetElementsByTagName("package");
foreach (XmlNode package in packages)
{
Add(
package.Attributes![0].Value,
package.Attributes[1].Value
);
}
}

public override bool UsesExactMatches => true;
}
16 changes: 16 additions & 0 deletions Corgibytes.Freshli.Lib/Languages/CSharp/PackagesManifestFinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Corgibytes.Freshli.Lib.Languages.CSharp;

public class PackagesManifestFinder : AbstractManifestFinder
{
protected override string ManifestPattern => "packages.config";
public override IPackageRepository RepositoryFor(string projectRootPath)
{
return new NuGetRepository();
}

public override IManifest ManifestFor(string projectRootPath)
{
return new PackagesManifest();
}

}
6 changes: 0 additions & 6 deletions Corgibytes.Freshli.Lib/ManifestService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using NLog;

namespace Corgibytes.Freshli.Lib
{
Expand All @@ -22,9 +19,6 @@ public IEnumerable<AbstractManifestFinder> SelectFindersFor(string analysisPath,
if (finder.GetManifestFilenames(analysisPath).Any())
{
yield return finder;

// TODO: Remove this break to add support for multiple manifests from different providers
yield break;
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions Corgibytes.Freshli.Lib/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public IList<ScanResult> Run(string analysisPath, DateTimeOffset asOf)
asOf,
abstractManifestFinders,
fileHistoryFinder
// TODO: Remove the call to `Take(1)` to support results from multiple manifest files
).Take(1).ToList();
).ToList();
}

DotNetEnv.Env.Load();
Expand Down Expand Up @@ -90,9 +89,6 @@ private IEnumerable<ScanResult> ProcessManifestFiles(string analysisPath, DateTi
).ToList();

yield return new ScanResult(manifestFile, metricsResults);

// TODO: Remove this break to enable multi-manifest file support, I _think_ ^_^
yield break;
}
}
}
Expand Down