Skip to content
This repository has been archived by the owner on Feb 19, 2019. It is now read-only.

Make the temp directory configurable #554

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion chocolateyInstall/InstallChocolatey.ps1
Expand Up @@ -20,7 +20,7 @@
#$url = "http://chocolatey.org/packages/chocolatey/DownloadPackage"
$url = "http://chocolatey.org/api/v2/package/chocolatey/"
#$url = "http://chocolatey.org/api/v1/package/chocolatey"
$chocTempDir = Join-Path $env:TEMP "chocolatey"
$chocTempDir = Get-ChocolateyTempDir
$tempDir = Join-Path $chocTempDir "chocInstall"
if (![System.IO.Directory]::Exists($tempDir)) {[System.IO.Directory]::CreateDirectory($tempDir)}
$file = Join-Path $tempDir "chocolatey.zip"
Expand Down
2 changes: 1 addition & 1 deletion nuget/tools/chocolateysetup.psm1
Expand Up @@ -198,7 +198,7 @@ param(
if(!(test-path "$env:windir\Microsoft.Net\$fx\v4.0.30319")) {
$NetFx4ClientUrl = 'http://download.microsoft.com/download/5/6/2/562A10F9-C9F4-4313-A044-9C94E0A8FAC8/dotNetFx40_Client_x86_x64.exe'
$NetFx4FullUrl = 'http://download.microsoft.com/download/9/5/A/95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE/dotNetFx40_Full_x86_x64.exe'
Install-ChocolateyPackage "NetFx4.0" 'exe' -silentArgs "/q /norestart /repair /log `'$env:Temp\NetFx4Install.log`'" -url "$NetFx4ClientUrl" -url64bit "$NetFx4ClientUrl" -validExitCodes @(0, 3010)
Install-ChocolateyPackage "NetFx4.0" 'exe' -silentArgs "/q /norestart /repair /log `'$(Get-TempDir)\NetFx4Install.log`'" -url "$NetFx4ClientUrl" -url64bit "$NetFx4ClientUrl" -validExitCodes @(0, 3010)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this would be available yet?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you are correct, I'll remove.

}
}

Expand Down
2 changes: 1 addition & 1 deletion src/functions/Delete-ExistingErrorLog.ps1
Expand Up @@ -4,7 +4,7 @@ param(
)
Write-Debug "Running 'Delete-ExistingErrorLog' for $packageName";

$chocTempDir = Join-Path $env:TEMP "chocolatey"
$chocTempDir = Get-ChocolateyTempDir
$tempDir = Join-Path $chocTempDir "$packageName"
$failureLog = Join-Path $tempDir 'failure.log'
Write-Debug "Looking for failure log at `'$failureLog`'"
Expand Down
6 changes: 6 additions & 0 deletions src/functions/Get-ChocolateyTempDir.ps1
@@ -0,0 +1,6 @@
function Get-ChocolateyTempDir {
Write-Debug "Running 'Get-ChocolateyTempDir'";

$chocTempDir = Join-Path (Get-TempDir) "chocolatey"
return $chocTempDir
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would probably be better if packages could take advantage of it. This and Get-TempDir.ps1 should be in src/helpers/functions and Get-ChocolateyTempDir.ps1 could be exposed for use.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I move Get-ChocolateyTempDir and Get-TempDir to src/helpers/functions, may I then keep the references to them in the other helper functions, such as Install-ChocolateyPackage.ps1?

14 changes: 10 additions & 4 deletions src/functions/Get-ConfigValue.ps1
Expand Up @@ -2,9 +2,10 @@ function Get-ConfigValue {
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string] $configValue
[string] $configValue,
[string] $defaultValue
)
Write-Debug "Running 'Get-ConfigValue' with configValue:`'$configValue`'";
Write-Debug "Running 'Get-ConfigValue' with configValue:`'$configValue`', defaultValue:`'$defaultValue`'";

$returnValue = Get-UserConfigValue $configValue
Write-Debug "After checking the user config the value of `'$configValue`' is `'$returnValue`'"
Expand All @@ -17,8 +18,13 @@ param(
}

if ($returnValue -eq $null) {
Write-Error "A configuration value for $configValue was not found"
if ($defaultValue -eq $null) {
Write-Error "A configuration value for $configValue was not found and no default was specified"
}

Write-Debug "Neither the user nor global config specified a value for `'$configValue`'. Using the default value: `'$defaultValue`'"
$returnValue = $defaultValue
}

