Skip to content

Commit

Permalink
Remove deasync (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-zaytsev authored May 22, 2024
1 parent efde24b commit 79bc298
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 162 deletions.
4 changes: 4 additions & 0 deletions node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 4.x

## 4.12.1

- Remove deasync from task-lib - [#1038](https://github.com/microsoft/azure-pipelines-task-lib/pull/1038)

### 4.12.0

- Added audit action for task.issue [#1033](https://github.com/microsoft/azure-pipelines-task-lib/pull/1033)
Expand Down
35 changes: 11 additions & 24 deletions node/buildutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@ var os = require('os');
var path = require('path');
var process = require('process');
var admZip = require('adm-zip');
var deasync = require('deasync')
const Downloader = require("nodejs-file-downloader");

var downloadPath = path.join(__dirname, '_download');
var testPath = path.join(__dirname, '_test');

exports.run = function (cl) {
var run = function (cl) {
console.log('> ' + cl);
var rc = exec(cl).code;
if (rc !== 0) {
echo('Exec failed with rc ' + rc);
exit(rc);
}
}
var run = exports.run;


exports.run = run;

const getExternalsAsync = async () => {
if (process.env['TF_BUILD']) {
Expand Down Expand Up @@ -50,8 +47,11 @@ const getExternalsAsync = async () => {
addPath(path.join(nodeArchivePath, 'node-' + nodeVersion + '-linux-x64', 'bin'));
break;
case 'win32':
var nodeExePath = await downloadFileAsync(nodeUrl + '/' + nodeVersion + '/win-x64/node.exe');
var nodeLibPath = await downloadFileAsync(nodeUrl + '/' + nodeVersion + '/win-x64/node.lib');
var [nodeExePath, nodeLibPath] = await Promise.all([
downloadFileAsync(nodeUrl + '/' + nodeVersion + '/win-x64/node.exe'),
downloadFileAsync(nodeUrl + '/' + nodeVersion + '/win-x64/node.lib')
]);

var nodeDirectory = path.join(testPath, 'node');
mkdir('-p', nodeDirectory);
cp(nodeExePath, path.join(nodeDirectory, 'node.exe'));
Expand All @@ -64,18 +64,6 @@ const getExternalsAsync = async () => {
exports.getExternalsAsync = getExternalsAsync



/**
* @deprecated This method uses library which is not prefered to use on production
*/
exports.getExternals = function () {
var result = false;
getExternalsAsync().then(t => result = true);
deasync.loopWhile(function () { return !result });
return result;
}


var downloadFileAsync = async function (url, fileName) {
// validate parameters
if (!url) {
Expand All @@ -84,7 +72,7 @@ var downloadFileAsync = async function (url, fileName) {

// skip if already downloaded
var scrubbedUrl = url.replace(/[/\:?]/g, '_');
if (fileName == undefined) {
if (fileName === undefined) {
fileName = scrubbedUrl;
}
var targetPath = path.join(downloadPath, 'file', fileName);
Expand All @@ -109,10 +97,9 @@ var downloadFileAsync = async function (url, fileName) {
fileName: fileName
});

const { fileName: downloadedFileName } = await downloader.download(); // Downloader.download() resolves with some useful properties.
const { filePath } = await downloader.download(); // Downloader.download() resolves with some useful properties.
fs.writeFileSync(marker, '');
return downloadedFileName;

return filePath;
};


Expand All @@ -123,7 +110,7 @@ var downloadArchiveAsync = async function (url, fileName) {

// skip if already downloaded and extracted
var scrubbedUrl = url.replace(/[\/\\:?]/g, '_');
if (fileName != undefined) {
if (fileName !== undefined) {
scrubbedUrl = fileName;
}
var targetPath = path.join(downloadPath, 'archive', scrubbedUrl);
Expand Down
14 changes: 12 additions & 2 deletions node/make.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ target.build = function() {
rm(path.join(buildPath, 'index.*'));
}

target.test = function() {
target.test = async function() {
target.build();

buildutils.getExternals();
await buildutils.getExternalsAsync();
run('tsc -p ./test');
cp('-Rf', rp('test/scripts'), testPath);
cp('-Rf', rp('test/fakeTasks'), testPath);
Expand All @@ -65,3 +65,13 @@ target.loc = function() {
var enContents = JSON.stringify(strings, null, 2);
fs.writeFileSync(path.join(strPath, 'resources.resjson'), enContents)
}

process.on('uncaughtException', err => {
console.error(`Uncaught exception: ${err.message}`);
console.debug(err.stack);
});

process.on('unhandledRejection', err => {
console.error(`Unhandled rejection: ${err.message}`);
console.debug(err.stack);
});
37 changes: 14 additions & 23 deletions node/mock-test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import cp = require('child_process');
import fs = require('fs');
import ncp = require('child_process');
import os = require('os');
import path = require('path');
import cmdm = require('./taskcommand');
import shelljs = require('shelljs');
import deasync = require('deasync')
const Downloader = require("nodejs-file-downloader");

const COMMAND_TAG = '[command]';
Expand All @@ -20,7 +18,6 @@ export class MockTestRunner {

this._taskJsonPath = taskJsonPath || '';
this._testPath = testPath;
this.nodePath = this.getNodePathSync();
}

private _testPath = '';
Expand All @@ -34,14 +31,20 @@ export class MockTestRunner {
public errorIssues: string[] = [];
public warningIssues: string[] = [];

public async LoadAsync(testPath: string, taskJsonPath?: string): Promise<MockTestRunner> {
public async LoadAsync(testPath?: string, taskJsonPath?: string): Promise<MockTestRunner> {
return new Promise(async (resolve, reject) => {
if (this.nodePath != '') {
resolve(this);
return;
}
this._taskJsonPath = taskJsonPath || '';
this._testPath = testPath;
if (testPath) {
this._testPath = testPath;
}

if (taskJsonPath) {
this._taskJsonPath = taskJsonPath;
}

this.nodePath = await this.getNodePath();
resolve(this)
})
Expand Down Expand Up @@ -82,7 +85,11 @@ export class MockTestRunner {
let nodePath = this.nodePath;
if (nodeVersion) {
nodePath = await this.getNodePath(nodeVersion);
} else if (!nodePath) {
nodePath = await this.getNodePath();
this.nodePath = nodePath;
}

let spawn = cp.spawnSync(nodePath, [this._testPath]);

// Clean environment
Expand Down Expand Up @@ -155,15 +162,6 @@ export class MockTestRunner {
}
}

/**
* @deprecated This method uses library which is not prefered to use on production
*/
public run(nodeVersion?: number): void {
let completeExecution = false;
this.runAsync(nodeVersion).then(t => completeExecution = true)
deasync.loopWhile(function () { return !completeExecution })
}

// Returns a path to node.exe with the correct version for this task (based on if its node10 or node)
private async getNodePath(nodeVersion?: number): Promise<string> {
const version: number = nodeVersion || this.getNodeVersion();
Expand Down Expand Up @@ -195,13 +193,6 @@ export class MockTestRunner {
}
}

private getNodePathSync(nodeVersion?: number): string {
let nodePath = ''
this.getNodePath(nodeVersion).then(t => nodePath = t);
deasync.loopWhile(function () { return nodePath == ''; });
return nodePath
}

// Determines the correct version of node to use based on the contents of the task's task.json. Defaults to Node 16.
private getNodeVersion(): number {
const taskJsonPath: string = this.getTaskJsonPath();
Expand Down Expand Up @@ -321,7 +312,7 @@ export class MockTestRunner {
const originalCwd: string = process.cwd();
process.chdir(downloadDestination);
try {
ncp.execSync(`tar -xzf "${path.join(downloadDestination, tarGzName)}"`);
cp.execSync(`tar -xzf "${path.join(downloadDestination, tarGzName)}"`);
}
catch {
throw new Error('Failed to unzip node tar.gz from ' + url);
Expand Down
35 changes: 4 additions & 31 deletions node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azure-pipelines-task-lib",
"version": "4.12.0",
"version": "4.12.1",
"description": "Azure Pipelines Task SDK",
"main": "./task.js",
"typings": "./task.d.ts",
Expand Down Expand Up @@ -28,7 +28,6 @@
"homepage": "https://github.com/Microsoft/azure-pipelines-task-lib",
"dependencies": {
"adm-zip": "^0.5.10",
"deasync": "^0.1.28",
"minimatch": "3.0.5",
"nodejs-file-downloader": "^4.11.1",
"q": "^1.5.1",
Expand Down
24 changes: 17 additions & 7 deletions node/test/mocktests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,34 +298,44 @@ describe('Mock Tests', function () {
assert.equal(numStdErrCalls, 1);
})

it('MockTest handles node 6 tasks correctly', function (done) {
it('MockTest handles node 6 tasks correctly', async function () {
this.timeout(30000);
const runner = new mtm.MockTestRunner(path.join(__dirname, 'fakeTasks', 'node6task', 'entry.js'));
await runner.LoadAsync();
const nodePath = runner.nodePath;
assert(nodePath, 'node path should have been correctly set');
const version = ncp.execSync(nodePath + ' -v').toString().trim();
assert(semver.satisfies(version, '6.x'), 'Downloaded node version should be Node 6 instead of ' + version);
done();
})

it('MockTest handles node 10 tasks correctly', function (done) {
it('MockTest handles node 10 tasks correctly', async function () {
this.timeout(30000);
const runner = new mtm.MockTestRunner(path.join(__dirname, 'fakeTasks', 'node10task', 'entry.js'));
const runner = new mtm.MockTestRunner();
await runner.LoadAsync(path.join(__dirname, 'fakeTasks', 'node10task', 'entry.js'));
const nodePath = runner.nodePath;
assert(nodePath, 'node path should have been correctly set');
const version = ncp.execSync(nodePath + ' -v').toString().trim();
assert(semver.satisfies(version, '10.x'), 'Downloaded node version should be Node 10 instead of ' + version);
done();
})

it('MockTest handles node 16 tasks correctly', function (done) {
it('MockTest handles node 16 tasks correctly', async function () {
this.timeout(30000);
const runner = new mtm.MockTestRunner(path.join(__dirname, 'fakeTasks', 'node16task', 'entry.js'));
await runner.LoadAsync();
const nodePath = runner.nodePath;
assert(nodePath, 'node path should have been correctly set');
const version = ncp.execSync(nodePath + ' -v').toString().trim();
assert(semver.satisfies(version, '16.x'), 'Downloaded node version should be Node 16 instead of ' + version);
done();
})

it('MockTest handles node tasks correctly by async call', async function() {
this.timeout(30000);
const runner = await (new mtm.MockTestRunner).LoadAsync(path.join(__dirname, 'fakeTasks', 'node16task', 'entry.js'));
const nodePath = runner.nodePath;
assert(nodePath, 'node path should have been correctly set');
const version = ncp.execSync(nodePath + ' -v').toString().trim();
assert(semver.satisfies(version, '16.x'), 'Downloaded node version should be Node 16 instead of ' + version);
await Promise.resolve()
})

it('MockTest handles node tasks correctly by async call', async () => {
Expand Down
Loading

0 comments on commit 79bc298

Please sign in to comment.