Skip to content

Commit 72ad042

Browse files
CopilotDaveTryon
andcommitted
Refactor FileInfoWriter tests to reduce duplication with helper method
Co-authored-by: DaveTryon <45672944+DaveTryon@users.noreply.github.com>
1 parent 8c77719 commit 72ad042

1 file changed

Lines changed: 24 additions & 24 deletions

File tree

test/Microsoft.Sbom.Api.Tests/Executors/FileInfoWriterTests.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,41 @@ public void AfterEach()
5252
// Just verify nothing throws
5353
}
5454

55-
[TestMethod]
56-
public async Task Write_FileWithinDropPath_WritesToFilesSection()
55+
private async Task<(ChannelReader<JsonDocWithSerializer>, ChannelReader<FileValidationResult>, ISbomConfig[])> SetupFileInfoWriterTest(
56+
string path,
57+
bool isOutsideDropPath,
58+
string checksumValue)
5759
{
58-
// Arrange
5960
var sbomConfigs = new[] { sbomConfigMock.Object };
6061
var fileInfo = new InternalSbomFileInfo
6162
{
62-
Path = "internal/package.spdx.json",
63-
IsOutsideDropPath = false,
63+
Path = path,
64+
IsOutsideDropPath = isOutsideDropPath,
6465
FileTypes = new[] { FileType.SPDX },
65-
Checksum = new[] { new Checksum { Algorithm = AlgorithmName.SHA256, ChecksumValue = "abc123" } }
66+
Checksum = new[] { new Checksum { Algorithm = AlgorithmName.SHA256, ChecksumValue = checksumValue } }
6667
};
6768

6869
var fileInfoChannel = Channel.CreateUnbounded<InternalSbomFileInfo>();
6970
await fileInfoChannel.Writer.WriteAsync(fileInfo);
7071
fileInfoChannel.Writer.Complete();
7172

73+
var (result, errors) = testSubject.Write(fileInfoChannel.Reader, sbomConfigs);
74+
return (result, errors, sbomConfigs);
75+
}
76+
77+
[TestMethod]
78+
public async Task Write_FileWithinDropPath_WritesToFilesSection()
79+
{
80+
// Arrange
81+
var (result, errors, sbomConfigs) = await SetupFileInfoWriterTest(
82+
"internal/package.spdx.json",
83+
false,
84+
"abc123");
85+
7286
// Setup expectations - SPDX files within drop path should record both types of IDs
7387
sbomPackageDetailsRecorderMock.Setup(m => m.RecordFileId(It.IsAny<string>()));
7488
sbomPackageDetailsRecorderMock.Setup(m => m.RecordSPDXFileId(It.IsAny<string>()));
7589

76-
// Act
77-
var (result, errors) = testSubject.Write(fileInfoChannel.Reader, sbomConfigs);
78-
7990
// Assert
8091
var resultList = new List<JsonDocWithSerializer>();
8192
await foreach (var item in result.ReadAllAsync())
@@ -98,25 +109,14 @@ public async Task Write_FileWithinDropPath_WritesToFilesSection()
98109
public async Task Write_FileOutsideDropPath_DoesNotWriteToFilesSection()
99110
{
100111
// Arrange
101-
var sbomConfigs = new[] { sbomConfigMock.Object };
102-
var fileInfo = new InternalSbomFileInfo
103-
{
104-
Path = "external/package.spdx.json",
105-
IsOutsideDropPath = true,
106-
FileTypes = new[] { FileType.SPDX },
107-
Checksum = new[] { new Checksum { Algorithm = AlgorithmName.SHA256, ChecksumValue = "def456" } }
108-
};
109-
110-
var fileInfoChannel = Channel.CreateUnbounded<InternalSbomFileInfo>();
111-
await fileInfoChannel.Writer.WriteAsync(fileInfo);
112-
fileInfoChannel.Writer.Complete();
112+
var (result, errors, sbomConfigs) = await SetupFileInfoWriterTest(
113+
"external/package.spdx.json",
114+
true,
115+
"def456");
113116

114117
// Setup expectations - SPDX file ID should still be recorded
115118
sbomPackageDetailsRecorderMock.Setup(m => m.RecordSPDXFileId(It.IsAny<string>()));
116119

117-
// Act
118-
var (result, errors) = testSubject.Write(fileInfoChannel.Reader, sbomConfigs);
119-
120120
// Assert
121121
var resultList = new List<JsonDocWithSerializer>();
122122
await foreach (var item in result.ReadAllAsync())

0 commit comments

Comments
 (0)