Skip to content

Commit

Permalink
Paywalls: add support for generating snapshots on CI (#3055)
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto committed Sep 6, 2023
1 parent b23f1dd commit a6086ea
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 24 deletions.
36 changes: 35 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ parameters:
generate_snapshots:
default: false
type: boolean
generate_revenuecatui_snapshots:
default: false
type: boolean

aliases:
base-job: &base-job
Expand All @@ -23,7 +26,8 @@ aliases:
xcode_version:
type: string
environment:
CIRCLECI_TESTS_GENERATE_SNAPSHOTS: << pipeline.parameters.generate_snapshots >>
CIRCLECI_TESTS_GENERATE_SNAPSHOTS: << pipeline.parameters.generate_snapshots >>
CIRCLECI_TESTS_GENERATE_REVENUECAT_UI_SNAPSHOTS: << pipeline.parameters.generate_revenuecatui_snapshots >>
working_directory: ~/purchases-ios
shell: /bin/bash --login -o pipefail
release-branches: &release-branches
Expand Down Expand Up @@ -335,6 +339,12 @@ jobs:
no_output_timeout: 5m
environment:
SCAN_DEVICE: iPhone 13,OS=15.5
- when:
condition: << pipeline.parameters.generate_revenuecatui_snapshots >>
steps:
- run:
name: Run create_snapshot_pr
command: bundle exec fastlane create_snapshot_pr version:"revenuecatui-15"
- compress_result_bundle:
directory: fastlane/test_output
bundle_name: revenuecatui
Expand All @@ -359,6 +369,12 @@ jobs:
no_output_timeout: 5m
environment:
SCAN_DEVICE: iPhone 14,OS=16.4
- when:
condition: << pipeline.parameters.generate_revenuecatui_snapshots >>
steps:
- run:
name: Run create_snapshot_pr
command: bundle exec fastlane create_snapshot_pr version:"revenuecatui-16"
- compress_result_bundle:
directory: fastlane/test_output
bundle_name: revenuecatui
Expand All @@ -382,6 +398,12 @@ jobs:
no_output_timeout: 15m
environment:
SCAN_DEVICE: iPhone 14 Pro,OS=17.0
- when:
condition: << pipeline.parameters.generate_revenuecatui_snapshots >>
steps:
- run:
name: Run create_snapshot_pr
command: bundle exec fastlane create_snapshot_pr version:"revenuecatui-17"
- compress_result_bundle:
directory: fastlane/test_output
bundle_name: revenuecatui
Expand Down Expand Up @@ -944,12 +966,24 @@ workflows:
xcode_version: '14.2.0'
- run-test-ios-12:
xcode_version: '14.2.0'

generate_revenuecatui_snapshots:
when: << pipeline.parameters.generate_revenuecatui_snapshots >>
jobs:
- spm-revenuecat-ui-ios-15:
xcode_version: '14.3.0'
- spm-revenuecat-ui-ios-16:
xcode_version: '14.3.0'
- spm-revenuecat-ui-ios-17:
xcode_version: '15.0.0'

build-test:
when:
and:
- not:
equal: [ scheduled_pipeline, << pipeline.trigger_source >> ]
- not: << pipeline.parameters.generate_snapshots >>
- not: << pipeline.parameters.generate_revenuecatui_snapshots >>
jobs:
- lint:
xcode_version: '14.3.0'
Expand Down
15 changes: 13 additions & 2 deletions .swiftpm/xcode/xcshareddata/xcschemes/RevenueCatUI.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,19 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
shouldUseLaunchSchemeArgsEnv = "YES">
<TestPlans>
<TestPlanReference
reference = "container:Tests/RevenueCatUITests/TestPlans/RevenueCatUI.xctestplan"
default = "YES">
</TestPlanReference>
<TestPlanReference
reference = "container:Tests/RevenueCatUITests/TestPlans/CI-Snapshots.xctestplan">
</TestPlanReference>
<TestPlanReference
reference = "container:Tests/RevenueCatUITests/TestPlans/CI-RevenueCatUI.xctestplan">
</TestPlanReference>
</TestPlans>
<Testables>
<TestableReference
skipped = "NO">
Expand Down
2 changes: 1 addition & 1 deletion Tests/RevenueCatUITests/Helpers/TestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private enum SnapshotTests {
static func updateSnapshotsIfNeeded() {
guard !Self.environmentVariableChecked else { return }

if ProcessInfo.processInfo.environment["CIRCLECI_TESTS_GENERATE_SNAPSHOTS"] == "1" {
if ProcessInfo.processInfo.environment["CIRCLECI_TESTS_GENERATE_REVENUECAT_UI_SNAPSHOTS"] == "1" {
isRecording = true
}
}
Expand Down
28 changes: 28 additions & 0 deletions Tests/RevenueCatUITests/TestPlans/CI-RevenueCatUI.xctestplan
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"configurations" : [
{
"id" : "9A1CEA19-F9E9-4C9C-9FC8-6F9A5B29F2AE",
"name" : "Test Scheme Action",
"options" : {

}
}
],
"defaultOptions" : {
"codeCoverage" : false,
"maximumTestExecutionTimeAllowance" : 180,
"maximumTestRepetitions" : 3,
"testRepetitionMode" : "retryOnFailure",
"testTimeoutsEnabled" : true
},
"testTargets" : [
{
"target" : {
"containerPath" : "container:",
"identifier" : "RevenueCatUITests",
"name" : "RevenueCatUITests"
}
}
],
"version" : 1
}
33 changes: 33 additions & 0 deletions Tests/RevenueCatUITests/TestPlans/CI-Snapshots.xctestplan
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"configurations" : [
{
"id" : "9A1CEA19-F9E9-4C9C-9FC8-6F9A5B29F2AE",
"name" : "Test Scheme Action",
"options" : {

}
}
],
"defaultOptions" : {
"codeCoverage" : false,
"environmentVariableEntries" : [
{
"key" : "CIRCLECI_TESTS_GENERATE_REVENUECAT_UI_SNAPSHOTS",
"value" : "1"
}
],
"maximumTestExecutionTimeAllowance" : 180,
"testExecutionOrdering" : "random",
"testTimeoutsEnabled" : true
},
"testTargets" : [
{
"target" : {
"containerPath" : "container:",
"identifier" : "RevenueCatUITests",
"name" : "RevenueCatUITests"
}
}
],
"version" : 1
}
26 changes: 26 additions & 0 deletions Tests/RevenueCatUITests/TestPlans/RevenueCatUI.xctestplan
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"configurations" : [
{
"id" : "9A1CEA19-F9E9-4C9C-9FC8-6F9A5B29F2AE",
"name" : "Test Scheme Action",
"options" : {

}
}
],
"defaultOptions" : {
"codeCoverage" : false,
"maximumTestExecutionTimeAllowance" : 180,
"testTimeoutsEnabled" : true
},
"testTargets" : [
{
"target" : {
"containerPath" : "container:",
"identifier" : "RevenueCatUITests",
"name" : "RevenueCatUITests"
}
}
],
"version" : 1
}
54 changes: 37 additions & 17 deletions fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,26 @@ platform :ios do

