diff --git a/Private/Get-InstallPath.ps1 b/Private/Get-InstallPath.ps1 index 386a21f..45bf318 100644 --- a/Private/Get-InstallPath.ps1 +++ b/Private/Get-InstallPath.ps1 @@ -1,7 +1,8 @@ function Get-InstallPath { # returns OS specific path for module installation, it support only -Scope CurrentUser - if ($IsLinux -or $IsOSX) { + + $defaultPath = if ($IsLinux -or $IsOSX) { #"$HOME/.local/share/powershell/Modules" # https://github.com/PowerShell/PowerShellGet/blob/d4dfebbbec4dfbe73392719a8a331541ed75d508/src/PowerShellGet/private/modulefile/PartOne.ps1#L71 Join-Path (Split-Path -Path ([System.Management.Automation.Platform]::SelectProductNameForDirectory('USER_MODULES')) -Parent) 'Modules' @@ -18,4 +19,22 @@ function Get-InstallPath { } } + $ModulePaths = $Env:PSModulePath -split (';:'[[int]($IsLinux -or $IsMacOS)]) + if ($defaultPath -in $ModulePaths) { + $defaultPath + } else { + # default path is not in findable by get-module, try to avoid it + $writablePath = '' + foreach ($P1 in $ModulePaths) { + if (([string]::IsNullOrEmpty($writablePath)) -and (Test-PathWritable $P1)) { + $writablePath = $P1 + } + } + if ([string]::IsNullOrEmpty($writablePath)) { + # we found no writable paths, return default one + $defaultPath + } else { + $writablePath + } + } } \ No newline at end of file diff --git a/Private/Test-PathWritable.ps1 b/Private/Test-PathWritable.ps1 new file mode 100644 index 0000000..20beaac --- /dev/null +++ b/Private/Test-PathWritable.ps1 @@ -0,0 +1,21 @@ +function Test-PathWritable { + param ( + [string]$Path + ) + # returns true if given directory is writable, false otherwise + if (!(Test-Path $Path -PathType Container)) { + #throw "Path $Path is not a directory" + $false + } + + $FileName = Join-Path $Path ([io.path]::GetRandomFileName()) + + try { + [io.file]::OpenWrite($FileName).close() + [io.file]::Delete($FileName) + $true + } catch { + $false + } + +} \ No newline at end of file diff --git a/Tests/module/InstallModuleFromGit.Tests.ps1 b/Tests/module/InstallModuleFromGit.Tests.ps1 index 510c976..50a5752 100644 --- a/Tests/module/InstallModuleFromGit.Tests.ps1 +++ b/Tests/module/InstallModuleFromGit.Tests.ps1 @@ -11,7 +11,7 @@ $ModuleName = 'InstallModuleFromGit' $here = Split-Path -Parent $MyInvocation.MyCommand.Path # Tests/Module folder $root = (get-item $here).Parent.Parent.FullName # module root folder -Import-Module (Join-Path $root "$ModuleName.psm1") -Force +Import-Module (Join-Path $root "$ModuleName.psd1") -Force # @@ -67,6 +67,8 @@ Describe 'Proper Functions Declaration' -Tag 'Other' { Describe 'Proper Documentation' -Tag 'Documentation' { + Push-Location $root + It 'Updates documentation and does git diff' { # install PlatyPS @@ -92,6 +94,7 @@ Describe 'Proper Documentation' -Tag 'Documentation' { } } + Pop-Location }