diff --git a/docs/mdsource/temp-directory.source.md b/docs/mdsource/temp-directory.source.md index a116a1864..98cd3ca1d 100644 --- a/docs/mdsource/temp-directory.source.md +++ b/docs/mdsource/temp-directory.source.md @@ -59,6 +59,13 @@ snippet: TempDirectoryDirectoryInfoConversion snippet: TempDirectoryInfoProperty +### Combining paths + +A `TempDirectory` can be combined with a relative `string` suffix using the `+` operator. The directory path and suffix are joined with a single separator (an existing leading separator on the suffix is respected). The result is a `string`. + +snippet: TempDirectoryAddOperator + + ### TempDirectory RootDirectory Property Allows access to the root directory for all TempDirectory instances: diff --git a/docs/temp-directory.md b/docs/temp-directory.md index a8c8bc6f8..e2e8dfd25 100644 --- a/docs/temp-directory.md +++ b/docs/temp-directory.md @@ -146,6 +146,28 @@ public void InfoProperty() +### Combining paths + +A `TempDirectory` can be combined with a relative `string` suffix using the `+` operator. The directory path and suffix are joined with a single separator (an existing leading separator on the suffix is respected). The result is a `string`. + + + +```cs +[Fact] +public void AddOperatorUsage() +{ + using var temp = new TempDirectory(); + + // combine with a file name, joined by a single separator + var filePath = temp + "test.txt"; + + File.WriteAllText(filePath, "content"); +} +``` +snippet source | anchor + + + ### TempDirectory RootDirectory Property Allows access to the root directory for all TempDirectory instances: @@ -191,7 +213,7 @@ public void IgnoreLockedFiles() Directory.Delete(path, true); } ``` -snippet source | anchor +snippet source | anchor @@ -255,7 +277,7 @@ public async Task BuildPath() }); } ``` -snippet source | anchor +snippet source | anchor @@ -277,7 +299,7 @@ public async Task Scrubbing() }); } ``` -snippet source | anchor +snippet source | anchor Result: @@ -322,7 +344,7 @@ public void NoUsing() Debug.WriteLine(temp); } ``` -snippet source | anchor +snippet source | anchor The directory can then be manually inspected. @@ -346,7 +368,7 @@ public void OpenExplorerAndDebug() temp.OpenExplorerAndDebug(); } ``` -snippet source | anchor +snippet source | anchor This method is designed to help debug tests by enabling the inspection of the contents of the temporary directory while the test is paused. It performs two actions: diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 66758bc76..c5e211951 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CA1822;CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters;PolyFillTargetsForNuget;xUnit1051;NU1608;NU1109 - 31.19.1 + 31.20.0 enable preview 1.0.0 diff --git a/src/Verify.Tests/TempDirectoryTests.cs b/src/Verify.Tests/TempDirectoryTests.cs index 7204d49ef..b88ae3f62 100644 --- a/src/Verify.Tests/TempDirectoryTests.cs +++ b/src/Verify.Tests/TempDirectoryTests.cs @@ -264,6 +264,34 @@ public void ToStringOverride() Assert.Equal(temp.Path, temp.ToString()); } + [Fact] + public void AddOperator() + { + using var directory = new TempDirectory(); + + var separator = Path.DirectorySeparatorChar; + + Assert.Equal($"{directory.Path}{separator}test.txt", directory + "test.txt"); + Assert.Equal($"{directory.Path}/test.txt", directory + "/test.txt"); + Assert.Equal($"{directory.Path}\\test.txt", directory + "\\test.txt"); + Assert.Equal(directory.Path, directory + ""); + } + + #region TempDirectoryAddOperator + + [Fact] + public void AddOperatorUsage() + { + using var temp = new TempDirectory(); + + // combine with a file name, joined by a single separator + var filePath = temp + "test.txt"; + + File.WriteAllText(filePath, "content"); + } + + #endregion + #region TempDirectoryOpenExplorerAndDebug [Fact(Explicit = true)] diff --git a/src/Verify/TempDirectory.cs b/src/Verify/TempDirectory.cs index bec8dcbeb..1941f90b9 100644 --- a/src/Verify/TempDirectory.cs +++ b/src/Verify/TempDirectory.cs @@ -266,6 +266,24 @@ public static implicit operator string(TempDirectory temp) => public static implicit operator DirectoryInfo(TempDirectory temp) => new(temp.Path); + /// + /// Combines the directory path with the relative , joined by a single separator. + /// + public static string operator +(TempDirectory directory, string suffix) + { + if (suffix.Length == 0) + { + return directory.Path; + } + + if (suffix[0] is '/' or '\\') + { + return directory.Path + suffix; + } + + return directory.Path + IoPath.DirectorySeparatorChar + suffix; + } + /// /// A represeting this instance. ///