public void Format_ChangeTypeCreate_FormatsAfterValue() { // Arrange. var whatIfChanges = new List <WhatIfChange> { new WhatIfChange { ResourceId = "/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/rg1/providers/p1/foo", ChangeType = ChangeType.Create, After = new { numberValue = 1.2, booleanValue = true, stringValue = "str", } } }; string colon = $"{Color.Reset}:{Color.Green}"; string afterValue = $@" numberValue: 1.2 booleanValue: true stringValue: ""str"" " .Replace(":", colon) .Replace("\r\n", Environment.NewLine); // Act. string result = WhatIfOperationResultFormatter.Format( new PSWhatIfOperationResult(new WhatIfOperationResult(changes: whatIfChanges))); // Assert. Assert.Contains(afterValue, result); }
public void Format_NonEmptyResourceChanges_ExtractApiVersion() { // Arrange. var whatIfChanges = new List <WhatIfChange> { new WhatIfChange { ResourceId = "/subscriptions/00000000-0000-0000-0000-000000000002/resourceGroups/rg1/providers/p2/foo", ChangeType = ChangeType.Modify, Before = new { apiVersion = "2018-07-01" }, After = new { apiVersion = "2018-07-01" } }, }; string changesInfo = $@" Scope: /subscriptions/00000000-0000-0000-0000-000000000002/resourceGroups/rg1 {Color.Purple} ~ p2/foo{Color.Reset} [2018-07-01]{Color.Purple} {Color.Reset}" .Replace("\r\n", Environment.NewLine); // Act. string result = WhatIfOperationResultFormatter.Format( new PSWhatIfOperationResult(new WhatIfOperationResult(changes: whatIfChanges))); // Assert. Assert.Contains(changesInfo, result); }
public void Format_ChangeTypeModify_FormatsPropertyChanges() { // Arrange. var whatIfChanges = new List <WhatIfChange> { new WhatIfChange { ResourceId = "/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/rg1/providers/p1/foo", ChangeType = ChangeType.Modify, Delta = new List <WhatIfPropertyChange> { new WhatIfPropertyChange { Path = "path.to.property.change", PropertyChangeType = PropertyChangeType.Modify, Before = "foo", After = "bar" }, new WhatIfPropertyChange { Path = "path.to.array.change", PropertyChangeType = PropertyChangeType.Array, Children = new List <WhatIfPropertyChange> { new WhatIfPropertyChange { Path = "1", PropertyChangeType = PropertyChangeType.Modify, Before = "foo", After = "bar" } } }, } } }; string foo = $@"{Color.Orange}""foo""{Color.Reset}"; string bar = $@"{Color.Green}""bar""{Color.Reset}"; string expected = $@" Scope: /subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/rg1 {Color.Purple} ~ p1/foo{Color.Reset} {Color.Purple}~{Color.Reset} path.to.array.change{Color.Reset}:{Color.Reset} [ {Color.Purple}~{Color.Reset} 1{Color.Reset}:{Color.Reset} ""foo"" => ""bar"" ] {Color.Purple}~{Color.Reset} path.to.property.change{Color.Reset}:{Color.Reset} ""foo"" => ""bar"" " .Replace(@"""foo""", foo) .Replace(@"""bar""", bar) .Replace("\r\n", Environment.NewLine); // Act. string result = WhatIfOperationResultFormatter.Format( new PSWhatIfOperationResult(new WhatIfOperationResult(changes: whatIfChanges))); // Assert. Assert.Contains(expected, result); }
public void Format_ResourceIdOnly_SortsAndGroupsByShortResourceId() { // Arrange. var whatIfChanges = new List <WhatIfChange> { new WhatIfChange { ResourceId = "/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/rg1/providers/p1/foo", ChangeType = ChangeType.Ignore, }, new WhatIfChange { ResourceId = "/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/rg1/providers/p2/foo", ChangeType = ChangeType.Create, }, new WhatIfChange { ResourceId = "/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/rg1/providers/p3/foo", ChangeType = ChangeType.NoChange, }, new WhatIfChange { ResourceId = "/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/rg1/providers/p4/foo", ChangeType = ChangeType.Deploy, }, new WhatIfChange { ResourceId = "/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/rg1/providers/p5/foo", ChangeType = ChangeType.Delete, }, new WhatIfChange { ResourceId = "/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/rg1/providers/p6/foo", ChangeType = ChangeType.Delete, } }; string shortResourceIds = $@" {Color.Orange} - p5/foo - p6/foo{Color.Reset}{Color.Green} + p2/foo{Color.Reset}{Color.Blue} ! p4/foo{Color.Reset}{Color.Reset} = p3/foo{Color.Reset}{Color.Gray} * p1/foo {Color.Reset} " .Replace("\r\n", Environment.NewLine); // Act. string result = WhatIfOperationResultFormatter.Format( new PSWhatIfOperationResult(new WhatIfOperationResult(changes: whatIfChanges))); // Assert. Assert.Contains(shortResourceIds, result); }
public void Format_EmptyResourceChanges_ReturnsNoChangeInfo() { // Arrange. var psWhatIfOperationResult = new PSWhatIfOperationResult(new WhatIfOperationResult()); string noChangeInfo = new ColoredStringBuilder() .AppendLine() .Append("Resource changes: no change.") .ToString(); // Act. string result = WhatIfOperationResultFormatter.Format(psWhatIfOperationResult); // Assert. Assert.Contains(noChangeInfo, result); }
public void Format_OnSuccess_FormatPreviewNotice() { // Arrange. var psWhatIfOperationResult = new PSWhatIfOperationResult(new WhatIfOperationResult()); string previewNotice = new ColoredStringBuilder() .AppendLine("Note: As What-If is currently in preview, the result may contain false positive predictions (noise).") .AppendLine("You can help us improve the accuracy of the result by opening an issue here: https://aka.ms/WhatIfIssues.") .ToString(); // Act. string result = WhatIfOperationResultFormatter.Format(psWhatIfOperationResult); // Assert. Assert.StartsWith(previewNotice, result); }
private string ExecuteWhatIf() { const string statusMessage = "Getting the latest status of all resources..."; var clearMessage = new string(' ', statusMessage.Length); var information = new HostInformationMessage { Message = statusMessage, NoNewLine = true }; var clearInformation = new HostInformationMessage { Message = $"\r{clearMessage}\r", NoNewLine = true }; var tags = new[] { "PSHOST" }; try { // Write status message. this.WriteInformation(information, tags); var parameters = new PSDeploymentWhatIfCmdletParameters { DeploymentName = this.Name, Location = this.Location, Mode = DeploymentMode.Incremental, TemplateUri = TemplateUri ?? this.TryResolvePath(TemplateFile), TemplateObject = this.TemplateObject, TemplateParametersUri = this.TemplateParameterUri, TemplateParametersObject = GetTemplateParameterObject(this.TemplateParameterObject), ResultFormat = this.WhatIfResultFormat }; PSWhatIfOperationResult whatIfResult = ResourceManagerSdkClient.ExecuteDeploymentWhatIf(parameters, this.WhatIfExcludeChangeType); string whatIfMessage = WhatIfOperationResultFormatter.Format(whatIfResult); // Clear status before returning result. this.WriteInformation(clearInformation, tags); // Use \r to override the built-in "What if:" in output. return($"\r \r{Environment.NewLine}{whatIfMessage}{Environment.NewLine}"); } catch (Exception) { // Clear status before handling exception. this.WriteInformation(clearInformation, tags); throw; } }
public void Format_NonEmptyResourceChanges_AddsLegendAtTheBeginning() { // Arrange. var resourceChanges = new List <WhatIfChange> { new WhatIfChange { ResourceId = "", ChangeType = ChangeType.Deploy }, new WhatIfChange { ResourceId = "", ChangeType = ChangeType.Create }, new WhatIfChange { ResourceId = "", ChangeType = ChangeType.Ignore }, new WhatIfChange { ResourceId = "", ChangeType = ChangeType.Create } }; var psWhatIfOperationResult = new PSWhatIfOperationResult(new WhatIfOperationResult(changes: resourceChanges)); string legend = @"Resource and property changes are indicated with these symbols: + Create ! Deploy * Ignore " .Replace("+", new ColoredStringBuilder().Append("+", Color.Green).ToString()) .Replace("!", new ColoredStringBuilder().Append("!", Color.Blue).ToString()) .Replace("*", new ColoredStringBuilder().Append("*", Color.Gray).ToString()) .Replace("\r\n", Environment.NewLine); // Act. string result = WhatIfOperationResultFormatter.Format(psWhatIfOperationResult); // Assert. Assert.Contains(legend, result); }
public void Format_NonEmptyResourceChanges_AddsStatsAtTheEnd() { // Arrange. var whatIfChanges = new List <WhatIfChange> { new WhatIfChange { ResourceId = "", ChangeType = ChangeType.Create }, new WhatIfChange { ResourceId = "", ChangeType = ChangeType.Create }, new WhatIfChange { ResourceId = "", ChangeType = ChangeType.Modify }, new WhatIfChange { ResourceId = "", ChangeType = ChangeType.Delete } }; string stats = new ColoredStringBuilder() .AppendLine() .Append("Resource changes: 1 to delete, 2 to create, 1 to modify.") .ToString(); // Act. string result = WhatIfOperationResultFormatter.Format( new PSWhatIfOperationResult(new WhatIfOperationResult(changes: whatIfChanges))); // Assert. Assert.EndsWith(stats, result); }
protected override void OnProcessRecord() { string whatIfMessage = null; string warningMessage = null; string captionMessage = null; if (this.ShouldExecuteWhatIf()) { PSWhatIfOperationResult whatIfResult = this.ExecuteWhatIf(); string whatIfFormattedOutput = WhatIfOperationResultFormatter.Format(whatIfResult); if (this.ShouldProcessGivenCurrentConfirmFlagAndPreference() && this.ShouldSkipConfirmationIfNoChange() && whatIfResult.Changes.All(x => x.ChangeType == ChangeType.NoChange || x.ChangeType == ChangeType.Ignore)) { var whatIfInformation = new HostInformationMessage { Message = whatIfFormattedOutput }; var tags = new[] { "PSHOST" }; this.WriteInformation(whatIfInformation, tags); this.ExecuteDeployment(); return; } string cursorUp = $"{(char)27}[1A"; // Use \r to override the built-in "What if:" in output. whatIfMessage = $"\r \r{Environment.NewLine}{whatIfFormattedOutput}{Environment.NewLine}"; warningMessage = $"{Environment.NewLine}{Resources.ConfirmDeploymentMessage}"; captionMessage = $"{cursorUp}{Color.Reset}{whatIfMessage}"; } if (this.ShouldProcess(whatIfMessage, warningMessage, captionMessage)) { this.ExecuteDeployment(); } }
protected override void OnProcessRecord() { string whatIfMessage = null; string warningMessage = null; string captionMessage = null; if (this.ShouldExecuteWhatIf()) { PSWhatIfOperationResult whatIfResult = this.ExecuteWhatIf(); string whatIfFormattedOutput = WhatIfOperationResultFormatter.Format(whatIfResult); string cursorUp = $"{(char)27}[1A"; // Use \r to override the built-in "What if:" in output. whatIfMessage = $"\r \r{Environment.NewLine}{whatIfFormattedOutput}{Environment.NewLine}"; warningMessage = $"{Environment.NewLine}{Resources.ConfirmDeploymentMessage}"; captionMessage = $"{cursorUp}{Color.Reset}{whatIfMessage}"; } if (this.ShouldProcess(whatIfMessage, warningMessage, captionMessage)) { this.ExecuteDeployment(); } }
public void Format_NonEmptyResourceChanges_SortsAndGroupsByScopeAndChangeType() { // Arrange. var whatIfChanges = new List <WhatIfChange> { new WhatIfChange { ResourceId = "/Subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/RG1/providers/p1/foo1", ChangeType = ChangeType.Create }, new WhatIfChange { ResourceId = "/Subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/rg1/providers/p2/bar", ChangeType = ChangeType.Create }, new WhatIfChange { ResourceId = "/subscriptions/00000000-0000-0000-0000-000000000002/resourceGroups/rg2/providers/p1/foo2", ChangeType = ChangeType.Modify }, new WhatIfChange { ResourceId = "/SUBSCRIPTIONS/00000000-0000-0000-0000-000000000002/providers/p3/foobar1", ChangeType = ChangeType.Ignore }, new WhatIfChange { ResourceId = "/subscriptions/00000000-0000-0000-0000-000000000002/providers/p3/foobar2", ChangeType = ChangeType.Delete }, new WhatIfChange { ResourceId = "/subscriptions/00000000-0000-0000-0000-000000000002/resourceGroups/rg3", ChangeType = ChangeType.Delete } }; string changesInfo = $@" Scope: /subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/RG1 {Color.Green} + p1/foo1 + p2/bar {Color.Reset} Scope: /subscriptions/00000000-0000-0000-0000-000000000002 {Color.Orange} - p3/foobar2 - resourceGroups/rg3{Color.Reset}{Color.Gray} * p3/foobar1 {Color.Reset} Scope: /subscriptions/00000000-0000-0000-0000-000000000002/resourceGroups/rg2 {Color.Purple} ~ p1/foo2 {Color.Reset}" .Replace("\r\n", Environment.NewLine); // Act. string result = WhatIfOperationResultFormatter.Format( new PSWhatIfOperationResult(new WhatIfOperationResult(changes: whatIfChanges))); // Assert. Assert.Contains(changesInfo, result); }