intents icon indicating copy to clipboard operation
intents copied to clipboard

Tests: Filter response files for files ending with yaml

Open zyberzero opened this issue 1 year ago • 4 comments

When creating some translations I noticed that when I, in my editor of choice, had opened a response file the tests failed. It turned out that due to my editor created a .swp file in the response folder the tests failed as the yaml parser doesn't (correctly) interpret the swap file as valid UTF-8 and throws an exception, for example this:

_______________________________________________ ERROR at setup of test_climate_HassTurnOff[sv] _______________________________________________

language = 'sv'

    @pytest.fixture(scope="session")
    def language_responses(language: str) -> dict[str, Any]:
        """Load intent responses for a language."""
        merged_responses: dict = {}
        for response_file in (RESPONSES_DIR / language).iterdir():
>               merge_dict(merged_responses, yaml.safe_load(response_file.read_text()))

tests/conftest.py:76: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/lib/python3.10/pathlib.py:1135: in read_text
    return f.read()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <encodings.utf_8.IncrementalDecoder object at 0x7fc56b37fd60>
input = b'b0VIM 8.2\x00\x00\x00\x00\x10\x00\x00\x9f\xdb\xa4fR\x05I\x00?\xff\x01\x00zyber\x00\x00\x00\x00\x00\x00\x00\x00\x00\x...e }}"\x00      default: "Slog av {{ slots.name }}"\x00    HassTurnOff:\x00  intents:\x00responses:\x00language: sv\x00'
final = True

    def decode(self, input, final=False):
        # decode input (taking the buffer into account)
        data = self.buffer + input
>       (result, consumed) = self._buffer_decode(data, self.errors, final)
E       UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9f in position 16: invalid start byte

/usr/lib/python3.10/codecs.py:322: UnicodeDecodeError

This PR introduce a simple check that the extension of the file it tries to load is indeed a yaml-file (or at least, it checks that the file says it is a yaml-file).

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling by ensuring only .yaml files are processed when loading intent responses, preventing potential errors from non-YAML files.

zyberzero avatar Jul 28 '24 12:07 zyberzero

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks :+1:

Learn more about our pull request process.

home-assistant[bot] avatar Jul 28 '24 12:07 home-assistant[bot]

Walkthrough

Walkthrough

This update enhances the robustness of the language_responses function in the tests configuration by ensuring that only files with a .yaml extension are processed when loading intent responses. The previous implementation lacked a filter, which could lead to errors if non-YAML files were included. By adding a conditional check, the code now gracefully handles file types, improving stability and predictability in response loading.

Changes

Files Change Summary
tests/conftest.py Added a conditional check to filter for .yaml files in the language_responses function, preventing errors from non-YAML files.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant LanguageResponses
    participant FileSystem

    User->>LanguageResponses: Request language responses
    LanguageResponses->>FileSystem: Retrieve files in directory
    FileSystem-->>LanguageResponses: Return list of files
    LanguageResponses->>LanguageResponses: Filter files for .yaml extension
    LanguageResponses->>FileSystem: Load .yaml files
    FileSystem-->>LanguageResponses: Return loaded responses
    LanguageResponses-->>User: Return processed responses

Recent review details

Configuration used: CodeRabbit UI Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5915fba355337958dc421662da4bd5cf9e30f1b2 and 35803089af6652c54de28940e341a2de853d847b.

Files selected for processing (1)
  • tests/conftest.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/conftest.py

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

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 an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Jul 28 '24 12:07 coderabbitai[bot]

What editor do you use? I don't want to start adding workarounds for every editor under the sun. Aren't swap files short lived, and are the odds of this happening tiny?

balloob avatar Nov 21 '24 03:11 balloob

What editor do you use? I don't want to start adding workarounds for every editor under the sun. Aren't swap files short lived, and are the odds of this happening tiny?

In this case, it's vim. However, I don't see it as a workaround for one specific editor - the proposed change filters out only yaml-files to be parsed rather then adding exceptions specific for an editor.

With the current state, adding for example an .md-file in the directory (for example, a file that describes the tests) would make the tests fail.

zyberzero avatar Nov 25 '24 12:11 zyberzero

In main now :)

synesthesiam avatar Jul 21 '25 15:07 synesthesiam