Check null for env (#8286)

As you see, the implementation of the 3 OS to check PATH is different.
There is not too much to extract. And I tried to move the
"contains a folder path in env:path?" to a method. But that
method requires to pass in env:path, targetapath and path separator for
different OS. This method has low value. And people still must
remember to use this method. So I just added simple null check to the
place I used GetEnvPath.
This commit is contained in:
William Lee 2017-12-22 12:47:33 -08:00 committed by GitHub
parent 15725d7cac
commit 4e5188817b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 9 deletions

View file

@ -48,8 +48,15 @@ namespace Microsoft.DotNet.ShellShim
private bool PackageExecutablePathExists() private bool PackageExecutablePathExists()
{ {
return _environmentProvider var environmentVariable = _environmentProvider
.GetEnvironmentVariable(PathName) .GetEnvironmentVariable(PathName);
if (environmentVariable == null)
{
return false;
}
return environmentVariable
.Split(':').Contains(_packageExecutablePath.Path); .Split(':').Contains(_packageExecutablePath.Path);
} }

View file

@ -49,10 +49,15 @@ namespace Microsoft.DotNet.ShellShim
private bool PackageExecutablePathExists() private bool PackageExecutablePathExists()
{ {
return _environmentProvider.GetEnvironmentVariable(PathName).Split(':') var environmentVariable = _environmentProvider.GetEnvironmentVariable(PathName);
.Contains(_packageExecutablePath.PathWithTilde) ||
_environmentProvider.GetEnvironmentVariable(PathName).Split(':') if (environmentVariable == null)
.Contains(_packageExecutablePath.Path); {
return false;
}
return environmentVariable.Split(':').Contains(_packageExecutablePath.PathWithTilde)
|| environmentVariable.Split(':').Contains(_packageExecutablePath.Path);
} }
public void PrintAddPathInstructionIfPathDoesNotExist() public void PrintAddPathInstructionIfPathDoesNotExist()

View file

@ -40,9 +40,19 @@ namespace Microsoft.DotNet.ShellShim
private bool PackageExecutablePathExists() private bool PackageExecutablePathExists()
{ {
return Environment.GetEnvironmentVariable(PathName, EnvironmentVariableTarget.User).Split(';').Contains(_packageExecutablePath) return EnvironmentVariableConatinsPackageExecutablePath(Environment.GetEnvironmentVariable(PathName, EnvironmentVariableTarget.User))
|| Environment.GetEnvironmentVariable(PathName, EnvironmentVariableTarget.Machine).Split(';').Contains(_packageExecutablePath) || EnvironmentVariableConatinsPackageExecutablePath(Environment.GetEnvironmentVariable(PathName, EnvironmentVariableTarget.Machine))
|| Environment.GetEnvironmentVariable(PathName, EnvironmentVariableTarget.Process).Split(';').Contains(_packageExecutablePath); || EnvironmentVariableConatinsPackageExecutablePath(Environment.GetEnvironmentVariable(PathName, EnvironmentVariableTarget.Process));
}
private bool EnvironmentVariableConatinsPackageExecutablePath(string environmentVariable)
{
if (environmentVariable == null)
{
return false;
}
return environmentVariable.Split(';').Contains(_packageExecutablePath);
} }
public void PrintAddPathInstructionIfPathDoesNotExist() public void PrintAddPathInstructionIfPathDoesNotExist()