Skip to content

Commit b077f24

Browse files
HeyItsGilbertrcarbonerasclaude
authored
fix(Git): use full Dependency.Target path when target doesn't exist (#169)
Supersedes #99 — that PR has been open since 2019 with no merge activity, so filing a fresh one with the fixes applied. ## Problem The `-Force` branch in `Git.ps1` used `Split-Path -Leaf` + `Join-Path $PWD` to construct the target path. This breaks in two ways: 1. Absolute paths or multi-level targets (e.g. `C:\Temp\Repos\myrepo`, `TestDrive:/PSDependPesterTest\buildhelpers`) are clobbered — only the leaf is used and it lands in `$PWD` instead of the intended location. 2. When `-Force` is used without a `Dependency.Target`, `Split-Path` throws because the input is empty. ## Fix In the unresolvable-target `else` block, simply set `$Target = $Dependency.Target` when it is non-empty. The existing `New-Item` call at line 109 already handles directory creation for the `Install` action — no duplication needed. When `Dependency.Target` is empty the previous default-to-`$PWD` behavior is preserved. ## Changes - `PSDepend/PSDependScripts/Git.ps1` — replaced the broken `-Force`/`Split-Path` branch with a clean `$Dependency.Target` assignment - `Tests/PSModuleGallery.Type.Tests.ps1` — added a Pester regression context that asserts `New-Item` is called with the full target path, not just the leaf joined to `$PWD` --------- Co-authored-by: Raúl Carboneras <rcarboneras@outlook.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent d792adb commit b077f24

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

PSDepend/PSDependScripts/Git.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,14 @@ if($Dependency.Target -and ($Target = (Get-Item $Dependency.Target -ErrorAction
9393
}
9494
else
9595
{
96-
$Target = $PWD.Path
97-
Write-Debug "Target defaulted to current dir: $Target"
96+
if ($Dependency.Target) {
97+
$Target = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Dependency.Target)
98+
Write-Debug "Target $($Dependency.Target) does not exist yet, will be created"
99+
}
100+
else {
101+
$Target = $PWD.Path
102+
Write-Debug "Target defaulted to current dir: $Target"
103+
}
98104
}
99105
$RepoPath = Join-Path $Target $GitName
100106
$GottaInstall = $True

Tests/PSModuleGallery.Type.Tests.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,24 @@ Describe "PSModuleGallery Type" -Tag 'Integration' {
512512
}
513513
}
514514

515+
Context 'Creates non-existent Target directory using full path' {
516+
BeforeAll {
517+
Mock Invoke-ExternalCommand {} -ModuleName PSDepend -ParameterFilter { $Arguments -contains 'checkout' -or $Arguments -contains 'clone' }
518+
Mock Push-Location {} -ModuleName PSDepend
519+
Mock Pop-Location {} -ModuleName PSDepend
520+
Mock Set-Location {} -ModuleName PSDepend
521+
Mock Test-Path { return $False } -ModuleName PSDepend -ParameterFilter { $Path -match 'buildhelpers' }
522+
Mock New-Item { [pscustomobject]@{ FullName = $Path } } -ModuleName PSDepend
523+
$null = Invoke-PSDepend @Verbose -Path "$TestDepends\git.depend.psd1" -Force
524+
}
525+
526+
It 'Calls New-Item with a path containing the full target name, not just the leaf joined to $PWD' {
527+
Should -Invoke New-Item -Times 1 -Exactly -Scope Context -ModuleName PSDepend -ParameterFilter {
528+
$Path -match 'buildhelpers' -and $Path -notmatch [regex]::Escape($PWD.Path)
529+
}
530+
}
531+
}
532+
515533
Context 'Tests dependency' {
516534
BeforeAll {
517535
Mock New-Item { return $true } -ModuleName PSDepend

0 commit comments

Comments
 (0)