Skip to content

Commit

Permalink
Merge branch 'pr204' into stable
Browse files Browse the repository at this point in the history
* pr204:
  (GH-181)(GH-184) Short prompt/Prompt character
  • Loading branch information
ferventcoder committed Mar 29, 2016
2 parents b19d797 + 35747eb commit e14a982
Show file tree
Hide file tree
Showing 13 changed files with 323 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/chocolatey.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ that chocolatey.licensed.dll exists at

if (config.RegularOutput)
{
"logfile".Log().Info(() => "".PadRight(60, '='));
"LogFileOnly".Log().Info(() => "".PadRight(60, '='));
#if DEBUG
"chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1}{2} (DEBUG BUILD)".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(license.LicenseType) : string.Empty));
#else
if (config.Information.ChocolateyVersion == config.Information.ChocolateyProductVersion && args.Any())
{
"logfile".Log().Info(() => "{0} v{1}{2}".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(license.LicenseType) : string.Empty));
"LogFileOnly".Log().Info(() => "{0} v{1}{2}".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(license.LicenseType) : string.Empty));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,5 +437,208 @@ public void should_error_when_any_choice_not_available_is_given()
console.Verify(c => c.ReadLine(), Times.AtLeast(8));
}
}

public class when_prompting_short_with_interactivePrompt_guard_errors : InteractivePromptSpecsBase
{
private Func<string> prompt;

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

[Fact]
public void should_error_when_the_choicelist_is_null()
{
choices = null;
bool errored = false;
console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
try
{
prompt();
}
catch (Exception)
{
errored = true;
}

errored.ShouldBeTrue();
console.Verify(c => c.ReadLine(), Times.Never);
}

[Fact]
public void should_error_when_the_choicelist_is_empty()
{
choices = new List<string>();
bool errored = false;
string errorMessage = string.Empty;
console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
try
{
prompt();
}
catch (Exception ex)
{
errored = true;
errorMessage = ex.Message;
}

errored.ShouldBeTrue();
errorMessage.ShouldContain("No choices passed in.");
console.Verify(c => c.ReadLine(), Times.Never);
}

[Fact]
public void should_error_when_the_prompt_input_is_null()
{
choices = new List<string> { "bob" };
prompt_value = null;
bool errored = false;
string errorMessage = string.Empty;
console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
try
{
prompt();
}
catch (Exception ex)
{
errored = true;
errorMessage = ex.Message;
}

errored.ShouldBeTrue();
errorMessage.ShouldContain("Value for prompt cannot be null.");
console.Verify(c => c.ReadLine(), Times.Never);
}

[Fact]
public void should_error_when_the_choicelist_contains_empty_values()
{
choices = new List<string> { "bob", "" };
bool errored = false;
string errorMessage = string.Empty;
console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
try
{
prompt();
}
catch (Exception ex)
{
errored = true;
errorMessage = ex.Message;
}

errored.ShouldBeTrue();
errorMessage.ShouldContain("Some choices are empty.");
console.Verify(c => c.ReadLine(), Times.Never);
}

[Fact]
public void should_error_when_the_choicelist_has_multiple_items_with_same_first_letter()
{
choices = new List<string> {"sally", "suzy"};
bool errored = false;
string errorMessage = string.Empty;
console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
try
{
prompt();
}
catch (Exception ex)
{
errored = true;
errorMessage = ex.Message;
}

errored.ShouldBeTrue();
errorMessage.ShouldContain("Multiple choices have the same first letter.");
console.Verify(c => c.ReadLine(), Times.Never);
}
}

public class when_prompting_short_with_interactivePrompt : InteractivePromptSpecsBase
{
private Func<string> prompt;

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

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

[Fact]
public void should_error_when_no_answer_given()
{
bool errored = false;

console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
try
{
prompt();
}
catch (Exception)
{
errored = true;
}
errored.ShouldBeTrue();
console.Verify(c => c.ReadLine(), Times.AtLeast(8));
}

[Fact]
public void should_return_yes_when_yes_is_given()
{
console.Setup(c => c.ReadLine()).Returns("yes");
var result = prompt();
result.ShouldEqual("yes");
}

[Fact]
public void should_return_yes_when_y_is_given()
{
console.Setup(c => c.ReadLine()).Returns("y");
var result = prompt();
result.ShouldEqual("yes");
}

[Fact]
public void should_return_no_choice_when_no_is_given()
{
console.Setup(c => c.ReadLine()).Returns("no");
var result = prompt();
result.ShouldEqual("no");
}

[Fact]
public void should_return_no_choice_when_n_is_given()
{
console.Setup(c => c.ReadLine()).Returns("n");
var result = prompt();
result.ShouldEqual("no");
}

[Fact]
public void should_error_when_any_choice_not_available_is_given()
{
bool errored = false;

console.Setup(c => c.ReadLine()).Returns("yup"); //Enter pressed
try
{
prompt();
}
catch (Exception)
{
errored = true;
}
errored.ShouldBeTrue();
console.Verify(c => c.ReadLine(), Times.AtLeast(8));
}
}
}
}
11 changes: 8 additions & 3 deletions src/chocolatey/infrastructure.app/runners/GenericRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ 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" }, defaultChoice: null, requireAnswer: true);
Do you want to continue?", new[] { "yes", "no" },
defaultChoice: null,
requireAnswer: true,
allowShortAnswer: true,
shortPrompt: true
);

