2017-11-27 10:45:43 -08:00
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System ;
using FluentAssertions ;
2018-03-23 12:40:58 -07:00
using Microsoft.DotNet.Cli.Utils ;
2017-12-04 14:13:24 -08:00
using Microsoft.DotNet.Configurer ;
2018-01-13 09:40:48 -08:00
using Microsoft.DotNet.Tools ;
2017-11-27 10:45:43 -08:00
using Microsoft.DotNet.Tools.Test.Utilities ;
using Microsoft.Extensions.DependencyModel.Tests ;
2018-03-23 12:40:58 -07:00
using Moq ;
2017-11-27 10:45:43 -08:00
using Xunit ;
2017-12-04 14:13:24 -08:00
namespace Microsoft.DotNet.ShellShim.Tests
2017-11-27 10:45:43 -08:00
{
public class LinuxEnvironmentPathTests
{
[Fact]
2018-03-23 12:40:58 -07:00
public void GivenPathNotSetItPrintsManualInstructions ( )
2017-11-27 10:45:43 -08:00
{
2018-01-17 19:26:52 -08:00
var reporter = new BufferedReporter ( ) ;
2018-03-23 12:40:58 -07:00
var toolsPath = new BashPathUnderHomeDirectory ( "/home/user" , ".dotnet/tools" ) ;
var pathValue = @"/usr/bin" ;
var provider = new Mock < IEnvironmentProvider > ( MockBehavior . Strict ) ;
provider
. Setup ( p = > p . GetEnvironmentVariable ( "PATH" ) )
. Returns ( pathValue ) ;
var environmentPath = new LinuxEnvironmentPath (
toolsPath ,
2018-01-17 19:26:52 -08:00
reporter ,
2018-03-23 12:40:58 -07:00
provider . Object ,
FileSystemMockBuilder . Empty . File ) ;
2017-11-27 10:45:43 -08:00
2018-03-23 12:40:58 -07:00
environmentPath . PrintAddPathInstructionIfPathDoesNotExist ( ) ;
2017-11-27 10:45:43 -08:00
2018-01-17 19:26:52 -08:00
reporter . Lines . Should ( ) . Equal (
2018-01-13 09:40:48 -08:00
string . Format (
2018-01-28 13:35:04 -08:00
CommonLocalizableStrings . EnvironmentPathLinuxManualInstructions ,
2018-03-23 12:40:58 -07:00
toolsPath . Path ) ) ;
2017-11-27 10:45:43 -08:00
}
[Fact]
2018-03-23 12:40:58 -07:00
public void GivenPathNotSetAndProfileExistsItPrintsLogoutMessage ( )
{
var reporter = new BufferedReporter ( ) ;
var toolsPath = new BashPathUnderHomeDirectory ( "/home/user" , ".dotnet/tools" ) ;
var pathValue = @"/usr/bin" ;
var provider = new Mock < IEnvironmentProvider > ( MockBehavior . Strict ) ;
provider
. Setup ( p = > p . GetEnvironmentVariable ( "PATH" ) )
. Returns ( pathValue ) ;
var environmentPath = new LinuxEnvironmentPath (
toolsPath ,
reporter ,
provider . Object ,
new FileSystemMockBuilder ( )
. AddFile ( LinuxEnvironmentPath . DotnetCliToolsProfilePath , "" )
. Build ( )
. File ) ;
environmentPath . PrintAddPathInstructionIfPathDoesNotExist ( ) ;
reporter . Lines . Should ( ) . Equal ( CommonLocalizableStrings . EnvironmentPathLinuxNeedLogout ) ;
}
[Theory]
[InlineData("/home/user/.dotnet/tools")]
[InlineData("~/.dotnet/tools")]
public void GivenPathSetItPrintsNothing ( string toolsDiretoryOnPath )
2017-11-27 10:45:43 -08:00
{
2018-01-17 19:26:52 -08:00
var reporter = new BufferedReporter ( ) ;
2018-03-23 12:40:58 -07:00
var toolsPath = new BashPathUnderHomeDirectory ( "/home/user" , ".dotnet/tools" ) ;
var pathValue = @"/usr/bin" ;
var provider = new Mock < IEnvironmentProvider > ( MockBehavior . Strict ) ;
provider
. Setup ( p = > p . GetEnvironmentVariable ( "PATH" ) )
. Returns ( pathValue + ":" + toolsDiretoryOnPath ) ;
var environmentPath = new LinuxEnvironmentPath (
toolsPath ,
2018-01-17 19:26:52 -08:00
reporter ,
2018-03-23 12:40:58 -07:00
provider . Object ,
FileSystemMockBuilder . Empty . File ) ;
2017-11-27 10:45:43 -08:00
2018-03-23 12:40:58 -07:00
environmentPath . PrintAddPathInstructionIfPathDoesNotExist ( ) ;
2017-11-27 10:45:43 -08:00
2018-01-17 19:26:52 -08:00
reporter . Lines . Should ( ) . BeEmpty ( ) ;
2017-11-27 10:45:43 -08:00
}
[Fact]
2018-03-23 12:40:58 -07:00
public void GivenPathSetItDoesNotAddPathToEnvironment ( )
2017-11-27 10:45:43 -08:00
{
2018-01-17 19:26:52 -08:00
var reporter = new BufferedReporter ( ) ;
2018-03-23 12:40:58 -07:00
var toolsPath = new BashPathUnderHomeDirectory ( "/home/user" , ".dotnet/tools" ) ;
var pathValue = @"/usr/bin" ;
var provider = new Mock < IEnvironmentProvider > ( MockBehavior . Strict ) ;
var fileSystem = new FileSystemMockBuilder ( ) . Build ( ) . File ;
provider
. Setup ( p = > p . GetEnvironmentVariable ( "PATH" ) )
. Returns ( pathValue + ":" + toolsPath . Path ) ;
var environmentPath = new LinuxEnvironmentPath (
toolsPath ,
2018-01-17 19:26:52 -08:00
reporter ,
2018-03-23 12:40:58 -07:00
provider . Object ,
fileSystem ) ;
environmentPath . AddPackageExecutablePathToUserPath ( ) ;
reporter . Lines . Should ( ) . BeEmpty ( ) ;
fileSystem
. Exists ( LinuxEnvironmentPath . DotnetCliToolsProfilePath )
. Should ( )
. Be ( false ) ;
}
[Fact]
public void GivenPathNotSetItAddsToEnvironment ( )
{
var reporter = new BufferedReporter ( ) ;
var toolsPath = new BashPathUnderHomeDirectory ( "/home/user" , ".dotnet/tools" ) ;
var pathValue = @"/usr/bin" ;
var provider = new Mock < IEnvironmentProvider > ( MockBehavior . Strict ) ;
var fileSystem = new FileSystemMockBuilder ( ) . Build ( ) . File ;
provider
. Setup ( p = > p . GetEnvironmentVariable ( "PATH" ) )
. Returns ( pathValue ) ;
var environmentPath = new LinuxEnvironmentPath (
toolsPath ,
reporter ,
provider . Object ,
fileSystem ) ;
environmentPath . AddPackageExecutablePathToUserPath ( ) ;
reporter . Lines . Should ( ) . BeEmpty ( ) ;
fileSystem
. ReadAllText ( LinuxEnvironmentPath . DotnetCliToolsProfilePath )
. Should ( )
. Be ( $"export PATH=\" $ PATH : { toolsPath . PathWithDollar } \ "" ) ;
2017-11-27 10:45:43 -08:00
}
}
}