Skip to content

Commit 4d77522

Browse files
Added unit test to check order of operations for Save File.
Added GitHub SourceCode URL
1 parent 36471ca commit 4d77522

File tree

2 files changed

+102
-39
lines changed

2 files changed

+102
-39
lines changed

CICD.Tools.GitHubToCatalogYamlTests/CatalogManagerTests.cs

+63
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
using Skyline.DataMiner.CICD.FileSystem;
1717
using Skyline.DataMiner.CICD.Tools.GitHubToCatalogYaml;
1818

19+
using YamlDotNet.Serialization;
20+
1921
[TestClass]
2022
public class CatalogManagerTests
2123
{
@@ -454,5 +456,66 @@ public async Task ProcessCatalogYamlAsync_ShouldUseCatalogYmlOverManifestWhenBot
454456
// Assert
455457
mockFileSystem.Verify(fs => fs.File.WriteAllText(catalogFilePath, It.Is<string>(s => s.Contains("short_description: from catalog"))), Times.Once);
456458
}
459+
460+
[TestMethod]
461+
public async Task ProcessCatalogYamlAsync_SaveFileShouldAccessFileSystemInCorrectOrderForBothFiles()
462+
{
463+
// Arrange
464+
var repoName = "SLC-AS-testRepo";
465+
var expectedCatalogPath = "testWorkspace/catalog.yml";
466+
var expectedAutoGenPath = "testWorkspace/.githubtocatalog/auto-generated-catalog.yml";
467+
var manifestYamlContent = "id: manifestId\nshort_description: from manifest";
468+
469+
// Simulate the YAML serialization
470+
mockFileSystem.Setup(fs => fs.File.ReadAllText(manifestFilePath)).Returns(manifestYamlContent);
471+
// Setup mocks for file system interactions
472+
var mockSequence = new MockSequence();
473+
474+
// Ensure the operations for 'catalog.yml' happen in sequence
475+
mockFileSystem.InSequence(mockSequence)
476+
.Setup(fs => fs.File.DeleteFile(expectedCatalogPath));
477+
mockFileSystem.InSequence(mockSequence)
478+
.Setup(fs => fs.Path.GetDirectoryName(expectedCatalogPath))
479+
.Returns("testDirectory");
480+
mockFileSystem.InSequence(mockSequence)
481+
.Setup(fs => fs.Directory.CreateDirectory("testDirectory"));
482+
mockFileSystem.InSequence(mockSequence)
483+
.Setup(fs => fs.Directory.TryAllowWritesOnDirectory("testDirectory"))
484+
.Returns(true);
485+
mockFileSystem.InSequence(mockSequence)
486+
.Setup(fs => fs.File.WriteAllText(expectedCatalogPath, It.IsAny<String>()));
487+
488+
// Ensure the operations for 'auto-generated-catalog.yml' happen in sequence
489+
mockFileSystem.InSequence(mockSequence)
490+
.Setup(fs => fs.File.DeleteFile(expectedAutoGenPath));
491+
mockFileSystem.InSequence(mockSequence)
492+
.Setup(fs => fs.Path.GetDirectoryName(expectedAutoGenPath))
493+
.Returns("testAutoGenDirectory");
494+
mockFileSystem.InSequence(mockSequence)
495+
.Setup(fs => fs.Directory.CreateDirectory("testAutoGenDirectory"));
496+
mockFileSystem.InSequence(mockSequence)
497+
.Setup(fs => fs.Directory.TryAllowWritesOnDirectory("testAutoGenDirectory"))
498+
.Returns(true);
499+
mockFileSystem.InSequence(mockSequence)
500+
.Setup(fs => fs.File.WriteAllText(expectedAutoGenPath, It.IsAny<String>()));
501+
502+
// Act
503+
await catalogManager.ProcessCatalogYamlAsync(repoName);
504+
505+
// Assert
506+
// Verify that the catalog.yml file system interactions occurred in the correct order
507+
mockFileSystem.Verify(fs => fs.File.DeleteFile(expectedCatalogPath), Times.Once);
508+
mockFileSystem.Verify(fs => fs.Path.GetDirectoryName(expectedCatalogPath), Times.Once);
509+
mockFileSystem.Verify(fs => fs.Directory.CreateDirectory("testDirectory"), Times.Once);
510+
mockFileSystem.Verify(fs => fs.Directory.TryAllowWritesOnDirectory("testDirectory"), Times.Once);
511+
mockFileSystem.Verify(fs => fs.File.WriteAllText(expectedCatalogPath, It.IsAny<String>()), Times.Once);
512+
513+
// Verify that the auto-generated-catalog.yml file system interactions occurred in the correct order
514+
mockFileSystem.Verify(fs => fs.File.DeleteFile(expectedAutoGenPath), Times.Once);
515+
mockFileSystem.Verify(fs => fs.Path.GetDirectoryName(expectedAutoGenPath), Times.Once);
516+
mockFileSystem.Verify(fs => fs.Directory.CreateDirectory("testAutoGenDirectory"), Times.Once);
517+
mockFileSystem.Verify(fs => fs.Directory.TryAllowWritesOnDirectory("testAutoGenDirectory"), Times.Once);
518+
mockFileSystem.Verify(fs => fs.File.WriteAllText(expectedAutoGenPath, It.IsAny<String>()), Times.Once);
519+
}
457520
}
458521
}

Skyline.DataMiner.CICD.Tools.GitHubToCatalogYaml/CatalogManager.cs

+39-39
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public CatalogManager(IFileSystem fs, ILogger logger, IGitHubService service, st
3434
this.service = service;
3535
this.workspace = workspace;
3636
}
37+
3738
/// <summary>
3839
/// Processes the catalog YAML file for the specified GitHub repository. It checks and updates various fields such as ID, description, tags, title, and type.
3940
/// If no catalog file is found, it attempts to create one from the manifest file.
@@ -84,7 +85,24 @@ public async Task ProcessCatalogYamlAsync(string repoName, string catalogIdentif
8485
}
8586

8687
/// <summary>
87-
/// Checks and sets the ID field in the provided catalog YAML.
88+
/// Infers the artifact content type based on the provided keyword. It checks if the keyword is in the artifact type map and returns the corresponding content type.
89+
/// </summary>
90+
/// <param name="keyword">The keyword to infer the artifact content type from.</param>
91+
/// <returns>The inferred content type, or an empty string if the keyword is not recognized.</returns>
92+
private static string InferArtifactContentType(string keyword)
93+
{
94+
// Check if the keyword exists in the dictionary
95+
if (Constants.ArtifactTypeMap.TryGetValue(keyword.ToUpper(), out var contentType))
96+
{
97+
return contentType;
98+
}
99+
100+
return Constants.ArtifactTypeMap.FirstOrDefault(pair => pair.Value.Equals(keyword, StringComparison.OrdinalIgnoreCase)).Value ??
101+
String.Empty;
102+
}
103+
104+
/// <summary>
105+
/// Checks and sets the ID field in the provided catalog YAML.
88106
/// If the ID is not present or is empty, it attempts to retrieve or create a new ID using the provided catalog identifier, or the ID from the auto-generated catalog YAML.
89107
/// </summary>
90108
/// <param name="catalogYaml">The catalog YAML object to check and update.</param>
@@ -134,7 +152,7 @@ private async Task CheckShortDescription(CatalogYaml catalogYaml)
134152
}
135153

