Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasrockhu committed Apr 16, 2021
1 parent 83cbbf8 commit 444b352
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
20 changes: 9 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49215,6 +49215,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
}
};
exports.__esModule = true;
exports.retrieveChecksum = void 0;
var crypto = __webpack_require__(417);
var core = __webpack_require__(470);
var request = __webpack_require__(335);
Expand All @@ -49233,7 +49234,7 @@ var validateUploader = function (body) { return __awaiter(void 0, void 0, void 0
case 1:
if (!(_i < _a.length)) return [3 /*break*/, 4];
i = _a[_i];
return [4 /*yield*/, retrieveChecksum(version, i)];
return [4 /*yield*/, exports.retrieveChecksum(version, i)];
case 2:
publicChecksum = _b.sent();
uploaderChecksum = calculateChecksum(body, i);
Expand All @@ -49252,30 +49253,27 @@ var validateUploader = function (body) { return __awaiter(void 0, void 0, void 0
});
}); };
var retrieveChecksum = function (version, encryption) { return __awaiter(void 0, void 0, void 0, function () {
var url, response, err_1;
var url, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
url = "https://raw.githubusercontent.com/codecov/codecov-bash/" + version + "/SHA" + encryption + "SUM";
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, request({
maxAttempts: 10,
timeout: 3000,
url: url,
})];
case 2:
case 1:
response = _a.sent();
if (response.statusCode != 200) {
core.warning("Codecov could not retrieve checksum SHA" + encryption + " at " + url);
return [2 /*return*/, ''];
}
return [2 /*return*/, response.body];
case 3:
err_1 = _a.sent();
core.warning("Codecov could not retrieve checksum SHA" + encryption + " at " + url);
return [2 /*return*/, false];
case 4: return [2 /*return*/];
}
});
}); };
exports.retrieveChecksum = retrieveChecksum;
var calculateChecksum = function (body, i) {
var shasum = crypto.createHash("sha" + i);
shasum.update(body);
Expand Down
13 changes: 9 additions & 4 deletions src/validate.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import validateUploader from './validate';
import validateUploader, {retrieveChecksum} from './validate';

const request = require('requestretry');

Expand All @@ -16,19 +16,24 @@ const bashScript = (async () => {
}
});

test('validChecksums', async () => {
test('valid checksums', async () => {
const valid = await validateUploader(await bashScript());
expect(valid).toBeTruthy();
});

test('invalidChecksums', async () => {
test('invalid checksums', async () => {
const script = await bashScript();
const valid = await validateUploader(script.substring(0, script.length - 1));
expect(valid).toBeFalsy();
});

test('invalidVersion', async () => {
test('invalid script version', async () => {
const script = await bashScript();
const valid = await validateUploader(script.substring(0, 20));
expect(valid).toBeFalsy();
});

test('invalid public checksum file', async () => {
const checksum = await retrieveChecksum('foo', 'bar');
expect(checksum).toBeFalsy();
});
20 changes: 10 additions & 10 deletions src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ const validateUploader = async (body) => {
return true;
};

const retrieveChecksum = async (version, encryption) => {
export const retrieveChecksum = async (version, encryption) => {
const url = `https://raw.githubusercontent.com/codecov/codecov-bash/${version}/SHA${encryption}SUM`;
try {
const response = await request({
maxAttempts: 10,
timeout: 3000,
url: url,
});
return response.body;
} catch (err) {
const response = await request({
maxAttempts: 10,
timeout: 3000,
url: url,
});

if (response.statusCode != 200) {
core.warning(
`Codecov could not retrieve checksum SHA${encryption} at ${url}`,
);
return false;
return '';
}
return response.body;
};

const calculateChecksum = (body, i) => {
Expand Down

0 comments on commit 444b352

Please sign in to comment.