Skip to content

Commit

Permalink
Fix for converting the relative path to absolute path for the test so…
Browse files Browse the repository at this point in the history
…urce. (#345)
  • Loading branch information
singhsarab authored and codito committed Jan 19, 2017
1 parent 61af6d7 commit 576309b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public DirectoryInfo CreateDirectory(string path)
return Directory.CreateDirectory(path);
}

/// <inheritdoc/>
public string GetCurrentDirectory()
{
return Directory.GetCurrentDirectory();
}

/// <inheritdoc/>
public bool Exists(string path)
{
Expand Down Expand Up @@ -52,5 +58,7 @@ public FileAttributes GetFileAttributes(string path)
{
return new FileInfo(path).Attributes;
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public interface IFileHelper
/// <returns><see cref="DirectoryInfo"/> for the created directory.</returns>
DirectoryInfo CreateDirectory(string path);

/// <summary>
/// Gets the current directory
/// </summary>
/// <returns>Current directory</returns>
string GetCurrentDirectory();

/// <summary>
/// Exists utility to check if file exists (case sensitive).
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions src/vstest.console/CommandLine/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine
using Utilities.Helpers.Interfaces;

using CommandLineResources = Microsoft.VisualStudio.TestPlatform.CommandLine.Resources.Resources;
using System.IO;

/// <summary>
/// Provides access to the command-line options.
Expand Down Expand Up @@ -253,6 +254,13 @@ public void AddSource(string source)
}

source = source.Trim();

// Convert the relative path to absolute path
if(!Path.IsPathRooted(source))
{
source = Path.Combine(FileHelper.GetCurrentDirectory(), source);
}

if (!FileHelper.Exists(source))
{
throw new CommandLineException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ namespace Microsoft.VisualStudio.TestPlatform.CommandLine.UnitTests.CommandLine
using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.IO;

[TestClass]
public class CommandLineOptionsTests
{
private readonly Mock<IFileHelper> fileHelper;
private readonly string currentDirectory = @"C:\\Temp";

public CommandLineOptionsTests()
{
this.fileHelper = new Mock<IFileHelper>();
CommandLineOptions.Instance.Reset();
CommandLineOptions.Instance.FileHelper = this.fileHelper.Object;
this.fileHelper.Setup(fh => fh.GetCurrentDirectory()).Returns(currentDirectory);
}

[TestMethod]
Expand Down Expand Up @@ -57,6 +60,18 @@ public void CommandLineOptionsAddSourceShouldThrowCommandLineExceptionForNullSou
{
Assert.ThrowsException<CommandLineException>(() => CommandLineOptions.Instance.AddSource(null));
}

[TestMethod]
public void CommandLineOptionsAddSourceShouldConvertRelativePathToAbsolutePath()
{
string relativeTestFilePath = "DummyTestFile.txt";
var absolutePath = Path.Combine(currentDirectory, relativeTestFilePath);
this.fileHelper.Setup(fh => fh.Exists(absolutePath)).Returns(true);

// Pass relative path
CommandLineOptions.Instance.AddSource(relativeTestFilePath);
Assert.IsTrue(CommandLineOptions.Instance.Sources.Contains(absolutePath));
}

[TestMethod]
public void CommandLineOptionsAddSourceShouldThrowCommandLineExceptionForInvalidSource()
Expand All @@ -67,7 +82,7 @@ public void CommandLineOptionsAddSourceShouldThrowCommandLineExceptionForInvalid
[TestMethod]
public void CommandLineOptionsAddSourceShouldAddSourceThrowExceptionIfDuplicateSource()
{
var testFilePath = "DummyTestFile.txt";
var testFilePath = "C:\\DummyTestFile.txt";
this.fileHelper.Setup(fh => fh.Exists(testFilePath)).Returns(true);

CommandLineOptions.Instance.AddSource(testFilePath);
Expand All @@ -78,7 +93,7 @@ public void CommandLineOptionsAddSourceShouldAddSourceThrowExceptionIfDuplicateS
[TestMethod]
public void CommandLineOptionsAddSourceShouldAddSourceForValidSource()
{
string testFilePath = "DummyTestFile.txt";
string testFilePath = "C:\\DummyTestFile.txt";
this.fileHelper.Setup(fh => fh.Exists(testFilePath)).Returns(true);

CommandLineOptions.Instance.AddSource(testFilePath);
Expand Down

0 comments on commit 576309b

Please sign in to comment.