Skip to content

Commit

Permalink
[UseDotNetV2] Update task to node16 using codegen (#18676)
Browse files Browse the repository at this point in the history
- replaced package.json with package-lock.json for node16 task
- added agent-base for node16 version
- modified make.json to remove it from azure-arm-rest package
- replaced util.error to Error
- Fixed test since the handler was incorrect
- removed duplicate task lib
  • Loading branch information
DmitriiBobreshev authored Jul 24, 2023
1 parent f882f66 commit 0568d10
Show file tree
Hide file tree
Showing 105 changed files with 13,913 additions and 195 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Place files overridden for this config in this directory
536 changes: 536 additions & 0 deletions Tasks/UseDotNetV2/_buildConfigs/Node16/Tests/L0.ts

Large diffs are not rendered by default.

136 changes: 136 additions & 0 deletions Tasks/UseDotNetV2/_buildConfigs/Node16/Tests/globaljsonfetcherTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
"use strict";
import * as tl from 'azure-pipelines-task-lib/task';
import { GlobalJson } from "../globaljsonfetcher";
import { Buffer } from "buffer";
import { VersionInfo } from '../models';
import { Promise } from 'q';
import fs = require('fs');
var mockery = require('mockery');

const workingDir: string = "work/";
const validRootGlobalJson = workingDir + "global.json";
const rootVersionNumber = "2.2.2";
const workingSubDir = workingDir + "testdir/";
const validSubDirGlobalJson = workingSubDir + "global.json";
const subDirVersionNumber = "3.0.0-pre285754637";
const pathToEmptyGlobalJsonDir = workingDir + "empty/";
const pathToEmptyGlobalJson = pathToEmptyGlobalJsonDir + "global.json";

//setup mocks
mockery.enable({
useCleanCache: true,
warnOnReplace: false,
warnOnUnregistered: false
});

mockery.registerMock('azure-pipelines-task-lib/task', {
findMatch: function (path: string, searchPattern: string): string[] {
if (searchPattern != "**/global.json") {
return [];
}
if (path == workingDir) {
// If it's working dir subdir is included, because it is a child;
return [validRootGlobalJson, validSubDirGlobalJson];
}
if (path == workingSubDir) {
return [validSubDirGlobalJson];
}
if (path == pathToEmptyGlobalJsonDir) {
return [pathToEmptyGlobalJson];
}
return [];
},
loc: function (locString, ...param: string[]) { return tl.loc(locString, param); },
debug: function (message) { return tl.debug(message); }
});

mockery.registerMock('fs', {
...fs,
readFileSync: function (path: string): Buffer {
if (path == validRootGlobalJson) {
var globalJson = new GlobalJson(rootVersionNumber);
return Buffer.from(JSON.stringify(globalJson));
}
if (path == validSubDirGlobalJson) {
var globalJson = new GlobalJson(subDirVersionNumber);
return Buffer.from(JSON.stringify(globalJson));
}
if (path == pathToEmptyGlobalJson) {
return Buffer.from("");
}
return Buffer.from(null);
}
});

mockery.registerMock('./versionfetcher', {
DotNetCoreVersionFetcher: function (explicitVersioning: boolean = false) {
return {
getVersionInfo: function (versionSpec: string, vsVersionSpec: string, packageType: string, includePreviewVersions: boolean): Promise<VersionInfo> {
return Promise<VersionInfo>((resolve, reject) => {
resolve(new VersionInfo({
version: versionSpec,
files: [{
name: 'testfile.json',
hash: 'testhash',
url: 'testurl',
rid: 'testrid'
}],
"runtime-version": versionSpec,
"vs-version": vsVersionSpec
}, packageType));
});
}
}
}

});

// start test
import { globalJsonFetcher } from "../globaljsonfetcher";
if (process.env["__case__"] == "subdirAsRoot") {
let fetcher = new globalJsonFetcher(workingSubDir);
fetcher.GetVersions().then(versionInfos => {
if (versionInfos.length != 1) {
throw "GetVersions should return one result if one global.json is found.";
}
if (versionInfos[0].getVersion() != subDirVersionNumber) {
throw `GetVersions should return the version number that was inside the global.json. Expected: ${subDirVersionNumber} Actual: ${versionInfos[0].getVersion()}`;
}
if (versionInfos[0].getPackageType() != 'sdk') {
throw `GetVersions return always 'sdk' as package type. Actual: ${versionInfos[0].getPackageType()}`;
}
});
}

if (process.env["__case__"] == "rootAsRoot") {
let fetcher = new globalJsonFetcher(workingDir);
fetcher.GetVersions().then(versionInfos => {
if (versionInfos.length != 2) {
throw "GetVersions should return all global.json in a folder hierarchy result if multiple global.json are found.";
}
});
}

if (process.env["__case__"] == "invalidDir") {
let fetcher = new globalJsonFetcher("invalidDir");
fetcher.GetVersions().then(versionInfos => {
throw "GetVersions shouldn't success if no matching version was found.";
}, err => {
// here we are good because the getVersion throw an error.
return;
});
}

if (process.env["__case__"] == "emptyGlobalJson") {
let fetcher = new globalJsonFetcher(pathToEmptyGlobalJsonDir);
fetcher.GetVersions().then(versionInfos => {
if (versionInfos == null) {
throw "GetVersions shouldn't return null if the global.json is empty.";
}
if (versionInfos.length != 0) {
throw "GetVersions shouldn't return a arry with 0 elements if global.json is empty.";
}
}, err => {
throw "GetVersions shouldn't throw an error if global.json is empty.";
});
}
94 changes: 94 additions & 0 deletions Tasks/UseDotNetV2/_buildConfigs/Node16/globaljsonfetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"use strict";
import * as fileSystem from "fs";
import * as tl from 'azure-pipelines-task-lib/task';
import { DotNetCoreVersionFetcher } from "./versionfetcher";
import { VersionInfo } from "./models";

export class globalJsonFetcher {

private workingDirectory: string;
private versionFetcher: DotNetCoreVersionFetcher = new DotNetCoreVersionFetcher(true);
/**
* The global json fetcher provider functionality to extract the version information from all global json in the working directory.
* @param workingDirectory
*/
constructor(workingDirectory: string) {
this.workingDirectory = workingDirectory;
}

/**
* Get all version information from all global.json starting from the working directory without duplicates.
*/
public async GetVersions(): Promise<VersionInfo[]> {
var versionInformation: VersionInfo[] = new Array<VersionInfo>();
var versionStrings = this.getVersionStrings();
for (let index = 0; index < versionStrings.length; index++) {
const version = versionStrings[index];
if (version != null) {
var versionInfo = await this.versionFetcher.getVersionInfo(version, null, "sdk", false);
versionInformation.push(versionInfo);
}
}

return Array.from(new Set(versionInformation)); // this remove all not unique values.
}

private getVersionStrings(): Array<string | null> {
let filePathsToGlobalJson = tl.findMatch(this.workingDirectory, "**/global.json");
if (filePathsToGlobalJson == null || filePathsToGlobalJson.length == 0) {
throw tl.loc("FailedToFindGlobalJson", this.workingDirectory);
}

return filePathsToGlobalJson.map(path => {
var content = this.readGlobalJson(path);
if (content != null) {
tl.loc("GlobalJsonSdkVersion", content.sdk.version, path);
return content.sdk.version;
}

return null;
})
.filter(d => d != null); // remove all global.json that can't read
}

private readGlobalJson(path: string): GlobalJson | null {
let globalJson: GlobalJson | null = null;
tl.loc("GlobalJsonFound", path);
try {
let fileContent = fileSystem.readFileSync(path);
// Since here is a buffer, we need to check length property to determine if it is empty.
if (!fileContent.length) {
// do not throw if globa.json is empty, task need not install any version in such case.
tl.loc("GlobalJsonIsEmpty", path);
return null;
}

globalJson = (JSON.parse(fileContent.toString())) as { sdk: { version: string } };
} catch (error) {
// we throw if the global.json is invalid
throw tl.loc("FailedToReadGlobalJson", path, error); // We don't throw if a global.json is invalid.
}

if (globalJson == null || globalJson.sdk == null || globalJson.sdk.version == null) {
tl.loc("FailedToReadGlobalJson", path);
return null;
}

return globalJson;
}

}

export class GlobalJson {
constructor(version: string | null = null) {
if (version != null) {
this.sdk = new sdk();
this.sdk.version = version;
}
}
public sdk: sdk;
}

class sdk {
public version: string;
}
Loading

0 comments on commit 0568d10

Please sign in to comment.