From 272494f796f00db5b8589c6f3dd51b9fa6bb48d9 Mon Sep 17 00:00:00 2001 From: David Guida <1432872+mizrael@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:37:36 -0500 Subject: [PATCH] addressing PR comments --- .../MsApp/MsappArchiveTests.cs | 38 ++++++++++++++----- src/Persistence/Models/ControlProperty.cs | 14 ------- src/Persistence/MsappArchive.cs | 18 ++++++--- src/Persistence/Utils/FileUtils.cs | 17 --------- 4 files changed, 42 insertions(+), 45 deletions(-) delete mode 100644 src/Persistence/Models/ControlProperty.cs delete mode 100644 src/Persistence/Utils/FileUtils.cs diff --git a/src/Persistence.Tests/MsApp/MsappArchiveTests.cs b/src/Persistence.Tests/MsApp/MsappArchiveTests.cs index e84d749d..9e52c6c4 100644 --- a/src/Persistence.Tests/MsApp/MsappArchiveTests.cs +++ b/src/Persistence.Tests/MsApp/MsappArchiveTests.cs @@ -4,7 +4,6 @@ using System.IO.Compression; using Microsoft.PowerPlatform.PowerApps.Persistence; using Microsoft.PowerPlatform.PowerApps.Persistence.Extensions; -using Microsoft.PowerPlatform.PowerApps.Persistence.Utils; namespace Persistence.Tests.MsApp; @@ -46,12 +45,9 @@ public void GetDirectoryEntriesTests(string[] entries, string directoryName, int msappArchive.GetDirectoryEntries(directoryName).Count().Should().Be(expectedDirectoryCount); } - [DataRow(new string[] { "abc.txt" })] - [DataRow(new string[] { "abc.txt", @$"{MsappArchive.Directories.Resources}\abc.txt" })] - [DataRow(new string[] { "abc.txt", @$"{MsappArchive.Directories.Resources}\DEF.txt" })] - [DataRow(new string[] { "abc.txt", @$"{MsappArchive.Directories.Resources}\DEF.txt", @"\start-with-slash\test.json" })] - [TestMethod] - public void AddEntryTests(string[] entries) + [DataTestMethod] + [DynamicData(nameof(AddEntryTestsData), DynamicDataSourceType.Method)] + public void AddEntryTests(string[] entries, string[] expectedEntries) { // Arrange: Create new ZipArchive in memory using var stream = new MemoryStream(); @@ -63,13 +59,37 @@ public void AddEntryTests(string[] entries) // Assert msappArchive.CanonicalEntries.Count.Should().Be(entries.Length); - foreach (var entry in entries) + for (var i = 0; i != expectedEntries.Length; ++i) { - msappArchive.CanonicalEntries.ContainsKey(FileUtils.NormalizePath(entry)).Should().BeTrue(); + msappArchive.CanonicalEntries.ContainsKey(expectedEntries[i]) + .Should() + .BeTrue($"Expected entry {expectedEntries[i]} to exist in the archive"); } // Get the required entry should throw if it doesn't exist var action = () => msappArchive.GetRequiredEntry("not-exist"); action.Invoking(a => a()).Should().Throw(); } + + private static IEnumerable AddEntryTestsData() + { + return new[] { + new [] { + new[] { "abc.txt" }, + new[] { "abc.txt" } + }, + new[]{ + new [] { "abc.txt", @$"{MsappArchive.Directories.Resources}\abc.txt" }, + new [] { "abc.txt", @$"{MsappArchive.Directories.Resources}/abc.txt".ToLowerInvariant() }, + }, + new[]{ + new [] { "abc.txt", @$"{MsappArchive.Directories.Resources}\DEF.txt" }, + new [] { "abc.txt", @$"{MsappArchive.Directories.Resources}/DEF.txt".ToLowerInvariant() }, + }, + new[]{ + new [] { "abc.txt", @$"{MsappArchive.Directories.Resources}\DEF.txt", @"\start-with-slash\test.json" }, + new [] { "abc.txt", @$"{MsappArchive.Directories.Resources}/DEF.txt".ToLowerInvariant(), @"start-with-slash/test.json" }, + } + }; + } } diff --git a/src/Persistence/Models/ControlProperty.cs b/src/Persistence/Models/ControlProperty.cs deleted file mode 100644 index 5fb5c995..00000000 --- a/src/Persistence/Models/ControlProperty.cs +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -namespace Microsoft.PowerPlatform.PowerApps.Persistence.Models; - -// TODO -//internal sealed record ControlProperty -//{ -// public string Name { get; init; } - -// public string? Value { get; init; } - -// public bool IsFormula { get; init; } -//} diff --git a/src/Persistence/MsappArchive.cs b/src/Persistence/MsappArchive.cs index aa0a1f7b..8020112a 100644 --- a/src/Persistence/MsappArchive.cs +++ b/src/Persistence/MsappArchive.cs @@ -4,7 +4,6 @@ using System.IO.Compression; using System.Text; using Microsoft.Extensions.Logging; -using Microsoft.PowerPlatform.PowerApps.Persistence.Utils; namespace Microsoft.PowerPlatform.PowerApps.Persistence; @@ -71,7 +70,7 @@ public MsappArchive(Stream stream, ZipArchiveMode mode, bool leaveOpen, Encoding foreach (var entry in ZipArchive.Entries) { - if (!canonicalEntries.TryAdd(FileUtils.NormalizePath(entry.FullName), entry)) + if (!canonicalEntries.TryAdd(NormalizePath(entry.FullName), entry)) _logger?.LogInformation($"Duplicate entry found in archive: {entry.FullName}"); } @@ -110,7 +109,7 @@ public IEnumerable GetDirectoryEntries(string directoryName, st { _ = directoryName ?? throw new ArgumentNullException(nameof(directoryName)); - directoryName = FileUtils.NormalizePath(directoryName); + directoryName = NormalizePath(directoryName); foreach (var entry in CanonicalEntries) { @@ -130,7 +129,7 @@ public IEnumerable GetDirectoryEntries(string directoryName, st if (string.IsNullOrWhiteSpace(entryName)) return null; - entryName = FileUtils.NormalizePath(entryName); + entryName = NormalizePath(entryName); if (CanonicalEntries.TryGetValue(entryName, out var entry)) return entry; @@ -143,7 +142,7 @@ public ZipArchiveEntry CreateEntry(string entryName) if (string.IsNullOrWhiteSpace(entryName)) throw new ArgumentException("Entry name cannot be null or whitespace.", nameof(entryName)); - var canonicalEntryName = FileUtils.NormalizePath(entryName); + var canonicalEntryName = NormalizePath(entryName); if (_canonicalEntries.Value.ContainsKey(canonicalEntryName)) throw new InvalidOperationException($"Entry {entryName} already exists in the archive."); @@ -155,6 +154,15 @@ public ZipArchiveEntry CreateEntry(string entryName) #endregion + #region Private methods + + private static string NormalizePath(string path) + { + return path.Trim().Replace('\\', '/').Trim('/').ToLowerInvariant(); + } + + #endregion Private methods + #region IDisposable protected virtual void Dispose(bool disposing) diff --git a/src/Persistence/Utils/FileUtils.cs b/src/Persistence/Utils/FileUtils.cs deleted file mode 100644 index 1425a033..00000000 --- a/src/Persistence/Utils/FileUtils.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -namespace Microsoft.PowerPlatform.PowerApps.Persistence.Utils; - -internal static class FileUtils -{ - /// - /// Converts backslashes to forward slashes, removes trailing slashes, and converts to lowercase. - /// - /// - /// - public static string NormalizePath(string path) - { - return path.Trim().Replace('\\', '/').Trim('/').ToLowerInvariant(); - } -}