Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions PSDepend/PSDependScripts/FileDownload.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Install: Install the dependency

.EXAMPLE
sqllite_dll = @{

Check warning on line 22 in PSDepend/PSDependScripts/FileDownload.ps1

View workflow job for this annotation

GitHub Actions / Continuous Integration / Run Linters

Unknown word (sqllite) Suggestions: (sqlite, SQLite, sallie, stylite, sulfite)
DependencyType = 'FileDownload'
Source = 'https://github.com/RamblingCookieMonster/PSSQLite/blob/main/PSSQLite/x64/System.Data.SQLite.dll?raw=true'
Target = 'C:\temp'
Expand All @@ -35,10 +35,10 @@

# Downloads System.Data.SQLite.dll to C:\temp\sqlite.dll
#>
[cmdletbinding()]

Check warning on line 38 in PSDepend/PSDependScripts/FileDownload.ps1

View workflow job for this annotation

GitHub Actions / Continuous Integration / Run Linters

Unknown word (cmdletbinding)
param(
[PSTypeName('PSDepend.Dependency')]
[psobject[]]

Check warning on line 41 in PSDepend/PSDependScripts/FileDownload.ps1

View workflow job for this annotation

GitHub Actions / Continuous Integration / Run Linters

Unknown word (psobject) Suggestions: (isobject, isObject, project, object, subject)
$Dependency,

[ValidateSet('Test', 'Install')]
Expand All @@ -46,7 +46,7 @@
)

function Parse-URLForFile {
[cmdletbinding()]

Check warning on line 49 in PSDepend/PSDependScripts/FileDownload.ps1

View workflow job for this annotation

GitHub Actions / Continuous Integration / Run Linters

Unknown word (cmdletbinding)
param($URL)
# This will need work. Assume leaf is file. If CGI exists in leaf, assume it is after the file
$FileName = $URL.split('/')[-1]
Expand Down Expand Up @@ -77,10 +77,13 @@
# Act on target path....
$ToInstall = $False # Anti pattern

# Normalize relative paths against $PWD so callers don't get burned by cwd-dependent splits
if (-not [IO.Path]::IsPathRooted($Target)) {
$Target = Join-Path $PWD $Target
}
# Capture trailing-separator intent before GetUnresolvedProviderPathFromPSPath normalizes it away;
# a trailing separator is an explicit signal that the target is a container, not a file.
$endsWithSeparator = $Target -and
$Target[-1] -in @([IO.Path]::DirectorySeparatorChar, [IO.Path]::AltDirectorySeparatorChar)

# Resolve PSDrive paths (e.g. TestDrive:) and relative paths to absolute filesystem paths
$Target = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Target)

$TargetParent = Split-Path $Target -Parent
$PathToAdd = $Target
Expand All @@ -94,24 +97,31 @@
}
$PathToAdd = Split-Path $Target -Parent
}
elseif ([IO.Path]::GetExtension($Target) -and -not (Test-Path $Target -PathType Container)) {
# Target has a file extension — treat as a full destination file path
elseif (
-not $endsWithSeparator -and
-not (Test-Path $Target -PathType Container) -and
([IO.Path]::GetExtension($Target) -or (Test-Path $TargetParent))
) {
# Treat as a full destination file path.
# Triggered when: has a file extension, OR parent already exists and caller gave no trailing separator.
# The trailing-separator check preserves extensionless binary targets (e.g. /usr/local/bin/terraform)

Check warning on line 107 in PSDepend/PSDependScripts/FileDownload.ps1

View workflow job for this annotation

GitHub Actions / Continuous Integration / Run Linters

Unknown word (extensionless) Suggestions: (extensions, extension's, extensiveness)
# while still allowing directory-like targets to be signalled with a trailing slash.
$PathToAdd = $TargetParent
if (-not (Test-Path $TargetParent)) {
Write-Error "Could not find parent path [$TargetParent] for target [$Target]"
if ($PSDependAction -contains 'Test') {
return $False
}
return
}
Comment thread
Copilot marked this conversation as resolved.
else {
$Path = $Target
$ToInstall = $True
Write-Verbose "Target has extension, treating as file path [$Target]"
}
$Path = $Target
$ToInstall = $True
Write-Verbose "Treating as destination file path [$Target]"
}
else {
# No extension (or already a container) — treat target as a directory
# No extension (or already a container, or explicit trailing separator) — treat target as a directory
Write-Verbose "[$Target] is a container, creating path to file"
if (-not (Test-Path $Target)) {
if (-not (Test-Path $Target) -and $PSDependAction -contains 'Install') {
New-Item -ItemType Directory -Path $Target -Force | Out-Null
}
If ($Name) {
Expand Down
9 changes: 6 additions & 3 deletions Tests/FileDownload.Type.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@
}

It 'Creates a new directory and downloads into it when Target has no extension and does not exist' {
$newDir = Join-Path (New-Item 'TestDrive:/dl5base' -ItemType Directory -Force).FullName 'newcontainer'
$dep = New-PSDependFixture -DependencyName 'https://example.com/sample.dll' -DependencyType 'FileDownload' -Target $newDir
$base = (New-Item 'TestDrive:/dl5base' -ItemType Directory -Force).FullName
$newDir = Join-Path $base 'newcontainer'

Check warning on line 77 in Tests/FileDownload.Type.Tests.ps1

View workflow job for this annotation

GitHub Actions / Continuous Integration / Run Linters

Unknown word (newcontainer) Suggestions: (devcontainer)
# Trailing separator signals "this is a container, not an extensionless file"

Check warning on line 78 in Tests/FileDownload.Type.Tests.ps1

View workflow job for this annotation

GitHub Actions / Continuous Integration / Run Linters

Unknown word (extensionless) Suggestions: (extensions, extension's, extensiveness)
$dep = New-PSDependFixture -DependencyName 'https://example.com/sample.dll' -DependencyType 'FileDownload' -Target "$newDir/"
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath; T = $newDir } {
& $ScriptPath -Dependency $Dep
}
Should -Invoke -CommandName Get-WebFile -ModuleName PSDepend -Times 1 -Exactly -ParameterFilter {
$URL -eq 'https://example.com/sample.dll' -and ($Path -like "*newcontainer*sample.dll")

Check warning on line 84 in Tests/FileDownload.Type.Tests.ps1

View workflow job for this annotation

GitHub Actions / Continuous Integration / Run Linters

Unknown word (newcontainer) Suggestions: (devcontainer)
}
Test-Path $newDir -PathType Container | Should -Be $true
}
Expand All @@ -97,10 +99,11 @@
}

It 'Roots a relative Target against $PWD and downloads to it' {
$baseDir = (New-Item 'TestDrive:/relbase' -ItemType Directory -Force).FullName

Check warning on line 102 in Tests/FileDownload.Type.Tests.ps1

View workflow job for this annotation

GitHub Actions / Continuous Integration / Run Linters

Unknown word (relbase) Suggestions: (rebase, release, rebate, relate, rebased)
Push-Location $baseDir
try {
$dep = New-PSDependFixture -DependencyName 'https://example.com/sample.dll' -DependencyType 'FileDownload' -Target 'subdir'
# Trailing separator signals "this is a container, not an extensionless file"

Check warning on line 105 in Tests/FileDownload.Type.Tests.ps1

View workflow job for this annotation

GitHub Actions / Continuous Integration / Run Linters

Unknown word (extensionless) Suggestions: (extensions, extension's, extensiveness)
$dep = New-PSDependFixture -DependencyName 'https://example.com/sample.dll' -DependencyType 'FileDownload' -Target 'subdir/'
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep
}
Expand Down
Loading