Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,10 @@ protected override void EndProcessing()

private void CreateFileStream()
{
Dbg.Assert(_path != null, "FileName is mandatory parameter");
if (_path == null)
{
throw new InvalidOperationException(CsvCommandStrings.FileNameIsAMandatoryParameter);
}

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

Expand Down Expand Up @@ -416,8 +419,15 @@ private void CreateFileStream()

private void ReconcilePreexistingPropertyNames()
{
Dbg.Assert(_isActuallyAppending, "This method should only get called when appending");
Dbg.Assert(_preexistingPropertyNames != null, "This method should only get called when we have successfully read preexisting property names");
if (!_isActuallyAppending)
{
throw new InvalidOperationException(CsvCommandStrings.ReconcilePreexistingPropertyNamesMethodShouldOnlyGetCalledWhenAppending);
}

if (_preexistingPropertyNames == null)
{
throw new InvalidOperationException(CsvCommandStrings.ReconcilePreexistingPropertyNamesMethodShouldOnlyGetCalledWhenPreexistingPropertyNamesHaveBeenReadSuccessfully);
}

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

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

StringBuilder dest = new StringBuilder();
bool first = true;
Expand Down Expand Up @@ -949,7 +965,10 @@ internal class ExportCsvHelper : IDisposable
string
ConvertPSObjectToCSV(PSObject mshObject, IList<string> propertyNames)
{
Dbg.Assert(propertyNames != null, "PropertyName collection can be empty here, but it should not be null");
if (propertyNames == null)
{
throw new ArgumentNullException("propertyNames");
}

StringBuilder dest = new StringBuilder();
bool first = true;
Expand Down Expand Up @@ -985,7 +1004,11 @@ internal class ExportCsvHelper : IDisposable
string
GetToStringValueForProperty(PSPropertyInfo property)
{
Dbg.Assert(property != null, "Caller should validate the parameter");
if (property == null)
{
throw new ArgumentNullException("property");
}

string value = null;
try
{
Expand Down Expand Up @@ -1021,7 +1044,11 @@ internal class ExportCsvHelper : IDisposable
}
else
{
Dbg.Assert(tnh[0] != null, "type hierarchy should not have null values");
if (tnh[0] == null)
{
throw new InvalidOperationException(CsvCommandStrings.TypeHierarchyShouldNotHaveNullValues);
}

string temp = tnh[0];
//If type starts with CSV: remove it. This would happen when you export
//an imported object. import-csv adds CSV. prefix to the type.
Expand Down Expand Up @@ -1135,8 +1162,15 @@ internal class ImportCsvHelper

internal ImportCsvHelper(PSCmdlet cmdlet, char delimiter, IList<string> header, string typeName, StreamReader streamReader)
{
Dbg.Assert(cmdlet != null, "Caller should verify cmdlet != null");
Dbg.Assert(streamReader != null, "Caller should verify textReader != null");
if (cmdlet == null)
{
throw new ArgumentNullException("cmdlet");
}

if (streamReader == null)
{
throw new ArgumentNullException("streamReader");
}

_cmdlet = cmdlet;
_delimiter = delimiter;
Expand Down Expand Up @@ -1165,7 +1199,11 @@ bool EOF
char
ReadChar()
{
Dbg.Assert(!EOF, "This should not be called if EOF is reached");
if (EOF)
{
throw new InvalidOperationException(CsvCommandStrings.EOFIsReached);
}

int i = _sr.Read();
return (char)i;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,22 @@
<data name="UseDefaultNameForUnspecifiedHeader" xml:space="preserve">
<value>One or more headers were not specified. Default names starting with "H" have been used in place of any missing headers.</value>
</data>
<data name="FileNameIsAMandatoryParameter" xml:space="preserve">
<value>FileName is a mandatory parameter.</value>
</data>
<data name="ReconcilePreexistingPropertyNamesMethodShouldOnlyGetCalledWhenAppending" xml:space="preserve">
<value>ReconcilePreexistingPropertyNames method should only get called when appending.</value>
</data>
<data name="ReconcilePreexistingPropertyNamesMethodShouldOnlyGetCalledWhenPreexistingPropertyNamesHaveBeenReadSuccessfully" xml:space="preserve">
<value>ReconcilePreexistingPropertyNames method should only get called when preexisting property names have been read successfully.</value>
</data>
<data name="BuildPropertyNamesMethodShouldBeCalledOnlyOncePerCmdletInstance" xml:space="preserve">
<value>BuildPropertyNames method should be called only once per cmdlet instance.</value>
</data>
<data name="TypeHierarchyShouldNotHaveNullValues" xml:space="preserve">
<value>Type hierarchy should not have null values.</value>
</data>
<data name="EOFIsReached" xml:space="preserve">
<value>EOF is reached.</value>
</data>
</root>