if (selection.is_equal_to("no"))
{
Expand All @@ -199,5 +204,5 @@ require admin rights. Only advanced users should run choco w/out an
}

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,14 @@ public void run(PackageResult packageResult, ChocolateyConfiguration config)
var skipUninstaller = true;
if (config.PromptForConfirmation)
{
var selection = InteractivePrompt.prompt_for_confirmation("Uninstall may not be silent (could not detect). Proceed?", new[] {"yes", "no"}, defaultChoice: null, requireAnswer: true);
var selection = InteractivePrompt.prompt_for_confirmation(
"Uninstall may not be silent (could not detect). Proceed?",
new[] { "yes", "no" },
defaultChoice: null,
requireAnswer: true,
allowShortAnswer: true,
shortPrompt: true
);
if (selection.is_equal_to("yes")) skipUninstaller = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,14 @@ 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" }, defaultChoice: null, requireAnswer: true);
var selection = InteractivePrompt.prompt_for_confirmation(
" Unsuccessful operation for {0}.{1} Rollback to previous version (package files only)?".format_with(packageResult.Name, Environment.NewLine),
new[] { "yes", "no" },
defaultChoice: null,
requireAnswer: true,
allowShortAnswer: true,
shortPrompt: true
);
if (selection.is_equal_to("no")) rollback = false;
}

Expand Down
6 changes: 5 additions & 1 deletion src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,11 @@ 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, defaultChoice: null, requireAnswer: true);
var selection = InteractivePrompt.prompt_for_confirmation("Which version of {0} would you like to uninstall?".format_with(packageName),
choices,
defaultChoice: null,
requireAnswer: true,
allowShortAnswer: false);

if (string.IsNullOrWhiteSpace(selection)) continue;
if (selection.is_equal_to(abortChoice)) continue;
Expand Down
16 changes: 14 additions & 2 deletions src/chocolatey/infrastructure.app/services/PowershellService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,26 @@ public bool run_action(ChocolateyConfiguration configuration, PackageResult pack
this.Log().Info(ChocolateyLoggers.Important, () => @"Note: To confirm automatically next time, use '-y' or consider setting
'allowGlobalConfirmation'. Run 'choco feature -h' for more details.");

var selection = InteractivePrompt.prompt_for_confirmation(@"Do you want to run the script?", new[] {"yes", "no", "print"}, defaultChoice: null, requireAnswer: true);
var selection = InteractivePrompt.prompt_for_confirmation(@"Do you want to run the script?",
new[] {"yes", "no", "print"},
defaultChoice : null,
requireAnswer : true,
allowShortAnswer : true,
shortPrompt: true
);

if (selection.is_equal_to("print"))
{
this.Log().Info(ChocolateyLoggers.Important, "------ BEGIN SCRIPT ------");
this.Log().Info(() => "{0}{1}{0}".format_with(Environment.NewLine, chocoPowerShellScriptContents.escape_curly_braces()));
this.Log().Info(ChocolateyLoggers.Important, "------- END SCRIPT -------");
selection = InteractivePrompt.prompt_for_confirmation(@"Do you want to run this script?", new[] { "yes", "no" }, defaultChoice: null, requireAnswer: true);
selection = InteractivePrompt.prompt_for_confirmation(@"Do you want to run this script?",
new[] { "yes", "no" },
defaultChoice : null,
requireAnswer : true,
allowShortAnswer : true,
shortPrompt: true
);
}

if (selection.is_equal_to("yes")) shouldRun = true;
Expand Down
10 changes: 10 additions & 0 deletions src/chocolatey/infrastructure/adapters/Console.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,15 @@ public void Write(object value)
{
System.Console.Write(value.to_string());
}

public void WriteLine()
{
System.Console.WriteLine();
}

public void WriteLine(object value)
{
System.Console.WriteLine(value);
}
}
}
15 changes: 15 additions & 0 deletions src/chocolatey/infrastructure/adapters/IConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ public interface IConsole
/// <exception cref="T:System.IO.IOException">An I/O error occurred.</exception>
/// <filterpriority>1</filterpriority>
void Write(object value);

/// <summary>
/// Writes the current line terminator to the standard output stream.
/// </summary>
/// <exception cref="T:System.IO.IOException">An I/O error occurred.</exception>
/// <filterpriority>1</filterpriority>
void WriteLine();

/// <summary>
/// Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream.
/// </summary>
/// <param name="value">The value to write.</param>
/// <exception cref="T:System.IO.IOException">An I/O error occurred.</exception>
/// <filterpriority>1</filterpriority>
void WriteLine(object value);
}

// ReSharper restore InconsistentNaming
Expand Down
Loading

0 comments on commit e14a982

Please sign in to comment.