Skip to content

Commit

Permalink
Add IsSymbolicLinkTestCases
Browse files Browse the repository at this point in the history
  • Loading branch information
shaopeng-gh committed Apr 16, 2024
1 parent 7b05868 commit 5b97caf
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/Test.UnitTests.Sarif/FileSystemTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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();

string tempFolder = Path.GetTempPath();
string folderTarget = Path.Combine(tempFolder, "symbolicLinkFolderTarget");
string folderSource = Path.Combine(tempFolder, "symbolicLinkFolderSource");

CleanupDirectory(folderTarget);
CleanupDirectory(folderSource);

Directory.CreateDirectory(folderTarget);

CreateSymbolicLink(folderSource, folderTarget, true);

mockFileSystem.IsSymbolicLink(folderSource).Should().BeTrue();
mockFileSystem.IsSymbolicLink(folderTarget).Should().BeFalse();
}

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 CleanupDirectory(string path)
{
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
else if (File.Exists(path))
{
File.Delete(path);
}
}
}
}

0 comments on commit 5b97caf

Please sign in to comment.