Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Export-Alias.Tests.ps1164 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Import-Module HelpersCommon5 6Describe "Export-Alias DRT Unit Tests" -Tags "CI" {7 8	BeforeAll {9		$testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ExportAliasTestDirectory10		$testAliases        = "TestAliases"11    	$fulltestpath       = Join-Path -Path $testAliasDirectory -ChildPath $testAliases12 13		Remove-Item alias:abcd* -Force -ErrorAction SilentlyContinue14		Remove-Item alias:ijkl* -Force -ErrorAction SilentlyContinue15		Set-Alias abcd01 efgh0116		Set-Alias abcd02 efgh0217		Set-Alias abcd03 efgh0318		Set-Alias abcd04 efgh0419		Set-Alias ijkl01 mnop0120		Set-Alias ijkl02 mnop0221		Set-Alias ijkl03 mnop0322		Set-Alias ijkl04 mnop0423	}24 25	AfterAll {26		Remove-Item alias:abcd* -Force -ErrorAction SilentlyContinue27		Remove-Item alias:ijkl* -Force -ErrorAction SilentlyContinue28	}29 30    BeforeEach {31		New-Item -Path $testAliasDirectory -ItemType Directory -Force32    }33 34	AfterEach {35		Remove-Item -Path $testAliasDirectory -Recurse -Force36	}37 38    It "Export-Alias for exist file should work"{39		New-Item -Path $fulltestpath -ItemType File -Force40		{Export-Alias $fulltestpath} | Should -Not -Throw41    }42 43	It "Export-Alias resolving to multiple files will throw ReadWriteMultipleFilesNotSupported" {44		$null = New-Item -Path $TestDrive\foo -ItemType File45		$null = New-Item -Path $TestDrive\bar -ItemType File46		{ Export-Alias $TestDrive\* } | Should -Throw -ErrorId "ReadWriteMultipleFilesNotSupported,Microsoft.PowerShell.Commands.ExportAliasCommand"47 48		Remove-Item $TestDrive\foo -Force -ErrorAction SilentlyContinue49		Remove-Item $TestDrive\bar -Force -ErrorAction SilentlyContinue50	}51 52	It "Export-Alias with Invalid Scope will throw PSArgumentException" {53		{ Export-Alias $fulltestpath -Scope foodbar } | Should -Throw -ErrorId "Argument,Microsoft.PowerShell.Commands.ExportAliasCommand"54	}55 56	It "Export-Alias for Default"{57		Export-Alias $fulltestpath abcd01 -PassThru58		$fulltestpath | Should -FileContentMatchExactly '"abcd01","efgh01","","None"'59    }60 61	It "Export-Alias As CSV"{62		Export-Alias $fulltestpath abcd01 -As CSV -PassThru63		$fulltestpath | Should -FileContentMatchExactly '"abcd01","efgh01","","None"'64    }65 66	It "Export-Alias As CSV With Description"{67		Export-Alias $fulltestpath abcd01 -As CSV -Description "My Aliases" -PassThru68		$fulltestpath | Should -FileContentMatchExactly '"abcd01","efgh01","","None"'69		$fulltestpath | Should -FileContentMatchExactly "My Aliases"70    }71 72	It "Export-Alias As CSV With Multiline Description"{73		Export-Alias $fulltestpath abcd01 -As CSV -Description "My Aliases\nYour Aliases\nEveryones Aliases" -PassThru74		$fulltestpath | Should -FileContentMatchExactly '"abcd01","efgh01","","None"'75		$fulltestpath | Should -FileContentMatchExactly "My Aliases"76		$fulltestpath | Should -FileContentMatchExactly "Your Aliases"77		$fulltestpath | Should -FileContentMatchExactly "Everyones Aliases"78    }79 80	It "Export-Alias As Script"{81		Export-Alias $fulltestpath abcd01 -As Script -PassThru82		$fulltestpath | Should -FileContentMatchExactly 'set-alias -Name:"abcd01" -Value:"efgh01" -Description:"" -Option:"None"'83    }84 85	It "Export-Alias As Script With Multiline Description"{86		Export-Alias $fulltestpath abcd01 -As Script -Description "My Aliases\nYour Aliases\nEveryones Aliases" -PassThru87		$fulltestpath | Should -FileContentMatchExactly 'set-alias -Name:"abcd01" -Value:"efgh01" -Description:"" -Option:"None"'88		$fulltestpath | Should -FileContentMatchExactly "My Aliases"89		$fulltestpath | Should -FileContentMatchExactly "Your Aliases"90		$fulltestpath | Should -FileContentMatchExactly "Everyones Aliases"91    }92 93	It "Export-Alias for Force Test"{94		Export-Alias $fulltestpath abcd0195		Export-Alias $fulltestpath abcd02 -Force96		$fulltestpath | Should -Not -FileContentMatchExactly '"abcd01","efgh01","","None"'97		$fulltestpath | Should -FileContentMatchExactly '"abcd02","efgh02","","None"'98    }99 100	It "Export-Alias for Force ReadOnly Test" -Skip:(Test-IsRoot) {101		Export-Alias $fulltestpath abcd01102		if ( $IsWindows )103		{104			attrib +r $fulltestpath105		}106		else107		{108			chmod 444 $fulltestpath109		}110 111		{ Export-Alias $fulltestpath abcd02 } | Should -Throw -ErrorId "FileOpenFailure,Microsoft.PowerShell.Commands.ExportAliasCommand"112		Export-Alias $fulltestpath abcd03 -Force113		$fulltestpath | Should -Not -FileContentMatchExactly '"abcd01","efgh01","","None"'114		$fulltestpath | Should -Not -FileContentMatchExactly '"abcd02","efgh02","","None"'115		$fulltestpath | Should -FileContentMatchExactly '"abcd03","efgh03","","None"'116 117		if ( $IsWindows )118		{119			attrib -Recurse $fulltestpath120		}121		else122		{123			chmod a+rw $fulltestpath124		}125 126    }127}128 129Describe "Export-Alias" -Tags "CI" {130 131	BeforeAll {132		$testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ExportAliasTestDirectory133		$testAliases        = "TestAliases"134		$fulltestpath       = Join-Path -Path $testAliasDirectory -ChildPath $testAliases135	}136 137	BeforeEach {138		New-Item -Path $testAliasDirectory -ItemType Directory -Force139	}140 141	AfterEach {142		Remove-Item -Path $testAliasDirectory -Recurse -Force143	}144 145	It "Should be able to create a file in the specified location"{146		Export-Alias $fulltestpath147		Test-Path $fulltestpath | Should -BeTrue148  }149 150  It "Should create a file with the list of aliases that match the expected list" {151		Export-Alias $fulltestpath152		Test-Path $fulltestpath | Should -BeTrue153 154		$actual   = Get-Content $fulltestpath | Sort-Object155		$expected = Get-Command -CommandType Alias156 157		for ( $i=0; $i -lt $expected.Length; $i++)158		{159			# We loop through the expected list and not the other because the output writes some comments to the file.160			$expected[$i] | Should -Match $actual[$i].Name161		}162  }163}164