136154
/// <summary>
137-
/// Checks and sets the SourceCodeUrl field in the provided catalog YAML.
155+
/// Checks and sets the SourceCodeUrl field in the provided catalog YAML.
138156
/// If the SourceCodeUrl is not present or is empty, it assigns a GitHub repository URL based on the provided repository name.
139157
/// </summary>
140158
/// <param name="catalogYaml">The catalog YAML object to check and update.</param>
@@ -242,29 +260,20 @@ private void CheckType(CatalogYaml catalogYaml, CleanTitle parsedRepoName)
242260
/// <param name="deserializer">The YAML deserializer to parse the existing YAML file.</param>
243261
/// <param name="foundFile">The path to the found YAML file, or null if no file is found.</param>
244262
/// <returns>The deserialized <see cref="CatalogYaml"/> object, or a new object if no file is found.</returns>
245-
private CatalogYaml CreateCatalogYaml(IDeserializer deserializer, out string foundFile)
263+
private CatalogYaml CreateAutoGeneratedCatalogYaml(IDeserializer deserializer, out string foundFile)
246264
{
247-
logger.LogDebug("Checking if user has provided a catalog.yml or manifest.yml file within the workspace root.");
265+
logger.LogDebug("Checking if ");
248266
foundFile = null;
249267

250-
string filePath = fs.Path.Combine(workspace, "catalog.yml");
268+
string filePath = fs.Path.Combine(workspace, ".githubtocatalog", "auto-generated-catalog.yml");
251269
if (!fs.File.Exists(filePath))
252270
{
253-
filePath = fs.Path.Combine(workspace, "manifest.yml");
254-
if (!fs.File.Exists(filePath))
255-
{
256-
logger.LogDebug("No existing configuration file found.");
257-
}
258-
else
259-
{
260-
foundFile = filePath;
261-
logger.LogDebug($"Found file at: {filePath}");
262-
}
271+
logger.LogDebug("No existing auto-generated configuration file found.");
263272
}
264273
else
265274
{
266275
foundFile = filePath;
267-
logger.LogDebug($"Found file at: {filePath}");
276+
logger.LogDebug($"Found auto-generated file at: {filePath}");
268277
}
269278

270279
if (String.IsNullOrWhiteSpace(foundFile))
@@ -284,20 +293,29 @@ private CatalogYaml CreateCatalogYaml(IDeserializer deserializer, out string fou
284293
/// <param name="deserializer">The YAML deserializer to parse the existing YAML file.</param>
285294
/// <param name="foundFile">The path to the found YAML file, or null if no file is found.</param>
286295
/// <returns>The deserialized <see cref="CatalogYaml"/> object, or a new object if no file is found.</returns>
287-
private CatalogYaml CreateAutoGeneratedCatalogYaml(IDeserializer deserializer, out string foundFile)
296+
private CatalogYaml CreateCatalogYaml(IDeserializer deserializer, out string foundFile)
288297
{
289-
logger.LogDebug("Checking if ");
298+
logger.LogDebug("Checking if user has provided a catalog.yml or manifest.yml file within the workspace root.");
290299
foundFile = null;
291300

292-
string filePath = fs.Path.Combine(workspace, ".githubtocatalog", "auto-generated-catalog.yml");
301+
string filePath = fs.Path.Combine(workspace, "catalog.yml");
293302
if (!fs.File.Exists(filePath))
294303
{
295-
logger.LogDebug("No existing auto-generated configuration file found.");
304+
filePath = fs.Path.Combine(workspace, "manifest.yml");
305+
if (!fs.File.Exists(filePath))
306+
{
307+
logger.LogDebug("No existing configuration file found.");
308+
}
309+
else
310+
{
311+
foundFile = filePath;
312+
logger.LogDebug($"Found file at: {filePath}");
313+
}
296314
}
297315
else
298316
{
299317
foundFile = filePath;
300-
logger.LogDebug($"Found auto-generated file at: {filePath}");
318+
logger.LogDebug($"Found file at: {filePath}");
301319
}
302320

303321
if (String.IsNullOrWhiteSpace(foundFile))
@@ -310,24 +328,6 @@ private CatalogYaml CreateAutoGeneratedCatalogYaml(IDeserializer deserializer, o
310328
return deserializer.Deserialize<CatalogYaml>(yamlContent) ?? new CatalogYaml();
311329
}
312330

313-
314-
/// <summary>
315-
/// Infers the artifact content type based on the provided keyword. It checks if the keyword is in the artifact type map and returns the corresponding content type.
316-
/// </summary>
317-
/// <param name="keyword">The keyword to infer the artifact content type from.</param>
318-
/// <returns>The inferred content type, or an empty string if the keyword is not recognized.</returns>
319-
private static string InferArtifactContentType(string keyword)
320-
{
321-
// Check if the keyword exists in the dictionary
322-
if (Constants.ArtifactTypeMap.TryGetValue(keyword.ToUpper(), out var contentType))
323-
{
324-
return contentType;
325-
}
326-
327-
return Constants.ArtifactTypeMap.FirstOrDefault(pair => pair.Value.Equals(keyword, StringComparison.OrdinalIgnoreCase)).Value ??
328-
String.Empty;
329-
}
330-
331331
/// <summary>
332332
/// Serializes the catalog YAML object and saves it to the specified output path.
333333
/// </summary>

0 commit comments

Comments
 (0)