File size: 5,719 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Import-Module HelpersCommon

Describe "Export-Alias DRT Unit Tests" -Tags "CI" {

	BeforeAll {
		$testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ExportAliasTestDirectory
		$testAliases        = "TestAliases"
    	$fulltestpath       = Join-Path -Path $testAliasDirectory -ChildPath $testAliases

		Remove-Item alias:abcd* -Force -ErrorAction SilentlyContinue
		Remove-Item alias:ijkl* -Force -ErrorAction SilentlyContinue
		Set-Alias abcd01 efgh01
		Set-Alias abcd02 efgh02
		Set-Alias abcd03 efgh03
		Set-Alias abcd04 efgh04
		Set-Alias ijkl01 mnop01
		Set-Alias ijkl02 mnop02
		Set-Alias ijkl03 mnop03
		Set-Alias ijkl04 mnop04
	}

	AfterAll {
		Remove-Item alias:abcd* -Force -ErrorAction SilentlyContinue
		Remove-Item alias:ijkl* -Force -ErrorAction SilentlyContinue
	}

    BeforeEach {
		New-Item -Path $testAliasDirectory -ItemType Directory -Force
    }

	AfterEach {
		Remove-Item -Path $testAliasDirectory -Recurse -Force
	}

    It "Export-Alias for exist file should work"{
		New-Item -Path $fulltestpath -ItemType File -Force
		{Export-Alias $fulltestpath} | Should -Not -Throw
    }

	It "Export-Alias resolving to multiple files will throw ReadWriteMultipleFilesNotSupported" {
		$null = New-Item -Path $TestDrive\foo -ItemType File
		$null = New-Item -Path $TestDrive\bar -ItemType File
		{ Export-Alias $TestDrive\* } | Should -Throw -ErrorId "ReadWriteMultipleFilesNotSupported,Microsoft.PowerShell.Commands.ExportAliasCommand"

		Remove-Item $TestDrive\foo -Force -ErrorAction SilentlyContinue
		Remove-Item $TestDrive\bar -Force -ErrorAction SilentlyContinue
	}

	It "Export-Alias with Invalid Scope will throw PSArgumentException" {
		{ Export-Alias $fulltestpath -Scope foodbar } | Should -Throw -ErrorId "Argument,Microsoft.PowerShell.Commands.ExportAliasCommand"
	}

	It "Export-Alias for Default"{
		Export-Alias $fulltestpath abcd01 -PassThru
		$fulltestpath | Should -FileContentMatchExactly '"abcd01","efgh01","","None"'
    }

	It "Export-Alias As CSV"{
		Export-Alias $fulltestpath abcd01 -As CSV -PassThru
		$fulltestpath | Should -FileContentMatchExactly '"abcd01","efgh01","","None"'
    }

	It "Export-Alias As CSV With Description"{
		Export-Alias $fulltestpath abcd01 -As CSV -Description "My Aliases" -PassThru
		$fulltestpath | Should -FileContentMatchExactly '"abcd01","efgh01","","None"'
		$fulltestpath | Should -FileContentMatchExactly "My Aliases"
    }

	It "Export-Alias As CSV With Multiline Description"{
		Export-Alias $fulltestpath abcd01 -As CSV -Description "My Aliases\nYour Aliases\nEveryones Aliases" -PassThru
		$fulltestpath | Should -FileContentMatchExactly '"abcd01","efgh01","","None"'
		$fulltestpath | Should -FileContentMatchExactly "My Aliases"
		$fulltestpath | Should -FileContentMatchExactly "Your Aliases"
		$fulltestpath | Should -FileContentMatchExactly "Everyones Aliases"
    }

	It "Export-Alias As Script"{
		Export-Alias $fulltestpath abcd01 -As Script -PassThru
		$fulltestpath | Should -FileContentMatchExactly 'set-alias -Name:"abcd01" -Value:"efgh01" -Description:"" -Option:"None"'
    }

	It "Export-Alias As Script With Multiline Description"{
		Export-Alias $fulltestpath abcd01 -As Script -Description "My Aliases\nYour Aliases\nEveryones Aliases" -PassThru
		$fulltestpath | Should -FileContentMatchExactly 'set-alias -Name:"abcd01" -Value:"efgh01" -Description:"" -Option:"None"'
		$fulltestpath | Should -FileContentMatchExactly "My Aliases"
		$fulltestpath | Should -FileContentMatchExactly "Your Aliases"
		$fulltestpath | Should -FileContentMatchExactly "Everyones Aliases"
    }

	It "Export-Alias for Force Test"{
		Export-Alias $fulltestpath abcd01
		Export-Alias $fulltestpath abcd02 -Force
		$fulltestpath | Should -Not -FileContentMatchExactly '"abcd01","efgh01","","None"'
		$fulltestpath | Should -FileContentMatchExactly '"abcd02","efgh02","","None"'
    }

	It "Export-Alias for Force ReadOnly Test" -Skip:(Test-IsRoot) {
		Export-Alias $fulltestpath abcd01
		if ( $IsWindows )
		{
			attrib +r $fulltestpath
		}
		else
		{
			chmod 444 $fulltestpath
		}

		{ Export-Alias $fulltestpath abcd02 } | Should -Throw -ErrorId "FileOpenFailure,Microsoft.PowerShell.Commands.ExportAliasCommand"
		Export-Alias $fulltestpath abcd03 -Force
		$fulltestpath | Should -Not -FileContentMatchExactly '"abcd01","efgh01","","None"'
		$fulltestpath | Should -Not -FileContentMatchExactly '"abcd02","efgh02","","None"'
		$fulltestpath | Should -FileContentMatchExactly '"abcd03","efgh03","","None"'

		if ( $IsWindows )
		{
			attrib -Recurse $fulltestpath
		}
		else
		{
			chmod a+rw $fulltestpath
		}

    }
}

Describe "Export-Alias" -Tags "CI" {

	BeforeAll {
		$testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ExportAliasTestDirectory
		$testAliases        = "TestAliases"
		$fulltestpath       = Join-Path -Path $testAliasDirectory -ChildPath $testAliases
	}

	BeforeEach {
		New-Item -Path $testAliasDirectory -ItemType Directory -Force
	}

	AfterEach {
		Remove-Item -Path $testAliasDirectory -Recurse -Force
	}

	It "Should be able to create a file in the specified location"{
		Export-Alias $fulltestpath
		Test-Path $fulltestpath | Should -BeTrue
  }

  It "Should create a file with the list of aliases that match the expected list" {
		Export-Alias $fulltestpath
		Test-Path $fulltestpath | Should -BeTrue

		$actual   = Get-Content $fulltestpath | Sort-Object
		$expected = Get-Command -CommandType Alias

		for ( $i=0; $i -lt $expected.Length; $i++)
		{
			# We loop through the expected list and not the other because the output writes some comments to the file.
			$expected[$i] | Should -Match $actual[$i].Name
		}
  }
}