From 5e3159d960f15ec81b98b49195c00586a52ed3fe Mon Sep 17 00:00:00 2001 From: Jared Petersen Date: Mon, 11 May 2020 21:10:44 -0700 Subject: [PATCH] created separate util module --- __tests__/auth.test.ts | 7 +++--- __tests__/util.test.ts | 56 +++++++++++++++++++++++++++++++++++++++++ dist/index.js | Bin 169915 -> 171117 bytes src/auth.ts | 3 ++- src/installer.ts | 21 +++------------- src/util.ts | 24 ++++++++++++++++++ 6 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 __tests__/util.test.ts create mode 100644 src/util.ts diff --git a/__tests__/auth.test.ts b/__tests__/auth.test.ts index e7b4c032..58b1134b 100644 --- a/__tests__/auth.test.ts +++ b/__tests__/auth.test.ts @@ -17,18 +17,19 @@ jest.mock('@actions/exec', () => { }; }); +const tempDir = path.join(__dirname, 'runner', 'temp'); +process.env['RUNNER_TEMP'] = tempDir; + import * as auth from '../src/auth'; const m2Dir = path.join(__dirname, auth.M2_DIR); const settingsFile = path.join(m2Dir, auth.SETTINGS_FILE); -const tempDir = path.join(__dirname, 'runner', 'temp'); const privateKeyFile = path.join(tempDir, auth.PRIVATE_KEY_FILE); -process.env['RUNNER_TEMP'] = tempDir; - describe('auth tests', () => { beforeEach(async () => { await io.rmRF(m2Dir); + await io.mkdirP(tempDir); }, 300000); afterAll(async () => { diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts new file mode 100644 index 00000000..d7809b51 --- /dev/null +++ b/__tests__/util.test.ts @@ -0,0 +1,56 @@ +import path = require('path'); + +const env = process.env; + +describe('util tests', () => { + beforeEach(() => { + process.env = Object.assign({}, env); + Object.defineProperty(process, 'platform', {value: 'linux'}); + }); + + describe('getTempDir', () => { + it('gets temp dir using env', () => { + process.env['RUNNER_TEMP'] = 'defaulttmp' + const util = require('../src/util'); + + const tempDir = util.getTempDir(); + + expect(tempDir).toEqual(process.env['RUNNER_TEMP']); + }); + + it('gets temp dir for windows using userprofile', () => { + Object.defineProperty(process, 'platform', {value: 'win32'}); + process.env['USERPROFILE'] = 'winusertmp'; + const util = require('../src/util'); + + const tempDir = util.getTempDir(); + + expect(tempDir).toEqual(path.join(process.env['USERPROFILE'], 'actions', 'temp')); + }); + + it('gets temp dir for windows using c drive', () => { + Object.defineProperty(process, 'platform', {value: 'win32'}); + const util = require('../src/util'); + + const tempDir = util.getTempDir(); + + expect(tempDir).toEqual(path.join('C:\\', 'actions', 'temp')); + }); + + it('gets temp dir for mac', () => { + Object.defineProperty(process, 'platform', {value: 'darwin'}); + const util = require('../src/util'); + + const tempDir = util.getTempDir(); + + expect(tempDir).toEqual(path.join('/Users', 'actions', 'temp')); + }); + + it('gets temp dir for linux', () => { + const util = require('../src/util'); + const tempDir = util.getTempDir(); + + expect(tempDir).toEqual(path.join('/home', 'actions', 'temp')); + }); + }); +}); diff --git a/dist/index.js b/dist/index.js index b708bbe3a55098c38df1efe8375163484bcba23b..c879345f2a9b1ce923c683ccb38ec1455449630d 100644 GIT binary patch delta 718 zcmdnJp6l%nu7)j)dqSCvjJC^XF>Yj>{wtWtLO4COBqTMrz$LRtLsOxeO92R`b53P6 zo31mRQEB>vsf=PQU@^NXjE0lroYba^r7)RKKas*DJUu3bNo#t!Bop8C2PI6xlQ%fB zOrJT8QFn5J5X^7(QW#=Rwj0X5{S*I z$tC$kmDXIfT&WcW`9&qgdI(o3*eW16(>G=^if-Q>%Gkrom7JefT%u4~l9@BTF^W-z z+1SWvx^pz6zA#7#$p8&aYp&@535=rCS4J~Rt9rXe26_6q$Gds@xB|^q2RR|XD6yzg zH#@bmC^IizFTEgr`_5>_|J>8_O_78GUXBDqIh2SowgP$cJ|ssI|1 z3G!13nogj2QesJR2Ac5nJ<*I3oNzm)`)4qEh+wm2dVL0?1R^+4J+=Kz2IIFM(*uH; zY^J}6VvL_|I*HL{`uQZLODymx(gQ}3CNPu`p#w~Hkhlj1C`PnRx0%9d%$Zi2mt2yW SpQkXrFN{fN`^r=%AtM0zN9Kh9 delta 178 zcmV;j08RhxxC* zz+eIN3zutc0caK{EiGwnaBp&SCoCW*EiGbUbYo~IESFJk0U4KbZUIP_(QE-Emr!j1 z5x4$s0rvQpJDUMEmr`E=p|_Q20tf+@cA5bz5@}{2C?RxZZE!?sa%E$5Z*qCJ3Y-C0 g0hfTC0Xz$R3TAa~V{~b6ZXlPyTmme&TWSIlGTGxj{{R30 diff --git a/src/auth.ts b/src/auth.ts index 6e6f2afa..e7978d55 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -4,9 +4,10 @@ import * as path from 'path'; import * as core from '@actions/core'; import * as io from '@actions/io'; import * as exec from '@actions/exec'; +import * as util from './util'; export const M2_DIR = '.m2'; -export const TEMP_DIR = process.env['RUNNER_TEMP'] || ''; +export const TEMP_DIR = util.getTempDir(); export const SETTINGS_FILE = 'settings.xml'; export const PRIVATE_KEY_FILE = 'private-key.asc'; diff --git a/src/installer.ts b/src/installer.ts index cce8fa3d..d7a44710 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -1,5 +1,3 @@ -let tempDirectory = process.env['RUNNER_TEMP'] || ''; - import * as core from '@actions/core'; import * as io from '@actions/io'; import * as exec from '@actions/exec'; @@ -8,23 +6,10 @@ import * as tc from '@actions/tool-cache'; import * as fs from 'fs'; import * as path from 'path'; import * as semver from 'semver'; +import * as util from './util'; -const IS_WINDOWS = process.platform === 'win32'; - -if (!tempDirectory) { - let baseLocation; - if (IS_WINDOWS) { - // On windows use the USERPROFILE env variable - baseLocation = process.env['USERPROFILE'] || 'C:\\'; - } else { - if (process.platform === 'darwin') { - baseLocation = '/Users'; - } else { - baseLocation = '/home'; - } - } - tempDirectory = path.join(baseLocation, 'actions', 'temp'); -} +const tempDirectory = util.getTempDir(); +const IS_WINDOWS = util.isWindows(); export async function getJava( version: string, diff --git a/src/util.ts b/src/util.ts new file mode 100644 index 00000000..a061593b --- /dev/null +++ b/src/util.ts @@ -0,0 +1,24 @@ +import * as path from 'path'; + +export function getTempDir() { + let tempDirectory = process.env.RUNNER_TEMP; + if (tempDirectory === undefined) { + let baseLocation; + if (isWindows()) { + // On windows use the USERPROFILE env variable + baseLocation = (process.env['USERPROFILE']) ? process.env['USERPROFILE'] : 'C:\\'; + } else { + if (process.platform === 'darwin') { + baseLocation = '/Users'; + } else { + baseLocation = '/home'; + } + } + tempDirectory = path.join(baseLocation, 'actions', 'temp'); + } + return tempDirectory; +} + +export function isWindows() { + return (process.platform === 'win32'); +} \ No newline at end of file