forked from 4U6U57/wsl-open
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsl-open.bats
200 lines (182 loc) · 3.81 KB
/
wsl-open.bats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env bats
load "node_modules/bats-support/load"
load "node_modules/bats-assert/load"
TestDir="$BATS_TEST_DIRNAME/test_folder"
TestDisks="$BATS_TEST_DIRNAME/test_mnt"
Source() {
$BATS_TEST_DIRNAME/wsl-open.sh $@
}
TestSource() {
if refute_wsl; then
export EnableWslCheck=false
fi
export WslTempDir=$WslTempDir
export WslDisks=$TestDisks
export WslOpenExe="echo Open:"
Source $*
}
Exe=$(basename $Source .sh)
ConfigFile=~/.$Exe
setup() {
create_test_env
cd $TestDir
if grep -q "env:" <(echo $BATS_TEST_NAME) &>/dev/null; then
# Env tests, do nothing
true
else
create_valid_windisk c
assert_valid_windisk
fi
}
@test "env: test environment" {
assert_equal $(pwd) $TestDir
assert [ -d $TestDisks ]
}
@test "env: not on WSL error" {
if assert_wsl; then
# We are on a real WSL
skip "Cannot test non-WSL behavior on WSL machine"
else
# Test functionality if ran on non WSL machine
run Source
assert_failure
assert_error
assert_output --partial "Could not detect WSL"
fi
}
@test "env: emulate WSL" {
if assert_wsl; then
skip "Cannot emulate WSL on WSL machine"
else
run TestSource
refute_error
refute_warning
assert_success
fi
}
@test "basic: no input" {
run TestSource
assert_success
refute_error
refute_warning
assert_output ""
}
@test "basic: help" {
run TestSource -h
assert_success
refute_error
refute_warning
assert_output --partial "wsl-open"
}
@test "basic: missing file" {
run TestSource noexist
assert_failure
assert_error
assert_output --partial "does not exist"
}
@test "basic: file on Windows" {
File="$UserDir/test.txt"
touch $File
run TestSource $File
assert_success
refute_error
refute_warning
assert_openfile "$UserFolder\\$(basename $File)"
}
@test "basic: directory on Windows" {
Dir="$UserDir/test"
mkdir $Dir
run TestSource $Dir
assert_success
refute_error
refute_warning
assert_openfile "$UserFolder\\$(basename $Dir)"
}
@test "basic: file on Linux" {
File="$TestDir/test.txt"
touch $File
run TestSource $File
assert_success
assert_warning
refute_error
assert_openfile "$ExecTempFolder\\$(basename $File)"
assert [ -d $ExecTempDir ]
assert [ -e $ExecTempDir/$(basename $File) ]
}
@test "basic: directory on Linux failure" {
Dir="$TestDir/test"
mkdir $Dir
run TestSource $Dir
assert_failure
assert_error
assert_output --partial "Directory not in Windows partition"
refute_warning
}
teardown() {
cd ..
rm -rf $TestDir $TestDisks
}
## Helper functions
# Environment checkers
assert_wsl() {
[[ $(uname -r) == *Microsoft ]]
}
refute_wsl() {
! assert_wsl
}
# Output checkers
assert_error() {
assert_output --partial "ERROR: "
}
assert_warning() {
assert_output --partial "WARNING: "
}
refute_error() {
refute_output --partial "ERROR: "
}
refute_warning() {
refute_output --partial "WARNING: "
}
assert_openfile() {
assert_output --partial "Open: \"$*\""
}
# Utilities
safe_mkdir() {
Dir="$*"
if [[ -e $Dir ]]; then
rm -rf $Dir
fi
refute [ -e $Dir ]
mkdir $Dir
assert [ -d $Dir ]
}
create_test_env() {
# Create test folder and test disk
for TempDir in "$TestDir" "$TestDisks"; do
safe_mkdir $TempDir
done
if [[ -z $USER ]]; then
export USER="wsl-tester"
fi
}
create_valid_windisk() {
Disk="$TestDisks/$*"
export WinDisk=$Disk
export UserDir=$Disk/Users/$USER
export WslTempDir=$UserDir/AppData/Temp
export ExecTempDir=$WslTempDir/wsl-open
export UserFolder=$(tr 'a-z' 'A-Z' <<< $*):\\\\Users\\$USER
export TempFolder=$UserFolder\\AppData\\Temp
export ExecTempFolder=$TempFolder\\wsl-open
for Dir in $Disk $Disk/Windows $Disk/Windows/System32 $Disk/Users \
$Disk/Users/$USER $UserDir/AppData $WslTempDir; do
safe_mkdir $Dir
done
}
assert_valid_windisk() {
Disk="$WinDisk"
assert [ -d $UserDir ]
assert_equal $UserDir $Disk/Users/$USER
assert [ -d $WslTempDir ]
assert_equal $WslTempDir $UserDir/AppData/Temp
}