-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add CreateSymbolicLink support (#871)
* Adds CreateSymbolicLink support Adds a FileSystemInfo extension for wrapping convenience. Adds FEATURE_CREATE_SYMBOLIC_LINK to the build properties. Includes mock implementations and mock tests. Updates the ApiParityTests to no longer ignore CreateSymbolicLink for .net 6.0. Bumps the version in version.json. * Skip path tests on non-Windows platform Co-authored-by: Florian Greinacher <[email protected]>
- Loading branch information
1 parent
df30ba4
commit defd44b
Showing
15 changed files
with
541 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
tests/System.IO.Abstractions.TestingHelpers.Tests/MockDirectorySymlinkTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.Versioning; | ||
using System.Security.AccessControl; | ||
using NUnit.Framework; | ||
|
||
namespace System.IO.Abstractions.TestingHelpers.Tests | ||
{ | ||
using XFS = MockUnixSupport; | ||
|
||
[TestFixture] | ||
public class MockDirectorySymlinkTests | ||
{ | ||
|
||
#if FEATURE_CREATE_SYMBOLIC_LINK | ||
|
||
[Test] | ||
public void MockDirectory_CreateSymbolicLink_ShouldReturnFileSystemInfo() | ||
{ | ||
// Arrange | ||
var fileSystem = new MockFileSystem(); | ||
var pathToTarget = XFS.Path(@"C:\Folder\foo"); | ||
var path = XFS.Path(@"C:\bar"); | ||
fileSystem.AddDirectory(pathToTarget); | ||
|
||
// Act | ||
IFileSystemInfo fileSystemInfo = fileSystem.Directory.CreateSymbolicLink(path, pathToTarget); | ||
|
||
// Assert | ||
Assert.AreEqual(path, fileSystemInfo.FullName); | ||
Assert.AreEqual(pathToTarget, fileSystemInfo.LinkTarget); | ||
} | ||
|
||
[Test] | ||
public void MockDirectory_CreateSymbolicLink_ShouldSucceedFromDirectoryInfo() | ||
{ | ||
// Arrange | ||
var fileSystem = new MockFileSystem(); | ||
var pathToTarget = XFS.Path(@"C:\Folder\foo"); | ||
var path = XFS.Path(@"C:\bar"); | ||
fileSystem.AddDirectory(pathToTarget); | ||
|
||
// Act | ||
fileSystem.Directory.CreateSymbolicLink(path, pathToTarget); | ||
IDirectoryInfo directoryInfo = fileSystem.DirectoryInfo.FromDirectoryName(path); | ||
|
||
// Assert | ||
Assert.AreEqual(path, directoryInfo.FullName); | ||
Assert.AreEqual(pathToTarget, directoryInfo.LinkTarget); | ||
} | ||
|
||
[Test] | ||
public void MockDirectory_CreateSymbolicLink_ShouldFailWithNullPath() | ||
{ | ||
// Arrange | ||
var fileSystem = new MockFileSystem(); | ||
var pathToTarget = XFS.Path(@"C:\Folder\foo"); | ||
fileSystem.AddDirectory(pathToTarget); | ||
|
||
// Act | ||
var ex = Assert.Throws<ArgumentNullException>(() => fileSystem.Directory.CreateSymbolicLink(null, pathToTarget)); | ||
|
||
// Assert | ||
Assert.That(ex.ParamName, Is.EqualTo("path")); | ||
} | ||
|
||
[Test] | ||
public void MockDirectory_CreateSymbolicLink_ShouldFailWithNullTarget() | ||
{ | ||
// Arrange | ||
var fileSystem = new MockFileSystem(); | ||
var path = XFS.Path(@"C:\Folder\foo"); | ||
fileSystem.AddDirectory(path); | ||
|
||
// Act | ||
var ex = Assert.Throws<ArgumentNullException>(() => fileSystem.Directory.CreateSymbolicLink(path, null)); | ||
|
||
// Assert | ||
Assert.That(ex.ParamName, Is.EqualTo("pathToTarget")); | ||
} | ||
|
||
[Test] | ||
public void MockDirectory_CreateSymbolicLink_ShouldFailWithEmptyPath() | ||
{ | ||
// Arrange | ||
var fileSystem = new MockFileSystem(); | ||
var pathToTarget = XFS.Path(@"C:\Folder\foo"); | ||
fileSystem.AddDirectory(pathToTarget); | ||
|
||
// Act | ||
var ex = Assert.Throws<ArgumentException>(() => fileSystem.Directory.CreateSymbolicLink("", pathToTarget)); | ||
|
||
// Assert | ||
Assert.That(ex.ParamName, Is.EqualTo("path")); | ||
} | ||
|
||
[Test] | ||
public void MockDirectory_CreateSymbolicLink_ShouldFailWithEmptyTarget() | ||
{ | ||
// Arrange | ||
var fileSystem = new MockFileSystem(); | ||
string path = XFS.Path(@"C:\Folder\foo"); | ||
fileSystem.AddDirectory(path); | ||
|
||
// Act | ||
var ex = Assert.Throws<ArgumentException>(() => fileSystem.Directory.CreateSymbolicLink(path, "")); | ||
|
||
// Assert | ||
Assert.That(ex.ParamName, Is.EqualTo("pathToTarget")); | ||
} | ||
|
||
[Test] | ||
public void MockDirectory_CreateSymbolicLink_ShouldFailWithIllegalPath() | ||
{ | ||
// Arrange | ||
var fileSystem = new MockFileSystem(); | ||
string pathToTarget = XFS.Path(@"C:\Folder\foo"); | ||
fileSystem.AddDirectory(pathToTarget); | ||
|
||
// Act | ||
var ex = Assert.Throws<ArgumentException>(() => fileSystem.Directory.CreateSymbolicLink(" ", pathToTarget)); | ||
|
||
// Assert | ||
Assert.That(ex.ParamName, Is.EqualTo("path")); | ||
} | ||
|
||
[Test] | ||
public void MockDirectory_CreateSymbolicLink_ShouldFailWithIllegalTarget() | ||
{ | ||
// Arrange | ||
var fileSystem = new MockFileSystem(); | ||
string path = XFS.Path(@"C:\Folder\foo"); | ||
fileSystem.AddDirectory(path); | ||
|
||
// Act | ||
var ex = Assert.Throws<ArgumentException>(() => fileSystem.Directory.CreateSymbolicLink(path, " ")); | ||
|
||
// Assert | ||
Assert.That(ex.ParamName, Is.EqualTo("pathToTarget")); | ||
} | ||
|
||
[Test] | ||
[WindowsOnly(WindowsSpecifics.StrictPathRules)] | ||
public void MockDirectory_CreateSymbolicLink_ShouldFailWithIllegalCharactersInPath() | ||
{ | ||
// Arrange | ||
var fileSystem = new MockFileSystem(); | ||
string pathToTarget = XFS.Path(@"C:\Folder\foo"); | ||
fileSystem.AddDirectory(pathToTarget); | ||
|
||
// Act | ||
TestDelegate ex = () => fileSystem.Directory.CreateSymbolicLink(@"C:\bar_?_", pathToTarget); | ||
|
||
// Assert | ||
Assert.Throws<ArgumentException>(ex); | ||
} | ||
|
||
[Test] | ||
[WindowsOnly(WindowsSpecifics.StrictPathRules)] | ||
public void MockDirectory_CreateSymbolicLink_ShouldFailWithIllegalCharactersInTarget() | ||
{ | ||
// Arrange | ||
var fileSystem = new MockFileSystem(); | ||
string path = XFS.Path(@"C:\foo"); | ||
|
||
// Act | ||
TestDelegate ex = () => fileSystem.Directory.CreateSymbolicLink(path, @"C:\bar_?_"); | ||
|
||
// Assert | ||
Assert.Throws<ArgumentException>(ex); | ||
} | ||
|
||
[Test] | ||
public void MockDirectory_CreateSymbolicLink_ShouldFailIfPathExists() | ||
{ | ||
// Arrange | ||
var fileSystem = new MockFileSystem(); | ||
string pathToTarget = XFS.Path(@"C:\Folder\foo"); | ||
string path = XFS.Path(@"C:\Folder\bar"); | ||
fileSystem.AddDirectory(pathToTarget); | ||
fileSystem.AddDirectory(path); | ||
|
||
// Act | ||
var ex = Assert.Throws<IOException>(() => fileSystem.Directory.CreateSymbolicLink(path, pathToTarget)); | ||
|
||
// Assert | ||
Assert.That(ex.Message.Contains("path")); | ||
} | ||
|
||
[Test] | ||
public void MockDirectory_CreateSymbolicLink_ShouldFailIfTargetDoesNotExist() | ||
{ | ||
// Arrange | ||
var fileSystem = new MockFileSystem(); | ||
string path = XFS.Path(@"C:\Folder\foo"); | ||
string pathToTarget = XFS.Path(@"C:\Target"); | ||
|
||
// Act | ||
var ex = Assert.Throws<FileNotFoundException>(() => fileSystem.Directory.CreateSymbolicLink(path, pathToTarget)); | ||
|
||
// Assert | ||
Assert.That(ex.Message.Contains(pathToTarget)); | ||
} | ||
#endif | ||
} | ||
} |
Oops, something went wrong.