| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
set -euo pipefail |
| 4 |
|
| 5 |
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" |
| 6 |
fixture="$(mktemp -d)" |
| 7 |
cleanup() { |
| 8 |
rm -rf "$fixture" |
| 9 |
} |
| 10 |
trap cleanup EXIT |
| 11 |
|
| 12 |
cp "$repo_root/scripts/check-architecture-imports.sh" "$fixture/check-architecture-imports.sh" |
| 13 |
chmod +x "$fixture/check-architecture-imports.sh" |
| 14 |
|
| 15 |
cd "$fixture" |
| 16 |
cat > go.mod <<'MOD' |
| 17 |
module example.test/clnkr |
| 18 |
|
| 19 |
go 1.24 |
| 20 |
MOD |
| 21 |
|
| 22 |
mkdir -p cmd/internal/clnkrapp internal/providers/openai |
| 23 |
cat > internal/providers/openai/openai.go <<'GO' |
| 24 |
package openai |
| 25 |
|
| 26 |
type Model struct{} |
| 27 |
GO |
| 28 |
cat > cmd/internal/clnkrapp/clnkrapp.go <<'GO' |
| 29 |
package clnkrapp |
| 30 |
|
| 31 |
import _ "example.test/clnkr/internal/providers/openai" |
| 32 |
GO |
| 33 |
|
| 34 |
set +e |
| 35 |
output="$(./check-architecture-imports.sh 2>&1)" |
| 36 |
status=$? |
| 37 |
set -e |
| 38 |
|
| 39 |
if [[ $status -ne 1 ]]; then |
| 40 |
echo "expected invalid fixture to fail with status 1, got $status" >&2 |
| 41 |
echo "$output" >&2 |
| 42 |
exit 1 |
| 43 |
fi |
| 44 |
|
| 45 |
for expected in \ |
| 46 |
"error: architecture import boundary violation" \ |
| 47 |
"rule: ARCH010 frontend-provider-construction" \ |
| 48 |
"importer: example.test/clnkr/cmd/internal/clnkrapp" \ |
| 49 |
"target: example.test/clnkr/internal/providers/openai" \ |
| 50 |
"import_source: imports" \ |
| 51 |
"trusted_rule: frontend coordinator must use internal/providerfactory instead of concrete provider adapters." \ |
| 52 |
"source_fact: go list reported importer imports target." \ |
| 53 |
"guidance: move provider construction behind internal/providerfactory; do not import concrete provider adapters from frontend packages." |
| 54 |
do |
| 55 |
if [[ "$output" != *"$expected"* ]]; then |
| 56 |
echo "missing expected output: $expected" >&2 |
| 57 |
echo "$output" >&2 |
| 58 |
exit 1 |
| 59 |
fi |
| 60 |
done |
| 61 |
|
| 62 |
mkdir -p cmd/internal/providerconfig internal/providerfactory internal/providers/providerconfig |
| 63 |
cat > internal/providers/providerconfig/providerconfig.go <<'GO' |
| 64 |
package providerconfig |
| 65 |
GO |
| 66 |
cat > cmd/internal/providerconfig/providerconfig.go <<'GO' |
| 67 |
package providerconfig |
| 68 |
|
| 69 |
import _ "example.test/clnkr/internal/providers/providerconfig" |
| 70 |
GO |
| 71 |
cat > internal/providerfactory/providerfactory.go <<'GO' |
| 72 |
package providerfactory |
| 73 |
|
| 74 |
import _ "example.test/clnkr/internal/providers/openai" |
| 75 |
GO |
| 76 |
cat > cmd/internal/clnkrapp/clnkrapp.go <<'GO' |
| 77 |
package clnkrapp |
| 78 |
|
| 79 |
import ( |
| 80 |
_ "example.test/clnkr/internal/providerfactory" |
| 81 |
_ "example.test/clnkr/internal/session" |
| 82 |
) |
| 83 |
GO |
| 84 |
|
| 85 |
mkdir -p cmd/internal/other |
| 86 |
cat > cmd/internal/other/other.go <<'GO' |
| 87 |
package other |
| 88 |
|
| 89 |
import _ "example.test/clnkr/internal/providers/openai" |
| 90 |
GO |
| 91 |
|
| 92 |
set +e |
| 93 |
output="$(./check-architecture-imports.sh 2>&1)" |
| 94 |
status=$? |
| 95 |
set -e |
| 96 |
|
| 97 |
if [[ $status -ne 1 ]]; then |
| 98 |
echo "expected generic cmd concrete-provider import to fail with status 1, got $status" >&2 |
| 99 |
echo "$output" >&2 |
| 100 |
exit 1 |
| 101 |
fi |
| 102 |
|
| 103 |
expected="cmd/... may import only root clnkr, cmd/internal/..., internal/providers/providerconfig, internal/session from cmd/internal/clnkrapp, or internal/providerfactory from clnkrapp" |
| 104 |
if [[ "$output" != *"$expected"* ]]; then |
| 105 |
echo "missing expected output: $expected" >&2 |
| 106 |
echo "$output" >&2 |
| 107 |
exit 1 |
| 108 |
fi |
| 109 |
|
| 110 |
rm -rf cmd/internal/other |
| 111 |
|
| 112 |
mkdir -p cmd/clnkr internal/session |
| 113 |
cat > internal/session/session.go <<'GO' |
| 114 |
package session |
| 115 |
GO |
| 116 |
cat > cmd/clnkr/main.go <<'GO' |
| 117 |
package main |
| 118 |
|
| 119 |
import _ "example.test/clnkr/internal/session" |
| 120 |
GO |
| 121 |
|
| 122 |
set +e |
| 123 |
output="$(./check-architecture-imports.sh 2>&1)" |
| 124 |
status=$? |
| 125 |
set -e |
| 126 |
|
| 127 |
if [[ $status -ne 1 ]]; then |
| 128 |
echo "expected frontend session import to fail with status 1, got $status" >&2 |
| 129 |
echo "$output" >&2 |
| 130 |
exit 1 |
| 131 |
fi |
| 132 |
|
| 133 |
for expected in \ |
| 134 |
"error: architecture import boundary violation" \ |
| 135 |
"rule: ARCH011 frontend-session-boundary" \ |
| 136 |
"importer: example.test/clnkr/cmd/clnkr" \ |
| 137 |
"target: example.test/clnkr/internal/session" \ |
| 138 |
"import_source: imports" \ |
| 139 |
"trusted_rule: frontend adapters must use cmd/internal/clnkrapp instead of importing internal/session directly." \ |
| 140 |
"source_fact: go list reported importer imports target." \ |
| 141 |
"guidance: move session persistence calls behind cmd/internal/clnkrapp; do not import internal/session from cmd/... outside cmd/internal/clnkrapp." |
| 142 |
do |
| 143 |
if [[ "$output" != *"$expected"* ]]; then |
| 144 |
echo "missing expected output: $expected" >&2 |
| 145 |
echo "$output" >&2 |
| 146 |
exit 1 |
| 147 |
fi |
| 148 |
done |
| 149 |
|
| 150 |
rm -rf cmd/clnkr |
| 151 |
|
| 152 |
mkdir -p cmd/clnkr cmd/internal/clnkrapp |
| 153 |
cat > cmd/internal/clnkrapp/clnkrapp.go <<'GO' |
| 154 |
package clnkrapp |
| 155 |
GO |
| 156 |
cat > cmd/clnkr/cli_options.go <<'GO' |
| 157 |
package main |
| 158 |
|
| 159 |
import _ "example.test/clnkr/cmd/internal/clnkrapp" |
| 160 |
GO |
| 161 |
|
| 162 |
set +e |
| 163 |
output="$(./check-architecture-imports.sh 2>&1)" |
| 164 |
status=$? |
| 165 |
set -e |
| 166 |
|
| 167 |
if [[ $status -ne 1 ]]; then |
| 168 |
echo "expected CLI parser app import to fail with status 1, got $status" >&2 |
| 169 |
echo "$output" >&2 |
| 170 |
exit 1 |
| 171 |
fi |
| 172 |
|
| 173 |
for expected in \ |
| 174 |
"error: architecture import boundary violation" \ |
| 175 |
"rule: ARCH012 cli-parser-boundary" \ |
| 176 |
"importer: cmd/clnkr/cli_options.go" \ |
| 177 |
"target: example.test/clnkr/cmd/internal/clnkrapp" \ |
| 178 |
"import_source: file_imports" \ |
| 179 |
"trusted_rule: CLI option parsing must stay local and stdlib-only." \ |
| 180 |
"source_fact: file import scan reported importer imports target." \ |
| 181 |
"guidance: keep cmd/clnkr/cli_*.go parser-only; move app-service and config-resolution calls out of the parser." |
| 182 |
do |
| 183 |
if [[ "$output" != *"$expected"* ]]; then |
| 184 |
echo "missing expected output: $expected" >&2 |
| 185 |
echo "$output" >&2 |
| 186 |
exit 1 |
| 187 |
fi |
| 188 |
done |
| 189 |
|
| 190 |
|
| 191 |
rm -rf cmd/clnkr |
| 192 |
mkdir -p cmd/internal/providerconfig cmd/clnkr |
| 193 |
cat > cmd/internal/providerconfig/providerconfig.go <<'GO' |
| 194 |
package providerconfig |
| 195 |
GO |
| 196 |
cat > cmd/clnkr/main.go <<'GO' |
| 197 |
package main |
| 198 |
|
| 199 |
import _ "example.test/clnkr/cmd/internal/providerconfig" |
| 200 |
GO |
| 201 |
|
| 202 |
set +e |
| 203 |
output="$(./check-architecture-imports.sh 2>&1)" |
| 204 |
status=$? |
| 205 |
set -e |
| 206 |
|
| 207 |
if [[ $status -ne 1 ]]; then |
| 208 |
echo "expected frontend providerconfig import to fail with status 1, got $status" >&2 |
| 209 |
echo "$output" >&2 |
| 210 |
exit 1 |
| 211 |
fi |
| 212 |
|
| 213 |
for expected in \ |
| 214 |
"error: architecture import boundary violation" \ |
| 215 |
"rule: ARCH013 frontend-providerconfig-boundary" \ |
| 216 |
"importer: example.test/clnkr/cmd/clnkr" \ |
| 217 |
"target: example.test/clnkr/cmd/internal/providerconfig" \ |
| 218 |
"import_source: imports" \ |
| 219 |
"trusted_rule: frontend adapters must use cmd/internal/clnkrapp instead of importing cmd/internal/providerconfig directly." \ |
| 220 |
"source_fact: go list reported importer imports target." \ |
| 221 |
"guidance: move provider config and startup construction behind cmd/internal/clnkrapp; do not import cmd/internal/providerconfig from cmd/... outside cmd/internal/clnkrapp." |
| 222 |
do |
| 223 |
if [[ "$output" != *"$expected"* ]]; then |
| 224 |
echo "missing expected output: $expected" >&2 |
| 225 |
echo "$output" >&2 |
| 226 |
exit 1 |
| 227 |
fi |
| 228 |
done |
| 229 |
|
| 230 |
rm -rf cmd/clnkr |
| 231 |
mkdir -p cmd/clnkr |
| 232 |
|
| 233 |
cat > cmd/clnkr/cli_options.go <<'GO' |
| 234 |
package main |
| 235 |
|
| 236 |
import "flag" |
| 237 |
|
| 238 |
func valueFlag(arg string) bool { |
| 239 |
switch arg { |
| 240 |
case "-p": |
| 241 |
return true |
| 242 |
default: |
| 243 |
return false |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
var _ = flag.ContinueOnError |
| 248 |
GO |
| 249 |
|
| 250 |
cat >> go.mod <<'MOD' |
| 251 |
|
| 252 |
require foo v0.0.0 |
| 253 |
|
| 254 |
replace foo => ./foo |
| 255 |
MOD |
| 256 |
mkdir -p foo/pkg |
| 257 |
cat > foo/go.mod <<'MOD' |
| 258 |
module foo |
| 259 |
|
| 260 |
go 1.24 |
| 261 |
MOD |
| 262 |
cat > foo/pkg/pkg.go <<'GO' |
| 263 |
package pkg |
| 264 |
GO |
| 265 |
cat > cmd/clnkr/cli_options.go <<'GO' |
| 266 |
package main |
| 267 |
|
| 268 |
import _ "foo/pkg" |
| 269 |
GO |
| 270 |
|
| 271 |
set +e |
| 272 |
output="$(./check-architecture-imports.sh 2>&1)" |
| 273 |
status=$? |
| 274 |
set -e |
| 275 |
|
| 276 |
if [[ $status -ne 1 ]]; then |
| 277 |
echo "expected CLI parser no-dot non-stdlib import to fail with status 1, got $status" >&2 |
| 278 |
echo "$output" >&2 |
| 279 |
exit 1 |
| 280 |
fi |
| 281 |
|
| 282 |
for expected in \ |
| 283 |
"error: architecture import boundary violation" \ |
| 284 |
"rule: ARCH012 cli-parser-boundary" \ |
| 285 |
"importer: cmd/clnkr/cli_options.go" \ |
| 286 |
"target: foo/pkg" \ |
| 287 |
"import_source: file_imports" |
| 288 |
do |
| 289 |
if [[ "$output" != *"$expected"* ]]; then |
| 290 |
echo "missing expected output: $expected" >&2 |
| 291 |
echo "$output" >&2 |
| 292 |
exit 1 |
| 293 |
fi |
| 294 |
done |
| 295 |
|
| 296 |
cat > cmd/clnkr/cli_options.go <<'GO' |
| 297 |
package main |
| 298 |
|
| 299 |
import "flag" |
| 300 |
|
| 301 |
var _ = flag.ContinueOnError |
| 302 |
GO |
| 303 |
|
| 304 |
cat >> go.mod <<'MOD' |
| 305 |
|
| 306 |
require example.com/notstdlib v0.0.0 |
| 307 |
|
| 308 |
replace example.com/notstdlib => ./notstdlib |
| 309 |
MOD |
| 310 |
mkdir -p notstdlib/pkg |
| 311 |
cat > notstdlib/go.mod <<'MOD' |
| 312 |
module example.com/notstdlib |
| 313 |
|
| 314 |
go 1.24 |
| 315 |
MOD |
| 316 |
cat > notstdlib/pkg/pkg.go <<'GO' |
| 317 |
package pkg |
| 318 |
GO |
| 319 |
cat > cmd/clnkr/cli_options.go <<'GO' |
| 320 |
package main |
| 321 |
|
| 322 |
import _ "example.com/notstdlib/pkg" |
| 323 |
GO |
| 324 |
|
| 325 |
set +e |
| 326 |
output="$(./check-architecture-imports.sh 2>&1)" |
| 327 |
status=$? |
| 328 |
set -e |
| 329 |
|
| 330 |
if [[ $status -ne 1 ]]; then |
| 331 |
echo "expected CLI parser non-stdlib import to fail with status 1, got $status" >&2 |
| 332 |
echo "$output" >&2 |
| 333 |
exit 1 |
| 334 |
fi |
| 335 |
|
| 336 |
for expected in \ |
| 337 |
"error: architecture import boundary violation" \ |
| 338 |
"rule: ARCH012 cli-parser-boundary" \ |
| 339 |
"importer: cmd/clnkr/cli_options.go" \ |
| 340 |
"target: example.com/notstdlib/pkg" \ |
| 341 |
"import_source: file_imports" |
| 342 |
do |
| 343 |
if [[ "$output" != *"$expected"* ]]; then |
| 344 |
echo "missing expected output: $expected" >&2 |
| 345 |
echo "$output" >&2 |
| 346 |
exit 1 |
| 347 |
fi |
| 348 |
done |
| 349 |
|
| 350 |
cat > cmd/clnkr/cli_options.go <<'GO' |
| 351 |
package main |
| 352 |
|
| 353 |
import "flag" |
| 354 |
|
| 355 |
var _ = flag.ContinueOnError |
| 356 |
GO |
| 357 |
|
| 358 |
cat > cmd/clnkr/cli_helper.go <<'GO' |
| 359 |
package main |
| 360 |
|
| 361 |
import _ "example.com/notstdlib/pkg" |
| 362 |
GO |
| 363 |
|
| 364 |
set +e |
| 365 |
output="$(./check-architecture-imports.sh 2>&1)" |
| 366 |
status=$? |
| 367 |
set -e |
| 368 |
|
| 369 |
if [[ $status -ne 1 ]]; then |
| 370 |
echo "expected CLI parser helper non-stdlib import to fail with status 1, got $status" >&2 |
| 371 |
echo "$output" >&2 |
| 372 |
exit 1 |
| 373 |
fi |
| 374 |
|
| 375 |
for expected in \ |
| 376 |
"error: architecture import boundary violation" \ |
| 377 |
"rule: ARCH012 cli-parser-boundary" \ |
| 378 |
"importer: cmd/clnkr/cli_helper.go" \ |
| 379 |
"target: example.com/notstdlib/pkg" \ |
| 380 |
"import_source: file_imports" |
| 381 |
do |
| 382 |
if [[ "$output" != *"$expected"* ]]; then |
| 383 |
echo "missing expected output: $expected" >&2 |
| 384 |
echo "$output" >&2 |
| 385 |
exit 1 |
| 386 |
fi |
| 387 |
done |
| 388 |
|
| 389 |
rm -f cmd/clnkr/cli_helper.go |
| 390 |
|
| 391 |
output="$(./check-architecture-imports.sh 2>&1)" |
| 392 |
if [[ "$output" != *"target architecture import checks passed"* ]]; then |
| 393 |
echo "missing success output" >&2 |
| 394 |
echo "$output" >&2 |
| 395 |
exit 1 |
| 396 |
fi |