-
Notifications
You must be signed in to change notification settings - Fork 7.1k
(fix) tests: temp_dir fixture fix in 4 tests #3160
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eb0eda7
split_bash_commands replaced; temp_dir fixture fix in some tests
tobitege 0b2a594
tweak test_runtime
tobitege 434e1b2
skip 2 tests in test_runtime that need fixing in extra PR
tobitege caa8736
reverting bash parsing changes and re-enabled tests
tobitege d2cc9b3
missed to revert a changed assert in test_runtime.py
tobitege File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,168 @@ | ||
import bashlex | ||
def split_bash_commands(commands): | ||
# States | ||
NORMAL = 0 | ||
IN_SINGLE_QUOTE = 1 | ||
IN_DOUBLE_QUOTE = 2 | ||
IN_HEREDOC = 3 | ||
|
||
from opendevin.core.logger import opendevin_logger as logger | ||
state = NORMAL | ||
heredoc_trigger = None | ||
result = [] | ||
current_command: list[str] = [] | ||
|
||
i = 0 | ||
while i < len(commands): | ||
char = commands[i] | ||
|
||
if state == NORMAL: | ||
if char == "'": | ||
state = IN_SINGLE_QUOTE | ||
elif char == '"': | ||
state = IN_DOUBLE_QUOTE | ||
elif char == '\\': | ||
# Check if this is escaping a newline | ||
if i + 1 < len(commands) and commands[i + 1] == '\n': | ||
current_command.append(char) # Append the backslash | ||
current_command.append(commands[i + 1]) # Append the newline | ||
i += 1 # Skip the newline | ||
|
||
# Find the next non-whitespace character | ||
j = i + 1 | ||
while j < len(commands) and commands[j].isspace(): | ||
j += 1 | ||
|
||
# Append the indentation | ||
indentation = '' | ||
k = i + 1 | ||
while k < j: | ||
indentation += commands[k] | ||
k += 1 | ||
|
||
current_command.append(indentation) | ||
|
||
# Find the next newline | ||
next_newline = j | ||
while ( | ||
next_newline < len(commands) and commands[next_newline] != '\n' | ||
): | ||
next_newline += 1 | ||
|
||
# Append the rest of the command | ||
current_command.append(commands[j:next_newline]) | ||
i = next_newline | ||
elif i + 1 < len(commands) and commands[i + 1] == '-': | ||
# If backslash is escaping a '-', skip the backslash | ||
i += 1 # Skip the backslash and append the '-' | ||
current_command.append(commands[i]) | ||
else: | ||
# If backslash is escaping another character, append the backslash and the escaped character | ||
current_command.append(commands[i : i + 2]) | ||
i += 1 | ||
elif char == '\n': | ||
if not heredoc_trigger: | ||
if current_command and current_command[-1] == '\\': | ||
# Remove the backslash and continue the command | ||
current_command.pop() | ||
# Preserve indentation after backslash-newline | ||
j = i + 1 | ||
while ( | ||
j < len(commands) | ||
and commands[j].isspace() | ||
and commands[j] != '\n' | ||
): | ||
if commands[j] == '\\': | ||
break | ||
current_command.append(commands[j]) | ||
j += 1 | ||
i = j - 1 # Adjust i to the last space character | ||
|
||
elif current_command and any( | ||
c in current_command for c in ['&&', '||', '|', '&'] | ||
): | ||
# If the current command contains a control operator, | ||
# continue to the next line | ||
current_command.append(char) | ||
|
||
elif current_command: | ||
# Check if the next line is a comment | ||
next_non_space = commands[i + 1 :].lstrip() | ||
if next_non_space.startswith('#'): | ||
current_command.append('\n') | ||
current_command.append(next_non_space.split('\n', 1)[0]) | ||
i += len(next_non_space.split('\n', 1)[0]) | ||
else: | ||
# Remove trailing whitespace | ||
while current_command and current_command[-1].isspace(): | ||
current_command.pop() | ||
result.append(''.join(current_command)) | ||
current_command = [] | ||
else: | ||
# Handle empty lines between commands | ||
j = i + 1 | ||
while ( | ||
j < len(commands) | ||
and commands[j].isspace() | ||
and commands[j] != '\n' | ||
): | ||
j += 1 | ||
if j < len(commands) and commands[j] == '\n': | ||
# Empty line, skip it | ||
i = j | ||
else: | ||
result.append(''.join(current_command).strip()) | ||
current_command = [] | ||
elif char == '<' and commands[i : i + 2] == '<<': | ||
# Detect heredoc | ||
state = IN_HEREDOC | ||
i += 2 # Skip '<<' | ||
while commands[i] == ' ': | ||
i += 1 | ||
start = i | ||
while commands[i] not in [' ', '\n']: | ||
i += 1 | ||
heredoc_trigger = commands[start:i] | ||
current_command.append(commands[start - 2 : i]) # Include '<<' | ||
continue # Skip incrementing i at the end of the loop | ||
current_command.append(char) | ||
|
||
elif state == IN_SINGLE_QUOTE: | ||
current_command.append(char) | ||
if char == "'" and commands[i - 1] != '\\': | ||
state = NORMAL | ||
|
||
elif state == IN_DOUBLE_QUOTE: | ||
current_command.append(char) | ||
if char == '"' and commands[i - 1] != '\\': | ||
state = NORMAL | ||
|
||
elif state == IN_HEREDOC: | ||
current_command.append(char) | ||
if ( | ||
char == '\n' | ||
and heredoc_trigger | ||
and commands[i + 1 : i + 1 + len(heredoc_trigger) + 1] | ||
== heredoc_trigger + '\n' | ||
): | ||
# Check if the next line starts with the heredoc trigger followed by a newline | ||
i += ( | ||
len(heredoc_trigger) + 1 | ||
) # Move past the heredoc trigger and newline | ||
current_command.append( | ||
heredoc_trigger + '\n' | ||
) # Include the heredoc trigger and newline | ||
result.append(''.join(current_command).strip()) | ||
current_command = [] | ||
heredoc_trigger = None | ||
state = NORMAL | ||
continue | ||
|
||
i += 1 | ||
|
||
# Add the last command if any | ||
if current_command: | ||
result.append(''.join(current_command)) | ||
|
||
# Remove any empty strings and strip leading/trailing whitespace | ||
result = [cmd.strip() for cmd in result if cmd.strip()] | ||
|
||
def split_bash_commands(commands): | ||
try: | ||
parsed = bashlex.parse(commands) | ||
except bashlex.errors.ParsingError as e: | ||
logger.error( | ||
f'Failed to parse bash commands\n[input]: {commands}\n[error]: {e}' | ||
) | ||
# If parsing fails, return the original commands | ||
return [commands] | ||
|
||
result: list[str] = [] | ||
last_end = 0 | ||
|
||
for node in parsed: | ||
start, end = node.pos | ||
|
||
# Include any text between the last command and this one | ||
if start > last_end: | ||
between = commands[last_end:start] | ||
logger.debug(f'BASH PARSING between: {between}') | ||
if result: | ||
result[-1] += between.rstrip() | ||
elif between.strip(): | ||
# THIS SHOULD NOT HAPPEN | ||
result.append(between.rstrip()) | ||
|
||
# Extract the command, preserving original formatting | ||
command = commands[start:end].rstrip() | ||
logger.debug(f'BASH PARSING command: {command}') | ||
result.append(command) | ||
|
||
last_end = end | ||
|
||
# Add any remaining text after the last command to the last command | ||
remaining = commands[last_end:].rstrip() | ||
logger.debug(f'BASH PARSING remaining: {remaining}') | ||
if last_end < len(commands) and result: | ||
result[-1] += remaining | ||
logger.debug(f'BASH PARSING result[-1] += remaining: {result[-1]}') | ||
elif last_end < len(commands): | ||
if remaining: | ||
result.append(remaining) | ||
logger.debug(f'BASH PARSING result.append(remaining): {result[-1]}') | ||
return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.