Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UseDotNetV2] Update task to node16 using codegen #18676

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

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