From ff086e5a70cd6fb0f352b9571420ea03f0b8207e Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Fri, 8 Mar 2019 19:25:28 +0100 Subject: [PATCH 01/90] regex expression to read csv string and ignore quotes --- .../commands/utility/Csv.cs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index aba6043d2bb..6584dc35875 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System.Collections.ObjectModel; +using System.Text.RegularExpressions; namespace Microsoft.PowerShell.Commands { @@ -29,6 +30,40 @@ internal CSVHelper(char delimiter) internal Collection ParseCsv(string csv) { Collection result = new Collection(); + + /////////new code with regex///// + + csv = csv.Trim(); + + //string data = "Method,\"value1,value2\",Method2"; + string[] pieces = Regex.Split(csv, ",|(\"[^\"]*\")").Where(exp => !String.IsNullOrEmpty(exp)).ToArray(); + + foreach (string piece in pieces) + { + result.add(piece); + } + + return result; + /* + //using a reader// + if (csv.Length == 0 || csv[0] == '#') + { + return result; + } + + using (StringReader reader = new StringReader(csv.ToString())) + { + while ((readText = await reader.ReadLineAsync()) != null) + { + String line = readText; + //split line + + + } + } + + ////////old code below//////// + string tempString = string.Empty; csv = csv.Trim(); if (csv.Length == 0 || csv[0] == '#') @@ -37,6 +72,10 @@ internal Collection ParseCsv(string csv) } bool inQuote = false; + + // for each character in the file (use stream instead of for loop for speed) + // if the character is a delimiter, + for (int i = 0; i < csv.Length; i++) { char c = csv[i]; @@ -106,6 +145,7 @@ internal Collection ParseCsv(string csv) } return result; + */ } } } From 232f28eec075e557e966c0332c727e4fa1b730bb Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Fri, 8 Mar 2019 19:25:48 +0100 Subject: [PATCH 02/90] added reference --- .../commands/utility/Csv.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 6584dc35875..f1b7d94b96f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -35,7 +35,8 @@ internal Collection ParseCsv(string csv) csv = csv.Trim(); - //string data = "Method,\"value1,value2\",Method2"; + //regex expression: + //https://stackoverflow.com/questions/31118964/delimit-a-string-by-character-unless-within-quotation-marks-c-sharp string[] pieces = Regex.Split(csv, ",|(\"[^\"]*\")").Where(exp => !String.IsNullOrEmpty(exp)).ToArray(); foreach (string piece in pieces) From ff2f7342751df45475d8d39f22eb0b3799756780 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Fri, 8 Mar 2019 19:39:26 +0100 Subject: [PATCH 03/90] cleanup and delimiter set included --- .../commands/utility/Csv.cs | 125 +----------------- 1 file changed, 7 insertions(+), 118 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index f1b7d94b96f..daa0c3e5654 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -28,125 +28,14 @@ internal CSVHelper(char delimiter) /// String to be parsed. /// internal Collection ParseCsv(string csv) - { - Collection result = new Collection(); - - /////////new code with regex///// - - csv = csv.Trim(); - - //regex expression: + { + //performing trimming, just like in the old implementation + csv = csv.Trim(); + //regex expression, inspiration from //https://stackoverflow.com/questions/31118964/delimit-a-string-by-character-unless-within-quotation-marks-c-sharp - string[] pieces = Regex.Split(csv, ",|(\"[^\"]*\")").Where(exp => !String.IsNullOrEmpty(exp)).ToArray(); - - foreach (string piece in pieces) - { - result.add(piece); - } - - return result; - /* - //using a reader// - if (csv.Length == 0 || csv[0] == '#') - { - return result; - } - - using (StringReader reader = new StringReader(csv.ToString())) - { - while ((readText = await reader.ReadLineAsync()) != null) - { - String line = readText; - //split line - - - } - } - - ////////old code below//////// - - string tempString = string.Empty; - csv = csv.Trim(); - if (csv.Length == 0 || csv[0] == '#') - { - return result; - } - - bool inQuote = false; - - // for each character in the file (use stream instead of for loop for speed) - // if the character is a delimiter, - - for (int i = 0; i < csv.Length; i++) - { - char c = csv[i]; - if (c == Delimiter) - { - if (!inQuote) - { - result.Add(tempString); - tempString = string.Empty; - } - else - { - tempString += c; - } - } - else - { - switch (c) - { - case '"': - if (inQuote) - { - // If we are at the end of the string or the end of the segment, create a new value - // Otherwise we have an error - if (i == csv.Length - 1) - { - result.Add(tempString); - tempString = string.Empty; - inQuote = false; - break; - } - - if (csv[i + 1] == Delimiter) - { - result.Add(tempString); - tempString = string.Empty; - inQuote = false; - i++; - } - else if (csv[i + 1] == '"') - { - tempString += '"'; - i++; - } - else - { - inQuote = false; - } - } - else - { - inQuote = true; - } - - break; - - default: - tempString += c; - break; - } - } - } - - if (tempString.Length > 0) - { - result.Add(tempString); - } - - return result; - */ + string[] elements = Regex.Split(csv, Delimiter+"|(\"[^\"]*\")").Where(exp => !String.IsNullOrEmpty(exp)).ToArray(); + Collection result = new Collection(elements); + return result; } } } From 8de7476364cfc7719487fab86111b7094f52c91d Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Fri, 8 Mar 2019 19:54:32 +0100 Subject: [PATCH 04/90] code comment issues from CI fixed --- .../commands/utility/Csv.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index daa0c3e5654..8393806385e 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -29,11 +29,12 @@ internal CSVHelper(char delimiter) /// internal Collection ParseCsv(string csv) { - //performing trimming, just like in the old implementation + + // performing trimming, just like in the old implementation csv = csv.Trim(); - //regex expression, inspiration from - //https://stackoverflow.com/questions/31118964/delimit-a-string-by-character-unless-within-quotation-marks-c-sharp - string[] elements = Regex.Split(csv, Delimiter+"|(\"[^\"]*\")").Where(exp => !String.IsNullOrEmpty(exp)).ToArray(); + // regex expression, inspiration from + // https://stackoverflow.com/questions/31118964/delimit-a-string-by-character-unless-within-quotation-marks-c-sharp + string[] elements = Regex.Split(csv, Delimiter + "|(\"[^\"]*\")").Where(exp => !String.IsNullOrEmpty(exp)).ToArray(); Collection result = new Collection(elements); return result; } From 9fe3089919949378559be34d8c6b272fabb56877 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Fri, 8 Mar 2019 19:58:16 +0100 Subject: [PATCH 05/90] removed comment that gave CI issues, since it is not really necessary --- .../commands/utility/Csv.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 8393806385e..34780d69ecb 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -29,8 +29,6 @@ internal CSVHelper(char delimiter) /// internal Collection ParseCsv(string csv) { - - // performing trimming, just like in the old implementation csv = csv.Trim(); // regex expression, inspiration from // https://stackoverflow.com/questions/31118964/delimit-a-string-by-character-unless-within-quotation-marks-c-sharp From 43aa9357c60e6f96049eccb5946c38205ed73ccf Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Fri, 8 Mar 2019 20:08:36 +0100 Subject: [PATCH 06/90] removal of toArray and wrong String usage --- .../commands/utility/Csv.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 34780d69ecb..6d7122d916f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -30,9 +30,10 @@ internal CSVHelper(char delimiter) internal Collection ParseCsv(string csv) { csv = csv.Trim(); + // regex expression, inspiration from // https://stackoverflow.com/questions/31118964/delimit-a-string-by-character-unless-within-quotation-marks-c-sharp - string[] elements = Regex.Split(csv, Delimiter + "|(\"[^\"]*\")").Where(exp => !String.IsNullOrEmpty(exp)).ToArray(); + string[] elements = Regex.Split(csv, Delimiter + "|(\"[^\"]*\")"); Collection result = new Collection(elements); return result; } From 021c63137c27a9358e49cc275fc5699bfc9ad202 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Fri, 8 Mar 2019 20:18:23 +0100 Subject: [PATCH 07/90] returns empty collection when conditions met reuse of the old implementation --- .../commands/utility/Csv.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 6d7122d916f..483dfbe0aac 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -30,6 +30,10 @@ internal CSVHelper(char delimiter) internal Collection ParseCsv(string csv) { csv = csv.Trim(); + if (csv.Length == 0 || csv[0] == '#') + { + return new Collection(); + } // regex expression, inspiration from // https://stackoverflow.com/questions/31118964/delimit-a-string-by-character-unless-within-quotation-marks-c-sharp From 643a75917bc886205a54790564db8d4634feef71 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 12 Mar 2019 13:10:36 +0100 Subject: [PATCH 08/90] implementation with reader --- .../commands/utility/Csv.cs | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 483dfbe0aac..78a0f9c070d 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -2,7 +2,8 @@ // Licensed under the MIT License. using System.Collections.ObjectModel; -using System.Text.RegularExpressions; +//using System.Text.RegularExpressions; +using Microsoft.VisualBasic.FileIO.TextFieldParser; namespace Microsoft.PowerShell.Commands { @@ -29,16 +30,31 @@ internal CSVHelper(char delimiter) /// internal Collection ParseCsv(string csv) { - csv = csv.Trim(); + csv = csv.Trim(); + Collection result = new Collection(); if (csv.Length == 0 || csv[0] == '#') { - return new Collection(); + return result; } + + var reader = new System.IO.StringReader(sampleText); + using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(reader)) + { + parser.Delimiters = new string[] { "," }; + parser.HasFieldsEnclosedInQuotes = true; // <--- !!! + string[] fields; + while ((fields = parser.ReadFields()) != null) + { + result.Add(fields); + } + } + // regex expression, inspiration from // https://stackoverflow.com/questions/31118964/delimit-a-string-by-character-unless-within-quotation-marks-c-sharp - string[] elements = Regex.Split(csv, Delimiter + "|(\"[^\"]*\")"); - Collection result = new Collection(elements); + // string[] elements = Regex.Split(csv, Delimiter + "|(\"[^\"]*\")"); + // todo: exclude " in split + // Collection result = new Collection(elements); return result; } } From 77a32ea8a6a5c8a69976bfa765f6a148545fbd55 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 12 Mar 2019 15:11:25 +0100 Subject: [PATCH 09/90] implementation with reader removed old commented code and implemented a version using the System.IO.Stringreader --- .../commands/utility/Csv.cs | 46 ++++++++++++------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 78a0f9c070d..2c9032ee0b8 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -3,7 +3,7 @@ using System.Collections.ObjectModel; //using System.Text.RegularExpressions; -using Microsoft.VisualBasic.FileIO.TextFieldParser; +//using System.IO; namespace Microsoft.PowerShell.Commands { @@ -21,6 +21,7 @@ internal CSVHelper(char delimiter) /// Gets or sets the delimiter that separates the values. /// internal char Delimiter { get; } = ','; + internal char Quote { get; } = '"'; /// /// Parse a CSV string. @@ -37,24 +38,37 @@ internal Collection ParseCsv(string csv) return result; } + var reader = new System.IO.StringReader(csv); + var tempString = string.Empty; - var reader = new System.IO.StringReader(sampleText); - using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(reader)) - { - parser.Delimiters = new string[] { "," }; - parser.HasFieldsEnclosedInQuotes = true; // <--- !!! - string[] fields; - while ((fields = parser.ReadFields()) != null) - { - result.Add(fields); + // old implementation but now using the reader class + while(reader.Peek() != -1) { + char nextChar = (char)reader.Read(); + if(nextChar == Delimiter) { + //next character was delimiter found, so add string to collection + result.Add(tempString); + tempString = string.Empty; + } else if(nextChar == Quote) { + //next character was quote, so perform reading untill next quote and add it to tempString + bool inQuote = true; + while(reader.Peek() != -1 && inQuote) { + nextChar = (char)reader.Read(); + if(nextChar == Quote) { + //exit quote found + inQuote = false; + } else { + // add to emptyString + tempString += nextChar; + } + } } } - - // regex expression, inspiration from - // https://stackoverflow.com/questions/31118964/delimit-a-string-by-character-unless-within-quotation-marks-c-sharp - // string[] elements = Regex.Split(csv, Delimiter + "|(\"[^\"]*\")"); - // todo: exclude " in split - // Collection result = new Collection(elements); + // add last word if toAdd is not empty + if(tempString != string.Empty) { + result.Add(tempString); + } + + //return new Collection(); return result; } } From 472416158d1b7ccb6cedf8627837ae8dd4126625 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 12 Mar 2019 15:25:05 +0100 Subject: [PATCH 10/90] codefactor warnings --- .../commands/utility/Csv.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 2c9032ee0b8..57994908868 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -42,21 +42,23 @@ internal Collection ParseCsv(string csv) var tempString = string.Empty; // old implementation but now using the reader class - while(reader.Peek() != -1) { + while (reader.Peek() != -1) { char nextChar = (char)reader.Read(); - if(nextChar == Delimiter) { + if (nextChar == Delimiter) { //next character was delimiter found, so add string to collection result.Add(tempString); tempString = string.Empty; - } else if(nextChar == Quote) { + } + else if(nextChar == Quote) { //next character was quote, so perform reading untill next quote and add it to tempString - bool inQuote = true; - while(reader.Peek() != -1 && inQuote) { + bool isinQuotes = true; + while (reader.Peek() != -1 && isinQuotes) { nextChar = (char)reader.Read(); - if(nextChar == Quote) { + if (nextChar == Quote) { //exit quote found - inQuote = false; - } else { + isinQuotes = false; + } + else { // add to emptyString tempString += nextChar; } From 1854f5351a2bac7c3bafd85316a2aa4859633e95 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 12 Mar 2019 15:34:18 +0100 Subject: [PATCH 11/90] more codefactor suggestions --- .../commands/utility/Csv.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 57994908868..285dd7e5388 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -2,8 +2,7 @@ // Licensed under the MIT License. using System.Collections.ObjectModel; -//using System.Text.RegularExpressions; -//using System.IO; +using System.IO; namespace Microsoft.PowerShell.Commands { @@ -38,33 +37,38 @@ internal Collection ParseCsv(string csv) return result; } - var reader = new System.IO.StringReader(csv); + var reader = new StringReader(csv); var tempString = string.Empty; // old implementation but now using the reader class while (reader.Peek() != -1) { char nextChar = (char)reader.Read(); if (nextChar == Delimiter) { + //next character was delimiter found, so add string to collection result.Add(tempString); tempString = string.Empty; } else if(nextChar == Quote) { + //next character was quote, so perform reading untill next quote and add it to tempString bool isinQuotes = true; while (reader.Peek() != -1 && isinQuotes) { nextChar = (char)reader.Read(); if (nextChar == Quote) { + //exit quote found isinQuotes = false; } else { + // add to emptyString tempString += nextChar; } } } } + // add last word if toAdd is not empty if(tempString != string.Empty) { result.Add(tempString); From d1c90f72295cdbc25a5e5def20884c17bbd60d9a Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 12 Mar 2019 16:05:36 +0100 Subject: [PATCH 12/90] more codefactor suggestions regarding wrongly placed brackets and comments --- .../commands/utility/Csv.cs | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 285dd7e5388..4c8b22a67a6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -41,28 +41,26 @@ internal Collection ParseCsv(string csv) var tempString = string.Empty; // old implementation but now using the reader class - while (reader.Peek() != -1) { + while (reader.Peek() != -1) + { char nextChar = (char)reader.Read(); - if (nextChar == Delimiter) { - //next character was delimiter found, so add string to collection + // if next character was delimiter, add string to collection and reset + if (nextChar == Delimiter) { result.Add(tempString); tempString = string.Empty; } - else if(nextChar == Quote) { - - //next character was quote, so perform reading untill next quote and add it to tempString + // if next character was quote, perform reading untill next quote and add it to tempString + else if (nextChar == Quote) { bool isinQuotes = true; - while (reader.Peek() != -1 && isinQuotes) { + while (reader.Peek() != -1 && isinQuotes) + { nextChar = (char)reader.Read(); + if (nextChar == Quote) { - - //exit quote found isinQuotes = false; } else { - - // add to emptyString tempString += nextChar; } } @@ -70,7 +68,7 @@ internal Collection ParseCsv(string csv) } // add last word if toAdd is not empty - if(tempString != string.Empty) { + if (tempString != string.Empty) { result.Add(tempString); } From a3573ee70511810c58a33c34c7a615901f7a7ef3 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 12 Mar 2019 16:19:41 +0100 Subject: [PATCH 13/90] more codefactor regarding brackets --- .../commands/utility/Csv.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 4c8b22a67a6..e5a60b9d1bf 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -20,6 +20,7 @@ internal CSVHelper(char delimiter) /// Gets or sets the delimiter that separates the values. /// internal char Delimiter { get; } = ','; + internal char Quote { get; } = '"'; /// @@ -46,12 +47,14 @@ internal Collection ParseCsv(string csv) char nextChar = (char)reader.Read(); // if next character was delimiter, add string to collection and reset - if (nextChar == Delimiter) { + // else if next character was quote, perform reading untill next quote and add it to tempString + if (nextChar == Delimiter) + { result.Add(tempString); tempString = string.Empty; } - // if next character was quote, perform reading untill next quote and add it to tempString - else if (nextChar == Quote) { + else if (nextChar == Quote) + { bool isinQuotes = true; while (reader.Peek() != -1 && isinQuotes) { @@ -71,7 +74,7 @@ internal Collection ParseCsv(string csv) if (tempString != string.Empty) { result.Add(tempString); } - + reader.Close(); //return new Collection(); return result; } From 823c5332f7e3eb11e2ce463aacaabdc2ecdd4776 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 12 Mar 2019 16:46:33 +0100 Subject: [PATCH 14/90] remove commented code --- .../commands/utility/Csv.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index e5a60b9d1bf..0b651968ea4 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -74,8 +74,8 @@ internal Collection ParseCsv(string csv) if (tempString != string.Empty) { result.Add(tempString); } - reader.Close(); - //return new Collection(); + + reader.Close(); return result; } } From 6de892fbd8284ca762c90a51b0d26b6d722b3bec Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 12 Mar 2019 17:14:34 +0100 Subject: [PATCH 15/90] added a stringbuilder --- .../commands/utility/Csv.cs | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 0b651968ea4..d5edc40369f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -3,6 +3,7 @@ using System.Collections.ObjectModel; using System.IO; +using System.Text; namespace Microsoft.PowerShell.Commands { @@ -39,19 +40,19 @@ internal Collection ParseCsv(string csv) } var reader = new StringReader(csv); - var tempString = string.Empty; + StringBuilder bld = new StringBuilder(); - // old implementation but now using the reader class while (reader.Peek() != -1) { char nextChar = (char)reader.Read(); - // if next character was delimiter, add string to collection and reset - // else if next character was quote, perform reading untill next quote and add it to tempString + // if next character was delimiter, add string to builder and clear builder + // else if next character was quote, perform reading untill next quote and add it to builder + // else read and add it to builder if (nextChar == Delimiter) { - result.Add(tempString); - tempString = string.Empty; + result.Add(bld.ToString()); + bld.Clear(); } else if (nextChar == Quote) { @@ -64,15 +65,17 @@ internal Collection ParseCsv(string csv) isinQuotes = false; } else { - tempString += nextChar; + bld.Append(nextChar); } } + } else { + bld.Append(nextChar); } } - // add last word if toAdd is not empty - if (tempString != string.Empty) { - result.Add(tempString); + // add last word if remainder is not empty + if (bld.ToString() != string.Empty) { + result.Add(bld.ToString()); } reader.Close(); From 40b7d856a81378f44f88690fb2538149a88f4c43 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 12 Mar 2019 17:17:15 +0100 Subject: [PATCH 16/90] rename for clarity --- .../commands/utility/Csv.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index d5edc40369f..51a7ead0402 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -40,7 +40,7 @@ internal Collection ParseCsv(string csv) } var reader = new StringReader(csv); - StringBuilder bld = new StringBuilder(); + StringBuilder stringBuilder = new StringBuilder(); while (reader.Peek() != -1) { @@ -51,8 +51,8 @@ internal Collection ParseCsv(string csv) // else read and add it to builder if (nextChar == Delimiter) { - result.Add(bld.ToString()); - bld.Clear(); + result.Add(stringBuilder.ToString()); + stringBuilder.Clear(); } else if (nextChar == Quote) { @@ -65,17 +65,17 @@ internal Collection ParseCsv(string csv) isinQuotes = false; } else { - bld.Append(nextChar); + stringBuilder.Append(nextChar); } } } else { - bld.Append(nextChar); + stringBuilder.Append(nextChar); } } // add last word if remainder is not empty - if (bld.ToString() != string.Empty) { - result.Add(bld.ToString()); + if (stringBuilder.ToString() != string.Empty) { + result.Add(stringBuilder.ToString()); } reader.Close(); From 2ca1eecb61bf3553ba387087c62a39d01140d1bd Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 12 Mar 2019 18:47:50 +0100 Subject: [PATCH 17/90] consistency with brackets --- .../commands/utility/Csv.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 51a7ead0402..ab006d6e4b1 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -61,20 +61,25 @@ internal Collection ParseCsv(string csv) { nextChar = (char)reader.Read(); - if (nextChar == Quote) { + if (nextChar == Quote) + { isinQuotes = false; } - else { + else + { stringBuilder.Append(nextChar); } } - } else { + } + else + { stringBuilder.Append(nextChar); } } // add last word if remainder is not empty - if (stringBuilder.ToString() != string.Empty) { + if (stringBuilder.ToString() != string.Empty) + { result.Add(stringBuilder.ToString()); } From 229fd2a9988165a15c42d4b4b7c9dda18ca3f628 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 13 Mar 2019 10:50:12 +0100 Subject: [PATCH 18/90] Implemented changes @Geweldig suggested --- .../commands/utility/Csv.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index ab006d6e4b1..460b35a0165 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -40,19 +40,19 @@ internal Collection ParseCsv(string csv) } var reader = new StringReader(csv); - StringBuilder stringBuilder = new StringBuilder(); + StringBuilder wordBuffer = new StringBuilder(); while (reader.Peek() != -1) { char nextChar = (char)reader.Read(); - // if next character was delimiter, add string to builder and clear builder + // if next character was delimiter, add string to result and clear builder // else if next character was quote, perform reading untill next quote and add it to builder // else read and add it to builder if (nextChar == Delimiter) { - result.Add(stringBuilder.ToString()); - stringBuilder.Clear(); + result.Add(wordBuffer.ToString()); + wordBuffer.Clear(); } else if (nextChar == Quote) { @@ -67,20 +67,20 @@ internal Collection ParseCsv(string csv) } else { - stringBuilder.Append(nextChar); + wordBuffer.Append(nextChar); } } } else { - stringBuilder.Append(nextChar); + wordBuffer.Append(nextChar); } } // add last word if remainder is not empty - if (stringBuilder.ToString() != string.Empty) + if (wordBuffer.ToString() != string.Empty) { - result.Add(stringBuilder.ToString()); + result.Add(wordBuffer.ToString()); } reader.Close(); From 23a78b72d3ff58d016a199a3d4715a949a74bacf Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 13 Mar 2019 15:53:20 +0100 Subject: [PATCH 19/90] iSazonov suggestions implemented --- .../commands/utility/Csv.cs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 460b35a0165..5d2cf465644 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -22,8 +22,6 @@ internal CSVHelper(char delimiter) /// internal char Delimiter { get; } = ','; - internal char Quote { get; } = '"'; - /// /// Parse a CSV string. /// @@ -46,22 +44,22 @@ internal Collection ParseCsv(string csv) { char nextChar = (char)reader.Read(); - // if next character was delimiter, add string to result and clear builder + // if next character was delimiter or we are at the end, add string to result and clear builder // else if next character was quote, perform reading untill next quote and add it to builder // else read and add it to builder - if (nextChar == Delimiter) + if (nextChar == Delimiter || reader.Peek() == -1) { result.Add(wordBuffer.ToString()); wordBuffer.Clear(); } - else if (nextChar == Quote) + else if (nextChar == '"') { bool isinQuotes = true; while (reader.Peek() != -1 && isinQuotes) { nextChar = (char)reader.Read(); - if (nextChar == Quote) + if (nextChar == '"') { isinQuotes = false; } @@ -77,12 +75,6 @@ internal Collection ParseCsv(string csv) } } - // add last word if remainder is not empty - if (wordBuffer.ToString() != string.Empty) - { - result.Add(wordBuffer.ToString()); - } - reader.Close(); return result; } From 8cfa8b2c4e794290c6f3863346533b18994e2749 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 13 Mar 2019 17:18:53 +0100 Subject: [PATCH 20/90] extra parentheses --- .../commands/utility/Csv.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 5d2cf465644..9e3e60229c9 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -47,7 +47,7 @@ internal Collection ParseCsv(string csv) // if next character was delimiter or we are at the end, add string to result and clear builder // else if next character was quote, perform reading untill next quote and add it to builder // else read and add it to builder - if (nextChar == Delimiter || reader.Peek() == -1) + if ((nextChar == Delimiter) || (reader.Peek() == -1)) { result.Add(wordBuffer.ToString()); wordBuffer.Clear(); From f4a59ed63b4290a361371e081a83d1367c204d74 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 13 Mar 2019 17:27:06 +0100 Subject: [PATCH 21/90] old way, adding last string outside of the while loop since the build was breaking --- .../commands/utility/Csv.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 9e3e60229c9..abefb4ba3ba 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -47,7 +47,7 @@ internal Collection ParseCsv(string csv) // if next character was delimiter or we are at the end, add string to result and clear builder // else if next character was quote, perform reading untill next quote and add it to builder // else read and add it to builder - if ((nextChar == Delimiter) || (reader.Peek() == -1)) + if (nextChar == Delimiter) { result.Add(wordBuffer.ToString()); wordBuffer.Clear(); @@ -75,6 +75,11 @@ internal Collection ParseCsv(string csv) } } + string lastWord = wordBuffer.ToString(); + if (lastWord != string.Empty) { + result.Add(lastWord); + } + reader.Close(); return result; } From a08775e1b139cdc1333865eba8cd6ca61cfeea11 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 13 Mar 2019 17:55:12 +0100 Subject: [PATCH 22/90] isinQuotes -> isInQuotes --- .../commands/utility/Csv.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index abefb4ba3ba..d11931054f3 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -30,7 +30,7 @@ internal CSVHelper(char delimiter) /// internal Collection ParseCsv(string csv) { - csv = csv.Trim(); + csv = csv.Trim(); Collection result = new Collection(); if (csv.Length == 0 || csv[0] == '#') { @@ -54,14 +54,14 @@ internal Collection ParseCsv(string csv) } else if (nextChar == '"') { - bool isinQuotes = true; - while (reader.Peek() != -1 && isinQuotes) + bool isInQuotes = true; + while (reader.Peek() != -1 && isInQuotes) { nextChar = (char)reader.Read(); if (nextChar == '"') { - isinQuotes = false; + isInQuotes = false; } else { From 242593314baeadb1bae2de9cc009f9e2a02abf26 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 13 Mar 2019 17:58:14 +0100 Subject: [PATCH 23/90] isInQuotes -> betweenQuotes --- .../commands/utility/Csv.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index d11931054f3..008f1a5d464 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -54,14 +54,14 @@ internal Collection ParseCsv(string csv) } else if (nextChar == '"') { - bool isInQuotes = true; - while (reader.Peek() != -1 && isInQuotes) + bool betweenQuotes = true; + while (reader.Peek() != -1 && betweenQuotes) { nextChar = (char)reader.Read(); if (nextChar == '"') { - isInQuotes = false; + betweenQuotes = false; } else { From d693032b0e44f5aa3f44ef0e7c3e8ef99ee5accd Mon Sep 17 00:00:00 2001 From: Ilya Date: Wed, 13 Mar 2019 18:02:18 +0100 Subject: [PATCH 24/90] Update src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs Co-Authored-By: SytzeAndr --- .../commands/utility/Csv.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 008f1a5d464..503071e98a6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -76,7 +76,8 @@ internal Collection ParseCsv(string csv) } string lastWord = wordBuffer.ToString(); - if (lastWord != string.Empty) { + if (lastWord != string.Empty) + { result.Add(lastWord); } From 888479d60fe800fedca3df4c900ba8c57d4ac1c9 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 13 Mar 2019 18:25:28 +0100 Subject: [PATCH 25/90] betweenQuotes -> inQuotes --- .../commands/utility/Csv.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 008f1a5d464..92959b39ab5 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -54,14 +54,14 @@ internal Collection ParseCsv(string csv) } else if (nextChar == '"') { - bool betweenQuotes = true; - while (reader.Peek() != -1 && betweenQuotes) + bool inQuotes = true; + while (reader.Peek() != -1 && inQuotes) { nextChar = (char)reader.Read(); if (nextChar == '"') { - betweenQuotes = false; + inQuotes = false; } else { From 5d9dc0a4edf18573fe6ea7511b5ab12b6ea90e7e Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sun, 17 Mar 2019 20:16:11 +0100 Subject: [PATCH 26/90] doesn't escape when two quotes are followed, untill -> until --- .../commands/utility/Csv.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index 302dc49dafd..d09e8b0147d 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -45,7 +45,7 @@ internal Collection ParseCsv(string csv) char nextChar = (char)reader.Read(); // if next character was delimiter or we are at the end, add string to result and clear builder - // else if next character was quote, perform reading untill next quote and add it to builder + // else if next character was quote, perform reading until next quote and add it to builder // else read and add it to builder if (nextChar == Delimiter) { @@ -61,7 +61,13 @@ internal Collection ParseCsv(string csv) if (nextChar == '"') { - inQuotes = false; + if(reader.Peek() == '"') + { + wordBuffer.Append(nextChar); + reader.Read(); + } else { + inQuotes = false; + } } else { From 8a0a5b176bcfbe049f800c0c0e8a292ceb0d5dd7 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sun, 17 Mar 2019 20:18:41 +0100 Subject: [PATCH 27/90] (char) before peek() and codefactor bracket warning fix --- .../commands/utility/Csv.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index d09e8b0147d..aece8558bd5 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -44,9 +44,9 @@ internal Collection ParseCsv(string csv) { char nextChar = (char)reader.Read(); - // if next character was delimiter or we are at the end, add string to result and clear builder - // else if next character was quote, perform reading until next quote and add it to builder - // else read and add it to builder + // if next character was delimiter or we are at the end, add string to result and clear wordBuffer + // else if next character was quote, perform reading until next quote and add it to wordBuffer + // else read and add it to wordBuffer if (nextChar == Delimiter) { result.Add(wordBuffer.ToString()); @@ -55,17 +55,22 @@ internal Collection ParseCsv(string csv) else if (nextChar == '"') { bool inQuotes = true; + // if we are within a quote section, read and append to wordBuffer until we find a next quote that is not followed by another quote + // if it is a single quote, escape the quote section + // if the quote is followed by an other quote, do not escape and add a quote character to wordBuffer while (reader.Peek() != -1 && inQuotes) { nextChar = (char)reader.Read(); if (nextChar == '"') { - if(reader.Peek() == '"') + if ((char)reader.Peek() == '"') { wordBuffer.Append(nextChar); reader.Read(); - } else { + } + else + { inQuotes = false; } } From 9ec85dae4fa155d86a0c3b6c838ddd9e3f32eb0b Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sun, 17 Mar 2019 20:26:01 +0100 Subject: [PATCH 28/90] codefactor: new line before comment --- .../commands/utility/Csv.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs index aece8558bd5..622cef148e5 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs @@ -55,6 +55,7 @@ internal Collection ParseCsv(string csv) else if (nextChar == '"') { bool inQuotes = true; + // if we are within a quote section, read and append to wordBuffer until we find a next quote that is not followed by another quote // if it is a single quote, escape the quote section // if the quote is followed by an other quote, do not escape and add a quote character to wordBuffer From 180f4aee5cf86723ddf47072e3be95e6d5f24f45 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Fri, 29 Mar 2019 15:57:14 +0100 Subject: [PATCH 29/90] added more tests regarding parsing difficult aliases. However, the test for including a quote character fails, as illegal characters are in the path --- .../Import-Alias.Tests.ps1 | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 1b3959e72df..d3f7a3ec42d 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -46,16 +46,32 @@ Describe "Import-Alias DRT Unit Tests" -Tags "CI" { Describe "Import-Alias" -Tags "CI" { $newLine=[Environment]::NewLine $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory - $testAliases = "pesteralias.txt" - $pesteraliasfile = Join-Path -Path $testAliasDirectory -ChildPath $testAliases + $testAliases = "pesteralias.txt" + + # import alias fails for '"abc""def"': it throws an "Illegal characters in path" error + $difficultToParseString_1 = '"abc""def"' # throws illegal character exception + # $difficultToParseString_1 = 'abc""def' # does not throw exception + + $difficultToParseString_2 = '"aaa"' + $difficultToParseString_3 = '"a,b"' + $pesteraliasfile = Join-Path -Path $testAliasDirectory -ChildPath $testAliases BeforeEach { + # create default pester testing file + # the file assigns "pesterecho" as an alias to "echo" New-Item -Path $testAliasDirectory -ItemType Directory -Force $pesteraliascontent ='# Alias File'+$newLine $pesteraliascontent+='# Exported by : alex'+$newLine $pesteraliascontent+='# Date/Time : Thursday, 12 November 2015 21:55:08'+$newLine - $pesteraliascontent+='# Computer : archvm'+$newLine+'"pesterecho","echo","","None"' + $pesteraliascontent+='# Computer : archvm'#+$newLine+$difficultToParseString+',"echo","","None"' + + # add various aliases for echo which we can then test + + $pesteraliascontent+= $newLine+'pesterecho,"echo","","None"' + $pesteraliascontent+= $newLine+$difficultToParseString_1+',"echo","","None"' + $pesteraliascontent+= $newLine+$difficultToParseString_2+',"echo","","None"' + $pesteraliascontent+= $newLine+$difficultToParseString_3+',"echo","","None"' $pesteraliascontent > $pesteraliasfile } @@ -80,4 +96,28 @@ Describe "Import-Alias" -Tags "CI" { (ipal $pesteraliasfile) (pesterecho pestertesting) | Should -BeExactly "pestertesting" } + + It "Should be able to parse ""abc""""def"" into abc""def " { + (Import-Alias $pesteraliasfile) + # By setting a breakpoint after we perform get-alias, we can check how the string is parsed + # A 'nicer' test would be to check if the parsed string is in the output of get-alias, + # however, the way in which get-alias outputs doesn't have a nice contains() method which can check this (as far as I could see) so I therefore chose to leave this for future work. + # $allAlias = (get-alias) + $callingDifficultString = (abc`"def pestertesting) + $callingDifficultString | Should -BeExactly "pestertesting" + } + + It "Should be able to parse ""aaa"" into aaa " { + (Import-Alias $pesteraliasfile) + $allAlias = (get-alias) + $callingDifficultString = (aaa pestertesting) + $callingDifficultString | Should -BeExactly "pestertesting" + } + + It "Should be able to parse ""a,b"" into a,b " { + (Import-Alias $pesteraliasfile) + # $allAlias = (get-alias) + $callingDifficultString = (a`,b pestertesting) + $callingDifficultString | Should -BeExactly "pestertesting" + } } From 674425d67c9ff4ea9d1ea2e065efdf523eabd88e Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Fri, 29 Mar 2019 16:41:08 +0100 Subject: [PATCH 30/90] added tests for parsing invalid files: should throw an error --- .../Import-Alias.Tests.ps1 | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index d3f7a3ec42d..4c348736161 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -47,17 +47,19 @@ Describe "Import-Alias" -Tags "CI" { $newLine=[Environment]::NewLine $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory $testAliases = "pesteralias.txt" + $aliasFilenameMoreThanFourValues = "aliasFileMoreThanFourValues.txt" + $aliasFilenameLessThanFourValues = "aliasFileLessThanFourValues.txt" - # import alias fails for '"abc""def"': it throws an "Illegal characters in path" error - $difficultToParseString_1 = '"abc""def"' # throws illegal character exception - # $difficultToParseString_1 = 'abc""def' # does not throw exception - + $difficultToParseString_1 = '"abc""def"' $difficultToParseString_2 = '"aaa"' $difficultToParseString_3 = '"a,b"' $pesteraliasfile = Join-Path -Path $testAliasDirectory -ChildPath $testAliases + $aliasPathMoreThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameMoreThanFourValues + $aliasPathLessThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameLessThanFourValues BeforeEach { # create default pester testing file + # has three lines of comments, then a few different aliases for the echo command. # the file assigns "pesterecho" as an alias to "echo" New-Item -Path $testAliasDirectory -ItemType Directory -Force @@ -73,6 +75,16 @@ Describe "Import-Alias" -Tags "CI" { $pesteraliascontent+= $newLine+$difficultToParseString_2+',"echo","","None"' $pesteraliascontent+= $newLine+$difficultToParseString_3+',"echo","","None"' $pesteraliascontent > $pesteraliasfile + + # create invalid file with more than four values + New-Item -Path $testAliasDirectory -ItemType Directory -Force + $pesteraliascontent+= $newLine+'"v_1","v_2","v_3","v_4","v_5"' + $pesteraliascontent > $aliasPathMoreThanFourValues + + # create invalid file with less than four values + New-Item -Path $testAliasDirectory -ItemType Directory -Force + $pesteraliascontent+= $newLine+'"v_1","v_2","v_3"' + $pesteraliascontent > $aliasPathLessThanFourValues } AfterEach { @@ -99,25 +111,29 @@ Describe "Import-Alias" -Tags "CI" { It "Should be able to parse ""abc""""def"" into abc""def " { (Import-Alias $pesteraliasfile) - # By setting a breakpoint after we perform get-alias, we can check how the string is parsed - # A 'nicer' test would be to check if the parsed string is in the output of get-alias, + # Note: A 'nicer' test would maybe be to check if the parsed string is in the output of get-alias, # however, the way in which get-alias outputs doesn't have a nice contains() method which can check this (as far as I could see) so I therefore chose to leave this for future work. - # $allAlias = (get-alias) $callingDifficultString = (abc`"def pestertesting) $callingDifficultString | Should -BeExactly "pestertesting" } It "Should be able to parse ""aaa"" into aaa " { (Import-Alias $pesteraliasfile) - $allAlias = (get-alias) $callingDifficultString = (aaa pestertesting) $callingDifficultString | Should -BeExactly "pestertesting" } It "Should be able to parse ""a,b"" into a,b " { (Import-Alias $pesteraliasfile) - # $allAlias = (get-alias) $callingDifficultString = (a`,b pestertesting) $callingDifficultString | Should -BeExactly "pestertesting" } + + It "Should throw an error when reading more than four values" { + { Import-Alias $aliasPathMoreThanFourValues } | Should -throw + } + + It "Should throw an error when reading less than four values" { + { Import-Alias $aliasPathLessThanFourValues } | Should -throw + } } From 5c2835bf9dc93e891c7456208499408f0d2eaddc Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sat, 30 Mar 2019 15:09:20 +0100 Subject: [PATCH 31/90] iSazonov suggestions for testing: added errorID, asserts are now checked by get-alias instead of executing the alias, which is a cleaner method to check whether the alias is imported correctly --- .../Import-Alias.Tests.ps1 | 94 ++++++++++--------- 1 file changed, 50 insertions(+), 44 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 4c348736161..74df9670f69 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -44,20 +44,32 @@ Describe "Import-Alias DRT Unit Tests" -Tags "CI" { } Describe "Import-Alias" -Tags "CI" { - $newLine=[Environment]::NewLine - $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory - $testAliases = "pesteralias.txt" - $aliasFilenameMoreThanFourValues = "aliasFileMoreThanFourValues.txt" - $aliasFilenameLessThanFourValues = "aliasFileLessThanFourValues.txt" - - $difficultToParseString_1 = '"abc""def"' - $difficultToParseString_2 = '"aaa"' - $difficultToParseString_3 = '"a,b"' - $pesteraliasfile = Join-Path -Path $testAliasDirectory -ChildPath $testAliases - $aliasPathMoreThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameMoreThanFourValues - $aliasPathLessThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameLessThanFourValues - - BeforeEach { + $testAliasDirectory + $pesteraliasfile + $aliasPathMoreThanFourValues + $aliasPathLessThanFourValues + $commandToAlias + + BeforeAll { + $newLine=[Environment]::NewLine + # set paths and names for the alias files + $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory + $testAliases = "pesteralias.txt" + $aliasFilenameMoreThanFourValues = "aliasFileMoreThanFourValues.txt" + $aliasFilenameLessThanFourValues = "aliasFileLessThanFourValues.txt" + + $pesteraliasfile = Join-Path -Path $testAliasDirectory -ChildPath $testAliases + $aliasPathMoreThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameMoreThanFourValues + $aliasPathLessThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameLessThanFourValues + + # define command to alias for the tests + $commandToAlias = "echo" + + # write the files and content + $difficultToParseString_1 = '"abc""def"' + $difficultToParseString_2 = '"aaa"' + $difficultToParseString_3 = '"a,b"' + # create default pester testing file # has three lines of comments, then a few different aliases for the echo command. # the file assigns "pesterecho" as an alias to "echo" @@ -66,14 +78,13 @@ Describe "Import-Alias" -Tags "CI" { $pesteraliascontent ='# Alias File'+$newLine $pesteraliascontent+='# Exported by : alex'+$newLine $pesteraliascontent+='# Date/Time : Thursday, 12 November 2015 21:55:08'+$newLine - $pesteraliascontent+='# Computer : archvm'#+$newLine+$difficultToParseString+',"echo","","None"' + $pesteraliascontent+='# Computer : archvm' # add various aliases for echo which we can then test - - $pesteraliascontent+= $newLine+'pesterecho,"echo","","None"' - $pesteraliascontent+= $newLine+$difficultToParseString_1+',"echo","","None"' - $pesteraliascontent+= $newLine+$difficultToParseString_2+',"echo","","None"' - $pesteraliascontent+= $newLine+$difficultToParseString_3+',"echo","","None"' + $pesteraliascontent+= $newLine+'pesterecho,"'+$commandToAlias+'","","None"' + $pesteraliascontent+= $newLine+$difficultToParseString_1+',"'+$commandToAlias+'","","None"' + $pesteraliascontent+= $newLine+$difficultToParseString_2+',"'+$commandToAlias+'","","None"' + $pesteraliascontent+= $newLine+$difficultToParseString_3+',"'+$commandToAlias+'","","None"' $pesteraliascontent > $pesteraliasfile # create invalid file with more than four values @@ -87,7 +98,7 @@ Describe "Import-Alias" -Tags "CI" { $pesteraliascontent > $aliasPathLessThanFourValues } - AfterEach { + AfterAll { Remove-Item -Path $testAliasDirectory -Recurse -Force } @@ -95,45 +106,40 @@ Describe "Import-Alias" -Tags "CI" { { Import-Alias $pesteraliasfile } | Should -Not -throw } - It "Should be able to import file via the Import-Alias alias of ipal" { - { ipal $pesteraliasfile } | Should -Not -throw + It "Should classify an alias as non existent when it is not imported yet" { + {get-alias pesterecho} | Should --BeExactly null } - It "Should be able to import an alias file and perform imported aliased echo cmd" { - (Import-Alias $pesteraliasfile) - (pesterecho pestertesting) | Should -BeExactly "pestertesting" - } - - It "Should be able to use ipal alias to import an alias file and perform cmd" { - (ipal $pesteraliasfile) - (pesterecho pestertesting) | Should -BeExactly "pestertesting" + It "Should be able to import an alias file and recognize an imported alias" { + $aliasToTest = "pesterecho" + Import-Alias $pesteraliasfile + (get-alias $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias } It "Should be able to parse ""abc""""def"" into abc""def " { - (Import-Alias $pesteraliasfile) - # Note: A 'nicer' test would maybe be to check if the parsed string is in the output of get-alias, - # however, the way in which get-alias outputs doesn't have a nice contains() method which can check this (as far as I could see) so I therefore chose to leave this for future work. - $callingDifficultString = (abc`"def pestertesting) - $callingDifficultString | Should -BeExactly "pestertesting" + $aliasToTest = 'abc"def' + Import-Alias $pesteraliasfile + (get-alias $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias } It "Should be able to parse ""aaa"" into aaa " { - (Import-Alias $pesteraliasfile) - $callingDifficultString = (aaa pestertesting) - $callingDifficultString | Should -BeExactly "pestertesting" + $aliasToTest = "aaa" + Import-Alias $pesteraliasfile + (get-alias $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias + } It "Should be able to parse ""a,b"" into a,b " { - (Import-Alias $pesteraliasfile) - $callingDifficultString = (a`,b pestertesting) - $callingDifficultString | Should -BeExactly "pestertesting" + $aliasToTest = "a,b" + Import-Alias $pesteraliasfile + (get-alias $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias } It "Should throw an error when reading more than four values" { - { Import-Alias $aliasPathMoreThanFourValues } | Should -throw + { Import-Alias $aliasPathMoreThanFourValues } | Should -Throw -ErrorId "ImportAliasFileFormatError,Microsoft.PowerShell.Commands.ImportAliasCommand" } It "Should throw an error when reading less than four values" { - { Import-Alias $aliasPathLessThanFourValues } | Should -throw + { Import-Alias $aliasPathLessThanFourValues } | Should -Throw -ErrorId "ImportAliasFileFormatError,Microsoft.PowerShell.Commands.ImportAliasCommand" } } From 0b2237b88c48195b55e13cc5245977880588fe8c Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sun, 31 Mar 2019 13:32:38 +0200 Subject: [PATCH 32/90] iSasonov suggestions: adding explicit parameters, fixed type regarding --BeExactly --- .../Import-Alias.Tests.ps1 | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 74df9670f69..af526cfbb5d 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -73,7 +73,7 @@ Describe "Import-Alias" -Tags "CI" { # create default pester testing file # has three lines of comments, then a few different aliases for the echo command. # the file assigns "pesterecho" as an alias to "echo" - New-Item -Path $testAliasDirectory -ItemType Directory -Force + New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null $pesteraliascontent ='# Alias File'+$newLine $pesteraliascontent+='# Exported by : alex'+$newLine @@ -88,12 +88,12 @@ Describe "Import-Alias" -Tags "CI" { $pesteraliascontent > $pesteraliasfile # create invalid file with more than four values - New-Item -Path $testAliasDirectory -ItemType Directory -Force + New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null $pesteraliascontent+= $newLine+'"v_1","v_2","v_3","v_4","v_5"' $pesteraliascontent > $aliasPathMoreThanFourValues # create invalid file with less than four values - New-Item -Path $testAliasDirectory -ItemType Directory -Force + New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null $pesteraliascontent+= $newLine+'"v_1","v_2","v_3"' $pesteraliascontent > $aliasPathLessThanFourValues } @@ -103,43 +103,43 @@ Describe "Import-Alias" -Tags "CI" { } It "Should be able to import an alias file successfully" { - { Import-Alias $pesteraliasfile } | Should -Not -throw + {Import-Alias -Path $pesteraliasfile} | Should -Not -throw } It "Should classify an alias as non existent when it is not imported yet" { - {get-alias pesterecho} | Should --BeExactly null + {Get-Alias -Name pesterecho} | Should -Be null } It "Should be able to import an alias file and recognize an imported alias" { $aliasToTest = "pesterecho" - Import-Alias $pesteraliasfile - (get-alias $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias + Import-Alias -Path $pesteraliasfile + (Get-Alias -Name $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias } It "Should be able to parse ""abc""""def"" into abc""def " { $aliasToTest = 'abc"def' - Import-Alias $pesteraliasfile - (get-alias $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias + Import-Alias -Path $pesteraliasfile + (Get-Alias -Name $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias } It "Should be able to parse ""aaa"" into aaa " { $aliasToTest = "aaa" - Import-Alias $pesteraliasfile - (get-alias $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias + Import-Alias -Path $pesteraliasfile + (Get-Alias -Name $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias } It "Should be able to parse ""a,b"" into a,b " { $aliasToTest = "a,b" - Import-Alias $pesteraliasfile - (get-alias $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias + Import-Alias -Path $pesteraliasfile + (Get-Alias -Name $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias } It "Should throw an error when reading more than four values" { - { Import-Alias $aliasPathMoreThanFourValues } | Should -Throw -ErrorId "ImportAliasFileFormatError,Microsoft.PowerShell.Commands.ImportAliasCommand" + { Import-Alias -Path $aliasPathMoreThanFourValues } | Should -Throw -ErrorId "ImportAliasFileFormatError,Microsoft.PowerShell.Commands.ImportAliasCommand" } It "Should throw an error when reading less than four values" { - { Import-Alias $aliasPathLessThanFourValues } | Should -Throw -ErrorId "ImportAliasFileFormatError,Microsoft.PowerShell.Commands.ImportAliasCommand" + { Import-Alias -Path $aliasPathLessThanFourValues } | Should -Throw -ErrorId "ImportAliasFileFormatError,Microsoft.PowerShell.Commands.ImportAliasCommand" } } From 8013cde2557a6ee98f44ccd63a0a20c065a83718 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sun, 31 Mar 2019 13:35:46 +0200 Subject: [PATCH 33/90] wrong indent fixed --- .../Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index af526cfbb5d..f51a4d88f4d 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -46,7 +46,7 @@ Describe "Import-Alias DRT Unit Tests" -Tags "CI" { Describe "Import-Alias" -Tags "CI" { $testAliasDirectory $pesteraliasfile - $aliasPathMoreThanFourValues + $aliasPathMoreThanFourValues $aliasPathLessThanFourValues $commandToAlias From d2f131137979c2d5e970b3524ef99dc8635e3338 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sun, 31 Mar 2019 14:04:54 +0200 Subject: [PATCH 34/90] removed the initializations and fixed the test for get-alias when none is present. The tests should now pass --- .../Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index f51a4d88f4d..69a5fb45d36 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -44,11 +44,6 @@ Describe "Import-Alias DRT Unit Tests" -Tags "CI" { } Describe "Import-Alias" -Tags "CI" { - $testAliasDirectory - $pesteraliasfile - $aliasPathMoreThanFourValues - $aliasPathLessThanFourValues - $commandToAlias BeforeAll { $newLine=[Environment]::NewLine @@ -103,11 +98,11 @@ Describe "Import-Alias" -Tags "CI" { } It "Should be able to import an alias file successfully" { - {Import-Alias -Path $pesteraliasfile} | Should -Not -throw + {Import-Alias -Path $pesteraliasfile} | Should -Not -Throw } It "Should classify an alias as non existent when it is not imported yet" { - {Get-Alias -Name pesterecho} | Should -Be null + Get-Alias -Name invalid_alias -ErrorAction SilentlyContinue | Should -BeExactly $null } It "Should be able to import an alias file and recognize an imported alias" { From a1ccb76afd31a8e6b03cac9fa1fee46046677d11 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sun, 31 Mar 2019 22:25:33 +0200 Subject: [PATCH 35/90] iSazonov suggestions about comments, and I renamed some variables --- .../Import-Alias.Tests.ps1 | 71 +++++++++---------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 69a5fb45d36..fe718909672 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -2,8 +2,8 @@ # Licensed under the MIT License. Describe "Import-Alias DRT Unit Tests" -Tags "CI" { $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory - $testAliases = "TestAliases" - $fulltestpath = Join-Path -Path $testAliasDirectory -ChildPath $testAliases + $aliasFilename = "aliasFilename" + $fulltestpath = Join-Path -Path $testAliasDirectory -ChildPath $aliasFilename BeforeEach { New-Item -Path $testAliasDirectory -ItemType Directory -Force @@ -47,50 +47,47 @@ Describe "Import-Alias" -Tags "CI" { BeforeAll { $newLine=[Environment]::NewLine - # set paths and names for the alias files + $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory - $testAliases = "pesteralias.txt" - $aliasFilenameMoreThanFourValues = "aliasFileMoreThanFourValues.txt" - $aliasFilenameLessThanFourValues = "aliasFileLessThanFourValues.txt" + $aliasFilename = "pesteralias.txt" + $aliasFilenameMoreThanFourValues = "aliasFileMoreThanFourValues.txt" + $aliasFilenameLessThanFourValues = "aliasFileLessThanFourValues.txt" - $pesteraliasfile = Join-Path -Path $testAliasDirectory -ChildPath $testAliases - $aliasPathMoreThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameMoreThanFourValues - $aliasPathLessThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameLessThanFourValues + $aliasfile = Join-Path -Path $testAliasDirectory -ChildPath $aliasFilename + $aliasPathMoreThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameMoreThanFourValues + $aliasPathLessThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameLessThanFourValues - # define command to alias for the tests $commandToAlias = "echo" + $alias1 = "pesterecho" + $alias2 = '"abc""def"' + $alias3 = '"aaa"' + $alias4 = '"a,b"' - # write the files and content - $difficultToParseString_1 = '"abc""def"' - $difficultToParseString_2 = '"aaa"' - $difficultToParseString_3 = '"a,b"' - - # create default pester testing file - # has three lines of comments, then a few different aliases for the echo command. - # the file assigns "pesterecho" as an alias to "echo" + # create alias file New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null - $pesteraliascontent ='# Alias File'+$newLine - $pesteraliascontent+='# Exported by : alex'+$newLine - $pesteraliascontent+='# Date/Time : Thursday, 12 November 2015 21:55:08'+$newLine - $pesteraliascontent+='# Computer : archvm' + # set header + $aliasFileContent ='# Alias File'+$newLine + $aliasFileContent+='# Exported by : alex'+$newLine + $aliasFileContent+='# Date/Time : Thursday, 12 November 2015 21:55:08'+$newLine + $aliasFileContent+='# Computer : archvm' - # add various aliases for echo which we can then test - $pesteraliascontent+= $newLine+'pesterecho,"'+$commandToAlias+'","","None"' - $pesteraliascontent+= $newLine+$difficultToParseString_1+',"'+$commandToAlias+'","","None"' - $pesteraliascontent+= $newLine+$difficultToParseString_2+',"'+$commandToAlias+'","","None"' - $pesteraliascontent+= $newLine+$difficultToParseString_3+',"'+$commandToAlias+'","","None"' - $pesteraliascontent > $pesteraliasfile + # add various aliases + $aliasFileContent+= $newLine+$alias1+',"'+$commandToAlias+'","","None"' + $aliasFileContent+= $newLine+$alias2+',"'+$commandToAlias+'","","None"' + $aliasFileContent+= $newLine+$alias3+',"'+$commandToAlias+'","","None"' + $aliasFileContent+= $newLine+$alias4+',"'+$commandToAlias+'","","None"' + $aliasFileContent > $aliasfile # create invalid file with more than four values New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null - $pesteraliascontent+= $newLine+'"v_1","v_2","v_3","v_4","v_5"' - $pesteraliascontent > $aliasPathMoreThanFourValues + $aliasFileContent+= $newLine+'"v_1","v_2","v_3","v_4","v_5"' + $aliasFileContent > $aliasPathMoreThanFourValues # create invalid file with less than four values New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null - $pesteraliascontent+= $newLine+'"v_1","v_2","v_3"' - $pesteraliascontent > $aliasPathLessThanFourValues + $aliasFileContent+= $newLine+'"v_1","v_2","v_3"' + $aliasFileContent > $aliasPathLessThanFourValues } AfterAll { @@ -98,7 +95,7 @@ Describe "Import-Alias" -Tags "CI" { } It "Should be able to import an alias file successfully" { - {Import-Alias -Path $pesteraliasfile} | Should -Not -Throw + {Import-Alias -Path $aliasfile} | Should -Not -Throw } It "Should classify an alias as non existent when it is not imported yet" { @@ -107,26 +104,26 @@ Describe "Import-Alias" -Tags "CI" { It "Should be able to import an alias file and recognize an imported alias" { $aliasToTest = "pesterecho" - Import-Alias -Path $pesteraliasfile + Import-Alias -Path $aliasfile (Get-Alias -Name $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias } It "Should be able to parse ""abc""""def"" into abc""def " { $aliasToTest = 'abc"def' - Import-Alias -Path $pesteraliasfile + Import-Alias -Path $aliasfile (Get-Alias -Name $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias } It "Should be able to parse ""aaa"" into aaa " { $aliasToTest = "aaa" - Import-Alias -Path $pesteraliasfile + Import-Alias -Path $aliasfile (Get-Alias -Name $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias } It "Should be able to parse ""a,b"" into a,b " { $aliasToTest = "a,b" - Import-Alias -Path $pesteraliasfile + Import-Alias -Path $aliasfile (Get-Alias -Name $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias } From ee4d5149019f4d7fbb2bcb363f8faaaf10db8236 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sun, 31 Mar 2019 22:30:17 +0200 Subject: [PATCH 36/90] reference to variable instead of to a string --- .../Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index fe718909672..23407f4626c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -103,9 +103,8 @@ Describe "Import-Alias" -Tags "CI" { } It "Should be able to import an alias file and recognize an imported alias" { - $aliasToTest = "pesterecho" Import-Alias -Path $aliasfile - (Get-Alias -Name $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias + (Get-Alias -Name $alias1 -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias } It "Should be able to parse ""abc""""def"" into abc""def " { @@ -118,7 +117,6 @@ Describe "Import-Alias" -Tags "CI" { $aliasToTest = "aaa" Import-Alias -Path $aliasfile (Get-Alias -Name $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias - } It "Should be able to parse ""a,b"" into a,b " { From c17d374bb1f98ff43d59a2d3e06399f3a44ed1e1 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sun, 31 Mar 2019 22:48:01 +0200 Subject: [PATCH 37/90] instead of adding the invalid aliases to the content to create an invalid file, it now creates a new file only containing the invalid alias --- .../Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 23407f4626c..19b00238b55 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -81,12 +81,12 @@ Describe "Import-Alias" -Tags "CI" { # create invalid file with more than four values New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null - $aliasFileContent+= $newLine+'"v_1","v_2","v_3","v_4","v_5"' + $aliasFileContent = $newLine+'"v_1","v_2","v_3","v_4","v_5"' $aliasFileContent > $aliasPathMoreThanFourValues # create invalid file with less than four values New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null - $aliasFileContent+= $newLine+'"v_1","v_2","v_3"' + $aliasFileContent = $newLine+'"v_1","v_2","v_3"' $aliasFileContent > $aliasPathLessThanFourValues } From f674fca5cf7adc10c1df2c0cd1e48248d366f104 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 2 Apr 2019 15:51:29 +0200 Subject: [PATCH 38/90] first version of an implementation base d upon the pseudocode from my last comment in #9091. Options has been done a little bit differently at th e moment --- .../commands/utility/ImportAliasCommand.cs | 271 +++++++++++++----- 1 file changed, 207 insertions(+), 64 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index 8ccd9a3a834..fca5ac284d7 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -8,6 +8,7 @@ using System.Management.Automation; using System.Management.Automation.Internal; using System.Security; +using System.Text; namespace Microsoft.PowerShell.Commands { @@ -290,104 +291,244 @@ private Collection GetAliasesFromFile(bool isLiteralPath) { Collection result = new Collection(); - string filePath = null; - using (StreamReader reader = OpenFile(out filePath, isLiteralPath)) + // it seems like filePath is always null and never defined, so we can remove it as far as I could see + // string filePath = null; + + using (StreamReader reader = OpenFile(isLiteralPath)) { - CSVHelper csvHelper = new CSVHelper(','); + //CSVHelper csvHelper = new CSVHelper(','); Int64 lineNumber = 0; - string line = null; + string line;// = null; while ((line = reader.ReadLine()) != null) { ++lineNumber; - // Ignore blank lines - if (line.Length == 0) - { + if(lineShouldBeSkipped(line)) { continue; } - // Ignore lines that only contain whitespace - if (OnlyContainsWhitespace(line)) - { - continue; - } + Collection parsedLine = ParseCsvLine(line); - // Ignore comment lines - if (line[0] == '#') - { - continue; + // create options + ScopedItemOptions options = createItemOptions(parsedLine, null, lineNumber); + + if(isValidParsedLine(parsedLine, options, lineNumber)) { + result.Add(constructAlias(parsedLine, options)); } + //// + + // if (parsedLine.Count != 4) + // { + // string message = StringUtil.Format(AliasCommandStrings.ImportAliasFileInvalidFormat, filePath, lineNumber); + + // FormatException formatException = + // new FormatException(message); + + // ErrorRecord errorRecord = + // new ErrorRecord( + // formatException, + // "ImportAliasFileFormatError", + // ErrorCategory.ReadError, + // filePath); + + // errorRecord.ErrorDetails = new ErrorDetails(message); + + // ThrowTerminatingError(errorRecord); + // } + + // ScopedItemOptions options = ScopedItemOptions.None; + + // try + // { + // options = (ScopedItemOptions)Enum.Parse(typeof(ScopedItemOptions), values[3], true); + // } + // catch (ArgumentException argException) + // { + // string message = StringUtil.Format(AliasCommandStrings.ImportAliasOptionsError, filePath, lineNumber); + + // ErrorRecord errorRecord = + // new ErrorRecord( + // argException, + // "ImportAliasOptionsError", + // ErrorCategory.ReadError, + // filePath); + + // errorRecord.ErrorDetails = new ErrorDetails(message); + // WriteError(errorRecord); + // continue; + // } + + // AliasInfo newAlias = + // new AliasInfo( + // values[0], + // values[1], + // Context, + // options); + + // if (!string.IsNullOrEmpty(values[2])) + // { + // newAlias.Description = values[2]; + // } + + // result.Add(newAlias); + // } + } + reader.Dispose(); + } + return result; + } - Collection values = csvHelper.ParseCsv(line); + private bool lineShouldBeSkipped(string line) + { + //if line is empty or a comment, return true + return line.Length == 0 || OnlyContainsWhitespace(line) || line[0] == '#'; + } - if (values.Count != 4) - { - string message = StringUtil.Format(AliasCommandStrings.ImportAliasFileInvalidFormat, filePath, lineNumber); + private ScopedItemOptions createItemOptions(Collection parsedLine, string filePath, Int64 lineNumber) { + ScopedItemOptions options = ScopedItemOptions.None; - FormatException formatException = - new FormatException(message); + try + { + options = (ScopedItemOptions)Enum.Parse(typeof(ScopedItemOptions), parsedLine[3], true); + } + catch (ArgumentException argException) + { + string message = StringUtil.Format(AliasCommandStrings.ImportAliasOptionsError, filePath, lineNumber); - ErrorRecord errorRecord = - new ErrorRecord( - formatException, - "ImportAliasFileFormatError", - ErrorCategory.ReadError, - filePath); + ErrorRecord errorRecord = + new ErrorRecord( + argException, + "ImportAliasOptionsError", + ErrorCategory.ReadError, + filePath); - errorRecord.ErrorDetails = new ErrorDetails(message); + errorRecord.ErrorDetails = new ErrorDetails(message); + WriteError(errorRecord); + } + return options; + } - ThrowTerminatingError(errorRecord); - } + private AliasInfo constructAlias(Collection parsedLine, ScopedItemOptions options) { + AliasInfo newAlias = + new AliasInfo( + parsedLine[0], + parsedLine[1], + Context, + options); - ScopedItemOptions options = ScopedItemOptions.None; + if (!string.IsNullOrEmpty(parsedLine[2])) + { + newAlias.Description = parsedLine[2]; + } - try - { - options = (ScopedItemOptions)Enum.Parse(typeof(ScopedItemOptions), values[3], true); - } - catch (ArgumentException argException) - { - string message = StringUtil.Format(AliasCommandStrings.ImportAliasOptionsError, filePath, lineNumber); + return newAlias; + } - ErrorRecord errorRecord = - new ErrorRecord( - argException, - "ImportAliasOptionsError", - ErrorCategory.ReadError, - filePath); + private bool isValidParsedLine(Collection parsedLine, ScopedItemOptions options, Int64 lineNumber) { + // if options object cannot be created + if(options == ScopedItemOptions.None) { + return false; + } - errorRecord.ErrorDetails = new ErrorDetails(message); - WriteError(errorRecord); - continue; - } + if(parsedLine.Count != 4) + { + // if not four values, do ThrowTerminatingError(errorRecord) with ImportAliasFileFormatError, just like old implementation + string message = StringUtil.Format(AliasCommandStrings.ImportAliasFileInvalidFormat, lineNumber); - AliasInfo newAlias = - new AliasInfo( - values[0], - values[1], - Context, - options); + FormatException formatException = + new FormatException(message); + + ErrorRecord errorRecord = + new ErrorRecord( + formatException, + "ImportAliasFileFormatError", + ErrorCategory.ReadError, + null); + + errorRecord.ErrorDetails = new ErrorDetails(message); + + ThrowTerminatingError(errorRecord); + return false; + } + + return true; + } + + private Collection ParseCsvLine(string csv) + { + csv = csv.Trim(); + Collection result = new Collection(); + if (csv.Length == 0 || csv[0] == '#') + { + return result; + } + + var reader = new StringReader(csv); + StringBuilder wordBuffer = new StringBuilder(); + + while (reader.Peek() != -1) + { + char nextChar = (char)reader.Read(); - if (!string.IsNullOrEmpty(values[2])) + // if next character was delimiter or we are at the end, add string to result and clear wordBuffer + // else if next character was quote, perform reading until next quote and add it to wordBuffer + // else read and add it to wordBuffer + if (nextChar == ',') + { + result.Add(wordBuffer.ToString()); + wordBuffer.Clear(); + } + else if (nextChar == '"') + { + bool inQuotes = true; + + // if we are within a quote section, read and append to wordBuffer until we find a next quote that is not followed by another quote + // if it is a single quote, escape the quote section + // if the quote is followed by an other quote, do not escape and add a quote character to wordBuffer + while (reader.Peek() != -1 && inQuotes) { - newAlias.Description = values[2]; + nextChar = (char)reader.Read(); + + if (nextChar == '"') + { + if ((char)reader.Peek() == '"') + { + wordBuffer.Append(nextChar); + reader.Read(); + } + else + { + inQuotes = false; + } + } + else + { + wordBuffer.Append(nextChar); + } } - - result.Add(newAlias); + } + else + { + wordBuffer.Append(nextChar); } + } - reader.Dispose(); + string lastWord = wordBuffer.ToString(); + if (lastWord != string.Empty) + { + result.Add(lastWord); } - return result; + reader.Close(); + return result; } - private StreamReader OpenFile(out string filePath, bool isLiteralPath) + private StreamReader OpenFile(bool isLiteralPath) { StreamReader result = null; - filePath = null; + //filePath = null; ProviderInfo provider = null; Collection paths = null; @@ -422,7 +563,7 @@ private StreamReader OpenFile(out string filePath, bool isLiteralPath) this.Path); } - filePath = paths[0]; + string filePath = paths[0]; try { @@ -479,4 +620,6 @@ private static bool OnlyContainsWhitespace(string line) } #endregion Command code } + + } From 67199244209b68284fb9ee5d0622deedf9a399d8 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 2 Apr 2019 15:58:20 +0200 Subject: [PATCH 39/90] removed the unnecessary extra check --- .../commands/utility/ImportAliasCommand.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index fca5ac284d7..e5fb3b0c63b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -459,10 +459,6 @@ private Collection ParseCsvLine(string csv) { csv = csv.Trim(); Collection result = new Collection(); - if (csv.Length == 0 || csv[0] == '#') - { - return result; - } var reader = new StringReader(csv); StringBuilder wordBuffer = new StringBuilder(); From 0b62c835b5e151339018fbdf3bfd5da830fa564b Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 2 Apr 2019 16:33:37 +0200 Subject: [PATCH 40/90] filepath now according to old implementation --- .../commands/utility/ImportAliasCommand.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index e5fb3b0c63b..dd167ca2b10 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -292,14 +292,14 @@ private Collection GetAliasesFromFile(bool isLiteralPath) Collection result = new Collection(); // it seems like filePath is always null and never defined, so we can remove it as far as I could see - // string filePath = null; + string filePath = null; - using (StreamReader reader = OpenFile(isLiteralPath)) + using (StreamReader reader = OpenFile(out filePath, isLiteralPath)) { //CSVHelper csvHelper = new CSVHelper(','); Int64 lineNumber = 0; - string line;// = null; + string line = null; while ((line = reader.ReadLine()) != null) { ++lineNumber; @@ -311,7 +311,7 @@ private Collection GetAliasesFromFile(bool isLiteralPath) Collection parsedLine = ParseCsvLine(line); // create options - ScopedItemOptions options = createItemOptions(parsedLine, null, lineNumber); + ScopedItemOptions options = createItemOptions(parsedLine, filePath, lineNumber); if(isValidParsedLine(parsedLine, options, lineNumber)) { result.Add(constructAlias(parsedLine, options)); @@ -425,7 +425,7 @@ private AliasInfo constructAlias(Collection parsedLine, ScopedItemOption return newAlias; } - private bool isValidParsedLine(Collection parsedLine, ScopedItemOptions options, Int64 lineNumber) { + private bool isValidParsedLine(Collection parsedLine, ScopedItemOptions options, Int64 lineNumber, string filePath) { // if options object cannot be created if(options == ScopedItemOptions.None) { return false; @@ -444,7 +444,7 @@ private bool isValidParsedLine(Collection parsedLine, ScopedItemOptions formatException, "ImportAliasFileFormatError", ErrorCategory.ReadError, - null); + filePath); errorRecord.ErrorDetails = new ErrorDetails(message); @@ -520,11 +520,12 @@ private Collection ParseCsvLine(string csv) return result; } - private StreamReader OpenFile(bool isLiteralPath) + private StreamReader OpenFile(out string filePath, bool isLiteralPath) { StreamReader result = null; - //filePath = null; + // so remove this line right + filePath = null; ProviderInfo provider = null; Collection paths = null; @@ -559,7 +560,7 @@ private StreamReader OpenFile(bool isLiteralPath) this.Path); } - string filePath = paths[0]; + filePath = paths[0]; try { From e4e8e482be17614deb9ec8680e9f7193c4ed2f36 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 2 Apr 2019 17:15:06 +0200 Subject: [PATCH 41/90] fixed build issue --- .../commands/utility/ImportAliasCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index dd167ca2b10..b2eb5a1da7f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -313,7 +313,7 @@ private Collection GetAliasesFromFile(bool isLiteralPath) // create options ScopedItemOptions options = createItemOptions(parsedLine, filePath, lineNumber); - if(isValidParsedLine(parsedLine, options, lineNumber)) { + if(isValidParsedLine(parsedLine, options, lineNumber, filePath)) { result.Add(constructAlias(parsedLine, options)); } //// From b2e9e6e04e88bb260107c378f93251513f1bc354 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 2 Apr 2019 18:18:16 +0200 Subject: [PATCH 42/90] remove commented code --- .../commands/utility/ImportAliasCommand.cs | 60 +------------------ 1 file changed, 2 insertions(+), 58 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index b2eb5a1da7f..6e86ef16e11 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -292,6 +292,7 @@ private Collection GetAliasesFromFile(bool isLiteralPath) Collection result = new Collection(); // it seems like filePath is always null and never defined, so we can remove it as far as I could see + // bug keeping it for now because tests are failing string filePath = null; using (StreamReader reader = OpenFile(out filePath, isLiteralPath)) @@ -316,69 +317,13 @@ private Collection GetAliasesFromFile(bool isLiteralPath) if(isValidParsedLine(parsedLine, options, lineNumber, filePath)) { result.Add(constructAlias(parsedLine, options)); } - //// - - // if (parsedLine.Count != 4) - // { - // string message = StringUtil.Format(AliasCommandStrings.ImportAliasFileInvalidFormat, filePath, lineNumber); - - // FormatException formatException = - // new FormatException(message); - - // ErrorRecord errorRecord = - // new ErrorRecord( - // formatException, - // "ImportAliasFileFormatError", - // ErrorCategory.ReadError, - // filePath); - - // errorRecord.ErrorDetails = new ErrorDetails(message); - - // ThrowTerminatingError(errorRecord); - // } - - // ScopedItemOptions options = ScopedItemOptions.None; - - // try - // { - // options = (ScopedItemOptions)Enum.Parse(typeof(ScopedItemOptions), values[3], true); - // } - // catch (ArgumentException argException) - // { - // string message = StringUtil.Format(AliasCommandStrings.ImportAliasOptionsError, filePath, lineNumber); - - // ErrorRecord errorRecord = - // new ErrorRecord( - // argException, - // "ImportAliasOptionsError", - // ErrorCategory.ReadError, - // filePath); - - // errorRecord.ErrorDetails = new ErrorDetails(message); - // WriteError(errorRecord); - // continue; - // } - - // AliasInfo newAlias = - // new AliasInfo( - // values[0], - // values[1], - // Context, - // options); - - // if (!string.IsNullOrEmpty(values[2])) - // { - // newAlias.Description = values[2]; - // } - - // result.Add(newAlias); - // } } reader.Dispose(); } return result; } + private bool lineShouldBeSkipped(string line) { //if line is empty or a comment, return true @@ -387,7 +332,6 @@ private bool lineShouldBeSkipped(string line) private ScopedItemOptions createItemOptions(Collection parsedLine, string filePath, Int64 lineNumber) { ScopedItemOptions options = ScopedItemOptions.None; - try { options = (ScopedItemOptions)Enum.Parse(typeof(ScopedItemOptions), parsedLine[3], true); From 1988aad5c2aafef5dfedf6cc0ee5525ef16d3c09 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 3 Apr 2019 11:48:36 +0200 Subject: [PATCH 43/90] Update test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 Co-Authored-By: SytzeAndr --- .../Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 19b00238b55..e31587d54e3 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -2,7 +2,7 @@ # Licensed under the MIT License. Describe "Import-Alias DRT Unit Tests" -Tags "CI" { $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory - $aliasFilename = "aliasFilename" + $aliasFilename = "aliasFilename" $fulltestpath = Join-Path -Path $testAliasDirectory -ChildPath $aliasFilename BeforeEach { From dbf0663e22589f3275f4a0ffd47d26281b856bc6 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 3 Apr 2019 11:48:46 +0200 Subject: [PATCH 44/90] Update test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 Co-Authored-By: SytzeAndr --- .../Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index e31587d54e3..7784606f27f 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -46,7 +46,7 @@ Describe "Import-Alias DRT Unit Tests" -Tags "CI" { Describe "Import-Alias" -Tags "CI" { BeforeAll { - $newLine=[Environment]::NewLine + $newLine = [Environment]::NewLine $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory $aliasFilename = "pesteralias.txt" From e33ad1587881d9089f1135b34e88fbe25e048967 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 3 Apr 2019 11:48:56 +0200 Subject: [PATCH 45/90] Update test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 Co-Authored-By: SytzeAndr --- .../Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 7784606f27f..5836b69166f 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -67,7 +67,7 @@ Describe "Import-Alias" -Tags "CI" { New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null # set header - $aliasFileContent ='# Alias File'+$newLine + $aliasFileContent = '# Alias File' + $newLine $aliasFileContent+='# Exported by : alex'+$newLine $aliasFileContent+='# Date/Time : Thursday, 12 November 2015 21:55:08'+$newLine $aliasFileContent+='# Computer : archvm' From 4369b0baa907709c50cef232237bcca08f2c29a6 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 3 Apr 2019 11:49:09 +0200 Subject: [PATCH 46/90] Update test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 Co-Authored-By: SytzeAndr --- .../Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 5836b69166f..4cefdc10a5e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -68,7 +68,7 @@ Describe "Import-Alias" -Tags "CI" { # set header $aliasFileContent = '# Alias File' + $newLine - $aliasFileContent+='# Exported by : alex'+$newLine + $aliasFileContent+= '# Exported by : alex' + $newLine $aliasFileContent+='# Date/Time : Thursday, 12 November 2015 21:55:08'+$newLine $aliasFileContent+='# Computer : archvm' From 122caf30482433c06198938fe60984ddd38d8bae Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 3 Apr 2019 11:49:23 +0200 Subject: [PATCH 47/90] Update test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 Co-Authored-By: SytzeAndr --- .../Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 4cefdc10a5e..06ce9348466 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -69,7 +69,7 @@ Describe "Import-Alias" -Tags "CI" { # set header $aliasFileContent = '# Alias File' + $newLine $aliasFileContent+= '# Exported by : alex' + $newLine - $aliasFileContent+='# Date/Time : Thursday, 12 November 2015 21:55:08'+$newLine + $aliasFileContent+= '# Date/Time : Thursday, 12 November 2015 21:55:08' + $newLine $aliasFileContent+='# Computer : archvm' # add various aliases From 7d58d9a65c3bb2b7e341b3092e7bcff8bf52a552 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 3 Apr 2019 11:49:30 +0200 Subject: [PATCH 48/90] Update test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 Co-Authored-By: SytzeAndr --- .../Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 06ce9348466..3fa8846b0f2 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -81,7 +81,7 @@ Describe "Import-Alias" -Tags "CI" { # create invalid file with more than four values New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null - $aliasFileContent = $newLine+'"v_1","v_2","v_3","v_4","v_5"' + $aliasFileContent = $newLine + '"v_1","v_2","v_3","v_4","v_5"' $aliasFileContent > $aliasPathMoreThanFourValues # create invalid file with less than four values From 4c52d24fb061bb78bde40b9de487329cd3d6fb26 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 3 Apr 2019 11:49:40 +0200 Subject: [PATCH 49/90] Update test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 Co-Authored-By: SytzeAndr --- .../Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 3fa8846b0f2..91adc26ad2c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -70,7 +70,7 @@ Describe "Import-Alias" -Tags "CI" { $aliasFileContent = '# Alias File' + $newLine $aliasFileContent+= '# Exported by : alex' + $newLine $aliasFileContent+= '# Date/Time : Thursday, 12 November 2015 21:55:08' + $newLine - $aliasFileContent+='# Computer : archvm' + $aliasFileContent+= '# Computer : archvm' # add various aliases $aliasFileContent+= $newLine+$alias1+',"'+$commandToAlias+'","","None"' From e907631c3bfb326107940c1e7b36d13e6bd7c6c6 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 3 Apr 2019 11:49:48 +0200 Subject: [PATCH 50/90] Update test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 Co-Authored-By: SytzeAndr --- .../Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 91adc26ad2c..5869d135597 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -86,7 +86,7 @@ Describe "Import-Alias" -Tags "CI" { # create invalid file with less than four values New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null - $aliasFileContent = $newLine+'"v_1","v_2","v_3"' + $aliasFileContent = $newLine + '"v_1","v_2","v_3"' $aliasFileContent > $aliasPathLessThanFourValues } From 21da70c727ab0531ed9aa32717f7c9ec72f3f4ef Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 3 Apr 2019 11:49:59 +0200 Subject: [PATCH 51/90] Update test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 Co-Authored-By: SytzeAndr --- .../Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 5869d135597..8578c668e67 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -73,7 +73,7 @@ Describe "Import-Alias" -Tags "CI" { $aliasFileContent+= '# Computer : archvm' # add various aliases - $aliasFileContent+= $newLine+$alias1+',"'+$commandToAlias+'","","None"' + $aliasFileContent+= $newLine + $alias1 + ',"' + $commandToAlias + '","","None"' $aliasFileContent+= $newLine+$alias2+',"'+$commandToAlias+'","","None"' $aliasFileContent+= $newLine+$alias3+',"'+$commandToAlias+'","","None"' $aliasFileContent+= $newLine+$alias4+',"'+$commandToAlias+'","","None"' From dbfc9478a01cbdf0fa8132c9202f961d3a4c871a Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 3 Apr 2019 11:50:11 +0200 Subject: [PATCH 52/90] Update test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 Co-Authored-By: SytzeAndr --- .../Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 8578c668e67..ebf4a42bc7e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -74,7 +74,7 @@ Describe "Import-Alias" -Tags "CI" { # add various aliases $aliasFileContent+= $newLine + $alias1 + ',"' + $commandToAlias + '","","None"' - $aliasFileContent+= $newLine+$alias2+',"'+$commandToAlias+'","","None"' + $aliasFileContent += $newLine + $alias2 + ',"' + $commandToAlias + '","","None"' $aliasFileContent+= $newLine+$alias3+',"'+$commandToAlias+'","","None"' $aliasFileContent+= $newLine+$alias4+',"'+$commandToAlias+'","","None"' $aliasFileContent > $aliasfile From 7f764186cbee0c9d92cf8ed259499d3cd983cf40 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 3 Apr 2019 11:50:29 +0200 Subject: [PATCH 53/90] Update test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 Co-Authored-By: SytzeAndr --- .../Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index ebf4a42bc7e..b3b5a7caf33 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -95,7 +95,7 @@ Describe "Import-Alias" -Tags "CI" { } It "Should be able to import an alias file successfully" { - {Import-Alias -Path $aliasfile} | Should -Not -Throw + { Import-Alias -Path $aliasfile } | Should -Not -Throw } It "Should classify an alias as non existent when it is not imported yet" { From 7057b97cabd0435cd722a16766ab5b1692865ab3 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 3 Apr 2019 11:51:17 +0200 Subject: [PATCH 54/90] Update test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 Co-Authored-By: SytzeAndr --- .../Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index b3b5a7caf33..2e6baa9389a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -76,7 +76,7 @@ Describe "Import-Alias" -Tags "CI" { $aliasFileContent+= $newLine + $alias1 + ',"' + $commandToAlias + '","","None"' $aliasFileContent += $newLine + $alias2 + ',"' + $commandToAlias + '","","None"' $aliasFileContent+= $newLine+$alias3+',"'+$commandToAlias+'","","None"' - $aliasFileContent+= $newLine+$alias4+',"'+$commandToAlias+'","","None"' + $aliasFileContent += $newLine + $alias4 + ',"' + $commandToAlias + '","","None"' $aliasFileContent > $aliasfile # create invalid file with more than four values From 5d40567b0b44e2376e7f234b1793678cfc513cba Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 3 Apr 2019 11:51:28 +0200 Subject: [PATCH 55/90] Update test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 Co-Authored-By: SytzeAndr --- .../Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 2e6baa9389a..a031b9ca509 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -75,7 +75,7 @@ Describe "Import-Alias" -Tags "CI" { # add various aliases $aliasFileContent+= $newLine + $alias1 + ',"' + $commandToAlias + '","","None"' $aliasFileContent += $newLine + $alias2 + ',"' + $commandToAlias + '","","None"' - $aliasFileContent+= $newLine+$alias3+',"'+$commandToAlias+'","","None"' + $aliasFileContent += $newLine + $alias3 + ',"' + $commandToAlias + '","","None"' $aliasFileContent += $newLine + $alias4 + ',"' + $commandToAlias + '","","None"' $aliasFileContent > $aliasfile From 38a2acb0513df5ead230146d9cb5f9278a929ff1 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 3 Apr 2019 12:25:45 +0200 Subject: [PATCH 56/90] removed the afterAll, tests are now with parameters --- .../Import-Alias.Tests.ps1 | 37 +++++-------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 19b00238b55..531559c1ad5 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -90,39 +90,22 @@ Describe "Import-Alias" -Tags "CI" { $aliasFileContent > $aliasPathLessThanFourValues } - AfterAll { - Remove-Item -Path $testAliasDirectory -Recurse -Force - } - It "Should be able to import an alias file successfully" { - {Import-Alias -Path $aliasfile} | Should -Not -Throw - } - - It "Should classify an alias as non existent when it is not imported yet" { - Get-Alias -Name invalid_alias -ErrorAction SilentlyContinue | Should -BeExactly $null + { Import-Alias -Path $aliasfile } | Should -Not -Throw } - It "Should be able to import an alias file and recognize an imported alias" { - Import-Alias -Path $aliasfile - (Get-Alias -Name $alias1 -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias - } - - It "Should be able to parse ""abc""""def"" into abc""def " { - $aliasToTest = 'abc"def' - Import-Alias -Path $aliasfile - (Get-Alias -Name $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias - } - - It "Should be able to parse ""aaa"" into aaa " { - $aliasToTest = "aaa" - Import-Alias -Path $aliasfile - (Get-Alias -Name $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias + It "Should throw an exception when the alias is non existent" { + ( Get-Alias -Name invalid_alias -ErrorAction SilentlyContinue ).Definition | Should -BeExactly $null } - It "Should be able to parse ""a,b"" into a,b " { - $aliasToTest = "a,b" + It "Should be able to parse " -TestCases @( + @{ aliasToTest = 'abc"def' } + @{ aliasToTest = 'aaa' } + @{ aliasToTest = 'a,b' } + ) { + param($aliasToTest) Import-Alias -Path $aliasfile - (Get-Alias -Name $aliasToTest -ErrorAction SilentlyContinue).Definition | Should -BeExactly $commandToAlias + ( Get-Alias -Name $aliasToTest ).Definition | Should -BeExactly $commandToAlias } It "Should throw an error when reading more than four values" { From 8db2decf56f0e10f514aa01a3c9544ecd072db79 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 3 Apr 2019 12:33:33 +0200 Subject: [PATCH 57/90] iSazonov suggestions, regarding spacing --- .../Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 33363491836..9635e989546 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -68,12 +68,12 @@ Describe "Import-Alias" -Tags "CI" { # set header $aliasFileContent = '# Alias File' + $newLine - $aliasFileContent+= '# Exported by : alex' + $newLine - $aliasFileContent+= '# Date/Time : Thursday, 12 November 2015 21:55:08' + $newLine - $aliasFileContent+= '# Computer : archvm' + $aliasFileContent += '# Exported by : alex' + $newLine + $aliasFileContent += '# Date/Time : Thursday, 12 November 2015 21:55:08' + $newLine + $aliasFileContent += '# Computer : archvm' # add various aliases - $aliasFileContent+= $newLine + $alias1 + ',"' + $commandToAlias + '","","None"' + $aliasFileContent += $newLine + $alias1 + ',"' + $commandToAlias + '","","None"' $aliasFileContent += $newLine + $alias2 + ',"' + $commandToAlias + '","","None"' $aliasFileContent += $newLine + $alias3 + ',"' + $commandToAlias + '","","None"' $aliasFileContent += $newLine + $alias4 + ',"' + $commandToAlias + '","","None"' From a98d2b1f819af1432ec62cdf68c3828a791f5295 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 3 Apr 2019 16:06:38 +0200 Subject: [PATCH 58/90] replaced all tabs with four spaces --- .../Import-Alias.Tests.ps1 | 192 +++++++++--------- 1 file changed, 96 insertions(+), 96 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 9635e989546..cb2bae6565e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -6,113 +6,113 @@ Describe "Import-Alias DRT Unit Tests" -Tags "CI" { $fulltestpath = Join-Path -Path $testAliasDirectory -ChildPath $aliasFilename BeforeEach { - New-Item -Path $testAliasDirectory -ItemType Directory -Force - remove-item alias:abcd* -force -ErrorAction SilentlyContinue - remove-item alias:ijkl* -force -ErrorAction SilentlyContinue - set-alias abcd01 efgh01 - set-alias abcd02 efgh02 - set-alias abcd03 efgh03 - set-alias abcd04 efgh04 - set-alias ijkl01 mnop01 - set-alias ijkl02 mnop02 - set-alias ijkl03 mnop03 - set-alias ijkl04 mnop04 + New-Item -Path $testAliasDirectory -ItemType Directory -Force + remove-item alias:abcd* -force -ErrorAction SilentlyContinue + remove-item alias:ijkl* -force -ErrorAction SilentlyContinue + set-alias abcd01 efgh01 + set-alias abcd02 efgh02 + set-alias abcd03 efgh03 + set-alias abcd04 efgh04 + set-alias ijkl01 mnop01 + set-alias ijkl02 mnop02 + set-alias ijkl03 mnop03 + set-alias ijkl04 mnop04 } - AfterEach { - Remove-Item -Path $testAliasDirectory -Recurse -Force -ErrorAction SilentlyContinue - } + AfterEach { + Remove-Item -Path $testAliasDirectory -Recurse -Force -ErrorAction SilentlyContinue + } - It "Import-Alias Resolve To Multiple will throw PSInvalidOperationException" { - { Import-Alias * -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.ImportAliasCommand" - } + It "Import-Alias Resolve To Multiple will throw PSInvalidOperationException" { + { Import-Alias * -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.ImportAliasCommand" + } - It "Import-Alias From Exported Alias File Aliases Already Exist should throw SessionStateException" { - { Export-Alias $fulltestpath abcd* } | Should -Not -Throw - { Import-Alias $fulltestpath -ErrorAction Stop } | Should -Throw -ErrorId "AliasAlreadyExists,Microsoft.PowerShell.Commands.ImportAliasCommand" + It "Import-Alias From Exported Alias File Aliases Already Exist should throw SessionStateException" { + { Export-Alias $fulltestpath abcd* } | Should -Not -Throw + { Import-Alias $fulltestpath -ErrorAction Stop } | Should -Throw -ErrorId "AliasAlreadyExists,Microsoft.PowerShell.Commands.ImportAliasCommand" } - It "Import-Alias Into Invalid Scope should throw PSArgumentException"{ - { Export-Alias $fulltestpath abcd* } | Should -Not -Throw - { Import-Alias $fulltestpath -scope bogus } | Should -Throw -ErrorId "Argument,Microsoft.PowerShell.Commands.ImportAliasCommand" + It "Import-Alias Into Invalid Scope should throw PSArgumentException"{ + { Export-Alias $fulltestpath abcd* } | Should -Not -Throw + { Import-Alias $fulltestpath -scope bogus } | Should -Throw -ErrorId "Argument,Microsoft.PowerShell.Commands.ImportAliasCommand" } - It "Import-Alias From Exported Alias File Aliases Already Exist using force should not throw"{ - {Export-Alias $fulltestpath abcd*} | Should -Not -Throw - {Import-Alias $fulltestpath -Force} | Should -Not -Throw + It "Import-Alias From Exported Alias File Aliases Already Exist using force should not throw"{ + {Export-Alias $fulltestpath abcd*} | Should -Not -Throw + {Import-Alias $fulltestpath -Force} | Should -Not -Throw } } Describe "Import-Alias" -Tags "CI" { - BeforeAll { - $newLine = [Environment]::NewLine - - $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory - $aliasFilename = "pesteralias.txt" - $aliasFilenameMoreThanFourValues = "aliasFileMoreThanFourValues.txt" - $aliasFilenameLessThanFourValues = "aliasFileLessThanFourValues.txt" - - $aliasfile = Join-Path -Path $testAliasDirectory -ChildPath $aliasFilename - $aliasPathMoreThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameMoreThanFourValues - $aliasPathLessThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameLessThanFourValues - - $commandToAlias = "echo" - $alias1 = "pesterecho" - $alias2 = '"abc""def"' - $alias3 = '"aaa"' - $alias4 = '"a,b"' - - # create alias file - New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null - - # set header - $aliasFileContent = '# Alias File' + $newLine - $aliasFileContent += '# Exported by : alex' + $newLine - $aliasFileContent += '# Date/Time : Thursday, 12 November 2015 21:55:08' + $newLine - $aliasFileContent += '# Computer : archvm' - - # add various aliases - $aliasFileContent += $newLine + $alias1 + ',"' + $commandToAlias + '","","None"' - $aliasFileContent += $newLine + $alias2 + ',"' + $commandToAlias + '","","None"' - $aliasFileContent += $newLine + $alias3 + ',"' + $commandToAlias + '","","None"' - $aliasFileContent += $newLine + $alias4 + ',"' + $commandToAlias + '","","None"' - $aliasFileContent > $aliasfile - - # create invalid file with more than four values - New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null - $aliasFileContent = $newLine + '"v_1","v_2","v_3","v_4","v_5"' - $aliasFileContent > $aliasPathMoreThanFourValues - - # create invalid file with less than four values - New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null - $aliasFileContent = $newLine + '"v_1","v_2","v_3"' - $aliasFileContent > $aliasPathLessThanFourValues - } - - It "Should be able to import an alias file successfully" { - { Import-Alias -Path $aliasfile } | Should -Not -Throw - } - - It "Should classify an alias as non existent when it is not imported yet" { - Get-Alias -Name invalid_alias -ErrorAction SilentlyContinue | Should -BeExactly $null - } - - It "Should be able to parse " -TestCases @( - @{ aliasToTest = 'abc"def' } - @{ aliasToTest = 'aaa' } - @{ aliasToTest = 'a,b' } - ) { - param($aliasToTest) - Import-Alias -Path $aliasfile - ( Get-Alias -Name $aliasToTest ).Definition | Should -BeExactly $commandToAlias - } - - It "Should throw an error when reading more than four values" { - { Import-Alias -Path $aliasPathMoreThanFourValues } | Should -Throw -ErrorId "ImportAliasFileFormatError,Microsoft.PowerShell.Commands.ImportAliasCommand" - } - - It "Should throw an error when reading less than four values" { - { Import-Alias -Path $aliasPathLessThanFourValues } | Should -Throw -ErrorId "ImportAliasFileFormatError,Microsoft.PowerShell.Commands.ImportAliasCommand" - } + BeforeAll { + $newLine = [Environment]::NewLine + + $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory + $aliasFilename = "pesteralias.txt" + $aliasFilenameMoreThanFourValues = "aliasFileMoreThanFourValues.txt" + $aliasFilenameLessThanFourValues = "aliasFileLessThanFourValues.txt" + + $aliasfile = Join-Path -Path $testAliasDirectory -ChildPath $aliasFilename + $aliasPathMoreThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameMoreThanFourValues + $aliasPathLessThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameLessThanFourValues + + $commandToAlias = "echo" + $alias1 = "pesterecho" + $alias2 = '"abc""def"' + $alias3 = '"aaa"' + $alias4 = '"a,b"' + + # create alias file + New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null + + # set header + $aliasFileContent = '# Alias File' + $newLine + $aliasFileContent += '# Exported by : alex' + $newLine + $aliasFileContent += '# Date/Time : Thursday, 12 November 2015 21:55:08' + $newLine + $aliasFileContent += '# Computer : archvm' + + # add various aliases + $aliasFileContent += $newLine + $alias1 + ',"' + $commandToAlias + '","","None"' + $aliasFileContent += $newLine + $alias2 + ',"' + $commandToAlias + '","","None"' + $aliasFileContent += $newLine + $alias3 + ',"' + $commandToAlias + '","","None"' + $aliasFileContent += $newLine + $alias4 + ',"' + $commandToAlias + '","","None"' + $aliasFileContent > $aliasfile + + # create invalid file with more than four values + New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null + $aliasFileContent = $newLine + '"v_1","v_2","v_3","v_4","v_5"' + $aliasFileContent > $aliasPathMoreThanFourValues + + # create invalid file with less than four values + New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null + $aliasFileContent = $newLine + '"v_1","v_2","v_3"' + $aliasFileContent > $aliasPathLessThanFourValues + } + + It "Should be able to import an alias file successfully" { + { Import-Alias -Path $aliasfile } | Should -Not -Throw + } + + It "Should classify an alias as non existent when it is not imported yet" { + Get-Alias -Name invalid_alias -ErrorAction SilentlyContinue | Should -BeExactly $null + } + + It "Should be able to parse " -TestCases @( + @{ aliasToTest = 'abc"def' } + @{ aliasToTest = 'aaa' } + @{ aliasToTest = 'a,b' } + ) { + param($aliasToTest) + Import-Alias -Path $aliasfile + ( Get-Alias -Name $aliasToTest ).Definition | Should -BeExactly $commandToAlias + } + + It "Should throw an error when reading more than four values" { + { Import-Alias -Path $aliasPathMoreThanFourValues } | Should -Throw -ErrorId "ImportAliasFileFormatError,Microsoft.PowerShell.Commands.ImportAliasCommand" + } + + It "Should throw an error when reading less than four values" { + { Import-Alias -Path $aliasPathLessThanFourValues } | Should -Throw -ErrorId "ImportAliasFileFormatError,Microsoft.PowerShell.Commands.ImportAliasCommand" + } } From d6ea184cf32ed5f5182175c014802b9cbae0269c Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Wed, 10 Apr 2019 18:42:59 +0200 Subject: [PATCH 59/90] Update test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 Co-Authored-By: SytzeAndr --- .../Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index cb2bae6565e..63e311a5581 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -95,7 +95,7 @@ Describe "Import-Alias" -Tags "CI" { } It "Should classify an alias as non existent when it is not imported yet" { - Get-Alias -Name invalid_alias -ErrorAction SilentlyContinue | Should -BeExactly $null + {Get-Alias -Name invalid_alias -ErrorAction Stop} | Should -Throw -ErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand' } } It "Should be able to parse " -TestCases @( From fe82633cc9378b3d880655f985e7b88c9220c84f Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 10 Apr 2019 18:50:32 +0200 Subject: [PATCH 60/90] removed the extra bracket introduced --- .../Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 63e311a5581..e13442278ac 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -95,7 +95,7 @@ Describe "Import-Alias" -Tags "CI" { } It "Should classify an alias as non existent when it is not imported yet" { - {Get-Alias -Name invalid_alias -ErrorAction Stop} | Should -Throw -ErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand' } + {Get-Alias -Name invalid_alias -ErrorAction Stop} | Should -Throw -ErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand' } It "Should be able to parse " -TestCases @( From 7a19388befebf5769ee0cd917875e48a7c73e350 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 10 Apr 2019 19:58:05 +0200 Subject: [PATCH 61/90] The bug was due to checking if the options was None. Apparently options in general parses to None also if the options was created with succes. I changed the check to a boolean one and by returning a pair instead of only an options object. --- .../commands/utility/ImportAliasCommand.cs | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index 6e86ef16e11..b7b26c5419e 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -294,10 +294,8 @@ private Collection GetAliasesFromFile(bool isLiteralPath) // it seems like filePath is always null and never defined, so we can remove it as far as I could see // bug keeping it for now because tests are failing string filePath = null; - using (StreamReader reader = OpenFile(out filePath, isLiteralPath)) { - //CSVHelper csvHelper = new CSVHelper(','); Int64 lineNumber = 0; string line = null; @@ -312,9 +310,9 @@ private Collection GetAliasesFromFile(bool isLiteralPath) Collection parsedLine = ParseCsvLine(line); // create options - ScopedItemOptions options = createItemOptions(parsedLine, filePath, lineNumber); - - if(isValidParsedLine(parsedLine, options, lineNumber, filePath)) { + KeyValuePair optionPair = createItemOptions(parsedLine, filePath, lineNumber); + ScopedItemOptions options = optionPair.Key; + if(isValidParsedLine(parsedLine, optionPair.Value, lineNumber, filePath)) { result.Add(constructAlias(parsedLine, options)); } } @@ -330,14 +328,12 @@ private bool lineShouldBeSkipped(string line) return line.Length == 0 || OnlyContainsWhitespace(line) || line[0] == '#'; } - private ScopedItemOptions createItemOptions(Collection parsedLine, string filePath, Int64 lineNumber) { + private KeyValuePair createItemOptions(Collection parsedLine, string filePath, Int64 lineNumber) { ScopedItemOptions options = ScopedItemOptions.None; - try - { - options = (ScopedItemOptions)Enum.Parse(typeof(ScopedItemOptions), parsedLine[3], true); - } - catch (ArgumentException argException) - { + bool succesfullParse = false; + try { + succesfullParse = Enum.TryParse(parsedLine[3], out options); + } catch (ArgumentException argException) { string message = StringUtil.Format(AliasCommandStrings.ImportAliasOptionsError, filePath, lineNumber); ErrorRecord errorRecord = @@ -350,7 +346,7 @@ private ScopedItemOptions createItemOptions(Collection parsedLine, strin errorRecord.ErrorDetails = new ErrorDetails(message); WriteError(errorRecord); } - return options; + return new KeyValuePair(options, succesfullParse); } private AliasInfo constructAlias(Collection parsedLine, ScopedItemOptions options) { @@ -369,14 +365,15 @@ private AliasInfo constructAlias(Collection parsedLine, ScopedItemOption return newAlias; } - private bool isValidParsedLine(Collection parsedLine, ScopedItemOptions options, Int64 lineNumber, string filePath) { + private bool isValidParsedLine(Collection parsedLine, bool optionsParsedSuccesfully, Int64 lineNumber, string filePath) { // if options object cannot be created - if(options == ScopedItemOptions.None) { + if(!optionsParsedSuccesfully) { return false; } if(parsedLine.Count != 4) { + Console.WriteLine("count was not 4"); // if not four values, do ThrowTerminatingError(errorRecord) with ImportAliasFileFormatError, just like old implementation string message = StringUtil.Format(AliasCommandStrings.ImportAliasFileInvalidFormat, lineNumber); From d670ef1da189bc7a0e3daa71be2d1f84b13d578c Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 10 Apr 2019 20:06:56 +0200 Subject: [PATCH 62/90] codefactor suggestions --- .../commands/utility/ImportAliasCommand.cs | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index b7b26c5419e..c5b06f10761 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -297,13 +297,14 @@ private Collection GetAliasesFromFile(bool isLiteralPath) using (StreamReader reader = OpenFile(out filePath, isLiteralPath)) { - Int64 lineNumber = 0; + long lineNumber = 0; string line = null; while ((line = reader.ReadLine()) != null) { ++lineNumber; - if(lineShouldBeSkipped(line)) { + if (lineShouldBeSkipped(line)) + { continue; } @@ -312,7 +313,8 @@ private Collection GetAliasesFromFile(bool isLiteralPath) // create options KeyValuePair optionPair = createItemOptions(parsedLine, filePath, lineNumber); ScopedItemOptions options = optionPair.Key; - if(isValidParsedLine(parsedLine, optionPair.Value, lineNumber, filePath)) { + if (isValidParsedLine(parsedLine, optionPair.Value, lineNumber, filePath)) + { result.Add(constructAlias(parsedLine, options)); } } @@ -320,20 +322,22 @@ private Collection GetAliasesFromFile(bool isLiteralPath) } return result; } - - + private bool lineShouldBeSkipped(string line) { - //if line is empty or a comment, return true + // if line is empty or a comment, return true return line.Length == 0 || OnlyContainsWhitespace(line) || line[0] == '#'; } - private KeyValuePair createItemOptions(Collection parsedLine, string filePath, Int64 lineNumber) { + private KeyValuePair createItemOptions(Collection parsedLine, string filePath, long lineNumber) { ScopedItemOptions options = ScopedItemOptions.None; bool succesfullParse = false; - try { + try + { succesfullParse = Enum.TryParse(parsedLine[3], out options); - } catch (ArgumentException argException) { + } + catch (ArgumentException argException) + { string message = StringUtil.Format(AliasCommandStrings.ImportAliasOptionsError, filePath, lineNumber); ErrorRecord errorRecord = @@ -365,15 +369,15 @@ private AliasInfo constructAlias(Collection parsedLine, ScopedItemOption return newAlias; } - private bool isValidParsedLine(Collection parsedLine, bool optionsParsedSuccesfully, Int64 lineNumber, string filePath) { + private bool isValidParsedLine(Collection parsedLine, bool optionsParsedSuccesfully, long lineNumber, string filePath) + { // if options object cannot be created - if(!optionsParsedSuccesfully) { + if (!optionsParsedSuccesfully) { return false; } - if(parsedLine.Count != 4) + if (parsedLine.Count != 4) { - Console.WriteLine("count was not 4"); // if not four values, do ThrowTerminatingError(errorRecord) with ImportAliasFileFormatError, just like old implementation string message = StringUtil.Format(AliasCommandStrings.ImportAliasFileInvalidFormat, lineNumber); From ce3c15de6931c97fc045abf8f6f6d82d9657b19d Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 10 Apr 2019 20:10:51 +0200 Subject: [PATCH 63/90] more codefactor suggestions --- .../commands/utility/ImportAliasCommand.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index c5b06f10761..21c6c0ee6fa 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -296,7 +296,6 @@ private Collection GetAliasesFromFile(bool isLiteralPath) string filePath = null; using (StreamReader reader = OpenFile(out filePath, isLiteralPath)) { - long lineNumber = 0; string line = null; while ((line = reader.ReadLine()) != null) @@ -329,7 +328,8 @@ private bool lineShouldBeSkipped(string line) return line.Length == 0 || OnlyContainsWhitespace(line) || line[0] == '#'; } - private KeyValuePair createItemOptions(Collection parsedLine, string filePath, long lineNumber) { + private KeyValuePair createItemOptions(Collection parsedLine, string filePath, long lineNumber) + { ScopedItemOptions options = ScopedItemOptions.None; bool succesfullParse = false; try @@ -350,10 +350,12 @@ private KeyValuePair createItemOptions(Collection(options, succesfullParse); } - private AliasInfo constructAlias(Collection parsedLine, ScopedItemOptions options) { + private AliasInfo constructAlias(Collection parsedLine, ScopedItemOptions options) + { AliasInfo newAlias = new AliasInfo( parsedLine[0], @@ -372,7 +374,8 @@ private AliasInfo constructAlias(Collection parsedLine, ScopedItemOption private bool isValidParsedLine(Collection parsedLine, bool optionsParsedSuccesfully, long lineNumber, string filePath) { // if options object cannot be created - if (!optionsParsedSuccesfully) { + if (!optionsParsedSuccesfully) + { return false; } @@ -562,6 +565,4 @@ private static bool OnlyContainsWhitespace(string line) } #endregion Command code } - - } From aae84f7afd267dfd3edaf96cbabe99c0e95045f3 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 10 Apr 2019 20:14:38 +0200 Subject: [PATCH 64/90] codefactor fixes --- .../commands/utility/ImportAliasCommand.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index 21c6c0ee6fa..b34f94a127f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -373,8 +373,7 @@ private AliasInfo constructAlias(Collection parsedLine, ScopedItemOption private bool isValidParsedLine(Collection parsedLine, bool optionsParsedSuccesfully, long lineNumber, string filePath) { - // if options object cannot be created - if (!optionsParsedSuccesfully) + if (!optionsParsedSuccesfully) { return false; } From d8cf4d343f5bffd6d8f0b8a53db7d9562a06d04c Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 10 Apr 2019 20:17:08 +0200 Subject: [PATCH 65/90] codefactor fixes, add extra empty line --- .../commands/utility/ImportAliasCommand.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index b34f94a127f..bc0c91cb181 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -317,8 +317,10 @@ private Collection GetAliasesFromFile(bool isLiteralPath) result.Add(constructAlias(parsedLine, options)); } } + reader.Dispose(); } + return result; } From a1918bf4dda065aa185883ce7e080c1129bc2766 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 10 Apr 2019 20:21:03 +0200 Subject: [PATCH 66/90] removal of csv.cs since its functionality is transfered to ImportAliasCommand.cd --- .../commands/utility/Csv.cs | 100 ------------------ 1 file changed, 100 deletions(-) delete mode 100644 src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs deleted file mode 100644 index 622cef148e5..00000000000 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Csv.cs +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System.Collections.ObjectModel; -using System.IO; -using System.Text; - -namespace Microsoft.PowerShell.Commands -{ - /// - /// This class is used to parse CSV text. - /// - internal class CSVHelper - { - internal CSVHelper(char delimiter) - { - Delimiter = delimiter; - } - - /// - /// Gets or sets the delimiter that separates the values. - /// - internal char Delimiter { get; } = ','; - - /// - /// Parse a CSV string. - /// - /// - /// String to be parsed. - /// - internal Collection ParseCsv(string csv) - { - csv = csv.Trim(); - Collection result = new Collection(); - if (csv.Length == 0 || csv[0] == '#') - { - return result; - } - - var reader = new StringReader(csv); - StringBuilder wordBuffer = new StringBuilder(); - - while (reader.Peek() != -1) - { - char nextChar = (char)reader.Read(); - - // if next character was delimiter or we are at the end, add string to result and clear wordBuffer - // else if next character was quote, perform reading until next quote and add it to wordBuffer - // else read and add it to wordBuffer - if (nextChar == Delimiter) - { - result.Add(wordBuffer.ToString()); - wordBuffer.Clear(); - } - else if (nextChar == '"') - { - bool inQuotes = true; - - // if we are within a quote section, read and append to wordBuffer until we find a next quote that is not followed by another quote - // if it is a single quote, escape the quote section - // if the quote is followed by an other quote, do not escape and add a quote character to wordBuffer - while (reader.Peek() != -1 && inQuotes) - { - nextChar = (char)reader.Read(); - - if (nextChar == '"') - { - if ((char)reader.Peek() == '"') - { - wordBuffer.Append(nextChar); - reader.Read(); - } - else - { - inQuotes = false; - } - } - else - { - wordBuffer.Append(nextChar); - } - } - } - else - { - wordBuffer.Append(nextChar); - } - } - - string lastWord = wordBuffer.ToString(); - if (lastWord != string.Empty) - { - result.Add(lastWord); - } - - reader.Close(); - return result; - } - } -} From 4998a71ddd5965720e9d68c93a78758af15a079e Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 10 Apr 2019 20:40:16 +0200 Subject: [PATCH 67/90] removed reader, implementation is a for loop again --- .../commands/utility/ImportAliasCommand.cs | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index bc0c91cb181..22a828e0c1f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -408,13 +408,10 @@ private Collection ParseCsvLine(string csv) { csv = csv.Trim(); Collection result = new Collection(); - - var reader = new StringReader(csv); StringBuilder wordBuffer = new StringBuilder(); - while (reader.Peek() != -1) - { - char nextChar = (char)reader.Read(); + for (int i = 0; i < csv.Length; i++) { + char nextChar = (char) csv[i]; // if next character was delimiter or we are at the end, add string to result and clear wordBuffer // else if next character was quote, perform reading until next quote and add it to wordBuffer @@ -431,16 +428,16 @@ private Collection ParseCsvLine(string csv) // if we are within a quote section, read and append to wordBuffer until we find a next quote that is not followed by another quote // if it is a single quote, escape the quote section // if the quote is followed by an other quote, do not escape and add a quote character to wordBuffer - while (reader.Peek() != -1 && inQuotes) - { - nextChar = (char)reader.Read(); + while(i ParseCsvLine(string csv) if (lastWord != string.Empty) { result.Add(lastWord); - } - - reader.Close(); + } return result; } From a77ecb9e4727ff7a78e8ad30b2336b64dbc69fe7 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 10 Apr 2019 20:47:50 +0200 Subject: [PATCH 68/90] made some methods static --- .../commands/utility/ImportAliasCommand.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index 22a828e0c1f..467d19b1063 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -324,7 +324,7 @@ private Collection GetAliasesFromFile(bool isLiteralPath) return result; } - private bool lineShouldBeSkipped(string line) + private static bool lineShouldBeSkipped(string line) { // if line is empty or a comment, return true return line.Length == 0 || OnlyContainsWhitespace(line) || line[0] == '#'; @@ -404,14 +404,14 @@ private bool isValidParsedLine(Collection parsedLine, bool optionsParsed return true; } - private Collection ParseCsvLine(string csv) + private static Collection ParseCsvLine(string csv) { - csv = csv.Trim(); + string csvTrimmed = csv.Trim(); Collection result = new Collection(); StringBuilder wordBuffer = new StringBuilder(); - for (int i = 0; i < csv.Length; i++) { - char nextChar = (char) csv[i]; + for (int i = 0; i < csvTrimmed.Length; i++) { + char nextChar = (char) csvTrimmed[i]; // if next character was delimiter or we are at the end, add string to result and clear wordBuffer // else if next character was quote, perform reading until next quote and add it to wordBuffer @@ -428,13 +428,13 @@ private Collection ParseCsvLine(string csv) // if we are within a quote section, read and append to wordBuffer until we find a next quote that is not followed by another quote // if it is a single quote, escape the quote section // if the quote is followed by an other quote, do not escape and add a quote character to wordBuffer - while(i Date: Wed, 10 Apr 2019 20:51:22 +0200 Subject: [PATCH 69/90] more codefactor regarding spaces and newlines --- .../commands/utility/ImportAliasCommand.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index 467d19b1063..b141d7ddb31 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -428,13 +428,13 @@ private static Collection ParseCsvLine(string csv) // if we are within a quote section, read and append to wordBuffer until we find a next quote that is not followed by another quote // if it is a single quote, escape the quote section // if the quote is followed by an other quote, do not escape and add a quote character to wordBuffer - while(i ParseCsvLine(string csv) if (lastWord != string.Empty) { result.Add(lastWord); - } + } + return result; } From 57fdb86169b4208a52e521bd5882cec656a04d77 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 10 Apr 2019 20:54:55 +0200 Subject: [PATCH 70/90] removed spaced codefactor was complaining about --- .../commands/utility/ImportAliasCommand.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index b141d7ddb31..a678ba3c2ed 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -411,7 +411,7 @@ private static Collection ParseCsvLine(string csv) StringBuilder wordBuffer = new StringBuilder(); for (int i = 0; i < csvTrimmed.Length; i++) { - char nextChar = (char) csvTrimmed[i]; + char nextChar = (char)csvTrimmed[i]; // if next character was delimiter or we are at the end, add string to result and clear wordBuffer // else if next character was quote, perform reading until next quote and add it to wordBuffer @@ -428,9 +428,10 @@ private static Collection ParseCsvLine(string csv) // if we are within a quote section, read and append to wordBuffer until we find a next quote that is not followed by another quote // if it is a single quote, escape the quote section // if the quote is followed by an other quote, do not escape and add a quote character to wordBuffer - while (i < csvTrimmed.Length && inQuotes) { + while (i < csvTrimmed.Length && inQuotes) + { i++; - nextChar = (char) csvTrimmed[i]; + nextChar = (char)csvTrimmed[i]; if (nextChar == '"') { From cc64a01dacd6107d4856d05d2c89801c1129ff27 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 10 Apr 2019 21:08:20 +0200 Subject: [PATCH 71/90] removed casts to char --- .../commands/utility/ImportAliasCommand.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index a678ba3c2ed..ce9a5c0b64f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -411,7 +411,7 @@ private static Collection ParseCsvLine(string csv) StringBuilder wordBuffer = new StringBuilder(); for (int i = 0; i < csvTrimmed.Length; i++) { - char nextChar = (char)csvTrimmed[i]; + char nextChar = csvTrimmed[i]; // if next character was delimiter or we are at the end, add string to result and clear wordBuffer // else if next character was quote, perform reading until next quote and add it to wordBuffer @@ -431,11 +431,11 @@ private static Collection ParseCsvLine(string csv) while (i < csvTrimmed.Length && inQuotes) { i++; - nextChar = (char)csvTrimmed[i]; + nextChar = csvTrimmed[i]; if (nextChar == '"') { - if (i + 1 < csvTrimmed.Length && (char)csvTrimmed[i + 1] == '"') + if (i + 1 < csvTrimmed.Length && csvTrimmed[i + 1] == '"') { wordBuffer.Append(nextChar); i++; From a63e2d560cfe9c2e8987fa340b8c9cf3b9a6cce9 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 10 Apr 2019 21:09:34 +0200 Subject: [PATCH 72/90] for loop bracket on new line --- .../commands/utility/ImportAliasCommand.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index ce9a5c0b64f..e63cd6587cd 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -410,7 +410,8 @@ private static Collection ParseCsvLine(string csv) Collection result = new Collection(); StringBuilder wordBuffer = new StringBuilder(); - for (int i = 0; i < csvTrimmed.Length; i++) { + for (int i = 0; i < csvTrimmed.Length; i++) + { char nextChar = csvTrimmed[i]; // if next character was delimiter or we are at the end, add string to result and clear wordBuffer From f942dac408979b9ec94205c742c1c1a6ad887dc8 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Wed, 10 Apr 2019 21:21:34 +0200 Subject: [PATCH 73/90] moved static methods to the top of the file. Did some renaming regarding capitals for function names --- .../commands/utility/ImportAliasCommand.cs | 188 +++++++++--------- 1 file changed, 94 insertions(+), 94 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index e63cd6587cd..fe47228ce5e 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -100,6 +100,93 @@ public SwitchParameter Force #endregion Parameters #region Command code + + private static bool OnlyContainsWhitespace(string line) + { + bool result = true; + + foreach (char c in line) + { + if (char.IsWhiteSpace(c) && c != '\n' && c != '\r') + { + continue; + } + + result = false; + break; + } + + return result; + } + + private static Collection ParseCsvLine(string csv) + { + string csvTrimmed = csv.Trim(); + Collection result = new Collection(); + StringBuilder wordBuffer = new StringBuilder(); + + for (int i = 0; i < csvTrimmed.Length; i++) + { + char nextChar = csvTrimmed[i]; + + // if next character was delimiter or we are at the end, add string to result and clear wordBuffer + // else if next character was quote, perform reading until next quote and add it to wordBuffer + // else read and add it to wordBuffer + if (nextChar == ',') + { + result.Add(wordBuffer.ToString()); + wordBuffer.Clear(); + } + else if (nextChar == '"') + { + bool inQuotes = true; + + // if we are within a quote section, read and append to wordBuffer until we find a next quote that is not followed by another quote + // if it is a single quote, escape the quote section + // if the quote is followed by an other quote, do not escape and add a quote character to wordBuffer + while (i < csvTrimmed.Length && inQuotes) + { + i++; + nextChar = csvTrimmed[i]; + + if (nextChar == '"') + { + if (i + 1 < csvTrimmed.Length && csvTrimmed[i + 1] == '"') + { + wordBuffer.Append(nextChar); + i++; + } + else + { + inQuotes = false; + } + } + else + { + wordBuffer.Append(nextChar); + } + } + } + else + { + wordBuffer.Append(nextChar); + } + } + + string lastWord = wordBuffer.ToString(); + if (lastWord != string.Empty) + { + result.Add(lastWord); + } + + return result; + } + + private static bool LineShouldBeSkipped(string line) + { + // if line is empty or a comment, return true + return line.Length == 0 || OnlyContainsWhitespace(line) || line[0] == '#'; + } /// /// The main processing loop of the command. @@ -302,7 +389,7 @@ private Collection GetAliasesFromFile(bool isLiteralPath) { ++lineNumber; - if (lineShouldBeSkipped(line)) + if (LineShouldBeSkipped(line)) { continue; } @@ -310,11 +397,11 @@ private Collection GetAliasesFromFile(bool isLiteralPath) Collection parsedLine = ParseCsvLine(line); // create options - KeyValuePair optionPair = createItemOptions(parsedLine, filePath, lineNumber); + KeyValuePair optionPair = CreateItemOptions(parsedLine, filePath, lineNumber); ScopedItemOptions options = optionPair.Key; - if (isValidParsedLine(parsedLine, optionPair.Value, lineNumber, filePath)) + if (IsValidParsedLine(parsedLine, optionPair.Value, lineNumber, filePath)) { - result.Add(constructAlias(parsedLine, options)); + result.Add(ConstructAlias(parsedLine, options)); } } @@ -323,14 +410,8 @@ private Collection GetAliasesFromFile(bool isLiteralPath) return result; } - - private static bool lineShouldBeSkipped(string line) - { - // if line is empty or a comment, return true - return line.Length == 0 || OnlyContainsWhitespace(line) || line[0] == '#'; - } - private KeyValuePair createItemOptions(Collection parsedLine, string filePath, long lineNumber) + private KeyValuePair CreateItemOptions(Collection parsedLine, string filePath, long lineNumber) { ScopedItemOptions options = ScopedItemOptions.None; bool succesfullParse = false; @@ -356,7 +437,7 @@ private KeyValuePair createItemOptions(Collection(options, succesfullParse); } - private AliasInfo constructAlias(Collection parsedLine, ScopedItemOptions options) + private AliasInfo ConstructAlias(Collection parsedLine, ScopedItemOptions options) { AliasInfo newAlias = new AliasInfo( @@ -373,7 +454,7 @@ private AliasInfo constructAlias(Collection parsedLine, ScopedItemOption return newAlias; } - private bool isValidParsedLine(Collection parsedLine, bool optionsParsedSuccesfully, long lineNumber, string filePath) + private bool IsValidParsedLine(Collection parsedLine, bool optionsParsedSuccesfully, long lineNumber, string filePath) { if (!optionsParsedSuccesfully) { @@ -404,69 +485,6 @@ private bool isValidParsedLine(Collection parsedLine, bool optionsParsed return true; } - private static Collection ParseCsvLine(string csv) - { - string csvTrimmed = csv.Trim(); - Collection result = new Collection(); - StringBuilder wordBuffer = new StringBuilder(); - - for (int i = 0; i < csvTrimmed.Length; i++) - { - char nextChar = csvTrimmed[i]; - - // if next character was delimiter or we are at the end, add string to result and clear wordBuffer - // else if next character was quote, perform reading until next quote and add it to wordBuffer - // else read and add it to wordBuffer - if (nextChar == ',') - { - result.Add(wordBuffer.ToString()); - wordBuffer.Clear(); - } - else if (nextChar == '"') - { - bool inQuotes = true; - - // if we are within a quote section, read and append to wordBuffer until we find a next quote that is not followed by another quote - // if it is a single quote, escape the quote section - // if the quote is followed by an other quote, do not escape and add a quote character to wordBuffer - while (i < csvTrimmed.Length && inQuotes) - { - i++; - nextChar = csvTrimmed[i]; - - if (nextChar == '"') - { - if (i + 1 < csvTrimmed.Length && csvTrimmed[i + 1] == '"') - { - wordBuffer.Append(nextChar); - i++; - } - else - { - inQuotes = false; - } - } - else - { - wordBuffer.Append(nextChar); - } - } - } - else - { - wordBuffer.Append(nextChar); - } - } - - string lastWord = wordBuffer.ToString(); - if (lastWord != string.Empty) - { - result.Add(lastWord); - } - - return result; - } - private StreamReader OpenFile(out string filePath, bool isLiteralPath) { StreamReader result = null; @@ -544,24 +562,6 @@ private void ThrowFileOpenError(Exception e, string pathWithError) errorRecord.ErrorDetails = new ErrorDetails(message); this.ThrowTerminatingError(errorRecord); } - - private static bool OnlyContainsWhitespace(string line) - { - bool result = true; - - foreach (char c in line) - { - if (char.IsWhiteSpace(c) && c != '\n' && c != '\r') - { - continue; - } - - result = false; - break; - } - - return result; - } #endregion Command code } } From 939babe106f8f3b10082ad42756e2d9303bf61c7 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 23 Apr 2019 14:25:59 +0200 Subject: [PATCH 74/90] iSazonov suggestions --- .../commands/utility/ImportAliasCommand.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index fe47228ce5e..aee4ec8bdd1 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -377,9 +377,6 @@ private bool VerifyShadowingExistingCommandsAndWriteError(string aliasName) private Collection GetAliasesFromFile(bool isLiteralPath) { Collection result = new Collection(); - - // it seems like filePath is always null and never defined, so we can remove it as far as I could see - // bug keeping it for now because tests are failing string filePath = null; using (StreamReader reader = OpenFile(out filePath, isLiteralPath)) { @@ -404,8 +401,6 @@ private Collection GetAliasesFromFile(bool isLiteralPath) result.Add(ConstructAlias(parsedLine, options)); } } - - reader.Dispose(); } return result; @@ -445,10 +440,10 @@ private AliasInfo ConstructAlias(Collection parsedLine, ScopedItemOption parsedLine[1], Context, options); - - if (!string.IsNullOrEmpty(parsedLine[2])) + string aliasDescription = parsedLine[2]; + if (!string.IsNullOrEmpty(aliasDescription)) { - newAlias.Description = parsedLine[2]; + newAlias.Description = aliasDescription; } return newAlias; From 11492366272ab06a432892576765d5ff42bdde60 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Tue, 23 Apr 2019 14:28:09 +0200 Subject: [PATCH 75/90] changed to else if --- .../commands/utility/ImportAliasCommand.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index aee4ec8bdd1..a562e05a72b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -454,9 +454,8 @@ private bool IsValidParsedLine(Collection parsedLine, bool optionsParsed if (!optionsParsedSuccesfully) { return false; - } - - if (parsedLine.Count != 4) + } + else if (parsedLine.Count != 4) { // if not four values, do ThrowTerminatingError(errorRecord) with ImportAliasFileFormatError, just like old implementation string message = StringUtil.Format(AliasCommandStrings.ImportAliasFileInvalidFormat, lineNumber); From 4c2795e8c5c3a810ee5e3975687f94d5c0a8e575 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sun, 12 May 2019 15:38:58 +0200 Subject: [PATCH 76/90] tests are failing locally, I reverted some things but not sure why they are suddenly failing --- .../commands/utility/ImportAliasCommand.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index a562e05a72b..79187818320 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -402,7 +402,7 @@ private Collection GetAliasesFromFile(bool isLiteralPath) } } } - + return result; } @@ -416,8 +416,8 @@ private KeyValuePair CreateItemOptions(Collection parsedLine, bool optionsParsed if (!optionsParsedSuccesfully) { return false; - } - else if (parsedLine.Count != 4) + } + if (parsedLine.Count != 4) { // if not four values, do ThrowTerminatingError(errorRecord) with ImportAliasFileFormatError, just like old implementation string message = StringUtil.Format(AliasCommandStrings.ImportAliasFileInvalidFormat, lineNumber); From 76d1992718f70f196e3d8cf106453a7ebf5c50b3 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sun, 12 May 2019 16:43:46 +0200 Subject: [PATCH 77/90] failing tests, the problem is that options is checked before the four values somehow. That throws an error instead of skipping the line --- .../commands/utility/ImportAliasCommand.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index 79187818320..c927a7bd65b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -412,7 +412,8 @@ private KeyValuePair CreateItemOptions(Collection(parsedLine[3], out options); + options = (ScopedItemOptions)Enum.Parse(parsedLine[3]); + succesfullParse = true; } catch (ArgumentException argException) { @@ -451,14 +452,15 @@ private AliasInfo ConstructAlias(Collection parsedLine, ScopedItemOption private bool IsValidParsedLine(Collection parsedLine, bool optionsParsedSuccesfully, long lineNumber, string filePath) { - if (!optionsParsedSuccesfully) - { - return false; - } + // if (!optionsParsedSuccesfully) + // { + // return false; + // } if (parsedLine.Count != 4) { // if not four values, do ThrowTerminatingError(errorRecord) with ImportAliasFileFormatError, just like old implementation - string message = StringUtil.Format(AliasCommandStrings.ImportAliasFileInvalidFormat, lineNumber); + //string message = StringUtil.Format(AliasCommandStrings.ImportAliasFileInvalidFormat, lineNumber); + string message = StringUtil.Format(AliasCommandStrings.ImportAliasFileInvalidFormat, filePath, lineNumber); FormatException formatException = new FormatException(message); @@ -476,7 +478,7 @@ private bool IsValidParsedLine(Collection parsedLine, bool optionsParsed return false; } - return true; + return optionsParsedSuccesfully; } private StreamReader OpenFile(out string filePath, bool isLiteralPath) From 82c882f45fed426cec5f79edb46bbfe3f030a256 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sun, 12 May 2019 17:00:18 +0200 Subject: [PATCH 78/90] changed order of error checking, now tests pass --- .../commands/utility/ImportAliasCommand.cs | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index c927a7bd65b..97e5c70bc3d 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -393,11 +393,9 @@ private Collection GetAliasesFromFile(bool isLiteralPath) Collection parsedLine = ParseCsvLine(line); - // create options - KeyValuePair optionPair = CreateItemOptions(parsedLine, filePath, lineNumber); - ScopedItemOptions options = optionPair.Key; - if (IsValidParsedLine(parsedLine, optionPair.Value, lineNumber, filePath)) + if (IsValidParsedLine(parsedLine, lineNumber, filePath)) { + ScopedItemOptions options = CreateItemOptions(parsedLine, filePath, lineNumber); result.Add(ConstructAlias(parsedLine, options)); } } @@ -406,14 +404,12 @@ private Collection GetAliasesFromFile(bool isLiteralPath) return result; } - private KeyValuePair CreateItemOptions(Collection parsedLine, string filePath, long lineNumber) + private ScopedItemOptions CreateItemOptions(Collection parsedLine, string filePath, long lineNumber) { ScopedItemOptions options = ScopedItemOptions.None; - bool succesfullParse = false; try { options = (ScopedItemOptions)Enum.Parse(parsedLine[3]); - succesfullParse = true; } catch (ArgumentException argException) { @@ -430,7 +426,7 @@ private KeyValuePair CreateItemOptions(Collection(options, succesfullParse); + return options; } private AliasInfo ConstructAlias(Collection parsedLine, ScopedItemOptions options) @@ -450,16 +446,11 @@ private AliasInfo ConstructAlias(Collection parsedLine, ScopedItemOption return newAlias; } - private bool IsValidParsedLine(Collection parsedLine, bool optionsParsedSuccesfully, long lineNumber, string filePath) + private bool IsValidParsedLine(Collection parsedLine, long lineNumber, string filePath) { - // if (!optionsParsedSuccesfully) - // { - // return false; - // } if (parsedLine.Count != 4) { // if not four values, do ThrowTerminatingError(errorRecord) with ImportAliasFileFormatError, just like old implementation - //string message = StringUtil.Format(AliasCommandStrings.ImportAliasFileInvalidFormat, lineNumber); string message = StringUtil.Format(AliasCommandStrings.ImportAliasFileInvalidFormat, filePath, lineNumber); FormatException formatException = @@ -478,7 +469,7 @@ private bool IsValidParsedLine(Collection parsedLine, bool optionsParsed return false; } - return optionsParsedSuccesfully; + return true; } private StreamReader OpenFile(out string filePath, bool isLiteralPath) From 4ac3f917cd0a5dc799e29280dd05342e60420a39 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sun, 12 May 2019 18:00:52 +0200 Subject: [PATCH 79/90] got rid of the try catch --- .../commands/utility/ImportAliasCommand.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index 97e5c70bc3d..cc2902e932d 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -406,18 +406,13 @@ private Collection GetAliasesFromFile(bool isLiteralPath) private ScopedItemOptions CreateItemOptions(Collection parsedLine, string filePath, long lineNumber) { - ScopedItemOptions options = ScopedItemOptions.None; - try - { - options = (ScopedItemOptions)Enum.Parse(parsedLine[3]); - } - catch (ArgumentException argException) - { + ScopedItemOptions options; + if(!Enum.TryParse(parsedLine[3], out options)){ // if parsing is no succes string message = StringUtil.Format(AliasCommandStrings.ImportAliasOptionsError, filePath, lineNumber); ErrorRecord errorRecord = new ErrorRecord( - argException, + new ArgumentException(), "ImportAliasOptionsError", ErrorCategory.ReadError, filePath); From cffef0138d57d2786c7b2b3433c3887433e238e8 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Mon, 13 May 2019 13:42:37 +0200 Subject: [PATCH 80/90] Update src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs Co-Authored-By: Ilya --- .../commands/utility/ImportAliasCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index cc2902e932d..a85158aca44 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -185,7 +185,7 @@ private static Collection ParseCsvLine(string csv) private static bool LineShouldBeSkipped(string line) { // if line is empty or a comment, return true - return line.Length == 0 || OnlyContainsWhitespace(line) || line[0] == '#'; + return line.Length == 0 || line[0] == '#' || OnlyContainsWhitespace(line); } /// From 98538ea786f6898f2b3db0acc357779b441b7f47 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Mon, 13 May 2019 13:56:17 +0200 Subject: [PATCH 81/90] small suggestions fix --- .../commands/utility/ImportAliasCommand.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index a85158aca44..58914dcb485 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -107,7 +107,7 @@ private static bool OnlyContainsWhitespace(string line) foreach (char c in line) { - if (char.IsWhiteSpace(c) && c != '\n' && c != '\r') + if (ReadOnlySpan.IsWhiteSpace(c) && c != '\n' && c != '\r') { continue; } @@ -470,8 +470,6 @@ private bool IsValidParsedLine(Collection parsedLine, long lineNumber, s private StreamReader OpenFile(out string filePath, bool isLiteralPath) { StreamReader result = null; - - // so remove this line right filePath = null; ProviderInfo provider = null; Collection paths = null; From e1e967133f4d68d7b56893556f7fab23b7838d91 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Mon, 13 May 2019 14:02:23 +0200 Subject: [PATCH 82/90] reverted alias tests to master version --- .../Import-Alias.Tests.ps1 | 163 +++++++----------- 1 file changed, 64 insertions(+), 99 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index e13442278ac..1b3959e72df 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -2,117 +2,82 @@ # Licensed under the MIT License. Describe "Import-Alias DRT Unit Tests" -Tags "CI" { $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory - $aliasFilename = "aliasFilename" - $fulltestpath = Join-Path -Path $testAliasDirectory -ChildPath $aliasFilename + $testAliases = "TestAliases" + $fulltestpath = Join-Path -Path $testAliasDirectory -ChildPath $testAliases BeforeEach { - New-Item -Path $testAliasDirectory -ItemType Directory -Force - remove-item alias:abcd* -force -ErrorAction SilentlyContinue - remove-item alias:ijkl* -force -ErrorAction SilentlyContinue - set-alias abcd01 efgh01 - set-alias abcd02 efgh02 - set-alias abcd03 efgh03 - set-alias abcd04 efgh04 - set-alias ijkl01 mnop01 - set-alias ijkl02 mnop02 - set-alias ijkl03 mnop03 - set-alias ijkl04 mnop04 + New-Item -Path $testAliasDirectory -ItemType Directory -Force + remove-item alias:abcd* -force -ErrorAction SilentlyContinue + remove-item alias:ijkl* -force -ErrorAction SilentlyContinue + set-alias abcd01 efgh01 + set-alias abcd02 efgh02 + set-alias abcd03 efgh03 + set-alias abcd04 efgh04 + set-alias ijkl01 mnop01 + set-alias ijkl02 mnop02 + set-alias ijkl03 mnop03 + set-alias ijkl04 mnop04 } - AfterEach { - Remove-Item -Path $testAliasDirectory -Recurse -Force -ErrorAction SilentlyContinue - } + AfterEach { + Remove-Item -Path $testAliasDirectory -Recurse -Force -ErrorAction SilentlyContinue + } - It "Import-Alias Resolve To Multiple will throw PSInvalidOperationException" { - { Import-Alias * -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.ImportAliasCommand" - } + It "Import-Alias Resolve To Multiple will throw PSInvalidOperationException" { + { Import-Alias * -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.ImportAliasCommand" + } - It "Import-Alias From Exported Alias File Aliases Already Exist should throw SessionStateException" { - { Export-Alias $fulltestpath abcd* } | Should -Not -Throw - { Import-Alias $fulltestpath -ErrorAction Stop } | Should -Throw -ErrorId "AliasAlreadyExists,Microsoft.PowerShell.Commands.ImportAliasCommand" + It "Import-Alias From Exported Alias File Aliases Already Exist should throw SessionStateException" { + { Export-Alias $fulltestpath abcd* } | Should -Not -Throw + { Import-Alias $fulltestpath -ErrorAction Stop } | Should -Throw -ErrorId "AliasAlreadyExists,Microsoft.PowerShell.Commands.ImportAliasCommand" } - It "Import-Alias Into Invalid Scope should throw PSArgumentException"{ - { Export-Alias $fulltestpath abcd* } | Should -Not -Throw - { Import-Alias $fulltestpath -scope bogus } | Should -Throw -ErrorId "Argument,Microsoft.PowerShell.Commands.ImportAliasCommand" + It "Import-Alias Into Invalid Scope should throw PSArgumentException"{ + { Export-Alias $fulltestpath abcd* } | Should -Not -Throw + { Import-Alias $fulltestpath -scope bogus } | Should -Throw -ErrorId "Argument,Microsoft.PowerShell.Commands.ImportAliasCommand" } - It "Import-Alias From Exported Alias File Aliases Already Exist using force should not throw"{ - {Export-Alias $fulltestpath abcd*} | Should -Not -Throw - {Import-Alias $fulltestpath -Force} | Should -Not -Throw + It "Import-Alias From Exported Alias File Aliases Already Exist using force should not throw"{ + {Export-Alias $fulltestpath abcd*} | Should -Not -Throw + {Import-Alias $fulltestpath -Force} | Should -Not -Throw } } Describe "Import-Alias" -Tags "CI" { - - BeforeAll { - $newLine = [Environment]::NewLine - - $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory - $aliasFilename = "pesteralias.txt" - $aliasFilenameMoreThanFourValues = "aliasFileMoreThanFourValues.txt" - $aliasFilenameLessThanFourValues = "aliasFileLessThanFourValues.txt" - - $aliasfile = Join-Path -Path $testAliasDirectory -ChildPath $aliasFilename - $aliasPathMoreThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameMoreThanFourValues - $aliasPathLessThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameLessThanFourValues - - $commandToAlias = "echo" - $alias1 = "pesterecho" - $alias2 = '"abc""def"' - $alias3 = '"aaa"' - $alias4 = '"a,b"' - - # create alias file - New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null - - # set header - $aliasFileContent = '# Alias File' + $newLine - $aliasFileContent += '# Exported by : alex' + $newLine - $aliasFileContent += '# Date/Time : Thursday, 12 November 2015 21:55:08' + $newLine - $aliasFileContent += '# Computer : archvm' - - # add various aliases - $aliasFileContent += $newLine + $alias1 + ',"' + $commandToAlias + '","","None"' - $aliasFileContent += $newLine + $alias2 + ',"' + $commandToAlias + '","","None"' - $aliasFileContent += $newLine + $alias3 + ',"' + $commandToAlias + '","","None"' - $aliasFileContent += $newLine + $alias4 + ',"' + $commandToAlias + '","","None"' - $aliasFileContent > $aliasfile - - # create invalid file with more than four values - New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null - $aliasFileContent = $newLine + '"v_1","v_2","v_3","v_4","v_5"' - $aliasFileContent > $aliasPathMoreThanFourValues - - # create invalid file with less than four values - New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null - $aliasFileContent = $newLine + '"v_1","v_2","v_3"' - $aliasFileContent > $aliasPathLessThanFourValues - } - - It "Should be able to import an alias file successfully" { - { Import-Alias -Path $aliasfile } | Should -Not -Throw - } - - It "Should classify an alias as non existent when it is not imported yet" { - {Get-Alias -Name invalid_alias -ErrorAction Stop} | Should -Throw -ErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand' - } - - It "Should be able to parse " -TestCases @( - @{ aliasToTest = 'abc"def' } - @{ aliasToTest = 'aaa' } - @{ aliasToTest = 'a,b' } - ) { - param($aliasToTest) - Import-Alias -Path $aliasfile - ( Get-Alias -Name $aliasToTest ).Definition | Should -BeExactly $commandToAlias - } - - It "Should throw an error when reading more than four values" { - { Import-Alias -Path $aliasPathMoreThanFourValues } | Should -Throw -ErrorId "ImportAliasFileFormatError,Microsoft.PowerShell.Commands.ImportAliasCommand" - } - - It "Should throw an error when reading less than four values" { - { Import-Alias -Path $aliasPathLessThanFourValues } | Should -Throw -ErrorId "ImportAliasFileFormatError,Microsoft.PowerShell.Commands.ImportAliasCommand" - } + $newLine=[Environment]::NewLine + $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory + $testAliases = "pesteralias.txt" + $pesteraliasfile = Join-Path -Path $testAliasDirectory -ChildPath $testAliases + + BeforeEach { + New-Item -Path $testAliasDirectory -ItemType Directory -Force + + $pesteraliascontent ='# Alias File'+$newLine + $pesteraliascontent+='# Exported by : alex'+$newLine + $pesteraliascontent+='# Date/Time : Thursday, 12 November 2015 21:55:08'+$newLine + $pesteraliascontent+='# Computer : archvm'+$newLine+'"pesterecho","echo","","None"' + $pesteraliascontent > $pesteraliasfile + } + + AfterEach { + Remove-Item -Path $testAliasDirectory -Recurse -Force + } + + It "Should be able to import an alias file successfully" { + { Import-Alias $pesteraliasfile } | Should -Not -throw + } + + It "Should be able to import file via the Import-Alias alias of ipal" { + { ipal $pesteraliasfile } | Should -Not -throw + } + + It "Should be able to import an alias file and perform imported aliased echo cmd" { + (Import-Alias $pesteraliasfile) + (pesterecho pestertesting) | Should -BeExactly "pestertesting" + } + + It "Should be able to use ipal alias to import an alias file and perform cmd" { + (ipal $pesteraliasfile) + (pesterecho pestertesting) | Should -BeExactly "pestertesting" + } } From 255dc56790d42fe3b94c8ab6ea637fa147443bcc Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Mon, 13 May 2019 14:06:22 +0200 Subject: [PATCH 83/90] typo in implementation, build broke --- .../commands/utility/ImportAliasCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index 58914dcb485..b898fece400 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -107,7 +107,7 @@ private static bool OnlyContainsWhitespace(string line) foreach (char c in line) { - if (ReadOnlySpan.IsWhiteSpace(c) && c != '\n' && c != '\r') + if (char.IsWhiteSpace(c) && c != '\n' && c != '\r') { continue; } From d9d8f37733f1ee84cb96d71781fd85d7de3f6cfd Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Mon, 20 May 2019 01:06:46 +0200 Subject: [PATCH 84/90] removed unnecessary checks --- .../commands/utility/ImportAliasCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index b898fece400..9949e79df3d 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -107,7 +107,7 @@ private static bool OnlyContainsWhitespace(string line) foreach (char c in line) { - if (char.IsWhiteSpace(c) && c != '\n' && c != '\r') + if (char.IsWhiteSpace(c)) { continue; } From fe0e2539bc7b8458a81e8f9e4c98dac112bb49ed Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sat, 13 Jul 2019 14:53:53 +0200 Subject: [PATCH 85/90] Update src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs Co-Authored-By: Ilya --- .../commands/utility/ImportAliasCommand.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index 9949e79df3d..013c5ed864b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -407,7 +407,8 @@ private Collection GetAliasesFromFile(bool isLiteralPath) private ScopedItemOptions CreateItemOptions(Collection parsedLine, string filePath, long lineNumber) { ScopedItemOptions options; - if(!Enum.TryParse(parsedLine[3], out options)){ + if(!Enum.TryParse(parsedLine[3], out options)) + { // if parsing is no succes string message = StringUtil.Format(AliasCommandStrings.ImportAliasOptionsError, filePath, lineNumber); ErrorRecord errorRecord = From 39d9f51a04dfd9c981f1950901fe8c1dfca15709 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sat, 13 Jul 2019 15:34:17 +0200 Subject: [PATCH 86/90] ReadOnlySpan suggestion --- .../commands/utility/ImportAliasCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index 013c5ed864b..a15a4f9875a 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -121,7 +121,7 @@ private static bool OnlyContainsWhitespace(string line) private static Collection ParseCsvLine(string csv) { - string csvTrimmed = csv.Trim(); + ReadOnlySpan csvTrimmed = csv.Trim(); Collection result = new Collection(); StringBuilder wordBuffer = new StringBuilder(); From e521cd162675fa31a5e1bae6e33083e091104c99 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sat, 13 Jul 2019 15:37:12 +0200 Subject: [PATCH 87/90] made methods static --- .../commands/utility/ImportAliasCommand.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index a15a4f9875a..0bcdb971737 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -404,7 +404,7 @@ private Collection GetAliasesFromFile(bool isLiteralPath) return result; } - private ScopedItemOptions CreateItemOptions(Collection parsedLine, string filePath, long lineNumber) + private static ScopedItemOptions CreateItemOptions(Collection parsedLine, string filePath, long lineNumber) { ScopedItemOptions options; if(!Enum.TryParse(parsedLine[3], out options)) @@ -425,7 +425,7 @@ private ScopedItemOptions CreateItemOptions(Collection parsedLine, strin return options; } - private AliasInfo ConstructAlias(Collection parsedLine, ScopedItemOptions options) + private static AliasInfo ConstructAlias(Collection parsedLine, ScopedItemOptions options) { AliasInfo newAlias = new AliasInfo( @@ -442,7 +442,7 @@ private AliasInfo ConstructAlias(Collection parsedLine, ScopedItemOption return newAlias; } - private bool IsValidParsedLine(Collection parsedLine, long lineNumber, string filePath) + private static bool IsValidParsedLine(Collection parsedLine, long lineNumber, string filePath) { if (parsedLine.Count != 4) { From 27937efce344b94d636679e4f2c9b5a8fe2580cb Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sat, 13 Jul 2019 15:52:06 +0200 Subject: [PATCH 88/90] made methods non static --- .../commands/utility/ImportAliasCommand.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index 0bcdb971737..28dc9674234 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -404,7 +404,7 @@ private Collection GetAliasesFromFile(bool isLiteralPath) return result; } - private static ScopedItemOptions CreateItemOptions(Collection parsedLine, string filePath, long lineNumber) + private ScopedItemOptions CreateItemOptions(Collection parsedLine, string filePath, long lineNumber) { ScopedItemOptions options; if(!Enum.TryParse(parsedLine[3], out options)) @@ -425,13 +425,13 @@ private static ScopedItemOptions CreateItemOptions(Collection parsedLine return options; } - private static AliasInfo ConstructAlias(Collection parsedLine, ScopedItemOptions options) + private AliasInfo ConstructAlias(Collection parsedLine, ScopedItemOptions options) { AliasInfo newAlias = new AliasInfo( parsedLine[0], parsedLine[1], - Context, + this.Context, options); string aliasDescription = parsedLine[2]; if (!string.IsNullOrEmpty(aliasDescription)) @@ -442,7 +442,7 @@ private static AliasInfo ConstructAlias(Collection parsedLine, ScopedIte return newAlias; } - private static bool IsValidParsedLine(Collection parsedLine, long lineNumber, string filePath) + private bool IsValidParsedLine(Collection parsedLine, long lineNumber, string filePath) { if (parsedLine.Count != 4) { From 8c59dac83c4df3edb41be398f0c83517bfd80e14 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sun, 14 Jul 2019 15:54:14 +0200 Subject: [PATCH 89/90] space after if, codefactor issue --- .../commands/utility/ImportAliasCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index 28dc9674234..058d9d900bd 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -407,7 +407,7 @@ private Collection GetAliasesFromFile(bool isLiteralPath) private ScopedItemOptions CreateItemOptions(Collection parsedLine, string filePath, long lineNumber) { ScopedItemOptions options; - if(!Enum.TryParse(parsedLine[3], out options)) + if (!Enum.TryParse(parsedLine[3], out options)) { // if parsing is no succes string message = StringUtil.Format(AliasCommandStrings.ImportAliasOptionsError, filePath, lineNumber); From d13b879c767f2ecce4e0cdcba8f38978e0383180 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Sat, 27 Jul 2019 15:43:23 +0200 Subject: [PATCH 90/90] i -> i + 1 in while check --- .../commands/utility/ImportAliasCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs index 058d9d900bd..c401f5311a1 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImportAliasCommand.cs @@ -144,7 +144,7 @@ private static Collection ParseCsvLine(string csv) // if we are within a quote section, read and append to wordBuffer until we find a next quote that is not followed by another quote // if it is a single quote, escape the quote section // if the quote is followed by an other quote, do not escape and add a quote character to wordBuffer - while (i < csvTrimmed.Length && inQuotes) + while (i + 1 < csvTrimmed.Length && inQuotes) { i++; nextChar = csvTrimmed[i];