|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using Fallout.Common.IO; |
| 4 | +using FluentAssertions; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace Fallout.Common.Specs; |
| 8 | + |
| 9 | +public class PathCompressionSpecs : IDisposable |
| 10 | +{ |
| 11 | + private readonly ITestOutputHelper testOutputHelper; |
| 12 | + private readonly AbsolutePath testDirectory = AbsolutePath.Temp(); |
| 13 | + private readonly AbsolutePath uncompressDirectory = AbsolutePath.Temp(); |
| 14 | + private const string ExpectedHash = "516e16d7450ea2d4f75e9cad5878d3fe"; |
| 15 | + private readonly Random random; |
| 16 | + |
| 17 | + public PathCompressionSpecs(ITestOutputHelper testOutputHelper) |
| 18 | + { |
| 19 | + this.testOutputHelper = testOutputHelper; |
| 20 | + testDirectory.CreateDirectory(); |
| 21 | + random = new Random(53463464); |
| 22 | + |
| 23 | + GenerateTestFiles(testDirectory, 100, 5); |
| 24 | + } |
| 25 | + |
| 26 | + [Theory] |
| 27 | + [InlineData("archive.zip")] |
| 28 | + [InlineData("archive.tar.bz2")] |
| 29 | + [InlineData("archive.tar.gz")] |
| 30 | + public void Test(string archiveFile) |
| 31 | + { |
| 32 | + // Arrange |
| 33 | + var archivePath = AbsolutePath.Temp() / archiveFile; |
| 34 | + testDirectory.CompressTo(archivePath); |
| 35 | + |
| 36 | + // Act |
| 37 | + archivePath.UncompressTo(uncompressDirectory); |
| 38 | + |
| 39 | + // Assert |
| 40 | + var hash = uncompressDirectory.GetDirectoryHash(); |
| 41 | + hash.Should().Be(ExpectedHash); |
| 42 | + } |
| 43 | + |
| 44 | + private void GenerateTestFiles(AbsolutePath absolutePath, int fileCount, int maximumNestingLevel) |
| 45 | + { |
| 46 | + for (var i = 0; i < fileCount; i++) |
| 47 | + { |
| 48 | + var nestingLevel = random.Next(maximumNestingLevel + 1); |
| 49 | + var directory = Enumerable.Range(0, nestingLevel) |
| 50 | + .Aggregate(absolutePath, (current, _) => current / $"dir-{random.Next(int.MaxValue)}"); |
| 51 | + |
| 52 | + if (random.Next(15) % 2 == 0) |
| 53 | + { |
| 54 | + testOutputHelper.WriteLine(directory); |
| 55 | + directory.CreateDirectory(); |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + var file = directory / $"file-{random.Next(int.MaxValue)}.txt"; |
| 60 | + file.WriteAllText(random.Next().ToString(), eofLineBreak: false); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public void Dispose() |
| 65 | + { |
| 66 | + testDirectory.DeleteDirectory(); |
| 67 | + uncompressDirectory.DeleteDirectory(); |
| 68 | + } |
| 69 | +} |
0 commit comments