|
| 1 | +/********************************************************************* |
| 2 | + * Copyright (c) 2023 Kichwa Coders Canada Inc. and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made |
| 5 | + * available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + *********************************************************************/ |
| 10 | + |
| 11 | +import * as path from 'path'; |
| 12 | +import * as tmp from 'tmp'; |
| 13 | +import * as fs from 'fs'; |
| 14 | +import { LaunchRequestArguments } from '../GDBDebugSession'; |
| 15 | +import { |
| 16 | + debugServerPort, |
| 17 | + defaultAdapter, |
| 18 | + fillDefaults, |
| 19 | + standardBeforeEach, |
| 20 | + testProgramsDir, |
| 21 | +} from './utils'; |
| 22 | +import { expect } from 'chai'; |
| 23 | + |
| 24 | +describe('config', function () { |
| 25 | + const emptyProgram = path.join(testProgramsDir, 'empty'); |
| 26 | + const emptySrc = path.join(testProgramsDir, 'empty.c'); |
| 27 | + |
| 28 | + async function verifyLaunchWorks( |
| 29 | + test: Mocha.Context, |
| 30 | + commandLine: string, |
| 31 | + requestArgs: LaunchRequestArguments |
| 32 | + ) { |
| 33 | + if (debugServerPort) { |
| 34 | + // This test requires launching the adapter to work |
| 35 | + test.skip(); |
| 36 | + } |
| 37 | + |
| 38 | + const dc = await standardBeforeEach(defaultAdapter, commandLine); |
| 39 | + |
| 40 | + try { |
| 41 | + await dc.hitBreakpoint(fillDefaults(test.test, requestArgs), { |
| 42 | + path: emptySrc, |
| 43 | + line: 3, |
| 44 | + }); |
| 45 | + } finally { |
| 46 | + await dc.stop(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + it('can specify program via --config=', async function () { |
| 51 | + await verifyLaunchWorks( |
| 52 | + this, |
| 53 | + `'--config={"program":"${emptyProgram}"}'`, |
| 54 | + {} as LaunchRequestArguments |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('program via --config= can be overridden', async function () { |
| 59 | + await verifyLaunchWorks( |
| 60 | + this, |
| 61 | + `'--config={"program":"/program/that/does/not/exist"}'`, |
| 62 | + { |
| 63 | + program: emptyProgram, |
| 64 | + } as LaunchRequestArguments |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('can specify program via --config-frozen=', async function () { |
| 69 | + await verifyLaunchWorks( |
| 70 | + this, |
| 71 | + `'--config-frozen={"program":"${emptyProgram}"}'`, |
| 72 | + {} as LaunchRequestArguments |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it('program via --config-frozen= can not be overridden', async function () { |
| 77 | + await verifyLaunchWorks( |
| 78 | + this, |
| 79 | + `'--config-frozen={"program":"${emptyProgram}"}'`, |
| 80 | + { |
| 81 | + program: '/program/that/does/not/exist', |
| 82 | + } as LaunchRequestArguments |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it('can specify program via --config= using response file', async function () { |
| 87 | + const json = `{"program":"${emptyProgram}"}`; |
| 88 | + const jsonFile = tmp.fileSync(); |
| 89 | + fs.writeFileSync(jsonFile.fd, json); |
| 90 | + fs.closeSync(jsonFile.fd); |
| 91 | + |
| 92 | + await verifyLaunchWorks( |
| 93 | + this, |
| 94 | + `'--config=@${jsonFile.name}'`, |
| 95 | + {} as LaunchRequestArguments |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it('can specify program via --config-frozen= using response file', async function () { |
| 100 | + const json = `{"program":"${emptyProgram}"}`; |
| 101 | + const jsonFile = tmp.fileSync(); |
| 102 | + fs.writeFileSync(jsonFile.fd, json); |
| 103 | + fs.closeSync(jsonFile.fd); |
| 104 | + |
| 105 | + await verifyLaunchWorks( |
| 106 | + this, |
| 107 | + `'--config-frozen=@${jsonFile.name}'`, |
| 108 | + {} as LaunchRequestArguments |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + it('config frozen can allow correct launch type', async function () { |
| 113 | + await verifyLaunchWorks( |
| 114 | + this, |
| 115 | + `'--config-frozen={"request":"launch"}'`, |
| 116 | + { |
| 117 | + program: emptyProgram, |
| 118 | + } as LaunchRequestArguments |
| 119 | + ); |
| 120 | + }); |
| 121 | + |
| 122 | + it('config frozen can prevent incorrect launch type', async function () { |
| 123 | + if (debugServerPort) { |
| 124 | + // This test requires launching the adapter to work |
| 125 | + this.skip(); |
| 126 | + } |
| 127 | + |
| 128 | + const dc = await standardBeforeEach( |
| 129 | + defaultAdapter, |
| 130 | + `'--config-frozen={"request":"attach"}'` |
| 131 | + ); |
| 132 | + |
| 133 | + const errorMessage = await new Promise<Error>((resolve, reject) => { |
| 134 | + dc.launchRequest( |
| 135 | + fillDefaults(this.test, { |
| 136 | + program: emptyProgram, |
| 137 | + } as LaunchRequestArguments) |
| 138 | + ) |
| 139 | + .then(reject) |
| 140 | + .catch(resolve); |
| 141 | + }); |
| 142 | + |
| 143 | + expect(errorMessage.message).to.satisfy((msg: string) => |
| 144 | + msg.includes('only attach requests are permitted') |
| 145 | + ); |
| 146 | + }); |
| 147 | +}); |
0 commit comments