From ded4f05b75632a6a0241f0b52ff07fb78fe1f916 Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Sat, 23 Feb 2019 17:57:45 -0600 Subject: [PATCH 01/24] [feature] Added Select-String -Color and made it work with -AllMatch and -SimpleMatch --- .../commands/utility/MatchString.cs | 69 +++++++++++++++++-- .../Select-String.Tests.ps1 | 12 ++++ 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 110dea324d3..02cc0a24ea5 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -88,6 +88,30 @@ public class MatchInfo /// The text of the matching line. public string Line { get; set; } = string.Empty; + /// + /// Gets or sets whether the matched portion of the string is highlighted. + /// + /// Whether the matched portion of the string is highlighted with asterisks. + public bool Color { get; set; } + + private readonly List MatchIndexes; + private readonly List MatchLengths; + + /// + /// Default constructor. + /// + public MatchInfo(){ + + } + /// + /// Constructor. + /// + public MatchInfo(bool isR, List matchIndexes, List matchLengths, bool color){ + this.Color = color; + this.MatchIndexes = matchIndexes; + this.MatchLengths = matchLengths; + } + /// /// Gets the base name of the file containing the matching line. /// @@ -143,6 +167,8 @@ public string Path /// public MatchInfoContext Context { get; set; } + + /// /// Returns the path of the matching file truncated relative to the parameter. /// @@ -216,12 +242,28 @@ public override string ToString() public string ToString(string directory) { string displayPath = (directory != null) ? RelativePath(directory) : _path; + string modifiedLine; + if (Color) + { + StringBuilder sb = new StringBuilder(Line); + for (int i = 0; i < MatchIndexes.Count; i++) + { + int offset = 2 * i; + sb.Insert(MatchIndexes[i]+offset, "*"); + sb.Insert(MatchIndexes[i]+MatchLengths[i]+offset+1, "*"); + } + modifiedLine = sb.ToString(); + } + else + { + modifiedLine = Line; + } // Just return a single line if the user didn't // enable context-tracking. if (Context == null) { - return FormatLine(Line, this.LineNumber, displayPath, EmptyPrefix); + return FormatLine(modifiedLine, this.LineNumber, displayPath, EmptyPrefix); } // Otherwise, render the full context. @@ -233,7 +275,7 @@ public string ToString(string directory) lines.Add(FormatLine(contextLine, displayLineNumber++, displayPath, ContextPrefix)); } - lines.Add(FormatLine(Line, displayLineNumber++, displayPath, MatchPrefix)); + lines.Add(FormatLine(modifiedLine, displayLineNumber++, displayPath, MatchPrefix)); foreach (string contextLine in Context.DisplayPostContext) { @@ -1062,6 +1104,13 @@ public string[] LiteralPath [Parameter] public SwitchParameter List { get; set; } + /// + /// Gets or sets a value indicating if the matching portion of the string should be highlighted by + /// surrounding it with asterisks. + /// + [Parameter] + public SwitchParameter Color { get; set; } + /// /// Gets or sets files to include. Files matching /// one of these (if specified) are included. @@ -1541,6 +1590,9 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI int patternIndex = 0; matchResult = null; + List indexes = new List(); + List lengths = new List(); + if (!SimpleMatch) { while (patternIndex < Pattern.Length) @@ -1557,6 +1609,10 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI { matches = new Match[mc.Count]; ((ICollection)mc).CopyTo(matches, 0); + foreach (Match match in matches) { + indexes.Add(match.Index); + lengths.Add(match.Length); + } gotMatch = true; } } @@ -1567,6 +1623,8 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI if (match.Success) { + indexes.Add(match.Index); + lengths.Add(match.Length); matches = new Match[] { match }; } } @@ -1587,8 +1645,11 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI { string pat = Pattern[patternIndex]; - if (operandString.IndexOf(pat, compareOption) >= 0) + int index = operandString.IndexOf(pat, compareOption); + if (index >= 0) { + indexes.Add(index); + lengths.Add(pat.Length); gotMatch = true; break; } @@ -1637,7 +1698,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI } // otherwise construct and populate a new MatchInfo object - matchResult = new MatchInfo + matchResult = new MatchInfo(false, indexes, lengths, Color) { IgnoreCase = !CaseSensitive, Line = operandString, diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 index 861d5b11465..ca0f2a2ff7e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 @@ -71,6 +71,18 @@ Describe "Select-String" -Tags "CI" { $testinputone | Select-String -Pattern "goodbye" -NotMatch | Should -BeExactly "hello", "Hello" } + it "Should return an array of matching strings with the first match highlighted when Color is used" { + $testinputone | Select-String -Pattern "l" -Color | Should -Be "he*l*lo", "he*l*lo" + } + + it "Should return an array of matching strings with all matches highlighted when Color and AllMatch is used" { + $testinputone | Select-String -Pattern "l" -Color -AllMatch | Should -Be "he*l**l*o", "he*l**l*o" + } + + it "Should return an array of matching strings with the first match highlighted when Color and SimpleMatch is used" { + $testinputone | Select-String -Pattern "l" -Color -SimpleMatch | Should -Be "he*l*lo", "he*l*lo" + } + it "Should return the same as NotMatch" { $firstMatch = $testinputone | Select-String -pattern "goodbye" -NotMatch $secondMatch = $testinputone | Select-String -pattern "goodbye" -n From 64534b95d01856e2123a01477d56a7fdce155f66 Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Sat, 23 Feb 2019 18:11:21 -0600 Subject: [PATCH 02/24] [feature] Added Select-String -Color and test cases --- .../commands/utility/MatchString.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 02cc0a24ea5..65c39ea0dca 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -94,7 +94,13 @@ public class MatchInfo /// Whether the matched portion of the string is highlighted with asterisks. public bool Color { get; set; } + /// + /// Stores the starting index of each match within the line. + /// private readonly List MatchIndexes; + /// + /// Stores the length of each match within the line. + /// private readonly List MatchLengths; /// From 7b4880591693741737eee8f3bc99546e78d4932b Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Sat, 23 Feb 2019 18:49:43 -0600 Subject: [PATCH 03/24] [feature] Adds -Emphasize for Select-String --- .../commands/utility/MatchString.cs | 12 ++++++------ .../Select-String.Tests.ps1 | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 65c39ea0dca..66b526cd6af 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -92,7 +92,7 @@ public class MatchInfo /// Gets or sets whether the matched portion of the string is highlighted. /// /// Whether the matched portion of the string is highlighted with asterisks. - public bool Color { get; set; } + public bool Emphasize { get; set; } /// /// Stores the starting index of each match within the line. @@ -112,8 +112,8 @@ public MatchInfo(){ /// /// Constructor. /// - public MatchInfo(bool isR, List matchIndexes, List matchLengths, bool color){ - this.Color = color; + public MatchInfo(bool isR, List matchIndexes, List matchLengths, bool emphasize){ + this.Emphasize = emphasize; this.MatchIndexes = matchIndexes; this.MatchLengths = matchLengths; } @@ -249,7 +249,7 @@ public string ToString(string directory) { string displayPath = (directory != null) ? RelativePath(directory) : _path; string modifiedLine; - if (Color) + if (Emphasize) { StringBuilder sb = new StringBuilder(Line); for (int i = 0; i < MatchIndexes.Count; i++) @@ -1115,7 +1115,7 @@ public string[] LiteralPath /// surrounding it with asterisks. /// [Parameter] - public SwitchParameter Color { get; set; } + public SwitchParameter Emphasize { get; set; } /// /// Gets or sets files to include. Files matching @@ -1704,7 +1704,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI } // otherwise construct and populate a new MatchInfo object - matchResult = new MatchInfo(false, indexes, lengths, Color) + matchResult = new MatchInfo(false, indexes, lengths, Emphasize) { IgnoreCase = !CaseSensitive, Line = operandString, diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 index ca0f2a2ff7e..05a8c8753ee 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 @@ -71,16 +71,16 @@ Describe "Select-String" -Tags "CI" { $testinputone | Select-String -Pattern "goodbye" -NotMatch | Should -BeExactly "hello", "Hello" } - it "Should return an array of matching strings with the first match highlighted when Color is used" { - $testinputone | Select-String -Pattern "l" -Color | Should -Be "he*l*lo", "he*l*lo" + it "Should return an array of matching strings with the first match highlighted when Emphasize is used" { + $testinputone | Select-String -Pattern "l" -Emphasize | Should -Be "he*l*lo", "he*l*lo" } - it "Should return an array of matching strings with all matches highlighted when Color and AllMatch is used" { - $testinputone | Select-String -Pattern "l" -Color -AllMatch | Should -Be "he*l**l*o", "he*l**l*o" + it "Should return an array of matching strings with all matches highlighted when Emphasize and AllMatch is used" { + $testinputone | Select-String -Pattern "l" -Emphasize -AllMatch | Should -Be "he*l**l*o", "he*l**l*o" } - it "Should return an array of matching strings with the first match highlighted when Color and SimpleMatch is used" { - $testinputone | Select-String -Pattern "l" -Color -SimpleMatch | Should -Be "he*l*lo", "he*l*lo" + it "Should return an array of matching strings with the first match highlighted when Emphasize and SimpleMatch is used" { + $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Should -Be "he*l*lo", "he*l*lo" } it "Should return the same as NotMatch" { From 4732734c6bfbf3b3a3b100340252ccdd812db129 Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Sun, 24 Feb 2019 00:39:16 -0600 Subject: [PATCH 04/24] [feature] Adds red highlighting for matched text --- .../commands/utility/MatchString.cs | 63 ++++++++++++++----- 1 file changed, 46 insertions(+), 17 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 66b526cd6af..bf70a9ad8af 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -89,7 +89,7 @@ public class MatchInfo public string Line { get; set; } = string.Empty; /// - /// Gets or sets whether the matched portion of the string is highlighted. + /// Gets or sets a value indicating whether the matched portion of the string is highlighted. /// /// Whether the matched portion of the string is highlighted with asterisks. public bool Emphasize { get; set; } @@ -97,25 +97,39 @@ public class MatchInfo /// /// Stores the starting index of each match within the line. /// - private readonly List MatchIndexes; + private readonly List matchIndexes; + /// /// Stores the length of each match within the line. /// - private readonly List MatchLengths; + private readonly List matchLengths; /// - /// Default constructor. + /// Stores a values indicating whether or not the terminal supports VT. /// - public MatchInfo(){ + private readonly bool? supportsVirtualTerminal; + /// + /// Initializes a new instance of the class. + /// + public MatchInfo() + { } + /// - /// Constructor. + /// Initializes a new instance of the class. /// - public MatchInfo(bool isR, List matchIndexes, List matchLengths, bool emphasize){ + /// Used for implementing -Raw. + /// Sets the matchIndexes. + /// Sets the matchLengths. + /// Used for implementing -Emphasize. + /// Sets a value indicating whether or not virtual terminal is supported. + public MatchInfo(bool isRaw, List matchIndexes, List matchLengths, bool emphasize, bool? vt) + { this.Emphasize = emphasize; - this.MatchIndexes = matchIndexes; - this.MatchLengths = matchLengths; + this.matchIndexes = matchIndexes; + this.matchLengths = matchLengths; + this.supportsVirtualTerminal = vt; } /// @@ -173,8 +187,6 @@ public string Path /// public MatchInfoContext Context { get; set; } - - /// /// Returns the path of the matching file truncated relative to the parameter. /// @@ -251,13 +263,28 @@ public string ToString(string directory) string modifiedLine; if (Emphasize) { + string open; + string close; + + if (supportsVirtualTerminal ?? false) + { + open = "\u001b[31m"; + close = "\u001b[0m"; + } + else + { + open = "*"; + close = "*"; + } + StringBuilder sb = new StringBuilder(Line); - for (int i = 0; i < MatchIndexes.Count; i++) + for (int i = 0; i < matchIndexes.Count; i++) { - int offset = 2 * i; - sb.Insert(MatchIndexes[i]+offset, "*"); - sb.Insert(MatchIndexes[i]+MatchLengths[i]+offset+1, "*"); + int offset = open.Length + close.Length; + sb.Insert(matchIndexes[i] + (i * offset), open); + sb.Insert(matchIndexes[i] + matchLengths[i] + (i * offset + open.Length), close); } + modifiedLine = sb.ToString(); } else @@ -1615,10 +1642,12 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI { matches = new Match[mc.Count]; ((ICollection)mc).CopyTo(matches, 0); - foreach (Match match in matches) { + foreach (Match match in matches) + { indexes.Add(match.Index); lengths.Add(match.Length); } + gotMatch = true; } } @@ -1704,7 +1733,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI } // otherwise construct and populate a new MatchInfo object - matchResult = new MatchInfo(false, indexes, lengths, Emphasize) + matchResult = new MatchInfo(false, indexes, lengths, Emphasize, Host?.UI.SupportsVirtualTerminal) { IgnoreCase = !CaseSensitive, Line = operandString, From f72f38edb1cd2ef84c39e6500b46da33bc13d5ea Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Sun, 24 Feb 2019 01:19:10 -0600 Subject: [PATCH 05/24] [feature] Adds red highlighting for matched text --- .../commands/utility/MatchString.cs | 2 +- .../Select-String.Tests.ps1 | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index bf70a9ad8af..4ccf8bf2d93 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -1733,7 +1733,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI } // otherwise construct and populate a new MatchInfo object - matchResult = new MatchInfo(false, indexes, lengths, Emphasize, Host?.UI.SupportsVirtualTerminal) + matchResult = new MatchInfo(false, indexes, lengths, Emphasize.IsPresent, Host?.UI.SupportsVirtualTerminal) { IgnoreCase = !CaseSensitive, Line = operandString, diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 index 05a8c8753ee..7bb64435bd8 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 @@ -72,15 +72,27 @@ Describe "Select-String" -Tags "CI" { } it "Should return an array of matching strings with the first match highlighted when Emphasize is used" { - $testinputone | Select-String -Pattern "l" -Emphasize | Should -Be "he*l*lo", "he*l*lo" + If ($IsWindows) { + $testinputone | Select-String -Pattern "l" -Emphasize | Should -Be "he*l*lo", "he*l*lo" + } Else { + $testinputone | Select-String -Pattern "l" -Emphasize | Should -Be "he[31ml[0mlo", "he[31ml[0mlo" + } } it "Should return an array of matching strings with all matches highlighted when Emphasize and AllMatch is used" { - $testinputone | Select-String -Pattern "l" -Emphasize -AllMatch | Should -Be "he*l**l*o", "he*l**l*o" + If ($IsWindows) { + $testinputone | Select-String -Pattern "l" -Emphasize | Should -Be "he*l**l*o", "he*l**l*o" + } Else { + $testinputone | Select-String -Pattern "l" -Emphasize -AllMatch | Should -Be "he[31ml[0m[31ml[0mo", "he[31ml[0m[31ml[0mo" + } } it "Should return an array of matching strings with the first match highlighted when Emphasize and SimpleMatch is used" { - $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Should -Be "he*l*lo", "he*l*lo" + If ($IsWindows) { + $testinputone | Select-String -Pattern "l" -Emphasize | Should -Be "he*l*lo", "he*l*lo" + } Else { + $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Should -Be "he[31ml[0mlo", "he[31ml[0mlo" + } } it "Should return the same as NotMatch" { From 00e7224b93c5705481f7f11c0f44cfda54ed7333 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Sun, 24 Feb 2019 02:31:41 -0600 Subject: [PATCH 06/24] Apply suggestions from code review Co-Authored-By: derek-xia --- .../commands/utility/MatchString.cs | 10 +++++----- .../Select-String.Tests.ps1 | 7 +++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 4ccf8bf2d93..6467a12db59 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -97,17 +97,17 @@ public class MatchInfo /// /// Stores the starting index of each match within the line. /// - private readonly List matchIndexes; + private readonly List _matchIndexes; /// /// Stores the length of each match within the line. /// - private readonly List matchLengths; + private readonly List _matchLengths; /// /// Stores a values indicating whether or not the terminal supports VT. /// - private readonly bool? supportsVirtualTerminal; + private readonly bool? _supportsVirtualTerminal; /// /// Initializes a new instance of the class. @@ -1623,8 +1623,8 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI int patternIndex = 0; matchResult = null; - List indexes = new List(); - List lengths = new List(); + var indexes = new List(); + var lengths = new List(); if (!SimpleMatch) { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 index 7bb64435bd8..d1de2b660be 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 @@ -72,9 +72,12 @@ Describe "Select-String" -Tags "CI" { } it "Should return an array of matching strings with the first match highlighted when Emphasize is used" { - If ($IsWindows) { + if ($IsWindows) + { $testinputone | Select-String -Pattern "l" -Emphasize | Should -Be "he*l*lo", "he*l*lo" - } Else { + } + else + { $testinputone | Select-String -Pattern "l" -Emphasize | Should -Be "he[31ml[0mlo", "he[31ml[0mlo" } } From ca39e177e27a7e6fb8052599052abca455ff78bd Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Sun, 24 Feb 2019 02:45:46 -0600 Subject: [PATCH 07/24] [feature] Refactored code --- .../commands/utility/MatchString.cs | 20 +++++++++---------- .../Select-String.Tests.ps1 | 8 ++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 6467a12db59..682aa53d38b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -107,7 +107,7 @@ public class MatchInfo /// /// Stores a values indicating whether or not the terminal supports VT. /// - private readonly bool? _supportsVirtualTerminal; + private readonly bool _supportsVirtualTerminal; /// /// Initializes a new instance of the class. @@ -124,12 +124,12 @@ public MatchInfo() /// Sets the matchLengths. /// Used for implementing -Emphasize. /// Sets a value indicating whether or not virtual terminal is supported. - public MatchInfo(bool isRaw, List matchIndexes, List matchLengths, bool emphasize, bool? vt) + public MatchInfo(bool isRaw, List matchIndexes, List matchLengths, bool emphasize, bool vt) { this.Emphasize = emphasize; - this.matchIndexes = matchIndexes; - this.matchLengths = matchLengths; - this.supportsVirtualTerminal = vt; + this._matchIndexes = matchIndexes; + this._matchLengths = matchLengths; + this._supportsVirtualTerminal = vt; } /// @@ -266,7 +266,7 @@ public string ToString(string directory) string open; string close; - if (supportsVirtualTerminal ?? false) + if (_supportsVirtualTerminal) { open = "\u001b[31m"; close = "\u001b[0m"; @@ -278,11 +278,11 @@ public string ToString(string directory) } StringBuilder sb = new StringBuilder(Line); - for (int i = 0; i < matchIndexes.Count; i++) + for (int i = 0; i < _matchIndexes.Count; i++) { int offset = open.Length + close.Length; - sb.Insert(matchIndexes[i] + (i * offset), open); - sb.Insert(matchIndexes[i] + matchLengths[i] + (i * offset + open.Length), close); + sb.Insert(_matchIndexes[i] + (i * offset), open); + sb.Insert(_matchIndexes[i] + _matchLengths[i] + ((i * offset) + open.Length), close); } modifiedLine = sb.ToString(); @@ -1733,7 +1733,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI } // otherwise construct and populate a new MatchInfo object - matchResult = new MatchInfo(false, indexes, lengths, Emphasize.IsPresent, Host?.UI.SupportsVirtualTerminal) + matchResult = new MatchInfo(false, indexes, lengths, Emphasize.IsPresent, Host.UI.SupportsVirtualTerminal) { IgnoreCase = !CaseSensitive, Line = operandString, diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 index d1de2b660be..3c3f04a829d 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 @@ -83,17 +83,17 @@ Describe "Select-String" -Tags "CI" { } it "Should return an array of matching strings with all matches highlighted when Emphasize and AllMatch is used" { - If ($IsWindows) { + if ($IsWindows) { $testinputone | Select-String -Pattern "l" -Emphasize | Should -Be "he*l**l*o", "he*l**l*o" - } Else { + } else { $testinputone | Select-String -Pattern "l" -Emphasize -AllMatch | Should -Be "he[31ml[0m[31ml[0mo", "he[31ml[0m[31ml[0mo" } } it "Should return an array of matching strings with the first match highlighted when Emphasize and SimpleMatch is used" { - If ($IsWindows) { + if ($IsWindows) { $testinputone | Select-String -Pattern "l" -Emphasize | Should -Be "he*l*lo", "he*l*lo" - } Else { + } else { $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Should -Be "he[31ml[0mlo", "he[31ml[0mlo" } } From 665bd4fdf1a34b5067a8f91fe2d3d3f421c370f0 Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Sat, 9 Mar 2019 02:46:21 -0600 Subject: [PATCH 08/24] [feature] Moved new functionality, changed formatter, fixed tests --- .../commands/utility/MatchString.cs | 117 ++++++++++++------ .../PowerShellCore_format_ps1xml.cs | 2 +- .../Select-String.Tests.ps1 | 44 ++++--- 3 files changed, 112 insertions(+), 51 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 682aa53d38b..8f42b13a8f4 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -119,12 +119,11 @@ public MatchInfo() /// /// Initializes a new instance of the class. /// - /// Used for implementing -Raw. /// Sets the matchIndexes. /// Sets the matchLengths. /// Used for implementing -Emphasize. /// Sets a value indicating whether or not virtual terminal is supported. - public MatchInfo(bool isRaw, List matchIndexes, List matchLengths, bool emphasize, bool vt) + public MatchInfo(List matchIndexes, List matchLengths, bool emphasize, bool vt) { this.Emphasize = emphasize; this._matchIndexes = matchIndexes; @@ -260,43 +259,12 @@ public override string ToString() public string ToString(string directory) { string displayPath = (directory != null) ? RelativePath(directory) : _path; - string modifiedLine; - if (Emphasize) - { - string open; - string close; - - if (_supportsVirtualTerminal) - { - open = "\u001b[31m"; - close = "\u001b[0m"; - } - else - { - open = "*"; - close = "*"; - } - - StringBuilder sb = new StringBuilder(Line); - for (int i = 0; i < _matchIndexes.Count; i++) - { - int offset = open.Length + close.Length; - sb.Insert(_matchIndexes[i] + (i * offset), open); - sb.Insert(_matchIndexes[i] + _matchLengths[i] + ((i * offset) + open.Length), close); - } - - modifiedLine = sb.ToString(); - } - else - { - modifiedLine = Line; - } // Just return a single line if the user didn't // enable context-tracking. if (Context == null) { - return FormatLine(modifiedLine, this.LineNumber, displayPath, EmptyPrefix); + return FormatLine(Line, this.LineNumber, displayPath, EmptyPrefix); } // Otherwise, render the full context. @@ -308,7 +276,7 @@ public string ToString(string directory) lines.Add(FormatLine(contextLine, displayLineNumber++, displayPath, ContextPrefix)); } - lines.Add(FormatLine(modifiedLine, displayLineNumber++, displayPath, MatchPrefix)); + lines.Add(FormatLine(Line, displayLineNumber++, displayPath, MatchPrefix)); foreach (string contextLine in Context.DisplayPostContext) { @@ -318,6 +286,83 @@ public string ToString(string directory) return string.Join(System.Environment.NewLine, lines.ToArray()); } + /// + /// Returns the string representation of the match object same format as ToString() + /// and adds color to the matched text if virtual terminal is supported. + /// + /// Directory to use as the root when calculating the relative path. + /// The colored string representation of the match object. + public string ToEmphasizedString(string directory) + { + string originalLine = Line; + if (Emphasize) + { + string open; + string close; + + if (_supportsVirtualTerminal) + { + open = "\u001b[31m"; + close = "\u001b[0m"; + } + else + { + open = "*"; + close = "*"; + } + + Line = String.Create(_matchIndexes.Count * (open.Length + close.Length) + Line.Length, Line, (chars, buf) => + { + int lineIndex = 0; + int charsIndex = 0; + for (int i = 0; i < _matchIndexes.Count; i++) + { + // Adds characters before match + while (lineIndex < _matchIndexes[i]) + { + chars[charsIndex] = Line[lineIndex]; + lineIndex++; + charsIndex++; + } + + // Adds opening vt sequence + for (int j = 0; j < open.Length; j++) + { + chars[charsIndex] = open[j]; + charsIndex++; + } + + // Adds characters being emphasized + for (int j = 0; j < _matchLengths[i]; j++) { + chars[charsIndex] = Line[lineIndex]; + lineIndex++; + charsIndex++; + } + + // Adds closing vt sequence + for (int j = 0; j < close.Length; j++) + { + chars[charsIndex] = close[j]; + charsIndex++; + } + } + + // Adds remaining characters in line + while (lineIndex < Line.Length) + { + chars[charsIndex] = Line[lineIndex]; + lineIndex++; + charsIndex++; + } + }); + } + + string modifiedLine = ToString(directory); + Line = originalLine; + + return modifiedLine; + } + /// /// Formats a line for use in ToString. /// @@ -1733,7 +1778,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI } // otherwise construct and populate a new MatchInfo object - matchResult = new MatchInfo(false, indexes, lengths, Emphasize.IsPresent, Host.UI.SupportsVirtualTerminal) + matchResult = new MatchInfo(indexes, lengths, Emphasize.IsPresent, Host.UI.SupportsVirtualTerminal) { IgnoreCase = !CaseSensitive, Line = operandString, diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 35d34c60670..440b06836b0 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -377,7 +377,7 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co yield return new FormatViewDefinition("MatchInfo", CustomControl.Create() .StartEntry() - .AddScriptBlockExpressionBinding(@"$_.ToString(((get-location).path))") + .AddScriptBlockExpressionBinding(@"$_.ToEmphasizedString(((get-location).path))") .EndEntry() .EndControl()); } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 index 3c3f04a829d..9eb931b53cf 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 @@ -71,31 +71,47 @@ Describe "Select-String" -Tags "CI" { $testinputone | Select-String -Pattern "goodbye" -NotMatch | Should -BeExactly "hello", "Hello" } - it "Should return an array of matching strings with the first match highlighted when Emphasize is used" { - if ($IsWindows) + it "Should output a string with the first match highlighted when Emphasize is used" { + if ($Host.UI.SupportsVirtualTerminal) { - $testinputone | Select-String -Pattern "l" -Emphasize | Should -Be "he*l*lo", "he*l*lo" + $result = $testinputone | Select-String -Pattern "l" -Emphasize | Out-String + $result | Should -Be "`nhe`e[31ml`e[0mlo`nHe`e[31ml`e[0mlo`n`n" } else { - $testinputone | Select-String -Pattern "l" -Emphasize | Should -Be "he[31ml[0mlo", "he[31ml[0mlo" + $result = $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Out-String + $result | Should -Be "`nhe*l*lo`nHe*l*lo`n`n" } } - it "Should return an array of matching strings with all matches highlighted when Emphasize and AllMatch is used" { - if ($IsWindows) { - $testinputone | Select-String -Pattern "l" -Emphasize | Should -Be "he*l**l*o", "he*l**l*o" - } else { - $testinputone | Select-String -Pattern "l" -Emphasize -AllMatch | Should -Be "he[31ml[0m[31ml[0mo", "he[31ml[0m[31ml[0mo" + it "Should output a string with all matches highlighted when Emphasize and AllMatch is used" { + if ($Host.UI.SupportsVirtualTerminal) + { + $result = $testinputone | Select-String -Pattern "l" -Emphasize -AllMatch | Out-String + $result | Should -Be "`nhe`e[31ml`e[0m`e[31ml`e[0mo`nHe`e[31ml`e[0m`e[31ml`e[0mo`n`n" + } + else + { + $result = $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Out-String + $result | Should -Be "`nhe*l**l*o`nHe*l**l*o`n`n" } } - it "Should return an array of matching strings with the first match highlighted when Emphasize and SimpleMatch is used" { - if ($IsWindows) { - $testinputone | Select-String -Pattern "l" -Emphasize | Should -Be "he*l*lo", "he*l*lo" - } else { - $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Should -Be "he[31ml[0mlo", "he[31ml[0mlo" + it "Should output a string with the first match highlighted when Emphasize and SimpleMatch is used" { + if ($Host.UI.SupportsVirtualTerminal) + { + $result = $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Out-String + $result | Should -Be "`nhe`e[31ml`e[0mlo`nHe`e[31ml`e[0mlo`n`n" } + else + { + $result = $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Out-String + $result | Should -Be "`nhe*l*lo`nHe*l*lo`n`n" + } + } + + it "Should return an array of matching strings without virtual terminal sequences when Emphasize is used" { + $testinputone | Select-String -Pattern "l" -Emphasize | Should -Be "hello", "hello" } it "Should return the same as NotMatch" { From 439400d73fe3949a012ddba3f53dfa08efae8b3e Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Sat, 9 Mar 2019 02:56:06 -0600 Subject: [PATCH 09/24] [feature] Fixed style errors --- .../commands/utility/MatchString.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 8f42b13a8f4..e8dacc02f1b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -311,7 +311,7 @@ public string ToEmphasizedString(string directory) close = "*"; } - Line = String.Create(_matchIndexes.Count * (open.Length + close.Length) + Line.Length, Line, (chars, buf) => + Line = string.Create((_matchIndexes.Count * (open.Length + close.Length)) + Line.Length, Line, (chars, buf) => { int lineIndex = 0; int charsIndex = 0; @@ -333,7 +333,8 @@ public string ToEmphasizedString(string directory) } // Adds characters being emphasized - for (int j = 0; j < _matchLengths[i]; j++) { + for (int j = 0; j < _matchLengths[i]; j++) + { chars[charsIndex] = Line[lineIndex]; lineIndex++; charsIndex++; From 92c92bc690f7fa75dcb16f60ed81d20ebcca6d37 Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Wed, 1 May 2019 00:50:33 -0500 Subject: [PATCH 10/24] [feature] Changed red escape sequence to negative escape sequence --- .../commands/utility/MatchString.cs | 2 +- .../Microsoft.PowerShell.Utility/Select-String.Tests.ps1 | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index e8dacc02f1b..d245e7d9675 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -302,7 +302,7 @@ public string ToEmphasizedString(string directory) if (_supportsVirtualTerminal) { - open = "\u001b[31m"; + open = "\u001b[7m"; close = "\u001b[0m"; } else diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 index 9eb931b53cf..52b02c21cc1 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 @@ -75,7 +75,7 @@ Describe "Select-String" -Tags "CI" { if ($Host.UI.SupportsVirtualTerminal) { $result = $testinputone | Select-String -Pattern "l" -Emphasize | Out-String - $result | Should -Be "`nhe`e[31ml`e[0mlo`nHe`e[31ml`e[0mlo`n`n" + $result | Should -Be "`nhe`e[7ml`e[0mlo`nHe`e[7ml`e[0mlo`n`n" } else { @@ -88,7 +88,7 @@ Describe "Select-String" -Tags "CI" { if ($Host.UI.SupportsVirtualTerminal) { $result = $testinputone | Select-String -Pattern "l" -Emphasize -AllMatch | Out-String - $result | Should -Be "`nhe`e[31ml`e[0m`e[31ml`e[0mo`nHe`e[31ml`e[0m`e[31ml`e[0mo`n`n" + $result | Should -Be "`nhe`e[7ml`e[0m`e[7ml`e[0mo`nHe`e[7ml`e[0m`e[7ml`e[0mo`n`n" } else { @@ -101,7 +101,7 @@ Describe "Select-String" -Tags "CI" { if ($Host.UI.SupportsVirtualTerminal) { $result = $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Out-String - $result | Should -Be "`nhe`e[31ml`e[0mlo`nHe`e[31ml`e[0mlo`n`n" + $result | Should -Be "`nhe`e[7ml`e[0mlo`nHe`e[7ml`e[0mlo`n`n" } else { From 6b97206a453f9d4d0a09668741bae54f40641aaf Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Wed, 1 May 2019 01:11:26 -0500 Subject: [PATCH 11/24] [feature] Removed surrounding asterisks if VT is unsupported --- .../commands/utility/MatchString.cs | 96 +++++++++---------- .../Select-String.Tests.ps1 | 6 +- 2 files changed, 46 insertions(+), 56 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index d245e7d9675..3d92a4a4b96 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -294,69 +294,59 @@ public string ToString(string directory) /// The colored string representation of the match object. public string ToEmphasizedString(string directory) { - string originalLine = Line; - if (Emphasize) - { - string open; - string close; + if (!Emphasize || ! _supportsVirtualTerminal) { + return ToString(directory); + } - if (_supportsVirtualTerminal) - { - open = "\u001b[7m"; - close = "\u001b[0m"; - } - else - { - open = "*"; - close = "*"; - } + string originalLine = Line; + string open = "\u001b[7m"; + string close = "\u001b[0m"; - Line = string.Create((_matchIndexes.Count * (open.Length + close.Length)) + Line.Length, Line, (chars, buf) => + Line = string.Create((_matchIndexes.Count * (open.Length + close.Length)) + Line.Length, Line, (chars, buf) => + { + int lineIndex = 0; + int charsIndex = 0; + for (int i = 0; i < _matchIndexes.Count; i++) { - int lineIndex = 0; - int charsIndex = 0; - for (int i = 0; i < _matchIndexes.Count; i++) + // Adds characters before match + while (lineIndex < _matchIndexes[i]) { - // Adds characters before match - while (lineIndex < _matchIndexes[i]) - { - chars[charsIndex] = Line[lineIndex]; - lineIndex++; - charsIndex++; - } - - // Adds opening vt sequence - for (int j = 0; j < open.Length; j++) - { - chars[charsIndex] = open[j]; - charsIndex++; - } - - // Adds characters being emphasized - for (int j = 0; j < _matchLengths[i]; j++) - { - chars[charsIndex] = Line[lineIndex]; - lineIndex++; - charsIndex++; - } - - // Adds closing vt sequence - for (int j = 0; j < close.Length; j++) - { - chars[charsIndex] = close[j]; - charsIndex++; - } + chars[charsIndex] = Line[lineIndex]; + lineIndex++; + charsIndex++; } - - // Adds remaining characters in line - while (lineIndex < Line.Length) + + // Adds opening vt sequence + for (int j = 0; j < open.Length; j++) + { + chars[charsIndex] = open[j]; + charsIndex++; + } + + // Adds characters being emphasized + for (int j = 0; j < _matchLengths[i]; j++) { chars[charsIndex] = Line[lineIndex]; lineIndex++; + charsIndex++; + } + + // Adds closing vt sequence + for (int j = 0; j < close.Length; j++) + { + chars[charsIndex] = close[j]; charsIndex++; } - }); - } + } + + // Adds remaining characters in line + while (lineIndex < Line.Length) + { + chars[charsIndex] = Line[lineIndex]; + lineIndex++; + charsIndex++; + } + }); string modifiedLine = ToString(directory); Line = originalLine; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 index 52b02c21cc1..5bc7ae177c3 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 @@ -80,7 +80,7 @@ Describe "Select-String" -Tags "CI" { else { $result = $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Out-String - $result | Should -Be "`nhe*l*lo`nHe*l*lo`n`n" + $result | Should -Be "`nhello`nHello`n`n" } } @@ -93,7 +93,7 @@ Describe "Select-String" -Tags "CI" { else { $result = $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Out-String - $result | Should -Be "`nhe*l**l*o`nHe*l**l*o`n`n" + $result | Should -Be "`nhello`nHello`n`n" } } @@ -106,7 +106,7 @@ Describe "Select-String" -Tags "CI" { else { $result = $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Out-String - $result | Should -Be "`nhe*l*lo`nHe*l*lo`n`n" + $result | Should -Be "`nhello`nHello`n`n" } } From d299906f3244ce2b6598239359ab2b05c670db43 Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Tue, 7 May 2019 18:36:47 -0500 Subject: [PATCH 12/24] [feature] Changed MatchInfo's Emphasize to private --- .../commands/utility/MatchString.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 3d92a4a4b96..95426906230 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -91,8 +91,8 @@ public class MatchInfo /// /// Gets or sets a value indicating whether the matched portion of the string is highlighted. /// - /// Whether the matched portion of the string is highlighted with asterisks. - public bool Emphasize { get; set; } + /// Whether the matched portion of the string is highlighted with the negative VT sequence. + private bool Emphasize { get; set; } /// /// Stores the starting index of each match within the line. @@ -1174,8 +1174,7 @@ public string[] LiteralPath public SwitchParameter List { get; set; } /// - /// Gets or sets a value indicating if the matching portion of the string should be highlighted by - /// surrounding it with asterisks. + /// Gets or sets a value indicating if the matching portion of the string should be highlighted. /// [Parameter] public SwitchParameter Emphasize { get; set; } From 2065727aa5c8816bd94febe84b146712fa5d6f48 Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Sun, 9 Jun 2019 00:17:23 -0700 Subject: [PATCH 13/24] [feature] Cleaned up matchInfo and DoMatchWorker --- .../commands/utility/MatchString.cs | 88 +++++++++++-------- 1 file changed, 50 insertions(+), 38 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 95426906230..f3e6f4d3856 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -92,43 +92,36 @@ public class MatchInfo /// Gets or sets a value indicating whether the matched portion of the string is highlighted. /// /// Whether the matched portion of the string is highlighted with the negative VT sequence. - private bool Emphasize { get; set; } + private readonly bool _emphasize; /// /// Stores the starting index of each match within the line. /// - private readonly List _matchIndexes; + private readonly IReadOnlyList _matchIndexes; /// /// Stores the length of each match within the line. /// - private readonly List _matchLengths; - - /// - /// Stores a values indicating whether or not the terminal supports VT. - /// - private readonly bool _supportsVirtualTerminal; + private readonly IReadOnlyList _matchLengths; /// /// Initializes a new instance of the class. /// public MatchInfo() { + this._emphasize = false; } /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class when the Emphasize parameter is set. /// /// Sets the matchIndexes. /// Sets the matchLengths. - /// Used for implementing -Emphasize. - /// Sets a value indicating whether or not virtual terminal is supported. - public MatchInfo(List matchIndexes, List matchLengths, bool emphasize, bool vt) + public MatchInfo(IReadOnlyList matchIndexes, IReadOnlyList matchLengths) { - this.Emphasize = emphasize; + this._emphasize = true; this._matchIndexes = matchIndexes; this._matchLengths = matchLengths; - this._supportsVirtualTerminal = vt; } /// @@ -294,15 +287,15 @@ public string ToString(string directory) /// The colored string representation of the match object. public string ToEmphasizedString(string directory) { - if (!Emphasize || ! _supportsVirtualTerminal) { + if (!_emphasize) { return ToString(directory); } string originalLine = Line; - string open = "\u001b[7m"; - string close = "\u001b[0m"; + string beginInvertedColorsVT100 = "\u001b[7m"; + string resetVT100 = "\u001b[0m"; - Line = string.Create((_matchIndexes.Count * (open.Length + close.Length)) + Line.Length, Line, (chars, buf) => + Line = string.Create((_matchIndexes.Count * (beginInvertedColorsVT100.Length + resetVT100.Length)) + Line.Length, Line, (chars, buf) => { int lineIndex = 0; int charsIndex = 0; @@ -317,9 +310,9 @@ public string ToEmphasizedString(string directory) } // Adds opening vt sequence - for (int j = 0; j < open.Length; j++) + for (int j = 0; j < beginInvertedColorsVT100.Length; j++) { - chars[charsIndex] = open[j]; + chars[charsIndex] = beginInvertedColorsVT100[j]; charsIndex++; } @@ -328,13 +321,13 @@ public string ToEmphasizedString(string directory) { chars[charsIndex] = Line[lineIndex]; lineIndex++; - charsIndex++; + charsIndex++; } // Adds closing vt sequence - for (int j = 0; j < close.Length; j++) + for (int j = 0; j < resetVT100.Length; j++) { - chars[charsIndex] = close[j]; + chars[charsIndex] = resetVT100[j]; charsIndex++; } } @@ -1658,8 +1651,17 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI int patternIndex = 0; matchResult = null; - var indexes = new List(); - var lengths = new List(); + List indexes = null; + List lengths = null; + + // If Emphasize is set and VT is supported, + // the lengths and starting indexes of regex matches + // need to be passed in to the matchInfo object. + if (Emphasize && Host.UI.SupportsVirtualTerminal) + { + indexes = new List(); + lengths = new List(); + } if (!SimpleMatch) { @@ -1677,10 +1679,14 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI { matches = new Match[mc.Count]; ((ICollection)mc).CopyTo(matches, 0); - foreach (Match match in matches) + + if (Emphasize && Host.UI.SupportsVirtualTerminal) { - indexes.Add(match.Index); - lengths.Add(match.Length); + foreach (Match match in matches) + { + indexes.Add(match.Index); + lengths.Add(match.Length); + } } gotMatch = true; @@ -1693,8 +1699,11 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI if (match.Success) { - indexes.Add(match.Index); - lengths.Add(match.Length); + if (Emphasize && Host.UI.SupportsVirtualTerminal) + { + indexes.Add(match.Index); + lengths.Add(match.Length); + } matches = new Match[] { match }; } } @@ -1718,8 +1727,11 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI int index = operandString.IndexOf(pat, compareOption); if (index >= 0) { - indexes.Add(index); - lengths.Add(pat.Length); + if (Emphasize && Host.UI.SupportsVirtualTerminal) + { + indexes.Add(index); + lengths.Add(pat.Length); + } gotMatch = true; break; } @@ -1768,12 +1780,12 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI } // otherwise construct and populate a new MatchInfo object - matchResult = new MatchInfo(indexes, lengths, Emphasize.IsPresent, Host.UI.SupportsVirtualTerminal) - { - IgnoreCase = !CaseSensitive, - Line = operandString, - Pattern = Pattern[patternIndex] - }; + matchResult = Emphasize && Host.UI.SupportsVirtualTerminal + ? new MatchInfo(indexes, lengths) + : new MatchInfo(); + matchResult.IgnoreCase = !CaseSensitive; + matchResult.Line = operandString; + matchResult.Pattern = Pattern[patternIndex]; if (_preContext > 0 || _postContext > 0) { From 6f47e792955e56628a45d760a68df9e817b8aab8 Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Thu, 13 Jun 2019 21:48:47 -0700 Subject: [PATCH 14/24] [feature] Changed ToEmphasizedString string creation --- .../commands/utility/MatchString.cs | 76 +++++++------------ 1 file changed, 29 insertions(+), 47 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index f3e6f4d3856..611ae8ddfbd 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -295,52 +295,34 @@ public string ToEmphasizedString(string directory) string beginInvertedColorsVT100 = "\u001b[7m"; string resetVT100 = "\u001b[0m"; - Line = string.Create((_matchIndexes.Count * (beginInvertedColorsVT100.Length + resetVT100.Length)) + Line.Length, Line, (chars, buf) => - { - int lineIndex = 0; - int charsIndex = 0; - for (int i = 0; i < _matchIndexes.Count; i++) - { - // Adds characters before match - while (lineIndex < _matchIndexes[i]) - { - chars[charsIndex] = Line[lineIndex]; - lineIndex++; - charsIndex++; - } - - // Adds opening vt sequence - for (int j = 0; j < beginInvertedColorsVT100.Length; j++) - { - chars[charsIndex] = beginInvertedColorsVT100[j]; - charsIndex++; - } - - // Adds characters being emphasized - for (int j = 0; j < _matchLengths[i]; j++) - { - chars[charsIndex] = Line[lineIndex]; - lineIndex++; - charsIndex++; - } - - // Adds closing vt sequence - for (int j = 0; j < resetVT100.Length; j++) - { - chars[charsIndex] = resetVT100[j]; - charsIndex++; - } - } - - // Adds remaining characters in line - while (lineIndex < Line.Length) - { - chars[charsIndex] = Line[lineIndex]; - lineIndex++; - charsIndex++; - } - }); - + char[] chars = new char[(_matchIndexes.Count * (beginInvertedColorsVT100.Length + resetVT100.Length)) + Line.Length]; + int lineIndex = 0; + int charsIndex = 0; + for (int i = 0; i < _matchIndexes.Count; i++) + { + // Adds characters before match + Line.CopyTo(lineIndex, chars, charsIndex, _matchIndexes[i]-lineIndex); + charsIndex += _matchIndexes[i]-lineIndex; + lineIndex = _matchIndexes[i]; + + // Adds opening vt sequence + beginInvertedColorsVT100.CopyTo(0, chars, charsIndex, beginInvertedColorsVT100.Length); + charsIndex += beginInvertedColorsVT100.Length; + + // Adds characters being emphasized + Line.CopyTo(lineIndex, chars, charsIndex, _matchLengths[i]); + lineIndex += _matchLengths[i]; + charsIndex += _matchLengths[i]; + + // Adds closing vt sequence + resetVT100.CopyTo(0, chars, charsIndex, resetVT100.Length); + charsIndex += resetVT100.Length; + } + + // Adds remaining characters in line + Line.CopyTo(lineIndex, chars, charsIndex, Line.Length - lineIndex); + + Line = new string(chars); string modifiedLine = ToString(directory); Line = originalLine; @@ -1654,7 +1636,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI List indexes = null; List lengths = null; - // If Emphasize is set and VT is supported, + // If Emphasize is set and VT is supported, // the lengths and starting indexes of regex matches // need to be passed in to the matchInfo object. if (Emphasize && Host.UI.SupportsVirtualTerminal) From 1bc3437a095a01656d4cf5716e553f6428143e7f Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Thu, 13 Jun 2019 22:23:13 -0700 Subject: [PATCH 15/24] [feature] Refactored ToEmphasizedString --- .../commands/utility/MatchString.cs | 56 ++++++++++++------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 611ae8ddfbd..3e3ccc9f610 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -113,7 +113,8 @@ public MatchInfo() } /// - /// Initializes a new instance of the class when the Emphasize parameter is set. + /// Initializes a new instance of the class with emphasized matched text. + /// Used when the Emphasize parameter is set and virtual terminal sequences are supported. /// /// Sets the matchIndexes. /// Sets the matchLengths. @@ -250,6 +251,19 @@ public override string ToString() /// Directory to use as the root when calculating the relative path. /// The string representation of the match object. public string ToString(string directory) + { + return ToString(directory, Line); + } + + /// + /// Returns the string representation of the match object with the matched line passed + /// in as and trims the path to be relative to + /// the argument. + /// + /// Directory to use as the root when calculating the relative path. + /// Line that the match occurs in. + /// The string representation of the match object. + private string ToString(string directory, string line) { string displayPath = (directory != null) ? RelativePath(directory) : _path; @@ -257,7 +271,7 @@ public string ToString(string directory) // enable context-tracking. if (Context == null) { - return FormatLine(Line, this.LineNumber, displayPath, EmptyPrefix); + return FormatLine(line, this.LineNumber, displayPath, EmptyPrefix); } // Otherwise, render the full context. @@ -269,7 +283,7 @@ public string ToString(string directory) lines.Add(FormatLine(contextLine, displayLineNumber++, displayPath, ContextPrefix)); } - lines.Add(FormatLine(Line, displayLineNumber++, displayPath, MatchPrefix)); + lines.Add(FormatLine(line, displayLineNumber++, displayPath, MatchPrefix)); foreach (string contextLine in Context.DisplayPostContext) { @@ -281,34 +295,42 @@ public string ToString(string directory) /// /// Returns the string representation of the match object same format as ToString() - /// and adds color to the matched text if virtual terminal is supported. + /// and inverts the color of the matched text if virtual terminal is supported. /// /// Directory to use as the root when calculating the relative path. - /// The colored string representation of the match object. + /// The string representation of the match object with matched text inverted. public string ToEmphasizedString(string directory) { if (!_emphasize) { return ToString(directory); } - string originalLine = Line; - string beginInvertedColorsVT100 = "\u001b[7m"; - string resetVT100 = "\u001b[0m"; + return ToString(directory, EmphasizeLine()); + } - char[] chars = new char[(_matchIndexes.Count * (beginInvertedColorsVT100.Length + resetVT100.Length)) + Line.Length]; + /// + /// Surrounds the matched text with virtual terminal sequences to invert it's color. Used in ToEmphasizedString. + /// + /// The matched line with matched text inverted. + private string EmphasizeLine() + { + const string invertColorsVT100 = "\u001b[7m"; + const string resetVT100 = "\u001b[0m"; + + char[] chars = new char[(_matchIndexes.Count * (invertColorsVT100.Length + resetVT100.Length)) + Line.Length]; int lineIndex = 0; int charsIndex = 0; - for (int i = 0; i < _matchIndexes.Count; i++) + for (int i = 0; i < _matchIndexes.Count; i++) { // Adds characters before match Line.CopyTo(lineIndex, chars, charsIndex, _matchIndexes[i]-lineIndex); charsIndex += _matchIndexes[i]-lineIndex; lineIndex = _matchIndexes[i]; - + // Adds opening vt sequence - beginInvertedColorsVT100.CopyTo(0, chars, charsIndex, beginInvertedColorsVT100.Length); - charsIndex += beginInvertedColorsVT100.Length; - + invertColorsVT100.CopyTo(0, chars, charsIndex, invertColorsVT100.Length); + charsIndex += invertColorsVT100.Length; + // Adds characters being emphasized Line.CopyTo(lineIndex, chars, charsIndex, _matchLengths[i]); lineIndex += _matchLengths[i]; @@ -322,11 +344,7 @@ public string ToEmphasizedString(string directory) // Adds remaining characters in line Line.CopyTo(lineIndex, chars, charsIndex, Line.Length - lineIndex); - Line = new string(chars); - string modifiedLine = ToString(directory); - Line = originalLine; - - return modifiedLine; + return new string(chars); } /// From 76233405813fc6f43772fff905be6ab2284556fe Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Tue, 25 Jun 2019 13:51:15 -0700 Subject: [PATCH 16/24] Update Select-String.Tests.ps1 --- .../Microsoft.PowerShell.Utility/Select-String.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 index 5bc7ae177c3..5d1f17c2496 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 @@ -1,5 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + Describe "Select-String" -Tags "CI" { $nl = [Environment]::NewLine $currentDirectory = $pwd.Path @@ -140,7 +141,7 @@ Describe "Select-String" -Tags "CI" { New-Item $testInputFile -Itemtype "file" -Force -Value "This is a text string, and another string${nl}This is the second line${nl}This is the third line${nl}This is the fourth line${nl}No matches" } - AfterEach { + AfterEach Remove-Item $testInputFile -Force } From 1d23b5c9fe2fbde1deb88db59191059ca10e841a Mon Sep 17 00:00:00 2001 From: Tyler James Leonhardt Date: Wed, 7 Aug 2019 22:14:32 -0700 Subject: [PATCH 17/24] readd curly brace --- .../Microsoft.PowerShell.Utility/Select-String.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 index 5d1f17c2496..5c846bd4745 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 @@ -141,7 +141,7 @@ Describe "Select-String" -Tags "CI" { New-Item $testInputFile -Itemtype "file" -Force -Value "This is a text string, and another string${nl}This is the second line${nl}This is the third line${nl}This is the fourth line${nl}No matches" } - AfterEach + AfterEach { Remove-Item $testInputFile -Force } From f840d88931f4d0719b791d9861c456504f90c55f Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Thu, 8 Aug 2019 20:27:50 -0700 Subject: [PATCH 18/24] shouldEmphasize --- .../commands/utility/MatchString.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 3e3ccc9f610..a8fee3338d7 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -1654,10 +1654,12 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI List indexes = null; List lengths = null; + bool shouldEmphasize = Emphasize && Host.UI.SupportsVirtualTerminal; + // If Emphasize is set and VT is supported, // the lengths and starting indexes of regex matches // need to be passed in to the matchInfo object. - if (Emphasize && Host.UI.SupportsVirtualTerminal) + if (shouldEmphasize) { indexes = new List(); lengths = new List(); @@ -1680,7 +1682,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI matches = new Match[mc.Count]; ((ICollection)mc).CopyTo(matches, 0); - if (Emphasize && Host.UI.SupportsVirtualTerminal) + if (shouldEmphasize) { foreach (Match match in matches) { @@ -1699,7 +1701,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI if (match.Success) { - if (Emphasize && Host.UI.SupportsVirtualTerminal) + if (shouldEmphasize) { indexes.Add(match.Index); lengths.Add(match.Length); @@ -1727,7 +1729,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI int index = operandString.IndexOf(pat, compareOption); if (index >= 0) { - if (Emphasize && Host.UI.SupportsVirtualTerminal) + if (shouldEmphasize) { indexes.Add(index); lengths.Add(pat.Length); @@ -1780,7 +1782,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI } // otherwise construct and populate a new MatchInfo object - matchResult = Emphasize && Host.UI.SupportsVirtualTerminal + matchResult = shouldEmphasize ? new MatchInfo(indexes, lengths) : new MatchInfo(); matchResult.IgnoreCase = !CaseSensitive; From 9395045f7877ff0d92046bf97cc5ae9c22f23e4d Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Thu, 8 Aug 2019 20:32:35 -0700 Subject: [PATCH 19/24] Formatting changes --- .../commands/utility/MatchString.cs | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index a8fee3338d7..1282a2bff1c 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -301,7 +301,8 @@ private string ToString(string directory, string line) /// The string representation of the match object with matched text inverted. public string ToEmphasizedString(string directory) { - if (!_emphasize) { + if (!_emphasize) + { return ToString(directory); } @@ -314,22 +315,22 @@ public string ToEmphasizedString(string directory) /// The matched line with matched text inverted. private string EmphasizeLine() { - const string invertColorsVT100 = "\u001b[7m"; - const string resetVT100 = "\u001b[0m"; + const string InvertColorsVT100 = "\u001b[7m"; + const string ResetVT100 = "\u001b[0m"; - char[] chars = new char[(_matchIndexes.Count * (invertColorsVT100.Length + resetVT100.Length)) + Line.Length]; + char[] chars = new char[(_matchIndexes.Count * (InvertColorsVT100.Length + ResetVT100.Length)) + Line.Length]; int lineIndex = 0; int charsIndex = 0; for (int i = 0; i < _matchIndexes.Count; i++) { // Adds characters before match - Line.CopyTo(lineIndex, chars, charsIndex, _matchIndexes[i]-lineIndex); - charsIndex += _matchIndexes[i]-lineIndex; + Line.CopyTo(lineIndex, chars, charsIndex, _matchIndexes[i] - lineIndex); + charsIndex += _matchIndexes[i] - lineIndex; lineIndex = _matchIndexes[i]; // Adds opening vt sequence - invertColorsVT100.CopyTo(0, chars, charsIndex, invertColorsVT100.Length); - charsIndex += invertColorsVT100.Length; + InvertColorsVT100.CopyTo(0, chars, charsIndex, InvertColorsVT100.Length); + charsIndex += InvertColorsVT100.Length; // Adds characters being emphasized Line.CopyTo(lineIndex, chars, charsIndex, _matchLengths[i]); @@ -337,8 +338,8 @@ private string EmphasizeLine() charsIndex += _matchLengths[i]; // Adds closing vt sequence - resetVT100.CopyTo(0, chars, charsIndex, resetVT100.Length); - charsIndex += resetVT100.Length; + ResetVT100.CopyTo(0, chars, charsIndex, ResetVT100.Length); + charsIndex += ResetVT100.Length; } // Adds remaining characters in line @@ -1706,6 +1707,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI indexes.Add(match.Index); lengths.Add(match.Length); } + matches = new Match[] { match }; } } @@ -1734,6 +1736,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI indexes.Add(index); lengths.Add(pat.Length); } + gotMatch = true; break; } From f9145bda234561587a6f6a508a8d1b90c02811f7 Mon Sep 17 00:00:00 2001 From: Derek Xia Date: Tue, 27 Aug 2019 13:55:04 -0500 Subject: [PATCH 20/24] Changed Emphasize to NoEmphasis --- .../commands/utility/MatchString.cs | 10 +++--- .../Select-String.Tests.ps1 | 32 ++++++++----------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 1282a2bff1c..f1052cbea52 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -105,7 +105,7 @@ public class MatchInfo private readonly IReadOnlyList _matchLengths; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class with emphasis disabled. /// public MatchInfo() { @@ -114,7 +114,7 @@ public MatchInfo() /// /// Initializes a new instance of the class with emphasized matched text. - /// Used when the Emphasize parameter is set and virtual terminal sequences are supported. + /// Used when virtual terminal sequences are supported. /// /// Sets the matchIndexes. /// Sets the matchLengths. @@ -1168,10 +1168,10 @@ public string[] LiteralPath public SwitchParameter List { get; set; } /// - /// Gets or sets a value indicating if the matching portion of the string should be highlighted. + /// Gets or sets a value indicating if highlighting should be disabled. /// [Parameter] - public SwitchParameter Emphasize { get; set; } + public SwitchParameter NoEmphasis { get; set; } /// /// Gets or sets files to include. Files matching @@ -1655,7 +1655,7 @@ private bool DoMatchWorker(string operandString, MatchInfo matchInfo, out MatchI List indexes = null; List lengths = null; - bool shouldEmphasize = Emphasize && Host.UI.SupportsVirtualTerminal; + bool shouldEmphasize = !NoEmphasis && Host.UI.SupportsVirtualTerminal; // If Emphasize is set and VT is supported, // the lengths and starting indexes of regex matches diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 index 5c846bd4745..7aa0e1063e4 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 @@ -72,55 +72,52 @@ Describe "Select-String" -Tags "CI" { $testinputone | Select-String -Pattern "goodbye" -NotMatch | Should -BeExactly "hello", "Hello" } - it "Should output a string with the first match highlighted when Emphasize is used" { + it "Should output a string with the first match highlighted" { if ($Host.UI.SupportsVirtualTerminal) { - $result = $testinputone | Select-String -Pattern "l" -Emphasize | Out-String + $result = $testinputone | Select-String -Pattern "l" | Out-String $result | Should -Be "`nhe`e[7ml`e[0mlo`nHe`e[7ml`e[0mlo`n`n" } else { - $result = $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Out-String + $result = $testinputone | Select-String -Pattern "l" | Out-String $result | Should -Be "`nhello`nHello`n`n" } } - it "Should output a string with all matches highlighted when Emphasize and AllMatch is used" { + it "Should output a string with all matches highlighted when AllMatch is used" { if ($Host.UI.SupportsVirtualTerminal) { - $result = $testinputone | Select-String -Pattern "l" -Emphasize -AllMatch | Out-String + $result = $testinputone | Select-String -Pattern "l" -AllMatch | Out-String $result | Should -Be "`nhe`e[7ml`e[0m`e[7ml`e[0mo`nHe`e[7ml`e[0m`e[7ml`e[0mo`n`n" } else { - $result = $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Out-String + $result = $testinputone | Select-String -Pattern "l" -AllMatch | Out-String $result | Should -Be "`nhello`nHello`n`n" } } - it "Should output a string with the first match highlighted when Emphasize and SimpleMatch is used" { + it "Should output a string with the first match highlighted when SimpleMatch is used" { if ($Host.UI.SupportsVirtualTerminal) { - $result = $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Out-String + $result = $testinputone | Select-String -Pattern "l" -SimpleMatch | Out-String $result | Should -Be "`nhe`e[7ml`e[0mlo`nHe`e[7ml`e[0mlo`n`n" } else { - $result = $testinputone | Select-String -Pattern "l" -Emphasize -SimpleMatch | Out-String + $result = $testinputone | Select-String -Pattern "l" -SimpleMatch | Out-String $result | Should -Be "`nhello`nHello`n`n" } } - it "Should return an array of matching strings without virtual terminal sequences when Emphasize is used" { - $testinputone | Select-String -Pattern "l" -Emphasize | Should -Be "hello", "hello" + it "Should output a string without highlighting when NoEmphasis is used" { + $result = $testinputone | Select-String -Pattern "l" -NoEmphasis | Out-String + $result | Should -Be "`nhello`nHello`n`n" } - it "Should return the same as NotMatch" { - $firstMatch = $testinputone | Select-String -pattern "goodbye" -NotMatch - $secondMatch = $testinputone | Select-String -pattern "goodbye" -n - - $equal = @(Compare-Object $firstMatch $secondMatch).Length -eq 0 - $equal | Should -BeTrue + it "Should return an array of matching strings without virtual terminal sequences" { + $testinputone | Select-String -Pattern "l" | Should -Be "hello", "hello" } It "Should return a string type when -Raw is used" { @@ -130,7 +127,6 @@ Describe "Select-String" -Tags "CI" { It "Should return ParameterBindingException when -Raw and -Quiet are used together" { { $testinputone | Select-String -Pattern "hello" -Raw -Quiet -ErrorAction Stop } | Should -Throw -ExceptionType ([System.Management.Automation.ParameterBindingException]) - } } Context "Filesystem actions" { From ccc09f85ce04c1ce96e2a78638d799e325831a8a Mon Sep 17 00:00:00 2001 From: Tyler James Leonhardt Date: Wed, 25 Sep 2019 09:03:55 -0700 Subject: [PATCH 21/24] add right } --- .../Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 index 7aa0e1063e4..19613b9d1bd 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 @@ -127,6 +127,7 @@ Describe "Select-String" -Tags "CI" { It "Should return ParameterBindingException when -Raw and -Quiet are used together" { { $testinputone | Select-String -Pattern "hello" -Raw -Quiet -ErrorAction Stop } | Should -Throw -ExceptionType ([System.Management.Automation.ParameterBindingException]) + } } Context "Filesystem actions" { From 46e62fd2dd88f36b765c194a67cdcf18049b715e Mon Sep 17 00:00:00 2001 From: Tyler James Leonhardt Date: Wed, 25 Sep 2019 09:05:32 -0700 Subject: [PATCH 22/24] tab instead of spaces? --- .../Microsoft.PowerShell.Utility/Select-String.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 index 19613b9d1bd..62501bdbd40 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 @@ -127,7 +127,7 @@ Describe "Select-String" -Tags "CI" { It "Should return ParameterBindingException when -Raw and -Quiet are used together" { { $testinputone | Select-String -Pattern "hello" -Raw -Quiet -ErrorAction Stop } | Should -Throw -ExceptionType ([System.Management.Automation.ParameterBindingException]) - } + } } Context "Filesystem actions" { From 40facf56a01af5bf85441cd2caab83282d0e1a10 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Sun, 29 Sep 2019 23:42:18 -0700 Subject: [PATCH 23/24] tabs to spaces --- .../Select-String.Tests.ps1 | 393 +++++++++--------- 1 file changed, 197 insertions(+), 196 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 index 62501bdbd40..a5dfdda92e7 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 @@ -4,245 +4,246 @@ Describe "Select-String" -Tags "CI" { $nl = [Environment]::NewLine $currentDirectory = $pwd.Path + Context "String actions" { - $testinputone = "hello","Hello","goodbye" - $testinputtwo = "hello","Hello" + $testinputone = "hello","Hello","goodbye" + $testinputtwo = "hello","Hello" + + it "Should be called without errors" { + { $testinputone | Select-String -Pattern "hello" } | Should -Not -Throw + } - it "Should be called without errors" { - { $testinputone | Select-String -Pattern "hello" } | Should -Not -Throw - } + it "Should return an array data type when multiple matches are found" { + $result = $testinputtwo | Select-String -Pattern "hello" + ,$result | Should -BeOfType "System.Array" + } - it "Should return an array data type when multiple matches are found" { - $result = $testinputtwo | Select-String -Pattern "hello" - ,$result | Should -BeOfType "System.Array" + it "Should return an object type when one match is found" { + $result = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive + ,$result | Should -BeOfType "System.Object" } - it "Should return an object type when one match is found" { - $result = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive - ,$result | Should -BeOfType "System.Object" - } + it "Should return matchinfo type" { + $result = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive + ,$result | Should -BeOfType "Microsoft.PowerShell.Commands.MatchInfo" + } - it "Should return matchinfo type" { - $result = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive - ,$result | Should -BeOfType "Microsoft.PowerShell.Commands.MatchInfo" - } + it "Should be called without an error using ca for casesensitive " { + {$testinputone | Select-String -Pattern "hello" -ca } | Should -Not -Throw + } - it "Should be called without an error using ca for casesensitive " { - {$testinputone | Select-String -Pattern "hello" -ca } | Should -Not -Throw - } + it "Should use the ca alias for casesensitive" { + $firstMatch = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive + $secondMatch = $testinputtwo | Select-String -Pattern "hello" -ca - it "Should use the ca alias for casesensitive" { - $firstMatch = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive - $secondMatch = $testinputtwo | Select-String -Pattern "hello" -ca + $equal = @(Compare-Object $firstMatch $secondMatch).Length -eq 0 + $equal | Should -Be True + } - $equal = @(Compare-Object $firstMatch $secondMatch).Length -eq 0 - $equal | Should -Be True - } + it "Should only return the case sensitive match when the casesensitive switch is used" { + $testinputtwo | Select-String -Pattern "hello" -CaseSensitive | Should -Be "hello" + } - it "Should only return the case sensitive match when the casesensitive switch is used" { - $testinputtwo | Select-String -Pattern "hello" -CaseSensitive | Should -Be "hello" - } + it "Should accept a collection of strings from the input object" { + { Select-String -InputObject "some stuff", "other stuff" -Pattern "other" } | Should -Not -Throw + } - it "Should accept a collection of strings from the input object" { - { Select-String -InputObject "some stuff", "other stuff" -Pattern "other" } | Should -Not -Throw - } + it "Should return system.object when the input object switch is used on a collection" { + $result = Select-String -InputObject "some stuff", "other stuff" -pattern "other" + ,$result | Should -BeOfType "System.Object" + } - it "Should return system.object when the input object switch is used on a collection" { - $result = Select-String -InputObject "some stuff", "other stuff" -pattern "other" - ,$result | Should -BeOfType "System.Object" - } + it "Should return null or empty when the input object switch is used on a collection and the pattern does not exist" { + Select-String -InputObject "some stuff", "other stuff" -Pattern "neither" | Should -BeNullOrEmpty + } - it "Should return null or empty when the input object switch is used on a collection and the pattern does not exist" { - Select-String -InputObject "some stuff", "other stuff" -Pattern "neither" | Should -BeNullOrEmpty - } + it "Should return a bool type when the quiet switch is used" { + ,($testinputtwo | Select-String -Quiet "hello" -CaseSensitive) | Should -BeOfType "System.Boolean" + } - it "Should return a bool type when the quiet switch is used" { - ,($testinputtwo | Select-String -Quiet "hello" -CaseSensitive) | Should -BeOfType "System.Boolean" - } + it "Should be true when select string returns a positive result when the quiet switch is used" { + ($testinputtwo | Select-String -Quiet "hello" -CaseSensitive) | Should -BeTrue + } - it "Should be true when select string returns a positive result when the quiet switch is used" { - ($testinputtwo | Select-String -Quiet "hello" -CaseSensitive) | Should -BeTrue - } - - it "Should be empty when select string does not return a result when the quiet switch is used" { - $testinputtwo | Select-String -Quiet "goodbye" | Should -BeNullOrEmpty - } - - it "Should return an array of non matching strings when the switch of NotMatch is used and the string do not match" { - $testinputone | Select-String -Pattern "goodbye" -NotMatch | Should -BeExactly "hello", "Hello" - } - - it "Should output a string with the first match highlighted" { - if ($Host.UI.SupportsVirtualTerminal) - { - $result = $testinputone | Select-String -Pattern "l" | Out-String - $result | Should -Be "`nhe`e[7ml`e[0mlo`nHe`e[7ml`e[0mlo`n`n" - } - else - { - $result = $testinputone | Select-String -Pattern "l" | Out-String - $result | Should -Be "`nhello`nHello`n`n" - } - } - - it "Should output a string with all matches highlighted when AllMatch is used" { - if ($Host.UI.SupportsVirtualTerminal) - { - $result = $testinputone | Select-String -Pattern "l" -AllMatch | Out-String - $result | Should -Be "`nhe`e[7ml`e[0m`e[7ml`e[0mo`nHe`e[7ml`e[0m`e[7ml`e[0mo`n`n" - } - else - { - $result = $testinputone | Select-String -Pattern "l" -AllMatch | Out-String - $result | Should -Be "`nhello`nHello`n`n" - } - } - - it "Should output a string with the first match highlighted when SimpleMatch is used" { - if ($Host.UI.SupportsVirtualTerminal) - { - $result = $testinputone | Select-String -Pattern "l" -SimpleMatch | Out-String - $result | Should -Be "`nhe`e[7ml`e[0mlo`nHe`e[7ml`e[0mlo`n`n" - } - else - { - $result = $testinputone | Select-String -Pattern "l" -SimpleMatch | Out-String - $result | Should -Be "`nhello`nHello`n`n" - } - } - - it "Should output a string without highlighting when NoEmphasis is used" { - $result = $testinputone | Select-String -Pattern "l" -NoEmphasis | Out-String - $result | Should -Be "`nhello`nHello`n`n" - } - - it "Should return an array of matching strings without virtual terminal sequences" { - $testinputone | Select-String -Pattern "l" | Should -Be "hello", "hello" - } - - It "Should return a string type when -Raw is used" { - $result = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive -Raw - $result | Should -BeOfType "System.String" - } - - It "Should return ParameterBindingException when -Raw and -Quiet are used together" { - { $testinputone | Select-String -Pattern "hello" -Raw -Quiet -ErrorAction Stop } | Should -Throw -ExceptionType ([System.Management.Automation.ParameterBindingException]) - } - } + it "Should be empty when select string does not return a result when the quiet switch is used" { + $testinputtwo | Select-String -Quiet "goodbye" | Should -BeNullOrEmpty + } - Context "Filesystem actions" { - $testDirectory = $TestDrive - $testInputFile = Join-Path -Path $testDirectory -ChildPath testfile1.txt + it "Should return an array of non matching strings when the switch of NotMatch is used and the string do not match" { + $testinputone | Select-String -Pattern "goodbye" -NotMatch | Should -BeExactly "hello", "Hello" + } - BeforeEach { - New-Item $testInputFile -Itemtype "file" -Force -Value "This is a text string, and another string${nl}This is the second line${nl}This is the third line${nl}This is the fourth line${nl}No matches" - } + it "Should output a string with the first match highlighted" { + if ($Host.UI.SupportsVirtualTerminal) + { + $result = $testinputone | Select-String -Pattern "l" | Out-String + $result | Should -Be "`nhe`e[7ml`e[0mlo`nHe`e[7ml`e[0mlo`n`n" + } + else + { + $result = $testinputone | Select-String -Pattern "l" | Out-String + $result | Should -Be "`nhello`nHello`n`n" + } + } - AfterEach { - Remove-Item $testInputFile -Force - } + it "Should output a string with all matches highlighted when AllMatch is used" { + if ($Host.UI.SupportsVirtualTerminal) + { + $result = $testinputone | Select-String -Pattern "l" -AllMatch | Out-String + $result | Should -Be "`nhe`e[7ml`e[0m`e[7ml`e[0mo`nHe`e[7ml`e[0m`e[7ml`e[0mo`n`n" + } + else + { + $result = $testinputone | Select-String -Pattern "l" -AllMatch | Out-String + $result | Should -Be "`nhello`nHello`n`n" + } + } - It "Should return an object when a match is found is the file on only one line" { - $result = Select-String $testInputFile -Pattern "string" - ,$result | Should -BeOfType "System.Object" - } + it "Should output a string with the first match highlighted when SimpleMatch is used" { + if ($Host.UI.SupportsVirtualTerminal) + { + $result = $testinputone | Select-String -Pattern "l" -SimpleMatch | Out-String + $result | Should -Be "`nhe`e[7ml`e[0mlo`nHe`e[7ml`e[0mlo`n`n" + } + else + { + $result = $testinputone | Select-String -Pattern "l" -SimpleMatch | Out-String + $result | Should -Be "`nhello`nHello`n`n" + } + } + + it "Should output a string without highlighting when NoEmphasis is used" { + $result = $testinputone | Select-String -Pattern "l" -NoEmphasis | Out-String + $result | Should -Be "`nhello`nHello`n`n" + } - It "Should return an array when a match is found is the file on several lines" { - $result = Select-String $testInputFile -Pattern "in" - ,$result | Should -BeOfType "System.Array" - $result[0] | Should -BeOfType "Microsoft.PowerShell.Commands.MatchInfo" + it "Should return an array of matching strings without virtual terminal sequences" { + $testinputone | Select-String -Pattern "l" | Should -Be "hello", "hello" + } + + It "Should return a string type when -Raw is used" { + $result = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive -Raw + $result | Should -BeOfType "System.String" + } + + It "Should return ParameterBindingException when -Raw and -Quiet are used together" { + { $testinputone | Select-String -Pattern "hello" -Raw -Quiet -ErrorAction Stop } | Should -Throw -ExceptionType ([System.Management.Automation.ParameterBindingException]) + } } - It "Should return the name of the file and the string that 'string' is found if there is only one lines that has a match" { - $expected = $testInputFile + ":1:This is a text string, and another string" + Context "Filesystem actions" { + $testDirectory = $TestDrive + $testInputFile = Join-Path -Path $testDirectory -ChildPath testfile1.txt - Select-String $testInputFile -Pattern "string" | Should -BeExactly $expected - } + BeforeEach { + New-Item $testInputFile -Itemtype "file" -Force -Value "This is a text string, and another string${nl}This is the second line${nl}This is the third line${nl}This is the fourth line${nl}No matches" + } - It "Should return all strings where 'second' is found in testfile1 if there is only one lines that has a match" { - $expected = $testInputFile + ":2:This is the second line" + AfterEach { + Remove-Item $testInputFile -Force + } - Select-String $testInputFile -Pattern "second"| Should -BeExactly $expected - } + It "Should return an object when a match is found is the file on only one line" { + $result = Select-String $testInputFile -Pattern "string" + ,$result | Should -BeOfType "System.Object" + } - It "Should return all strings where 'in' is found in testfile1 pattern switch is not required" { - $expected1 = "This is a text string, and another string" - $expected2 = "This is the second line" - $expected3 = "This is the third line" - $expected4 = "This is the fourth line" + It "Should return an array when a match is found is the file on several lines" { + $result = Select-String $testInputFile -Pattern "in" + ,$result | Should -BeOfType "System.Array" + $result[0] | Should -BeOfType "Microsoft.PowerShell.Commands.MatchInfo" + } - (Select-String in $testInputFile)[0].Line | Should -BeExactly $expected1 - (Select-String in $testInputFile)[1].Line | Should -BeExactly $expected2 - (Select-String in $testInputFile)[2].Line | Should -BeExactly $expected3 - (Select-String in $testInputFile)[3].Line | Should -BeExactly $expected4 - (Select-String in $testInputFile)[4].Line | Should -BeNullOrEmpty - } + It "Should return the name of the file and the string that 'string' is found if there is only one lines that has a match" { + $expected = $testInputFile + ":1:This is a text string, and another string" - It "Should return empty because 'for' is not found in testfile1 " { - Select-String for $testInputFile | Should -BeNullOrEmpty - } + Select-String $testInputFile -Pattern "string" | Should -BeExactly $expected + } - It "Should return the third line in testfile1 and the lines above and below it " { - $expectedLine = "testfile1.txt:2:This is the second line" - $expectedLineBefore = "testfile1.txt:3:This is the third line" - $expectedLineAfter = "testfile1.txt:4:This is the fourth line" + It "Should return all strings where 'second' is found in testfile1 if there is only one lines that has a match" { + $expected = $testInputFile + ":2:This is the second line" - Select-String third $testInputFile -Context 1 | Should -Match $expectedLine - Select-String third $testInputFile -Context 1 | Should -Match $expectedLineBefore - Select-String third $testInputFile -Context 1 | Should -Match $expectedLineAfter - } + Select-String $testInputFile -Pattern "second"| Should -BeExactly $expected + } - It "Should return the number of matches for 'is' in textfile1 " { - (Select-String is $testInputFile -CaseSensitive).count| Should -Be 4 - } + It "Should return all strings where 'in' is found in testfile1 pattern switch is not required" { + $expected1 = "This is a text string, and another string" + $expected2 = "This is the second line" + $expected3 = "This is the third line" + $expected4 = "This is the fourth line" + + (Select-String in $testInputFile)[0].Line | Should -BeExactly $expected1 + (Select-String in $testInputFile)[1].Line | Should -BeExactly $expected2 + (Select-String in $testInputFile)[2].Line | Should -BeExactly $expected3 + (Select-String in $testInputFile)[3].Line | Should -BeExactly $expected4 + (Select-String in $testInputFile)[4].Line | Should -BeNullOrEmpty + } - It "Should return the third line in testfile1 when a relative path is used" { - $expected = "testfile1.txt:3:This is the third line" + It "Should return empty because 'for' is not found in testfile1 " { + Select-String for $testInputFile | Should -BeNullOrEmpty + } + + It "Should return the third line in testfile1 and the lines above and below it " { + $expectedLine = "testfile1.txt:2:This is the second line" + $expectedLineBefore = "testfile1.txt:3:This is the third line" + $expectedLineAfter = "testfile1.txt:4:This is the fourth line" + + Select-String third $testInputFile -Context 1 | Should -Match $expectedLine + Select-String third $testInputFile -Context 1 | Should -Match $expectedLineBefore + Select-String third $testInputFile -Context 1 | Should -Match $expectedLineAfter + } + + It "Should return the number of matches for 'is' in textfile1 " { + (Select-String is $testInputFile -CaseSensitive).count| Should -Be 4 + } - $relativePath = Join-Path -Path $testDirectory -ChildPath ".." - $relativePath = Join-Path -Path $relativePath -ChildPath $TestDirectory.Name - $relativePath = Join-Path -Path $relativePath -ChildPath testfile1.txt - Select-String third $relativePath | Should -Match $expected - } + It "Should return the third line in testfile1 when a relative path is used" { + $expected = "testfile1.txt:3:This is the third line" - It "Should return the fourth line in testfile1 when a relative path is used" { - $expected = "testfile1.txt:5:No matches" + $relativePath = Join-Path -Path $testDirectory -ChildPath ".." + $relativePath = Join-Path -Path $relativePath -ChildPath $TestDirectory.Name + $relativePath = Join-Path -Path $relativePath -ChildPath testfile1.txt + Select-String third $relativePath | Should -Match $expected + } - Push-Location $testDirectory + It "Should return the fourth line in testfile1 when a relative path is used" { + $expected = "testfile1.txt:5:No matches" - Select-String matches (Join-Path -Path $testDirectory -ChildPath testfile1.txt) | Should -Match $expected - Pop-Location - } + Push-Location $testDirectory - It "Should return the fourth line in testfile1 when a regular expression is used" { - $expected = "testfile1.txt:5:No matches" + Select-String matches (Join-Path -Path $testDirectory -ChildPath testfile1.txt) | Should -Match $expected + Pop-Location + } - Select-String 'matc*' $testInputFile -CaseSensitive | Should -Match $expected - } + It "Should return the fourth line in testfile1 when a regular expression is used" { + $expected = "testfile1.txt:5:No matches" - It "Should return the fourth line in testfile1 when a regular expression is used, using the alias for casesensitive" { - $expected = "testfile1.txt:5:No matches" + Select-String 'matc*' $testInputFile -CaseSensitive | Should -Match $expected + } - Select-String 'matc*' $testInputFile -ca | Should -Match $expected - } + It "Should return the fourth line in testfile1 when a regular expression is used, using the alias for casesensitive" { + $expected = "testfile1.txt:5:No matches" - It "Should return all strings where 'in' is found in testfile1, when -Raw is used." { - $expected1 = "This is a text string, and another string" - $expected2 = "This is the second line" - $expected3 = "This is the third line" - $expected4 = "This is the fourth line" + Select-String 'matc*' $testInputFile -ca | Should -Match $expected + } - (Select-String in $testInputFile -Raw)[0] | Should -BeExactly $expected1 - (Select-String in $testInputFile -Raw)[1] | Should -BeExactly $expected2 - (Select-String in $testInputFile -Raw)[2] | Should -BeExactly $expected3 - (Select-String in $testInputFile -Raw)[3] | Should -BeExactly $expected4 - (Select-String in $testInputFile -Raw)[4] | Should -BeNullOrEmpty - } + It "Should return all strings where 'in' is found in testfile1, when -Raw is used." { + $expected1 = "This is a text string, and another string" + $expected2 = "This is the second line" + $expected3 = "This is the third line" + $expected4 = "This is the fourth line" + + (Select-String in $testInputFile -Raw)[0] | Should -BeExactly $expected1 + (Select-String in $testInputFile -Raw)[1] | Should -BeExactly $expected2 + (Select-String in $testInputFile -Raw)[2] | Should -BeExactly $expected3 + (Select-String in $testInputFile -Raw)[3] | Should -BeExactly $expected4 + (Select-String in $testInputFile -Raw)[4] | Should -BeNullOrEmpty + } - It "Should ignore -Context parameter when -Raw is used." { - $expected = "This is the second line" - Select-String second $testInputFile -Raw -Context 2,2 | Should -BeExactly $expected - } + It "Should ignore -Context parameter when -Raw is used." { + $expected = "This is the second line" + Select-String second $testInputFile -Raw -Context 2,2 | Should -BeExactly $expected + } } Push-Location $currentDirectory } From 52a9c0faddf862355cacfd66c67fc03d7f403eb3 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Mon, 30 Sep 2019 18:22:47 -0700 Subject: [PATCH 24/24] address steve's comments --- .../Select-String.Tests.ps1 | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 index a5dfdda92e7..8a73ddfe8b4 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-String.Tests.ps1 @@ -2,8 +2,10 @@ # Licensed under the MIT License. Describe "Select-String" -Tags "CI" { - $nl = [Environment]::NewLine - $currentDirectory = $pwd.Path + BeforeAll { + $nl = [Environment]::NewLine + $currentDirectory = $pwd.Path + } Context "String actions" { $testinputone = "hello","Hello","goodbye" @@ -13,10 +15,10 @@ Describe "Select-String" -Tags "CI" { { $testinputone | Select-String -Pattern "hello" } | Should -Not -Throw } - it "Should return an array data type when multiple matches are found" { - $result = $testinputtwo | Select-String -Pattern "hello" + it "Should return an array data type when multiple matches are found" { + $result = $testinputtwo | Select-String -Pattern "hello" ,$result | Should -BeOfType "System.Array" - } + } it "Should return an object type when one match is found" { $result = $testinputtwo | Select-String -Pattern "hello" -CaseSensitive @@ -245,5 +247,8 @@ Describe "Select-String" -Tags "CI" { Select-String second $testInputFile -Raw -Context 2,2 | Should -BeExactly $expected } } - Push-Location $currentDirectory + + AfterAll { + Push-Location $currentDirectory + } }