Skip to content

Commit 9cea231

Browse files
Surayya Huseyn ZadaSurayya Huseyn Zada
authored andcommitted
if ArchiveOutputPath does't have file extension then treat it as directory; add ArchiveFileRegistryTests
1 parent aef3546 commit 9cea231

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

src/Containers/Microsoft.NET.Build.Containers/LocalDaemons/ArchiveFileRegistry.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@ public ArchiveFileRegistry(string archiveOutputPath)
1414
ArchiveOutputPath = archiveOutputPath;
1515
}
1616

17-
private async Task LoadAsync<T>(T image, SourceImageReference sourceReference,
17+
internal async Task LoadAsync<T>(T image, SourceImageReference sourceReference,
1818
DestinationImageReference destinationReference, CancellationToken cancellationToken,
1919
Func<T, SourceImageReference, DestinationImageReference, Stream, CancellationToken, Task> writeStreamFunc)
2020
{
2121
var fullPath = Path.GetFullPath(ArchiveOutputPath);
2222

23+
var directorySeparatorChar = Path.DirectorySeparatorChar;
24+
25+
// if doesn't end with a file extension, assume it's a directory
26+
if (!fullPath.Split(directorySeparatorChar).Last().Contains('.'))
27+
{
28+
fullPath += Path.DirectorySeparatorChar;
29+
}
30+
2331
// pointing to a directory? -> append default name
24-
if (Directory.Exists(fullPath) || ArchiveOutputPath.EndsWith("/") || ArchiveOutputPath.EndsWith("\\"))
32+
if (fullPath.EndsWith(directorySeparatorChar))
2533
{
2634
fullPath = Path.Combine(fullPath, destinationReference.Repository + ".tar.gz");
2735
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.NET.Build.Containers.LocalDaemons;
5+
6+
namespace Microsoft.NET.Build.Containers.IntegrationTests;
7+
8+
public class ArchiveFileRegistryTests
9+
{
10+
[Fact]
11+
public async Task ArchiveOutputPathIsExistingDirectory_CreatesFileWithRepositoryNameAndTarGz()
12+
{
13+
string archiveOutputPath = TestSettings.TestArtifactsDirectory;
14+
string expectedCreatedFilePath = Path.Combine(TestSettings.TestArtifactsDirectory, "repository.tar.gz");
15+
16+
await CreateRegistryAndCallLoadAsync(archiveOutputPath).ConfigureAwait(false);
17+
18+
Assert.True(File.Exists(expectedCreatedFilePath));
19+
}
20+
21+
[Theory]
22+
[InlineData(true)]
23+
[InlineData(false)]
24+
public async Task ArchiveOutputPathIsNonExistingDirectory_CreatesDirectoryAndFileWithRepositoryNameAndTarGz(bool includeDirectorySeperatorAtTheEnd)
25+
{
26+
string archiveOutputPath = Path.Combine(
27+
TestSettings.TestArtifactsDirectory,
28+
"nonexisting" + (includeDirectorySeperatorAtTheEnd ? Path.DirectorySeparatorChar : ""));
29+
string expectedCreatedFilePath = Path.Combine(archiveOutputPath, "repository.tar.gz");
30+
31+
await CreateRegistryAndCallLoadAsync(archiveOutputPath).ConfigureAwait(false);
32+
33+
Assert.True(File.Exists(expectedCreatedFilePath));
34+
}
35+
36+
[Fact]
37+
public async Task ArchiveOutputPathIsCustomFileNameInExistingDirectory_CreatesFileWithThatName()
38+
{
39+
string archiveOutputPath = Path.Combine(TestSettings.TestArtifactsDirectory, "custom-name.withextension");
40+
string expectedCreatedFilePath = archiveOutputPath;
41+
42+
await CreateRegistryAndCallLoadAsync(archiveOutputPath).ConfigureAwait(false);
43+
44+
Assert.True(File.Exists(expectedCreatedFilePath));
45+
}
46+
47+
[Fact]
48+
public async Task ArchiveOutputPathIsCustomFileNameInNonExistingDirectory_CreatesDirectoryAndFileWithThatName()
49+
{
50+
string archiveOutputPath = Path.Combine(TestSettings.TestArtifactsDirectory, $"nonexisting-directory{Path.AltDirectorySeparatorChar}custom-name.withextension");
51+
string expectedCreatedFilePath = archiveOutputPath;
52+
53+
await CreateRegistryAndCallLoadAsync(archiveOutputPath).ConfigureAwait(false);
54+
55+
Assert.True(File.Exists(expectedCreatedFilePath));
56+
}
57+
58+
private async Task CreateRegistryAndCallLoadAsync(string archiveOutputPath)
59+
{
60+
var registry = new ArchiveFileRegistry(archiveOutputPath);
61+
var destinationImageReference = new DestinationImageReference(registry, "repository", ["tag"]);
62+
63+
await registry.LoadAsync(
64+
"test image",
65+
new SourceImageReference(),
66+
destinationImageReference,
67+
CancellationToken.None,
68+
async (img, srcRef, destRef, stream, token) =>
69+
{
70+
await Task.CompletedTask;
71+
}).ConfigureAwait(false);
72+
}
73+
}

0 commit comments

Comments
 (0)