Last active
March 31, 2016 16:14
-
-
Save jrolstad/e93ea5dc11a6bcacbf799638718ed072 to your computer and use it in GitHub Desktop.
Sometimes the Windows Path variable exceeds 2048 characters, which can lead to errors in many applications and the Windows OS itself. This is a series of tests to check this and possibly fix.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using NUnit.Framework; | |
namespace PathTests | |
{ | |
[TestFixture] | |
public class PathVariableTests | |
{ | |
[Test] | |
public void PathVariable_IsLessThan2048Characters() | |
{ | |
var pathVariable = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine); | |
Console.WriteLine("Path Variable length is {0} characters",pathVariable.Length); | |
Assert.That(pathVariable.Length,Is.LessThanOrEqualTo(2048)); | |
} | |
[Test] | |
public void PathVariable_DistinctValues_IsLessThan2048Characters() | |
{ | |
var pathVariable = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine); | |
var uniquePaths = pathVariable.Split(';').Distinct().ToList(); | |
var pathWithUniqueOnly = string.Join(";", uniquePaths); | |
Console.WriteLine("Path Variable length is {0} characters", pathVariable.Length); | |
Console.WriteLine("There are {0} unique paths in the path variable", uniquePaths.Count); | |
Console.WriteLine("Path Variable length with unique paths is {0} characters", pathWithUniqueOnly.Length); | |
Assert.That(pathWithUniqueOnly.Length,Is.LessThanOrEqualTo(2048)); | |
} | |
[Test] | |
public void PathVariable_DistinctValuesAndPathExists_IsLessThan2048Characters() | |
{ | |
var pathVariable = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine); | |
var uniquePaths = pathVariable.Split(';') | |
.Distinct() | |
.Where(Directory.Exists) | |
.ToList(); | |
var pathWithUniqueOnly = string.Join(";", uniquePaths); | |
Console.WriteLine("Path Variable length is {0} characters", pathVariable.Length); | |
Console.WriteLine("There are {0} unique paths in the path variable", uniquePaths.Count); | |
Console.WriteLine("Path Variable length with unique paths is {0} characters", pathWithUniqueOnly.Length); | |
Assert.That(pathWithUniqueOnly.Length, Is.LessThanOrEqualTo(2048)); | |
} | |
[Test] | |
public void PathVariable_ListPaths() | |
{ | |
var pathVariable = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine); | |
var uniquePaths = pathVariable.Split(';').Distinct().OrderBy(p=>p).ToList(); | |
uniquePaths.ForEach(Console.WriteLine); | |
} | |
[Test] | |
public void PathVariable_PathsInVaraible_ShouldExist() | |
{ | |
var pathVariable = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine); | |
var pathExistence = pathVariable.Split(';') | |
.Distinct() | |
.Select(p=>new {Path=p,Exists = Directory.Exists(p)}) | |
.ToList(); | |
var maximumPathLength = pathExistence.Select(p => p.Path.Length).Max(); | |
Console.WriteLine("|Exists|{0}|", "Directory".PadRight(maximumPathLength)); | |
foreach (var path in pathExistence) | |
{ | |
Console.WriteLine("|{0}|{1}|", path.Exists.ToString().PadRight(6), path.Path.PadRight(maximumPathLength)); | |
} | |
Assert.That(pathExistence.All(p=>p.Exists),"There is at least 1 path that does not exist"); | |
} | |
[Test] | |
public void PathVariable_SetToUniquePathsThatExist() | |
{ | |
var pathVariable = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine); | |
var pathsThatExist = pathVariable.Split(';') | |
.Distinct() | |
.Where(Directory.Exists) | |
.ToList(); | |
var newPathVariable = string.Join(";", pathsThatExist); | |
Console.WriteLine("Old Path Variable is: \r\n'{0}'",pathVariable); | |
Console.WriteLine("----------------------------------------------------------------------------"); | |
Console.WriteLine("New Path Variable is: \r\n'{0}'", newPathVariable); | |
// Only do this if you want to update the Path | |
//Environment.SetEnvironmentVariable("Path", newPathVariable,EnvironmentVariableTarget.Machine); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment