Skip to content

refactor: updated Visual Studio setup process for required components installation #386

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

Merged
merged 2 commits into from
May 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
61 changes: 60 additions & 1 deletion __tests__/utils/visual_studio/setup.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import * as path from 'path'
import {promises as fs} from 'fs'
import os from 'os'
import crypto from 'crypto'
import * as exec from '@actions/exec'
import {VisualStudio} from '../../../src/utils/visual_studio'
import {
VisualStudio,
VisualStudioConfig
} from '../../../src/utils/visual_studio'

describe('visual studio setup validation', () => {
const env = process.env
Expand All @@ -21,11 +25,13 @@ describe('visual studio setup validation', () => {

beforeEach(() => {
process.env = {...env}
VisualStudio.shared = undefined
})

afterEach(() => {
jest.restoreAllMocks()
process.env = env
VisualStudio.shared = undefined
})

it('tests visual studio setup fails when invalid path', async () => {
Expand Down Expand Up @@ -58,6 +64,59 @@ describe('visual studio setup validation', () => {
await expect(
VisualStudio.setup({version: '16', components: visualStudio.components})
).resolves.toMatchObject(visualStudio)
expect(VisualStudio.shared).toStrictEqual(visualStudio)
})

it('tests visual studio duplicate setup', async () => {
VisualStudio.shared = visualStudio
const fsAccessSpy = jest.spyOn(fs, 'access')
const execSpy = jest.spyOn(exec, 'exec')
const getExecOutputSpy = jest.spyOn(exec, 'getExecOutput')
await expect(
VisualStudio.setup({version: '16', components: visualStudio.components})
).resolves.toMatchObject(visualStudio)
expect(VisualStudio.shared).toBe(visualStudio)
for (const spy of [fsAccessSpy, execSpy, getExecOutputSpy]) {
expect(spy).not.toHaveBeenCalled()
}
})

it('tests visual studio setup successfully skipping components installation', async () => {
process.env.VSWHERE_PATH = path.join('C:', 'Visual Studio')
const ucrtVersion = '1'
const ucrtSdkDir = path.join('C:', 'UniversalCRTSdkDir')
const vcToolsInstallDir = path.join('C:', 'VCToolsInstallDir')
const tmpDir = process.env.RUNNER_TEMP || os.tmpdir()
const configId = '792a1d5c-ef88-45da-858c-baf3e6e0d048'
const configFileName = `swift-setup-installation-${configId}.vsconfig`
const vsConfig: VisualStudioConfig = {
version: visualStudio.installationVersion,
components: visualStudio.components
}

jest.spyOn(fs, 'access').mockResolvedValue()
jest.spyOn(exec, 'exec').mockResolvedValue(0)
jest.spyOn(crypto, 'randomUUID').mockReturnValue(configId)
jest
.spyOn(exec, 'getExecOutput')
.mockResolvedValueOnce({
exitCode: 0,
stdout: JSON.stringify([visualStudio]),
stderr: ''
})
.mockResolvedValueOnce({
exitCode: 0,
stdout: `UniversalCRTSdkDir=${ucrtSdkDir}\nUCRTVersion=${ucrtVersion}\nVCToolsInstallDir=${vcToolsInstallDir}`,
stderr: ''
})
const readFileSpy = jest.spyOn(fs, 'readFile')
readFileSpy.mockResolvedValue(JSON.stringify(vsConfig))

await expect(
VisualStudio.setup({version: '16', components: visualStudio.components})
).resolves.toMatchObject(visualStudio)
expect(readFileSpy.mock.calls[0][0]).toBe(path.join(tmpDir, configFileName))
expect(VisualStudio.shared).toStrictEqual(visualStudio)
})

it('tests visual studio environment setup', async () => {
Expand Down
94 changes: 62 additions & 32 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 8 additions & 10 deletions src/installer/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

private async downloadSignature() {
try {
return this.signatureUrl
? await toolCache.downloadTool(this.signatureUrl)
: undefined
if (this.signatureUrl) {
core.debug(`Downloading snapshot signature from "${this.signatureUrl}"`)
return await toolCache.downloadTool(this.signatureUrl)
}
return undefined

Check warning on line 22 in src/installer/verify.ts

View check run for this annotation

Codecov / codecov/patch

src/installer/verify.ts#L22

Added line #L22 was not covered by tests
} catch (error) {
if (
error instanceof toolCache.HTTPError &&
Expand All @@ -32,18 +34,14 @@

protected async download(arch: string) {
const sigUrl = this.signatureUrl
const signature = await this.downloadSignature()
async function setupKeys() {
if (sigUrl) {
if (sigUrl && signature) {
await gpg.setupKeys()
}
}

core.debug(`Downloading snapshot signature from "${sigUrl}"`)
const [, toolchain, signature] = await Promise.all([
setupKeys(),
super.download(arch),
this.downloadSignature()
])
const [, toolchain] = await Promise.all([setupKeys(), super.download(arch)])
if (signature) {
await gpg.verify(signature, toolchain)
}
Expand Down
12 changes: 6 additions & 6 deletions src/installer/windows/installation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {env, fallback} from './fallback'
declare module './base' {
// eslint-disable-next-line no-shadow, @typescript-eslint/no-namespace
export namespace Installation {
let lastInstallation: Installation | CustomInstallation
export function get(
install?: string
): Promise<Installation | CustomInstallation | undefined>
Expand All @@ -20,9 +21,9 @@ declare module './base' {
}
}

Installation.get = async (install?: string) => {
Installation.get = async function (install?: string) {
if (!(install?.length ?? 1)) {
return lastInstallation
return this.lastInstallation
}

const approaches = [
Expand All @@ -47,14 +48,13 @@ Installation.get = async (install?: string) => {
return undefined
}

let lastInstallation: Installation | CustomInstallation
Installation.install = async (exe: string) => {
Installation.install = async function (exe: string) {
core.debug(`Installing toolchain from "${exe}"`)
const oldEnv = await env()
await exec(`"${exe}"`, ['-q'])
const newEnv = await env()
lastInstallation = await Installation.detect(oldEnv, newEnv)
return lastInstallation
this.lastInstallation = await Installation.detect(oldEnv, newEnv)
return this.lastInstallation
}

Installation.detect = async (
Expand Down
Loading
Loading