Skip to content

Commit

Permalink
fix: CycloneDX should fail when the provided file was not found
Browse files Browse the repository at this point in the history
* CycloneDX should fail when the provided file was not found

#882

Signed-off-by: Michael Tsfoni <[email protected]>
  • Loading branch information
mtsfoni committed Jun 7, 2024
1 parent 8d75418 commit 953f59b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
28 changes: 28 additions & 0 deletions CycloneDX.Tests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,33 @@ public void CheckMetaDataTemplate()
Assert.Matches("CycloneDX", bom.Metadata.Tools.Tools[0].Vendor);
Assert.Matches("1.2.0", bom.Metadata.Tools.Tools[0].Version);
}

[Theory]
[InlineData(@"c:\SolutionPath\SolutionFile.sln", false)]
[InlineData(@"c:\SolutionPath\ProjectFile.csproj", false)]
[InlineData(@"c:\SolutionPath\ProjectFile.csproj", true)]
[InlineData(@"c:\SolutionPath\packages.config", false)]
public async Task CallingCycloneDX_WithSolutionOrProjectFileThatDoesntExistsReturnAnythingButZero(string path, bool rs)
{
var mockFileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
var mockSolutionFileService = new Mock<ISolutionFileService>();
mockSolutionFileService
.Setup(s => s.GetSolutionDotnetDependencys(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(new HashSet<DotnetDependency>());

Runner runner = new Runner(fileSystem: mockFileSystem, null, null, null, null, null, solutionFileService: mockSolutionFileService.Object, null);

RunOptions runOptions = new RunOptions
{
SolutionOrProjectFile = XFS.Path(path),
scanProjectReferences = rs,
outputDirectory = XFS.Path(@"c:\NewDirectory"),
outputFilename = XFS.Path(@"my_bom.xml")
};

var exitCode = await runner.HandleCommandAsync(runOptions);

Assert.NotEqual((int)ExitCode.OK, exitCode);
}
}
}
24 changes: 22 additions & 2 deletions CycloneDX/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,41 @@ public async Task<int> HandleCommandAsync(RunOptions options)
{
if (SolutionOrProjectFile.ToLowerInvariant().EndsWith(".sln", StringComparison.OrdinalIgnoreCase))
{
if (!fileSystem.File.Exists(SolutionOrProjectFile))
{
Console.Error.WriteLine($"No file found at path {SolutionOrProjectFile}");
return (int)ExitCode.InvalidOptions;
}
packages = await solutionFileService.GetSolutionDotnetDependencys(fullSolutionOrProjectFilePath, baseIntermediateOutputPath, excludetestprojects, framework, runtime).ConfigureAwait(false);
topLevelComponent.Name = fileSystem.Path.GetFileNameWithoutExtension(SolutionOrProjectFile);
}
else if (Utils.IsSupportedProjectType(SolutionOrProjectFile) && scanProjectReferences)
{
if(!fileSystem.File.Exists(SolutionOrProjectFile))
{
Console.Error.WriteLine($"No file found at path {SolutionOrProjectFile}");
return (int)ExitCode.InvalidOptions;
}
packages = await projectFileService.RecursivelyGetProjectDotnetDependencysAsync(fullSolutionOrProjectFilePath, baseIntermediateOutputPath, excludetestprojects, framework, runtime).ConfigureAwait(false);
topLevelComponent.Name = fileSystem.Path.GetFileNameWithoutExtension(SolutionOrProjectFile);
}
else if (Utils.IsSupportedProjectType(SolutionOrProjectFile))
{
{
if(!fileSystem.File.Exists(SolutionOrProjectFile))
{
Console.Error.WriteLine($"No file found at path {SolutionOrProjectFile}");
return (int)ExitCode.InvalidOptions;
}
packages = await projectFileService.GetProjectDotnetDependencysAsync(fullSolutionOrProjectFilePath, baseIntermediateOutputPath, excludetestprojects, framework, runtime).ConfigureAwait(false);
topLevelComponent.Name = fileSystem.Path.GetFileNameWithoutExtension(SolutionOrProjectFile);
}
else if (this.fileSystem.Path.GetFileName(SolutionOrProjectFile).ToLowerInvariant().Equals("packages.config", StringComparison.OrdinalIgnoreCase))
else if (fileSystem.Path.GetFileName(SolutionOrProjectFile).ToLowerInvariant().Equals("packages.config", StringComparison.OrdinalIgnoreCase))
{
if (!fileSystem.File.Exists(SolutionOrProjectFile))
{
Console.Error.WriteLine($"No file found at path {SolutionOrProjectFile}");
return (int)ExitCode.InvalidOptions;
}
packages = await packagesFileService.GetDotnetDependencysAsync(fullSolutionOrProjectFilePath).ConfigureAwait(false);
topLevelComponent.Name = fileSystem.Path.GetDirectoryName(fullSolutionOrProjectFilePath);
}
Expand Down

0 comments on commit 953f59b

Please sign in to comment.