-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: capture standard error in server mode session
- Loading branch information
Showing
5 changed files
with
170 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/AWS.Deploy.ServerMode.Client/Utilities/CappedStringBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace AWS.Deploy.ServerMode.Client.Utilities | ||
{ | ||
public class CappedStringBuilder | ||
{ | ||
public int LineLimit { get; } | ||
public int LineCount { | ||
get | ||
{ | ||
return _lines?.Count ?? 0; | ||
} | ||
} | ||
|
||
private readonly Queue<string> _lines; | ||
|
||
public CappedStringBuilder(int lineLimit) | ||
{ | ||
_lines = new Queue<string>(lineLimit); | ||
LineLimit = lineLimit; | ||
} | ||
|
||
public void AppendLine(string value) | ||
{ | ||
if (LineCount >= LineLimit) | ||
{ | ||
_lines.Dequeue(); | ||
} | ||
|
||
_lines.Enqueue(value); | ||
} | ||
|
||
public string GetLastLines(int lineCount) | ||
{ | ||
return _lines.Reverse().Take(lineCount).Reverse().Aggregate((x, y) => x + Environment.NewLine + y); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return _lines.Aggregate((x, y) => x + Environment.NewLine + y); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
test/AWS.Deploy.ServerMode.Client.UnitTests/CappedStringBuilderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System; | ||
using AWS.Deploy.ServerMode.Client.Utilities; | ||
using Xunit; | ||
|
||
namespace AWS.Deploy.ServerMode.Client.UnitTests | ||
{ | ||
public class CappedStringBuilderTests | ||
{ | ||
private readonly CappedStringBuilder _cappedStringBuilder; | ||
|
||
public CappedStringBuilderTests() | ||
{ | ||
_cappedStringBuilder = new CappedStringBuilder(5); | ||
} | ||
|
||
[Fact] | ||
public void AppendLineTest() | ||
{ | ||
_cappedStringBuilder.AppendLine("test1"); | ||
_cappedStringBuilder.AppendLine("test2"); | ||
_cappedStringBuilder.AppendLine("test3"); | ||
|
||
Assert.Equal(3, _cappedStringBuilder.LineCount); | ||
Assert.Equal($"test1{Environment.NewLine}test2{Environment.NewLine}test3", _cappedStringBuilder.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void GetLastLinesTest() | ||
{ | ||
_cappedStringBuilder.AppendLine("test1"); | ||
_cappedStringBuilder.AppendLine("test2"); | ||
|
||
Assert.Equal(2, _cappedStringBuilder.LineCount); | ||
Assert.Equal("test2", _cappedStringBuilder.GetLastLines(1)); | ||
Assert.Equal($"test1{Environment.NewLine}test2", _cappedStringBuilder.GetLastLines(2)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters