Skip to content

Commit

Permalink
addressing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mizrael committed Jan 29, 2024
1 parent 0ccc63b commit 272494f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 45 deletions.
38 changes: 29 additions & 9 deletions src/Persistence.Tests/MsApp/MsappArchiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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<FileNotFoundException>();
}

private static IEnumerable<object[]> 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" },
}
};
}
}
14 changes: 0 additions & 14 deletions src/Persistence/Models/ControlProperty.cs

This file was deleted.

18 changes: 13 additions & 5 deletions src/Persistence/MsappArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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}");
}

Expand Down Expand Up @@ -110,7 +109,7 @@ public IEnumerable<ZipArchiveEntry> GetDirectoryEntries(string directoryName, st
{
_ = directoryName ?? throw new ArgumentNullException(nameof(directoryName));

directoryName = FileUtils.NormalizePath(directoryName);
directoryName = NormalizePath(directoryName);

foreach (var entry in CanonicalEntries)
{
Expand All @@ -130,7 +129,7 @@ public IEnumerable<ZipArchiveEntry> 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;

Expand All @@ -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.");

Expand All @@ -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)
Expand Down
17 changes: 0 additions & 17 deletions src/Persistence/Utils/FileUtils.cs

This file was deleted.

0 comments on commit 272494f

Please sign in to comment.