From a4c80b3045e65afbf86de44c89ad18deca51a43f Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Mon, 4 Jul 2022 00:39:01 +0000 Subject: [PATCH] chore(ci): add mocks check - Check for missing `//go:generate` comments - Check for outdated mocks --- .github/workflows/ci.yml | 3 +++ Dockerfile | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 981e6f74..1bf46ce0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,6 +49,9 @@ jobs: - name: Linting run: docker build --target lint . + - name: Mocks check + run: docker build --target mocks . + - name: Build test image run: docker build --target test -t test-container . diff --git a/Dockerfile b/Dockerfile index c43f4938..5c840214 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,16 +3,20 @@ ARG GO_ALPINE_VERSION=3.16 ARG GO_VERSION=1.17 ARG XCPUTRANSLATE_VERSION=v0.6.0 ARG GOLANGCI_LINT_VERSION=v1.46.2 +ARG MOCKGEN_VERSION=v1.6.0 ARG BUILDPLATFORM=linux/amd64 FROM --platform=${BUILDPLATFORM} qmcgaw/xcputranslate:${XCPUTRANSLATE_VERSION} AS xcputranslate FROM --platform=${BUILDPLATFORM} qmcgaw/binpot:golangci-lint-${GOLANGCI_LINT_VERSION} AS golangci-lint +FROM --platform=${BUILDPLATFORM} qmcgaw/binpot:mockgen-${MOCKGEN_VERSION} AS mockgen FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION}-alpine${GO_ALPINE_VERSION} AS base COPY --from=xcputranslate /xcputranslate /usr/local/bin/xcputranslate -RUN apk --update add git g++ +# Note: findutils needed to have xargs support `-d` flag for mocks stage. +RUN apk --update add git g++ findutils ENV CGO_ENABLED=0 COPY --from=golangci-lint /bin /go/bin/golangci-lint +COPY --from=mockgen /bin /go/bin/mockgen WORKDIR /tmp/gobuild COPY go.mod go.sum ./ RUN go mod download @@ -30,6 +34,18 @@ FROM --platform=${BUILDPLATFORM} base AS lint COPY .golangci.yml ./ RUN golangci-lint run --timeout=10m +FROM --platform=${BUILDPLATFORM} base AS mocks +RUN git init && \ + git config user.email ci@localhost && \ + git config user.name ci && \ + git config core.fileMode false && \ + git add -A && \ + git commit -m "snapshot" && \ + grep -lr -E '^// Code generated by MockGen\. DO NOT EDIT\.$' . | xargs -r -d '\n' rm && \ + go generate -run "mockgen" ./... && \ + git diff --exit-code && \ + rm -rf .git/ + FROM --platform=${BUILDPLATFORM} base AS build ARG TARGETPLATFORM ARG VERSION=unknown