created separate util module

This commit is contained in:
Jared Petersen 2020-05-11 21:10:44 -07:00
parent 1ecbe18c8b
commit 5e3159d960
6 changed files with 145 additions and 42 deletions

View file

@ -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'; import * as auth from '../src/auth';
const m2Dir = path.join(__dirname, auth.M2_DIR); const m2Dir = path.join(__dirname, auth.M2_DIR);
const settingsFile = path.join(m2Dir, auth.SETTINGS_FILE); const settingsFile = path.join(m2Dir, auth.SETTINGS_FILE);
const tempDir = path.join(__dirname, 'runner', 'temp');
const privateKeyFile = path.join(tempDir, auth.PRIVATE_KEY_FILE); const privateKeyFile = path.join(tempDir, auth.PRIVATE_KEY_FILE);
process.env['RUNNER_TEMP'] = tempDir;
describe('auth tests', () => { describe('auth tests', () => {
beforeEach(async () => { beforeEach(async () => {
await io.rmRF(m2Dir); await io.rmRF(m2Dir);
await io.mkdirP(tempDir);
}, 300000); }, 300000);
afterAll(async () => { afterAll(async () => {

56
__tests__/util.test.ts Normal file
View file

@ -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'));
});
});
});

76
dist/index.js generated vendored
View file

@ -2850,6 +2850,46 @@ function coerce (version, options) {
} }
/***/ }),
/***/ 322:
/***/ (function(__unusedmodule, exports, __webpack_require__) {
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = __importStar(__webpack_require__(622));
function getTempDir() {
let tempDirectory = process.env['RUNNER_TEMP'] || '';
if (!tempDirectory) {
let baseLocation;
const IS_WINDOWS = process.platform === 'win32';
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');
}
return tempDirectory;
}
exports.getTempDir = getTempDir;
/***/ }), /***/ }),
/***/ 331: /***/ 331:
@ -2880,9 +2920,11 @@ const path = __importStar(__webpack_require__(622));
const core = __importStar(__webpack_require__(470)); const core = __importStar(__webpack_require__(470));
const io = __importStar(__webpack_require__(1)); const io = __importStar(__webpack_require__(1));
const exec = __importStar(__webpack_require__(986)); const exec = __importStar(__webpack_require__(986));
const util = __importStar(__webpack_require__(322));
exports.M2_DIR = '.m2'; exports.M2_DIR = '.m2';
exports.TEMP_DIR = process.env['RUNNER_TEMP'] || ''; exports.TEMP_DIR = util.getTempDir();
exports.SETTINGS_FILE = 'settings.xml'; exports.SETTINGS_FILE = 'settings.xml';
exports.KEYRING_FILE = 'temporary-keyring.gpg';
exports.PRIVATE_KEY_FILE = 'private-key.asc'; exports.PRIVATE_KEY_FILE = 'private-key.asc';
exports.DEFAULT_ID = 'github'; exports.DEFAULT_ID = 'github';
exports.DEFAULT_USERNAME = 'GITHUB_ACTOR'; exports.DEFAULT_USERNAME = 'GITHUB_ACTOR';
@ -2955,11 +2997,20 @@ function remove(path) {
} }
function importGPG(gpgPrivateKey) { function importGPG(gpgPrivateKey) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const temporaryKeyRingPath = path.join(exports.TEMP_DIR, exports.KEYRING_FILE);
const temporaryPrivateKeyPath = path.join(exports.TEMP_DIR, exports.PRIVATE_KEY_FILE);
yield write(exports.TEMP_DIR, exports.PRIVATE_KEY_FILE, gpgPrivateKey); yield write(exports.TEMP_DIR, exports.PRIVATE_KEY_FILE, gpgPrivateKey);
yield exec.exec('gpg', ['--import', '--batch', exports.PRIVATE_KEY_FILE], { yield exec.exec('gpg', [
'--primary-keyring',
temporaryKeyRingPath,
'--import',
'--batch',
exports.PRIVATE_KEY_FILE
], {
cwd: exports.TEMP_DIR cwd: exports.TEMP_DIR
}); });
yield remove(path.join(exports.TEMP_DIR, exports.PRIVATE_KEY_FILE)); yield remove(temporaryKeyRingPath);
yield remove(temporaryPrivateKeyPath);
}); });
} }
@ -4672,7 +4723,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result; return result;
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
let tempDirectory = process.env['RUNNER_TEMP'] || '';
const core = __importStar(__webpack_require__(470)); const core = __importStar(__webpack_require__(470));
const io = __importStar(__webpack_require__(1)); const io = __importStar(__webpack_require__(1));
const exec = __importStar(__webpack_require__(986)); const exec = __importStar(__webpack_require__(986));
@ -4681,23 +4731,9 @@ const tc = __importStar(__webpack_require__(533));
const fs = __importStar(__webpack_require__(747)); const fs = __importStar(__webpack_require__(747));
const path = __importStar(__webpack_require__(622)); const path = __importStar(__webpack_require__(622));
const semver = __importStar(__webpack_require__(280)); const semver = __importStar(__webpack_require__(280));
const util = __importStar(__webpack_require__(322));
const tempDirectory = util.getTempDir();
const IS_WINDOWS = process.platform === 'win32'; 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');
}
function getJava(version, arch, jdkFile, javaPackage) { function getJava(version, arch, jdkFile, javaPackage) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
let toolPath = tc.find(javaPackage, version); let toolPath = tc.find(javaPackage, version);

View file

@ -4,9 +4,10 @@ import * as path from 'path';
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as io from '@actions/io'; import * as io from '@actions/io';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
import * as util from './util';
export const M2_DIR = '.m2'; 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 SETTINGS_FILE = 'settings.xml';
export const PRIVATE_KEY_FILE = 'private-key.asc'; export const PRIVATE_KEY_FILE = 'private-key.asc';

View file

@ -1,5 +1,3 @@
let tempDirectory = process.env['RUNNER_TEMP'] || '';
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as io from '@actions/io'; import * as io from '@actions/io';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
@ -8,23 +6,10 @@ import * as tc from '@actions/tool-cache';
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as semver from 'semver'; import * as semver from 'semver';
import * as util from './util';
const IS_WINDOWS = process.platform === 'win32'; const tempDirectory = util.getTempDir();
const IS_WINDOWS = util.isWindows();
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');
}
export async function getJava( export async function getJava(
version: string, version: string,

24
src/util.ts Normal file
View file

@ -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');
}