1
0
Fork 0
mirror of https://github.com/actions/setup-go synced 2024-12-22 22:32:42 +00:00

Add unit tests

This commit is contained in:
Sergey Dolin 2023-08-18 09:01:34 +02:00
parent 6f72b31c94
commit deaf43692d
7 changed files with 114 additions and 78 deletions

View file

@ -3,6 +3,7 @@ import * as cache from '@actions/cache';
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as cacheUtils from '../src/cache-utils'; import * as cacheUtils from '../src/cache-utils';
import {PackageManagerInfo} from '../src/package-managers'; import {PackageManagerInfo} from '../src/package-managers';
import * as utils from '../src/utils';
describe('getCommandOutput', () => { describe('getCommandOutput', () => {
//Arrange //Arrange
@ -209,3 +210,76 @@ describe('isCacheFeatureAvailable', () => {
expect(warningSpy).toHaveBeenCalledWith(warningMessage); expect(warningSpy).toHaveBeenCalledWith(warningMessage);
}); });
}); });
describe('Detect environment', () => {
it('"RUNNER_ENVIRONMENT" = "github-hosted" should be hosted environment', () => {
delete process.env['AGENT_ISSELFHOSTED'];
process.env['RUNNER_ENVIRONMENT'] = 'github-hosted';
expect(utils.isSelfHosted()).toBeFalsy();
});
it('"RUNNER_ENVIRONMENT" = "hosted" should be self-hosted environment', () => {
delete process.env['AGENT_ISSELFHOSTED'];
process.env['RUNNER_ENVIRONMENT'] = 'hosted';
expect(utils.isSelfHosted()).toBeTruthy();
});
it('"AGENT_ISSELFHOSTED" = "0" should be hosted environment', () => {
process.env['AGENT_ISSELFHOSTED'] = '0';
delete process.env['RUNNER_ENVIRONMENT'];
expect(utils.isSelfHosted()).toBeFalsy();
});
it('"AGENT_ISSELFHOSTED" = "0" should be self-hosted environment', () => {
process.env['AGENT_ISSELFHOSTED'] = '1';
delete process.env['RUNNER_ENVIRONMENT'];
expect(utils.isSelfHosted()).toBeTruthy();
});
it('unset "RUNNER_ENVIRONMENT" and "AGENT_ISSELFHOSTED" should be self-hosted environment', () => {
delete process.env['AGENT_ISSELFHOSTED'];
delete process.env['RUNNER_ENVIRONMENT'];
expect(utils.isSelfHosted()).toBeTruthy();
});
});
describe('Default cache values', () => {
const inputSpy = jest.spyOn(utils, 'isSelfHosted');
beforeEach(() => {
delete process.env['INPUT_CACHE'];
});
it('default cache should be false in self-hosted environment', () => {
inputSpy.mockReturnValueOnce(true);
expect(cacheUtils.getCacheInput()).toBeFalsy();
});
it('cache should be false if set to false in self-hosted environment', () => {
inputSpy.mockReturnValueOnce(true);
process.env['INPUT_CACHE'] = 'false';
expect(cacheUtils.getCacheInput()).toBeFalsy();
});
it('cache should be tue if set to true in self-hosted environment', () => {
inputSpy.mockReturnValueOnce(true);
process.env['INPUT_CACHE'] = 'true';
expect(cacheUtils.getCacheInput()).toBeTruthy();
});
it('default cache should be handled by action.yml default in hosted environment', () => {
inputSpy.mockReturnValueOnce(false);
expect(() => cacheUtils.getCacheInput()).toThrow();
});
it('cache should be false if set to false in hosted environment', () => {
inputSpy.mockReturnValueOnce(false);
process.env['INPUT_CACHE'] = 'true';
expect(cacheUtils.getCacheInput()).toBeTruthy();
});
it('cache should be tue if set to true in hosted environment', () => {
inputSpy.mockReturnValueOnce(false);
process.env['INPUT_CACHE'] = 'false';
expect(cacheUtils.getCacheInput()).toBeFalsy();
});
});

View file

