|
| 1 | +import { expect } from 'chai'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the export compile commands logic in cmakeDriver.ts |
| 5 | + * |
| 6 | + * This tests the LOGIC PATTERN used for deciding whether to inject |
| 7 | + * -DCMAKE_EXPORT_COMPILE_COMMANDS into CMake configure arguments. |
| 8 | + * |
| 9 | + * The actual implementation lives in cmakeDriver.ts but depends on vscode, |
| 10 | + * so we mirror the decision logic here to validate correctness. |
| 11 | + * |
| 12 | + * Issue #4893: When cmake.exportCompileCommandsFile is set to false, |
| 13 | + * the flag should NOT be passed at all (not even as FALSE), to avoid |
| 14 | + * CMake warnings for projects with LANGUAGES NONE. |
| 15 | + */ |
| 16 | + |
| 17 | +suite('[Export Compile Commands Logic]', () => { |
| 18 | + /** |
| 19 | + * Mirrors the logic from cmakeDriver.ts generateConfigArgsFromPreset() |
| 20 | + * and generateCMakeSettingsFlags() |
| 21 | + * |
| 22 | + * @param exportCompileCommandsSetting The raw setting value (undefined, true, or false) |
| 23 | + * @param hasExportCompileCommandsInPresetOrArgs Whether preset or args already specify the flag |
| 24 | + * @returns Whether to inject the -DCMAKE_EXPORT_COMPILE_COMMANDS flag |
| 25 | + */ |
| 26 | + function shouldInjectExportCompileCommandsFlag( |
| 27 | + exportCompileCommandsSetting: boolean | undefined, |
| 28 | + hasExportCompileCommandsInPresetOrArgs: boolean |
| 29 | + ): boolean { |
| 30 | + // Mirror the logic from cmakeDriver.ts: |
| 31 | + // const exportCompileCommandsFile: boolean = exportCompileCommandsSetting === undefined ? true : (exportCompileCommandsSetting || false); |
| 32 | + const exportCompileCommandsFile: boolean = exportCompileCommandsSetting === undefined ? true : (exportCompileCommandsSetting || false); |
| 33 | + |
| 34 | + // For presets mode (generateConfigArgsFromPreset): |
| 35 | + // if (!hasExportCompileCommands && exportCompileCommandsFile) |
| 36 | + // |
| 37 | + // For kits mode (generateCMakeSettingsFlags): |
| 38 | + // if (exportCompileCommandsFile) |
| 39 | + // |
| 40 | + // Both modes now check exportCompileCommandsFile is truthy before injecting |
| 41 | + return !hasExportCompileCommandsInPresetOrArgs && exportCompileCommandsFile; |
| 42 | + } |
| 43 | + |
| 44 | + suite('Setting is undefined (default)', () => { |
| 45 | + test('Should inject flag when preset/args do not specify it', () => { |
| 46 | + const result = shouldInjectExportCompileCommandsFlag(undefined, false); |
| 47 | + expect(result).to.equal(true, 'Default behavior should inject TRUE to enable compile_commands.json'); |
| 48 | + }); |
| 49 | + |
| 50 | + test('Should NOT inject flag when preset/args already specify it', () => { |
| 51 | + const result = shouldInjectExportCompileCommandsFlag(undefined, true); |
| 52 | + expect(result).to.equal(false, 'Should respect preset/args override'); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + suite('Setting is explicitly true', () => { |
| 57 | + test('Should inject flag when preset/args do not specify it', () => { |
| 58 | + const result = shouldInjectExportCompileCommandsFlag(true, false); |
| 59 | + expect(result).to.equal(true, 'Explicit true should inject the flag'); |
| 60 | + }); |
| 61 | + |
| 62 | + test('Should NOT inject flag when preset/args already specify it', () => { |
| 63 | + const result = shouldInjectExportCompileCommandsFlag(true, true); |
| 64 | + expect(result).to.equal(false, 'Should respect preset/args override'); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + suite('Setting is explicitly false (Issue #4893 fix)', () => { |
| 69 | + test('Should NOT inject flag when preset/args do not specify it', () => { |
| 70 | + const result = shouldInjectExportCompileCommandsFlag(false, false); |
| 71 | + expect(result).to.equal(false, 'Explicit false should NOT inject the flag at all'); |
| 72 | + }); |
| 73 | + |
| 74 | + test('Should NOT inject flag when preset/args already specify it', () => { |
| 75 | + const result = shouldInjectExportCompileCommandsFlag(false, true); |
| 76 | + expect(result).to.equal(false, 'Should respect preset/args override'); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + suite('exportCompileCommandsFile computation', () => { |
| 81 | + /** |
| 82 | + * Mirrors the computation: exportCompileCommandsSetting === undefined ? true : (exportCompileCommandsSetting || false) |
| 83 | + */ |
| 84 | + function computeExportCompileCommandsFile(setting: boolean | undefined): boolean { |
| 85 | + return setting === undefined ? true : (setting || false); |
| 86 | + } |
| 87 | + |
| 88 | + test('undefined setting defaults to true', () => { |
| 89 | + expect(computeExportCompileCommandsFile(undefined)).to.equal(true); |
| 90 | + }); |
| 91 | + |
| 92 | + test('explicit true remains true', () => { |
| 93 | + expect(computeExportCompileCommandsFile(true)).to.equal(true); |
| 94 | + }); |
| 95 | + |
| 96 | + test('explicit false becomes false', () => { |
| 97 | + expect(computeExportCompileCommandsFile(false)).to.equal(false); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + suite('Kits mode logic (generateCMakeSettingsFlags)', () => { |
| 102 | + /** |
| 103 | + * In kits mode, the logic is simpler: just check if exportCompileCommandsFile is truthy |
| 104 | + * (no hasExportCompileCommands check needed as configureSettings handles that) |
| 105 | + */ |
| 106 | + function shouldAddToSettingMapInKitsMode(exportCompileCommandsSetting: boolean | undefined): boolean { |
| 107 | + const exportCompileCommandsFile: boolean = exportCompileCommandsSetting === undefined ? true : (exportCompileCommandsSetting || false); |
| 108 | + return exportCompileCommandsFile; |
| 109 | + } |
| 110 | + |
| 111 | + test('undefined setting should add to settingMap', () => { |
| 112 | + expect(shouldAddToSettingMapInKitsMode(undefined)).to.equal(true); |
| 113 | + }); |
| 114 | + |
| 115 | + test('explicit true should add to settingMap', () => { |
| 116 | + expect(shouldAddToSettingMapInKitsMode(true)).to.equal(true); |
| 117 | + }); |
| 118 | + |
| 119 | + test('explicit false should NOT add to settingMap (Issue #4893 fix)', () => { |
| 120 | + expect(shouldAddToSettingMapInKitsMode(false)).to.equal(false); |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments