Skip to content

Commit

Permalink
Merge pull request #4665 from Meet2rohit99/Issue4353
Browse files Browse the repository at this point in the history
Add Prefix in TestContext.AddTestAttachment for long file paths
  • Loading branch information
stevenaw committed Mar 19, 2024
2 parents 95cd3b3 + 11d612d commit 2f9d052
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/NUnitFramework/framework/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,13 @@ public static void AddTestAttachment(string filePath, string? description = null
if (!Path.IsPathRooted(filePath))
filePath = Path.Combine(TestContext.CurrentContext.WorkDirectory, filePath);

if (!File.Exists(filePath))
string prefix = string.Empty;
#if NETFRAMEWORK
if (Environment.OSVersion.Platform == PlatformID.Win32NT & filePath.Length > 260)
prefix = @"\\?\";
#endif

if (!File.Exists($"{prefix}{filePath}"))
throw new FileNotFoundException("Test attachment file path could not be found.", filePath);

var result = TestExecutionContext.CurrentContext.CurrentResult;
Expand Down
25 changes: 24 additions & 1 deletion src/NUnitFramework/tests/TestContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class TestContextTests
[OneTimeSetUp]
public void CreateTempFile()
{
_tempFilePath = Path.Combine(TestContext.CurrentContext.WorkDirectory, TempFileName);
_tempFilePath = Path.Combine(_workDirectory, TempFileName);
File.Create(_tempFilePath).Dispose();
}

Expand Down Expand Up @@ -386,6 +386,29 @@ public void NoneExistentFileThrowsFileNotFoundException()
Assert.That(() => TestContext.AddTestAttachment("NotAFile.txt"), Throws.InstanceOf<FileNotFoundException>());
}

[TestCase(IncludePlatform = "Net")]
public void LogFilePathDoesNotThrow()
{
string longPathPrefix = string.Empty;

if (Environment.OSVersion.Platform == PlatformID.Win32NT)
longPathPrefix = $@"\\?\";

/* Absolute file path length greater than 260 are considered long paths for .NET Framework
* Creating 261 length file path */
string tempLongFilePath = Path.Combine(_workDirectory, new string('A', (260 - _workDirectory.Length) - 4) + ".tmp");
try
{
File.Create($"{longPathPrefix}{tempLongFilePath}").Dispose();

Assert.That(() => TestContext.AddTestAttachment(tempLongFilePath), Throws.Nothing);
}
finally
{
File.Delete($"{longPathPrefix}{tempLongFilePath}");
}
}

#endregion

#region Retry
Expand Down

0 comments on commit 2f9d052

Please sign in to comment.