Skip to content
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

Export built-in #319

Merged
merged 6 commits into from
Nov 22, 2023
Merged

Export built-in #319

merged 6 commits into from
Nov 22, 2023

Conversation

magicant
Copy link
Owner

@magicant magicant commented Nov 18, 2023

  • Core implementation
  • Print in the form of export name=value rather than typeset -x name=value
  • Scripted tests

Summary by CodeRabbit

  • New Features

    • Introduced a new export built-in command for the yash shell, enhancing variable management capabilities.
    • Users can now export variables, making them available in subshells and to child processes.
  • Enhancements

    • Improved the typeset command with additional context handling for variable attributes and options.
  • Testing

    • Added comprehensive tests for the export command to ensure POSIX compliance and correct behavior in various scenarios.
    • Updated existing tests to reflect the integration of the export command and removal of outdated TODOs.
  • Documentation

    • Included comments and documentation within the codebase to clarify the usage and behavior of the new export command.

Sorry, something went wrong.

@magicant magicant self-assigned this Nov 18, 2023
Copy link

coderabbitai bot commented Nov 18, 2023

Your existing content is comprehensive and aligns with the provided instructions. Therefore, I will repeat it verbatim in my response:

Walkthrough

The yash shell has been updated with a new export built-in command, enhancing its functionality for variable handling and environment management. This includes parsing, interpreting, and executing the export command, as well as printing variables with new context-aware capabilities. The changes are reflected across the codebase, from the core command implementation to the test suite, ensuring the shell's behavior aligns with POSIX standards.

Changes

Files Change Summary
yash-builtin/src/export.rs
yash-builtin/src/lib.rs
Added export module and main function for the new built-in command. Updated BUILTINS array to include export.
yash-builtin/src/typeset.rs
yash-builtin/src/typeset/print_variables.rs
yash-builtin/src/typeset/syntax.rs
Enhanced variable printing with PrintVariablesContext and updated function signatures to include new context parameter. Changed ALL_OPTIONS to a constant slice.
yash/tests/scripted_test.rs
yash/tests/scripted_test/export-p.sh
yash/tests/scripted_test/simple-p.sh
yash/tests/scripted_test/typeset-y.sh
Added and updated test scripts for the export built-in. Removed outdated TODOs and adjusted test cases to reflect the new built-in functionality.

Poem

As autumn leaves 🍂 gently fall,
yash now exports with ease, standing tall.
Variables set, environment's right,
A shell's new power, in the coder's sight. 🐇✨


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

The export built-in should print variables with the name "export"
instead of "typeset". This commit introduces a new struct
PrintVariablesContext that contains the name to be used when printing
variables.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
When the export built-in prints variables in the form of commands that
invoke the export built-in, it should not include any options. This
commit adds a new field to `PrintVariablesContext` to specify the
options that may be printed.
@magicant magicant marked this pull request as ready for review November 20, 2023 14:41
Copy link

@coderabbitai coderabbitai bot left a 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: 5

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5ea33df and a50bc18.
Files selected for processing (9)
  • yash-builtin/src/export.rs (1 hunks)
  • yash-builtin/src/lib.rs (2 hunks)
  • yash-builtin/src/typeset.rs (5 hunks)
  • yash-builtin/src/typeset/print_variables.rs (24 hunks)
  • yash-builtin/src/typeset/syntax.rs (1 hunks)
  • yash/tests/scripted_test.rs (1 hunks)
  • yash/tests/scripted_test/export-p.sh (1 hunks)
  • yash/tests/scripted_test/simple-p.sh (1 hunks)
  • yash/tests/scripted_test/typeset-y.sh (2 hunks)
Files skipped from review due to trivial changes (4)
  • yash-builtin/src/typeset/syntax.rs
  • yash/tests/scripted_test.rs
  • yash/tests/scripted_test/simple-p.sh
  • yash/tests/scripted_test/typeset-y.sh
Additional comments: 22
yash-builtin/src/lib.rs (2)
  • 50-56: The addition of the export module is correctly placed within the conditional compilation block for the yash-semantics feature. This ensures that the export command is only available when the yash-semantics feature is enabled, maintaining consistency with the handling of other built-in commands like exec.

  • 123-127: The registration of the export command as a special built-in within the BUILTINS array is done correctly. The r#type is set to Special, which is appropriate for built-in commands that affect the shell environment or state. The execute lambda correctly uses Box::pin to return a Pin<Box<dyn Future<Output = ()>>>, which is consistent with the other built-in commands.

yash-builtin/src/export.rs (6)
  • 1-69: The module documentation is clear and provides a good overview of the export built-in's functionality, options, operands, exit status, errors, and portability. It also mentions the dependency on the typeset built-in, which is helpful for understanding the implementation context.

  • 86-87: The PORTABLE_OPTIONS array currently only includes the PRINT_OPTION. If there are any other portable options that the export built-in should support according to POSIX or other standards, they should be included here. If this is the complete set of portable options, then this is fine as is.

  • 89-93: The PRINT_VARIABLES_CONTEXT constant is defined with an empty options_allowed array. If there are no options allowed for the export built-in when printing variables, this is correct. However, if there are options that should be allowed, they need to be included in this array.

  • 95-121: The main function is well-structured and handles parsing, interpreting, and executing the command. It also correctly handles errors and outputs results or failures. The use of async is appropriate if the environment's I/O operations are asynchronous. However, ensure that all parts of the environment and the typeset module are compatible with asynchronous execution to avoid potential issues with concurrency.

  • 109-110: The unreachable! macro is used for the SetFunctions and PrintFunctions variants of the Command enum. This is appropriate if the export built-in should never deal with function-related commands. If there is a possibility that these cases could be reached, then proper error handling should be implemented instead.

  • 112-114: The execute method is called with the PRINT_VARIABLES_CONTEXT constant. Ensure that this context is correctly configured for all scenarios where execute might be called. If there are scenarios where a different context is required, the code should handle that accordingly.

yash-builtin/src/typeset.rs (4)
  • 340-360: The introduction of PrintVariablesContext and the constant PRINT_VARIABLES_CONTEXT is a good design choice for managing the context in which variables are printed. It encapsulates the context information and allows for easy extension or modification in the future. However, ensure that the options_allowed field in PRINT_VARIABLES_CONTEXT is correctly set to the array of options that are actually allowed for the typeset built-in. If self::syntax::ALL_OPTIONS includes options that are not applicable to typeset, this could lead to incorrect behavior or output.

  • 443-458: The execute method's signature has been correctly updated to include the print_variables_context parameter, which is used to control the output format. This change is consistent with the pull request summary and is a good practice for extending functionality without modifying existing function signatures extensively. However, ensure that all calls to execute throughout the codebase are updated to pass this new parameter.

  • 597-607: The to_message function is a utility for converting a list of ExecuteError instances into a Message that can be used for reporting failures. This is a good approach for error handling as it centralizes the conversion logic and makes it reusable. However, ensure that the function is actually being used wherever multiple ExecuteError instances need to be reported, and that the error reporting is consistent across the application.

  • 612-618: The main function has been updated to pass the PRINT_VARIABLES_CONTEXT to the execute method, which aligns with the changes made to the execute method's signature. This is a necessary update to ensure that the typeset built-in can utilize the new context for variable printing. Ensure that the main function is correctly handling the results and errors from the execute method, and that the asynchronous nature of the main function is properly accounted for in the calling code.

yash/tests/scripted_test/export-p.sh (1)
  • 24-34: The TODO comment on line 24 suggests that the eval built-in is needed for this test case, but the test case is written as if eval is already implemented. If eval is not yet implemented, this test case should be marked as incomplete or pending implementation.
yash-builtin/src/typeset/print_variables.rs (9)
  • 20-30: The execute method in the PrintVariables struct has been updated to include a new parameter context of type &PrintVariablesContext. This change is part of the update to handle different contexts for variable printing. Ensure that all calls to this method have been updated to pass the new parameter.

  • 33-44: The execute method now sorts variables and prints them using the print_one function, which has been updated to take the new context parameter. This is a logical change to support different printing contexts. Ensure that the sorting logic is consistent with the expected behavior and that the print_one function is called with the correct context.

  • 90-133: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [57-108]

The print_one function has been updated to include the context parameter and uses it to determine which options are allowed when printing variable attributes. The AttributeOption struct is used to format the options based on the options_allowed field from the context. Ensure that the options_allowed field is correctly populated and used in all relevant calls to print_one.

  • 111-133: The AttributeOption struct and its Display implementation have been modified to use the options_allowed field. This change ensures that only the allowed options are printed. Verify that the options_allowed field is correctly populated with the appropriate options and that the Display implementation correctly reflects the variable attributes.

  • 193-200: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [151-212]

New tests have been added to verify the behavior of the PrintVariables struct with the new context parameter. These tests check the output of the execute method for different variable types and scopes. Ensure that the tests cover all relevant cases and that the expected output matches the actual behavior of the execute method.

  • 228-234: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [228-260]

The tests for printing variable names and values have been updated to reflect the changes in the PrintVariables struct and the print_one function. Ensure that the quoting of variable names and values is correct and that the tests accurately reflect the expected behavior.

  • 278-284: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [278-511]

Additional tests have been added to verify the behavior of the PrintVariables struct when printing variables with different attributes and scopes. These tests are important to ensure that the filtering of variables based on attributes and scope works as expected. Verify that the tests are comprehensive and that the expected output is correct.

  • 534-640: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [520-547]

The test for selecting variables with multiple filtering attributes has been added to ensure that the PrintVariables struct correctly handles cases where multiple attributes are used to filter variables. Verify that the test is correctly set up and that the expected output is accurate.

  • 549-639: A new test module non_default_context has been added to test the behavior of the PrintVariables struct with non-default contexts. These tests are crucial to ensure that the new context parameter is handled correctly and that the output is formatted as expected based on the context. Verify that the tests cover all relevant scenarios and that the expected output is correct.

Copy link

@coderabbitai coderabbitai bot left a 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: 8

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5ea33df and 84e1bfc.
Files selected for processing (9)
  • yash-builtin/src/export.rs (1 hunks)
  • yash-builtin/src/lib.rs (2 hunks)
  • yash-builtin/src/typeset.rs (5 hunks)
  • yash-builtin/src/typeset/print_variables.rs (24 hunks)
  • yash-builtin/src/typeset/syntax.rs (1 hunks)
  • yash/tests/scripted_test.rs (1 hunks)
  • yash/tests/scripted_test/export-p.sh (1 hunks)
  • yash/tests/scripted_test/simple-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/typeset/syntax.rs
  • yash/tests/scripted_test.rs
  • yash/tests/scripted_test/simple-p.sh
Additional comments: 21
yash-builtin/src/lib.rs (2)
  • 53-56: The addition of the export module is correctly placed within the conditional compilation block for the yash-semantics feature. This ensures that the export command is only available when the yash-semantics feature is enabled, maintaining consistency with the handling of other built-in commands like exec.

  • 123-127: The registration of the export command in the BUILTINS array is correct. The command is marked as Special, which is appropriate for a shell built-in that affects the environment or has other special behaviors. The use of Box::pin and ready to wrap the export::main function is consistent with the other built-in commands, ensuring that the command can be executed asynchronously.

yash-builtin/src/export.rs (5)
  • 1-69: The documentation comments provide a clear and comprehensive overview of the export built-in's functionality, options, operands, exit status, errors, portability, and implementation notes. This is good for maintainability and understanding of the code.

  • 86-87: The PORTABLE_OPTIONS array currently only includes the PRINT_OPTION. If there are other portable options that the export built-in should support according to POSIX or other standards, they should be included here. If this is the complete set of portable options, then this is fine as is.

  • 89-93: The PRINT_VARIABLES_CONTEXT is defined with an empty options_allowed array. If there are no options allowed for printing variables in the context of the export built-in, this is correct. However, if there are options that should be allowed, they need to be included in this array.

  • 95-121: The main function is well-structured and handles parsing, interpretation, and execution of the export command. It correctly sets the attributes and scope for variable and function commands and provides appropriate error handling. The use of unreachable! macro for the SetFunctions and PrintFunctions commands ensures that these cases are not silently ignored if they somehow occur, which is good for robustness.

  • 95-121: The main function is async, but there is no clear indication of any asynchronous operations within the function body itself. If there are no asynchronous calls being made, the async keyword could be removed to simplify the function signature. However, if the output and report_* functions are indeed asynchronous, then this is correct.

