diff --git a/bin/__snapshots__/cli.test.js.snap b/bin/__snapshots__/cli.test.js.snap index 18b0c56..53049e0 100644 --- a/bin/__snapshots__/cli.test.js.snap +++ b/bin/__snapshots__/cli.test.js.snap @@ -132,6 +132,17 @@ Options: " `; +exports[`openapi-format CLI command should respect boolean options from .openapiformatrc 1`] = ` +"================================================================================ +OpenAPI-Format CLI settings: +- .openapiformatrc: /Users/joseph/oss/openapi-format/.openapiformatrc +- Input file: test/yaml-no-sort-keep-comments/input.yaml +================================================================================ +✅ OpenAPI formatted successfully +================================================================================ +" +`; + exports[`openapi-format CLI command should show unused components 1`] = ` "================================================================================ OpenAPI-Format CLI settings: diff --git a/bin/cli.js b/bin/cli.js index 6414e9f..9ea4c86 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -90,9 +90,14 @@ async function run(oaFile, options) { // Merge .openapiformatrc and configOptions configOptions = Object.assign({}, defaultOptions, configOptions); + // Preserve config file values for options that have CLI defaults + // These must be set before Object.assign to prevent CLI defaults from overwriting config values options.lineWidth = configOptions?.lineWidth ?? options.lineWidth; options.sort = configOptions?.sort ?? options.sort; options.bundle = configOptions?.bundle ?? options.bundle; + options.keepComments = configOptions?.keepComments ?? options.keepComments; + options.sortComponentsProps = configOptions?.sortComponentsProps ?? options.sortComponentsProps; + options.split = configOptions?.split ?? options.split; // Merge configOptions and CLI options options = Object.assign({}, configOptions, options); diff --git a/bin/cli.test.js b/bin/cli.test.js index 6f4b2bd..3f1297d 100644 --- a/bin/cli.test.js +++ b/bin/cli.test.js @@ -146,6 +146,63 @@ describe('openapi-format CLI command', () => { fs.readFileSync.mockRestore(); }); + it('should respect boolean options from .openapiformatrc', async () => { + // Create a temporary .openapiformatrc file with boolean options + const defaultConfigPath = '.openapiformatrc'; + const configContent = { + keepComments: true, + sortComponentsProps: true, + split: false + }; + + // Write the config file + fs.writeFileSync(defaultConfigPath, JSON.stringify(configContent, null, 2)); + + try { + const inputFile = `test/yaml-no-sort-keep-comments/input.yaml`; + let result = await testUtils.cli([inputFile, '--no-sort'], '.'); + expect(result.code).toBe(0); + expect(result.stdout).toContain('formatted successfully'); + expect(result.stdout).toMatchSnapshot(); + // The output should preserve comments due to keepComments: true in config + expect(result.stderr).toContain('#'); + } finally { + // Clean up - remove the temporary config file + if (fs.existsSync(defaultConfigPath)) { + fs.unlinkSync(defaultConfigPath); + } + } + }); + + it('should use config file boolean options when set', async () => { + // Create a config file with boolean options set to non-default values + const defaultConfigPath = '.openapiformatrc'; + const configContent = { + keepComments: true, + sortComponentsProps: true, + split: false, + sort: false + }; + + // Write the config file + fs.writeFileSync(defaultConfigPath, JSON.stringify(configContent, null, 2)); + + try { + const inputFile = `test/yaml-no-sort-keep-comments/input.yaml`; + // Config file values should be applied + let result = await testUtils.cli([inputFile], '.'); + expect(result.code).toBe(0); + expect(result.stdout).toContain('formatted successfully'); + // The output should preserve comments due to config file setting + expect(result.stderr).toContain('#'); + } finally { + // Clean up - remove the temporary config file + if (fs.existsSync(defaultConfigPath)) { + fs.unlinkSync(defaultConfigPath); + } + } + }); + it('should use the casingFile', async () => { const path = `test/yaml-casing`; const inputFile = `${path}/input.yaml`; diff --git a/test/_cli-configfile/configFile.json b/test/_cli-configfile/configFile.json index 8ecc1fb..06362cc 100644 --- a/test/_cli-configfile/configFile.json +++ b/test/_cli-configfile/configFile.json @@ -22,5 +22,8 @@ }, "no-sort": false, "lineWidth": 80, - "rename": "Updated API" + "rename": "Updated API", + "keepComments": true, + "sortComponentsProps": true, + "split": false } \ No newline at end of file