Skip to content

Commit 8fdceb4

Browse files
Merge pull request #535 from ThaminduDilshan/thamindu-mocks
Improve mock generation
2 parents 80f4112 + 7f804c7 commit 8fdceb4

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed

.github/copilot-instructions.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ The project is structured as a monorepo to manage the backend, frontend, and sam
4040
- tests/: Common unit tests related files.
4141
- mocks/: Generated mocks for unit tests.
4242
- resources/: Test resource files.
43-
- .mockery.yml: Mockery configurations related to mock generation.
43+
- .mockery.private.yml: Mockery configurations for private interfaces.
44+
- .mockery.public.yml: Mockery configurations for public interfaces.
4445
- frontend/: Individual frontend application code.
4546
- apps/gate/: Gate app implementation which serves UIs for login, registration and recovery.
4647
- packages/: Common frontend packages such as UI components, services, and contexts.
@@ -113,7 +114,7 @@ The project is structured as a monorepo to manage the backend, frontend, and sam
113114
#### Unit Tests
114115
- Ensure unit tests are written to achieve at least 80% coverage.
115116
- Use `stretchr/testify` for tests and follow the test suite pattern.
116-
- `mockery` is used to generate mocks; configurations are in `/backend/.mockery.yml`.
117+
- `mockery` is used to generate mocks; configurations for private and public interfaces are in `.mockery.private.yml` and `.mockery.public.yml` respectively. Mocks can be generated using `make mockery` command from the project root directory.
117118
- Place generated mocks in the `/backend/tests/mocks/` directory.
118119
- Unit tests can be run using `make test_unit` command from the project root directory. Alternatively `go test` command can also be used from the `/backend` directory with applicable flags.
119120

Makefile

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ PROJECT_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))/backen
2626
PROJECT_BIN_DIR := $(PROJECT_DIR)/bin
2727
TOOL_BIN ?= $(PROJECT_BIN_DIR)/tools
2828
GOLANGCI_LINT ?= $(TOOL_BIN)/golangci-lint
29+
MOCKERY ?= $(TOOL_BIN)/mockery
30+
31+
# Tools versions
2932
GOLANGCI_LINT_VERSION ?= v1.64.8
33+
MOCKERY_VERSION ?= v3.5.5
3034

3135
$(TOOL_BIN):
3236
mkdir -p $(TOOL_BIN)
@@ -95,6 +99,10 @@ docker-build-multiarch-push:
9599
lint: golangci-lint
96100
cd backend && $(GOLANGCI_LINT) run ./...
97101

102+
mockery: install-mockery
103+
cd backend && $(MOCKERY) --config .mockery.public.yml
104+
cd backend && $(MOCKERY) --config .mockery.private.yml
105+
98106
help:
99107
@echo "Makefile targets:"
100108
@echo " all - Clean, build, and test the project."
@@ -116,13 +124,15 @@ help:
116124
@echo " docker-build-multiarch-latest - Build multi-arch Docker image with latest tag."
117125
@echo " docker-build-multiarch-push - Build and push multi-arch images to registry."
118126
@echo " lint - Run golangci-lint on the project."
127+
@echo " mockery - Generate mocks for unit tests using mockery."
119128
@echo " help - Show this help message."
120129

121130
.PHONY: all prepare clean clean_all build build_samples package_samples run
122131
.PHONY: docker-build docker-build-latest docker-build-multiarch
123132
.PHONY: docker-build-multiarch-latest docker-build-multiarch-push
124133
.PHONY: test_unit test_integration build_with_coverage test
125-
.PHONY: lint help go_install_tool golangci-lint
134+
.PHONY: help go_install_tool
135+
.PHONY: lint golangci-lint mockery install-mockery
126136

127137
define go_install_tool
128138
cd /tmp && \
@@ -133,3 +143,8 @@ golangci-lint: $(GOLANGCI_LINT)
133143

134144
$(GOLANGCI_LINT): $(TOOL_BIN)
135145
$(call go_install_tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
146+
147+
install-mockery: $(MOCKERY)
148+
149+
$(MOCKERY): $(TOOL_BIN)
150+
$(call go_install_tool,$(MOCKERY),github.com/vektra/mockery/v3,$(MOCKERY_VERSION))

backend/.mockery.private.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ------------------------------------------------------------------------------
2+
# Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
3+
#
4+
# WSO2 LLC. licenses this file to you under the Apache License,
5+
# Version 2.0 (the "License"); you may not use this file except
6+
# in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
# ------------------------------------------------------------------------------
18+
19+
# Mockery configuration file for generating private mocks within the package
20+
template: testify
21+
template-data:
22+
unroll-variadic: true
23+
packages:
24+
github.com/asgardeo/thunder/internal/system/cache:
25+
config:
26+
all: true
27+
dir: internal/system/cache
28+
structname: '{{.InterfaceName}}Mock'
29+
pkgname: cache
30+
filename: "{{.InterfaceName}}_mock_test.go"

backend/.mockery.yml renamed to backend/.mockery.public.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717
# ------------------------------------------------------------------------------
1818

19+
# Mockery configuration file for generating public mocks
1920
template: testify
2021
template-data:
2122
unroll-variadic: true
@@ -59,15 +60,6 @@ packages:
5960
structname: '{{.InterfaceName}}Mock'
6061
pkgname: providermock
6162
filename: "{{.InterfaceName}}_mock.go"
62-
63-
# Uncomment to generate mocks in the cache package.
64-
# github.com/asgardeo/thunder/internal/system/cache:
65-
# config:
66-
# all: true
67-
# dir: internal/system/cache
68-
# structname: '{{.InterfaceName}}Mock'
69-
# pkgname: cache
70-
# filename: "{{.InterfaceName}}_mock_test.go"
7163

7264
github.com/asgardeo/thunder/internal/system/cache:
7365
config:

0 commit comments

Comments
 (0)