Skip to content

Commit b8ff88c

Browse files
author
Yoshihiro Takahara
committed
test: improve test coverage for shell executor
1 parent 17ba1b7 commit b8ff88c

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

tests/test_shell_executor.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,94 @@ async def test_directory_permissions(executor, temp_test_dir, monkeypatch):
407407
finally:
408408
# Restore permissions for cleanup
409409
os.chmod(no_exec_dir, 0o700)
410+
411+
412+
def test_validate_redirection_syntax(executor):
413+
"""Test validation of redirection syntax with various input combinations"""
414+
# Valid cases
415+
executor._validate_redirection_syntax(["echo", "hello", ">", "file.txt"])
416+
executor._validate_redirection_syntax(["cat", "<", "input.txt", ">", "output.txt"])
417+
418+
# Test consecutive operators
419+
with pytest.raises(ValueError) as exc:
420+
executor._validate_redirection_syntax(["echo", "text", ">", ">", "file.txt"])
421+
assert str(exc.value) == "Invalid redirection syntax: consecutive operators"
422+
423+
with pytest.raises(ValueError) as exc:
424+
executor._validate_redirection_syntax(["cat", "<", "<", "input.txt"])
425+
assert str(exc.value) == "Invalid redirection syntax: consecutive operators"
426+
427+
428+
def test_create_shell_command(executor):
429+
"""Test shell command creation with various input combinations"""
430+
# Test basic command
431+
assert executor._create_shell_command(["echo", "hello"]) == "echo hello"
432+
433+
# Test command with space-only argument
434+
assert executor._create_shell_command(["echo", " "]) == "echo ' '"
435+
436+
# Test command with wildcards
437+
assert executor._create_shell_command(["ls", "*.txt"]) == "ls '*.txt'"
438+
439+
# Test command with special characters
440+
assert executor._create_shell_command(["echo", "hello;", "world"]) == "echo 'hello;' world"
441+
442+
# Test empty command
443+
assert executor._create_shell_command([]) == ""
444+
445+
446+
def test_preprocess_command(executor):
447+
"""Test command preprocessing for pipeline handling"""
448+
# Test basic command
449+
assert executor._preprocess_command(["ls"]) == ["ls"]
450+
451+
# Test command with separate pipe
452+
assert executor._preprocess_command(["ls", "|", "grep", "test"]) == [
453+
"ls",
454+
"|",
455+
"grep",
456+
"test",
457+
]
458+
459+
# Test command with attached pipe
460+
assert executor._preprocess_command(["ls|", "grep", "test"]) == [
461+
"ls",
462+
"|",
463+
"grep",
464+
"test",
465+
]
466+
467+
# Test command with special operators
468+
assert executor._preprocess_command(["echo", "hello", "&&", "ls"]) == [
469+
"echo",
470+
"hello",
471+
"&&",
472+
"ls",
473+
]
474+
475+
# Test empty command
476+
assert executor._preprocess_command([]) == []
477+
478+
479+
def test_validate_pipeline(executor, monkeypatch):
480+
"""Test pipeline validation"""
481+
monkeypatch.setenv("ALLOW_COMMANDS", "echo,grep,cat")
482+
483+
# Test valid pipeline
484+
executor._validate_pipeline(["echo", "hello", "|", "grep", "h"])
485+
486+
# Test empty command before pipe
487+
with pytest.raises(ValueError) as exc:
488+
executor._validate_pipeline(["|", "grep", "test"])
489+
assert str(exc.value) == "Empty command before pipe operator"
490+
491+
# Test disallowed commands in pipeline
492+
with pytest.raises(ValueError) as exc:
493+
executor._validate_pipeline(["rm", "|", "grep", "test"])
494+
assert "Command not allowed: rm" in str(exc.value)
495+
496+
# Test shell operators in pipeline
497+
with pytest.raises(ValueError) as exc:
498+
executor._validate_pipeline(["echo", "hello", "|", "grep", "h", "&&", "ls"])
499+
assert "Unexpected shell operator in pipeline: &&" in str(exc.value)
500+
assert executor._preprocess_command([]) == []

0 commit comments

Comments
 (0)