Skip to content
Merged
Prev Previous commit
Next Next commit
Fix for mac
  • Loading branch information
Danny McCormick committed Jun 5, 2019
commit 1ad4b89c008c20e37f54d32298032bc261b64e76
10 changes: 7 additions & 3 deletions packages/tool-cache/__tests__/tool-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ import * as nock from 'nock'
import * as path from 'path'
import * as io from '@actions/io'
import * as exec from '@actions/exec'
import * as tc from '../src/tool-cache'

'use strict'

const cachePath = path.join(__dirname, 'CACHE')
const tempPath = path.join(__dirname, 'TEMP')
// Set temp and tool directories before running
process.env['RUNNER_TEMPDIRECTORY'] = tempPath
process.env['RUNNER_TOOLSDIRECTORY'] = cachePath

// eslint-disable-next-line import/first
import * as tc from '../src/tool-cache'

const IS_WINDOWS = process.platform === 'win32'

describe('@actions/tool-cache', function() {
Expand All @@ -27,8 +33,6 @@ describe('@actions/tool-cache', function() {
await io.rmRF(tempPath)
await io.mkdirP(cachePath)
await io.mkdirP(tempPath)
process.env['RUNNER_TEMPDIRECTORY'] = tempPath
process.env['RUNNER_TOOLSDIRECTORY'] = cachePath
})

afterAll(async function() {
Expand Down
77 changes: 38 additions & 39 deletions packages/tool-cache/src/tool-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import * as semver from 'semver'
import * as uuidV4 from 'uuid/v4'
import {exec} from '@actions/exec/lib/exec'
import {ExecOptions} from '@actions/exec/lib/interfaces'
import {ok} from 'assert'

// Add index signature so that we can attach additional fields without breaking type checking (e.g. httpStatusCode)
interface Error {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any
class HTTPError extends Error {
constructor(readonly httpStatusCode: number | undefined) {
super(`Unexpected HTTP response: ${httpStatusCode}`)
Object.setPrototypeOf(this, new.target.prototype)
}
}

const IS_WINDOWS = process.platform === 'win32'
Expand All @@ -30,7 +32,11 @@ if (!tempDirectory || !cacheRoot) {
// On windows use the USERPROFILE env variable
baseLocation = process.env['USERPROFILE'] || 'C:\\'
} else {
baseLocation = '/home'
if (process.platform === 'darwin') {
baseLocation = '/Users'
} else {
baseLocation = '/home'
}
}
if (!tempDirectory) {
tempDirectory = path.join(baseLocation, 'actions', 'temp')
Expand All @@ -47,13 +53,14 @@ if (!tempDirectory || !cacheRoot) {
* @returns path to downloaded tool
*/
export async function downloadTool(url: string): Promise<string> {
// Wrap in a promise so that we can resolve from within stream callbacks
return new Promise<string>(async (resolve, reject) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need to construct a promise here manually, do we? If so, I think a comment is warranted. Keep in mind that async functions implicitly return a promise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wrapped it so that I can resolve from within the stream callbacks. e.g.

file.on('open', async () => {
   *do stuff*
   resolve
})

If I just return normally there, it just returns from the callback. Is there an easier way to handle that? Added a comment for now

try {
const http: httpm.HttpClient = new httpm.HttpClient(userAgent, [], {
const http = new httpm.HttpClient(userAgent, [], {
allowRetries: true,
maxRetries: 3
})
const destPath: string = path.join(tempDirectory, uuidV4())
const destPath = path.join(tempDirectory, uuidV4())

await io.mkdirP(tempDirectory)
core.debug(`Downloading ${url}`)
Expand All @@ -66,15 +73,12 @@ export async function downloadTool(url: string): Promise<string> {
const response: httpm.HttpClientResponse = await http.get(url)

if (response.message.statusCode !== 200) {
const err: Error = new Error(
`Unexpected HTTP response: ${response.message.statusCode}`
)
const err = new HTTPError(response.message.statusCode)
core.debug(
`Failed to download from "${url}". Code(${
response.message.statusCode
}) Message(${response.message.statusMessage})`
)
err['httpStatusCode'] = response.message.statusCode
throw err
}

Expand Down Expand Up @@ -113,40 +117,35 @@ export async function downloadTool(url: string): Promise<string> {
* @returns path to the destination directory
*/
export async function extract7z(file: string, dest?: string): Promise<string> {
if (!IS_WINDOWS) {
throw new Error('extract7z() not supported on current OS')
}

if (!file) {
throw new Error("parameter 'file' is required")
}
ok(IS_WINDOWS, 'extract7z() not supported on current OS')
ok(file, 'parameter "file" is required')

dest = dest || (await _createExtractFolder(dest))

const originalCwd = process.cwd()
process.chdir(dest)
const escapedScript = path
.join(__dirname, '..', 'scripts', 'Invoke-7zdec.ps1')
.replace(/'/g, "''")
.replace(/"|\n|\r/g, '') // double-up single quotes, remove double quotes and newlines
const escapedFile = file.replace(/'/g, "''").replace(/"|\n|\r/g, '')
const escapedTarget = dest.replace(/'/g, "''").replace(/"|\n|\r/g, '')
const command = `& '${escapedScript}' -Source '${escapedFile}' -Target '${escapedTarget}'`
const args: string[] = [
'-NoLogo',
'-Sta',
'-NoProfile',
'-NonInteractive',
'-ExecutionPolicy',
'Unrestricted',
'-Command',
command
]
const options: ExecOptions = {
silent: true
}
try {
process.chdir(dest)
const escapedScript = path
.join(__dirname, '..', 'scripts', 'Invoke-7zdec.ps1')
.replace(/'/g, "''")
.replace(/"|\n|\r/g, '') // double-up single quotes, remove double quotes and newlines
const escapedFile = file.replace(/'/g, "''").replace(/"|\n|\r/g, '')
const escapedTarget = dest.replace(/'/g, "''").replace(/"|\n|\r/g, '')
const command = `& '${escapedScript}' -Source '${escapedFile}' -Target '${escapedTarget}'`
const powershellPath: string = await io.which('powershell', true)
const args: string[] = [
'-NoLogo',
'-Sta',
'-NoProfile',
'-NonInteractive',
'-ExecutionPolicy',
'Unrestricted',
'-Command',
command
]
const options: ExecOptions = {
silent: true
}
await exec(`"${powershellPath}"`, args, options)
} finally {
process.chdir(originalCwd)
Expand Down