-
Notifications
You must be signed in to change notification settings - Fork 26
fix(exists): return boolean values in JSON output instead of strings #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Changed INFO_EXISTS_TRUE and INFO_EXISTS_FALSE constants from strings to booleans - Updated OutputResult and FabricCLIOutput to accept bool | str message type - Added boolean-to-string conversion for text output format - Added default fallback to "text" format when format_type is None - Updated existing tests to work with boolean constants - Added new unit tests for boolean message JSON/text output Co-authored-by: ayeshurun <98805507+ayeshurun@users.noreply.github.com>
Extract inline boolean-to-string conversion logic into _format_message_for_text() helper function to improve readability and maintainability. Co-authored-by: ayeshurun <98805507+ayeshurun@users.noreply.github.com>
src/fabric_cli/utils/fab_ui.py
Outdated
| # Get format from output or config, default to text | ||
| format_type = output.output_format_type or fab_state_config.get_config( | ||
| fab_constant.FAB_OUTPUT_FORMAT | ||
| ) | ||
| ) or "text" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot revert this change and if necessary pass --output_format json in the tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted the default fallback and added explicit output_format="text" to the test helper functions (mkdir, rm, import_cmd) instead. Changes in commit 6c4730e.
- Removed 'or "text"' fallback from print_output_format as requested - Added explicit output_format="text" to mkdir, rm, and import_cmd test helpers - This ensures tests work without relying on implicit defaults Co-authored-by: ayeshurun <98805507+ayeshurun@users.noreply.github.com>
src/fabric_cli/utils/fab_ui.py
Outdated
| if output_result.message: | ||
| print_done(f"{output_result.message}\n") | ||
| if output_result.message is not None: | ||
| # Convert boolean messages to string for text output |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot remove the comment - this is clear from the code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed in commit 810a9ec.
tests/test_commands/conftest.py
Outdated
| command_path="rm", | ||
| path=element_full_path, | ||
| force=True, | ||
| output_format="text", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot why you added output_format="text"? now it will override the format_type also when we set the config OUTPUT_FORMAT. I believe it will make some test's failure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right - changed to use state_config.set_config(FAB_OUTPUT_FORMAT, "text") instead so it respects config overrides from fixtures. Fixed in commit 810a9ec.
tests/test_commands/conftest.py
Outdated
| input=content_path, | ||
| force=True, | ||
| format=format, | ||
| output_format="text", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot why you added output_format="text"? now it will override the format_type also when we set the config OUTPUT_FORMAT. I believe it will make some test's failure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right - changed to use state_config.set_config(FAB_OUTPUT_FORMAT, "text") instead so it respects config overrides from fixtures. Fixed in commit 810a9ec.
src/fabric_cli/core/fab_constant.py
Outdated
| # Info | ||
| INFO_EXISTS_TRUE = "true" | ||
| INFO_EXISTS_FALSE = "false" | ||
| INFO_EXISTS_TRUE = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ayeshurun If we removed the use of these constants in tests and the only place we use it is in exists command I think we can remove it and just return True/False - WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot since the only place we use it is in exists command, and not in tests, please remove those constants and update the occurences to use True or False accordingly inline.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the constants and replaced all usages with inline True and False in the exists command. Changes in commit f2080ba.
|
|
||
| @property | ||
| def message(self) -> Optional[str]: | ||
| def message(self) -> Optional[str | bool]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ayeshurun wouldn't be better approach to keep the login as is and only before printing the message in output format json convert the string boolean value? I believe this issue can accrue in other commands as well (can't think of an example now)
- Removed unnecessary comments that explain obvious code flow - Removed unused import (json) from test_exists.py - Changed test helpers to use state_config.set_config instead of args.output_format - This allows tests to override config via fixtures while helpers respect config - Cleaned up test comments that explain what's obvious from assertions Co-authored-by: aviatco <32952699+aviatco@users.noreply.github.com>
tests/test_commands/conftest.py
Outdated
|
|
||
| def mkdir(element_full_path, params=None): | ||
| state_config.set_config(fab_constant.FAB_CACHE_ENABLED, "false") | ||
| state_config.set_config(fab_constant.FAB_OUTPUT_FORMAT, "text") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot this is wrong. now any test who use this fixture will have FAB_OUTPUT_FORMAT value text, event if it was set in the test to another value. I think we should not have it here and if specific test need this configuration it should be at the test and not here.
Why did you add this?
Same for rm and import_cmd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're absolutely right - removed the state_config.set_config(FAB_OUTPUT_FORMAT, "text") lines from all three helper functions. Instead, changed the setup_default_format fixture to class scope so it runs before class-scoped fixtures like workspace. Fixed in commit 50ec5b8.
- Removed state_config.set_config(FAB_OUTPUT_FORMAT, "text") from mkdir, rm, and import_cmd helpers - These helper functions should not set config that would override test-specific settings - Changed setup_default_format fixture to class scope and set config directly - This allows class-scoped workspace fixture to have format config available Co-authored-by: aviatco <32952699+aviatco@users.noreply.github.com>
- Removed INFO_EXISTS_TRUE and INFO_EXISTS_FALSE constants from fab_constant.py - Replaced usages with inline True and False in fab_fs_exists.py - Constants were only used in exists command, so inline booleans are cleaner Co-authored-by: ayeshurun <98805507+ayeshurun@users.noreply.github.com>
📥 Pull Request
✨ Description of new changes
The
fab existscommand was returning"true"/"false"strings in JSON output instead of native JSON booleans. This caused logic errors where"false"evaluates as truthy in conditionals.Before:
{ "result": { "message": "true" } // String, not boolean }After:
{ "result": { "message": true } // Boolean }Changes
OutputResultandFabricCLIOutputto acceptmessage: str | bool_format_message_for_text()helper to convert booleans to strings for text output, preserving backward compatibilityTrue/False) instead of constantssetup_default_formatfixture to class scope to ensure format config is available for class-scoped fixtures likeworkspaceINFO_EXISTS_TRUE/INFO_EXISTS_FALSEconstants based on code review feedbackTesting
Text output remains unchanged (displays "true"/"false" strings). All existing tests pass:
Original prompt
fab existsreturns strings instead of booleans in--output_format json#107💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.