Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit e454bd6

Browse files
edyoungalerickson
authored andcommitted
filter out hidden files from nupkg (#396)
1 parent 0d77cf1 commit e454bd6

File tree

2 files changed

+66
-22
lines changed

2 files changed

+66
-22
lines changed

Tests/PSGetPublishModule.Tests.ps1

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ Describe PowerShell.PSGet.PublishModuleTests -Tags 'BVT','InnerLoop' {
121121
#Copy module to $script:ProgramFilesModulesPath
122122
Copy-Item $script:PublishModuleBase $script:ProgramFilesModulesPath -Recurse -Force
123123

124-
Publish-Module -Name $script:PublishModuleName -ReleaseNotes "$script:PublishModuleName release notes" -Tags PSGet -LicenseUri "https://$script:PublishModuleName.com/license" -ProjectUri "https://$script:PublishModuleName.com" -WarningAction SilentlyContinue
124+
Publish-Module -Name $script:PublishModuleName -ReleaseNotes "$script:PublishModuleName release notes" -Tags PSGet -LicenseUri "https://$script:PublishModuleName.com/license" -ProjectUri "https://$script:PublishModuleName.com" -WarningAction SilentlyContinue
125125

126126
$psgetItemInfo = Find-Module $script:PublishModuleName -RequiredVersion $version
127127
# This version can be 1.0 or 1.0.0, depending on the version of NuGet.exe or dotnet command.
@@ -201,6 +201,39 @@ Describe PowerShell.PSGet.PublishModuleTests -Tags 'BVT','InnerLoop' {
201201
Assert ($psgetItemInfo.Name -eq $script:PublishModuleName) "Publish-Module should publish a module with valid module path, $($psgetItemInfo.Name)"
202202
}
203203

204+
# Purpose: Check that files with hidden attributes (like .git dirs) are not published
205+
#
206+
# Action: Publish-Module -Path <ModulePath> -NuGetApiKey <ApiKey>
207+
#
208+
# Expected Result: published module should not contain these files
209+
#
210+
It "PublishModuleLeavesOutHiddenFiles" {
211+
212+
# create module dir containing a hidden dir with a normal file inside, and a normal file
213+
$version = "1.0"
214+
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -NestedModules "$script:PublishModuleName.psm1"
215+
New-Item -Type Directory -Path $script:PublishModuleBase -Name ".git"
216+
New-Item -Type File -Path $script:PublishModuleBase\.git\dummy
217+
New-Item -Type File -Path $script:PublishModuleBase -Name "normal"
218+
$hiddenDir = Get-Item $script:PublishModuleBase\.git -force
219+
$hiddenDir.Attributes = "hidden"
220+
221+
# publish it, then save the published one and inspect it
222+
Publish-Module -Path $script:PublishModuleBase -NuGetApiKey $script:ApiKey -ReleaseNotes "$script:PublishModuleName release notes" -Tags PSGet -LicenseUri "https://$script:PublishModuleName.com/license" -ProjectUri "https://$script:PublishModuleName.com" -WarningAction SilentlyContinue
223+
New-Item -type directory -path $script:PublishModuleBase -Name saved
224+
Save-Module $script:PublishModuleName -RequiredVersion $version -Path $script:PublishModuleBase\saved
225+
226+
# it should contain the normal file, but not the hidden dir
227+
$contents = (dir -rec -force $script:PublishModuleBase | Out-String)
228+
$basedir = "$script:PublishModuleBase\saved\$script:PublishModuleName"
229+
if($PSVersionTable.PSVersion -gt '5.0.0') {
230+
$basedir = join-path $basedir "1.0.0"
231+
}
232+
233+
Assert ((Test-Path $basedir\.git) -eq $false) ".git dir shouldn't be included ($contents)"
234+
Assert ((Test-Path $basedir\normal) -eq $true) "normal file should be included ($contents)"
235+
}
236+
204237
# Purpose: Publish a module with -Path
205238
#
206239
# Action: Publish-Module -Path <ModulePath> -NuGetApiKey <ApiKey>
@@ -273,11 +306,11 @@ Describe PowerShell.PSGet.PublishModuleTests -Tags 'BVT','InnerLoop' {
273306
$moduleBaseWithoutVersion = $script:PublishModuleBase
274307

275308
$moduleBase = Join-Path -Path $script:PublishModuleBase -ChildPath $version1
276-
$null = New-Item -ItemType Directory -Path $moduleBase -Force
309+
$null = New-Item -ItemType Directory -Path $moduleBase -Force
277310
New-ModuleManifest -Path (Join-Path -Path $moduleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version1 -Description "$script:PublishModuleName module"
278311

279312
$moduleBase = Join-Path -Path $script:PublishModuleBase -ChildPath $version2
280-
$null = New-Item -ItemType Directory -Path $moduleBase -Force
313+
$null = New-Item -ItemType Directory -Path $moduleBase -Force
281314
New-ModuleManifest -Path (Join-Path -Path $moduleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version2 -Description "$script:PublishModuleName module"
282315

283316
AssertFullyQualifiedErrorIdEquals -scriptblock {Publish-Module -Path $moduleBaseWithoutVersion -WarningAction SilentlyContinue}`
@@ -923,7 +956,7 @@ Describe PowerShell.PSGet.PublishModuleTests -Tags 'BVT','InnerLoop' {
923956
$ProjectUri = 'https://contoso.com/'
924957
$IconUri = 'https://contoso.com/icon'
925958
$ReleaseNotes = 'Test module for external module dependecies'
926-
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
959+
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
927960
#Copy module to $script:ProgramFilesModulesPath
928961
Copy-Item $script:PublishModuleBase $script:ProgramFilesModulesPath -Recurse -Force
929962

@@ -941,7 +974,7 @@ Describe PowerShell.PSGet.PublishModuleTests -Tags 'BVT','InnerLoop' {
941974

942975
# Purpose: Test Publish-Module cmdlet gives warnings if Cmdlets/Functions/DscResourcesToExport has "*" in manifest
943976
#
944-
# Action: Publish-Module
977+
# Action: Publish-Module
945978
#
946979
# Expected Result: Publish operation should succeed but throw warnings
947980
#
@@ -959,17 +992,17 @@ Describe PowerShell.PSGet.PublishModuleTests -Tags 'BVT','InnerLoop' {
959992
using System;
960993
using System.Management.Automation;
961994
namespace PSGetTestModule
962-
{
995+
{
963996
[Cmdlet("Test","PSGetTestCmdlet")]
964997
public class PSGetTestCmdlet : PSCmdlet
965-
{
998+
{
966999
[Parameter]
9671000
public int a {
9681001
get;
9691002
set;
970-
}
1003+
}
9711004
protected override void ProcessRecord()
972-
{
1005+
{
9731006
String s = "Value is :" + a;
9741007
WriteObject(s);
9751008
}
@@ -1205,7 +1238,7 @@ Describe PowerShell.PSGet.PublishModuleTests -Tags 'BVT','InnerLoop' {
12051238

12061239
# Purpose: Validate that Publish-Module prompts to upgrade NuGet.exe if local NuGet.exe file is less than minimum required version
12071240
#
1208-
# Action: Publish-Module
1241+
# Action: Publish-Module
12091242
#
12101243
# Expected Result: Publish operation should fail, NuGet.exe should not upgrade to latest version
12111244
#
@@ -1372,7 +1405,7 @@ Describe PowerShell.PSGet.PublishModuleTests.P1 -Tags 'P1','OuterLoop' {
13721405
Publish-Module -Path $script:PublishModuleBase -NuGetApiKey $script:ApiKey -Force -WarningAction SilentlyContinue
13731406

13741407
$psgetItemInfo = Find-Module $script:PublishModuleName -RequiredVersion $version
1375-
Assert ($psgetItemInfo.Name -eq $script:PublishModuleName) "Publish-Module should publish a module with lower version, $($psgetItemInfo.Name)"
1408+
Assert ($psgetItemInfo.Name -eq $script:PublishModuleName) "Publish-Module should publish a module with lower version, $($psgetItemInfo.Name)"
13761409
}
13771410

13781411
It "PublishModuleWithoutForceAndLowerVersion" {
@@ -1553,7 +1586,7 @@ Describe PowerShell.PSGet.PublishModuleTests.P1 -Tags 'P1','OuterLoop' {
15531586
#
15541587
It "PublishInvalidModule" {
15551588
$tempmodulebase = Join-Path (Join-Path $script:TempPath "$(Get-Random)") "InvalidModule"
1556-
$null = New-Item $tempmodulebase -Force -ItemType Directory
1589+
$null = New-Item $tempmodulebase -Force -ItemType Directory
15571590

15581591
try
15591592
{
@@ -1779,7 +1812,7 @@ Describe PowerShell.PSGet.PublishModuleTests.P1 -Tags 'P1','OuterLoop' {
17791812
$null = New-Item -Path $DepModuleBase -ItemType Directory -Force
17801813
New-ModuleManifest -Path (Join-Path $DepModuleBase "$_.psd1") `
17811814
-ModuleVersion '1.0' `
1782-
-Description "$_ module"
1815+
-Description "$_ module"
17831816
}
17841817

17851818

@@ -1809,7 +1842,7 @@ Describe PowerShell.PSGet.PublishModuleTests.P1 -Tags 'P1','OuterLoop' {
18091842
}
18101843
}
18111844
}
1812-
"@
1845+
"@
18131846

18141847
($psd1Text -replace '__VERSION__',$version) | Out-File -FilePath (Join-Path -Path $ModuleBase -ChildPath "$ModuleName.psd1") -Force
18151848

@@ -1870,20 +1903,20 @@ Describe PowerShell.PSGet.PublishModuleTests.P2 -Tags 'P2','OuterLoop' {
18701903
$RequiredModules1 = @('RequiredModule1',
18711904
@{ModuleName = 'RequiredModule2'; ModuleVersion = '1.5'; })
18721905

1873-
$RequiredModules2 = @('RequiredModule1',
1906+
$RequiredModules2 = @('RequiredModule1',
18741907
@{ModuleName = 'RequiredModule2'; ModuleVersion = '2.0'; })
18751908

1876-
$NestedRequiredModules1 = @('NestedRequiredModule1',
1909+
$NestedRequiredModules1 = @('NestedRequiredModule1',
18771910
@{ModuleName = 'NestedRequiredModule2'; ModuleVersion = '1.5'; })
18781911

1879-
$NestedRequiredModules2 = @('NestedRequiredModule1',
1912+
$NestedRequiredModules2 = @('NestedRequiredModule1',
18801913
@{ModuleName = 'NestedRequiredModule2'; ModuleVersion = '2.0'; })
18811914

18821915
if($PSVersionTable.PSVersion -ge '5.0.0')
18831916
{
18841917
$DepencyModuleNames += @("RequiredModule3",
18851918
"RequiredModule4",
1886-
"RequiredModule5"<#,
1919+
"RequiredModule5"<#,
18871920
"NestedRequiredModule3",
18881921
"NestedRequiredModule4",
18891922
"NestedRequiredModule5"#>
@@ -1991,7 +2024,7 @@ Describe PowerShell.PSGet.PublishModuleTests.P2 -Tags 'P2','OuterLoop' {
19912024
$null = New-Item -Path $DepModuleBase -ItemType Directory -Force
19922025
New-ModuleManifest -Path (Join-Path $DepModuleBase "$_.psd1") `
19932026
-ModuleVersion '1.0.0' `
1994-
-Description "$_ module"
2027+
-Description "$_ module"
19952028
}
19962029

19972030
$psd1Text = @"
@@ -2020,7 +2053,7 @@ Describe PowerShell.PSGet.PublishModuleTests.P2 -Tags 'P2','OuterLoop' {
20202053
}
20212054
}
20222055
}
2023-
"@
2056+
"@
20242057

20252058
($psd1Text -replace '__VERSION__',$version) | Out-File -FilePath (Join-Path -Path $ModuleBase -ChildPath "$ModuleName.psd1") -Force
20262059

@@ -2097,4 +2130,4 @@ Describe PowerShell.PSGet.PublishModuleTests.P2 -Tags 'P2','OuterLoop' {
20972130
}
20982131
}
20992132
}
2100-
}
2133+
}

src/PowerShellGet/public/psgetfunctions/Publish-Module.ps1

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,18 @@ function Publish-Module {
365365
}
366366

367367
$null = Microsoft.PowerShell.Management\New-Item -Path $tempModulePathForFormatVersion -ItemType Directory -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -Confirm:$false -WhatIf:$false
368-
Microsoft.PowerShell.Management\Copy-Item -Path "$Path\*" -Destination $tempModulePathForFormatVersion -Force -Recurse -Confirm:$false -WhatIf:$false
368+
369+
# Copy-Item -Recurse -Force includes hidden items like .git directories, which we don't want
370+
# This finds all the items without force (leaving out hidden files and dirs) then copies them
371+
Microsoft.PowerShell.Management\Get-ChildItem $Path -recurse |
372+
Microsoft.PowerShell.Management\Copy-Item -Force -Confirm:$false -WhatIf:$false -Destination {
373+
if ($_.PSIsContainer) {
374+
Join-Path $tempModulePathForFormatVersion $_.Parent.FullName.substring($path.length)
375+
}
376+
else {
377+
join-path $tempModulePathForFormatVersion $_.FullName.Substring($path.Length)
378+
}
379+
}
369380

370381
try {
371382
$manifestPath = Join-PathUtility -Path $tempModulePathForFormatVersion -ChildPath "$moduleName.psd1" -PathType File

0 commit comments

Comments
 (0)