Skip to content

Commit

Permalink
(GH-185) Remove require answer 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 64581ac commit 171888f
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 105 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"}, requireAnswer: true);
Do you want to continue?", new[] {"yes", "no"});

if (selection.is_equal_to("no"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,13 @@ public void should_have_called_Console_ReadLine()
}
}

public class when_prompting_with_interactivePrompt : InteractivePromptSpecsBase
public class when_prompting_with_interactivePrompt_with_errors : InteractivePromptSpecsBase
{
private string default_choice;
private Func<string> prompt;

public override void Because()
{
console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices, requireAnswer: false);
prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices);
}

[Fact]
Expand Down Expand Up @@ -119,101 +117,13 @@ public void should_error_when_the_prompt_input_is_null()
}
}

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, requireAnswer: false);
}

public override void AfterObservations()
{
base.AfterObservations();
should_have_called_Console_ReadLine();
}

[Fact]
public void should_return_null_when_no_answer_given()
{
console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
var result = prompt();
result.ShouldBeNull();
}

[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_first_choice_when_value_of_choice_is_given()
{
console.Setup(c => c.ReadLine()).Returns("yes");
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_and_answer_is_required : InteractivePromptSpecsBase
public class when_prompting_with_interactivePrompt : InteractivePromptSpecsBase
{
private Func<string> prompt;

public override void Because()
{
prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices, requireAnswer: true);
prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices);
}

public override void AfterObservations()
Expand Down
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" }, 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" });
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, true);
var selection = InteractivePrompt.prompt_for_confirmation("Which version of {0} would you like to uninstall?".format_with(packageName), choices);

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"}, requireAnswer: true);
, new[] {"yes", "no", "skip"});
if (selection.is_equal_to("yes")) shouldRun = true;
if (selection.is_equal_to("no"))
{
Expand Down
10 changes: 3 additions & 7 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, bool requireAnswer, int repeat = 10)
public static string prompt_for_confirmation(string prompt, IEnumerable<string> choices, int repeat = 10)
{
if (repeat < 0) throw new ApplicationException("Too many bad attempts. Stopping before application crash.");
Ensure.that(() => prompt).is_not_null();
Expand Down Expand Up @@ -82,12 +82,8 @@ public static string prompt_for_confirmation(string prompt, IEnumerable<string>
if (!selectionFound)
{
"chocolatey".Log().Error(ChocolateyLoggers.Important, "Your choice of '{0}' is not a valid selection.".format_with(selection));
if (requireAnswer)
{
"chocolatey".Log().Warn(ChocolateyLoggers.Important, "You must select an answer");
return prompt_for_confirmation(prompt, choices, requireAnswer, repeat - 1);
}
return null;
"chocolatey".Log().Warn(ChocolateyLoggers.Important, "You must select an answer");
return prompt_for_confirmation(prompt, choices, repeat - 1);
}
}

Expand Down

0 comments on commit 171888f

Please sign in to comment.