Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions bin/__snapshots__/cli.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rework this, since it should not snapshot your local user path.

- 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:
Expand Down
5 changes: 5 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
57 changes: 57 additions & 0 deletions bin/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down
5 changes: 4 additions & 1 deletion test/_cli-configfile/configFile.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
},
"no-sort": false,
"lineWidth": 80,
"rename": "Updated API"
"rename": "Updated API",
"keepComments": true,
"sortComponentsProps": true,
"split": false
}
Loading