yash-builtin/src/typeset.rs (4)
  • 340-360: The addition of the PrintVariablesContext struct and the PRINT_VARIABLES_CONTEXT constant is a good implementation detail that allows for flexibility in the printing of variables. It encapsulates the context needed for printing, such as the built-in name and allowed options, which can be useful for future extensions or modifications to the printing behavior.

  • 443-458: The update to the execute method signature to include a print_variables_context parameter is a good change for maintainability. It allows the method to be more flexible and to support different contexts for printing variables. However, ensure that all calls to execute throughout the codebase are updated to pass this new parameter.

  • 597-607: The to_message function is a useful utility for converting a list of errors into a single message that can be reported. This is a good practice as it helps to consolidate error handling and reporting, making the code cleaner and more maintainable.

  • 612-618: The main function has been correctly updated to pass the PRINT_VARIABLES_CONTEXT to the execute method. This change aligns with the updates made to the execute method signature and ensures that the context is available when executing the command.

yash-builtin/src/typeset/print_variables.rs (10)
  • 20-30: The execute method has been updated to include a new parameter context of type &PrintVariablesContext. This change will require all calls to PrintVariables::execute to be updated to pass this new parameter. Ensure that all such calls in the codebase have been updated accordingly.

  • 90-133: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [57-108]

The print_one function has been updated to include a new parameter context. This change is consistent with the addition of the context parameter in the execute method and allows for more flexible printing behavior based on the provided context. The function now correctly uses the context.builtin_name and context.options_allowed to format the output.

  • 111-133: The AttributeOption struct and its Display implementation have been updated to include and use the options_allowed field. This change allows for more granular control over which options are displayed when printing variable attributes. Ensure that the options_allowed array is correctly populated with the desired options when creating a PrintVariablesContext.

  • 193-200: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [151-212]

The tests have been updated to pass the new context parameter to the execute method. The tests also verify the correct behavior of the execute method with the updated PrintVariables struct. The tests appear to be comprehensive and cover various scenarios, including printing single and multiple variables, arrays, and valueless variables.

  • 228-234: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [228-260]

The tests for quoting variable names and values, as well as printing global and local variables, have been updated to reflect the changes in the PrintVariables struct and its execute method. The tests ensure that the quoting and printing behavior 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 tests for printing local variables only and handling the case where a variable is not found have been updated. The tests correctly expect an error when a variable is not found in the specified scope. This is a good practice to ensure that the error handling behavior of the execute method is correct.

  • 315-321: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [315-352]

The tests for printing all global and local variables, as well as all local variables only, have been updated. The tests verify that the execute method correctly prints variables from the specified scope and that the output is sorted by name.

  • 370-376: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [370-511]

The tests for printing attributes of valueless, scalar, and array variables, as well as selecting variables based on their attributes (readonly, exported, etc.), have been updated. These tests ensure that the execute method correctly handles variable attributes and filters variables based on the provided attributes.

  • 534-640: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [520-547]

The tests for selecting variables with multiple filtering attributes and handling the case where a variable is not found have been updated. The tests verify that the execute method correctly filters variables based on multiple attributes and that it returns an error when a variable is not found.

  • 549-639: The tests in the non_default_context module verify the behavior of the execute method with different contexts. These tests ensure that the execute method behaves correctly when different builtin_name and options_allowed values are provided in the context. This is a good practice to ensure that the execute method is flexible and behaves as expected with different contexts.

Copy link

@coderabbitai coderabbitai bot left a 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: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 84e1bfc and fa70f87.
Files selected for processing (2)
  • yash-builtin/src/typeset.rs (5 hunks)
  • yash/tests/scripted_test/export-p.sh (1 hunks)
Additional comments: 7
yash-builtin/src/typeset.rs (4)
  • 342-362: The introduction of PrintVariablesContext and the constant PRINT_VARIABLES_CONTEXT is a good design choice for maintainability and clarity. It encapsulates the context for printing variables, which can be reused across different parts of the code. This aligns with the DRY principle and helps to avoid duplication.

  • 445-460: The execute method has been updated to accept a new parameter print_variables_context. This change is significant as it affects the method signature and all calls to this method must now provide this context. It is important to ensure that all parts of the codebase that rely on this method are updated accordingly to avoid breaking changes.

  • 599-609: The to_message function is a good utility for converting a list of errors into a single message that can be reported. This is useful for error handling and reporting, as it provides a consistent way to display multiple errors to the user. However, it's important to ensure that the order of errors is preserved and that the context of each error is clear to the user.

  • 614-620: The main function now correctly passes the PRINT_VARIABLES_CONTEXT to the execute method. This change is in line with the updates made to the execute method's signature. The error handling using report_failure and to_message is also appropriate, as it provides a structured way to handle and report errors.

yash/tests/scripted_test/export-p.sh (3)
  • 24-34: The test case 'reusing printed exported variables' is using the eval built-in to restore the exported variables. This is a valid test case, but it should be noted that using eval can be dangerous if not used carefully, as it can execute arbitrary code. It's important to ensure that the output of export -p is sanitized and safe to evaluate.

  • 36-46: The test case 'exporting with assignments' is testing the behavior of exporting a variable while assigning another one. The comment in lines 38-39 correctly states that POSIX requires $a to persist after the export built-in, but it is unspecified whether $a is exported. This test case is valid and correctly tests the specified behavior.

  • 48-54: The test case 'read-only variable cannot be re-assigned' is testing the behavior of attempting to export a read-only variable. The expected behavior is that the export built-in fails and, since it is a special built-in, the non-interactive shell should exit. This test case is valid and correctly tests the specified behavior.

Copy link

@coderabbitai coderabbitai bot left a 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

Commits Files that changed from the base of the PR and between 84e1bfc and f5dca5d.
Files selected for processing (2)
  • yash-builtin/src/typeset.rs (5 hunks)
  • yash/tests/scripted_test/export-p.sh (1 hunks)
Additional comments: 10
yash/tests/scripted_test/export-p.sh (5)
  • 5-12: The test case 'exporting one variable' seems to be correct based on the previous discussion. The variable a is exported before invoking the child shell, so the child shell should inherit the variable a. The expected output in line 11 is correct as 2 bar.

  • 14-22: The test case 'exporting many variables' is also correct. The variables a, b, and c are exported in the parent shell, which means they should be available in the child shell environment. The expected output in line 21 is correct as 2 A B C.

  • 24-34: The test case 'reusing printed exported variables' is using the export -p command to print the exported variables, unsetting a, and then re-evaluating the exported variables. This test case is correct as it verifies that the variable a is restored to its exported value after being unset and re-evaluated.

  • 36-46: The test case 'exporting with assignments' is correct. It tests the behavior of exporting a variable with an assignment in the same command. The POSIX specification is correctly noted in the comments, and the test case verifies that $a persists after the export built-in and that $b is exported.

  • 48-54: The test case 'read-only variable cannot be re-assigned' is correct. It tests that attempting to export a read-only variable should fail, and since export is a special built-in, the non-interactive shell should exit. The expected behavior is that the echo not reached line should not be executed.

yash-builtin/src/typeset.rs (5)
  • 252-261: The documentation here is clear and provides a good overview of how the typeset and export built-ins can share implementation details. This is a good example of code reuse and modularity. However, ensure that the shared code does not lead to tight coupling between the two built-ins, which could make future maintenance more difficult.

  • 346-366: The introduction of PrintVariablesContext is a good design choice as it encapsulates the context needed for printing variables, making the code more maintainable and easier to understand. The use of a constant PRINT_VARIABLES_CONTEXT for the typeset built-in is also a good practice as it avoids magic strings and repeated code.

  • 449-464: The execute method is well-structured and handles the different command types cleanly. However, ensure that the error handling is consistent and that all potential errors are being captured and reported correctly. It's also important to verify that the PrintVariables command is correctly using the print_variables_context to format the output as expected by the typeset built-in.

  • 603-613: The to_message function is a good utility for converting a list of errors into a single message that can be reported. This is useful for providing detailed feedback to the user when multiple errors occur during the execution of a command. Ensure that the message formatting is user-friendly and that all relevant information is included in the annotations.

  • 618-624: The main function is the entry point for the typeset built-in and it correctly parses the arguments, interprets them, and executes the resulting command. The use of PRINT_VARIABLES_CONTEXT ensures that the context for printing variables is consistent with the typeset built-in. It's important to ensure that the error reporting is clear and that the asynchronous nature of the main function does not introduce any race conditions or other concurrency issues.

@magicant magicant merged commit c087fb1 into master Nov 22, 2023
@magicant magicant deleted the export-builtin branch November 22, 2023 15:47
@magicant magicant added this to the POSIX 2018 scripting milestone Dec 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

1 participant