Skip to content

Commit 7e46936

Browse files
authored
Fix test failure in Apple OSs due to paths too long and using Ustar which does not support long paths. (#71115)
Co-authored-by: carlossanlop <carlossanlop@users.noreply.github.com>
1 parent fc813dd commit 7e46936

3 files changed

Lines changed: 44 additions & 22 deletions

File tree

src/libraries/System.Formats.Tar/tests/TarEntry/TarEntry.Conversion.Tests.Base.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,6 @@ private void GetExpectedTimestampsFromOriginalPaxOrGnu(TarEntry originalEntry, o
177177

178178
}
179179

180-
protected TarEntry InvokeTarEntryCreationConstructor(TarEntryFormat targetFormat, TarEntryType entryType, string entryName)
181-
=> targetFormat switch
182-
{
183-
TarEntryFormat.V7 => new V7TarEntry(entryType, entryName),
184-
TarEntryFormat.Ustar => new UstarTarEntry(entryType, entryName),
185-
TarEntryFormat.Pax => new PaxTarEntry(entryType, entryName),
186-
TarEntryFormat.Gnu => new GnuTarEntry(entryType, entryName),
187-
_ => throw new FormatException($"Unexpected format: {targetFormat}")
188-
};
189-
190180
protected TarEntry InvokeTarEntryConversionConstructor(TarEntryFormat targetFormat, TarEntry other)
191181
=> targetFormat switch
192182
{

src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.Stream.Tests.cs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,35 +94,56 @@ public void Extract_LinkEntry_TargetOutsideDirectory(TarEntryType entryType)
9494
Assert.Equal(0, Directory.GetFileSystemEntries(root.Path).Count());
9595
}
9696

97-
[ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))]
98-
public void Extract_SymbolicLinkEntry_TargetInsideDirectory() => Extract_LinkEntry_TargetInsideDirectory_Internal(TarEntryType.SymbolicLink);
99-
100-
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.SupportsHardLinkCreation))]
101-
public void Extract_HardLinkEntry_TargetInsideDirectory() => Extract_LinkEntry_TargetInsideDirectory_Internal(TarEntryType.HardLink);
102-
103-
private void Extract_LinkEntry_TargetInsideDirectory_Internal(TarEntryType entryType)
97+
[ConditionalTheory(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))]
98+
[InlineData(TarEntryFormat.Pax)]
99+
[InlineData(TarEntryFormat.Gnu)]
100+
public void Extract_SymbolicLinkEntry_TargetInsideDirectory(TarEntryFormat format) => Extract_LinkEntry_TargetInsideDirectory_Internal(TarEntryType.SymbolicLink, format, null);
101+
102+
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.SupportsHardLinkCreation))]
103+
[InlineData(TarEntryFormat.Pax)]
104+
[InlineData(TarEntryFormat.Gnu)]
105+
public void Extract_HardLinkEntry_TargetInsideDirectory(TarEntryFormat format) => Extract_LinkEntry_TargetInsideDirectory_Internal(TarEntryType.HardLink, format, null);
106+
107+
[ConditionalTheory(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))]
108+
[InlineData(TarEntryFormat.Pax)]
109+
[InlineData(TarEntryFormat.Gnu)]
110+
public void Extract_SymbolicLinkEntry_TargetInsideDirectory_LongBaseDir(TarEntryFormat format) => Extract_LinkEntry_TargetInsideDirectory_Internal(TarEntryType.SymbolicLink, format, new string('a', 99));
111+
112+
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.SupportsHardLinkCreation))]
113+
[InlineData(TarEntryFormat.Pax)]
114+
[InlineData(TarEntryFormat.Gnu)]
115+
public void Extract_HardLinkEntry_TargetInsideDirectory_LongBaseDir(TarEntryFormat format) => Extract_LinkEntry_TargetInsideDirectory_Internal(TarEntryType.HardLink, format, new string('a', 99));
116+
117+
// This test would not pass for the V7 and Ustar formats in some OSs like MacCatalyst, tvOSSimulator and OSX, because the TempDirectory gets created in
118+
// a folder with a path longer than 100 bytes, and those tar formats have no way of handling pathnames and linknames longer than that length.
119+
// The rest of the OSs create the TempDirectory in a path that does not surpass the 100 bytes, so the 'subfolder' parameter gives a chance to extend
120+
// the base directory past that length, to ensure this scenario is tested everywhere.
121+
private void Extract_LinkEntry_TargetInsideDirectory_Internal(TarEntryType entryType, TarEntryFormat format, string subfolder)
104122
{
105123
using TempDirectory root = new TempDirectory();
106124

125+
string baseDir = string.IsNullOrEmpty(subfolder) ? root.Path : Path.Join(root.Path, subfolder);
126+
Directory.CreateDirectory(baseDir);
127+
107128
string linkName = "link";
108129
string targetName = "target";
109-
string targetPath = Path.Join(root.Path, targetName);
130+
string targetPath = Path.Join(baseDir, targetName);
110131

111132
File.Create(targetPath).Dispose();
112133

113134
using MemoryStream archive = new MemoryStream();
114-
using (TarWriter writer = new TarWriter(archive, TarEntryFormat.Ustar, leaveOpen: true))
135+
using (TarWriter writer = new TarWriter(archive, format, leaveOpen: true))
115136
{
116-
UstarTarEntry entry = new UstarTarEntry(entryType, linkName);
137+
TarEntry entry= InvokeTarEntryCreationConstructor(format, entryType, linkName);
117138
entry.LinkName = targetPath;
118139
writer.WriteEntry(entry);
119140
}
120141

121142
archive.Seek(0, SeekOrigin.Begin);
122143

123-
TarFile.ExtractToDirectory(archive, root.Path, overwriteFiles: false);
144+
TarFile.ExtractToDirectory(archive, baseDir, overwriteFiles: false);
124145

125-
Assert.Equal(2, Directory.GetFileSystemEntries(root.Path).Count());
146+
Assert.Equal(2, Directory.GetFileSystemEntries(baseDir).Count());
126147
}
127148
}
128149
}

src/libraries/System.Formats.Tar/tests/TarTestsBase.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,5 +323,16 @@ protected Type GetTypeForFormat(TarEntryFormat expectedFormat)
323323
_ => throw new FormatException($"Unrecognized format: {expectedFormat}"),
324324
};
325325
}
326+
327+
protected TarEntry InvokeTarEntryCreationConstructor(TarEntryFormat targetFormat, TarEntryType entryType, string entryName)
328+
=> targetFormat switch
329+
{
330+
TarEntryFormat.V7 => new V7TarEntry(entryType, entryName),
331+
TarEntryFormat.Ustar => new UstarTarEntry(entryType, entryName),
332+
TarEntryFormat.Pax => new PaxTarEntry(entryType, entryName),
333+
TarEntryFormat.Gnu => new GnuTarEntry(entryType, entryName),
334+
_ => throw new FormatException($"Unexpected format: {targetFormat}")
335+
};
336+
326337
}
327338
}

0 commit comments

Comments
 (0)