-
Notifications
You must be signed in to change notification settings - Fork 91
feat: support prebuilt VPP packages (runtime-v4 upload + arduino-cli mixed compile/link) #886
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a60528f
feat(vpp): package prebuilt-object plugins for upload
marconetsf bd3c007
refactor(vpp): drop unused minRuntimeVersion, test prebuilt packaging…
marconetsf 648da22
feat(compile): support arduino prebuilt mixed VPPs (source + precompi…
marconetsf 3608ae9
fix(compile): link the vendor precompiled lib in the editor arduino path
marconetsf f9095d2
chore(compile): relax prebuilt core pin to same-core-present for now
marconetsf 4be25bc
test(compile): cover the arduino prebuilt mixed-VPP path
marconetsf a429b8d
fix(compile): pin the prebuilt core to the exact manifest version
marconetsf ee03886
docs(compile): describe the core pin as install-exact, not post-insta…
marconetsf d037135
style: prettier-format handle-core-installation test
marconetsf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
src/backend/editor/compiler/__tests__/handle-core-installation.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { spawn } from 'node:child_process' | ||
| import { EventEmitter } from 'node:events' | ||
|
|
||
| import { CompilerModule } from '../compiler-module' | ||
|
|
||
| // Electron is imported transitively by compiler-module; stub the bits the | ||
| // instantiation path actually touches so jest doesn't load the real runtime. | ||
| jest.mock('electron', () => ({ | ||
| app: { | ||
| getPath: jest.fn().mockReturnValue('/tmp/mock-user-data'), | ||
| getAppPath: jest.fn().mockReturnValue('/tmp/mock-app-root'), | ||
| isPackaged: false, | ||
| getVersion: jest.fn().mockReturnValue('0.0.0-test'), | ||
| }, | ||
| dialog: { showSaveDialog: jest.fn().mockResolvedValue({ filePath: '/tmp/mock-save-path' }) }, | ||
| })) | ||
| jest.mock('electron/main', () => ({}), { virtual: true }) | ||
|
|
||
| // compiler-module pulls in recipe-exec, which calls promisify(execFile) at | ||
| // module load, so the mock must expose exec/execFile (with promisify.custom) | ||
| // alongside spawn. handleCoreInstallation reaches spawn only on the install | ||
| // path (core absent OR a pinned version is requested); the skip-path tests | ||
| // assert spawn is NOT called. | ||
| jest.mock('node:child_process', () => { | ||
| const { promisify } = jest.requireActual('node:util') as typeof import('node:util') | ||
| const noop = async () => ({ stdout: '', stderr: '' }) | ||
| const exec = ( | ||
| _cmd: string, | ||
| _opts: unknown, | ||
| cb: (err: Error | null, val?: { stdout: string; stderr: string }) => void, | ||
| ) => { | ||
| noop().then((v) => cb(null, v)) | ||
| return { kill: () => undefined } | ||
| } | ||
| ;(exec as unknown as { [k: symbol]: unknown })[promisify.custom] = () => noop() | ||
| const execFile = ( | ||
| _command: string, | ||
| _args: ReadonlyArray<string>, | ||
| _opts: unknown, | ||
| cb: (err: Error | null, val?: { stdout: string; stderr: string }) => void, | ||
| ) => { | ||
| noop().then((v) => cb(null, v)) | ||
| return { kill: () => undefined } | ||
| } | ||
| ;(execFile as unknown as { [k: symbol]: unknown })[promisify.custom] = () => noop() | ||
| return { exec, execFile, spawn: jest.fn() } | ||
| }) | ||
| ;(process as unknown as { resourcesPath: string }).resourcesPath ??= process.cwd() | ||
|
|
||
| type InstalledCores = Awaited<ReturnType<CompilerModule['getArduinoInstalledCores']>> | ||
|
|
||
| // A fake ChildProcess that satisfies handleCoreInstallation's wiring | ||
| // (stdout/stderr `.on`, plus a `close` event) and reports the given exit code | ||
| // on the next tick so the `.on('close')` handler is registered first. | ||
| function fakeChild(exitCode = 0) { | ||
| const child = new EventEmitter() as EventEmitter & { stdout: EventEmitter; stderr: EventEmitter } | ||
| child.stdout = new EventEmitter() | ||
| child.stderr = new EventEmitter() | ||
| setImmediate(() => child.emit('close', exitCode)) | ||
| return child | ||
| } | ||
|
|
||
| describe('handleCoreInstallation (prebuilt core pin = exact manifest version)', () => { | ||
| let compilerModule: CompilerModule | ||
|
|
||
| beforeEach(() => { | ||
| compilerModule = new CompilerModule() | ||
| jest.mocked(spawn).mockReset() | ||
| }) | ||
|
|
||
| it('does nothing when boardCore is null', async () => { | ||
| const log = jest.fn() | ||
| const coresSpy = jest.spyOn(compilerModule, 'getArduinoInstalledCores') | ||
| await compilerModule.handleCoreInstallation(null, log) | ||
| expect(coresSpy).not.toHaveBeenCalled() | ||
| expect(spawn).not.toHaveBeenCalled() | ||
| expect(log).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('installs the EXACT pinned version even when a different version is already present', async () => { | ||
| const log = jest.fn() | ||
| jest.mocked(spawn).mockReturnValue(fakeChild(0) as unknown as ReturnType<typeof spawn>) | ||
| jest | ||
| .spyOn(compilerModule, 'getArduinoInstalledCores') | ||
| .mockResolvedValue({ 'FACTS:samd': { version: '1.7.99' } } as unknown as InstalledCores) | ||
|
|
||
| await compilerModule.handleCoreInstallation('FACTS:samd', log, '1.7.13') | ||
|
|
||
| expect(spawn).toHaveBeenCalledTimes(1) | ||
| const [, argv] = jest.mocked(spawn).mock.calls[0] | ||
| expect(argv).toEqual(expect.arrayContaining(['core', 'install', 'FACTS:samd@1.7.13'])) | ||
| }) | ||
|
|
||
| it('installs the pinned version when the core is absent', async () => { | ||
| const log = jest.fn() | ||
| jest.mocked(spawn).mockReturnValue(fakeChild(0) as unknown as ReturnType<typeof spawn>) | ||
| jest.spyOn(compilerModule, 'getArduinoInstalledCores').mockResolvedValue({} as InstalledCores) | ||
|
|
||
| await compilerModule.handleCoreInstallation('FACTS:samd', log, '1.7.13') | ||
|
|
||
| expect(spawn).toHaveBeenCalledTimes(1) | ||
| const [, argv] = jest.mocked(spawn).mock.calls[0] | ||
| expect(argv).toEqual(expect.arrayContaining(['core', 'install', 'FACTS:samd@1.7.13'])) | ||
| }) | ||
|
|
||
| it('rejects when the pinned version install fails (non-zero exit)', async () => { | ||
| const log = jest.fn() | ||
| jest.mocked(spawn).mockReturnValue(fakeChild(1) as unknown as ReturnType<typeof spawn>) | ||
| jest.spyOn(compilerModule, 'getArduinoInstalledCores').mockResolvedValue({} as InstalledCores) | ||
|
|
||
| await expect(compilerModule.handleCoreInstallation('FACTS:samd', log, '9.9.9')).rejects.toThrow( | ||
| /exited with code 1/, | ||
| ) | ||
| }) | ||
|
|
||
| it('skips install (no spawn) only when the core is present AND no version is pinned', async () => { | ||
| const log = jest.fn() | ||
| jest | ||
| .spyOn(compilerModule, 'getArduinoInstalledCores') | ||
| .mockResolvedValue({ 'arduino:avr': { version: '1.8.6' } } as unknown as InstalledCores) | ||
|
|
||
| await compilerModule.handleCoreInstallation('arduino:avr', log) | ||
|
|
||
| expect(spawn).not.toHaveBeenCalled() | ||
| const message = log.mock.calls.map((c) => String(c[0])).join('\n') | ||
| expect(message).toMatch(/already installed/) | ||
| }) | ||
| }) |
159 changes: 159 additions & 0 deletions
159
src/backend/editor/compiler/__tests__/handle-vendor-plugin-packaging.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| /** | ||
| * `CompilerModule.handleVendorPluginPackaging` — prebuilt vs source | ||
| * provisioning branch. | ||
| * | ||
| * The packager treats `hal.pluginEntry` differently depending on | ||
| * `hal.provisioning`: | ||
| * - "prebuilt": pluginEntry IS the directory holding the precompiled | ||
| * `.o` objects + link-only Makefile — copied verbatim. | ||
| * - source (default / absent): pluginEntry is the entry source FILE, | ||
| * so the directory to copy is its parent. | ||
| * | ||
| * We drive the real method against a temp filesystem, mocking only the | ||
| * package manager (which board/manifest it sees) and electron (so the | ||
| * module import doesn't try to reach the Electron app at load time). | ||
| * The method doesn't touch `this`, so we invoke it via the prototype | ||
| * and skip the constructor entirely. | ||
| */ | ||
|
|
||
| import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs' | ||
| import { tmpdir } from 'os' | ||
| import { join } from 'path' | ||
|
|
||
| // The module calls `electronApp.getPath(...)` in its constructor; we never | ||
| // construct it here, but the top-level `import electron` still has to resolve. | ||
| jest.mock('electron', () => ({ | ||
| app: { getPath: () => tmpdir() }, | ||
| dialog: {}, | ||
| MessageChannelMain: class {}, | ||
| })) | ||
|
|
||
| const listInstalled = jest.fn() | ||
| const getInstalledPackageManifest = jest.fn() | ||
| jest.mock('../../package-manager', () => ({ | ||
| PackageManagerModule: jest.fn().mockImplementation(() => ({ | ||
| listInstalled, | ||
| getInstalledPackageManifest, | ||
| })), | ||
| })) | ||
|
|
||
| // eslint-disable-next-line import/first | ||
| import { CompilerModule } from '../compiler-module' | ||
|
|
||
| type LogEntry = { message: string; level: string } | ||
|
|
||
| const BOARD = 'Raspberry Pi (prebuilt test)' | ||
|
|
||
| const handler = CompilerModule.prototype.handleVendorPluginPackaging | ||
|
|
||
| function makeManifest(hal: Record<string, unknown>) { | ||
| return { | ||
| devices: [ | ||
| { | ||
| name: BOARD, | ||
| target: { type: 'runtime-v4' }, | ||
| hal, | ||
| moduleSystem: undefined, | ||
| }, | ||
| ], | ||
| } | ||
| } | ||
|
|
||
| /** Writes a plugin directory with two payload files + an excluded one. */ | ||
| function writePluginDir(pkgDir: string): string { | ||
| const pluginDir = join(pkgDir, 'hal', 'runtime-v4', 'plugin') | ||
| mkdirSync(pluginDir, { recursive: true }) | ||
| writeFileSync(join(pluginDir, 'rpi_plugin.o'), 'OBJECT-BYTES') | ||
| writeFileSync(join(pluginDir, 'Makefile'), 'all:\n\techo link\n') | ||
| // Excluded by the packager — must not be copied into vpp_plugin/. | ||
| writeFileSync(join(pluginDir, 'config_template.json'), JSON.stringify({ plugin_name: 'rpi_gpio', pins: [] })) | ||
| return pluginDir | ||
| } | ||
|
|
||
| describe('handleVendorPluginPackaging — provisioning branch', () => { | ||
| let pkgDir: string | ||
| let projectDir: string | ||
| let targetDir: string | ||
| let logs: LogEntry[] | ||
|
|
||
| const runFor = (hal: Record<string, unknown>) => { | ||
| listInstalled.mockReturnValue([{ packageId: 'com.openplc.rpi', path: pkgDir }]) | ||
| getInstalledPackageManifest.mockReturnValue(makeManifest(hal)) | ||
| return handler.call( | ||
| {} as CompilerModule, | ||
| BOARD, | ||
| projectDir, | ||
| targetDir, | ||
| (message: string | Buffer, level?: string) => { | ||
| logs.push({ message: String(message), level: level ?? '' }) | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| pkgDir = mkdtempSync(join(tmpdir(), 'vpp-pkg-')) | ||
| projectDir = mkdtempSync(join(tmpdir(), 'vpp-proj-')) | ||
| targetDir = mkdtempSync(join(tmpdir(), 'vpp-target-')) | ||
| logs = [] | ||
| writePluginDir(pkgDir) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| for (const dir of [pkgDir, projectDir, targetDir]) { | ||
| rmSync(dir, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| it('treats pluginEntry as a directory when provisioning is "prebuilt"', async () => { | ||
| await runFor({ | ||
| type: 'runtime-v4-plugin', | ||
| pluginType: 'native', | ||
| provisioning: 'prebuilt', | ||
| pluginEntry: 'hal/runtime-v4/plugin', | ||
| configTemplate: 'hal/runtime-v4/plugin/config_template.json', | ||
| }) | ||
|
|
||
| const dest = join(targetDir, 'vpp_plugin') | ||
| expect(existsSync(join(dest, 'rpi_plugin.o'))).toBe(true) | ||
| expect(existsSync(join(dest, 'Makefile'))).toBe(true) | ||
| // Excluded file is never copied. | ||
| expect(existsSync(join(dest, 'config_template.json'))).toBe(false) | ||
| // Deterministic integrity checksum is emitted. | ||
| expect(existsSync(join(dest, 'checksum.sha256'))).toBe(true) | ||
| // The summary log distinguishes the prebuilt path. | ||
| expect(logs.some((l) => /prebuilt file\(s\)/.test(l.message))).toBe(true) | ||
| }) | ||
|
|
||
| it('treats pluginEntry as a file and copies its parent dir in source mode (provisioning absent)', async () => { | ||
| // Source-mode pluginEntry points at the entry FILE; the directory to copy | ||
| // is its parent — the same plugin dir, reached via dirname(). | ||
| writeFileSync(join(pkgDir, 'hal', 'runtime-v4', 'plugin', 'rpi_plugin.c'), 'int main(){}') | ||
|
|
||
| await runFor({ | ||
| type: 'runtime-v4-plugin', | ||
| pluginType: 'native', | ||
| pluginEntry: 'hal/runtime-v4/plugin/rpi_plugin.c', | ||
| configTemplate: 'hal/runtime-v4/plugin/config_template.json', | ||
| }) | ||
|
|
||
| const dest = join(targetDir, 'vpp_plugin') | ||
| expect(existsSync(join(dest, 'rpi_plugin.c'))).toBe(true) | ||
| expect(existsSync(join(dest, 'Makefile'))).toBe(true) | ||
| expect(existsSync(join(dest, 'config_template.json'))).toBe(false) | ||
| expect(logs.some((l) => /source file\(s\)/.test(l.message))).toBe(true) | ||
| }) | ||
|
|
||
| it('copies the payload byte-for-byte (prebuilt object content preserved)', async () => { | ||
| await runFor({ | ||
| type: 'runtime-v4-plugin', | ||
| pluginType: 'native', | ||
| provisioning: 'prebuilt', | ||
| pluginEntry: 'hal/runtime-v4/plugin', | ||
| configTemplate: 'hal/runtime-v4/plugin/config_template.json', | ||
| }) | ||
|
|
||
| const copied = readFileSync(join(targetDir, 'vpp_plugin', 'rpi_plugin.o'), 'utf-8') | ||
| expect(copied).toBe('OBJECT-BYTES') | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The “byte-for-byte” assertion should compare binary buffers, not UTF-8 text.
Reading with
'utf-8'can mask binary differences for real.opayloads. CompareBuffervalues directly to make this test truly byte-preserving.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents