Skip to content
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
32 changes: 30 additions & 2 deletions src/extensions/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { execFileSync } from 'node:child_process'
import { mkdirSync, mkdtempSync, readdirSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'

import { describe, it, expect, beforeEach } from 'vitest'
import { beforeEach, describe, expect, it, vi } from 'vitest'

import { findEditorCli, findOldExtensionDirs, removeExtensionDirs } from './index'
import { findEditorCli, findOldExtensionDirs, removeExtensionDirs, runInstallExtension } from './index'

vi.mock('node:child_process', () => ({
execFileSync: vi.fn(),
}))

let tempDir: string

beforeEach(() => {
vi.mocked(execFileSync).mockReset()
tempDir = mkdtempSync(join(tmpdir(), 'mdm-ext-test-'))
})

Expand All @@ -29,6 +35,28 @@ describe('findEditorCli', () => {
})
})

describe('runInstallExtension', () => {
it('runs the editor CLI from the user home directory on macOS', () => {
runInstallExtension(
'/usr/local/bin/cursor',
'testuser',
'/Users/testuser',
'/Users/testuser/.cursor/extensions',
'darwin',
)

expect(execFileSync).toHaveBeenCalledWith(
'sudo',
['-H', '-u', 'testuser', '/usr/local/bin/cursor', '--install-extension', 'glean.glean'],
{
cwd: '/Users/testuser',
stdio: 'pipe',
timeout: 120_000,
},
)
})
})

describe('findOldExtensionDirs', () => {
it('returns paths matching glean.glean-*', () => {
const extensionsDir = join(tempDir, 'extensions')
Expand Down
6 changes: 4 additions & 2 deletions src/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ export function removeExtensionDirs(dirs: string[]): void {
}
}

function runInstallExtension(
export function runInstallExtension(
cliPath: string,
username: string,
userHomeDir: string,
extensionsDir: string,
platform: Platform,
): void {
Expand All @@ -156,6 +157,7 @@ function runInstallExtension(
})
} else {
execFileSync('sudo', ['-H', '-u', username, cliPath, '--install-extension', EXTENSION_ID], {
cwd: userHomeDir,
stdio: 'pipe',
timeout: INSTALL_TIMEOUT_MS,
})
Expand Down Expand Up @@ -187,7 +189,7 @@ export function installExtensions(options: InstallExtensionsOptions): ExtensionI
try {
const extensionsDir = join(userHomeDir, editor.extensionsDirName, 'extensions')
const oldDirs = new Set(findOldExtensionDirs(extensionsDir))
runInstallExtension(cliPath, username, extensionsDir, platform)
runInstallExtension(cliPath, username, userHomeDir, extensionsDir, platform)
const currentDirs = new Set(findOldExtensionDirs(extensionsDir))
// Only clean old versions if the install actually added a new version dir.
// If no new dirs appeared (no-op / already current), keep everything to
Expand Down
Loading