Skip to content

Commit 9760a51

Browse files
Merge pull request #462 from codeflash-ai/enhancement/better-shell-regex
[Enhancement] Better shell regex for matching api key export
2 parents fd9781d + ad542ca commit 9760a51

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

codeflash/code_utils/shell_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
SHELL_RC_EXPORT_PATTERN = re.compile(r"^set CODEFLASH_API_KEY=(cf-.*)$", re.MULTILINE)
1616
SHELL_RC_EXPORT_PREFIX = "set CODEFLASH_API_KEY="
1717
else:
18-
SHELL_RC_EXPORT_PATTERN = re.compile(r'^export CODEFLASH_API_KEY="?(cf-[^\s"]+)"?$', re.MULTILINE)
18+
SHELL_RC_EXPORT_PATTERN = re.compile(r'^(?!#)export CODEFLASH_API_KEY=[\'"]?(cf-[^\s"]+)[\'"]$', re.MULTILINE)
1919
SHELL_RC_EXPORT_PREFIX = "export CODEFLASH_API_KEY="
2020

2121

tests/test_shell_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ def test_valid_api_key(self):
5454
) as mock_file:
5555
self.assertEqual(read_api_key_from_shell_config(), self.api_key)
5656
mock_file.assert_called_once_with(self.test_rc_path, encoding="utf8")
57+
with patch(
58+
"builtins.open", mock_open(read_data=f'export CODEFLASH_API_KEY=\'{self.api_key}\'\n')
59+
) as mock_file:
60+
self.assertEqual(read_api_key_from_shell_config(), self.api_key)
61+
mock_file.assert_called_once_with(self.test_rc_path, encoding="utf8")
62+
with patch(
63+
"builtins.open", mock_open(read_data=f'#export CODEFLASH_API_KEY=\'{self.api_key}\'\n')
64+
) as mock_file:
65+
self.assertEqual(read_api_key_from_shell_config(), None)
66+
mock_file.assert_called_once_with(self.test_rc_path, encoding="utf8")
5767

5868
@patch("codeflash.code_utils.shell_utils.get_shell_rc_path")
5969
def test_no_api_key(self, mock_get_shell_rc_path):

0 commit comments

Comments
 (0)