Skip to content

Commit 682b1da

Browse files
committed
Add the test case before refactoring
1 parent c3a46e6 commit 682b1da

2 files changed

Lines changed: 70 additions & 6 deletions

File tree

tests/Fallout.Utilities.Specs/IO/CompressionTasksSpecs.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,12 @@
66

77
namespace Fallout.Common.Specs;
88

9-
public class CompressionTasksSpecs : FileSystemDependentSpecs
9+
public class CompressionTasksSpecs(ITestOutputHelper testOutputHelper) : FileSystemDependentSpecs(testOutputHelper)
1010
{
1111
private AbsolutePath RootFile => TestTempDirectory / "root-file";
1212

1313
private AbsolutePath NestedFile => TestTempDirectory / "a" / "b" / "c" / "nested-file";
1414

15-
public CompressionTasksSpecs(ITestOutputHelper testOutputHelper)
16-
: base(testOutputHelper)
17-
{
18-
}
19-
2015
[Theory]
2116
[InlineData("archive.zip")]
2217
[InlineData("archive.tar.gz")]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)