Skip to content

Commit

Permalink
fix: remove custom and add global error handling (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
byCedric authored May 8, 2020
1 parent cb49096 commit a4955da
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 64 deletions.
43 changes: 16 additions & 27 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20413,15 +20413,9 @@ function authenticate(username, password) {
const bin = process.platform === 'win32'
? 'expo.cmd'
: 'expo';
try {
yield cli.exec(bin, ['login', `--username=${username}`], {
env: Object.assign(Object.assign({}, process.env), { EXPO_CLI_PASSWORD: password }),
});
}
catch (error) {
core.setFailed(error);
throw error;
}
yield cli.exec(bin, ['login', `--username=${username}`], {
env: Object.assign(Object.assign({}, process.env), { EXPO_CLI_PASSWORD: password }),
});
});
}
exports.authenticate = authenticate;
Expand Down Expand Up @@ -25945,9 +25939,17 @@ Promise._SomePromiseArray = SomePromiseArray;

"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 core = __importStar(__webpack_require__(470));
const run_1 = __webpack_require__(180);
run_1.run();
run_1.run().catch(core.setFailed);


/***/ }),
Expand Down Expand Up @@ -54205,7 +54207,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const lib_1 = __webpack_require__(484);
const core = __importStar(__webpack_require__(470));
const toolCache = __importStar(__webpack_require__(533));
const path_1 = __importDefault(__webpack_require__(622));
const os_1 = __importDefault(__webpack_require__(87));
Expand Down Expand Up @@ -54242,15 +54243,9 @@ function fromRemoteCache(version, packager, customCacheKey) {
// see: https://github.com/actions/toolkit/blob/8a4134761f09d0d97fb15f297705fd8644fef920/packages/tool-cache/src/tool-cache.ts#L401
const target = path_1.default.join(process.env['RUNNER_TOOL_CACHE'] || '', 'expo-cli', version, os_1.default.arch());
const cacheKey = customCacheKey || getRemoteKey(version, packager);
try {
const hit = yield lib_1.restoreCache(target, cacheKey, cacheKey);
if (hit) {
return target;
}
}
catch (error) {
core.setFailed(error);
throw error;
const hit = yield lib_1.restoreCache(target, cacheKey, cacheKey);
if (hit) {
return target;
}
});
}
Expand All @@ -54262,13 +54257,7 @@ exports.fromRemoteCache = fromRemoteCache;
function toRemoteCache(source, version, packager, customCacheKey) {
return __awaiter(this, void 0, void 0, function* () {
const cacheKey = customCacheKey || getRemoteKey(version, packager);
try {
yield lib_1.saveCache(source, cacheKey);
}
catch (error) {
core.setFailed(error);
throw error;
}
yield lib_1.saveCache(source, cacheKey);
});
}
exports.toRemoteCache = toRemoteCache;
Expand Down
20 changes: 4 additions & 16 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { restoreCache, saveCache } from '@actions/cache/lib';
import * as core from '@actions/core';
import * as toolCache from '@actions/tool-cache';
import path from 'path';
import os from 'os';
Expand Down Expand Up @@ -32,16 +31,10 @@ export async function fromRemoteCache(version: string, packager: string, customC
// see: https://github.com/actions/toolkit/blob/8a4134761f09d0d97fb15f297705fd8644fef920/packages/tool-cache/src/tool-cache.ts#L401
const target = path.join(process.env['RUNNER_TOOL_CACHE'] || '', 'expo-cli', version, os.arch());
const cacheKey = customCacheKey || getRemoteKey(version, packager);
const hit = await restoreCache(target, cacheKey, cacheKey);

try {
const hit = await restoreCache(target, cacheKey, cacheKey);

if (hit) {
return target;
}
} catch (error) {
core.setFailed(error);
throw error;
if (hit) {
return target;
}
}

Expand All @@ -52,12 +45,7 @@ export async function fromRemoteCache(version: string, packager: string, customC
export async function toRemoteCache(source: string, version: string, packager: string, customCacheKey?: string) {
const cacheKey = customCacheKey || getRemoteKey(version, packager);

try {
await saveCache(source, cacheKey);
} catch (error) {
core.setFailed(error);
throw error;
}
await saveCache(source, cacheKey);
}

/**
Expand Down
17 changes: 6 additions & 11 deletions src/expo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,10 @@ export async function authenticate(username: string, password: string) {
? 'expo.cmd'
: 'expo';

try {
await cli.exec(bin, ['login', `--username=${username}`], {
env: {
...process.env,
EXPO_CLI_PASSWORD: password,
},
});
} catch (error) {
core.setFailed(error);
throw error;
}
await cli.exec(bin, ['login', `--username=${username}`], {
env: {
...process.env,
EXPO_CLI_PASSWORD: password,
},
});
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as core from '@actions/core';
import { run } from './run';

run();
run().catch(core.setFailed);
5 changes: 0 additions & 5 deletions tests/cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as remoteCache from '@actions/cache/lib';
import * as core from '@actions/core';
import * as toolCache from '@actions/tool-cache';
import os from 'os';
import * as cache from '../src/cache';
Expand Down Expand Up @@ -29,7 +28,6 @@ describe('toLocalCache', () => {

describe('fromRemoteCache', () => {
const spy = {
fail: jest.spyOn(core, 'setFailed').mockImplementation(),
restore: jest.spyOn(remoteCache, 'restoreCache').mockImplementation(),
};

Expand Down Expand Up @@ -70,13 +68,11 @@ describe('fromRemoteCache', () => {
const error = new Error('Remote cache restore failed');
spy.restore.mockRejectedValueOnce(error);
await expect(cache.fromRemoteCache('3.20.1', 'yarn')).rejects.toBe(error);
expect(spy.fail).toBeCalledWith(error);
});
});

describe('toRemoteCache', () => {
const spy = {
fail: jest.spyOn(core, 'setFailed').mockImplementation(),
save: jest.spyOn(remoteCache, 'saveCache').mockImplementation(),
};

Expand All @@ -97,6 +93,5 @@ describe('toRemoteCache', () => {
const error = new Error('Remote cache save failed');
spy.save.mockRejectedValueOnce(error);
await expect(cache.toRemoteCache('/local/path', '3.20.1', 'yarn')).rejects.toBe(error);
expect(spy.fail).toBeCalledWith(error);
});
});
4 changes: 0 additions & 4 deletions tests/expo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@ describe('authenticate', () => {
const spy = {
exec: jest.spyOn(cli, 'exec').mockImplementation(),
info: jest.spyOn(core, 'info').mockImplementation(),
fail: jest.spyOn(core, 'setFailed').mockImplementation(),
};

it('skips authentication without credentials', async () => {
await expo.authenticate('', '');
expect(spy.exec).not.toBeCalled();
expect(spy.fail).not.toBeCalled();
expect(spy.info).toBeCalled();
});

it('skips authentication without password', async () => {
await expo.authenticate('bycedric', '');
expect(spy.exec).not.toBeCalled();
expect(spy.fail).not.toBeCalled();
expect(spy.info).toBeCalled();
});

Expand Down Expand Up @@ -51,6 +48,5 @@ describe('authenticate', () => {
const error = new Error('Invalid username/password. Please try again.');
spy.exec.mockRejectedValue(error);
await expect(expo.authenticate('bycedric', 'incorrect')).rejects.toBe(error);
expect(spy.fail).toBeCalledWith(error);
});
});

0 comments on commit a4955da

Please sign in to comment.