@@ -34,7 +34,7 @@ Describe 'Save-SqlDscSqlServerMediaFile' -Tag @('Integration_SQL2017', 'Integrat
3434 # Create a temporary directory for testing downloads
3535 $script :testDownloadPath = Join-Path - Path $env: TEMP - ChildPath " SqlDscTestDownloads_$ ( Get-Random ) "
3636 New-Item - Path $script :testDownloadPath - ItemType Directory - Force | Out-Null
37-
37+
3838 Write-Verbose - Message " Created test download directory: $script :testDownloadPath " - Verbose
3939 }
4040
@@ -52,7 +52,7 @@ Describe 'Save-SqlDscSqlServerMediaFile' -Tag @('Integration_SQL2017', 'Integrat
5252 # Use SQL Server 2017 ISO URL for testing direct ISO download
5353 $script :directIsoUrl = ' https://download.microsoft.com/download/E/F/2/EF23C21D-7860-4F05-88CE-39AA114B014B/SQLServer2017-x64-ENU.iso'
5454 $script :expectedFileName = ' SQLServer2017-test.iso'
55-
55+
5656 # Create separate subdirectory for this context to avoid ISO file conflicts
5757 $script :directIsoTestPath = Join-Path - Path $script :testDownloadPath - ChildPath ' DirectIso'
5858 New-Item - Path $script :directIsoTestPath - ItemType Directory - Force | Out-Null
@@ -63,12 +63,12 @@ Describe 'Save-SqlDscSqlServerMediaFile' -Tag @('Integration_SQL2017', 'Integrat
6363
6464 # Verify the result is a FileInfo object
6565 $result | Should - BeOfType [System.IO.FileInfo ]
66-
66+
6767 # Verify the file was downloaded
6868 $result.Name | Should - Be $script :expectedFileName
6969 $result.Exists | Should - BeTrue
7070 $result.Length | Should - BeGreaterThan 0
71-
71+
7272 # Verify the file is in the expected location
7373 $expectedPath = Join-Path - Path $script :directIsoTestPath - ChildPath $script :expectedFileName
7474 Test-Path - Path $expectedPath | Should - BeTrue
@@ -78,22 +78,22 @@ Describe 'Save-SqlDscSqlServerMediaFile' -Tag @('Integration_SQL2017', 'Integrat
7878 # Create separate subdirectory for this test to avoid conflicts
7979 $overwriteTestPath = Join-Path - Path $script :testDownloadPath - ChildPath ' OverwriteTest'
8080 New-Item - Path $overwriteTestPath - ItemType Directory - Force | Out-Null
81-
82- # Use SkipExecution to avoid the safety check while still testing Force parameter
81+
82+ # Use SkipExecution to download only the executable without extracting ISO
8383 $executableUrl = ' https://download.microsoft.com/download/e/6/4/e6477a2a-9b58-40f7-8ad6-62bb8491ea78/SQLServerReportingServices.exe'
8484 $targetFileName = ' overwrite-test.exe'
85-
85+
8686 # First, create a dummy file with the target name
8787 $dummyFilePath = Join-Path - Path $overwriteTestPath - ChildPath $targetFileName
8888 ' dummy content for overwrite test' | Out-File - FilePath $dummyFilePath - Encoding UTF8
89-
89+
9090 # Verify the dummy file exists and get its original size
9191 Test-Path - Path $dummyFilePath | Should - BeTrue
9292 $originalSize = (Get-Item - Path $dummyFilePath ).Length
93-
93+
9494 # Download with SkipExecution and Force should overwrite the dummy file
9595 $result = Save-SqlDscSqlServerMediaFile - Url $executableUrl - DestinationPath $overwriteTestPath - FileName $targetFileName - SkipExecution - Force - Quiet - ErrorAction ' Stop'
96-
96+
9797 # Verify the file was overwritten (should be much larger than the dummy content)
9898 $result | Should - BeOfType [System.IO.FileInfo ]
9999 $result.Name | Should - Be $targetFileName
@@ -108,7 +108,7 @@ Describe 'Save-SqlDscSqlServerMediaFile' -Tag @('Integration_SQL2017', 'Integrat
108108 # Use SQL Server 2022 executable URL for testing executable download and extraction
109109 $script :executableUrl = ' https://download.microsoft.com/download/c/c/9/cc9c6797-383c-4b24-8920-dc057c1de9d3/SQL2022-SSEI-Dev.exe'
110110 $script :expectedIsoFileName = ' SQL2022-media.iso'
111-
111+
112112 # Create separate subdirectory for this context to avoid ISO file conflicts
113113 $script :executableTestPath = Join-Path - Path $script :testDownloadPath - ChildPath ' ExecutableTest'
114114 New-Item - Path $script :executableTestPath - ItemType Directory - Force | Out-Null
@@ -128,7 +128,7 @@ Describe 'Save-SqlDscSqlServerMediaFile' -Tag @('Integration_SQL2017', 'Integrat
128128 # Verify the executable was cleaned up (should not exist)
129129 $executablePath = [System.IO.Path ]::ChangeExtension($result.FullName , ' exe' )
130130 Test-Path - Path $executablePath | Should - BeFalse
131-
131+
132132 # Verify only one ISO file exists in the directory
133133 $isoFiles = Get-ChildItem - Path $script :executableTestPath - Filter ' *.iso'
134134 $isoFiles.Count | Should - Be 1
@@ -140,7 +140,7 @@ Describe 'Save-SqlDscSqlServerMediaFile' -Tag @('Integration_SQL2017', 'Integrat
140140 # Use SQL Server Reporting Services executable for testing SkipExecution
141141 $script :rsExecutableUrl = ' https://download.microsoft.com/download/e/6/4/e6477a2a-9b58-40f7-8ad6-62bb8491ea78/SQLServerReportingServices.exe'
142142 $script :expectedExecutableFileName = ' SSRS-Test.exe'
143-
143+
144144 # Create separate subdirectory for this context to avoid file conflicts
145145 $script :skipExecutionTestPath = Join-Path - Path $script :testDownloadPath - ChildPath ' SkipExecutionTest'
146146 New-Item - Path $script :skipExecutionTestPath - ItemType Directory - Force | Out-Null
@@ -168,19 +168,63 @@ Describe 'Save-SqlDscSqlServerMediaFile' -Tag @('Integration_SQL2017', 'Integrat
168168 $script :errorTestPath = Join-Path - Path $script :testDownloadPath - ChildPath ' ErrorTest'
169169 New-Item - Path $script :errorTestPath - ItemType Directory - Force | Out-Null
170170 }
171-
172- It ' Should throw error when ISO files already exist in destination and SkipExecution is not used' {
173- # Create a dummy ISO file to trigger the error condition
174- $dummyIsoPath = Join-Path - Path $script :errorTestPath - ChildPath ' existing.iso'
175- ' dummy iso content' | Out-File - FilePath $dummyIsoPath - Encoding UTF8
176171
177- # This should throw an error due to existing ISO file
178- {
179- Save-SqlDscSqlServerMediaFile - Url $script :directIsoUrl - DestinationPath $script :errorTestPath - FileName ' new-download.iso' - Quiet - ErrorAction ' Stop'
180- } | Should - Throw
172+ It ' Should download successfully even when multiple different ISO files already exist in destination' {
173+ # Create two different dummy ISO files in the destination directory
174+ $dummyIso1Path = Join-Path - Path $script :errorTestPath - ChildPath ' existing1.iso'
175+ $dummyIso2Path = Join-Path - Path $script :errorTestPath - ChildPath ' existing2.iso'
176+ ' dummy iso content 1' | Out-File - FilePath $dummyIso1Path - Encoding UTF8
177+ ' dummy iso content 2' | Out-File - FilePath $dummyIso2Path - Encoding UTF8
178+
179+ # Verify the dummy ISO files exist
180+ Test-Path - Path $dummyIso1Path | Should - BeTrue
181+ Test-Path - Path $dummyIso2Path | Should - BeTrue
182+
183+ # This should succeed - the command no longer blocks on non-target ISO files
184+ $targetFileName = ' new-download.iso'
185+ $result = Save-SqlDscSqlServerMediaFile - Url $script :directIsoUrl - DestinationPath $script :errorTestPath - FileName $targetFileName - Force - Quiet - ErrorAction ' Stop'
186+
187+ # Verify the download succeeded
188+ $result | Should - BeOfType [System.IO.FileInfo ]
189+ $result.Name | Should - Be $targetFileName
190+ $result.Exists | Should - BeTrue
191+
192+ # Verify the other ISO files still exist (they should not be affected)
193+ Test-Path - Path $dummyIso1Path | Should - BeTrue
194+ Test-Path - Path $dummyIso2Path | Should - BeTrue
195+
196+ # Clean up
197+ Remove-Item - Path $dummyIso1Path - Force - ErrorAction SilentlyContinue
198+ Remove-Item - Path $dummyIso2Path - Force - ErrorAction SilentlyContinue
199+ Remove-Item - Path (Join-Path - Path $script :errorTestPath - ChildPath $targetFileName ) - Force - ErrorAction SilentlyContinue
200+ }
201+
202+ It ' Should allow overwriting the same ISO file when Force parameter is used' {
203+ # Create a separate subdirectory for this test
204+ $forceOverwriteTestPath = Join-Path - Path $script :testDownloadPath - ChildPath ' ForceOverwriteIso'
205+ New-Item - Path $forceOverwriteTestPath - ItemType Directory - Force | Out-Null
206+
207+ # First, create a dummy ISO file with the exact same name we'll download
208+ $targetFileName = ' force-overwrite-test.iso'
209+ $dummyIsoPath = Join-Path - Path $forceOverwriteTestPath - ChildPath $targetFileName
210+ ' dummy iso content for force overwrite test' | Out-File - FilePath $dummyIsoPath - Encoding UTF8
211+
212+ # Verify the dummy file exists and get its original size
213+ Test-Path - Path $dummyIsoPath | Should - BeTrue
214+ $originalSize = (Get-Item - Path $dummyIsoPath ).Length
215+
216+ # Download with Force parameter should overwrite the existing ISO file
217+ $result = Save-SqlDscSqlServerMediaFile - Url $script :directIsoUrl - DestinationPath $forceOverwriteTestPath - FileName $targetFileName - Force - Quiet - ErrorAction ' Stop'
218+
219+ # Verify the file was overwritten (should be much larger than the dummy content)
220+ $result | Should - BeOfType [System.IO.FileInfo ]
221+ $result.Name | Should - Be $targetFileName
222+ $result.Exists | Should - BeTrue
223+ $result.Length | Should - BeGreaterThan $originalSize
224+ $result.Length | Should - BeGreaterThan 1000000 # Real ISO should be at least 1MB
181225
182226 # Clean up
183- Remove-Item - Path $dummyIsoPath - Force - ErrorAction SilentlyContinue
227+ Remove-Item - Path $forceOverwriteTestPath - Recurse - Force - ErrorAction SilentlyContinue
184228 }
185229
186230 It ' Should handle invalid URL gracefully' {
@@ -195,7 +239,7 @@ Describe 'Save-SqlDscSqlServerMediaFile' -Tag @('Integration_SQL2017', 'Integrat
195239 BeforeAll {
196240 # Use SQL Server 2019 executable for language testing
197241 $script :sql2019Url = ' https://download.microsoft.com/download/d/a/2/da259851-b941-459d-989c-54a18a5d44dd/SQL2019-SSEI-Dev.exe'
198-
242+
199243 # Create separate subdirectory for language testing to avoid conflicts
200244 $script :languageTestPath = Join-Path - Path $script :testDownloadPath - ChildPath ' LanguageTest'
201245 New-Item - Path $script :languageTestPath - ItemType Directory - Force | Out-Null
0 commit comments