Skip to content

Commit 05d270e

Browse files
authoredMay 12, 2023
Merge pull request #622 from LandonTClipp/migration
Add packages migration docs
2 parents 587e962 + 18a0dba commit 05d270e

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed
 

‎docs/features.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ func (_m *Handler) HandleMessage(m pubsub.Message) error {
9595
:octicons-tag-24: v2.21.0 · :material-test-tube: Alpha Feature
9696

9797
!!! danger
98-
This feature is considered alpha. It is likely that bugs exist, and subfeatures may be added/subtracted/modified at any time. Use at your own risk. This warning will be updated as this feature matures.
98+
This feature is considered alpha. It is likely that bugs exist, and subfeatures may be added/subtracted/modified at any time. Use at your own risk. This warning will be updated as this feature matures.
99+
100+
!!! info
101+
See the [Migration Docs](/mockery/migrating_to_packages/) on how to migrate to this new feature.
99102

100103
Mockery has a configuration parameter called `packages`. This config represents a huge paradigm shift that is highly recommended for the large amount of flexibility it grants you.
101104

‎docs/migrating_to_packages.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: Migrating To Packages
3+
---
4+
5+
The [packages](/mockery/features/#packages-configuration) feature is a new configuration scheme that aims to simplify and improve a lot of legacy behavior. This will be the only way to generate mocks in v3. These docs outline general principals for migrating to the new scheme.
6+
7+
Background
8+
----------
9+
10+
mockery was built during the pre-module era of Golang. Much of its codebase and configuration syntax was designed around file-based operations. This model became highly inefficient once Golang migrated to module-based packages. The old configuration semantics also proved limiting -- many users introduced and requested feature additions to mockery to support esoteric use-cases. This proved to be a huge maintenance burden that existed solely because the configuration model could not flexibly describe all the situations users wanted. The `packages` semantics provides us a few highly desirable traits:
11+
12+
1. Orders of magnitude performance increase, due to calling `packages.Load` once or twice for an entire project, versus once per file in the legacy semantics.
13+
2. Hierarchical configuration model that allows interface-specific config to be inherited from package-level config, which is inherited from defaults.
14+
3. Single configuration file that describes the entirety of mockery's behavior, instead of spread out by `//go: generate` statements.
15+
4. Extensive and flexible usage of a Golang string templating environment that allows users to dynamically specify parameter values.
16+
17+
Configuration Changes
18+
----------------------
19+
20+
The existence of the `#!yaml packages:` map in your configuration acts as a feature flag that enables the feature.
21+
22+
The configuration parameters used in `packages` have either changed meanings, been removed, or been renamed. It is recommended to wipe out all previous configuration and command-line parameters previously used.
23+
24+
The [configuration docs](/mockery/configuration/#packages-config) show the parameters that are available for use in the `packages` scheme. You should only use the parameters shown in this section. Mockery will not prevent you from using the legacy parameter set, but doing so will result in undefined behavior.
25+
26+
All of the parameters in the config section can be specified at the top level of the config file, which serves as the default values. The `packages` config section defines package-specific config. See some exampes [here](/mockery/features/#examples).
27+
28+
`//go: generate` directives
29+
----------------------------
30+
31+
Previously, the recommended way of generating mocks was to call `mockery` once per interface using `//go: generate`. Generating interface-specific mocks this way is no longer supported. You may still use `//go: generate` to call mockery, however it will generate all interfaces defined in your config file. There currently exists no semantics to specify the generation of specific interfaces from the command line (not because we reject the idea, but because it was not seen as a requirement for the initial iteration of `packages`).
32+
33+
Behavior Changes
34+
-----------------
35+
36+
The legacy behavior iterated over every `.go` file in your project, called [`packages.Load`](https://pkg.go.dev/golang.org/x/tools/go/packages#Load) to parse the syntax tree, and generated mocks for every interface found in the file. The new behavior instead simply grabs the list of packages to load from the config file, or in the case of `#!yaml recursive: True`, walks the filesystem tree to discover the packages that exist (without actually parsing the files). Using this list, it calls `packages.Load` once with the list of packages that were discovered.
37+
38+
Filesystem Tree Layouts
39+
------------------------
40+
41+
The legacy config provided the `keeptree` parameter which, if `#!yaml keeptree: True`, would place the mocks in the same package as your interfaces. Otherwise, it would place it in a separate directory.
42+
43+
These two layouts are supported in the `packages` scheme. See the relevant docs [here](/mockery/features/#layouts).

‎mkdocs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ theme:
1010
name: material
1111
icon:
1212
logo: fontawesome/brands/golang
13-
favicon: fontawesome/brands/golang
1413
palette:
1514
# Palette toggle for light mode
1615
- media: "(prefers-color-scheme: light)"
@@ -66,6 +65,7 @@ nav:
6665
- Notes:
6766
- FAQ: notes.md
6867
- Changelog: changelog.md
68+
- Migrating to Packages: migrating_to_packages.md
6969

7070
extra_css:
7171
- stylesheets/extra.css

0 commit comments

Comments
 (0)
Please sign in to comment.