|
| 1 | +/* |
| 2 | + Copyright (c) 2026, Oracle and/or its affiliates. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | +import { expect } from 'chai'; |
| 17 | +import { describe, it } from 'mocha'; |
| 18 | +import { applyRunConfigurationOverrides } from '../../../views/runConfigurationUtils'; |
| 19 | +import { DebugConfiguration } from 'vscode'; |
| 20 | + |
| 21 | +describe('runConfigurationUtils', () => { |
| 22 | + it('leaves the configuration unchanged when no overrides are provided', () => { |
| 23 | + const config = createDebugConfiguration({ |
| 24 | + args: '--existing', |
| 25 | + vmArgs: '-Xmx512m', |
| 26 | + env: { EXISTING: '1' }, |
| 27 | + cwd: '/existing' |
| 28 | + }); |
| 29 | + |
| 30 | + const updated = applyRunConfigurationOverrides(config, {}); |
| 31 | + |
| 32 | + expect(updated).to.deep.equal(config); |
| 33 | + }); |
| 34 | + |
| 35 | + it('appends launch arguments to an existing string value', () => { |
| 36 | + const config = createDebugConfiguration({ |
| 37 | + args: '--existing' |
| 38 | + }); |
| 39 | + |
| 40 | + const updated = applyRunConfigurationOverrides(config, { |
| 41 | + args: '--added' |
| 42 | + }); |
| 43 | + |
| 44 | + expect(updated.args).to.equal('--existing --added'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('adds parsed vm arguments into an existing array', () => { |
| 48 | + const config = createDebugConfiguration({ |
| 49 | + vmArgs: ['-Xmx512m'] |
| 50 | + }); |
| 51 | + |
| 52 | + const updated = applyRunConfigurationOverrides(config, { |
| 53 | + vmArgs: '-Dfoo=bar "-Dquoted=value here"' |
| 54 | + }); |
| 55 | + |
| 56 | + expect(updated.vmArgs).to.deep.equal([ |
| 57 | + '-Xmx512m', |
| 58 | + '-Dfoo=bar', |
| 59 | + '"-Dquoted=value here"' |
| 60 | + ]); |
| 61 | + }); |
| 62 | + |
| 63 | + it('appends vm arguments to an existing string value', () => { |
| 64 | + const config = createDebugConfiguration({ |
| 65 | + vmArgs: '-Xmx512m' |
| 66 | + }); |
| 67 | + |
| 68 | + const updated = applyRunConfigurationOverrides(config, { |
| 69 | + vmArgs: '-Dfoo=bar' |
| 70 | + }); |
| 71 | + |
| 72 | + expect(updated.vmArgs).to.equal('-Xmx512m -Dfoo=bar'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('uses new vm arguments as-is when the config has none yet', () => { |
| 76 | + const config = createDebugConfiguration(); |
| 77 | + |
| 78 | + const updated = applyRunConfigurationOverrides(config, { |
| 79 | + vmArgs: '-Dfoo=bar' |
| 80 | + }); |
| 81 | + |
| 82 | + expect(updated.vmArgs).to.equal('-Dfoo=bar'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('merges environment variables and ignores malformed entries', () => { |
| 86 | + const config = createDebugConfiguration({ |
| 87 | + env: { EXISTING: '1' } |
| 88 | + }); |
| 89 | + |
| 90 | + const updated = applyRunConfigurationOverrides(config, { |
| 91 | + env: ' FIRST = one , invalid, =missingName, SECOND=two=2 ' |
| 92 | + }); |
| 93 | + |
| 94 | + expect(updated.env).to.deep.equal({ |
| 95 | + EXISTING: '1', |
| 96 | + 'FIRST ': ' one', |
| 97 | + SECOND: 'two=2' |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('parses environment variables through applyRunConfigurationOverrides', () => { |
| 102 | + const config = createDebugConfiguration(); |
| 103 | + |
| 104 | + const updated = applyRunConfigurationOverrides(config, { |
| 105 | + env: 'A=1, B=two' |
| 106 | + }); |
| 107 | + |
| 108 | + expect(updated.env).to.deep.equal({ |
| 109 | + A: '1', |
| 110 | + B: 'two' |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it('overrides existing environment variable keys with new values', () => { |
| 115 | + const config = createDebugConfiguration({ |
| 116 | + env: { A: '1', B: '2' } |
| 117 | + }); |
| 118 | + |
| 119 | + const updated = applyRunConfigurationOverrides(config, { |
| 120 | + env: 'B=updated, C=3' |
| 121 | + }); |
| 122 | + |
| 123 | + expect(updated.env).to.deep.equal({ |
| 124 | + A: '1', |
| 125 | + B: 'updated', |
| 126 | + C: '3' |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + it('creates a fresh env record when the current env is not an object', () => { |
| 131 | + const config = createDebugConfiguration({ |
| 132 | + env: 'invalid-env-shape' |
| 133 | + }); |
| 134 | + |
| 135 | + const updated = applyRunConfigurationOverrides(config, { |
| 136 | + env: 'A=1' |
| 137 | + }); |
| 138 | + |
| 139 | + expect(updated.env).to.deep.equal({ |
| 140 | + A: '1' |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + it('overrides cwd when provided', () => { |
| 145 | + const config = createDebugConfiguration(); |
| 146 | + |
| 147 | + const updated = applyRunConfigurationOverrides(config, { |
| 148 | + cwd: '/tmp/workdir' |
| 149 | + }); |
| 150 | + |
| 151 | + expect(updated.cwd).to.equal('/tmp/workdir'); |
| 152 | + }); |
| 153 | + |
| 154 | + it('does not replace existing values with empty-string overrides', () => { |
| 155 | + const config = createDebugConfiguration({ |
| 156 | + args: '--existing', |
| 157 | + vmArgs: '-Xmx512m', |
| 158 | + env: { EXISTING: '1' }, |
| 159 | + cwd: '/existing' |
| 160 | + }); |
| 161 | + |
| 162 | + const updated = applyRunConfigurationOverrides(config, { |
| 163 | + args: '', |
| 164 | + vmArgs: '', |
| 165 | + env: '', |
| 166 | + cwd: '' |
| 167 | + }); |
| 168 | + |
| 169 | + expect(updated).to.deep.equal({ |
| 170 | + type: 'java+', |
| 171 | + name: 'Test Config', |
| 172 | + request: 'launch', |
| 173 | + args: '--existing', |
| 174 | + vmArgs: '-Xmx512m', |
| 175 | + env: { EXISTING: '1' }, |
| 176 | + cwd: '/existing' |
| 177 | + }); |
| 178 | + }); |
| 179 | +}); |
| 180 | + |
| 181 | +const createDebugConfiguration = (overrides: Record<string, unknown> = {}): DebugConfiguration => { |
| 182 | + return { |
| 183 | + type: 'java+', |
| 184 | + name: 'Test Config', |
| 185 | + request: 'launch', |
| 186 | + ...overrides |
| 187 | + }; |
| 188 | +} |
0 commit comments