Skip to content

Commit 03bb150

Browse files
authored
UseDotNetV2: Support global.json files with comments (#19226)
* support global json * reverted task-utility-common version * updated _generated files
1 parent b7e6493 commit 03bb150

20 files changed

+231
-280
lines changed

Tasks/UseDotNetV2/Tests/L0.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,14 @@ describe('UseDotNet', function () {
531531
assert(tr.succeeded == true, ("Should passed."));
532532
assert(tr.stdout.indexOf("GlobalJsonIsEmpty") > -1, "should throw an error that no file was found.");
533533
}, tr, done);
534-
})
535-
});
536-
534+
});
535+
it("[globaljsonfetcher] run shouldn't fail if the global.json has comments.", (done) => {
536+
process.env["__case__"] = "globalJsonWithComments";
537+
let tr = new ttm.MockTestRunner(path.join(__dirname, "globaljsonfetcherTest.js"))
538+
tr.run();
539+
runValidations(() => {
540+
assert(tr.succeeded == true, ("Should have passed."));
541+
assert(tr.stdout.indexOf("GlobalJsonFound") > -1, "should found a global.json file");
542+
}, tr, done);
543+
});
544+
});

Tasks/UseDotNetV2/Tests/globaljsonfetcherTest.ts

+30
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const validSubDirGlobalJson = workingSubDir + "global.json";
1515
const subDirVersionNumber = "3.0.0-pre285754637";
1616
const pathToEmptyGlobalJsonDir = workingDir + "empty/";
1717
const pathToEmptyGlobalJson = pathToEmptyGlobalJsonDir + "global.json";
18+
const pathToGlobalJsonWithCommentsDir = workingDir + "comments/";
19+
const pathToGlobalJsonWithComments = pathToGlobalJsonWithCommentsDir + "global.json";
1820

1921
//setup mocks
2022
mockery.enable({
@@ -38,6 +40,9 @@ mockery.registerMock('azure-pipelines-task-lib/task', {
3840
if (path == pathToEmptyGlobalJsonDir) {
3941
return [pathToEmptyGlobalJson];
4042
}
43+
if (path == pathToGlobalJsonWithCommentsDir) {
44+
return [pathToGlobalJsonWithComments];
45+
}
4146
return [];
4247
},
4348
loc: function (locString, ...param: string[]) { return tl.loc(locString, param); },
@@ -58,6 +63,17 @@ mockery.registerMock('fs', {
5863
if (path == pathToEmptyGlobalJson) {
5964
return Buffer.from("");
6065
}
66+
if (path == pathToGlobalJsonWithComments) {
67+
return Buffer.from(`{
68+
/*
69+
This is a mult-line comment
70+
*/
71+
"sdk": {
72+
// This is a single-line comment
73+
"version": "${rootVersionNumber}"
74+
}
75+
}`);
76+
}
6177
return Buffer.from(null);
6278
}
6379
});
@@ -133,4 +149,18 @@ if (process.env["__case__"] == "emptyGlobalJson") {
133149
}, err => {
134150
throw "GetVersions shouldn't throw an error if global.json is empty.";
135151
});
152+
}
153+
154+
if (process.env["__case__"] == "globalJsonWithComments") {
155+
let fetcher = new globalJsonFetcher(pathToGlobalJsonWithCommentsDir);
156+
fetcher.GetVersions().then(versionInfos => {
157+
if (versionInfos == null) {
158+
throw "GetVersions shouldn't return null if the global.json has comments.";
159+
}
160+
if (versionInfos.length != 1) {
161+
throw "GetVersions shouldn't return a arry with 0 elements if global.json has comments.";
162+
}
163+
}, err => {
164+
throw "GetVersions shouldn't throw an error if global.json has comments.";
165+
});
136166
}

Tasks/UseDotNetV2/globaljsonfetcher.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22
import * as fileSystem from "fs";
33
import * as tl from 'azure-pipelines-task-lib/task';
4+
import * as JSON5 from 'json5';
45
import { DotNetCoreVersionFetcher } from "./versionfetcher";
56
import { VersionInfo } from "./models";
67

@@ -63,7 +64,7 @@ export class globalJsonFetcher {
6364
return null;
6465
}
6566

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

Tasks/UseDotNetV2/package-lock.json

+37-121
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tasks/UseDotNetV2/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
"@types/node": "^16.11.39",
2929
"@types/semver": "6.0.0",
3030
"azure-pipelines-task-lib": "^4.4.0",
31-
"azure-pipelines-tasks-packaging-common": "^2.198.1",
31+
"azure-pipelines-tasks-packaging-common": "^3.230.0",
3232
"azure-pipelines-tasks-utility-common": "^3.198.1",
33-
"azure-pipelines-tool-lib": "^2.0.4",
33+
"azure-pipelines-tool-lib": "^2.0.7",
34+
"json5": "^2.2.3",
3435
"semver": "6.3.0",
3536
"typed-rest-client": "^1.8.9"
3637
},

Tasks/UseDotNetV2/task.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"author": "Microsoft Corporation",
1414
"version": {
1515
"Major": 2,
16-
"Minor": 230,
17-
"Patch": 2
16+
"Minor": 231,
17+
"Patch": 0
1818
},
1919
"satisfies": [
2020
"DotNetCore"

Tasks/UseDotNetV2/task.loc.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"author": "Microsoft Corporation",
1414
"version": {
1515
"Major": 2,
16-
"Minor": 230,
17-
"Patch": 2
16+
"Minor": 231,
17+
"Patch": 0
1818
},
1919
"satisfies": [
2020
"DotNetCore"

_generated/UseDotNetV2.versionmap.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Default|2.230.2
2-
Node20_229_3|2.230.4
1+
Default|2.231.0
2+
Node20_229_3|2.231.1

0 commit comments

Comments
 (0)