Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Alexandru Dura
pintos-public
Commits
cf70aa64
Commit
cf70aa64
authored
Mar 25, 2021
by
Alexandru Dura
Browse files
Add a test target for the shell
parent
30ec7d78
Changes
2
Hide whitespace changes
Inline
Side-by-side
shell/Makefile
View file @
cf70aa64
...
...
@@ -10,7 +10,9 @@ OBJS = sh.o list.o
main
:
$(OBJS)
$(CC)
$(LDFLAGS)
$(OBJS)
-o
$(OUT)
./sh
clean
:
rm
-f
*
.o sh core out
test
:
main
python3 shell-test.py
$(OUT)
shell/shell-test.py
0 → 100644
View file @
cf70aa64
import
os
import
sys
import
subprocess
import
time
import
tempfile
def
run_command_through_shell
(
cmd
,
shellp
,
no_read
=
False
)
:
shellp
.
stdin
.
write
(
cmd
)
shellp
.
stdin
.
flush
()
if
not
no_read
:
result
=
os
.
read
(
shellp
.
stdout
.
fileno
(),
1000
)
return
result
.
decode
(
'utf-8'
)
return
None
def
create_shell_process
(
shell_exec
)
:
p
=
subprocess
.
Popen
(
shell_exec
,
universal_newlines
=
True
,
stdout
=
subprocess
.
PIPE
,
stdin
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
STDOUT
)
return
p
def
expect
(
shellp
,
command
,
expected
,
min_time
=
None
,
max_time
=
None
)
:
if
expected
and
type
(
expected
)
!=
set
:
expected
=
{
expected
}
print
(
"="
*
80
)
print
(
f
"CMD:
{
command
}
"
)
t0
=
time
.
time
()
r
=
run_command_through_shell
(
command
,
shellp
,
expected
is
None
)
t1
=
time
.
time
()
print
(
f
"RESULT:
{
r
}
"
)
if
expected
and
r
not
in
expected
:
print
(
f
"EXPECTED:
{
' OR '
.
join
(
expected
)
}
"
)
print
(
"FAIL"
)
return
False
elif
min_time
and
(
t1
-
t0
)
<
min_time
:
print
(
f
"COMMAND FINISHED TOO FAST"
)
return
False
elif
max_time
and
(
t1
-
t0
)
>
max_time
:
print
(
f
"TIME LIMIT EXCEEDED"
)
return
False
else
:
print
(
"PASS"
)
return
True
if
__name__
==
"__main__"
:
if
len
(
sys
.
argv
)
!=
2
:
print
(
f
"Run as f
{
sys
.
argv
[
0
]
}
SHELL_EXEC"
)
exit
(
1
)
shell_exec
=
os
.
path
.
abspath
(
sys
.
argv
[
1
])
with
tempfile
.
TemporaryDirectory
()
as
tmpdir
:
os
.
chdir
(
tmpdir
)
# Create a process running the shell
p
=
create_shell_process
(
shell_exec
)
# Push the following commands through the shell and
# check their output.
tests
=
[(
"/bin/pwd
\n
"
,
tmpdir
+
"
\n
"
),
(
"pwd
\n
"
,
tmpdir
+
"
\n
"
),
(
"touch file1.txt
\n
"
,
None
),
(
"ls
\n
"
,
"file1.txt
\n
"
),
(
"sleep 5; echo 'hello'
\n
"
,
"hello
\n
"
,
4
,
6
),
(
"sleep 5 & echo 'hello'
\n
"
,
"hello
\n
"
,
0
,
1
),
(
"echo line1 > file1.txt
\n
"
,
None
),
(
"cat < file1.txt
\n
"
,
"line1
\n
"
),
(
"cat file1.txt | wc -l
\n
"
,
"1
\n
"
),
(
"cat file1.txt | cat | cat | cat | cat | wc -l
\n
"
,
"1
\n
"
),
(
"echo 'x' | cat | wc -l & echo 2
\n
"
,
{
"1
\n
"
,
"2
\n
"
})
]
all_pass
=
True
for
test
in
tests
:
all_pass
=
expect
(
p
,
*
test
)
and
all_pass
# Send EOF to the shell
p
.
stdin
.
close
()
# The shell should exit
p
.
wait
()
print
(
"="
*
80
)
if
all_pass
:
print
(
"PASS: All tests have passed."
)
else
:
print
(
"FAIL: Some tests have failed."
)
print
(
"="
*
80
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment