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 @@ -67,22 +67,18 @@ public abstract PSObject InputObject
}

/// <summary>
/// NoTypeInformation : should the #TYPE line be generated
/// IncludeTypeInformation : The #TYPE line should be generated. Default is false. Cannot specify with NoTypeInformation.
/// </summary>
[Parameter]
[Alias("ITI")]
public SwitchParameter IncludeTypeInformation { get; set; }

/// <summary>
/// NoTypeInformation : The #TYPE line should not be generated. Default is true. Cannot specify with IncludeTypeInformation.
/// </summary>
[Parameter(DontShow = true)]
[Alias("NTI")]
public SwitchParameter NoTypeInformation
{
get
{
return _noTypeInformation;
}
set
{
_noTypeInformation = value;
}
}
private bool _noTypeInformation;
public SwitchParameter NoTypeInformation { get; set; } = true;

#endregion Command Line Parameters

Expand All @@ -100,6 +96,16 @@ public virtual void WriteCsvLine(string line)
/// </summary>
protected override void BeginProcessing()
{
if (this.MyInvocation.BoundParameters.ContainsKey(nameof(IncludeTypeInformation)) && this.MyInvocation.BoundParameters.ContainsKey(nameof(NoTypeInformation)))
{
InvalidOperationException exception = new InvalidOperationException(CsvCommandStrings.CannotSpecifyIncludeTypeInformationAndNoTypeInformation);
ErrorRecord errorRecord = new ErrorRecord(exception, "CannotSpecifyIncludeTypeInformationAndNoTypeInformation", ErrorCategory.InvalidData, null);
this.ThrowTerminatingError(errorRecord);
}
if (this.MyInvocation.BoundParameters.ContainsKey("IncludeTypeInformation"))
{
NoTypeInformation = !IncludeTypeInformation;
}
_delimiter = ImportExportCSVHelper.SetDelimiter(this, ParameterSetName, _delimiter, UseCulture);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@
Reviewed by TArcher on 2010-06-29.
</comment>
</data>
<data name="CannotSpecifyIncludeTypeInformationAndNoTypeInformation" xml:space="preserve">
<value>You must specify either the -IncludeTypeInformation or -NoTypeInformation parameters, but not both.</value>
</data>
<data name="CannotSpecifyPathAndLiteralPath" xml:space="preserve">
<value>You must specify either the -Path or -LiteralPath parameters, but not both.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Describe "ConvertTo-Csv DRT Unit Tests" -Tags "CI" {
$inputObject = [pscustomobject]@{ First = 1; Second = 2 }

It "Test convertto-csv with psobject pipelined" {
$returnObject = $inputObject | ConvertTo-Csv
$returnObject = $inputObject | ConvertTo-Csv -IncludeTypeInformation
$returnObject.Count | Should Be 3
$returnObject[0] | Should Be "#TYPE System.Management.Automation.PSCustomObject"
$returnObject[1] | Should Be "`"First`",`"Second`""
Expand All @@ -18,7 +18,7 @@ Describe "ConvertTo-Csv DRT Unit Tests" -Tags "CI" {

It "Test convertto-csv with a useculture flag" {
#The default value is ','
$returnObject = $inputObject | ConvertTo-Csv -UseCulture
$returnObject = $inputObject | ConvertTo-Csv -UseCulture -IncludeTypeInformation
$returnObject.Count | Should Be 3
$returnObject[0] | Should Be "#TYPE System.Management.Automation.PSCustomObject"
$returnObject[1] | Should Be "`"First`",`"Second`""
Expand All @@ -27,7 +27,7 @@ Describe "ConvertTo-Csv DRT Unit Tests" -Tags "CI" {

It "Test convertto-csv with Delimiter" {
#The default value is ','
$returnObject = $inputObject | ConvertTo-Csv -Delimiter ";"
$returnObject = $inputObject | ConvertTo-Csv -Delimiter ";" -IncludeTypeInformation
$returnObject.Count | Should Be 3
$returnObject[0] | Should Be "#TYPE System.Management.Automation.PSCustomObject"
$returnObject[1] | Should Be "`"First`";`"Second`""
Expand All @@ -50,22 +50,47 @@ Describe "ConvertTo-Csv" -Tags "CI" {
}

It "Should return the type of data in the first element of the output array" {
$result = $testObject | ConvertTo-Csv
$result = $testObject | ConvertTo-Csv -IncludeTypeInformation

$result[0] | Should Be "#TYPE System.Management.Automation.PSCustomObject"
}

It "Should return the column info in the second element of the output array" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could remove the test - it is dup test in line 28.
Or it is better move the checks and next test to the test in line 28 - there we can check a type, a header and data.

It is minor comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iSazonov Which test do you want to remove? the one you comment is on (lin 58) is not a duplicate of any of the tests. Line 28 is a delimiter test which needs to remain as its a different logic gate (parameter set).

the test on line 52 is now redundant with the the test on line 84, though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we could try to refactor the tests but not in the PR.
So skip the minor comment.

$result = $testObject | ConvertTo-Csv
$result = $testObject | ConvertTo-Csv -IncludeTypeInformation

$result[1] | Should Match "`"FirstColumn`""
$result[1] | Should Match "`"SecondColumn`""
}

It "Should return the data as a comma-separated list in the third element of the output array" {
$result = $testObject | ConvertTo-Csv
$result = $testObject | ConvertTo-Csv -IncludeTypeInformation
$result[2] | Should Match "`"Hello`""
$result[2] | Should Match "`"World`""
}

It "Includes type information when -IncludeTypeInformation is supplied" {
$result = $testObject | ConvertTo-Csv -IncludeTypeInformation

($result -split ([Environment]::NewLine))[0] | Should BeExactly "#TYPE System.Management.Automation.PSCustomObject"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need add reverse test to check that ""#TYPE " is absent if -IncludeTypeInformation is absent.
Also that -NoTypeInformation do nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure I'll add those and do the same for the Export-Csv tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}

It "Does not include type information by default" {
$result = $testObject | ConvertTo-Csv

$result | Should Not Match ([regex]::Escape('System.Management.Automation.PSCustomObject'))
$result | Should Not Match ([regex]::Escape('#TYPE'))
}

It "Does not include type information with -NoTypeInformation" {
$result = $testObject | ConvertTo-Csv -NoTypeInformation

$result | Should Not Match ([regex]::Escape('System.Management.Automation.PSCustomObject'))
$result | Should Not Match ([regex]::Escape('#TYPE'))
}

It "Does not support -IncludeTypeInformation and -NoTypeInformation at the same time" {
{ $testObject | ConvertTo-Csv -IncludeTypeInformation -NoTypeInformation } |
ShouldBeErrorId "CannotSpecifyIncludeTypeInformationAndNoTypeInformation,Microsoft.PowerShell.Commands.ConvertToCsvCommand"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ Describe "Export-Csv" -Tags "CI" {
}

It "Should be a string when exporting via pipe" {
$testObject | Export-Csv $testCsv
$testObject | Export-Csv $testCsv -IncludeTypeInformation

$piped = Get-Content $testCsv

$piped[0] | Should Match ".String"
}

It "Should be an object when exporting via the inputObject switch" {
Export-Csv -InputObject $testObject -Path $testCsv
Export-Csv -InputObject $testObject -Path $testCsv -IncludeTypeInformation

$switch = Get-Content $testCsv
$switch = Get-Content $testCsv

$switch[0] | Should Match ".Object"
}

It "Should output a csv file containing a string of all the lengths of each element when piped input is used" {
$testObject | Export-Csv -Path $testCsv
$testObject | Export-Csv -Path $testCsv -IncludeTypeInformation

$first = "`"" + $testObject[0].Length.ToString() + "`""
$second = "`"" + $testObject[1].Length.ToString() + "`""
Expand Down Expand Up @@ -63,6 +63,31 @@ Describe "Export-Csv" -Tags "CI" {
# Clean up after yourself
Remove-Item $aliasObject -Force
}

It "Does not include type information by default" {
$testObject | Export-Csv -Path $testCsv

$(Get-Content $testCsv)[0] | Should Not Match ([regex]::Escape("System.String"))
$(Get-Content $testCsv)[0] | Should Not Match ([regex]::Escape("#TYPE"))
}

It "Does not include type information with -NoTypeInformation" {
$testObject | Export-Csv -Path $testCsv -NoTypeInformation

$(Get-Content $testCsv)[0] | Should Not Match ([regex]::Escape("System.String"))
$(Get-Content $testCsv)[0] | Should Not Match ([regex]::Escape("#TYPE"))
}

It "Includes type information when -IncludeTypeInformation is supplied" {
$testObject | Export-Csv -Path $testCsv -IncludeTypeInformation

$(Get-Content $testCsv)[0] | Should BeExactly "#TYPE System.String"
}

It "Does not support -IncludeTypeInformation and -NoTypeInformation at the same time" {
{ $testObject | Export-Csv -Path $testCsv -IncludeTypeInformation -NoTypeInformation } |
ShouldBeErrorId "CannotSpecifyIncludeTypeInformationAndNoTypeInformation,Microsoft.PowerShell.Commands.ExportCsvCommand"
}
}

Describe "Export-Csv DRT Unit Tests" -Tags "CI" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Describe "Import-Csv #Type Tests" -Tags "CI" {
$testfile = Join-Path $TestDrive -ChildPath "testfile.csv"
Remove-Item -Path $testfile -Force -ErrorAction SilentlyContinue
$processlist = (Get-Process)[0..1]
$processlist | Export-Csv -Path $testfile -Force
$processlist | Export-Csv -Path $testfile -Force -IncludeTypeInformation
# Import-Csv add "CSV:" before actual type
$expectedProcessType = "CSV:System.Diagnostics.Process"
}
Expand Down