Skip to content

Conversation

@mohammedahmed18
Copy link
Contributor

@mohammedahmed18 mohammedahmed18 commented Sep 4, 2025

PR Type

Bug fix, Tests


Description

  • Fix regex to match unquoted API key in export

  • Allow optional single/double quotes around API key

  • Add unit test for unquoted export line


File Walkthrough

Relevant files
Bug fix
shell_utils.py
Refine shell export regex for API key                                       

codeflash/code_utils/shell_utils.py

  • Updated regex pattern for non-Windows shells
  • Made quotes around API key optional
  • Captures API key with pattern cf-[^\s"']+
+3/-1     
Tests
test_shell_utils.py
Add test for unquoted API key export                                         

tests/test_shell_utils.py

  • Added test for unquoted export line
  • Validates reading API key without quotes
+6/-0     

@github-actions
Copy link

github-actions bot commented Sep 4, 2025

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Regex Scope

The current regex only matches lines that start exactly with export; it doesn't allow any leading whitespace, so valid export lines indented in rc files may not be detected.

SHELL_RC_EXPORT_PATTERN = re.compile(
    r'^(?!#)export CODEFLASH_API_KEY=(?:"|\')?(cf-[^\s"\']+)(?:"|\')?$', re.MULTILINE
)
Missing Test

There is no test covering the case of a double-quoted API key export entry; consider adding a unit test for export CODEFLASH_API_KEY="cf-...".

with patch(
    "builtins.open", mock_open(read_data=f'export CODEFLASH_API_KEY={self.api_key}\n')
) as mock_file:
    self.assertEqual(read_api_key_from_shell_config(), self.api_key)
    mock_file.assert_called_once_with(self.test_rc_path, encoding="utf8")

@github-actions
Copy link

github-actions bot commented Sep 4, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
Enforce matching quotes

Ensure that opening and closing quotes match to prevent mismatched‐quote cases by
using a backreference group. This will only accept properly quoted or unquoted keys.

codeflash/code_utils/shell_utils.py [18-20]

 SHELL_RC_EXPORT_PATTERN = re.compile(
-    r'^(?!#)export CODEFLASH_API_KEY=(?:"|\')?(cf-[^\s"\']+)(?:"|\')?$', re.MULTILINE
+    r'^(?!#)export CODEFLASH_API_KEY=(?:(?P<q>["\'])(cf-[^\s"\']+)(?P=q)|(cf-[^\s"\']+))$', 
+    re.MULTILINE
 )
Suggestion importance[1-10]: 6

__

Why: Adding a backreference ensures opening and closing quotes match, improving pattern correctness with minimal impact.

Low
Simplify optional quotes

Simplify the optional quote groups by using a character class for both quote types.
This makes the regex more concise and easier to read without changing its behavior.

codeflash/code_utils/shell_utils.py [18-20]

 SHELL_RC_EXPORT_PATTERN = re.compile(
-    r'^(?!#)export CODEFLASH_API_KEY=(?:"|\')?(cf-[^\s"\']+)(?:"|\')?$', re.MULTILINE
+    r'^(?!#)export CODEFLASH_API_KEY=["\']?(cf-[^\s"\']+)["\']?$', re.MULTILINE
 )
Suggestion importance[1-10]: 5

__

Why: Simplifying the quote grouping improves regex readability without changing its behavior.

Low

@mohammedahmed18 mohammedahmed18 merged commit 02ae60b into main Sep 4, 2025
18 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants