Skip to content

Commit

Permalink
(GH-853) Handle null values better
Browse files Browse the repository at this point in the history
When a path is empty or null, handle it with either better error
messaging or work around the issue.

- Do not allow combining paths when the left side is null.
- When attempting to get files from a directory, if the directory value
   is empty, do not perform the search.
- When splitting strings, attempt to always remove empty entries.
- Check null or empty for strings
  • Loading branch information
ferventcoder committed Jul 13, 2016
1 parent ed9d5c9 commit 401335b
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,14 @@ public static void update_environment_variables()

private static IDictionary convert_to_case_insensitive_dictionary(IDictionary originalDictionary)
{
return new Hashtable(originalDictionary, StringComparer.InvariantCultureIgnoreCase);
if (originalDictionary == null) return new Hashtable(new Dictionary<string, string>(), StringComparer.OrdinalIgnoreCase);

return new Hashtable(originalDictionary, StringComparer.OrdinalIgnoreCase);
}

private static void refresh_environment_variables(IDictionary environmentVariables)
{
foreach (DictionaryEntry variable in environmentVariables)
foreach (DictionaryEntry variable in environmentVariables.or_empty_list_if_null())
{
Environment.SetEnvironmentVariable(variable.Key.to_string(), variable.Value.to_string());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ private void uninstall_cleanup(ChocolateyConfiguration config, PackageResult pac

if (config.Force)
{
var packageDirectory = _fileSystem.combine_paths(packageResult.InstallLocation);
var packageDirectory = packageResult.InstallLocation;

if (string.IsNullOrWhiteSpace(packageDirectory) || !_fileSystem.directory_exists(packageDirectory)) return;

Expand Down Expand Up @@ -1010,7 +1010,7 @@ private void move_bad_package_to_failure_location(PackageResult packageResult)
{
_fileSystem.create_directory_if_not_exists(ApplicationParameters.PackageFailuresLocation);

if (packageResult.InstallLocation != null && _fileSystem.directory_exists(packageResult.InstallLocation))
if (!string.IsNullOrWhiteSpace(packageResult.InstallLocation) && _fileSystem.directory_exists(packageResult.InstallLocation))
{
FaultTolerance.try_catch_with_logging_exception(
() => _fileSystem.move_directory(packageResult.InstallLocation, packageResult.InstallLocation.Replace(ApplicationParameters.PackagesLocation, ApplicationParameters.PackageFailuresLocation)),
Expand Down
6 changes: 3 additions & 3 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public ConcurrentDictionary<string, PackageResult> install_run(ChocolateyConfigu

//todo: handle all

SemanticVersion version = config.Version != null ? new SemanticVersion(config.Version) : null;
SemanticVersion version = !string.IsNullOrWhiteSpace(config.Version) ? new SemanticVersion(config.Version) : null;
if (config.Force) config.AllowDowngrade = true;

IList<string> packageNames = config.PackageNames.Split(new[] { ApplicationParameters.PackageNamesSeparator }, StringSplitOptions.RemoveEmptyEntries).or_empty_list_if_null().ToList();
Expand Down Expand Up @@ -513,7 +513,7 @@ public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfigu
_fileSystem.create_directory_if_not_exists(ApplicationParameters.PackagesLocation);
var packageInstalls = new ConcurrentDictionary<string, PackageResult>(StringComparer.InvariantCultureIgnoreCase);

SemanticVersion version = config.Version != null ? new SemanticVersion(config.Version) : null;
SemanticVersion version = !string.IsNullOrWhiteSpace(config.Version) ? new SemanticVersion(config.Version) : null;
if (config.Force) config.AllowDowngrade = true;

var packageManager = NugetCommon.GetPackageManager(
Expand Down Expand Up @@ -1280,7 +1280,7 @@ private void set_package_names_if_all_is_specified(ChocolateyConfiguration confi
if (!string.IsNullOrWhiteSpace(config.UpgradeCommand.PackageNamesToSkip))
{
var packagesToSkip = config.UpgradeCommand.PackageNamesToSkip
.Split(',')
.Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries)
.Where(item => !string.IsNullOrWhiteSpace(item))
.Select(p => p.trim_safe())
.ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static string prompt_for_confirmation(string prompt, IEnumerable<string>
c => c.Count() > 0,
(name, value) => { throw new ApplicationException("No choices passed in. Please ensure you pass choices"); });

if (defaultChoice != null)
if (!string.IsNullOrWhiteSpace(defaultChoice))
{
Ensure
.that(() => choices)
Expand Down Expand Up @@ -117,7 +117,7 @@ public static string prompt_for_confirmation(string prompt, IEnumerable<string>
var selection = timeoutInSeconds == 0 ? Console.ReadLine() : Console.ReadLine(timeoutInSeconds * 1000);
if (shortPrompt) Console.WriteLine();

if (string.IsNullOrWhiteSpace(selection) && defaultChoice != null)
if (string.IsNullOrWhiteSpace(selection) && !string.IsNullOrWhiteSpace(defaultChoice))
{
"chocolatey".Log().Info(ChocolateyLoggers.LogFileOnly, "Choosing default choice of '{0}'".format_with(defaultChoice.escape_curly_braces()));
return defaultChoice;
Expand Down
6 changes: 6 additions & 0 deletions src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ private static IEnvironment Environment

public string combine_paths(string leftItem, params string[] rightItems)
{
if (leftItem == null) throw new ApplicationException("Path to combine cannot be empty.");

var combinedPath = Platform.get_platform() == PlatformType.Windows ? leftItem : leftItem.Replace('\\', '/');
foreach (var rightItem in rightItems)
{
Expand Down Expand Up @@ -166,11 +168,15 @@ public string get_current_assembly_path()

public IEnumerable<string> get_files(string directoryPath, string pattern = "*.*", SearchOption option = SearchOption.TopDirectoryOnly)
{
if (string.IsNullOrWhiteSpace(directoryPath)) return new List<string>();

return Directory.EnumerateFiles(directoryPath, pattern, option);
}

public IEnumerable<string> get_files(string directoryPath, string[] extensions, SearchOption option = SearchOption.TopDirectoryOnly)
{
if (string.IsNullOrWhiteSpace(directoryPath)) return new List<string>();

return Directory.EnumerateFiles(directoryPath, "*.*", option)
.Where(f => extensions.Any(x => f.EndsWith(x, StringComparison.OrdinalIgnoreCase)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static void configure(string outputDirectory = null)

XmlConfigurator.Configure(xmlConfigStream);

if (outputDirectory != null)
if (!string.IsNullOrWhiteSpace(outputDirectory))
{
set_file_appender(outputDirectory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public override Dictionary<string, PSObject> Prompt(string caption, string messa
private static string[] get_hotkey_and_label(string input)
{
var result = new[] { String.Empty, String.Empty };
string[] fragments = input.Split('&');
string[] fragments = input.Split(new char[] {'&'}, StringSplitOptions.RemoveEmptyEntries);
if (fragments.Length == 2)
{
if (fragments[1].Length > 0) result[0] = fragments[1][0].to_string().ToUpper(CultureInfo.CurrentCulture);
Expand Down

0 comments on commit 401335b

Please sign in to comment.