desc "Runs all RevenueCatUI tests"
lane :test_revenuecatui do |options|
# See https://forums.swift.org/t/i-made-a-little-spm-package-that-uses-uikit-how-can-i-run-tests-from-command-line/50399/2
xcodebuild(
workspace: '.',
scheme: 'RevenueCatUI',
destination: "platform=iOS Simulator,name=" + (ENV['SCAN_DEVICE'] || "iPhone 14,OS=16.4"),
sdk: 'iphonesimulator',
output_style: :basic,
result_bundle_path: 'fastlane/test_output/revenuecatui.xcresult',
report_formats: [:junit],
report_path: 'fastlane/test_output/revenuecatui/tests.xml',
test: true,
xcargs: "-test-iterations 3 -retry-tests-on-failure"
)
generate_snapshots = ENV["CIRCLECI_TESTS_GENERATE_REVENUECAT_UI_SNAPSHOTS"] == "true"

begin
# See https://forums.swift.org/t/i-made-a-little-spm-package-that-uses-uikit-how-can-i-run-tests-from-command-line/50399/2
xcodebuild(
workspace: '.',
scheme: 'RevenueCatUI',
destination: "platform=iOS Simulator,name=" + (ENV['SCAN_DEVICE'] || "iPhone 14,OS=16.4"),
sdk: 'iphonesimulator',
output_style: :basic,
result_bundle_path: 'fastlane/test_output/revenuecatui.xcresult',
report_formats: [:junit],
report_path: 'fastlane/test_output/revenuecatui/tests.xml',
test: true,
xcargs: generate_snapshots ? "-testPlan CI-Snapshots" : "-testPlan CI-RevenueCatUI"
)
rescue => e
# Equivalent to `fail_build: !generate_snapshots`
raise e if !generate_snapshots
end
end

desc "Release checks"
Expand Down Expand Up @@ -388,8 +395,21 @@ platform :ios do
end
end

desc "Trigger CircleCI job to generate snapshots"
lane :generate_snapshots do
desc "Trigger CircleCI job to generate snapshots for RevenueCat"
lane :generate_snapshots_RC do
generate_snapshots(
pipeline: "generate_snapshots"
)
end

desc "Trigger CircleCI job to generate snapshots for RevenueCatUI"
lane :generate_snapshots_RCUI do
generate_snapshots(
pipeline: "generate_revenuecatui_snapshots"
)
end

private_lane :generate_snapshots do |options|
require 'rest-client'

# Prompt branch
Expand All @@ -403,7 +423,7 @@ platform :ios do

# Make request
headers = {"Circle-Token": circle_token, "Content-Type": "application/json", "Accept": "application/json"}
data = {parameters: {generate_snapshots: true}, branch: branch}
data = {parameters: {options[:pipeline] => true}, branch: branch}
url = "https://circleci.com/api/v2/project/github/RevenueCat/purchases-ios/pipeline"

resp = RestClient.post url, data.to_json, headers
Expand All @@ -425,7 +445,7 @@ platform :ios do
branch_name = "generated_snapshots/#{base_branch}-#{build_number}-#{version}"
sh("git", "checkout", "-b", branch_name)

sh("git", "add", "../Tests")
sh("git", "add", "../*/__Snapshots__/*")
file_count = sh("git diff --cached --numstat | wc -l").strip.to_i

if file_count == 0
Expand Down
14 changes: 11 additions & 3 deletions fastlane/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,21 @@ Release to CocoaPods

Tag current branch with current version number

### ios generate_snapshots
### ios generate_snapshots_RC

```sh
[bundle exec] fastlane ios generate_snapshots
[bundle exec] fastlane ios generate_snapshots_RC
```

Trigger CircleCI job to generate snapshots
Trigger CircleCI job to generate snapshots for RevenueCat

### ios generate_snapshots_RCUI

```sh
[bundle exec] fastlane ios generate_snapshots_RCUI
```

Trigger CircleCI job to generate snapshots for RevenueCatUI

### ios create_snapshot_pr

Expand Down

0 comments on commit a6086ea

Please sign in to comment.