Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Import-Csv.Tests.ps1185 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Import-Csv DRT Unit Tests" -Tags "CI" {4    BeforeAll {5        $fileToGenerate = Join-Path $TestDrive -ChildPath "importCSVTest.csv"6        $psObject = [pscustomobject]@{ "First" = "1"; "Second" = "2" }7    }8 9    It "Test import-csv with a delimiter parameter" {10        $delimiter = ';'11        $psObject | Export-Csv -Path $fileToGenerate -Delimiter $delimiter12        $returnObject = Import-Csv -Path $fileToGenerate -Delimiter $delimiter13        $returnObject.First | Should -Be 114        $returnObject.Second | Should -Be 215    }16 17    It "Test import-csv with UseCulture parameter" {18        $psObject | Export-Csv -Path $fileToGenerate -UseCulture19        $returnObject = Import-Csv -Path $fileToGenerate -UseCulture20        $returnObject.First | Should -Be 121        $returnObject.Second | Should -Be 222    }23}24 25Describe "Import-Csv Double Quote Delimiter" -Tags "CI" {26    BeforeAll {27        $empyValueCsv = @'28        a1""a329        v1"v2"v330'@31 32        $withValueCsv = @'33        a1"a2"a334        v1"v2"v335'@36 37        $quotedCharacterCsv = @'38        a1,a2,a339        v1,"v2",v340'@41    }42 43 44    It "Should handle <name> and bind to LiteralPath from pipeline" -TestCases @(45        @{ name = "quote with empty value"  ; expectedHeader = "a1,H1,a3"; file = "EmptyValue.csv"      ; content = $empyValueCsv       ; delimiter = '"' }46        @{ name = "quote with value"        ; expectedHeader = "a1,a2,a3"; file = "WithValue.csv"       ; content = $withValueCsv       ; delimiter = '"' }47        @{ name = "value enclosed in quote" ; expectedHeader = "a1,a2,a3"; file = "QuotedCharacter.csv" ; content = $quotedCharacterCsv ; delimiter = ',' }48        ){49        param($expectedHeader, $file, $content, $delimiter)50 51        $testPath = Join-Path $TestDrive $file52        Set-Content $testPath -Value $content53 54        $returnObject = Get-ChildItem -Path $testPath | Import-Csv -Delimiter $delimiter55        $actualHeader = $returnObject[0].psobject.Properties.name -join ','56        $actualHeader | Should -BeExactly $expectedHeader57 58        $returnObject = $testPath | Import-Csv -Delimiter $delimiter59        $actualHeader = $returnObject[0].psobject.Properties.name -join ','60        $actualHeader | Should -BeExactly $expectedHeader61 62        $returnObject = [pscustomobject]@{ LiteralPath = $testPath } | Import-Csv -Delimiter $delimiter63        $actualHeader = $returnObject[0].psobject.Properties.name -join ','64        $actualHeader | Should -BeExactly $expectedHeader65    }66 67    It "Should handle <name> and bind to Path from pipeline" -TestCases @(68        @{ name = "quote with empty value"  ; expectedHeader = "a1,H1,a3"; file = "EmptyValue.csv"      ; content = $empyValueCsv       ; delimiter = '"' }69        @{ name = "quote with value"        ; expectedHeader = "a1,a2,a3"; file = "WithValue.csv"       ; content = $withValueCsv       ; delimiter = '"' }70        @{ name = "value enclosed in quote" ; expectedHeader = "a1,a2,a3"; file = "QuotedCharacter.csv" ; content = $quotedCharacterCsv ; delimiter = ',' }71        ){72        param($expectedHeader, $file, $content, $delimiter)73 74        $testPath = Join-Path $TestDrive $file75        Set-Content $testPath -Value $content76 77        $returnObject = Get-ChildItem -Path $testPath | Import-Csv -Delimiter $delimiter78        $actualHeader = $returnObject[0].psobject.Properties.name -join ','79        $actualHeader | Should -BeExactly $expectedHeader80 81        $returnObject = $testPath | Import-Csv -Delimiter $delimiter82        $actualHeader = $returnObject[0].psobject.Properties.name -join ','83        $actualHeader | Should -BeExactly $expectedHeader84 85        $returnObject = [pscustomobject]@{ Path = $testPath } | Import-Csv -Delimiter $delimiter86        $actualHeader = $returnObject[0].psobject.Properties.name -join ','87        $actualHeader | Should -BeExactly $expectedHeader88    }89}90 91Describe "Import-Csv File Format Tests" -Tags "CI" {92    BeforeAll {93        # The file is w/o header94        $TestImportCsv_NoHeader = Join-Path -Path (Join-Path $PSScriptRoot -ChildPath assets) -ChildPath TestImportCsv_NoHeader.csv95        # The file is with header96        $TestImportCsv_WithHeader = Join-Path -Path (Join-Path $PSScriptRoot -ChildPath assets) -ChildPath TestImportCsv_WithHeader.csv97        # The file is W3C Extended Log File Format98        $TestImportCsv_W3C_ELF = Join-Path -Path (Join-Path $PSScriptRoot -ChildPath assets) -ChildPath TestImportCsv_W3C_ELF.csv99 100        $testCSVfiles = $TestImportCsv_NoHeader, $TestImportCsv_WithHeader, $TestImportCsv_W3C_ELF101        $orginalHeader = "Column1","Column2","Column 3"102        $customHeader = "test1","test2","test3"103    }104    # Test set is the same for all file formats105    foreach ($testCsv in $testCSVfiles) {106       $FileName = (Get-ChildItem $testCsv).Name107        Context "Next test file: $FileName" {108            BeforeAll {109                $CustomHeaderParams = @{Header = $customHeader; Delimiter = ","}110                if ($FileName -eq "TestImportCsv_NoHeader.csv") {111                    # The file does not have header112                    # (w/o Delimiter here we get throw (bug?))113                    $HeaderParams = @{Header = $orginalHeader; Delimiter = ","}114                } else {115                    # The files have header116                    $HeaderParams = @{Delimiter = ","}117                }118 119            }120 121            It "Should be able to import all fields" {122                $actual = Import-Csv -Path $testCsv @HeaderParams123                $actualfields = $actual[0].psobject.Properties.Name124                $actualfields | Should -Be $orginalHeader125            }126 127            It "Should be able to import all fields with custom header" {128                $actual = Import-Csv -Path $testCsv @CustomHeaderParams129                $actualfields = $actual[0].psobject.Properties.Name130                $actualfields | Should -Be $customHeader131            }132 133            It "Should be able to import correct values" {134                $actual = Import-Csv -Path $testCsv @HeaderParams135                $actual.count         | Should -Be 4136                $actual[0].'Column1'  | Should -BeExactly "data1"137                $actual[0].'Column2'  | Should -BeExactly "1"138                $actual[0].'Column 3' | Should -BeExactly "A"139            }140 141        }142    }143}144 145Describe "Import-Csv #Type Tests" -Tags "CI" {146    BeforeAll {147        $testfile = Join-Path $TestDrive -ChildPath "testfile.csv"148        Remove-Item -Path $testfile -Force -ErrorAction SilentlyContinue149        $processlist = (Get-Process)[0..1]150        $processlist | Export-Csv -Path $testfile -Force -IncludeTypeInformation151        $expectedProcessTypes = "System.Diagnostics.Process","CSV:System.Diagnostics.Process"152    }153 154    It "Test import-csv import Object" {155        $importObjectList = Import-Csv -Path $testfile156        $processlist.Count | Should -Be $importObjectList.Count157 158        $importTypes = $importObjectList[0].psobject.TypeNames159        $importTypes.Count | Should -Be $expectedProcessTypes.Count160        $importTypes[0] | Should -Be $expectedProcessTypes[0]161        $importTypes[1] | Should -Be $expectedProcessTypes[1]162    }163}164 165Describe "Import-Csv with different newlines" -Tags "CI" {166    It "Test import-csv with '<name>' newline" -TestCases @(167        @{ name = "CR"; newline = "`r" }168        @{ name = "LF"; newline = "`n" }169        @{ name = "CRLF"; newline = "`r`n" }170        ) {171        param($newline)172        $csvFile = Join-Path $TestDrive -ChildPath $((New-Guid).Guid)173        $delimiter = ','174        "h1,h2,h3$($newline)11,12,13$($newline)21,22,23$($newline)" | Out-File -FilePath $csvFile175        $returnObject = Import-Csv -Path $csvFile -Delimiter $delimiter176        $returnObject.Count | Should -Be 2177        $returnObject[0].h1 | Should -Be 11178        $returnObject[0].h2 | Should -Be 12179        $returnObject[0].h3 | Should -Be 13180        $returnObject[1].h1 | Should -Be 21181        $returnObject[1].h2 | Should -Be 22182        $returnObject[1].h3 | Should -Be 23183    }184}185