Skip to content

Commit 3b1a4a4

Browse files
sethvsiSazonov
authored andcommitted
Replace "Dbg.Assert" with 'if () throw' in CSVCommands.cs.. (#6910)
Add more useful messages.
1 parent f54085f commit 3b1a4a4

2 files changed

Lines changed: 67 additions & 11 deletions

File tree

src/Microsoft.PowerShell.Commands.Utility/commands/utility/CSVCommands.cs

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,10 @@ protected override void EndProcessing()
329329

330330
private void CreateFileStream()
331331
{
332-
Dbg.Assert(_path != null, "FileName is mandatory parameter");
332+
if (_path == null)
333+
{
334+
throw new InvalidOperationException(CsvCommandStrings.FileNameIsAMandatoryParameter);
335+
}
333336

334337
string resolvedFilePath = PathUtils.ResolveFilePath(this.Path, this, _isLiteralPath);
335338

@@ -416,8 +419,15 @@ private void CreateFileStream()
416419

417420
private void ReconcilePreexistingPropertyNames()
418421
{
419-
Dbg.Assert(_isActuallyAppending, "This method should only get called when appending");
420-
Dbg.Assert(_preexistingPropertyNames != null, "This method should only get called when we have successfully read preexisting property names");
422+
if (!_isActuallyAppending)
423+
{
424+
throw new InvalidOperationException(CsvCommandStrings.ReconcilePreexistingPropertyNamesMethodShouldOnlyGetCalledWhenAppending);
425+
}
426+
427+
if (_preexistingPropertyNames == null)
428+
{
429+
throw new InvalidOperationException(CsvCommandStrings.ReconcilePreexistingPropertyNamesMethodShouldOnlyGetCalledWhenPreexistingPropertyNamesHaveBeenReadSuccessfully);
430+
}
421431

422432
HashSet<string> appendedPropertyNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
423433
foreach (string appendedPropertyName in _propertyNames)
@@ -896,7 +906,10 @@ internal class ExportCsvHelper : IDisposable
896906
IList<string>
897907
BuildPropertyNames(PSObject source, IList<string> propertyNames)
898908
{
899-
Dbg.Assert(propertyNames == null, "This method should be called only once per cmdlet instance");
909+
if (propertyNames != null)
910+
{
911+
throw new InvalidOperationException(CsvCommandStrings.BuildPropertyNamesMethodShouldBeCalledOnlyOncePerCmdletInstance);
912+
}
900913

901914
// serialize only Extended and Adapted properties..
902915
PSMemberInfoCollection<PSPropertyInfo> srcPropertiesToSearch =
@@ -919,7 +932,10 @@ internal class ExportCsvHelper : IDisposable
919932
string
920933
ConvertPropertyNamesCSV(IList<string> propertyNames)
921934
{
922-
Dbg.Assert(propertyNames != null, "BuildPropertyNames should be called before this method");
935+
if (propertyNames == null)
936+
{
937+
throw new ArgumentNullException("propertyNames");
938+
}
923939

924940
StringBuilder dest = new StringBuilder();
925941
bool first = true;
@@ -949,7 +965,10 @@ internal class ExportCsvHelper : IDisposable
949965
string
950966
ConvertPSObjectToCSV(PSObject mshObject, IList<string> propertyNames)
951967
{
952-
Dbg.Assert(propertyNames != null, "PropertyName collection can be empty here, but it should not be null");
968+
if (propertyNames == null)
969+
{
970+
throw new ArgumentNullException("propertyNames");
971+
}
953972

954973
StringBuilder dest = new StringBuilder();
955974
bool first = true;
@@ -985,7 +1004,11 @@ internal class ExportCsvHelper : IDisposable
9851004
string
9861005
GetToStringValueForProperty(PSPropertyInfo property)
9871006
{
988-
Dbg.Assert(property != null, "Caller should validate the parameter");
1007+
if (property == null)
1008+
{
1009+
throw new ArgumentNullException("property");
1010+
}
1011+
9891012
string value = null;
9901013
try
9911014
{
@@ -1021,7 +1044,11 @@ internal class ExportCsvHelper : IDisposable
10211044
}
10221045
else
10231046
{
1024-
Dbg.Assert(tnh[0] != null, "type hierarchy should not have null values");
1047+
if (tnh[0] == null)
1048+
{
1049+
throw new InvalidOperationException(CsvCommandStrings.TypeHierarchyShouldNotHaveNullValues);
1050+
}
1051+
10251052
string temp = tnh[0];
10261053
//If type starts with CSV: remove it. This would happen when you export
10271054
//an imported object. import-csv adds CSV. prefix to the type.
@@ -1135,8 +1162,15 @@ internal class ImportCsvHelper
11351162

11361163
internal ImportCsvHelper(PSCmdlet cmdlet, char delimiter, IList<string> header, string typeName, StreamReader streamReader)
11371164
{
1138-
Dbg.Assert(cmdlet != null, "Caller should verify cmdlet != null");
1139-
Dbg.Assert(streamReader != null, "Caller should verify textReader != null");
1165+
if (cmdlet == null)
1166+
{
1167+
throw new ArgumentNullException("cmdlet");
1168+
}
1169+
1170+
if (streamReader == null)
1171+
{
1172+
throw new ArgumentNullException("streamReader");
1173+
}
11401174

11411175
_cmdlet = cmdlet;
11421176
_delimiter = delimiter;
@@ -1165,7 +1199,11 @@ bool EOF
11651199
char
11661200
ReadChar()
11671201
{
1168-
Dbg.Assert(!EOF, "This should not be called if EOF is reached");
1202+
if (EOF)
1203+
{
1204+
throw new InvalidOperationException(CsvCommandStrings.EOFIsReached);
1205+
}
1206+
11691207
int i = _sr.Read();
11701208
return (char)i;
11711209
}

src/Microsoft.PowerShell.Commands.Utility/resources/CsvCommandStrings.resx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,22 @@
136136
<data name="UseDefaultNameForUnspecifiedHeader" xml:space="preserve">
137137
<value>One or more headers were not specified. Default names starting with "H" have been used in place of any missing headers.</value>
138138
</data>
139+
<data name="FileNameIsAMandatoryParameter" xml:space="preserve">
140+
<value>FileName is a mandatory parameter.</value>
141+
</data>
142+
<data name="ReconcilePreexistingPropertyNamesMethodShouldOnlyGetCalledWhenAppending" xml:space="preserve">
143+
<value>ReconcilePreexistingPropertyNames method should only get called when appending.</value>
144+
</data>
145+
<data name="ReconcilePreexistingPropertyNamesMethodShouldOnlyGetCalledWhenPreexistingPropertyNamesHaveBeenReadSuccessfully" xml:space="preserve">
146+
<value>ReconcilePreexistingPropertyNames method should only get called when preexisting property names have been read successfully.</value>
147+
</data>
148+
<data name="BuildPropertyNamesMethodShouldBeCalledOnlyOncePerCmdletInstance" xml:space="preserve">
149+
<value>BuildPropertyNames method should be called only once per cmdlet instance.</value>
150+
</data>
151+
<data name="TypeHierarchyShouldNotHaveNullValues" xml:space="preserve">
152+
<value>Type hierarchy should not have null values.</value>
153+
</data>
154+
<data name="EOFIsReached" xml:space="preserve">
155+
<value>EOF is reached.</value>
156+
</data>
139157
</root>

0 commit comments

Comments
 (0)