From 309a8cb5c10e4624b3de8e0a623ecceb03bc7230 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Fri, 11 Oct 2024 23:00:11 +0300 Subject: [PATCH] Fixes file system browser Presentation Api integration tests failing on Unix systems --- .../DirectoriesControllerTests.cs | 29 +++++++++++++++---- .../FileManagement/FilesControllerTests.cs | 10 ++++--- .../Fixtures/FileSystemStructureFixture.cs | 6 ++-- .../FileManagement/PathControllerTests.cs | 6 +++- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/FileManagement/DirectoriesControllerTests.cs b/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/FileManagement/DirectoriesControllerTests.cs index 7a7a33c3..02be4ed8 100644 --- a/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/FileManagement/DirectoriesControllerTests.cs +++ b/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/FileManagement/DirectoriesControllerTests.cs @@ -11,6 +11,7 @@ using System.IO; using System.Linq; using System.Net; using System.Net.Http; +using System.Runtime.InteropServices; using System.Text.Json; using System.Text.Json.Serialization; using System.Threading; @@ -33,6 +34,7 @@ public class DirectoriesControllerTests : IClassFixture PropertyNameCaseInsensitive = true, Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) } }; + private static readonly bool s_isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); #endregion #region ====================================================================== CTOR ===================================================================================== @@ -83,7 +85,8 @@ public class DirectoriesControllerTests : IClassFixture treeNodes!.Should().NotBeEmpty(); string[] pathSegments = testPath.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries); - + if (s_isLinux) // on UNIX, the leading slash is considered "the drive", the root location - add it! + pathSegments = new string[] { "/" }.Concat(pathSegments).ToArray(); // recursively validate the tree structure void ValidateNode(FileSystemTreeNodeResponse node, int depth) { @@ -97,6 +100,8 @@ public class DirectoriesControllerTests : IClassFixture node.Name.Should().Be(expectedSegment); string expectedPath = string.Join(Path.DirectorySeparatorChar.ToString(), pathSegments.Take(depth + 1)); + if (expectedPath.StartsWith("//")) + expectedPath = expectedPath.Substring(1); if (!expectedPath.EndsWith(Path.DirectorySeparatorChar)) expectedPath += Path.DirectorySeparatorChar; node.Path.Should().EndWith(expectedPath); @@ -162,6 +167,8 @@ public class DirectoriesControllerTests : IClassFixture treeNodes!.Should().NotBeEmpty(); string[] pathSegments = testPath.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries); + if (s_isLinux) // on UNIX, the leading slash is considered "the drive", the root location - add it! + pathSegments = new string[] { "/" }.Concat(pathSegments).ToArray(); // recursively validate the tree structure void ValidateNode(FileSystemTreeNodeResponse node, int depth) @@ -178,6 +185,8 @@ public class DirectoriesControllerTests : IClassFixture string expectedPath = string.Join(Path.DirectorySeparatorChar.ToString(), pathSegments.Take(depth + 1)); if (!expectedPath.EndsWith(Path.DirectorySeparatorChar)) expectedPath += Path.DirectorySeparatorChar; + if (expectedPath.StartsWith("//")) + expectedPath = expectedPath.Substring(1); node.Path.Should().EndWith(expectedPath); // if the node is a drive or directory, check its children recursively @@ -191,7 +200,7 @@ public class DirectoriesControllerTests : IClassFixture else if (childNode.ItemType is FileSystemItemType.File) { childNode.Children.Should().BeEmpty(); // files should not have children - childNode.Name.Should().BeOneOf("TestFile_1.txt", "TestFile_2.txt"); + childNode.Name.Should().BeOneOf((s_isLinux ? "." : string.Empty) + "TestFile_1.txt", "TestFile_2.txt"); } } } @@ -244,6 +253,8 @@ public class DirectoriesControllerTests : IClassFixture treeNodes.Should().AllSatisfy(node => node.ItemType.Should().BeOneOf(FileSystemItemType.Directory, FileSystemItemType.Root)); string[] pathSegments = testPath.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries); + if (s_isLinux) // on UNIX, the leading slash is considered "the drive", the root location - add it! + pathSegments = new string[] { "/" }.Concat(pathSegments).ToArray(); // recursively validate the tree structure void ValidateNode(FileSystemTreeNodeResponse node, int depth) @@ -260,6 +271,8 @@ public class DirectoriesControllerTests : IClassFixture string expectedPath = string.Join(Path.DirectorySeparatorChar.ToString(), pathSegments.Take(depth + 1)); if (!expectedPath.EndsWith(Path.DirectorySeparatorChar)) expectedPath += Path.DirectorySeparatorChar; + if (expectedPath.StartsWith("//")) + expectedPath = expectedPath.Substring(1); node.Path.Should().EndWith(expectedPath); node.Children.Should().NotBeNull(); @@ -314,6 +327,8 @@ public class DirectoriesControllerTests : IClassFixture treeNodes.Should().AllSatisfy(node => node.ItemType.Should().BeOneOf(FileSystemItemType.Directory, FileSystemItemType.Root)); string[] pathSegments = testPath.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries); + if (s_isLinux) // on UNIX, the leading slash is considered "the drive", the root location - add it! + pathSegments = new string[] { "/" }.Concat(pathSegments).ToArray(); // recursively validate the tree structure void ValidateNode(FileSystemTreeNodeResponse node, int depth) @@ -330,6 +345,8 @@ public class DirectoriesControllerTests : IClassFixture string expectedPath = string.Join(Path.DirectorySeparatorChar.ToString(), pathSegments.Take(depth + 1)); if (!expectedPath.EndsWith(Path.DirectorySeparatorChar)) expectedPath += Path.DirectorySeparatorChar; + if (expectedPath.StartsWith("//")) + expectedPath = expectedPath.Substring(1); node.Path.Should().EndWith(expectedPath); node.Children.Should().NotBeNull(); @@ -521,8 +538,8 @@ public class DirectoriesControllerTests : IClassFixture FileSystemTreeNodeResponse firstNode = treeNodes.First(); - firstNode.Name.Should().Be("NestedDirectory_2"); - firstNode.Path.Should().Be(Path.Combine(testPath, "NestedDirectory_2" + Path.DirectorySeparatorChar)); + firstNode.Name.Should().Be((s_isLinux ? "." : string.Empty) + "NestedDirectory_2"); + firstNode.Path.Should().Be(Path.Combine(testPath, (s_isLinux ? "." : string.Empty) + "NestedDirectory_2" + Path.DirectorySeparatorChar)); firstNode.ItemType.Should().Be(FileSystemItemType.Directory); treeNodes.Should().AllSatisfy(n => n.ItemType.Should().Be(FileSystemItemType.Directory)); treeNodes.Count.Should().Be(1); // only directories @@ -706,8 +723,8 @@ public class DirectoriesControllerTests : IClassFixture DirectoryResponse firstDirectory = directories.First(); - firstDirectory.Name.Should().Be("NestedDirectory_2"); - firstDirectory.Path.Should().Be(Path.Combine(testPath, "NestedDirectory_2" + Path.DirectorySeparatorChar)); + firstDirectory.Name.Should().Be((s_isLinux ? "." : string.Empty) + "NestedDirectory_2"); + firstDirectory.Path.Should().Be(Path.Combine(testPath, (s_isLinux ? "." : string.Empty) + "NestedDirectory_2" + Path.DirectorySeparatorChar)); directories.Count.Should().Be(1); // only directories } finally diff --git a/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/FileManagement/FilesControllerTests.cs b/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/FileManagement/FilesControllerTests.cs index f91b2b22..75f12a90 100644 --- a/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/FileManagement/FilesControllerTests.cs +++ b/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/FileManagement/FilesControllerTests.cs @@ -10,6 +10,7 @@ using System.IO; using System.Linq; using System.Net; using System.Net.Http; +using System.Runtime.InteropServices; using System.Text.Json; using System.Text.Json.Serialization; using System.Threading; @@ -32,6 +33,7 @@ public class FilesControllerTests : IClassFixture PropertyNameCaseInsensitive = true, Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) } }; + private static readonly bool s_isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); #endregion #region ====================================================================== CTOR ===================================================================================== @@ -175,8 +177,8 @@ public class FilesControllerTests : IClassFixture string[] pathSegments = testPath.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries); FileSystemTreeNodeResponse firstDirectory = files.First(); - firstDirectory.Name.Should().Be("TestFile_1.txt"); - firstDirectory.Path.Should().Be(Path.Combine(testPath, "TestFile_1.txt")); + firstDirectory.Name.Should().Be((s_isLinux ? "." : string.Empty) + "TestFile_1.txt"); + firstDirectory.Path.Should().Be(Path.Combine(testPath, (s_isLinux ? "." : string.Empty) + "TestFile_1.txt")); FileSystemTreeNodeResponse secondDirectory = files.Last(); secondDirectory.Name.Should().Be("TestFile_2.txt"); secondDirectory.Path.Should().Be(Path.Combine(testPath, "TestFile_2.txt")); @@ -362,8 +364,8 @@ public class FilesControllerTests : IClassFixture string[] pathSegments = testPath.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries); FileResponse firstDirectory = files.First(); - firstDirectory.Name.Should().Be("TestFile_1.txt"); - firstDirectory.Path.Should().Be(Path.Combine(testPath, "TestFile_1.txt")); + firstDirectory.Name.Should().Be( (s_isLinux ? "." : string.Empty) + "TestFile_1.txt"); + firstDirectory.Path.Should().Be(Path.Combine(testPath, (s_isLinux ? "." : string.Empty) + "TestFile_1.txt")); FileResponse secondDirectory = files.Last(); secondDirectory.Name.Should().Be("TestFile_2.txt"); secondDirectory.Path.Should().Be(Path.Combine(testPath, "TestFile_2.txt")); diff --git a/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/FileManagement/Fixtures/FileSystemStructureFixture.cs b/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/FileManagement/Fixtures/FileSystemStructureFixture.cs index 1f894c91..7dff3a76 100644 --- a/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/FileManagement/Fixtures/FileSystemStructureFixture.cs +++ b/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/FileManagement/Fixtures/FileSystemStructureFixture.cs @@ -57,7 +57,7 @@ public class FileSystemStructureFixture } // make the second nested directory hidden if (i == 2) - SetHidden(currentPath); + currentPath = SetHidden(currentPath); } return currentPath; } @@ -66,7 +66,7 @@ public class FileSystemStructureFixture /// Sets the hidden attribute for a file or directory. /// /// The path to the file or directory. - private static void SetHidden(string path) + private static string SetHidden(string path) { if (OperatingSystem.IsWindows()) File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden); @@ -80,7 +80,9 @@ public class FileSystemStructureFixture Directory.Move(path, newPath); else if (File.Exists(path)) File.Move(path, newPath); + path = newPath; } + return path; } /// diff --git a/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/FileManagement/PathControllerTests.cs b/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/FileManagement/PathControllerTests.cs index 8b78dc02..119d9fb6 100644 --- a/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/FileManagement/PathControllerTests.cs +++ b/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/FileManagement/PathControllerTests.cs @@ -1,14 +1,17 @@ #region ========================================================================= USING ===================================================================================== using FluentAssertions; using Lumina.Contracts.Responses.FileManagement; +using Lumina.Presentation.Api.Core.Controllers.FileManagement; using Lumina.Presentation.Api.IntegrationTests.Common.Setup; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.IO; using System.Linq; using System.Net; using System.Net.Http; +using System.Runtime.InteropServices; using System.Text.Json; using System.Text.Json.Serialization; using System.Threading; @@ -31,6 +34,7 @@ public class PathControllerTests : IClassFixture PropertyNameCaseInsensitive = true, Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) } }; + private static readonly bool s_isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); #endregion #region ====================================================================== CTOR ===================================================================================== @@ -220,7 +224,7 @@ public class PathControllerTests : IClassFixture string path = System.IO.Path.GetDirectoryName(testPath)!; while (!string.IsNullOrEmpty(path)) { - string segment = System.IO.Path.GetFileName(path); + string segment = s_isLinux && path == Path.DirectorySeparatorChar.ToString() ? path : System.IO.Path.GetFileName(path); if (string.IsNullOrEmpty(segment)) { hierarchy.Add(System.IO.Path.GetPathRoot(path)!.TrimEnd(System.IO.Path.DirectorySeparatorChar));