Skip to content

Commit 4f397f8

Browse files
wexmanfgreinacher
authored andcommitted
Do not throw exception when calling DirectoryInfoWrapper.Parent for the root directory (#430)
Fixes #429
1 parent d236bd1 commit 4f397f8

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace System.IO.Abstractions.TestingHelpers.Tests
7+
{
8+
[TestFixture]
9+
public class DirectoryInfoTests
10+
{
11+
[Test]
12+
public void Parent_ForRootDirectory_ShouldReturnNull()
13+
{
14+
var wrapperFilesystem = new FileSystem();
15+
16+
var current = wrapperFilesystem.Directory.GetCurrentDirectory();
17+
var root = wrapperFilesystem.DirectoryInfo.FromDirectoryName(current).Root;
18+
var rootsParent = root.Parent;
19+
Assert.IsNull(rootsParent);
20+
}
21+
}
22+
}

System.IO.Abstractions/DirectoryInfoWrapper.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,17 @@ public override void SetAccessControl(DirectorySecurity directorySecurity)
227227

228228
public override DirectoryInfoBase Parent
229229
{
230-
get { return new DirectoryInfoWrapper(FileSystem, instance.Parent); }
230+
get
231+
{
232+
if (instance.Parent == null)
233+
{
234+
return null;
235+
}
236+
else
237+
{
238+
return new DirectoryInfoWrapper(FileSystem, instance.Parent);
239+
}
240+
}
231241
}
232242

233243
public override DirectoryInfoBase Root

0 commit comments

Comments
 (0)