diff --git a/src/chocolatey.console/Program.cs b/src/chocolatey.console/Program.cs index 67f50cc3f1..9490441e10 100644 --- a/src/chocolatey.console/Program.cs +++ b/src/chocolatey.console/Program.cs @@ -194,7 +194,7 @@ require admin rights. Only advanced users should run choco w/out an elevated shell. When you open the command shell, you should ensure that you do so with ""Run as Administrator"" selected. - Do you want to continue?", new[] {"yes", "no"}, "no", requireAnswer: true); + Do you want to continue?", new[] {"yes", "no"}, requireAnswer: true); if (selection.is_equal_to("no")) { diff --git a/src/chocolatey.tests/infrastructure/commandline/InteractivePromptSpecs.cs b/src/chocolatey.tests/infrastructure/commandline/InteractivePromptSpecs.cs index 0b3c4eaf66..a5dfe85f30 100644 --- a/src/chocolatey.tests/infrastructure/commandline/InteractivePromptSpecs.cs +++ b/src/chocolatey.tests/infrastructure/commandline/InteractivePromptSpecs.cs @@ -54,7 +54,7 @@ public class when_prompting_with_interactivePrompt : InteractivePromptSpecsBase public override void Because() { console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed - prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices, default_choice, requireAnswer: false); + prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices, requireAnswer: false); } [Fact] @@ -117,40 +117,15 @@ public void should_error_when_the_prompt_input_is_null() errored.ShouldBeTrue(); console.Verify(c => c.ReadLine(), Times.Never); } - - [Fact] - public void should_error_when_the_default_choice_is_not_in_list() - { - choices = new List {"bob"}; - default_choice = "maybe"; - bool errored = false; - string errorMessage = string.Empty; - console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed - string result = null; - try - { - result = prompt(); - } - catch (Exception ex) - { - errored = true; - errorMessage = ex.Message; - } - - result.ShouldNotEqual("maybe"); - errored.ShouldBeTrue(); - errorMessage.ShouldEqual("Default choice value must be one of the given choices."); - console.Verify(c => c.ReadLine(), Times.Never); - } } - public class when_prompting_with_interactivePrompt_without_default_and_answer_is_not_required : InteractivePromptSpecsBase + public class when_prompting_with_interactivePrompt_and_answer_is_not_required : InteractivePromptSpecsBase { private Func prompt; public override void Because() { - prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices, null, requireAnswer: false); + prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices, requireAnswer: false); } public override void AfterObservations() @@ -232,13 +207,13 @@ public void should_return_null_choice_when_alphabetical_characters_are_given() } } - public class when_prompting_with_interactivePrompt_without_default_and_answer_is_required : InteractivePromptSpecsBase + public class when_prompting_with_interactivePrompt_and_answer_is_required : InteractivePromptSpecsBase { private Func prompt; public override void Because() { - prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices, null, requireAnswer: true); + prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices, requireAnswer: true); } public override void AfterObservations() @@ -299,143 +274,5 @@ public void should_error_when_any_choice_not_available_is_given() console.Verify(c => c.ReadLine(), Times.AtLeast(8)); } } - - public class when_prompting_with_interactivePrompt_with_default_and_answer_is_not_required : InteractivePromptSpecsBase - { - private Func prompt; - - public override void Because() - { - prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices, choices[1], requireAnswer: false); - } - - public override void AfterObservations() - { - base.AfterObservations(); - should_have_called_Console_ReadLine(); - } - - [Fact] - public void should_return_default_when_no_answer_given() - { - console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed - var result = prompt(); - result.ShouldEqual(choices[1]); - } - - [Fact] - public void should_return_first_choice_when_1_is_given() - { - console.Setup(c => c.ReadLine()).Returns("1"); - var result = prompt(); - result.ShouldEqual(choices[0]); - } - - [Fact] - public void should_return_second_choice_when_2_is_given() - { - console.Setup(c => c.ReadLine()).Returns("2"); - var result = prompt(); - result.ShouldEqual(choices[1]); - } - - [Fact] - public void should_return_null_choice_when_3_is_given() - { - console.Setup(c => c.ReadLine()).Returns("3"); - var result = prompt(); - result.ShouldBeNull(); - } - - [Fact] - public void should_return_null_choice_when_4_is_given() - { - console.Setup(c => c.ReadLine()).Returns("4"); - var result = prompt(); - result.ShouldBeNull(); - } - - [Fact] - public void should_return_null_choice_when_0_is_given() - { - console.Setup(c => c.ReadLine()).Returns("0"); - var result = prompt(); - result.ShouldBeNull(); - } - - [Fact] - public void should_return_null_choice_when_negative_1_is_given() - { - console.Setup(c => c.ReadLine()).Returns("-1"); - var result = prompt(); - result.ShouldBeNull(); - } - - [Fact] - public void should_return_null_choice_when_alphabetical_characters_are_given() - { - console.Setup(c => c.ReadLine()).Returns("abc"); - var result = prompt(); - result.ShouldBeNull(); - } - } - - public class when_prompting_with_interactivePrompt_with_default_and_answer_is_required : InteractivePromptSpecsBase - { - private Func prompt; - - public override void Because() - { - prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices, choices[0], requireAnswer: true); - } - - public override void AfterObservations() - { - base.AfterObservations(); - should_have_called_Console_ReadLine(); - } - - [Fact] - public void should_error_when_no_answer_given() - { - console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed - var result = prompt(); - result.ShouldEqual(choices[0]); - } - - [Fact] - public void should_return_first_choice_when_1_is_given() - { - console.Setup(c => c.ReadLine()).Returns("1"); - var result = prompt(); - result.ShouldEqual(choices[0]); - } - - [Fact] - public void should_return_second_choice_when_2_is_given() - { - console.Setup(c => c.ReadLine()).Returns("2"); - var result = prompt(); - result.ShouldEqual(choices[1]); - } - - [Fact] - public void should_error_when_any_choice_not_available_is_given() - { - bool errored = false; - - console.Setup(c => c.ReadLine()).Returns("3"); //Enter pressed - try - { - prompt(); - } - catch (Exception) - { - errored = true; - } - errored.ShouldBeTrue(); - console.Verify(c => c.ReadLine(), Times.AtLeast(8)); - } - } } } \ No newline at end of file diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index d7802d03b1..0408a9c374 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -618,7 +618,7 @@ private void rollback_previous_version(ChocolateyConfiguration config, PackageRe var rollback = true; if (config.PromptForConfirmation) { - var selection = InteractivePrompt.prompt_for_confirmation(" Unsuccessful operation for {0}.{1} Do you want to rollback to previous version (package files only)?".format_with(packageResult.Name, Environment.NewLine), new[] { "yes", "no" }, "yes", requireAnswer: true); + var selection = InteractivePrompt.prompt_for_confirmation(" Unsuccessful operation for {0}.{1} Do you want to rollback to previous version (package files only)?".format_with(packageResult.Name, Environment.NewLine), new[] { "yes", "no" }, requireAnswer: true); if (selection.is_equal_to("no")) rollback = false; } diff --git a/src/chocolatey/infrastructure.app/services/NugetService.cs b/src/chocolatey/infrastructure.app/services/NugetService.cs index cf28d204b5..de67225fe3 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -816,7 +816,7 @@ public ConcurrentDictionary uninstall_run(ChocolateyConfi choices.Add(allVersionsChoice); } - var selection = InteractivePrompt.prompt_for_confirmation("Which version of {0} would you like to uninstall?".format_with(packageName), choices, abortChoice, true); + var selection = InteractivePrompt.prompt_for_confirmation("Which version of {0} would you like to uninstall?".format_with(packageName), choices, true); if (string.IsNullOrWhiteSpace(selection)) continue; if (selection.is_equal_to(abortChoice)) continue; diff --git a/src/chocolatey/infrastructure.app/services/PowershellService.cs b/src/chocolatey/infrastructure.app/services/PowershellService.cs index 23ec61ecd7..5ebc2865c0 100644 --- a/src/chocolatey/infrastructure.app/services/PowershellService.cs +++ b/src/chocolatey/infrastructure.app/services/PowershellService.cs @@ -210,7 +210,7 @@ Do you want to run the script? fail. Skip is an advanced option and most likely will never be wanted. " - , new[] {"yes", "no", "skip"}, "no", requireAnswer: true); + , new[] {"yes", "no", "skip"}, requireAnswer: true); if (selection.is_equal_to("yes")) shouldRun = true; if (selection.is_equal_to("no")) { diff --git a/src/chocolatey/infrastructure/commandline/InteractivePrompt.cs b/src/chocolatey/infrastructure/commandline/InteractivePrompt.cs index 4f5eed890b..3aad9e5e05 100644 --- a/src/chocolatey/infrastructure/commandline/InteractivePrompt.cs +++ b/src/chocolatey/infrastructure/commandline/InteractivePrompt.cs @@ -39,7 +39,7 @@ private static IConsole Console get { return _console.Value; } } - public static string prompt_for_confirmation(string prompt, IEnumerable choices, string defaultChoice, bool requireAnswer, int repeat = 10) + public static string prompt_for_confirmation(string prompt, IEnumerable choices, bool requireAnswer, int repeat = 10) { if (repeat < 0) throw new ApplicationException("Too many bad attempts. Stopping before application crash."); Ensure.that(() => prompt).is_not_null(); @@ -49,14 +49,6 @@ public static string prompt_for_confirmation(string prompt, IEnumerable .meets( c => c.Count() > 0, (name, value) => { throw new ApplicationException("No choices passed in. Please ensure you pass choices"); }); - if (defaultChoice != null) - { - Ensure - .that(() => choices) - .meets( - c => c.Contains(defaultChoice), - (name, value) => { throw new ApplicationException("Default choice value must be one of the given choices."); }); - } "chocolatey".Log().Info(ChocolateyLoggers.Important, prompt); @@ -65,17 +57,12 @@ public static string prompt_for_confirmation(string prompt, IEnumerable foreach (var choice in choices.or_empty_list_if_null()) { choiceDictionary.Add(counter, choice); - "chocolatey".Log().Info(" {0}) {1}{2}".format_with(counter, choice.to_string(), choice == defaultChoice ? " [Default - Press Enter]" : "")); + "chocolatey".Log().Info(" {0}) {1}".format_with(counter, choice.to_string())); counter++; } var selection = Console.ReadLine(); - if (string.IsNullOrWhiteSpace(selection) && defaultChoice != null) - { - return defaultChoice; - } - int selected = -1; if (!int.TryParse(selection, out selected) || selected <= 0 || selected > (counter - 1)) @@ -98,7 +85,7 @@ public static string prompt_for_confirmation(string prompt, IEnumerable if (requireAnswer) { "chocolatey".Log().Warn(ChocolateyLoggers.Important, "You must select an answer"); - return prompt_for_confirmation(prompt, choices, defaultChoice, requireAnswer, repeat - 1); + return prompt_for_confirmation(prompt, choices, requireAnswer, repeat - 1); } return null; }