Skip to content

Commit

Permalink
(GH-185) Remove default choice from `InteractivePrompt.prompt_for_con…
Browse files Browse the repository at this point in the history
…firmation` and all associated tests
  • Loading branch information
christianrondeau committed Mar 24, 2015
1 parent 1d1ee1d commit 64581ac
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 188 deletions.
2 changes: 1 addition & 1 deletion src/chocolatey.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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<string> {"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<string> 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()
Expand Down Expand Up @@ -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<string> 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()
Expand Down Expand Up @@ -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<string> 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<string> 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));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ public ConcurrentDictionary<string, PackageResult> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand Down
19 changes: 3 additions & 16 deletions src/chocolatey/infrastructure/commandline/InteractivePrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private static IConsole Console
get { return _console.Value; }
}

public static string prompt_for_confirmation(string prompt, IEnumerable<string> choices, string defaultChoice, bool requireAnswer, int repeat = 10)
public static string prompt_for_confirmation(string prompt, IEnumerable<string> 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();
Expand All @@ -49,14 +49,6 @@ public static string prompt_for_confirmation(string prompt, IEnumerable<string>
.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);

Expand All @@ -65,17 +57,12 @@ public static string prompt_for_confirmation(string prompt, IEnumerable<string>
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))
Expand All @@ -98,7 +85,7 @@ public static string prompt_for_confirmation(string prompt, IEnumerable<string>
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;
}
Expand Down

0 comments on commit 64581ac

Please sign in to comment.