Add support for installing .NET runtimes alongside SDKs (#806)

* Add support for installing .NET runtimes alongside SDKs

* Add tests for preview runtime pairing and fallback logic in installer
This commit is contained in:
v-mahabaleshwars
2026-11-20 19:43:57 +05:30
committed by GitHub
parent 0f4a354dd5
commit 84bc3a3451
8 changed files with 821 additions and 40 deletions

View File

@@ -40,6 +40,7 @@ jest.unstable_mockModule('fs', () => {
const mkdtempSync = jest.fn(actual.mkdtempSync);
const rmSync = jest.fn(actual.rmSync);
const readdirSync = jest.fn();
const readFileSync = jest.fn(actual.readFileSync);
const existsSync = jest.fn(() => true);
const statSync = jest.fn(() => ({isFile: () => true}));
const accessSync = jest.fn();
@@ -50,6 +51,7 @@ jest.unstable_mockModule('fs', () => {
mkdtempSync,
rmSync,
readdirSync,
readFileSync,
existsSync,
statSync,
accessSync
@@ -82,6 +84,7 @@ jest.unstable_mockModule('../src/utils.js', () => {
const exec = await import('@actions/exec');
const core = await import('@actions/core');
const io = await import('@actions/io');
const hc = await import('@actions/http-client');
const fs = await import('fs');
const installer = await import('../src/installer.js');
const {IS_WINDOWS} = await import('../src/utils.js');
@@ -1371,6 +1374,182 @@ describe('installer tests', () => {
});
});
describe('installRuntime() tests', () => {
const readdirSyncSpy = fs.readdirSync as unknown as jest.Mock;
const statSyncSpy = fs.statSync as unknown as jest.Mock;
const accessSyncSpy = fs.accessSync as unknown as jest.Mock;
const scriptArgs = () =>
getExecOutputSpy.mock.calls.map(call =>
(call[1] as string[]).join(' ')
);
const arg = (name: string, value: string) =>
IS_WINDOWS ? `-${name} ${value}` : `--${name.toLowerCase()} ${value}`;
beforeEach(() => {
jest.clearAllMocks();
delete process.env['DOTNET_INSTALL_DIR'];
whichSpy.mockImplementation(() => Promise.resolve('PathToShell'));
chmodSyncSpy.mockImplementation(() => {});
readdirSyncSpy.mockReturnValue([]);
statSyncSpy.mockReturnValue({isFile: () => true});
accessSyncSpy.mockImplementation(() => undefined);
getExecOutputSpy.mockImplementation(() =>
Promise.resolve({
exitCode: 0,
stdout: 'Installed version is 8.0.11',
stderr: ''
})
);
});
afterAll(() => {
jest.resetAllMocks();
});
it('should install the .NET and ASP.NET Core runtimes and set DOTNET_INSTALL_DIR', async () => {
const installedVersion = await new installer.DotnetCoreInstaller(
'8.0.11',
''
).installRuntime();
expect(installedVersion).toBe('8.0.11');
expect(process.env['DOTNET_INSTALL_DIR']).toBe(
installer.DotnetInstallDir.dirPath
);
expect(scriptArgs()).toHaveLength(2);
expect(scriptArgs()[0]).toContain(arg('Runtime', 'dotnet'));
expect(scriptArgs()[1]).toContain(arg('Runtime', 'aspnetcore'));
for (const args of scriptArgs()) {
expect(args).toContain(arg('Version', '8.0.11'));
}
});
it('should install the ASP.NET Core build paired with a prerelease runtime', async () => {
const getJsonSpy = jest
.spyOn(hc.HttpClient.prototype, 'getJson')
.mockResolvedValue({
statusCode: 200,
headers: {},
result: {
releases: [
{
runtime: {version: '8.0.0-rc.2.23479.6'},
'aspnetcore-runtime': {version: '8.0.0-rc.2.23480.2'}
}
]
}
});
try {
await new installer.DotnetCoreInstaller(
'8.0.0-rc.2.23479.6',
''
).installRuntime();
} finally {
getJsonSpy.mockRestore();
}
expect(scriptArgs()[0]).toContain(arg('Version', '8.0.0-rc.2.23479.6'));
expect(scriptArgs()[1]).toContain(arg('Version', '8.0.0-rc.2.23480.2'));
});
it('should reuse a complete local runtime pair when check-latest is false', async () => {
readdirSyncSpy.mockReturnValue([
{name: '8.0.11', isDirectory: () => true, isSymbolicLink: () => false}
]);
const installedVersion = await new installer.DotnetCoreInstaller(
'8.0.x',
'',
undefined,
undefined,
false
).installRuntime();
expect(installedVersion).toBe('8.0.11');
expect(getExecOutputSpy).not.toHaveBeenCalled();
});
describe('check-latest: false preview runtime pairing', () => {
const readFileSyncSpy = fs.readFileSync as unknown as jest.Mock;
const runtimeVersion = '8.0.0-rc.2.23479.6';
const config = (version: string) => () =>
JSON.stringify({
runtimeOptions: {
framework: {name: 'Microsoft.NETCore.App', version}
}
});
afterEach(() => readFileSyncSpy.mockReset());
it.each([
['skip installation for a valid mapping', config(runtimeVersion), 0],
[
'fall back online for a missing config',
() => {
throw new Error('ENOENT');
},
2
],
['fall back online for a malformed config', () => '{', 2],
[
'fall back online for an absent declared runtime',
config('8.0.0-rc.2.23475.1'),
2
]
])('should %s', async (_, readConfig, installCalls) => {
// The ASP.NET Core preview build differs from the .NET runtime build.
readdirSyncSpy.mockImplementation((...args: unknown[]) =>
[
path.basename(args[0] as string) === 'Microsoft.NETCore.App'
? runtimeVersion
: '8.0.0-rc.2.23480.2'
].map(name => ({
name,
isDirectory: () => true,
isSymbolicLink: () => false
}))
);
readFileSyncSpy.mockImplementation(readConfig);
const getJsonSpy = jest
.spyOn(hc.HttpClient.prototype, 'getJson')
.mockRejectedValue(new Error('offline'));
try {
await new installer.DotnetCoreInstaller(
runtimeVersion,
'',
undefined,
undefined,
false
).installRuntime();
} finally {
getJsonSpy.mockRestore();
}
expect(getExecOutputSpy).toHaveBeenCalledTimes(installCalls);
});
});
it.each([
[
'8.0.1xx',
` The A.B.Cxx feature band syntax applies only to .NET SDKs and is supported only by 'dotnet-version'.`
],
['abc', '']
])(
"should reject '%s' with an error naming 'dotnet-runtime' before installing",
async (version, reason) => {
await expect(
new installer.DotnetCoreInstaller(version, '').installRuntime()
).rejects.toThrow(
`The 'dotnet-runtime' was supplied in invalid format: ${version}!${reason} Supported syntax: A.B.C, A.B, A.B.x, A, A.x, latest`
);
expect(getExecOutputSpy).not.toHaveBeenCalled();
}
);
});
describe('addToPath() tests', () => {
it(`should export DOTNET_ROOT env.var with value from DOTNET_INSTALL_DIR env.var`, async () => {
process.env['DOTNET_INSTALL_DIR'] = 'fictitious/dotnet/install/dir';

View File

@@ -9,6 +9,7 @@ import {
} from '@jest/globals';
import semver from 'semver';
import os from 'os';
import path from 'path';
jest.unstable_mockModule('@actions/core', () => ({
getInput: jest.fn(),
@@ -75,6 +76,11 @@ describe('setup-dotnet tests', () => {
'installDotnet'
);
const installRuntimeSpy = jest.spyOn(
DotnetCoreInstaller.prototype,
'installRuntime'
);
const isCacheFeatureAvailableSpy =
cacheUtils.isCacheFeatureAvailable as jest.Mock;
const restoreCacheSpy = cacheRestore.restoreCache as jest.Mock;
@@ -84,6 +90,7 @@ describe('setup-dotnet tests', () => {
describe('run() tests', () => {
beforeEach(() => {
DotnetInstallDir.addToPath = jest.fn();
inputs['dotnet-runtime'] = [];
getMultilineInputSpy.mockImplementation(input => inputs[input as string]);
getInputSpy.mockImplementation(input => inputs[input as string]);
getBooleanInputSpy.mockImplementation(input => {
@@ -205,6 +212,97 @@ describe('setup-dotnet tests', () => {
expect(DotnetInstallDir.addToPath).toHaveBeenCalledTimes(1);
});
describe('dotnet-runtime input', () => {
beforeEach(() => {
inputs['global-json-file'] = '';
inputs['dotnet-version'] = [];
inputs['dotnet-quality'] = '';
inputs['dotnet-channel'] = '';
inputs['architecture'] = '';
existsSyncSpy.mockReturnValue(false);
});
afterEach(() => {
inputs['dotnet-channel'] = '';
inputs['dotnet-quality'] = '';
inputs['check-latest'] = '';
inputs['architecture'] = '';
delete process.env['DOTNET_INSTALL_DIR'];
});
it('should install unique runtime versions, add PATH and not set the dotnet-version output without an SDK', async () => {
inputs['dotnet-runtime'] = ['8.0.x', '9.0.x', '8.0.x'];
installRuntimeSpy
.mockResolvedValueOnce('8.0.11')
.mockResolvedValueOnce('9.0.1');
await setup.run();
expect(setFailedSpy).not.toHaveBeenCalled();
expect(installDotnetSpy).not.toHaveBeenCalled();
expect(installRuntimeSpy).toHaveBeenCalledTimes(2);
expect(DotnetInstallDir.addToPath).toHaveBeenCalledTimes(1);
expect(setOutputSpy).not.toHaveBeenCalled();
});
it('should forward dotnet-channel (feature band as A.B), dotnet-quality and check-latest to runtime latest', async () => {
inputs['dotnet-runtime'] = ['latest'];
inputs['dotnet-channel'] = '8.0.1xx';
inputs['dotnet-quality'] = 'preview';
inputs['check-latest'] = 'false';
let capturedOptions: object | undefined;
installRuntimeSpy.mockImplementation(function (this: {
dotnetChannel?: string;
quality: string;
checkLatest: boolean;
}) {
capturedOptions = {
dotnetChannel: this.dotnetChannel,
quality: this.quality,
checkLatest: this.checkLatest
};
return Promise.resolve('8.0.0-preview.1');
});
await setup.run();
expect(setFailedSpy).not.toHaveBeenCalled();
expect(capturedOptions).toEqual({
dotnetChannel: '8.0',
quality: 'preview',
checkLatest: false
});
});
it('should fail the action if unsupported dotnet-channel value is provided with runtime latest', async () => {
inputs['dotnet-runtime'] = ['latest'];
inputs['dotnet-channel'] = 'invalid';
await setup.run();
expect(setFailedSpy).toHaveBeenCalledWith(
`Value 'invalid' is not supported for the 'dotnet-channel' option. Supported values are: LTS, STS, A.B (e.g. 8.0), A.B.Cxx (e.g. 8.0.1xx).`
);
expect(installRuntimeSpy).not.toHaveBeenCalled();
});
it('should keep the cross-architecture DOTNET_INSTALL_DIR after installing an SDK and a runtime', async () => {
const architecture = os.arch() === 'arm64' ? 'x64' : 'arm64';
inputs['dotnet-version'] = ['8.0.x'];
inputs['dotnet-runtime'] = ['9.0.x'];
inputs['architecture'] = architecture;
installDotnetSpy.mockResolvedValue('8.0.404');
installRuntimeSpy.mockImplementation(() => {
DotnetInstallDir.setEnvironmentVariable();
return Promise.resolve('9.0.1');
});
await setup.run();
expect(setFailedSpy).not.toHaveBeenCalled();
expect(process.env['DOTNET_INSTALL_DIR']).toBe(
path.join(DotnetInstallDir.dirPath, architecture)
);
expect(DotnetInstallDir.addToPath).toHaveBeenCalledTimes(1);
});
});
it(`shouldn't call setOutput() if parsing dotnet-installer logs failed`, async () => {
inputs['dotnet-version'] = ['10.0.101'];
const warningMessage = `Failed to output the installed version of .NET. The 'dotnet-version' output will not be set.`;
@@ -502,7 +600,7 @@ describe('setup-dotnet tests', () => {
await setup.run();
expect(setFailedSpy).not.toHaveBeenCalled();
expect(warningSpy).toHaveBeenCalledWith(
`The 'dotnet-channel' input is only supported when 'dotnet-version' is set to 'latest'.`
`The 'dotnet-channel' input is only supported when 'dotnet-version' or 'dotnet-runtime' is set to 'latest'.`
);
});