@ -58495,7 +58495,6 @@ const cache = __importStar(__nccwpck_require__(7799));
const fs_1 = __importDefault(__nccwpck_require__(7147)); const fs_1 = __importDefault(__nccwpck_require__(7147));
const constants_1 = __nccwpck_require__(9042); const constants_1 = __nccwpck_require__(9042);
const cache_utils_1 = __nccwpck_require__(1678); const cache_utils_1 = __nccwpck_require__(1678);
const utils_1 = __nccwpck_require__(1314);
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in // Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to // @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
// throw an uncaught exception. Instead of failing this action, just warn. // throw an uncaught exception. Instead of failing this action, just warn.
@ -58522,7 +58521,7 @@ function run() {
} }
exports.run = run; exports.run = run;
const cachePackages = () => __awaiter(void 0, void 0, void 0, function* () { const cachePackages = () => __awaiter(void 0, void 0, void 0, function* () {
if (!utils_1.getCacheInput()) if (!cache_utils_1.getCacheInput())
return; return;
const packageManager = 'default'; const packageManager = 'default';
const state = core.getState(constants_1.State.CacheMatchedKey); const state = core.getState(constants_1.State.CacheMatchedKey);
@ -58594,11 +58593,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}); });
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isCacheFeatureAvailable = exports.isGhes = exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = void 0; exports.getCacheInput = exports.isCacheFeatureAvailable = exports.isGhes = exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = void 0;
const cache = __importStar(__nccwpck_require__(7799)); const cache = __importStar(__nccwpck_require__(7799));
const core = __importStar(__nccwpck_require__(2186)); const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514)); const exec = __importStar(__nccwpck_require__(1514));
const package_managers_1 = __nccwpck_require__(6663); const package_managers_1 = __nccwpck_require__(6663);
const utils_1 = __nccwpck_require__(1314);
const getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () { const getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () {
let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true }); let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true });
if (exitCode) { if (exitCode) {
@ -58653,6 +58653,13 @@ function isCacheFeatureAvailable() {
return false; return false;
} }
exports.isCacheFeatureAvailable = isCacheFeatureAvailable; exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
const getCacheInput = () => {
// for self-hosted environment turn off cache by default
if (utils_1.isSelfHosted() && core.getInput('cache') === '')
return false;
return core.getBooleanInput('cache');
};
exports.getCacheInput = getCacheInput;
/***/ }), /***/ }),
@ -58695,47 +58702,20 @@ exports.supportedPackageManagers = {
/***/ }), /***/ }),
/***/ 1314: /***/ 1314:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { /***/ ((__unused_webpack_module, exports) => {
"use strict"; "use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getCacheInput = exports.isSelfHosted = exports.StableReleaseAlias = void 0; exports.isSelfHosted = exports.StableReleaseAlias = void 0;
const core = __importStar(__nccwpck_require__(2186));
var StableReleaseAlias; var StableReleaseAlias;
(function (StableReleaseAlias) { (function (StableReleaseAlias) {
StableReleaseAlias["Stable"] = "stable"; StableReleaseAlias["Stable"] = "stable";
StableReleaseAlias["OldStable"] = "oldstable"; StableReleaseAlias["OldStable"] = "oldstable";
})(StableReleaseAlias = exports.StableReleaseAlias || (exports.StableReleaseAlias = {})); })(StableReleaseAlias = exports.StableReleaseAlias || (exports.StableReleaseAlias = {}));
const isSelfHosted = () => process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted' && const isSelfHosted = () => process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted' &&
process.env['AGENT_ISSELFHOSTED'] === '1'; process.env['AGENT_ISSELFHOSTED'] !== '0';
exports.isSelfHosted = isSelfHosted; exports.isSelfHosted = isSelfHosted;
const getCacheInput = () => {
// for self-hosted environment turn off cache by default
if (exports.isSelfHosted() && core.getInput('cache') === '')
return false;
return core.getBooleanInput('cache');
};
exports.getCacheInput = getCacheInput;
/***/ }), /***/ }),

46
dist/setup/index.js vendored
View file

