-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix for #2442 - automounted drives in modules #3034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cea5b7a
Fix for #2442 - automounted drives in modules
d7315e2
Fix for #2442 - removed BVT tag from tests as required by AppVeyor
6495420
Addressed CR feedback
c1641d0
fix formatting and replace tabs w/ spaces
lzybkr 6141591
Fix formatting and replace tabs with spaces
lzybkr c9b8e29
Fixed tabs in tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Precondition: start from fresh PS session, do not have the media mounted | ||
| param([switch]$useModule, [string]$FullPath) | ||
|
|
||
| $global:CoreScriptPath = Join-Path $PSScriptRoot 'AutomountSubstDriveCore.ps1' | ||
|
|
||
| if ($useModule) | ||
| { | ||
| $m = New-Module { | ||
| function Test-DrivePresenceFromModule | ||
| { | ||
| param ([String]$Path) | ||
|
|
||
| & $global:CoreScriptPath -Path $Path | ||
| } | ||
|
|
||
| Export-ModuleMember -Function Test-DrivePresenceFromModule | ||
| } | ||
| } | ||
|
|
||
| try | ||
| { | ||
| if ($useModule) | ||
| { | ||
| Import-Module $m -Force | ||
| Test-DrivePresenceFromModule -Path $FullPath | ||
| } | ||
| else | ||
| { | ||
| & $global:CoreScriptPath -Path $FullPath | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| if ($useModule) | ||
| { | ||
| Remove-Module $m | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| param ([String]$Path) | ||
|
|
||
| try | ||
| { | ||
| # Get a drive letter between F and Y that is not being used for the drive name. | ||
| $driveLetter = [char[]](70..89) | Where-Object {$_ -notin (Get-PSDrive).Name} | Select-Object -Last 1 | ||
|
|
||
| $dir = New-Item $Path -ItemType Directory -Force | ||
|
|
||
| # Create virtual drive pointing to the parent of the directory | ||
| subst.exe "$driveLetter`:" $dir.Parent.FullName | ||
| $exitCode = $LASTEXITCODE | ||
| if ($exitCode -ne 0) { Write-Error "Creating drive with subst.exe failed with exit code $exitCode" } | ||
|
|
||
| $root = [String]::Format('{0}:\', $driveLetter) | ||
| $pathToCheck = Join-Path -Path $root -ChildPath $dir.Name | ||
|
|
||
| if (Test-Path $pathToCheck) | ||
| { | ||
| "Drive found" | ||
| if (-not (Get-PSDrive -Name $driveLetter -Scope Global -ErrorAction SilentlyContinue)) | ||
| { | ||
| Write-Error "Drive is NOT in Global scope" | ||
| } | ||
| } | ||
| else { Write-Error "$pathToCheck not found" } | ||
| } | ||
| finally | ||
| { | ||
| subst.exe "$driveLetter`:" /d | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Precondition: start from fresh PS session, do not have the media mounted | ||
| param([switch]$useModule, [string]$VHDPath) | ||
|
|
||
| function CreateVHD ($VHDPath, $Size) | ||
| { | ||
| $drive = (New-VHD -path $vhdpath -SizeBytes $size -Dynamic | ` | ||
| Mount-VHD -Passthru | ` | ||
| get-disk -number {$_.DiskNumber} | ` | ||
| Initialize-Disk -PartitionStyle MBR -PassThru | ` | ||
| New-Partition -UseMaximumSize -AssignDriveLetter:$False -MbrType IFS | ` | ||
| Format-Volume -Confirm:$false -FileSystem NTFS -force | ` | ||
| get-partition | ` | ||
| Add-PartitionAccessPath -AssignDriveLetter -PassThru | ` | ||
| get-volume).DriveLetter | ||
|
|
||
| $drive | ||
| } | ||
|
|
||
| if ($useModule) | ||
| { | ||
| $m = New-Module { | ||
| function Test-DrivePresenceFromModule | ||
| { | ||
| param ([String]$Path) | ||
|
|
||
| if (Test-Path $Path) | ||
| { | ||
| "Drive found" | ||
| if (-not (Get-PSDrive -Name $Path[0] -Scope Global -ErrorAction SilentlyContinue)) | ||
| { | ||
| Write-Error "Drive is NOT in Global scope" | ||
| } | ||
| } | ||
| else { Write-Error "$Path not found" } | ||
| } | ||
|
|
||
| Export-ModuleMember -Function Test-DrivePresenceFromModule | ||
| } | ||
| } | ||
|
|
||
| try | ||
| { | ||
| if ($useModule) | ||
| { | ||
| Import-Module $m -Force | ||
| } | ||
|
|
||
| $drive = CreateVHD -VHDPath $VHDPath -Size 5mb | ||
| $pathToCheck = "${drive}:" | ||
|
|
||
| if ($useModule) | ||
| { | ||
| Test-DrivePresenceFromModule -Path $pathToCheck | ||
| } | ||
| else | ||
| { | ||
| if (Test-Path $pathToCheck) | ||
| { | ||
| "Drive found" | ||
| if (-not (Get-PSDrive -Name $drive -Scope Global -ErrorAction SilentlyContinue)) | ||
| { | ||
| Write-Error "Drive is NOT in Global scope" | ||
| } | ||
| } | ||
| else { Write-Error "$pathToCheck not found" } | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| if ($useModule) | ||
| { | ||
| Remove-Module $m | ||
| } | ||
| Dismount-VHD $VHDPath | ||
| Remove-Item $VHDPath | ||
| } |
55 changes: 55 additions & 0 deletions
55
test/powershell/Provider/Pester.AutomountedDrives.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <############################################################################################ | ||
| # File: Pester.AutomountedDrives.Tests.ps1 | ||
| # Pester.AutomountedDrives.Tests suite contains Tests that are | ||
| # used for validating automounted PowerShell drives. | ||
| ############################################################################################> | ||
| $script:TestSourceRoot = $PSScriptRoot | ||
| Describe "Test suite for validating automounted PowerShell drives" -Tags "CI","Slow" { | ||
|
|
||
| BeforeAll { | ||
| $powershell = Join-Path -Path $PsHome -ChildPath "powershell" | ||
|
|
||
| $AutomountVHDDriveScriptPath = Join-Path $script:TestSourceRoot 'AutomountVHDDrive.ps1' | ||
| $vhdPath = Join-Path $TestDrive 'TestAutomountVHD.vhd' | ||
|
|
||
| $AutomountSubstDriveScriptPath = Join-Path $script:TestSourceRoot 'AutomountSubstDrive.ps1' | ||
| $substDir = Join-Path (Join-Path $TestDrive 'TestAutomountSubstDrive') 'TestDriveRoot' | ||
| New-Item $substDir -ItemType Directory -Force | Out-Null | ||
|
|
||
| $SubstNotFound = $false | ||
| try { subst.exe } catch { $SubstNotFound = $true } | ||
|
|
||
| $VHDToolsNotFound = $false | ||
| try | ||
| { | ||
| $tmpVhdPath = Join-Path $TestDrive 'TestVHD.vhd' | ||
| New-VHD -path $tmpVhdPath -SizeBytes 5mb -Dynamic -ErrorAction Stop | ||
| Remove-Item $tmpVhdPath | ||
| } | ||
| catch | ||
| { $VHDToolsNotFound = $true } | ||
| } | ||
|
|
||
| Context "Validating automounting FileSystem drives" { | ||
|
|
||
| It "Test automounting using subst.exe" -Skip:$SubstNotFound { | ||
| & $powershell -noprofile -command "& '$AutomountSubstDriveScriptPath' -FullPath '$substDir'" | Should Be "Drive found" | ||
| } | ||
|
|
||
| It "Test automounting using New-VHD/Mount-VHD" -Skip:$VHDToolsNotFound { | ||
| & $powershell -noprofile -command "& '$AutomountVHDDriveScriptPath' -VHDPath '$vhdPath'" | Should Be "Drive found" | ||
| } | ||
| } | ||
|
|
||
| Context "Validating automounting FileSystem drives from modules" { | ||
|
|
||
| It "Test automounting using subst.exe" -Skip:$SubstNotFound { | ||
| & $powershell -noprofile -command "& '$AutomountSubstDriveScriptPath' -useModule -FullPath '$substDir'" | Should Be "Drive found" | ||
| } | ||
|
|
||
| It "Test automounting using New-VHD/Mount-VHD" -Skip:$VHDToolsNotFound { | ||
| $vhdPath = Join-Path $TestDrive 'TestAutomountVHD.vhd' | ||
| & $powershell -noprofile -command "& '$AutomountVHDDriveScriptPath' -useModule -VHDPath '$vhdPath'" | Should Be "Drive found" | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old code only created file system drives if the current scope was global.
Will your change create the drive in global scope or module scope?
Personally, I would expect the drive to be in global scope.
I would also like to see tests to cover that scenario either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After this change, new drives will be created in global scope; I've added a check for this to tests.