-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
SetCurrentDirectory.cs
91 lines (79 loc) · 3.52 KB
/
SetCurrentDirectory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;
namespace System.IO.Tests
{
public sealed class Directory_SetCurrentDirectory : FileCleanupTestBase
{
[Fact]
public void Null_Path_Throws_ArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => Directory.SetCurrentDirectory(null));
}
[Fact]
public void Empty_Path_Throws_ArgumentException()
{
Assert.Throws<ArgumentException>(() => Directory.SetCurrentDirectory(string.Empty));
}
[Fact]
public void SetToNonExistentDirectory_ThrowsDirectoryNotFoundException()
{
Assert.Throws<DirectoryNotFoundException>(() => Directory.SetCurrentDirectory(GetTestFilePath()));
}
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void SetToValidOtherDirectory()
{
RemoteExecutor.Invoke(() =>
{
Directory.SetCurrentDirectory(TestDirectory);
if (OperatingSystem.IsMacOS())
{
// On OSX, the temp directory /tmp/ is a symlink to /private/tmp, so setting the current
// directory to a symlinked path will result in GetCurrentDirectory returning the absolute
// path that followed the symlink.
Assert.Equal("/private" + TestDirectory, Directory.GetCurrentDirectory());
}
else
{
Assert.Equal(TestDirectory, Directory.GetCurrentDirectory());
}
Directory.SetCurrentDirectory(Path.GetTempPath());
}).Dispose();
}
public sealed class Directory_SetCurrentDirectory_SymLink : FileSystemTest
{
private static bool CanCreateSymbolicLinksAndRemoteExecutorSupported => MountHelper.CanCreateSymbolicLinks && RemoteExecutor.IsSupported;
[ConditionalFact(nameof(CanCreateSymbolicLinksAndRemoteExecutorSupported))]
public void SetToPathContainingSymLink()
{
RemoteExecutor.Invoke(() =>
{
var path = GetTestFilePath();
var linkPath = GetRandomLinkPath();
Directory.CreateDirectory(path);
Assert.True(MountHelper.CreateSymbolicLink(linkPath, path, isDirectory: true));
// Both the symlink and the target exist
Assert.True(Directory.Exists(path), "path should exist");
Assert.True(Directory.Exists(linkPath), "linkPath should exist");
// Set Current Directory to symlink
Directory.SetCurrentDirectory(linkPath);
if (OperatingSystem.IsWindows())
{
Assert.Equal(linkPath, Directory.GetCurrentDirectory());
}
else if (OperatingSystem.IsMacOS())
{
Assert.Equal("/private" + path, Directory.GetCurrentDirectory());
}
else
{
Assert.Equal(path, Directory.GetCurrentDirectory());
}
Directory.SetCurrentDirectory(Path.GetTempPath());
}).Dispose();
}
}
}
}