Skip to content

Commit 5c985ad

Browse files
Fix findCommonParent behaviour when NativeLibrariestPath targets a single .so file (#12395)
* Fix findCommonParent behaviour when NativeLibrariestPath targets to single .so file * Add Unit tests for utils * Fix tests * Rename unitTest to assertByExitCode * Add timeout to spawnSync * Add test for unit tests exit code Co-authored-by: Alex Kharchenko <[email protected]>
1 parent b414a7a commit 5c985ad

10 files changed

+110
-2
lines changed

Tasks/AppCenterDistributeV3/Tests/L0.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import * as path from 'path';
66
import * as assert from 'assert';
77
import * as ttm from 'vsts-task-lib/mock-test';
8+
import { utilsUnitTests } from './L0UtilsUnitTests';
9+
import { spawnSync } from 'child_process';
810

911
describe('AppCenterDistribute L0 Suite', function () {
1012
before(() => {
@@ -310,4 +312,14 @@ describe('AppCenterDistribute L0 Suite', function () {
310312
tr.run();
311313
assert(tr.succeeded, 'task should have succeeded');
312314
});
315+
316+
describe("Unit tests", function() {
317+
it('Negative path: should keep exit code', function() {
318+
const tp = path.join(__dirname, 'UnitTests', 'UnitTestsExitCodeIsKept.js');
319+
const spawn = spawnSync('node', [tp], {timeout: 2000});
320+
assert.equal(spawn.status, 1);
321+
});
322+
323+
utilsUnitTests();
324+
});
313325
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as path from 'path';
2+
import * as assert from 'assert';
3+
import {spawnSync} from 'child_process';
4+
5+
export function utilsUnitTests() {
6+
describe('Utils unit tests', function () {
7+
describe('findCommonParent', function() {
8+
it('returns containing directory for single file', function() {
9+
const tp = path.join(__dirname, 'UnitTests', 'Utils', 'FindCommonParentForSingleFileReturnsContainingDirectory.js');
10+
const spawn = spawnSync('node', [tp], {timeout: 2000});
11+
assert.equal(spawn.status, 0);
12+
});
13+
14+
it('returns directory itself for single directory', function() {
15+
const tp = path.join(__dirname, 'UnitTests', 'Utils', 'FindCommonParentForSingleDirectoryReturnsDirectoryItself.js');
16+
const spawn = spawnSync('node', [tp], {timeout: 2000});
17+
assert.equal(spawn.status, 0);
18+
});
19+
20+
it('returns common parent for multiple values', function() {
21+
const tp = path.join(__dirname, 'UnitTests', 'Utils', 'FindCommonParentForMultipleFilesReturnsCorrectResult.js');
22+
const spawn = spawnSync('node', [tp], {timeout: 2000});
23+
assert.equal(spawn.status, 0);
24+
});
25+
});
26+
});
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as assert from "assert";
2+
3+
/**
4+
* Exit code is used to determine whether unit test passed or not.
5+
* When executing code requires vsts-task-lib somewhere it makes exit code = 0 regardless whether exception was thrown.
6+
* This helper allows to follow default NodeJS exit code behaviour when exception is thrown.
7+
*/
8+
export const assertByExitCode = {
9+
equal: (actual, expected) => wrapAssertWithExitCode(assert.equal, actual, expected),
10+
};
11+
12+
function wrapAssertWithExitCode(assert, ...args) {
13+
try {
14+
assert.apply(undefined, args);
15+
} catch (error) {
16+
process.exit(1);
17+
}
18+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { assertByExitCode } from "./TestHelpers";
2+
3+
// Verify that exit code doesn't overwritten
4+
assertByExitCode.equal(true, false);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import fs = require('fs');
2+
3+
import { findCommonParent } from '../../../utils';
4+
import { assertByExitCode } from '../TestHelpers';
5+
6+
fs.lstatSync = (s: string) => {
7+
const stat = {} as fs.Stats;
8+
stat.isFile = () => s.endsWith('.so');
9+
stat.isSymbolicLink = () => false;
10+
return stat;
11+
}
12+
const expectedParentPath = "/a/b";
13+
const actualParentPath = findCommonParent(["/a/b/c/x", "/a/b/c/y", "/a/b/d/z"]);
14+
assertByExitCode.equal(actualParentPath, expectedParentPath);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import fs = require('fs');
2+
3+
import { findCommonParent } from '../../../utils';
4+
import { assertByExitCode } from '../TestHelpers';
5+
6+
fs.lstatSync = (s: string) => {
7+
const stat = {} as fs.Stats;
8+
stat.isFile = () => s.endsWith('.so');
9+
stat.isSymbolicLink = () => false;
10+
return stat;
11+
}
12+
const expectedParentPath = "/a/b/c";
13+
const actualParentPath = findCommonParent(["/a/b/c"]);
14+
assertByExitCode.equal(actualParentPath, expectedParentPath);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import fs = require('fs');
2+
3+
import { findCommonParent } from '../../../utils';
4+
import { assertByExitCode } from '../TestHelpers';
5+
6+
fs.lstatSync = (s: string) => {
7+
const stat = {} as fs.Stats;
8+
stat.isFile = () => s.endsWith('.so');
9+
return stat;
10+
}
11+
const singleSoFilePath = "/a/b/c/symbol.so";
12+
const expectedParentPath = "/a/b/c";
13+
const actualParentPath = findCommonParent([singleSoFilePath]);
14+
assertByExitCode.equal(actualParentPath, expectedParentPath);

Tasks/AppCenterDistributeV3/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"author": "Microsoft Corporation",
1414
"version": {
1515
"Major": 3,
16-
"Minor": 159,
16+
"Minor": 160,
1717
"Patch": 0
1818
},
1919
"releaseNotes": "Added support for forwarding Android mapping to App Center Diagnostics. Added missing descriptions.",

Tasks/AppCenterDistributeV3/task.loc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"author": "Microsoft Corporation",
1414
"version": {
1515
"Major": 3,
16-
"Minor": 159,
16+
"Minor": 160,
1717
"Patch": 0
1818
},
1919
"releaseNotes": "ms-resource:loc.releaseNotes",

Tasks/AppCenterDistributeV3/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ export function findCommonParent(list: string[]): string {
203203
return null;
204204
}
205205

206+
if(list.length === 1 && fs.lstatSync(list[0]).isFile()) {
207+
// We expect that common parent for single file is its containing directory, not the file path itself
208+
return path.dirname(list[0]);
209+
}
210+
206211
let commonSegments: string[] = [];
207212
let parentPath: string = null;
208213

0 commit comments

Comments
 (0)