@@ -32,11 +32,10 @@ Describe $CommandName -Tag IntegrationTests {
3232 $PSDefaultParameterValues [" *-Dba*:EnableException" ] = $true
3333
3434 $dbname = " dbatoolsci_orphanedfile_$ ( Get-Random ) "
35- $server = Connect-DbaInstance - SqlInstance $TestConfig.instance2
36- $db1 = New-DbaDatabase - SqlInstance $server - Name $dbname
37-
3835 $dbname2 = " dbatoolsci_orphanedfile_$ ( Get-Random ) "
39- $db2 = New-DbaDatabase - SqlInstance $server - Name $dbname2
36+
37+ $db1 = New-DbaDatabase - SqlInstance $TestConfig.instance2 - Name $dbname
38+ $db2 = New-DbaDatabase - SqlInstance $TestConfig.instance2 - Name $dbname2
4039
4140 $tmpdir = " $ ( $TestConfig.Temp ) \orphan_$ ( Get-Random ) "
4241 if (-not (Test-Path $tmpdir )) {
@@ -63,25 +62,26 @@ Describe $CommandName -Tag IntegrationTests {
6362
6463 $backupFile = Backup-DbaDatabase - SqlInstance $TestConfig.instance2 - Database $dbname - Path $tmpBackupPath - Type Full
6564 $backupFile2 = Backup-DbaDatabase - SqlInstance $TestConfig.instance2 - Database $dbname2 - Path $tmpBackupPath2 - Type Full
66- Copy-Item - Path $backupFile.BackupPath - Destination " C:\"
6765
68- $tmpBackupPath3 = Join-Path (Get-SqlDefaultPaths $server data ) " dbatoolsci_$ ( Get-Random ) "
69- $ null = New-Item - Path $tmpBackupPath3 - ItemType Directory
66+ $tmpBackupPath3 = Join-Path (Get-DbaDefaultPath - SqlInstance $TestConfig .instance2 ).Data " dbatoolsci_$ ( Get-Random ) "
67+ Invoke-Command2 - ComputerName $TestConfig .instance2 - ScriptBlock { $ null = New-Item - Path $args - ItemType Directory } - ArgumentList $tmpBackupPath3
7068
7169 # We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
7270 $PSDefaultParameterValues.Remove (" *-Dba*:EnableException" )
7371 }
72+
7473 AfterAll {
7574 # We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
7675 $PSDefaultParameterValues [" *-Dba*:EnableException" ] = $true
76+
7777 Get-DbaDatabase - SqlInstance $TestConfig.instance2 - Database $dbname , $dbname2 | Remove-DbaDatabase
78- Remove-Item $tmpdir - Recurse - Force - ErrorAction SilentlyContinue
79- Remove-Item $tmpdir2 - Recurse - Force - ErrorAction SilentlyContinue
80- Remove-Item " C:\$ ( $backupFile.BackupFile ) " - Force - ErrorAction SilentlyContinue
81- Remove-Item $tmpBackupPath3 - Recurse - Force - ErrorAction SilentlyContinue
78+ Remove-Item $tmpdir - Recurse - Force
79+ Remove-Item $tmpdir2 - Recurse - Force
80+ Invoke-Command2 - ComputerName $TestConfig.instance2 - ScriptBlock { Remove-Item - Path $args - Recurse - Force } - ArgumentList $tmpBackupPath3
8281
8382 $PSDefaultParameterValues.Remove (" *-Dba*:EnableException" )
8483 }
84+
8585 It " Has the correct properties" {
8686 $null = Dismount-DbaDatabase - SqlInstance $TestConfig.instance2 - Database $dbname - Force
8787 $results = Find-DbaOrphanedFile - SqlInstance $TestConfig.instance2
@@ -91,43 +91,40 @@ Describe $CommandName -Tag IntegrationTests {
9191 ($results [0 ].PsObject.Properties.Name | Sort-Object ) | Should - Be ($ExpectedProps | Sort-Object )
9292 }
9393
94-
9594 It " Finds two files" {
9695 $results = Find-DbaOrphanedFile - SqlInstance $TestConfig.instance2
9796 $results.Filename.Count | Should - Be 2
9897 }
9998
10099 It " Finds zero files after cleaning up" {
101100 $results = Find-DbaOrphanedFile - SqlInstance $TestConfig.instance2
102- $results .FileName | Remove-Item
101+ Invoke-Command2 - ComputerName $TestConfig .instance2 - ScriptBlock { Remove-Item - Path $args } - ArgumentList $results .FileName
103102 $results = Find-DbaOrphanedFile - SqlInstance $TestConfig.instance2
104103 $results.Filename.Count | Should - Be 0
105104 }
106- It " works with -Recurse" {
105+
106+ It " works with -Path" {
107107 " a" | Out-File (Join-Path $tmpdir " out.mdf" )
108108 $results = Find-DbaOrphanedFile - SqlInstance $TestConfig.instance2 - Path $tmpdir
109109 $results.Filename.Count | Should - Be 1
110+
110111 Move-Item " $tmpdir \out.mdf" - Destination $tmpdirInner
111112 $results = Find-DbaOrphanedFile - SqlInstance $TestConfig.instance2 - Path $tmpdir
112113 $results.Filename.Count | Should - Be 0
114+ }
115+
116+ It " works with -Recurse" {
113117 $results = Find-DbaOrphanedFile - SqlInstance $TestConfig.instance2 - Path $tmpdir - Recurse
114118 $results.Filename.Count | Should - Be 1
115119
116- Copy-Item - Path " $tmpdirInner \out.mdf" - Destination $tmpBackupPath3
117-
118120 $results = Find-DbaOrphanedFile - SqlInstance $TestConfig.instance2 - Path $tmpdir , $tmpdir2 - Recurse - FileType bak
119121 $results.Filename | Should - Contain $backupFile.BackupPath
120122 $results.Filename | Should - Contain $backupFile2.BackupPath
121- $results.Filename | Should - Contain " $tmpdirInner \out.mdf"
122- $results.Filename | Should - Contain " $tmpBackupPath3 \out.mdf"
123- $results.Count | Should - Be 4
123+ $results.Count | Should - Be 3
124124
125+ Invoke-Command2 - ComputerName $TestConfig.instance2 - ScriptBlock { " a" | Out-File (Join-Path $args " out.mdf" ) } - ArgumentList $tmpBackupPath3
125126 $results = Find-DbaOrphanedFile - SqlInstance $TestConfig.instance2 - Recurse
126127 $results.Filename | Should - Be " $tmpBackupPath3 \out.mdf"
127128 }
128- It " works with -Path" {
129- $results = Find-DbaOrphanedFile - SqlInstance $TestConfig.instance2 - Path " C:" - FileType bak
130- $results.Filename | Should - Contain " C:\$ ( $backupFile.BackupFile ) "
131- }
132129 }
133130}
0 commit comments