1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.Diagnostics;
6 using System.Runtime.InteropServices;
7 using Xunit;
8 
9 namespace System.IO.Tests
10 {
11     public sealed class Directory_SetCurrentDirectory : RemoteExecutorTestBase
12     {
13         [Fact]
Null_Path_Throws_ArgumentNullException()14         public void Null_Path_Throws_ArgumentNullException()
15         {
16             Assert.Throws<ArgumentNullException>(() => Directory.SetCurrentDirectory(null));
17         }
18 
19         [Fact]
Empty_Path_Throws_ArgumentException()20         public void Empty_Path_Throws_ArgumentException()
21         {
22             Assert.Throws<ArgumentException>(() => Directory.SetCurrentDirectory(string.Empty));
23         }
24 
25         [Fact]
SetToNonExistentDirectory_ThrowsDirectoryNotFoundException()26         public void SetToNonExistentDirectory_ThrowsDirectoryNotFoundException()
27         {
28             Assert.Throws<DirectoryNotFoundException>(() => Directory.SetCurrentDirectory(GetTestFilePath()));
29         }
30 
31         [Fact]
SetToValidOtherDirectory()32         public void SetToValidOtherDirectory()
33         {
34             RemoteInvoke(() =>
35             {
36                 Directory.SetCurrentDirectory(TestDirectory);
37                 // On OSX, the temp directory /tmp/ is a symlink to /private/tmp, so setting the current
38                 // directory to a symlinked path will result in GetCurrentDirectory returning the absolute
39                 // path that followed the symlink.
40                 if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
41                 {
42                     Assert.Equal(TestDirectory, Directory.GetCurrentDirectory());
43                 }
44                 return SuccessExitCode;
45             }).Dispose();
46         }
47 
48         public sealed class Directory_SetCurrentDirectory_SymLink : FileSystemTest
49         {
50             [ConditionalFact(nameof(CanCreateSymbolicLinks))]
SetToPathContainingSymLink()51             public void SetToPathContainingSymLink()
52             {
53                 RemoteInvoke(() =>
54                 {
55                     var path = GetTestFilePath();
56                     var linkPath = GetTestFilePath();
57 
58                     Directory.CreateDirectory(path);
59                     Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true));
60 
61                     // Both the symlink and the target exist
62                     Assert.True(Directory.Exists(path), "path should exist");
63                     Assert.True(Directory.Exists(linkPath), "linkPath should exist");
64 
65                     // Set Current Directory to symlink
66                     string currentDir = Directory.GetCurrentDirectory();
67 
68                     Directory.SetCurrentDirectory(linkPath);
69                     if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
70                     {
71                         Assert.Equal(linkPath, Directory.GetCurrentDirectory());
72                     }
73                     else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
74                     {
75                         Assert.Equal("/private" + path, Directory.GetCurrentDirectory());
76                     }
77                     else
78                     {
79                         Assert.Equal(path, Directory.GetCurrentDirectory());
80                     }
81                     return SuccessExitCode;
82                 }).Dispose();
83             }
84         }
85     }
86 }
87