Skip to content

包括的なテストカバレッジの追加#34

Merged
harakeishi merged 12 commits intomainfrom
test/add-comprehensive-test-coverage
Feb 2, 2026
Merged

包括的なテストカバレッジの追加#34
harakeishi merged 12 commits intomainfrom
test/add-comprehensive-test-coverage

Conversation

@harakeishi
Copy link
Owner

@harakeishi harakeishi commented Feb 2, 2026

概要

config, errors, generator, logger, parser, resolver, scanner
の各パッケージに対して包括的なユニットテストを追加し、プロジェクト全体のテストカバレッジを大幅に向上させる。テストユーティリティ (testutil)
の導入により、テストコードのボイラープレートも削減した。

変更内容

  • internal/testutil/mocks.go を新規追加し、NewTestLogger() / MockPortDetector / MockPortAllocator を共通化
  • internal/config/default_test.go を追加 — デフォルト設定・環境別設定・設定インスタンスの独立性テスト
  • internal/errors/codes_test.go, types_test.go を追加 — エラーコード分類・AppError のチェーンテスト
  • internal/generator/override_test.go, unified_generator_test.go を追加 — オーバーライド生成・バリデーション・YAML出力テスト
  • internal/logger/structured_test.go を追加 — ログファクトリ・レベル解析・フォーマットテスト
  • internal/parser/yaml_test.go を拡張 — ポート解析・環境変数・depends_on・ネットワーク・エッジケーステスト
  • internal/resolver/conflict_test.go を追加 — コンフリクト検出・解決・分析・最適化テスト
  • internal/scanner/network_test.go, unified_detector_test.go, validator_test.go を追加 — ネットワーク検出・統合検出・ポートバリデーションテスト
  • internal/scanner/port_allocator_test.go を拡張 — 予約ポートスキップの網羅テスト
  • cmd/up_test.go のテスト名を英語に統一
  • internal/parser/yaml.go のポート正規表現に後方互換性コメントを追加
  • testdata/parser/edge_cases.yml, invalid.yml をテストデータとして追加

期待すること

  • 主要パッケージのテストカバレッジが向上し、リグレッションの早期検出が可能になる
  • testutil によりテストコードの保守性・一貫性が向上する
  • CI 環境での安定動作 (flaky テストの排除済み)

動作確認

項目 証憑(スクショなど) 備考
go test ./... 全パッケージ通過 CLI出力 全8パッケージ OK
golangci-lint run ./... 0 issues CLI出力 lint エラーなし
go build ./... ビルド成功 CLI出力 コンパイルエラーなし
Docker 未起動時にネットワークテストが t.Skip でスキップされる CLI出力 統合テストの安全性確認
Docker 起動時にネットワーク検出テストが通過する CLI出力 bridge/host/none が検出される
エッジケース YAML (testdata/parser/edge_cases.yml) のパースが正常に完了する テスト結果 空サービス・オブジェクト形式ポート等
不正 YAML (testdata/parser/invalid.yml) でエラーが返される テスト結果 abc:80 形式でパースエラー
予約ポートがアロケーションから除外される テスト結果 複数パターンで検証済み
コンフリクト解決で重複ポートが発生しない テスト結果 散在する予約ポートのスキップ確認

harakeishi and others added 12 commits February 2, 2026 16:34
## 変更内容
- 失敗していたテスト修正: 環境変数PORTのクリア処理追加
- internal/errors: テストカバレッジ 0% -> 46.2%
- internal/logger: テストカバレッジ 0% -> 51.4%
- internal/config: テストカバレッジ 0% -> 75.0%
- cmd: テストカバレッジ 9.2% -> 28.0%

## 修正詳細
### 1. yaml_test.go (環境変数バグ修正)
- PORT環境変数が設定されていたため、デフォルト値のテストが失敗
- テスト実行前に環境変数をクリアする処理を追加

### 2. 新規テストファイル追加
- internal/errors/codes_test.go: ErrorCodeのテスト
- internal/errors/types_test.go: AppErrorの各メソッドテスト
- internal/logger/structured_test.go: StructuredLoggerのテスト
- internal/config/default_test.go: 設定関連のテスト

## テスト結果
全テストがパス。主要モジュールのカバレッジが大幅に向上。

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- Fix existing test failure in yaml_test.go (environment variable issue)
- Add internal/config tests (100% coverage achieved)
- Add internal/scanner/validator tests (port validation)
- Improve overall test coverage from 13.7% to 45.6%

Test statistics:
- New test files: 2
- Total test coverage improvement: 3.3x
- All tests: PASS

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- Implement comprehensive tests for internal/scanner/network.go
- Coverage: NewDockerNetworkDetector 100%, DetectNetworks 88.9%
- Test cases:
  - Constructor validation
  - Network detection integration test
  - Context cancellation handling
  - NetworkInfo data structure validation
  - Edge cases and boundary conditions
- Achieves 88.9% coverage (target: 60%)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- Add comprehensive test cases for ParseServicePorts
- Add tests for parsePortObject (object-format ports)
- Add tests for parseEnvironment (array/map formats)
- Add tests for parseDependsOn (array/map formats)
- Add tests for parseNetworks (simple/detailed configs)
- Add tests for convertToNetwork (empty/no-ipam/multi-subnet)
- Add edge case tests with real YAML files
- Add error handling tests for invalid port formats

Test data files:
- testdata/parser/edge_cases.yml: comprehensive edge cases
- testdata/parser/invalid.yml: invalid format tests

Coverage improved: 14.1% → 72.5%

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- errcheck: WithField の戻り値を変数に代入
- staticcheck SA5011: t.Error を t.Fatal に変更し nil ポインタ参照を防止

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- override_test.go: カスタムcontains関数をstrings.Containsに置換
- unified_generator_test.go: string(rune(i))をfmt.Sprintfに修正
- network_test.go: nil contextテスト名と実装を一致させる
- structured_test.go: FormatJSONテストに出力内容検証を追加
- testutil/mocks.go: 共通モックをtestutilパッケージに集約
- conflict_test.go: 未使用変数を削除、共通モック使用に移行
- yaml_test.go: 環境変数クリーンアップ改善、invalid.ymlテスト追加
- テスト名を英語に統一

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Translate all Japanese test names and comments to English across all test files
- Replace Reserved array loop comparison with reflect.DeepEqual in config/default_test.go
- Remove duplicate boundary test cases from TestValidatePort (consolidated in TestValidatePortEdgeCases)
- Strengthen TestAllocateNewSubnet validation (t.Logf -> t.Errorf for strict assertion)
- Fix misleading env cleanup comments in yaml_test.go

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Translate Japanese comments to English in testdata and testutil
- Add NewTestLogger helper to reduce boilerplate across tests
- Verify slog.Level values in TestParseLogLevel
- Use t.Skip for Docker-dependent integration tests
- Replace fragile string comparison with checkServiceIPs field
- Restructure invalid.yml for clearer test intent

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Replace test boilerplate with testutil.NewTestLogger() across 8 files (-67 lines)
- Replace time.Now() with fixed time in override_test.go for reproducibility
- Add pointer comparison clarifying comment in default_test.go
- Add backward compatibility comment for port regex in yaml.go
- Add minimum network count assertion in network_test.go

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
IsZero() check is sufficient to verify GeneratedAt is set.
time.Since() with 1-second threshold could fail in slow CI environments.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@harakeishi harakeishi merged commit 884d4f8 into main Feb 2, 2026
4 checks passed
@harakeishi harakeishi deleted the test/add-comprehensive-test-coverage branch February 2, 2026 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant