Skip to content

CycloneDX should fail when the provided file was not found #883

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

Merged
merged 3 commits into from
Jun 7, 2024
Merged
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
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