Skip to content

Commit b658b0f

Browse files
committed
Add TempDirectory + string operator for combining paths
Only TempDirectory + relative string suffix is meaningful, since TempFile.Path and TempDirectory.Path are both full paths. Combining two full paths (or prepending to one) produces nonsense, so those overloads are omitted.
1 parent 4cde3fb commit b658b0f

4 files changed

Lines changed: 80 additions & 5 deletions

File tree

docs/mdsource/temp-directory.source.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ snippet: TempDirectoryDirectoryInfoConversion
5959
snippet: TempDirectoryInfoProperty
6060

6161

62+
### Combining paths
63+
64+
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`.
65+
66+
snippet: TempDirectoryAddOperator
67+
68+
6269
### TempDirectory RootDirectory Property
6370

6471
Allows access to the root directory for all TempDirectory instances:

docs/temp-directory.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,28 @@ public void InfoProperty()
146146
<!-- endSnippet -->
147147

148148

149+
### Combining paths
150+
151+
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`.
152+
153+
<!-- snippet: TempDirectoryAddOperator -->
154+
<a id='snippet-TempDirectoryAddOperator'></a>
155+
```cs
156+
[Fact]
157+
public void AddOperatorUsage()
158+
{
159+
using var temp = new TempDirectory();
160+
161+
// combine with a file name, joined by a single separator
162+
var filePath = temp + "test.txt";
163+
164+
File.WriteAllText(filePath, "content");
165+
}
166+
```
167+
<sup><a href='/src/Verify.Tests/TempDirectoryTests.cs#L280-L293' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempDirectoryAddOperator' title='Start of snippet'>anchor</a></sup>
168+
<!-- endSnippet -->
169+
170+
149171
### TempDirectory RootDirectory Property
150172

151173
Allows access to the root directory for all TempDirectory instances:
@@ -191,7 +213,7 @@ public void IgnoreLockedFiles()
191213
Directory.Delete(path, true);
192214
}
193215
```
194-
<sup><a href='/src/Verify.Tests/TempDirectoryTests.cs#L340-L364' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempDirectoryIgnoreLockedFiles' title='Start of snippet'>anchor</a></sup>
216+
<sup><a href='/src/Verify.Tests/TempDirectoryTests.cs#L368-L392' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempDirectoryIgnoreLockedFiles' title='Start of snippet'>anchor</a></sup>
195217
<!-- endSnippet -->
196218

197219

@@ -255,7 +277,7 @@ public async Task BuildPath()
255277
});
256278
}
257279
```
258-
<sup><a href='/src/Verify.Tests/TempDirectoryTests.cs#L299-L319' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempDirectoryBuildPath' title='Start of snippet'>anchor</a></sup>
280+
<sup><a href='/src/Verify.Tests/TempDirectoryTests.cs#L327-L347' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempDirectoryBuildPath' title='Start of snippet'>anchor</a></sup>
259281
<!-- endSnippet -->
260282

261283

@@ -277,7 +299,7 @@ public async Task Scrubbing()
277299
});
278300
}
279301
```
280-
<sup><a href='/src/Verify.Tests/TempDirectoryTests.cs#L321-L336' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempDirectoryScrubbing' title='Start of snippet'>anchor</a></sup>
302+
<sup><a href='/src/Verify.Tests/TempDirectoryTests.cs#L349-L364' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempDirectoryScrubbing' title='Start of snippet'>anchor</a></sup>
281303
<!-- endSnippet -->
282304

283305
Result:
@@ -322,7 +344,7 @@ public void NoUsing()
322344
Debug.WriteLine(temp);
323345
}
324346
```
325-
<sup><a href='/src/Verify.Tests/TempDirectoryTests.cs#L282-L295' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempDirectoryNoUsing' title='Start of snippet'>anchor</a></sup>
347+
<sup><a href='/src/Verify.Tests/TempDirectoryTests.cs#L310-L323' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempDirectoryNoUsing' title='Start of snippet'>anchor</a></sup>
326348
<!-- endSnippet -->
327349

328350
The directory can then be manually inspected.
@@ -346,7 +368,7 @@ public void OpenExplorerAndDebug()
346368
temp.OpenExplorerAndDebug();
347369
}
348370
```
349-
<sup><a href='/src/Verify.Tests/TempDirectoryTests.cs#L267-L280' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempDirectoryOpenExplorerAndDebug' title='Start of snippet'>anchor</a></sup>
371+
<sup><a href='/src/Verify.Tests/TempDirectoryTests.cs#L295-L308' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempDirectoryOpenExplorerAndDebug' title='Start of snippet'>anchor</a></sup>
350372
<!-- endSnippet -->
351373

352374
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:

src/Verify.Tests/TempDirectoryTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,34 @@ public void ToStringOverride()
264264
Assert.Equal(temp.Path, temp.ToString());
265265
}
266266

267+
[Fact]
268+
public void AddOperator()
269+
{
270+
using var directory = new TempDirectory();
271+
272+
var separator = Path.DirectorySeparatorChar;
273+
274+
Assert.Equal($"{directory.Path}{separator}test.txt", directory + "test.txt");
275+
Assert.Equal($"{directory.Path}/test.txt", directory + "/test.txt");
276+
Assert.Equal($"{directory.Path}\\test.txt", directory + "\\test.txt");
277+
Assert.Equal(directory.Path, directory + "");
278+
}
279+
280+
#region TempDirectoryAddOperator
281+
282+
[Fact]
283+
public void AddOperatorUsage()
284+
{
285+
using var temp = new TempDirectory();
286+
287+
// combine with a file name, joined by a single separator
288+
var filePath = temp + "test.txt";
289+
290+
File.WriteAllText(filePath, "content");
291+
}
292+
293+
#endregion
294+
267295
#region TempDirectoryOpenExplorerAndDebug
268296

269297
[Fact(Explicit = true)]

src/Verify/TempDirectory.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,24 @@ public static implicit operator string(TempDirectory temp) =>
266266
public static implicit operator DirectoryInfo(TempDirectory temp) =>
267267
new(temp.Path);
268268

269+
/// <summary>
270+
/// Combines the directory path with the relative <paramref name="suffix"/>, joined by a single separator.
271+
/// </summary>
272+
public static string operator +(TempDirectory directory, string suffix)
273+
{
274+
if (suffix.Length == 0)
275+
{
276+
return directory.Path;
277+
}
278+
279+
if (suffix[0] is '/' or '\\')
280+
{
281+
return directory.Path + suffix;
282+
}
283+
284+
return directory.Path + IoPath.DirectorySeparatorChar + suffix;
285+
}
286+
269287
/// <summary>
270288
/// A <see cref="DirectoryInfo"/> represeting this instance.
271289
/// </summary>

0 commit comments

Comments
 (0)