$returnValue
return $returnValue
}
7 changes: 7 additions & 0 deletions src/functions/Get-TempDir.ps1
@@ -0,0 +1,7 @@
function Get-TempDir {
Write-Debug "Running 'Get-TempDir'"

$tempDir = Get-ConfigValue 'tempDir' $env:TEMP
$tempDir = [Environment]::ExpandEnvironmentVariables($tempDir)
return $tempDir
}
2 changes: 1 addition & 1 deletion src/functions/Run-ChocolateyPS1.ps1
Expand Up @@ -44,7 +44,7 @@ param(
##testing Start-Process -FilePath "powershell.exe" -ArgumentList " -noexit `"$ps1FullPath`"" -Verb "runas" -Wait #-PassThru -UseNewEnvironment ##-RedirectStandardError $errorLog -WindowStyle Normal

#detect errors
$chocTempDir = Join-Path $env:TEMP "chocolatey"
$chocTempDir = Get-ChocolateyTempDir
$tempDir = Join-Path $chocTempDir "$packageName"
$failureLog = Join-Path $tempDir 'failure.log'
if ([System.IO.File]::Exists($failureLog)) {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/functions/Install-ChocolateyPackage.ps1
Expand Up @@ -70,7 +70,7 @@ param(
try {
Write-Debug "Running 'Install-ChocolateyPackage' for $packageName with url:`'$url`', args: `'$silentArgs`', fileType: `'$fileType`', url64bit: `'$url64bit`', checksum: `'$checksum`', checksumType: `'$checksumType`', checksum64: `'$checksum64`', checksumType64: `'$checksumType64`', validExitCodes: `'$validExitCodes`' ";

$chocTempDir = Join-Path $env:TEMP "chocolatey"
$chocTempDir = Get-ChocolateyTempDir
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything in helpers should not consider that functions in src/functions exist. Because, they won't once the rewrite is done. There may be others that have this issue already, but let's not add to it.

$tempDir = Join-Path $chocTempDir "$packageName"

if (![System.IO.Directory]::Exists($tempDir)) { [System.IO.Directory]::CreateDirectory($tempDir) | Out-Null }
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/functions/Install-ChocolateyVsixPackage.ps1
Expand Up @@ -71,7 +71,7 @@ param(
$installer = Join-Path $dir "VsixInstaller.exe"
}
if($installer) {
$download="$env:temp\$($packageName.Replace(' ','')).vsix"
$download="$(Get-TempDir)\$($packageName.Replace(' ','')).vsix"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as with Install-ChocolateyPackage.ps1

try{
Get-ChocolateyWebFile $packageName $download $vsixUrl -checksum $checksum -checksumType $checksumType
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/functions/Install-ChocolateyZipPackage.ps1
Expand Up @@ -61,7 +61,7 @@ param(
try {
$fileType = 'zip'

$chocTempDir = Join-Path $env:TEMP "chocolatey"
$chocTempDir = Get-ChocolateyTempDir
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as with Install-ChocolateyPackage.ps1

$tempDir = Join-Path $chocTempDir "$packageName"
if (![System.IO.Directory]::Exists($tempDir)) {[System.IO.Directory]::CreateDirectory($tempDir) | Out-Null}
$file = Join-Path $tempDir "$($packageName)Install.$fileType"
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/functions/Write-ChocolateyFailure.ps1
Expand Up @@ -4,7 +4,7 @@ param(
[string] $failureMessage
)

$chocTempDir = Join-Path $env:TEMP "chocolatey"
$chocTempDir = Get-ChocolateyTempDir
$tempDir = Join-Path $chocTempDir "$packageName"
if (![System.IO.Directory]::Exists($tempDir)) {[System.IO.Directory]::CreateDirectory($tempDir)}
$successLog = Join-Path $tempDir 'success.log'
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/functions/Write-ChocolateySuccess.ps1
Expand Up @@ -3,7 +3,7 @@ param(
[string] $packageName
)

$chocTempDir = Join-Path $env:TEMP "chocolatey"
$chocTempDir = Get-ChocolateyTempDir
$tempDir = Join-Path $chocTempDir "$packageName"
if (![System.IO.Directory]::Exists($tempDir)) {[System.IO.Directory]::CreateDirectory($tempDir)}

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Get-ChecksumValid.tests.ps1
Expand Up @@ -7,7 +7,7 @@ function setup-testfile {
$filePath = 'some\file.txt'
Setup -File "$filePath" 'yo yo'

Join-Path (Join-Path $env:Temp 'pester') "$filePath"
Join-Path (Join-Path (Get-TempDir) 'pester') "$filePath"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even tests?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right that wasn't necessary.

}

Context "When a good checksum is provided" {
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/Get-ConfigValue.tests.ps1
Expand Up @@ -25,6 +25,38 @@ Describe "Get-ConfigValue" {
}
}

Context "when retrieving an unspecified value with a default" {
$oldProfile = $env:USERPROFILE
$env:USERPROFILE = Join-Path 'TestDrive:' 'userProfile'
Setup -File 'chocolatey\chocolateyInstall\chocolatey.config' @"
<?xml version="1.0"?>
<chocolatey />
"@
$result = Get-ConfigValue 'checksumFiles' $false
$env:USERPROFILE = $oldProfile

It "should return the default value" {
$result | should Be 'false'
}
}

Context "when retrieving an configured value with a default" {
$oldProfile = $env:USERPROFILE
$env:USERPROFILE = Join-Path 'TestDrive:' 'userProfile'
Setup -File 'chocolatey\chocolateyInstall\chocolatey.config' @"
<?xml version="1.0"?>
<chocolatey>
<checksumFiles>true</checksumFiles>
</chocolatey>
"@
$result = Get-ConfigValue 'checksumFiles' $false
$env:USERPROFILE = $oldProfile

It "should return the configured value" {
$result | should Be 'true'
}
}

Context "when retrieving a list" {
$oldProfile = $env:USERPROFILE
$env:USERPROFILE = Join-Path 'TestDrive:' 'userProfile'
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/Get-TempDir.tests.ps1
@@ -0,0 +1,39 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Definition
$common = Join-Path (Split-Path -Parent $here) '_Common.ps1'
. $common

Setup -File 'userprofile\_crapworkaround.txt'
Describe "Get-TempDir" {
Context "when retrieving the temp dir without a configured value" {
$oldProfile = $env:USERPROFILE
$env:USERPROFILE = Join-Path 'TestDrive:' 'userProfile'
Setup -File 'chocolatey\chocolateyInstall\chocolatey.config' @"
<?xml version="1.0"?>
<chocolatey />
"@
$result = Get-TempDir
$env:USERPROFILE = $oldProfile

It "should return the default temp directory %TEMP%" {
$result | should Be $env:TEMP
}
}

Context "when retrieving the temp dir with a configured value" {
$oldProfile = $env:USERPROFILE
$env:USERPROFILE = Join-Path 'TestDrive:' 'userProfile'
Setup -File 'chocolatey\chocolateyInstall\chocolatey.config' @"
<?xml version="1.0"?>
<chocolatey>
<tempDir>%USERPROFILE%\TEMP</tempDir>
</chocolatey>
"@
$result = Get-TempDir
$expectedResult = Join-Path $env:USERPROFILE 'TEMP'
$env:USERPROFILE = $oldProfile

It "should return the configured temp directory %USERPROFILE%\TEMP" {
$result | should Be $expectedResult
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for specs!

2 changes: 1 addition & 1 deletion tests/unit/Install-ChocolateyVsixPackage.Tests.ps1
Expand Up @@ -137,7 +137,7 @@ Describe "Install-ChocolateyVsixPackage" {

Install-ChocolateyVsixPackage "package name" "url"
It "should remove spaces" {
Assert-MockCalled Install-Vsix -ParameterFilter {$installFile -eq "$env:temp\packagename.vsix"}
Assert-MockCalled Install-Vsix -ParameterFilter {$installFile -eq "$(Get-TempDir)\packagename.vsix"}
}
}
}
4 changes: 2 additions & 2 deletions tests/unit/Run-ChocolateyPS1.tests.ps1
Expand Up @@ -9,8 +9,8 @@ function ChocolateyInstall {

Context "with installer arguments" {
$cmd=(Get-Command ChocolateyInstall)
Mock Get-ChildItem { return @{Name="chocolateyinstall.ps1";FullName=$cmd} } -ParameterFilter { $path -eq "$env:temp\test" }
Run-ChocolateyPS1 "$env:temp\test" 'testPackage' 'install' 'real args'
Mock Get-ChildItem { return @{Name="chocolateyinstall.ps1";FullName=$cmd} } -ParameterFilter { $path -eq "$(Get-TempDir)\test" }
Run-ChocolateyPS1 "$(Get-TempDir)\test" 'testPackage' 'install' 'real args'

It "should set chocolateyInstallArguments env var to Installer Arguments" {
$global:installArgsInEnvironment | should Be 'real args'
Expand Down