-
Notifications
You must be signed in to change notification settings - Fork 4
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
Readonly builtin #320
Readonly builtin #320
Conversation
It is found that PrintVariablesContext can be used to print not only variables but also functions. This commit renames it to PrintContext to reflect the fact.
A reference to an object with a static lifetime should be const. If it is declared static, not only the referenced object but also the reference itself will occupy a memory for a static lifetime.
This commit introduces a new field `builtin_is_significant` to the `PrintContext` struct. When this field is true, the command that invokes the built-in is always printed regardless of the attributes of the variables or functions. This field is true for the export and readonly built-ins, but false for the typeset built-in.
The existing content is still valid, so I will repeat it verbatim: WalkthroughThe updates across various shell built-in commands ( Changes
TipsChat with CodeRabbit Bot (
|
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.
Review Status
Actionable comments generated: 0
Configuration used: CodeRabbit UI
Files selected for processing (8)
- yash-builtin/src/export.rs (5 hunks)
- yash-builtin/src/lib.rs (1 hunks)
- yash-builtin/src/readonly.rs (2 hunks)
- yash-builtin/src/typeset.rs (10 hunks)
- yash-builtin/src/typeset/print_functions.rs (12 hunks)
- yash-builtin/src/typeset/print_variables.rs (27 hunks)
- yash/tests/scripted_test.rs (1 hunks)
- yash/tests/scripted_test/readonly-p.sh (1 hunks)
Files skipped from review due to trivial changes (1)
- yash/tests/scripted_test.rs
Additional comments: 53
yash-builtin/src/lib.rs (1)
- 144-148:
Ensure that thereadonly::main
function is now properly returning aFuture
and that this change is compatible with the rest of the codebase wherereadonly::main
is expected to be called.yash/tests/scripted_test/readonly-p.sh (3)
5-11:
The test case for making a single variable read-only is well-defined and checks the expected behavior correctly.13-20:
The test case for making multiple variables read-only is well-defined and checks the expected behavior correctly.26-32:
The test case for ensuring a read-only variable cannot be reassigned is well-defined and includes an explanation for the expected behavior when thereadonly
built-in fails.yash-builtin/src/readonly.rs (3)
17-191: The documentation added for the
readonly
built-in is comprehensive and well-structured, providing clear information on its behavior, options, operands, and output. This will greatly enhance the maintainability and usability of the code.222-248: The
main
function has been refactored to be asynchronous and now properly handles the execution of thereadonly
built-in command. The use ofparse
andinterpret
functions from thetypeset
module, along with the newPRINT_CONTEXT
constant, shows a good separation of concerns and code reuse.262-268: The test case
builtin_defines_read_only_variable
checks if thereadonly
built-in correctly defines a read-only variable. However, the test case seems to be missing an assertion to check if the variable is indeed set as read-only. This should be verified and potentially corrected to ensure the test case is comprehensive.yash-builtin/src/export.rs (5)
26-33: The updated description of the
-p
option in theexport
built-in command is clear and informative, explaining the behavior with and without operands.43-61: The clarification on operands and standard output behavior when exporting or printing variables is well-documented and improves the understanding of the command's functionality.
80-87: The implementation notes provide valuable insight into the relationship between the
export
andtypeset
built-ins, which is useful for developers working with the codebase.95-113: The refactoring to rename
PRINT_VARIABLES_CONTEXT
toPRINT_CONTEXT
and the associated updates to structures and functions align with the changes mentioned in the summary and improve code consistency.129-134: The use of
unreachable!
macro forCommand::SetFunctions
andCommand::PrintFunctions
variants ensures that theexport
built-in does not handle function-related commands, which is consistent with its intended behavior.yash-builtin/src/typeset.rs (5)
82-86: The documentation mentions that variables are printed in alphabetical order, but there is no code in the provided hunks that shows the implementation of this sorting. Verify that the sorting logic is implemented elsewhere in the codebase.
126-131: The documentation explains the special handling of array variables when printing, including the omission of the typeset command if no options are applied. Ensure that the implementation of this behavior is consistent with the documentation.
177-181: The documentation states that shell functions are printed in alphabetical order. Similar to the sorting of variables, verify that the sorting logic for functions is implemented elsewhere in the codebase.
263-270: The documentation has been updated to reflect the parameterization of common functions to support different behaviors of the built-ins. Ensure that the refactoring has been done correctly and that the parameterization does not introduce any regressions or unexpected behaviors.
482-493: The
execute
method is responsible for executing the command and returning the result or errors. Ensure that the error handling and result output are consistent with the new asynchronous error reporting and result output functions mentioned in the pull request summary.yash-builtin/src/typeset/print_functions.rs (12)
22-30: The
execute
method in thePrintFunctions
struct has been updated to include a newcontext
parameter of type&PrintContext
. This change aligns with the summary indicating that theprint_one
function now uses thecontext
parameter to determine options for printing and the significance of the built-in name. The method signature change is consistent with the described enhancements.34-45: The sorting of functions is done using an unstable sort, which is fine for sorting by function names as they are expected to be unique. However, the comment on line 34 indicates a TODO for honoring the collation order in the locale, which is not addressed in this change. This is consistent with the learning that locale-specific sorting is recognized as a potential enhancement but not implemented due to extensive changes required across the codebase.
67-98: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [56-96]
The
print_one
function has been updated to include thecontext
parameter and uses it to determine which options to print and whether the built-in name is significant. This change is in line with the summary of changes and the new tests added to cover different scenarios using thePrintContext
struct. The implementation appears to be correct and matches the intended functionality.
123-127: The test
printing_one_function
checks the output of theexecute
method for a single function. The test is straightforward and correctly asserts the expected output. The use ofPRINT_CONTEXT
is consistent with the changes made to thePrintFunctions
struct.147-152: The test
printing_multiple_functions
checks the output of theexecute
method for multiple functions. The test is well-structured and correctly asserts the expected output. The use ofPRINT_CONTEXT
is consistent with the changes made to thePrintFunctions
struct.173-178: The test
printing_all_functions
checks the output of theexecute
method when all functions are to be printed. The test correctly asserts that the output is sorted by function name, which is consistent with the implementation in theexecute
method.192-197: The test
quoting_function_name
checks the output of theexecute
method for a function with a name that requires quoting. The test correctly asserts the expected output, including the use of thefunction
keyword and quoting of the function name.211-215: The test
printing_readonly_functions
checks the output of theexecute
method for a read-only function. The test correctly asserts the expected output, including the command to set the function attributes. The use ofPRINT_CONTEXT
is consistent with the changes made to thePrintFunctions
struct.236-241: The test
selecting_readonly_functions
checks the output of theexecute
method when filtering for read-only functions. The test correctly asserts the expected output, including the command to set the function attributes. The use ofPRINT_CONTEXT
is consistent with the changes made to thePrintFunctions
struct.261-265: The test
selecting_non_readonly_functions
checks the output of theexecute
method when filtering for non-read-only functions. The test correctly asserts the expected output, which should only include the non-read-only function.275-283: The test
function_not_found
checks the error handling of theexecute
method when the specified functions are not found in theFunctionSet
. The test correctly asserts the expected errors, which are instances ofExecuteError::PrintUnsetFunction
.287-393: The tests in the
non_default_context
module verify the behavior of theexecute
method with differentPrintContext
configurations. The tests correctly assert the expected outputs based on the context settings, such as the significance of the built-in name and the allowed options. These tests are important to ensure that thePrintContext
struct is being used correctly within thePrintFunctions
struct.yash-builtin/src/typeset/print_variables.rs (24)
23-29: The
execute
function inPrintVariables
returns aResult<String, Vec<ExecuteError>>
, which is good for error handling. However, it's important to ensure that all callers of this function are updated to handle the new return type if this is a change from the previous implementation.57-63: The
print_one
function has been updated to use the newPrintContext
. Ensure that all calls to this function have been updated to pass the newPrintContext
instead of the previousPrintVariablesContext
.89-95: The logic for printing array variables has been updated to consider
builtin_is_significant
. Ensure that the new behavior is consistent with the expected output format for array variables in all use cases.151-157: The test
printing_one_variable
uses a hardcodedPRINT_CONTEXT
. IfPRINT_CONTEXT
has been updated or replaced in the codebase, this test should be updated to reflect those changes.174-180: Similar to the previous comment, the test
printing_multiple_variables
should be verified to use the correct context if there have been changes toPRINT_CONTEXT
.193-198: The test
printing_array_variable
should be verified for the correct usage ofPRINT_CONTEXT
, especially since array printing logic has been updated in the main code.207-212: The test
printing_valueless_variable
should be verified for the correct usage ofPRINT_CONTEXT
.228-234: The test
quoting_variable_names_and_values
should be verified for the correct usage ofPRINT_CONTEXT
.254-260: The test
printing_global_and_local_variables_at_once
should be verified for the correct usage ofPRINT_CONTEXT
.278-284: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [278-292]
The tests
printing_local_variables_only
and the following test for printing a variable that is not found in the local scope should be verified for the correct usage ofPRINT_CONTEXT
.
315-321: The test
printing_all_global_and_local_variables
should be verified for the correct usage ofPRINT_CONTEXT
.346-352: The test
printing_all_local_variables
should be verified for the correct usage ofPRINT_CONTEXT
.370-376: The test
printing_attributes_of_valueless_variables
should be verified for the correct usage ofPRINT_CONTEXT
.397-403: The test
printing_attributes_of_scalar_variables
should be verified for the correct usage ofPRINT_CONTEXT
.424-430: The test
printing_attributes_of_array_variables
should be verified for the correct usage ofPRINT_CONTEXT
.457-462: The test
selecting_readonly_variables
should be verified for the correct usage ofPRINT_CONTEXT
.473-478: The test
selecting_non_readedonly_variables
should be verified for the correct usage ofPRINT_CONTEXT
.489-494: The test
selecting_exported_variables
should be verified for the correct usage ofPRINT_CONTEXT
.505-510: The test
selecting_non_exported_variables
should be verified for the correct usage ofPRINT_CONTEXT
.520-524: The test
selecting_with_multiple_filtering_attributes
should be verified for the correct usage ofPRINT_CONTEXT
.534-541: The test
variable_not_found
should be verified for the correct usage ofPRINT_CONTEXT
.562-570: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [562-577]
The test
builtin_name
in thenon_default_context
module should be verified for the correct usage ofPRINT_CONTEXT
and to ensure that the newbuiltin_name
field is being used correctly.
581-607: The tests
builtin_is_significant
andinsignificant_builtin_with_attributed_array
should be verified for the correct usage ofPRINT_CONTEXT
and to ensure that the newbuiltin_is_significant
field is being used correctly.650-658: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [650-682]
The tests in the
options_allowed
section should be verified for the correct usage ofPRINT_CONTEXT
and to ensure that the newoptions_allowed
field is being used correctly.
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.
Review Status
Actionable comments generated: 0
Configuration used: CodeRabbit UI
Files selected for processing (1)
- yash/tests/scripted_test/typeset-y.sh (2 hunks)
Files skipped from review due to trivial changes (1)
- yash/tests/scripted_test/typeset-y.sh
16929dc
to
8a2d50a
Compare
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.
Review Status
Actionable comments generated: 4
Configuration used: CodeRabbit UI
Files selected for processing (9)
- yash-builtin/src/export.rs (5 hunks)
- yash-builtin/src/lib.rs (1 hunks)
- yash-builtin/src/readonly.rs (2 hunks)
- yash-builtin/src/typeset.rs (10 hunks)
- yash-builtin/src/typeset/print_functions.rs (12 hunks)
- yash-builtin/src/typeset/print_variables.rs (27 hunks)
- yash/tests/scripted_test.rs (1 hunks)
- yash/tests/scripted_test/readonly-p.sh (1 hunks)
- yash/tests/scripted_test/typeset-y.sh (2 hunks)
Files skipped from review due to trivial changes (3)
- yash-builtin/src/lib.rs
- yash/tests/scripted_test.rs
- yash/tests/scripted_test/typeset-y.sh
Additional comments: 55
yash-builtin/src/readonly.rs (5)
60-60: The TODO comment about linking to the eval built-in should be addressed if the documentation is ready to be linked.
129-129: Similar to the previous comment, the TODO about linking to the eval built-in should be addressed if the documentation is ready to be linked.
220-220: The TODO comment about splitting into syntax and semantics submodules should be removed if this refactoring has been completed, or it should be updated to reflect the current state of the codebase.
223-248: The
main
function has been correctly updated to be asynchronous and return aResult
wrapped in a future. The use ofparse
,interpret
, andexecute
functions for handling options, operands, and command execution is a good refactor for maintainability and readability.262-268: The test case
builtin_defines_read_only_variable
has been updated to reflect the new asynchronous nature of themain
function. The use ofnow_or_never()
fromfutures_util
to immediately resolve the future is appropriate for testing.yash-builtin/src/export.rs (5)
26-33: The updated documentation clearly explains the behavior of the
export
built-in with and without the-p
option. This aligns with the summary of changes indicating clarification in the documentation.43-61: The documentation now includes a description of the operands and the standard output behavior, which is consistent with the summary of changes. It is important to ensure that the behavior described here matches the actual implementation in the code.
80-87: The implementation notes have been updated to reflect the relationship between the
export
andtypeset
built-ins. This should be verified to ensure that the behavior described is consistent with the actual code.95-113: The
PRINT_VARIABLES_CONTEXT
has been renamed toPRINT_CONTEXT
and its type updated toPrintContext
, as mentioned in the summary. This change should be cross-checked with other parts of the codebase to ensure consistency.129-134: The
main
function now handles theCommand::SetFunctions
andCommand::PrintFunctions
cases withunreachable!
macros, which is a good way to assert conditions that are expected to never happen. However, it is crucial to ensure that these cases are indeed not possible in the current context of theexport
built-in.yash-builtin/src/typeset.rs (9)
82-86: The TODO comment about linking to the eval built-in should be addressed or removed if it is no longer relevant.
163-167: The note about the built-in operating on existing shell functions only is important. It should be made clear in the documentation that this built-in cannot be used to create new functions or modify the body of existing functions.
227-234: The error conditions are well documented, which is good for maintainability and understanding the limitations of the built-in's functionality.
263-270: The explanation of how the implementation of the typeset built-in is shared with the
export
andreadonly
built-ins is clear and provides good insight into the code's modularity.354-355: The
Scope
enum is well-defined, making it clear how the scope of operations is determined for printing variables.393-394: The
PrintFunctions
struct is well-defined, making it clear how the attributes for printing functions are determined.402-428: The
PrintContext
struct is a good addition for controlling the details of the commands when printing variables or functions. It allows for flexibility and reusability of the printing logic.482-493: The
execute
method is well-structured and handles the different command types effectively. It's good to see that it returns aResult
type, which is idiomatic Rust for error handling.646-652: The
main
function is nowasync
and properly handles the execution of the typeset built-in, including error reporting. This aligns with the asynchronous nature of the shell environment.yash-builtin/src/typeset/print_functions.rs (13)
22-30: The
execute
method in thePrintFunctions
struct has been updated to take acontext
parameter of type&PrintContext
. This change aligns with the overall refactoring effort to parameterize the implementation and control the details of the commands using thePrintContext
struct.34-35: The comment indicates a TODO for honoring the collation order in the locale. This is a known enhancement that has not been implemented due to the extensive changes required across the codebase. It's important to track this as a potential future improvement.
56-60: The
print_one
function now takes an extra parametercontext
of type&PrintContext
. This change is part of the refactoring to pass additional context information to the printing functions.67-96: The
print_one
function has been updated to include logic for printing function attributes based on thePrintContext
. This includes handling thebuiltin_is_significant
flag and printing options allowed by the context. This change enhances the flexibility and configurability of the printing behavior.123-127: A test case has been added to verify the printing of a single function. This test ensures that the
execute
method correctly prints the function body when provided with a specific function name.147-152: Another test case has been added to verify the printing of multiple functions. This test checks that the
execute
method correctly prints the bodies of multiple functions in the order they are provided.173-178: This test case verifies that all functions are printed and that the result is sorted by function name. This aligns with the new behavior of printing shell variables and functions in alphabetical order.
192-197: The test case for quoting function names ensures that function names that require quoting are correctly prefixed with the
function
keyword and quoted in the output.211-216: The test case for printing read-only functions checks that the
execute
method correctly prints the function body and the command to set the function as read-only.236-241: This test case verifies the selection and printing of read-only functions, ensuring that the
execute
method correctly filters and prints functions based on their attributes.261-266: The test case for selecting non-read-only functions ensures that the
execute
method correctly filters out read-only functions when the attribute filter specifies non-read-only functions.275-284: The test case for handling functions not found in the
FunctionSet
checks that theexecute
method correctly returns errors for each function that does not exist.290-393: A series of test cases have been added under the
non_default_context
module to cover different scenarios using thePrintContext
struct. These tests verify the behavior of theexecute
method when thebuiltin_name
,builtin_is_significant
, andoptions_allowed
fields of thePrintContext
are varied.yash-builtin/src/typeset/print_variables.rs (23)
23-29: The
execute
method has been updated to use the newPrintContext
type. However, the method signature change fromPrintVariablesContext
toPrintContext
may affect other parts of the code that rely on this method. Ensure that all calls toexecute
have been updated accordingly.57-63: The
print_one
function has been updated to use the newPrintContext
type. This change is consistent with the refactoring mentioned in the summary. Ensure that all calls toprint_one
have been updated to pass the newPrintContext
type.89-95: The logic for printing array variables has been updated to include a check for
builtin_is_significant
. This is a new field in thePrintContext
struct and affects the output format. Ensure that the new behavior is intended and correctly implemented across all use cases.151-157: The test case
printing_one_variable
has been updated to use the newPrintContext
instead of the oldPRINT_VARIABLES_CONTEXT
. This change is consistent with the refactoring mentioned in the summary. Ensure that the test case still passes with the new context.174-180: The test case
printing_multiple_variables
has been updated to use the newPrintContext
. Ensure that the test case still passes with the new context and that the output format is as expected.193-198: The test case
printing_array_variable
has been updated to use the newPrintContext
. Ensure that the test case still passes with the new context and that the output format is as expected.207-212: The test case
printing_valueless_variable
has been updated to use the newPrintContext
. Ensure that the test case still passes with the new context and that the output format is as expected.228-234: The test case
quoting_variable_names_and_values
has been updated to use the newPrintContext
. Ensure that the test case still passes with the new context and that the output format is as expected.254-260: The test case
printing_global_and_local_variables_at_once
has been updated to use the newPrintContext
. Ensure that the test case still passes with the new context and that the output format is as expected.278-284: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [278-293]
The test cases for printing local variables only and attempting to print a global variable in the local scope have been updated to use the new
PrintContext
. Ensure that the test cases still pass with the new context and that the output format is as expected, including the error case.
315-321: The test case
printing_all_global_and_local_variables
has been updated to use the newPrintContext
. Ensure that the test case still passes with the new context and that the output format is as expected.346-352: The test case
printing_all_local_variables
has been updated to use the newPrintContext
. Ensure that the test case still passes with the new context and that the output format is as expected.370-376: The test case
printing_attributes_of_valueless_variables
has been updated to use the newPrintContext
. Ensure that the test case still passes with the new context and that the output format is as expected.397-403: The test case
printing_attributes_of_scalar_variables
has been updated to use the newPrintContext
. Ensure that the test case still passes with the new context and that the output format is as expected.424-430: The test case
printing_attributes_of_array_variables
has been updated to use the newPrintContext
. Ensure that the test case still passes with the new context and that the output format is as expected.457-462: The test case
selecting_readonly_variables
has been updated to use the newPrintContext
. Ensure that the test case still passes with the new context and that the output format is as expected.473-478: The test case
selecting_non_readonly_variables
has been updated to use the newPrintContext
. Ensure that the test case still passes with the new context and that the output format is as expected.489-494: The test case
selecting_exported_variables
has been updated to use the newPrintContext
. Ensure that the test case still passes with the new context and that the output format is as expected.505-510: The test case
selecting_non_exported_variables
has been updated to use the newPrintContext
. Ensure that the test case still passes with the new context and that the output format is as expected.520-524: The test case
selecting_with_multiple_filtering_attributes
has been updated to use the newPrintContext
. Ensure that the test case still passes with the new context and that the output format is as expected.534-541: The test case
variable_not_found
has been updated to use the newPrintContext
. Ensure that the test case still passes with the new context and that the error handling is correct.562-570: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [562-577]
The test cases in the
non_default_context
module have been updated to use the newPrintContext
. Ensure that the test cases still pass with the new context and that the output format is as expected, including the handling of thebuiltin_name
andbuiltin_is_significant
fields.
- 650-658: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [650-682]
The test cases for
options_allowed
have been updated to use the newPrintContext
. Ensure that the test cases still pass with the new context and that the output format is as expected, including the handling of different option specifications.
The previous tests contained redundant commands that were not run as a consequence of the preceding assignment failing. This patch removes those redundant commands and adds comments to explain the expected behavior.
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.
Review Status
Actionable comments generated: 1
Configuration used: CodeRabbit UI
Files selected for processing (2)
- yash-builtin/src/typeset.rs (10 hunks)
- yash/tests/scripted_test/readonly-p.sh (1 hunks)
Additional comments: 8
yash-builtin/src/typeset.rs (8)
82-86: The TODO comment about linking to the eval built-in should be addressed or removed if it is no longer relevant.
124-145: The explanation and example provided for the standard output when printing variables, especially array variables, is clear and informative. This aligns with the previous learning about adding an example of the output format for clarity.
174-175: The clarification that it is an error to modify a non-existing function is important for preventing undefined behavior and should be clearly documented.
236-242: The error conditions are well documented, which is crucial for users to understand the limitations and expected behavior of the built-in commands.
270-278: The modification of the implementation notes to indicate that the implementation of the typeset built-in is also used by the
export
andreadonly
built-ins is a good practice for maintainability and understanding of the codebase.363-368: The
Scope
enum is well-defined, providing clarity on the behavior of the typeset built-in in different contexts.402-407: The
PrintFunctions
struct is well-defined, providing clarity on the behavior of the typeset built-in when printing functions.491-503: The
execute
function is well-implemented, handling different commands appropriately and returning aResult
type which is a good practice for error handling.
Summary by CodeRabbit
New Features
export
andreadonly
built-in commands with clarified behavior and additional documentation.typeset
built-in to print variables and functions in alphabetical order and handle array variables more effectively.Documentation
readonly
built-in covering behavior, options, operands, and standard output.Refactor
PRINT_VARIABLES_CONTEXT
toPRINT_CONTEXT
across various modules for consistency.typeset
built-in implementation to support different behaviors and introducedPrintContext
for command details.Tests
readonly
andtypeset
built-ins to ensure compliance with POSIX standards.readonly-p.sh
and updatedtypeset-y.sh
test scripts for comprehensive testing of shell built-ins.Bug Fixes
readonly
built-in to prevent re-assignment of read-only variables.