Skip to content

Commit badc327

Browse files
authored
Merge pull request #3 from omaralalwi/add-more-tests
add more tests
2 parents 0138b5a + f2a7b07 commit badc327

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

example-scripts/advance_example.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
# this shell script just for tests
3+
4+
# Check if at least one argument is provided
5+
if [ $# -eq 0 ]; then
6+
echo '{"error": "At least one numeric argument is required."}'
7+
exit 1
8+
fi
9+
10+
# Initialize sum
11+
sum=0
12+
13+
# Iterate over arguments and check if they are numbers
14+
for arg in "$@"; do
15+
if ! [[ "$arg" =~ ^-?[0-9]+([.][0-9]+)?$ ]]; then
16+
echo '{"error": "All arguments must be numeric."}'
17+
exit 1
18+
fi
19+
sum=$(echo "$sum + $arg" | bc)
20+
done
21+
22+
# Output the sum in JSON format
23+
echo "$sum"

src/PhpPy.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ public function loadScript(string $path): self
7474
}
7575

7676
if (!str_starts_with($realPath, $allowedDirReal . DIRECTORY_SEPARATOR)) {
77-
throw new InvalidArgumentException("Script path is not within the allowed directory.");
77+
throw new InvalidArgumentException("Script path {$realPath} is not within the allowed directory.");
7878
}
7979

8080
if (!is_file($realPath)) {
81-
throw new InvalidArgumentException("Script path is not a file.");
81+
throw new InvalidArgumentException("Script path {$realPath} is not a file.");
8282
}
8383

8484
if (!is_readable($realPath)) {
85-
throw new InvalidArgumentException("Script file is not readable.");
85+
throw new InvalidArgumentException("Script file {$realPath} is not readable.");
8686
}
8787

8888
if (pathinfo($realPath, PATHINFO_EXTENSION) !== 'py') {

tests/PhpPyTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@
2020
expect(json_decode($result))->toBe(60.0);
2121
});
2222

23+
test('PhpPy does not pass shell scripts', function () {
24+
25+
$configManager = new ConfigManager([
26+
'scripts_directory' => __DIR__ . '/../example-scripts',
27+
'python_executable' => '/usr/bin/python3',
28+
'max_timeout' => 30,
29+
]);
30+
31+
$this->expectException(\InvalidArgumentException::class);
32+
$this->expectExceptionMessage('Script path does not exist.');
33+
34+
$result = PhpPy::build()
35+
->setConfig($configManager)
36+
->loadScript('sum_calculator.sh')
37+
->withArguments([10, 20, 30])
38+
->run();
39+
40+
expect(json_decode($result))->toBe(60.0);
41+
});
42+
43+
2344
test('PhpPy throws exception when try to run script outside allowed path', function () {
2445

2546
$configManager = new ConfigManager([

0 commit comments

Comments
 (0)