-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathFileSystemTests.cs
More file actions
92 lines (76 loc) · 3.77 KB
/
Copy pathFileSystemTests.cs
File metadata and controls
92 lines (76 loc) · 3.77 KB
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
92
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using FluentAssertions;
using Xunit;
namespace Microsoft.CodeAnalysis.Sarif
{
public class FileSystemTests
{
[Fact]
[Trait(TestTraits.WindowsOnly, "true")]
public void FileSystem_IsSymbolicLinkTestCases()
{
var mockFileSystem = new FileSystem();
mockFileSystem.IsSymbolicLink(Environment.SystemDirectory).Should().BeFalse();
mockFileSystem.IsSymbolicLink(Path.Combine(Environment.SystemDirectory, "notepad.exe")).Should().BeFalse();
string tempFolder = Path.GetTempPath();
string folderTarget = Path.Combine(tempFolder, "symbolicLinkFolderTarget");
string folderSource = Path.Combine(tempFolder, "symbolicLinkFolderSource");
string fileTarget = Path.Combine(tempFolder, "symbolicLinkFileTarget.txt");
string fileSource = Path.Combine(tempFolder, "symbolicLinkFileSource.txt");
CleanupDirectoryOrFile(new string[] { folderTarget, folderSource, fileTarget, fileSource });
Directory.CreateDirectory(folderTarget);
File.WriteAllText(fileTarget, "This is the target file.");
CreateSymbolicLink(folderSource, folderTarget, isDirectory: true);
CreateSymbolicLink(fileSource, fileTarget, isDirectory: false);
mockFileSystem.IsSymbolicLink(folderSource).Should().BeTrue();
mockFileSystem.IsSymbolicLink(folderTarget).Should().BeFalse();
mockFileSystem.IsSymbolicLink(fileSource).Should().BeTrue();
mockFileSystem.IsSymbolicLink(fileTarget).Should().BeFalse();
CleanupDirectoryOrFile(new string[] { folderTarget, folderSource, fileTarget, fileSource });
}
private void CreateSymbolicLink(string linkPath, string targetPath, bool isDirectory)
{
string targetType = isDirectory ? "/D" : "";
var processStartInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/c mklink {targetType} \"{linkPath}\" \"{targetPath}\"",
UseShellExecute = false,
CreateNoWindow = true
};
using (var process = System.Diagnostics.Process.Start(processStartInfo))
{
process.WaitForExit();
}
}
private void CleanupDirectoryOrFile(string[] paths)
{
foreach (string path in paths)
{
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
else if (File.Exists(path))
{
File.Delete(path);
}
}
}
[Fact]
public void PathGetExtension_ShouldExtractExtension()
{
FileSystem.Instance.PathGetExtension("test.txt").Should().Be(".txt");
FileSystem.Instance.PathGetExtension("C:\\test.abcde").Should().Be(".abcde");
FileSystem.Instance.PathGetExtension("C:\\some_dir\\test.yft").Should().Be(".yft");
FileSystem.Instance.PathGetExtension("http://example.com/some.file.ext").Should().Be(".ext");
FileSystem.Instance.PathGetExtension("some.other.dir/extensionless").Should().Be(string.Empty);
FileSystem.Instance.PathGetExtension("yet<another>dir\\with|pipes|file.a").Should().Be(".a");
FileSystem.Instance.PathGetExtension("yet<another>dir\\with|pipes|file.a.").Should().Be(string.Empty);
FileSystem.Instance.PathGetExtension("yet<another>dir\\with.pipes|file").Should().Be(".pipes|file");
}
}
}