Repository navigation
Expand file tree
/
Copy pathtest_check_solution_projects.sh
More file actions
executable file
·253 lines (226 loc) · 7.05 KB
/
Copy pathtest_check_solution_projects.sh
File metadata and controls
executable file
·253 lines (226 loc) · 7.05 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/bash
# The MIT License (MIT)
# Copyright (c) 2026 Irakli Gabisonia
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
set -uo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
GUARD_SCRIPT="${1:-$SCRIPT_DIR/check_solution_projects.sh}"
TEST_ROOT=$(mktemp -d "${TMPDIR:-/tmp}/brighter-solution-tests.XXXXXX") || exit 1
trap 'rm -rf -- "$TEST_ROOT"' EXIT
PASS=0
FAIL=0
CASES=0
arrange() {
CASE_NAME="$1"
CASE_DIR=$(mktemp -d "$TEST_ROOT/case.XXXXXX") || exit 1
git -C "$CASE_DIR" init --quiet || exit 1
CASES=$((CASES + 1))
}
write_project() {
mkdir -p "$CASE_DIR/$(dirname "$1")" || exit 1
printf '<Project Sdk="Microsoft.NET.Sdk" />\n' > "$CASE_DIR/$1"
if [[ "${2:-tracked}" == tracked ]]; then
git -C "$CASE_DIR" add -- "$1" || exit 1
fi
}
write_solution() {
{
printf '<Solution>\n'
for project in "$@"; do
printf ' <Project Path="%s" />\n' "$project"
done
printf '</Solution>\n'
} > "$CASE_DIR/Brighter.slnx"
}
write_exclusions() {
mkdir -p "$CASE_DIR/.github" || exit 1
printf '%s\n' "$@" > "$CASE_DIR/.github/solution-project-exclusions.txt"
}
run_guard() {
(cd "$CASE_DIR" && bash "$GUARD_SCRIPT") > "$CASE_DIR/output" 2>&1
STATUS=$?
OUTPUT=$(cat "$CASE_DIR/output")
}
assert_equal() {
if [[ "$1" == "$2" ]]; then
PASS=$((PASS + 1))
else
echo "FAIL: $CASE_NAME: expected '$1', got '$2'"
echo "$OUTPUT"
FAIL=$((FAIL + 1))
fi
}
assert_contains() {
if [[ "$OUTPUT" == *"$1"* ]]; then
PASS=$((PASS + 1))
else
echo "FAIL: $CASE_NAME: missing '$1'"
echo "$OUTPUT"
FAIL=$((FAIL + 1))
fi
}
# Arrange / Act / Assert: each case runs the real script in a temporary Git repository.
arrange 'all tracked projects are registered'
projects=()
for folder in benchmarks samples src tests tools; do
projects+=("$folder/Example.csproj")
write_project "$folder/Example.csproj"
done
write_solution "${projects[@]}"
run_guard
assert_equal 0 "$STATUS"
for folder in benchmarks samples src tests tools bugfixes; do
arrange "missing tracked project under $folder"
write_project "$folder/Missing.csproj"
write_solution
run_guard
assert_equal 1 "$STATUS"
assert_contains "$folder/Missing.csproj"
done
arrange 'untracked projects and build artifacts are ignored'
write_project 'src/Example.csproj'
write_solution 'src/Example.csproj'
for project in scratch/Untracked.csproj src/bin/Copied.csproj src/obj/Copied.csproj; do
write_project "$project" untracked
done
run_guard
assert_equal 0 "$STATUS"
arrange 'solution path casing differs from Git'
write_project 'src/Example.AWS/Example.csproj'
write_solution 'src/Example.Aws/Example.csproj'
run_guard
assert_equal 1 "$STATUS"
assert_contains 'src/Example.AWS/Example.csproj'
arrange 'project paths contain spaces'
write_project 'samples/Example App/Example App.csproj'
write_solution 'samples/Example App/Example App.csproj'
run_guard
assert_equal 0 "$STATUS"
arrange 'commented-out project is not registered'
write_project 'src/Example.csproj'
cat > "$CASE_DIR/Brighter.slnx" <<'XML'
<Solution>
<!-- <Project Path="src/Example.csproj" /> -->
</Solution>
XML
run_guard
assert_equal 1 "$STATUS"
assert_contains 'src/Example.csproj'
arrange 'solution item is not a project registration'
write_project 'src/Example.csproj'
cat > "$CASE_DIR/Brighter.slnx" <<'XML'
<Solution>
<Folder Name="/Files/">
<File Path="src/Example.csproj" />
</Folder>
</Solution>
XML
run_guard
assert_equal 1 "$STATUS"
assert_contains 'src/Example.csproj'
arrange 'single-quoted project path is registered'
write_project 'src/Example.csproj'
cat > "$CASE_DIR/Brighter.slnx" <<'XML'
<Solution>
<Project Path='src/Example.csproj' />
</Solution>
XML
run_guard
assert_equal 0 "$STATUS"
arrange 'multiline project attributes with whitespace are registered'
write_project 'src/Example.csproj'
cat > "$CASE_DIR/Brighter.slnx" <<'XML'
<Solution>
<Folder Name="/Source/">
<Project
Path = "src/Example.csproj"
/>
</Folder>
</Solution>
XML
run_guard
assert_equal 0 "$STATUS"
arrange 'XML entities in project paths are decoded'
write_project 'samples/A&B/Example.csproj'
cat > "$CASE_DIR/Brighter.slnx" <<'XML'
<Solution>
<Project Path="samples/A&B/Example.csproj" />
</Solution>
XML
run_guard
assert_equal 0 "$STATUS"
arrange 'malformed XML is rejected despite a matching project path'
write_project 'src/Example.csproj'
cat > "$CASE_DIR/Brighter.slnx" <<'XML'
<Solution>
<Project Path="src/Example.csproj">
</Solution>
XML
run_guard
assert_equal 1 "$STATUS"
assert_contains 'Brighter.slnx'
arrange 'solution element must be the document root'
write_project 'src/Example.csproj'
cat > "$CASE_DIR/Brighter.slnx" <<'XML'
<Other>
<Solution>
<Project Path="src/Example.csproj" />
</Solution>
</Other>
XML
run_guard
assert_equal 1 "$STATUS"
assert_contains 'Brighter.slnx'
arrange 'standalone project is explicitly excluded'
write_project 'bugfixes/diagnostic/Diagnostic.csproj'
write_solution
write_exclusions '# Standalone diagnostic, run manually.' '' 'bugfixes/diagnostic/Diagnostic.csproj'
run_guard
assert_equal 0 "$STATUS"
arrange 'exclusion does not match a tracked project'
write_project 'src/Example.csproj'
write_solution 'src/Example.csproj'
write_exclusions 'bugfixes/Removed.csproj'
run_guard
assert_equal 1 "$STATUS"
assert_contains 'bugfixes/Removed.csproj'
arrange 'excluded project is now registered'
write_project 'src/Example.csproj'
write_solution 'src/Example.csproj'
write_exclusions 'src/Example.csproj'
run_guard
assert_equal 1 "$STATUS"
assert_contains 'src/Example.csproj'
arrange 'solution references an untracked project'
write_solution 'samples/Removed.csproj'
run_guard
assert_equal 1 "$STATUS"
assert_contains 'samples/Removed.csproj'
arrange 'solution is missing'
run_guard
assert_equal 1 "$STATUS"
assert_contains 'Brighter.slnx'
arrange 'solution is incomplete'
printf '<Solution>\n' > "$CASE_DIR/Brighter.slnx"
run_guard
assert_equal 1 "$STATUS"
assert_contains 'Brighter.slnx'
echo "$CASES cases: $PASS assertions passed, $FAIL failed."
[[ "$FAIL" -eq 0 ]]