Skip to content

Commit cfae239

Browse files
authored
performance: first check if assetsfile exists with its regular name of before using dotnet build to retrieve its name (#960)
* performance: first check regular name of assetsfile before using dotnet msbuild to retrieve it from Signed-off-by: MTsfoni <mibau89@gmail.com>
1 parent 3f2f569 commit cfae239

2 files changed

Lines changed: 66 additions & 30 deletions

File tree

CycloneDX.Tests/ProjectFileServiceTests.cs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
using CycloneDX.Services;
2828
using System.IO.Abstractions;
2929
using System.Linq;
30+
using System.IO;
31+
using System.Xml.Schema;
3032

3133
namespace CycloneDX.Tests
3234
{
@@ -41,16 +43,44 @@ private ProjectFileService GetInstanceOfProjectFileService()
4143
new DotnetUtilsService(fileSystem, dotnetCommandService),
4244
new PackagesFileService(fileSystem),
4345
new ProjectAssetsFileService(fileSystem, () => new AssetFileReader()));
44-
4546
}
4647

4748
[Theory]
48-
[InlineData(@"C:\github\cyclonedx-dotnet\Core\CycloneDX.csproj", "", @"C:\github\cyclonedx-dotnet\Core\obj")]
49-
[InlineData(@"C:\github\cyclonedx-dotnet\Core\CycloneDX.csproj", @"C:\github\cyclonedx-dotnet\artifacts", @"C:\github\cyclonedx-dotnet\artifacts\obj\CycloneDX")]
50-
public void GetPropertyUseProjectFileName(string projectFilePath, string baseIntermediateOutputPath, string expected)
49+
[InlineData("", @"C:\Projects\Foo\obj\project.assets.json", false)] // expected file exists
50+
[InlineData("", @"C:\Projects\artifacts\obj\Foo\project.assets.json", true)] // expected missing, service finds it
51+
[InlineData(@"C:\build", @"C:\build\obj\Foo\project.assets.json", false)] // using baseIntermediateOutputPath
52+
public void GetProjectAssetsFilePath_CoversAllScenarios(
53+
string baseOutputPath,
54+
string assetsPath,
55+
bool dotnetServiceShouldBeUsed)
5156
{
52-
string outputPath = ProjectFileService.GetProjectProperty(XFS.Path(projectFilePath), XFS.Path(baseIntermediateOutputPath));
53-
Assert.Equal(XFS.Path(expected), outputPath);
57+
58+
baseOutputPath = XFS.Path(baseOutputPath);
59+
assetsPath = XFS.Path(assetsPath);
60+
61+
var mockFileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
62+
{
63+
{ XFS.Path(assetsPath), "" }
64+
});
65+
66+
string projectPath = XFS.Path(@"C:\Projects\Foo\Foo.csproj");
67+
// Arrange
68+
var dotnetMock = new Mock<IDotnetUtilsService>();
69+
var service = new ProjectFileService(mockFileSystem, dotnetMock.Object, null, null);
70+
71+
dotnetMock.Setup(d => d.GetAssetsPath(projectPath))
72+
.Returns(new DotnetUtilsResult<string>
73+
{
74+
ErrorMessage = null,
75+
Result = assetsPath
76+
});
77+
78+
// Act
79+
var result = service.GetProjectAssetsFilePath(projectPath, baseOutputPath);
80+
81+
// Assert
82+
Assert.Equal(assetsPath, result);
83+
dotnetMock.Verify(d => d.GetAssetsPath(It.IsAny<string>()), dotnetServiceShouldBeUsed ? Times.Once : Times.Never);
5484
}
5585

5686
[Fact]

CycloneDX/Services/ProjectFileService.cs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,6 @@ public bool IsTestProject(string projectFilePath)
153153
return (name, version);
154154
}
155155

156-
static internal string GetProjectProperty(string projectFilePath, string baseIntermediateOutputPath)
157-
{
158-
if (string.IsNullOrEmpty(baseIntermediateOutputPath))
159-
{
160-
return Path.Combine(Path.GetDirectoryName(projectFilePath), "obj");
161-
}
162-
else
163-
{
164-
string folderName = Path.GetFileNameWithoutExtension(projectFilePath);
165-
return Path.Combine(baseIntermediateOutputPath, "obj", folderName);
166-
}
167-
}
168-
169156
public bool DisablePackageRestore { get; set; }
170157

171158
/// <summary>
@@ -232,21 +219,40 @@ public async Task<HashSet<DotnetDependency>> GetProjectDotnetDependencysAsync(st
232219
return packages;
233220
}
234221

235-
private string GetProjectAssetsFilePath(string projectFilePath, string baseIntermediateOutputPath)
236-
{
237-
if (string.IsNullOrEmpty(baseIntermediateOutputPath ))
222+
internal string GetProjectAssetsFilePath(string projectFilePath, string baseIntermediateOutputPath)
223+
{
224+
string assetsPath;
225+
226+
if (string.IsNullOrEmpty(baseIntermediateOutputPath))
238227
{
239-
var result = _dotnetUtilsService.GetAssetsPath(projectFilePath);
240-
if(result.Success && _fileSystem.File.Exists(result.Result))
241-
{
242-
Console.WriteLine($" Found Assetsfile under {result.Result}");
243-
return result.Result;
244-
}
245-
228+
// Default to <projectDir>/obj
229+
assetsPath = Path.Combine(Path.GetDirectoryName(projectFilePath), "obj", "project.assets.json");
230+
}
231+
else
232+
{
233+
// Use <baseIntermediateOutputPath>/obj/<projectName>
234+
string projectName = Path.GetFileNameWithoutExtension(projectFilePath);
235+
assetsPath = Path.Combine(baseIntermediateOutputPath, "obj", projectName, "project.assets.json");
236+
}
237+
238+
if (_fileSystem.File.Exists(assetsPath))
239+
{
240+
Console.WriteLine($" Found Assetsfile under {assetsPath}");
241+
return assetsPath;
242+
}
243+
244+
var result = _dotnetUtilsService.GetAssetsPath(projectFilePath);
245+
if (result.Success && _fileSystem.File.Exists(result.Result))
246+
{
247+
Console.WriteLine($" Found Assetsfile under {result.Result}");
248+
return result.Result;
246249
}
247-
return _fileSystem.Path.Combine(GetProjectProperty(projectFilePath, baseIntermediateOutputPath), "project.assets.json");
250+
251+
// Fall back to expected path even if file doesn't exist
252+
return assetsPath;
248253
}
249254

255+
250256
/// <summary>
251257
/// Analyzes all Project file references for NuGet package references.
252258
/// </summary>

0 commit comments

Comments
 (0)