@ -61265,11 +61265,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}); });
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isCacheFeatureAvailable = exports.isGhes = exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = void 0; exports.getCacheInput = exports.isCacheFeatureAvailable = exports.isGhes = exports.getCacheDirectoryPath = exports.getPackageManagerInfo = exports.getCommandOutput = void 0;
const cache = __importStar(__nccwpck_require__(7799)); const cache = __importStar(__nccwpck_require__(7799));
const core = __importStar(__nccwpck_require__(2186)); const core = __importStar(__nccwpck_require__(2186));
const exec = __importStar(__nccwpck_require__(1514)); const exec = __importStar(__nccwpck_require__(1514));
const package_managers_1 = __nccwpck_require__(6663); const package_managers_1 = __nccwpck_require__(6663);
const utils_1 = __nccwpck_require__(1314);
const getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () { const getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () {
let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true }); let { stdout, stderr, exitCode } = yield exec.getExecOutput(toolCommand, undefined, { ignoreReturnCode: true });
if (exitCode) { if (exitCode) {
@ -61324,6 +61325,13 @@ function isCacheFeatureAvailable() {
return false; return false;
} }
exports.isCacheFeatureAvailable = isCacheFeatureAvailable; exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
const getCacheInput = () => {
// for self-hosted environment turn off cache by default
if (utils_1.isSelfHosted() && core.getInput('cache') === '')
return false;
return core.getBooleanInput('cache');
};
exports.getCacheInput = getCacheInput;
/***/ }), /***/ }),
@ -61776,7 +61784,6 @@ const cache_utils_1 = __nccwpck_require__(1678);
const child_process_1 = __importDefault(__nccwpck_require__(2081)); const child_process_1 = __importDefault(__nccwpck_require__(2081));
const fs_1 = __importDefault(__nccwpck_require__(7147)); const fs_1 = __importDefault(__nccwpck_require__(7147));
const os_1 = __importDefault(__nccwpck_require__(2037)); const os_1 = __importDefault(__nccwpck_require__(2037));
const utils_1 = __nccwpck_require__(1314);
function run() { function run() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
@ -61785,7 +61792,7 @@ function run() {
// If not supplied then problem matchers will still be setup. Useful for self-hosted. // If not supplied then problem matchers will still be setup. Useful for self-hosted.
// //
const versionSpec = resolveVersionInput(); const versionSpec = resolveVersionInput();
const cache = utils_1.getCacheInput(); const cache = cache_utils_1.getCacheInput();
core.info(`Setup go version spec ${versionSpec}`); core.info(`Setup go version spec ${versionSpec}`);
let arch = core.getInput('architecture'); let arch = core.getInput('architecture');
if (!arch) { if (!arch) {
@ -61966,47 +61973,20 @@ exports.getArch = getArch;
/***/ }), /***/ }),
/***/ 1314: /***/ 1314:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { /***/ ((__unused_webpack_module, exports) => {
"use strict"; "use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getCacheInput = exports.isSelfHosted = exports.StableReleaseAlias = void 0; exports.isSelfHosted = exports.StableReleaseAlias = void 0;
const core = __importStar(__nccwpck_require__(2186));
var StableReleaseAlias; var StableReleaseAlias;
(function (StableReleaseAlias) { (function (StableReleaseAlias) {
StableReleaseAlias["Stable"] = "stable"; StableReleaseAlias["Stable"] = "stable";
StableReleaseAlias["OldStable"] = "oldstable"; StableReleaseAlias["OldStable"] = "oldstable";
})(StableReleaseAlias = exports.StableReleaseAlias || (exports.StableReleaseAlias = {})); })(StableReleaseAlias = exports.StableReleaseAlias || (exports.StableReleaseAlias = {}));
const isSelfHosted = () => process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted' && const isSelfHosted = () => process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted' &&
process.env['AGENT_ISSELFHOSTED'] === '1'; process.env['AGENT_ISSELFHOSTED'] !== '0';
exports.isSelfHosted = isSelfHosted; exports.isSelfHosted = isSelfHosted;
const getCacheInput = () => {
// for self-hosted environment turn off cache by default
if (exports.isSelfHosted() && core.getInput('cache') === '')
return false;
return core.getBooleanInput('cache');
};
exports.getCacheInput = getCacheInput;
/***/ }), /***/ }),

View file

@ -2,8 +2,11 @@ import * as core from '@actions/core';
import * as cache from '@actions/cache'; import * as cache from '@actions/cache';
import fs from 'fs'; import fs from 'fs';
import {State} from './constants'; import {State} from './constants';
import {getCacheDirectoryPath, getPackageManagerInfo} from './cache-utils'; import {
import {getCacheInput} from './utils'; getCacheDirectoryPath,
getPackageManagerInfo,
getCacheInput
} from './cache-utils';
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in // Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to // @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to

View file

@ -2,6 +2,7 @@ import * as cache from '@actions/cache';
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
import {supportedPackageManagers, PackageManagerInfo} from './package-managers'; import {supportedPackageManagers, PackageManagerInfo} from './package-managers';
import {isSelfHosted} from './utils';
export const getCommandOutput = async (toolCommand: string) => { export const getCommandOutput = async (toolCommand: string) => {
let {stdout, stderr, exitCode} = await exec.getExecOutput( let {stdout, stderr, exitCode} = await exec.getExecOutput(
@ -83,3 +84,9 @@ export function isCacheFeatureAvailable(): boolean {
); );
return false; return false;
} }
export const getCacheInput = (): boolean => {
// for self-hosted environment turn off cache by default
if (isSelfHosted() && core.getInput('cache') === '') return false;
return core.getBooleanInput('cache');
};

View file

@ -4,11 +4,10 @@ import * as installer from './installer';
import * as semver from 'semver'; import * as semver from 'semver';
import path from 'path'; import path from 'path';
import {restoreCache} from './cache-restore'; import {restoreCache} from './cache-restore';
import {isCacheFeatureAvailable} from './cache-utils'; import {isCacheFeatureAvailable, getCacheInput} from './cache-utils';
import cp from 'child_process'; import cp from 'child_process';
import fs from 'fs'; import fs from 'fs';
import os from 'os'; import os from 'os';
import {getCacheInput} from './utils';
export async function run() { export async function run() {
try { try {

View file

@ -7,11 +7,4 @@ export enum StableReleaseAlias {
export const isSelfHosted = (): boolean => export const isSelfHosted = (): boolean =>
process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted' && process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted' &&
process.env['AGENT_ISSELFHOSTED'] === '1'; process.env['AGENT_ISSELFHOSTED'] !== '0';
export const getCacheInput = (): boolean => {
// for self-hosted environment turn off cache by default
if (isSelfHosted() && core.getInput('cache') === '') return false;
return core.getBooleanInput('cache');
};