Skip to content

Simplify Fakes into a Swift package #15772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
422 changes: 0 additions & 422 deletions Fakes/Fakes.xcodeproj/project.pbxproj

This file was deleted.

This file was deleted.

This file was deleted.

18 changes: 0 additions & 18 deletions Fakes/Fakes/Fakes.h

This file was deleted.

1 change: 0 additions & 1 deletion Fakes/Fakes/Fakes.xcconfig

This file was deleted.

22 changes: 0 additions & 22 deletions Fakes/Fakes/Info.plist

This file was deleted.

26 changes: 16 additions & 10 deletions Modules/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ let package = Package(
name: "Codegen",
targets: ["Codegen"]
),
.library(
name: "Fakes",
targets: ["Fakes"]
),
.library(
name: "Experiments",
targets: ["Experiments"]
Expand Down Expand Up @@ -107,6 +111,15 @@ let package = Package(
.product(name: "AutomatticTracks", package: "Automattic-Tracks-iOS"),
]
),
.target(
name: "Fakes",
dependencies: [
"Codegen",
"Hardware",
"Networking",
"Yosemite"
]
),
.target(
name: "Hardware",
dependencies: [
Expand Down Expand Up @@ -266,7 +279,6 @@ enum XcodeTargetNames {
enum XcodeSupport {
static var products: [Product] {
[
XcodeTargetNames.fakes,
XcodeTargetNames.hardwareTests,
XcodeTargetNames.networkingTests,
XcodeTargetNames.notificationExtension,
Expand All @@ -286,15 +298,6 @@ enum XcodeSupport {

static var targets: [Target] {
[
.xcodeTarget(
XcodeTargetNames.fakes,
dependencies: [
"Codegen",
"Hardware",
"Networking",
"Yosemite"
]
),
.xcodeTarget(
XcodeTargetNames.hardwareTests,
dependencies: [
Expand All @@ -305,6 +308,7 @@ enum XcodeSupport {
XcodeTargetNames.networkingTests,
dependencies: [
"Codegen",
"Fakes",
"Networking",
"TestKit",
"WooFoundation",
Expand Down Expand Up @@ -389,6 +393,7 @@ enum XcodeSupport {
XcodeTargetNames.wooCommerceTests,
dependencies: [
"Codegen",
"Fakes",
"TestKit",
"WordPressShared",
.product(name: "Aztec", package: "AztecEditor-iOS"),
Expand Down Expand Up @@ -442,6 +447,7 @@ enum XcodeSupport {
XcodeTargetNames.yosemiteTests,
dependencies: [
"Codegen",
"Fakes",
"TestKit",
"WooFoundation",
"WordPressShared",
Expand Down
2 changes: 1 addition & 1 deletion Modules/Sources/Codegen/Sourcery/Fakes/Hardware-Fakes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ sources:
- ../../../../../Modules/Sources/Hardware/
templates:
- Fakes.swifttemplate
output: ../../../../../Fakes/Fakes/Hardware.generated.swift
output: ../../../../../Modules/Sources/Fakes/Hardware.generated.swift
args:
moduleName: Hardware
4 changes: 2 additions & 2 deletions Modules/Sources/Codegen/Sourcery/Fakes/Networking-Fakes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ configurations:
- ../../../../../Modules/Sources/Networking/
templates:
- Fakes.swifttemplate
output: ../../../../../Fakes/Fakes/Networking.generated.swift
output: ../../../../../Modules/Sources/Fakes/Networking.generated.swift
args:
moduleName: Networking
- sources:
include:
- ../../../../../Modules/Sources/NetworkingCore/
templates:
- Fakes.swifttemplate
output: ../../../../../Fakes/Fakes/NetworkingCore.generated.swift
output: ../../../../../Modules/Sources/Fakes/NetworkingCore.generated.swift
args:
moduleName: NetworkingCore
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ configurations:
- ../../../../../Modules/Sources/WooFoundation
templates:
- Fakes.swifttemplate
output: ../../../../../Fakes/Fakes/WooFoundation.generated.swift
output: ../../../../../Modules/Sources/Fakes/WooFoundation.generated.swift
args:
moduleName: WooFoundation
- sources:
include:
- ../../../../../Modules/Sources/WooFoundationCore
templates:
- Fakes.swifttemplate
output: ../../../../../Fakes/Fakes/WooFoundationCore.generated.swift
output: ../../../../../Modules/Sources/Fakes/WooFoundationCore.generated.swift
args:
moduleName: WooFoundationCore
2 changes: 1 addition & 1 deletion Modules/Sources/Codegen/Sourcery/Fakes/Yosemite-Fakes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ sources:
- ../../../../../Modules/Sources/Yosemite/
templates:
- Fakes.swifttemplate
output: ../../../../../Fakes/Fakes/Yosemite.generated.swift
output: ../../../../../Modules/Sources/Fakes/Yosemite.generated.swift
args:
moduleName: Yosemite
File renamed without changes.
7 changes: 0 additions & 7 deletions Modules/Sources/XcodeSupport/XcodeTarget_Fakes/Empty.swift

This file was deleted.

4 changes: 0 additions & 4 deletions Networking/Networking.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
2670C3FC270F4E06002FE931 /* SiteListMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2670C3FB270F4E06002FE931 /* SiteListMapperTests.swift */; };
2685C0DA263B551300D9EE97 /* AddOnGroupMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2685C0D9263B551300D9EE97 /* AddOnGroupMapperTests.swift */; };
2685C102263B6A1000D9EE97 /* AddOnGroupRemoteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2685C101263B6A1000D9EE97 /* AddOnGroupRemoteTests.swift */; };
26FB056C25F6CB9100A40B26 /* Fakes.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 26FB056B25F6CB9100A40B26 /* Fakes.framework */; };
311976E02602BD4B006AC56C /* SitePluginsMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 311976DF2602BD4B006AC56C /* SitePluginsMapperTests.swift */; };
31A451BD2786344B00FE81AA /* StripeRemoteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31A451BC2786344B00FE81AA /* StripeRemoteTests.swift */; };
31D27C952602B737002EDB1D /* SitePluginsRemoteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31D27C942602B737002EDB1D /* SitePluginsRemoteTests.swift */; };
Expand Down Expand Up @@ -288,7 +287,6 @@
2685C0D9263B551300D9EE97 /* AddOnGroupMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddOnGroupMapperTests.swift; sourceTree = "<group>"; };
2685C101263B6A1000D9EE97 /* AddOnGroupRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddOnGroupRemoteTests.swift; sourceTree = "<group>"; };
26E5A08B25A66FD3000DF8F6 /* ProductAttributeTermRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductAttributeTermRemoteTests.swift; sourceTree = "<group>"; };
26FB056B25F6CB9100A40B26 /* Fakes.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Fakes.framework; sourceTree = BUILT_PRODUCTS_DIR; };
311976DF2602BD4B006AC56C /* SitePluginsMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SitePluginsMapperTests.swift; sourceTree = "<group>"; };
31A451BC2786344B00FE81AA /* StripeRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StripeRemoteTests.swift; sourceTree = "<group>"; };
31D27C942602B737002EDB1D /* SitePluginsRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SitePluginsRemoteTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -478,7 +476,6 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
26FB056C25F6CB9100A40B26 /* Fakes.framework in Frameworks */,
3F2B4ACE2DDC30F400E5E49C /* XcodeTarget_NetworkingTests in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -498,7 +495,6 @@
35C45CCE73A311BE252F6BF4 /* Frameworks */ = {
isa = PBXGroup;
children = (
26FB056B25F6CB9100A40B26 /* Fakes.framework */,
);
name = Frameworks;
sourceTree = "<group>";
Expand Down
3 changes: 0 additions & 3 deletions WooCommerce.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions WooCommerce/WooCommerce.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,6 @@
26F94E26267A559300DB6CCF /* ProductAddOn.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26F94E25267A559300DB6CCF /* ProductAddOn.swift */; };
26F94E2E267A96A000DB6CCF /* ProductAddOnViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26F94E2D267A96A000DB6CCF /* ProductAddOnViewModel.swift */; };
26F94E34267AA42F00DB6CCF /* ProductAddOnViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26F94E33267AA42F00DB6CCF /* ProductAddOnViewModelTests.swift */; };
26FB056825F6CB6000A40B26 /* Fakes.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 26FB056725F6CB6000A40B26 /* Fakes.framework */; };
26FE09DD24D9F3F600B9BDF5 /* LoadingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26FE09DC24D9F3F600B9BDF5 /* LoadingView.swift */; };
26FE09DF24DB871100B9BDF5 /* SurveyViewControllersFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26FE09DE24DB871100B9BDF5 /* SurveyViewControllersFactory.swift */; };
26FE09E124DB8FA000B9BDF5 /* SurveyCoordinatorControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26FE09E024DB8FA000B9BDF5 /* SurveyCoordinatorControllerTests.swift */; };
Expand Down Expand Up @@ -4385,7 +4384,6 @@
26F94E25267A559300DB6CCF /* ProductAddOn.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductAddOn.swift; sourceTree = "<group>"; };
26F94E2D267A96A000DB6CCF /* ProductAddOnViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductAddOnViewModel.swift; sourceTree = "<group>"; };
26F94E33267AA42F00DB6CCF /* ProductAddOnViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductAddOnViewModelTests.swift; sourceTree = "<group>"; };
26FB056725F6CB6000A40B26 /* Fakes.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Fakes.framework; sourceTree = BUILT_PRODUCTS_DIR; };
26FE09DC24D9F3F600B9BDF5 /* LoadingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadingView.swift; sourceTree = "<group>"; };
26FE09DE24DB871100B9BDF5 /* SurveyViewControllersFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SurveyViewControllersFactory.swift; sourceTree = "<group>"; };
26FE09E024DB8FA000B9BDF5 /* SurveyCoordinatorControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SurveyCoordinatorControllerTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -6456,7 +6454,6 @@
buildActionMask = 2147483647;
files = (
3F2B4AEC2DDC319800E5E49C /* XcodeTarget_WooCommerceTests in Frameworks */,
26FB056825F6CB6000A40B26 /* Fakes.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -10413,7 +10410,6 @@
80E6FC6E276325F60086CD67 /* Clibsodium.xcframework */,
3FF314EF26FC784A0012E68E /* XCTest.framework */,
315E14F32698DA24000AD5FF /* PassKit.framework */,
26FB056725F6CB6000A40B26 /* Fakes.framework */,
5744BEB0248FE44C000A6FE2 /* SwiftUI.framework */,
3F1FA84128B60125009E246C /* WidgetKit.framework */,
260837082AA66E4A0004A12B /* UserNotifications.framework */,
Expand Down
4 changes: 0 additions & 4 deletions Yosemite/Yosemite.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
265BCA0024301ACD004E53EE /* ProductCategoryStoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 265BC9FF24301ACD004E53EE /* ProductCategoryStoreTests.swift */; };
2685C121263E064200D9EE97 /* AddOnGroupStoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2685C120263E064200D9EE97 /* AddOnGroupStoreTests.swift */; };
26E5A09225A8A453000DF8F6 /* ProductAttributeTermStoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26E5A09125A8A453000DF8F6 /* ProductAttributeTermStoreTests.swift */; };
26FB056A25F6CB7600A40B26 /* Fakes.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 26FB056925F6CB7600A40B26 /* Fakes.framework */; };
312DB64D268BD22B00AA0EE9 /* AppSettingsStoreTests+CardReaderSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 312DB64C268BD22B00AA0EE9 /* AppSettingsStoreTests+CardReaderSettings.swift */; };
3F2B4ADA2DDC313E00E5E49C /* XcodeTarget_YosemiteTests in Frameworks */ = {isa = PBXBuildFile; productRef = 3F2B4AD92DDC313E00E5E49C /* XcodeTarget_YosemiteTests */; };
3F7E6AC82DDAD3C1008309F0 /* PointOfSalePopularPurchasableItemFetchStrategyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F7E6AC72DDAD3C1008309F0 /* PointOfSalePopularPurchasableItemFetchStrategyTests.swift */; };
Expand Down Expand Up @@ -286,7 +285,6 @@
265BC9FF24301ACD004E53EE /* ProductCategoryStoreTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductCategoryStoreTests.swift; sourceTree = "<group>"; };
2685C120263E064200D9EE97 /* AddOnGroupStoreTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddOnGroupStoreTests.swift; sourceTree = "<group>"; };
26E5A09125A8A453000DF8F6 /* ProductAttributeTermStoreTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductAttributeTermStoreTests.swift; sourceTree = "<group>"; };
26FB056925F6CB7600A40B26 /* Fakes.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Fakes.framework; sourceTree = BUILT_PRODUCTS_DIR; };
312DB64C268BD22B00AA0EE9 /* AppSettingsStoreTests+CardReaderSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppSettingsStoreTests+CardReaderSettings.swift"; sourceTree = "<group>"; };
3F7E6AC72DDAD3C1008309F0 /* PointOfSalePopularPurchasableItemFetchStrategyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PointOfSalePopularPurchasableItemFetchStrategyTests.swift; sourceTree = "<group>"; };
3FAB48822E02D29700511E19 /* POSEligibilityServiceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = POSEligibilityServiceTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -386,7 +384,6 @@
buildActionMask = 2147483647;
files = (
3F2B4ADA2DDC313E00E5E49C /* XcodeTarget_YosemiteTests in Frameworks */,
26FB056A25F6CB7600A40B26 /* Fakes.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -695,7 +692,6 @@
B5C4EB7720D17329002AC0AF /* Frameworks */ = {
isa = PBXGroup;
children = (
26FB056925F6CB7600A40B26 /* Fakes.framework */,
);
name = Frameworks;
sourceTree = "<group>";
Expand Down
12 changes: 6 additions & 6 deletions docs/fakeable.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Instantiating models for tests is not a simple task. Sometimes, the initializer has too many attributes and the test does not rely on the full set of attributes.
This inconvenience hurts our ability to efficiently unit test our app, which could discourage members from writing them at all.

To help with this, we have introduced a new framework called `Fakes.framework`. This framework defines `.fake()` functions for all of our networking models.
To help with this, we have introduced a module called Fakes at `Modules/Sources/Fakes`. This module defines `.fake()` functions for all of our networking models.
The `.fake()` function instantiates a type with fake values. As of now, we are [defining](https://github.com/woocommerce/woocommerce-ios/blob/trunk/Fakes/Fakes/Fake.swift) fake values as empty values.

This, in conjunction with the [copiable pattern](https://github.com/woocommerce/woocommerce-ios/blob/trunk/docs/copiable.md) allow us to write tests like:
Expand All @@ -23,7 +23,7 @@ func test() {
}
```

**Note: This framework is meant to be used in test targets only!**
**Note: This module is meant to be used in test targets only!**


## Generating Fake Methods
Expand Down Expand Up @@ -70,10 +70,10 @@ It uses a single template, [`Fakes.swifttemplate`](../CodeGeneration/Sourcery/Fa

Please refer to the [Sourcery reference](https://cdn.rawgit.com/krzysztofzablocki/Sourcery/master/docs/index.html) for more info about how to write templates.

## Adding Fakeable to a New Xcode Framework
## Adding `Fakeable` to a new module

1. In Xcode target settings, add Codegen to the Xcode framework in General > Frameworks and Libraries.
2. Add a new file `{{FrameworkName}}-Fakes.yaml` under [`CodeGeneration/Sourcery/Fakes`](../CodeGeneration/Sourcery/Fakes) similar to other yaml files in the same folder.
1. Add `Codegen` to the `dependencies` in the module target definition in `Modules/Package.swift`
2. Add a new file `{{ModuleName}}-Fakes.yaml` under [`CodeGeneration/Sourcery/Fakes`](../CodeGeneration/Sourcery/Fakes) similar to other yaml files in the same folder.
3. In [`Fakes.swifttemplate`](../CodeGeneration/Sourcery/Fakes/Fakes.swifttemplate), add a line to import the new framework.
4. In [`Rakefile`](../Rakefile) which includes the script for `rake generate` command, add the new framework to the list of frameworks for Fakeable generation similar to other frameworks.
4. In [`Rakefile`](../Rakefile) which includes the script for `rake generate` command, add the new framework to the list of frameworks for `Fakeable` generation similar to other frameworks.
5. Now you can try generating fake methods as instructed in an earlier section.