Skip to content

Commit 488853f

Browse files
committed
added tests, a mock for osascript, and fixed a bug
mocks are still actually really bad in general... looks like it's easy for parameters to get interpreted by echo or print instead of getting captured and written.
1 parent 38a4513 commit 488853f

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

archive_fusionvm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ i=0
2323
running=(${(@f)"$(vmrun list | grep "$SRCPATH")"})
2424

2525
# Save this initial list of .vmx files
26-
vmxlist=$running
26+
vmxlist=($running)
2727

2828
while true; do
2929
echo "Current running VM list is $running"
@@ -49,7 +49,9 @@ while true; do
4949
done
5050

5151
# Test that every VMX in the list has no lockfile; otherwise quit Fusion
52+
echo "Checking for lockfiles for VM list $vmxlist"
5253
for vmx in $vmxlist; do
54+
echo "Testing lockfile $vmx.lck"
5355
if [ -f "$vmx.lck" ]; then
5456
osascript -e 'tell application "VMware Fusion" to quit'
5557
break

tests/_support/mocks/hdiutil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env zsh
22

3-
print -l $@ > $TEMP/hdiutil.calls
3+
print -l $@ >> $TEMP/hdiutil.calls
44

55
exit 0

tests/_support/mocks/osascript

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env zsh
2+
3+
echo "$@" >> $TEMP/osascript.calls
4+
5+
exit 0

tests/archive_fusionvm.zunit

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22

33
# $TEMP is set up in bootstrap
44
@setup {
5-
[ -d $TEMP ] || mkdir $TEMP
5+
rm -r $TEMP && mkdir $TEMP
66
[ -f $TEMP/decoy_file ] || touch $TEMP/decoy_file
77
[ -d $TEMP/decoy_dir ] || mkdir $TEMP/decoy_dir
88
}
99

10-
@teardown {
11-
rm -r $TEMP
12-
}
13-
1410
@test 'Exit with error when no arguments are supplied' {
1511
run archive_fusionvm
1612

@@ -114,6 +110,57 @@
114110
fi
115111
}
116112

113+
@test 'osascript executed when lockfile exists' {
114+
vmx="$TEMP/decoy_dir/example vm.vmx"
115+
116+
cat > $TEMP/vmrun_list_1 <<-EOF
117+
$vmx
118+
EOF
119+
120+
touch "$vmx.lck"
121+
122+
run archive_fusionvm $TEMP/decoy_dir $TEMP/out.dmg
123+
124+
if [ -f $TEMP/osascript.calls ]; then
125+
pass
126+
else
127+
fail 'osascript was not called'
128+
fi
129+
}
130+
131+
@test 'osascript is not executed if no lockfiles exist' {
132+
cat > $TEMP/vmrun_list_1 <<-EOF
133+
$TEMP/decoy_dir/example vm.vmx
134+
EOF
135+
136+
run archive_fusionvm $TEMP/decoy_dir $TEMP/out.dmg
137+
138+
if [ -f $TEMP/osascript.calls ]; then
139+
fail 'osascript was called'
140+
else
141+
pass
142+
fi
143+
}
144+
145+
@test 'osascript is executed only once if multiple lockfiles exist' {
146+
vmone="$TEMP/decoy_dir/examplevm one.vmx"
147+
vmtwo="$TEMP/decoy_dir/examplevmtwo.vmx"
148+
149+
cat > $TEMP/vmrun_list_1 <<-EOF
150+
$vmone
151+
$vmtwo
152+
EOF
153+
154+
touch "$vmone.lck"
155+
touch "$vmtwo.lck"
156+
157+
run archive_fusionvm $TEMP/decoy_dir $TEMP/out.dmg
158+
159+
osascriptcalls=(${(@f)"$(<$TEMP/osascript.calls)"})
160+
161+
assert ${#osascriptcalls} equals 1
162+
}
163+
117164
@test 'hdiutil create is executed' {
118165
run archive_fusionvm $TEMP/decoy_dir $TEMP/out.dmg
119166

0 commit comments

Comments
 (0)