Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs503zchen
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ziheng Chen
cs503zchen
Commits
5b6246b2
Commit
5b6246b2
authored
2 months ago
by
Ziheng Chen
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
14afdd0a
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
w7/student_tests.sh
+111
-0
111 additions, 0 deletions
w7/student_tests.sh
with
111 additions
and
0 deletions
w7/student_tests.sh
0 → 100644
+
111
−
0
View file @
5b6246b2
#!/usr/bin/env bats
# File: student_tests.sh
#
# Create your unit tests suit in this file
@test
"Example: check ls runs without errors"
{
run ./dsh
<<
EOF
ls
EOF
# Assertions
[
"
$status
"
-eq
0
]
}
# Test: Basic command execution
@test
"Basic command execution: echo"
{
run ./dsh
<<
EOF
echo Hello World
EOF
output
=
$(
echo
"
$output
"
|
tr
-d
'\r'
)
echo
"Captured output:
$output
"
# Debugging
[
"
$status
"
-eq
0
]
[[
"
$output
"
==
*
"Hello World"
*
]]
}
# Test: Input Redirection (`<`)
@test
"Input Redirection: wc -w < test_input.txt"
{
echo
"Hello World"
>
test_input.txt
run ./dsh
<<
EOF
wc -w < test_input.txt
EOF
output
=
$(
echo
"
$output
"
|
tr
-d
'\r'
)
echo
"Captured output:
$output
"
# Debugging
[
"
$status
"
-eq
0
]
[[
"
$output
"
==
*
"2"
*
]]
rm
-f
test_input.txt
}
# Test: Built-in Commands (`cd`)
@test
"Built-in command: cd"
{
run ./dsh
<<
EOF
cd /tmp
pwd
EOF
[
"
$status
"
-eq
0
]
[[
"
$output
"
=
~
"/tmp"
]]
}
# Test: Built-in Commands (`exit`)
@test
"Built-in command: exit"
{
run ./dsh
<<
EOF
exit
EOF
[
"
$status
"
-eq
0
]
}
@test
"Output Redirection: echo Hello > test_output.txt"
{
rm
-f
test_output.txt
# Ensure file does not exist before test
run ./dsh
<<
EOF
echo Hello > test_output.txt
EOF
sleep
1
# Give time for file creation
echo
"Debug: Checking test_output.txt after execution..."
ls
-l
test_output.txt
[
"
$status
"
-eq
0
]
if
[
!
-f
test_output.txt
]
;
then
echo
"Test Failed: Output file test_output.txt does not exist!"
exit
1
fi
trimmed_output
=
$(
cat
test_output.txt |
tr
-d
'\r'
)
echo
"Captured output:
$trimmed_output
"
[[
"
$trimmed_output
"
==
*
"Hello"
*
]]
rm
-f
test_output.txt
}
# Test: Append Redirection (`>>`)
@test
"Append Redirection: echo Hello > test_append.txt && echo World >> test_append.txt"
{
rm
-f
test_append.txt
# Ensure file does not exist
run ./dsh
<<
EOF
echo Hello > test_append.txt
echo World >> test_append.txt
EOF
sleep
1
# Ensure file is written
[
"
$status
"
-eq
0
]
[
-f
test_append.txt
]
# Ensure file exists
trimmed_output
=
$(
cat
test_append.txt |
tr
-d
'\r'
)
echo
"Captured output:
$trimmed_output
"
# Debugging
[[
"
$trimmed_output
"
==
*
"Hello"
*
&&
"
$trimmed_output
"
==
*
"World"
*
]]
rm
-f
test_append.txt
}
# Extra Credit: Combining Pipes and Redirection
@test
"Extra Credit: ls | grep .c > output.txt"
{
rm
-f
output.txt
# Ensure file does not exist
run ./dsh
<<
EOF
ls | grep .c > output.txt
EOF
sleep
1
# Ensure file is written
[
"
$status
"
-eq
0
]
[
-f
output.txt
]
# Ensure file exists
trimmed_output
=
$(
cat
output.txt |
tr
-d
'\r'
)
echo
"Captured output:
$trimmed_output
"
# Debugging
[[
"
$trimmed_output
"
==
*
"dshlib.c"
*
]]
rm
-f
output.txt
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment