Compare commits

..

15 Commits

Author SHA1 Message Date
78bd24e01a Merge pull request #50 from actions/goenv
go env block
2020-04-06 09:29:27 -04:00
6613fc89fc go env block 2020-04-06 08:42:55 -04:00
202a594963 Merge pull request #48 from actions/matcher
More focused problem matcher regex
2020-04-03 11:51:07 -04:00
0dbc7e4965 toString 2020-03-31 10:40:32 -04:00
2091469f9f lint 2020-03-31 10:38:13 -04:00
f32657ccaf output version of go it resolved to 2020-03-31 10:37:33 -04:00
cec6ecefb4 output version of go it resolved to 2020-03-31 10:32:03 -04:00
e36ce1d6cf tabs or spaces 2020-03-30 10:46:01 -04:00
89c89c5036 cleaner regex 2020-03-30 10:39:29 -04:00
0a62a734da another test case 2020-03-30 09:12:21 -04:00
5156bc5dd3 lint 2020-03-27 20:21:13 -04:00
7837b03976 more tests 2020-03-27 20:14:29 -04:00
74c8095946 lint 2020-03-27 19:58:10 -04:00
34223181a5 starting 2020-03-27 19:56:10 -04:00
1c06f0e82e couple tests 2020-03-27 00:55:12 -04:00
4 changed files with 214 additions and 45 deletions

View File

@ -1,15 +1,17 @@
import * as tc from '@actions/tool-cache';
import * as core from '@actions/core';
import fs = require('fs');
import * as io from '@actions/io';
import * as tc from '@actions/tool-cache';
import fs from 'fs';
import cp from 'child_process';
import osm = require('os');
import path = require('path');
import {run} from '../src/main';
import * as httpm from '@actions/http-client';
import path from 'path';
import * as main from '../src/main';
import * as im from '../src/installer';
import * as sys from '../src/system';
import {ITypedResponse} from '@actions/http-client/interfaces';
let goJsonData = require('./data/golang-dl.json');
let matchers = require('../matchers.json');
let matcherPattern = matchers.problemMatcher[0].pattern[0];
let matcherRegExp = new RegExp(matcherPattern.regexp);
describe('setup-go', () => {
let inputs = {} as any;
@ -25,6 +27,11 @@ describe('setup-go', () => {
let dlSpy: jest.SpyInstance;
let exSpy: jest.SpyInstance;
let cacheSpy: jest.SpyInstance;
let dbgSpy: jest.SpyInstance;
let whichSpy: jest.SpyInstance;
let existsSpy: jest.SpyInstance;
let mkdirpSpy: jest.SpyInstance;
let execSpy: jest.SpyInstance;
beforeEach(() => {
// @actions/core
@ -32,12 +39,13 @@ describe('setup-go', () => {
inSpy = jest.spyOn(core, 'getInput');
inSpy.mockImplementation(name => inputs[name]);
// node 'os'
// node
os = {};
platSpy = jest.spyOn(osm, 'platform');
platSpy.mockImplementation(() => os['platform']);
archSpy = jest.spyOn(osm, 'arch');
archSpy.mockImplementation(() => os['arch']);
execSpy = jest.spyOn(cp, 'execSync');
// @actions/tool-cache
findSpy = jest.spyOn(tc, 'find');
@ -46,9 +54,15 @@ describe('setup-go', () => {
cacheSpy = jest.spyOn(tc, 'cacheDir');
getSpy = jest.spyOn(im, 'getVersions');
// io
whichSpy = jest.spyOn(io, 'which');
existsSpy = jest.spyOn(fs, 'existsSync');
mkdirpSpy = jest.spyOn(io, 'mkdirP');
// writes
cnSpy = jest.spyOn(process.stdout, 'write');
logSpy = jest.spyOn(console, 'log');
dbgSpy = jest.spyOn(main, '_debug');
getSpy.mockImplementation(() => <im.IGoVersion[]>goJsonData);
cnSpy.mockImplementation(line => {
// uncomment to debug
@ -58,11 +72,16 @@ describe('setup-go', () => {
// uncomment to debug
// process.stderr.write('log:' + line + '\n');
});
dbgSpy.mockImplementation(msg => {
// uncomment to see debug output
// process.stderr.write(msg + '\n');
});
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
//jest.restoreAllMocks();
});
afterAll(async () => {}, 100000);
@ -164,7 +183,7 @@ describe('setup-go', () => {
let toolPath = path.normalize('/cache/go/1.13.0/x64');
findSpy.mockImplementation(() => toolPath);
await run();
await main.run();
expect(logSpy).toHaveBeenCalledWith(`Setup go stable version spec 1.13.0`);
});
@ -176,7 +195,7 @@ describe('setup-go', () => {
let toolPath = path.normalize('/cache/go/1.13.0/x64');
findSpy.mockImplementation(() => toolPath);
await run();
await main.run();
expect(logSpy).toHaveBeenCalledWith(`Setup go stable version spec 1.13.0`);
});
@ -186,7 +205,7 @@ describe('setup-go', () => {
let toolPath = path.normalize('/cache/go/1.13.0/x64');
findSpy.mockImplementation(() => toolPath);
await run();
await main.run();
let expPath = path.join(toolPath, 'bin');
});
@ -195,7 +214,7 @@ describe('setup-go', () => {
inputs['go-version'] = '1.13.0';
let toolPath = path.normalize('/cache/go/1.13.0/x64');
findSpy.mockImplementation(() => toolPath);
await run();
await main.run();
let expPath = path.join(toolPath, 'bin');
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
@ -208,7 +227,7 @@ describe('setup-go', () => {
findSpy.mockImplementation(() => {
throw new Error(errMsg);
});
await run();
await main.run();
expect(cnSpy).toHaveBeenCalledWith('::error::' + errMsg + osm.EOL);
});
@ -223,7 +242,7 @@ describe('setup-go', () => {
let toolPath = path.normalize('/cache/go/1.13.0/x64');
exSpy.mockImplementation(() => '/some/other/temp/path');
cacheSpy.mockImplementation(() => toolPath);
await run();
await main.run();
let expPath = path.join(toolPath, 'bin');
@ -239,7 +258,7 @@ describe('setup-go', () => {
inputs['go-version'] = '9.99.9';
findSpy.mockImplementation(() => '');
await run();
await main.run();
expect(cnSpy).toHaveBeenCalledWith(
`::error::Could not find a version that satisfied version spec: 9.99.9${osm.EOL}`
@ -257,7 +276,7 @@ describe('setup-go', () => {
dlSpy.mockImplementation(() => {
throw new Error(errMsg);
});
await run();
await main.run();
expect(cnSpy).toHaveBeenCalledWith(
`::error::Failed to download version 1.13.1: Error: ${errMsg}${osm.EOL}`
@ -273,13 +292,133 @@ describe('setup-go', () => {
findSpy.mockImplementation(() => '');
getSpy.mockImplementation(() => null);
await run();
await main.run();
expect(cnSpy).toHaveBeenCalledWith(
`::error::Failed to download version 1.13.1: Error: golang download url did not return results${osm.EOL}`
);
});
it('does not add BIN if go is not in path', async () => {
whichSpy.mockImplementation(async () => {
return '';
});
let added = await main.addBinToPath();
expect(added).toBeFalsy();
});
it('adds bin if dir not exists', async () => {
whichSpy.mockImplementation(async () => {
return '/usr/local/go/bin/go';
});
execSpy.mockImplementation(() => {
return '/Users/testuser/go';
});
mkdirpSpy.mockImplementation(async () => {});
existsSpy.mockImplementation(path => {
return false;
});
let added = await main.addBinToPath();
expect(added).toBeTruthy;
});
interface Annotation {
file: string;
line: number;
column: number;
message: string;
}
//
// problem matcher regex pattern tests
function testMatch(line: string): Annotation {
let annotation = <Annotation>{};
let match = matcherRegExp.exec(line);
if (match) {
annotation.line = parseInt(match[matcherPattern.line], 10);
annotation.column = parseInt(match[matcherPattern.column], 10);
annotation.file = match[matcherPattern.file].trim();
annotation.message = match[matcherPattern.message].trim();
}
return annotation;
}
it('matches on relative unix path', async () => {
let line = './main.go:13:2: undefined: fmt.Printl';
let annotation = testMatch(line);
expect(annotation).toBeDefined();
expect(annotation.line).toBe(13);
expect(annotation.column).toBe(2);
expect(annotation.file).toBe('./main.go');
expect(annotation.message).toBe('undefined: fmt.Printl');
});
it('matches on unix path up the tree', async () => {
let line = '../main.go:13:2: undefined: fmt.Printl';
let annotation = testMatch(line);
expect(annotation).toBeDefined();
expect(annotation.line).toBe(13);
expect(annotation.column).toBe(2);
expect(annotation.file).toBe('../main.go');
expect(annotation.message).toBe('undefined: fmt.Printl');
});
it('matches on rooted unix path', async () => {
let line = '/assert.go:4:1: missing return at end of function';
let annotation = testMatch(line);
expect(annotation).toBeDefined();
expect(annotation.line).toBe(4);
expect(annotation.column).toBe(1);
expect(annotation.file).toBe('/assert.go');
expect(annotation.message).toBe('missing return at end of function');
});
it('matches on unix path with spaces', async () => {
let line = ' ./assert.go:5:2: missing return at end of function ';
let annotation = testMatch(line);
expect(annotation).toBeDefined();
expect(annotation.line).toBe(5);
expect(annotation.column).toBe(2);
expect(annotation.file).toBe('./assert.go');
expect(annotation.message).toBe('missing return at end of function');
});
it('matches on unix path with tabs', async () => {
let line = '\t./assert.go:5:2: missing return at end of function ';
let annotation = testMatch(line);
expect(annotation).toBeDefined();
expect(annotation.line).toBe(5);
expect(annotation.column).toBe(2);
expect(annotation.file).toBe('./assert.go');
expect(annotation.message).toBe('missing return at end of function');
});
it('matches on relative windows path', async () => {
let line = '.\\main.go:13:2: undefined: fmt.Printl';
let annotation = testMatch(line);
expect(annotation).toBeDefined();
expect(annotation.line).toBe(13);
expect(annotation.column).toBe(2);
expect(annotation.file).toBe('.\\main.go');
expect(annotation.message).toBe('undefined: fmt.Printl');
});
it('matches on windows path up the tree', async () => {
let line = '..\\main.go:13:2: undefined: fmt.Printl';
let annotation = testMatch(line);
expect(annotation).toBeDefined();
expect(annotation.line).toBe(13);
expect(annotation.column).toBe(2);
expect(annotation.file).toBe('..\\main.go');
expect(annotation.message).toBe('undefined: fmt.Printl');
});
// 1.13.1 => 1.13.1
// 1.13 => 1.13.0
// 1.10beta1 => 1.10.0-beta1, 1.10rc1 => 1.10.0-rc1

44
dist/index.js vendored
View File

@ -1274,14 +1274,17 @@ var __importStar = (this && this.__importStar) || function (mod) {
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(__webpack_require__(470));
const io = __importStar(__webpack_require__(1));
const tc = __importStar(__webpack_require__(533));
const installer = __importStar(__webpack_require__(749));
const path = __importStar(__webpack_require__(622));
const cp = __importStar(__webpack_require__(129));
const fs = __importStar(__webpack_require__(747));
const path_1 = __importDefault(__webpack_require__(622));
const child_process_1 = __importDefault(__webpack_require__(129));
const fs_1 = __importDefault(__webpack_require__(747));
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
@ -1303,7 +1306,7 @@ function run() {
}
if (installDir) {
core.exportVariable('GOROOT', installDir);
core.addPath(path.join(installDir, 'bin'));
core.addPath(path_1.default.join(installDir, 'bin'));
console.log('Added go to the path');
let added = addBinToPath();
core.debug(`add bin ${added}`);
@ -1313,8 +1316,16 @@ function run() {
}
}
// add problem matchers
const matchersPath = path.join(__dirname, '..', 'matchers.json');
const matchersPath = path_1.default.join(__dirname, '..', 'matchers.json');
console.log(`##[add-matcher]${matchersPath}`);
// output the version actually being used
let goPath = yield io.which('go');
let goVersion = (child_process_1.default.execSync(`${goPath} version`) || '').toString();
console.log(goVersion);
core.startGroup('go env');
let goEnv = (child_process_1.default.execSync(`${goPath} env`) || '').toString();
console.log(goEnv);
core.endGroup();
}
catch (error) {
core.setFailed(error.message);
@ -1326,23 +1337,23 @@ function addBinToPath() {
return __awaiter(this, void 0, void 0, function* () {
let added = false;
let g = yield io.which('go');
core.debug(`which go :${g}:`);
_debug(`which go :${g}:`);
if (!g) {
core.debug('go not in the path');
_debug('go not in the path');
return added;
}
let buf = cp.execSync('go env GOPATH');
let buf = child_process_1.default.execSync('go env GOPATH');
if (buf) {
let gp = buf.toString().trim();
core.debug(`go env GOPATH :${gp}:`);
if (!fs.existsSync(gp)) {
_debug(`go env GOPATH :${gp}:`);
if (!fs_1.default.existsSync(gp)) {
// some of the hosted images have go install but not profile dir
core.debug(`creating ${gp}`);
_debug(`creating ${gp}`);
io.mkdirP(gp);
}
let bp = path.join(gp, 'bin');
if (!fs.existsSync(bp)) {
core.debug(`creating ${bp}`);
let bp = path_1.default.join(gp, 'bin');
if (!fs_1.default.existsSync(bp)) {
_debug(`creating ${bp}`);
io.mkdirP(bp);
}
core.addPath(bp);
@ -1351,6 +1362,11 @@ function addBinToPath() {
return added;
});
}
exports.addBinToPath = addBinToPath;
function _debug(message) {
core.debug(message);
}
exports._debug = _debug;
/***/ }),

View File

@ -4,11 +4,11 @@
"owner": "go",
"pattern": [
{
"regexp": "^([^:]*: )?((.:)?[^:]*):(\\d+)(:(\\d+))?: (.*)$",
"file": 2,
"line": 4,
"column": 6,
"message": 7
"regexp": "^\\s*(\\.{0,2}[\\/\\\\].+\\.go):(?:(\\d+):(\\d+):)? (.*)",
"file": 1,
"line": 2,
"column": 3,
"message": 4
}
]
}

View File

@ -2,9 +2,9 @@ import * as core from '@actions/core';
import * as io from '@actions/io';
import * as tc from '@actions/tool-cache';
import * as installer from './installer';
import * as path from 'path';
import * as cp from 'child_process';
import * as fs from 'fs';
import path from 'path';
import cp from 'child_process';
import fs from 'fs';
export async function run() {
try {
@ -50,33 +50,43 @@ export async function run() {
// add problem matchers
const matchersPath = path.join(__dirname, '..', 'matchers.json');
console.log(`##[add-matcher]${matchersPath}`);
// output the version actually being used
let goPath = await io.which('go');
let goVersion = (cp.execSync(`${goPath} version`) || '').toString();
console.log(goVersion);
core.startGroup('go env');
let goEnv = (cp.execSync(`${goPath} env`) || '').toString();
console.log(goEnv);
core.endGroup();
} catch (error) {
core.setFailed(error.message);
}
}
async function addBinToPath(): Promise<boolean> {
export async function addBinToPath(): Promise<boolean> {
let added = false;
let g = await io.which('go');
core.debug(`which go :${g}:`);
_debug(`which go :${g}:`);
if (!g) {
core.debug('go not in the path');
_debug('go not in the path');
return added;
}
let buf = cp.execSync('go env GOPATH');
if (buf) {
let gp = buf.toString().trim();
core.debug(`go env GOPATH :${gp}:`);
_debug(`go env GOPATH :${gp}:`);
if (!fs.existsSync(gp)) {
// some of the hosted images have go install but not profile dir
core.debug(`creating ${gp}`);
_debug(`creating ${gp}`);
io.mkdirP(gp);
}
let bp = path.join(gp, 'bin');
if (!fs.existsSync(bp)) {
core.debug(`creating ${bp}`);
_debug(`creating ${bp}`);
io.mkdirP(bp);
}
@ -85,3 +95,7 @@ async function addBinToPath(): Promise<boolean> {
}
return added;
}
export function _debug(message: string) {
core.debug(message);
}