Skip to content

Conversation

fedemkr
Copy link
Member

@fedemkr fedemkr commented Jul 7, 2025

🎟️ Tracking

PM-23496

📔 Objective

Add Sourcery and configure it to be able to automatically generate mocks.
This library is powered by Stencil and we have one that acts as a template for the generated mocks: AutoMockable.stencil.

In terms of organization I've split what we use globally as the stencils/templates in root -> Sourcery and then specific targets configurations in each target. For the time being I've only configured BitwardenShared and AuthenticatorShared which can be seen in BitwardenShared -> Sourcery -> sourcery.yml or alike for BWA.
Additionally, the generated mocks for BitwardenShared will be located in BitwardenShared -> Sourcery -> Generated folder in a single file called AutoMockable.generated.swift.

Usage

Add // sourcery: AutoMockable comment to any protocol so the mock is automatically generated.
Contributing docs PR: bitwarden/contributing-docs#631

⏰ Reminders before review

  • Contributor guidelines followed
  • All formatters and local linters executed and passed
  • Written new unit and / or integration tests where applicable
  • Protected functional changes with optionality (feature flags)
  • Used internationalization (i18n) for all UI strings
  • CI builds passed
  • Communicated to DevOps any deployment requirements
  • Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team

🦮 Reviewer guidelines

  • 👍 (:+1:) or similar for great changes
  • 📝 (:memo:) or ℹ️ (:information_source:) for notes or general info
  • ❓ (:question:) for questions
  • 🤔 (:thinking:) or 💭 (:thought_balloon:) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion
  • 🎨 (:art:) for suggestions / improvements
  • ❌ (:x:) or ⚠️ (:warning:) for more significant problems or concerns needing attention
  • 🌱 (:seedling:) or ♻️ (:recycle:) for future improvements or indications of technical debt
  • ⛏ (:pick:) for minor or nitpick changes

@fedemkr fedemkr requested review from a team and matt-livefront as code owners July 7, 2025 17:19
Copy link
Contributor

github-actions bot commented Jul 7, 2025

Logo
Checkmarx One – Scan Summary & Details7352ad9f-72cf-4b17-8560-62cbad8f1581

Great job, no security vulnerabilities found in this Pull Request

Copy link

codecov bot commented Jul 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.76%. Comparing base (fe7b68f) to head (e0eff3a).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1724      +/-   ##
==========================================
- Coverage   89.39%   85.76%   -3.64%     
==========================================
  Files         798      996     +198     
  Lines       50600    60485    +9885     
==========================================
+ Hits        45236    51875    +6639     
- Misses       5364     8610    +3246     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Collaborator

@matt-livefront matt-livefront left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran into an issue where I needed to bootstrap after the mock file was initially created. I think this is a case where we'll want to specify the generated file in the sources for Xcodegen and mark it as optional. It looks like you did this for the Generated directory, but I'm not sure if that will apply for anything that's created in that directory after the fact. We may also need to specify it as an output file in the Sourcery build phase to get Xcode to correctly pick it up when it is initially generated. And we may want to add basedOnDependencyAnalysis: false.

diff --git a/project-pm.yml b/project-pm.yml
index e92140496..af86a9abc 100644
--- a/project-pm.yml
+++ b/project-pm.yml
@@ -398,6 +398,9 @@ targets:
             PATH="/opt/homebrew/bin:$PATH"
           fi
           mint run sourcery --config BitwardenShared/Sourcery/sourcery.yml
+        basedOnDependencyAnalysis: false
+        outputFiles:
+          - $(SRCROOT)/BitwardenShared/Sourcery/Generated/AutoMockable.generated.swift
   BitwardenSharedTests:
     type: bundle.unit-test
     platform: iOS
@@ -418,6 +421,8 @@ targets:
       - path: GlobalTestHelpers
       - path: BitwardenShared/Sourcery/Generated
         optional: true
+      - path: BitwardenShared/Sourcery/Generated/AutoMockable.generated.swift
+        optional: true
     dependencies:
       - target: Bitwarden
       - target: BitwardenShared

Finally, in terms of CI I had to explicitly call Sourcery generation before bootstrapping in the test.yml workflow so the generated mocks are available to be used in the tests. Moreover, had to move installing Homebrew dependencies earlier so Mint is available for Sourcery.

If you make the changes above, I'd be curious if that also fixes CI without needing to make the additional workflow changes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is showing up in the app's bundle. I think you can prevent this by removing it from a build phase (by specifying it's path and setting buildPhase: none similar to swiftgen-pm.yml ) or by moving it to the root of the repo.
Screenshot 2025-07-07 at 2 01 47 PM

ios/project-pm.yml

Lines 165 to 166 in 5ab1cf6

- path: swiftgen-pm.yml
buildPhase: none

fedemkr added 3 commits July 7, 2025 16:57
…e workflow so it's available for Sourcery."

This reverts commit 5ab1cf6.
…clude generate mock files. Somehow they're not automatically included when building in the CI on the test run."

This reverts commit 523a500.
@fedemkr fedemkr requested a review from matt-livefront July 7, 2025 19:58
@fedemkr fedemkr merged commit bbb93bd into main Jul 8, 2025
12 checks passed
@fedemkr fedemkr deleted the PM-23496/add-sourcery branch July 8, 2025 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants