Lompat ke konten
dokumentasi.id
Commit ecba1e8
v2
Star2,8 rb

Kamu membaca versi lama. Halaman ini hanya bisa dibaca.

9

Pengembangan dan Pengujian

Repository ini memakai satu Makefile sebagai pintu masuk pekerjaan lokal: setup, compile, fmt, lint, test, dan laporan coverage. Target all merangkai build lalu test (Makefile:1-2). Halaman ini merangkum perintah tersebut, konfigurasi golangci-lint, pola test yang dipakai, dan aturan kontribusi repository.

Alur kerja lokal

Target build bergantung pada compile, fmt, dan lint (Makefile:15-21). Target test bergantung pada fmt dan build, lalu menjalankan go test dengan prefix ENVIRONMENT=test (Makefile:23-24). Akibatnya, satu perintah make test memformat kode, mengompilasi seluruh paket dengan -race, menjalankan golangci-lint, dan baru menjalankan test.

compile menyiapkan direktori out/ sebelum go build -race ./... (Makefile:11-13). Direktori itu dipakai lagi oleh test-cover-html untuk menaruh out/coverage.html (Makefile:26-31).

graph TD
  setup["make setup: golangci-lint v2.8.0 + goveralls v0.0.12"] --> fmt["make fmt: go fmt ./..."]
  fmt --> compile["make compile: mkdir -p out/ lalu go build -race ./..."]
  compile --> lint["make lint: golangci-lint run ./..."]
  lint --> test["make test: ENVIRONMENT=test go test -race -covermode=atomic"]
  test --> cov["coverage.out"]
  cov --> html["make test-cover-html: coverage-all.out lalu out/coverage.html"]
  style setup fill:#f59e0b,color:#000
  style fmt fill:#2d7d46,color:#fff
  style compile fill:#2d7d46,color:#fff
  style lint fill:#2d7d46,color:#fff
  style test fill:#2d7d46,color:#fff
  style cov fill:#8b5cf6,color:#fff
  style html fill:#8b5cf6,color:#fff
%% fig: Rantai target Makefile untuk kerja lokal
%% desc: build merangkai fmt, compile, dan lint; test menambahkan go test -race dan menghasilkan coverage.out yang dirakit menjadi laporan HTML.
Fig. 1: Rantai target Makefile untuk kerja lokalbuild merangkai fmt, compile, dan lint; test menambahkan go test -race dan menghasilkan coverage.out yang dirakit menjadi laporan HTML.
TargetPrasyaratPerintah utamaArtefakRujukan
setup—mkdir -p $(GOPATH)/bin, lalu go install untuk golangci-lint v2.8.0 dan goveralls v0.0.12binary di $(GOPATH)/binMakefile:6-9
compile—mkdir -p out/ lalu go build -race ./...direktori out/Makefile:11-13
fmt—go fmt ./...—Makefile:15-21
lint—golangci-lint run ./...—Makefile:15-21
buildcompile fmt lint——Makefile:15-21
testfmt buildENVIRONMENT=test go test -race -covermode=atomic -coverprofile=coverage.out ./...coverage.outMakefile:23-24
test-cover-html—loop $(ALL_PACKAGES): go test -race -coverprofile=coverage.out -covermode=count per paket, lalu go tool cover -html=coverage-all.out -o out/coverage.htmlcoverage-all.out, out/coverage.htmlMakefile:26-31
allbuild test——Makefile:1-2

test-cover-html mengambil daftar paket dari ALL_PACKAGES, yaitu keluaran go list ./... yang disaring tanpa vendor (Makefile:4-4).

⚠️ make setup memakai variabel make $(GOPATH). Bila variabel lingkungan GOPATH tidak diset, perintah pertama mengembang menjadi mkdir -p /bin, sehingga binary golangci-lint tidak mendarat di direktori yang ikut PATH (Makefile:6-9).

⚠️ make test-cover-html menulis ulang coverage.out untuk setiap paket dan menyambung barisnya ke coverage-all.out. Setelah target itu berjalan, coverage.out hanya berisi hasil paket terakhir, jadi jalankan make test lagi bila butuh coverage.out utuh (Makefile:26-31).

Konfigurasi golangci-lint

Berkas .golangci.yml memakai version: "2" dan menyalakan run.tests: true, sehingga berkas _test.go ikut diperiksa. linters.default diset none, lalu linter diaktifkan satu per satu: govet, ineffassign, staticcheck, unused, modernize, dan paralleltest (.golangci.yml:1-12).

BagianNilaiRujukan
linters.enablegovet, ineffassign, staticcheck, unused, modernize, paralleltest.golangci.yml:1-12
linters.exclusionsgenerated: lax; paths: vendor, third_party$, builtin$, examples/.golangci.yml:13-19
issuesmax-issues-per-linter 0, max-same-issues 0.golangci.yml:20-32
formattersenable: gofmt, dengan paths eksklusi yang sama.golangci.yml:20-32

Nilai 0 pada max-issues-per-linter dan max-same-issues berarti golangci-lint tidak memotong jumlah temuan yang dilaporkan (.golangci.yml:20-32). Linter paralleltest menjelaskan kebiasaan di repository ini: setiap fungsi Test* memanggil t.Parallel() di baris pertama, seperti pada TestRetrierWithExponentialBackoff (retry_test.go:10-17).

Pola pengujian

Test memakai testify dalam tiga bentuk, yaitu assert, require, dan mock (httpclient/client_test.go:17-19). Paket httpclient mencontohkan pola yang diulang di banyak test: NewClient dengan option, dummyHandler yang memeriksa method dan header, lalu httptest.NewServer sebagai target request (httpclient/client_test.go:22-36). Dependensi github.com/gojek/hystrix-go/hystrix v1.0.0 dan testify v1.11.1 tercatat di go.mod bersama direktif go 1.25 (go.mod:1-8).

Prefix ENVIRONMENT=test muncul di target test (Makefile:23-24) dan diulang pada loop test-cover-html (Makefile:26-31). Karena test dijalankan dengan -race, seluruh paket ikut dibangun dengan -race pula lewat compile (Makefile:11-13).

Siklus request dan retry yang diuji di paket httpclient dibahas di Siklus Request dan Retry, sedangkan perilaku backoff yang diuji TestRetrierWithExponentialBackoff dibahas di Retry dan Backoff.

Aturan kontribusi

CONTRIBUTING.md meminta kontributor membuka issue lebih dulu, mengikuti Effective Go Community Guidelines, dan menyatukan seluruh commit menjadi satu sebelum push dengan pesan yang merujuk bug (CONTRIBUTING.md:6-17). Laporan issue harus jelas, ringkas, memakai judul deskriptif, dan menyertakan potongan kode yang relevan (CONTRIBUTING.md:19-24). pull request memakai topic branch, mengikuti konvensi kode yang sudah ada, dan hanya membahas satu subjek (CONTRIBUTING.md:26-32).

Riwayat rilis

CHANGELOG.md hanya memuat satu entri, v0.0.1 (2018-JAN-19), berisi initial fork commit dan open-source stepping stones (CHANGELOG.md:3-6). Versi modul saat ini terbaca dari path modul github.com/gojek/heimdall/v8 (go.mod:1-8).

Alur CI

CI berjalan untuk push dan pull_request ke master (.github/workflows/ci.yml:3-7), dengan matriks tiga versi Go: 1.25.x, 1.26.x, dan stable (.github/workflows/ci.yml:13-15). Sebelum build, cache ~/.cache/go-build dan ~/go/pkg/mod dipulihkan dengan key dari hashFiles('**/go.sum') (.github/workflows/ci.yml:26-34).

graph LR
  trigger["push / pull_request ke master"] --> matrix["matrix go-version: 1.25.x, 1.26.x, stable"]
  matrix --> cache["actions/cache: ~/.cache/go-build + ~/go/pkg/mod"]
  cache --> setup["make setup"]
  setup --> test["make test"]
  test --> coveralls["coverallsapp/github-action@v2: coverage.out"]
  style trigger fill:#f59e0b,color:#000
  style matrix fill:#f59e0b,color:#000
  style cache fill:#8b5cf6,color:#fff
  style setup fill:#f59e0b,color:#000
  style test fill:#2d7d46,color:#fff
  style coveralls fill:#2d7d46,color:#fff
%% fig: Jalur job CI
%% desc: Trigger push atau pull request menjalankan job test pada tiga versi Go, memulihkan cache modul, lalu mengunggah coverage.out ke Coveralls.
Fig. 2: Jalur job CITrigger push atau pull request menjalankan job test pada tiga versi Go, memulihkan cache modul, lalu mengunggah coverage.out ke Coveralls.
LangkahIsiRujukan
Triggerpush dan pull_request ke master.github/workflows/ci.yml:3-7
Matriksgo-version: ['1.25.x', '1.26.x', 'stable'].github/workflows/ci.yml:13-15
Cache~/.cache/go-build, ~/go/pkg/mod, key dari hashFiles('**/go.sum').github/workflows/ci.yml:26-34
Build dan testmake setup lalu make test.github/workflows/ci.yml:36-40
Coveragecoverallsapp/github-action@v2, file: coverage.out, format: golang.github/workflows/ci.yml:42-47

Pipeline memanggil target Makefile yang sama seperti di lokal, yaitu make setup lalu make test (.github/workflows/ci.yml:36-40). goveralls dipasang oleh setup tetapi tidak dipakai target mana pun; unggahan coverage dikerjakan coverallsapp/github-action@v2 dengan file: coverage.out (.github/workflows/ci.yml:42-47).