Integration Testing ‐ Folder contents as filetree - Habilya/LearningCourseNotes GitHub Wiki

public static class IntegrationTestsHelper
{
	public static List<string> GetFolderContentsAsFileTree(string directory, string indent = "|   ")
	{
		var fileTree = new List<string>();

		// Get all subdirectories
		var subdirectories = Directory.GetDirectories(directory);
		foreach (var subdirectory in subdirectories)
		{
			string subdirectoryName = Path.GetFileName(subdirectory);

			// For subdirectories, add the appropriate indentation and prefix
			fileTree.Add($"{indent}-- {subdirectoryName}");

			// Recursively add subdirectories with additional indentation
			fileTree.AddRange(GetFolderContentsAsFileTree(subdirectory, $"{indent}|   "));
		}

		// Get all files in the current directory
		var files = Directory.GetFiles(directory);
		foreach (var file in files)
		{
			string fileName = Path.GetFileName(file);
			// For files, add the appropriate indentation and prefix
			fileTree.Add($"{indent}-- {fileName}");
		}

		return fileTree;
	}

	public static List<string> GetFolderContents(string folderFullPath)
	{
		List<string> contents = new List<string>();
		var datedOutputFolder = new DirectoryInfo(folderFullPath);

		contents.AddRange(datedOutputFolder
			.GetFiles()
			.Select(s => s.Name.ToString())
			.ToList());

		contents.AddRange(datedOutputFolder
			.GetDirectories()
			.Select(s => s.Name.ToString())
			.ToList());

		return contents;
	}
}

Then, inside of the test

	[Fact]
	public void ClientsFolderContentsContents_ShouldMatch()
	{
		// Arrange
		var expected = new List<string>
		{
			"|   -- ALLSTATE",
			"|   |   -- DataIn",
			"|   |   |   -- WatcherFileSystem",
			"|   |   |   |   -- FromKOFAX",
			"|   |   |   |   |   -- Error",
			"|   |   |   |   |   |   -- incoming-document-JSON_OK_FailingDB",
			"|   |   |   |   |   |   |   -- incoming-document-JSON_OK_FailingDB.json",
			"|   |   |   |   |   |   |   -- incoming-document-JSON_OK_FailingDB.log",
			"|   |   |   |   |   -- Processed",
			"|   |   |   |   |   |   -- incoming-document-31160d21-06d5-40a9-ab67-4bbe10d854f8",
			"|   |   |   |   |   |   |   -- incoming-document-31160d21-06d5-40a9-ab67-4bbe10d854f8.json",
			"|   |   |   |   |   |   |   -- incoming-document-31160d21-06d5-40a9-ab67-4bbe10d854f8.log",
			"|   |   |   |   |   -- WatcherFileSystemFromKofax_2008-8.xlsx",
			"|   -- REALOGY",
			"|   |   -- DataIn",
			"|   |   |   -- WatcherFileSystem",
			"|   |   |   |   -- FromKOFAX",
			"|   |   |   |   |   -- Error",
			"|   |   |   |   |   -- Processed",
			"|   |   |   |   |   |   -- incoming-document-0002c4dc_Realogy",
			"|   |   |   |   |   |   |   -- incoming-document-0002c4dc_Realogy.json",
			"|   |   |   |   |   |   |   -- incoming-document-0002c4dc_Realogy.log",
			"|   |   |   |   |   -- WatcherFileSystemFromKofax_2008-8.xlsx"
		};


		// Act
		var actual = _fixture.ClientsFolderContents;

		// Assert
		actual.Should().BeEquivalentTo(expected);
	}
⚠️ **GitHub.com Fallback** ⚠️