Skip to content

Commit

Permalink
fix: pass CI env explicitly in fastfile and testplans - WPB-14950 (#2278
Browse files Browse the repository at this point in the history
)
  • Loading branch information
netbe authored Dec 16, 2024
1 parent 0b030d1 commit aea89da
Show file tree
Hide file tree
Showing 30 changed files with 300 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AccountsAPIV7: AccountsAPIV6 {

let encodedJSON: Data
do {
encodedJSON = try JSONEncoder().encode(body)
encodedJSON = try JSONEncoder.defaultEncoder.encode(body)
} catch {
assertionFailure("failed to encode body")
throw AccountsAPIError.invalidRequestBody
Expand Down
11 changes: 11 additions & 0 deletions WireAPI/Tests/TestPlans/AllTests.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@
}
],
"defaultOptions" : {
"environmentVariableEntries" : [
{
"key" : "CI",
"value" : "${CI}"
}
],
"language" : "en",
"region" : "DE",
"targetForVariableExpansion" : {
"containerPath" : "container:WireAPI",
"identifier" : "WireAPITests",
"name" : "WireAPITests"
},
"testExecutionOrdering" : "random"
},
"testTargets" : [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
curl \
--request POST \
--header "Content-Type: application/json" \
--data "{\"name\":\"iOS Team\",\"icon\":\"default\"}" \
--data "{\"icon\":\"default\",\"name\":\"iOS Team\"}" \
"upgrade-personal-to-team"
18 changes: 10 additions & 8 deletions WireAPI/Tests/WireAPITests/Helpers/HTTPRequestSnapshotHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ import struct WireAPI.HTTPRequest
/// Provides convenience to snapshot `HTTPRequest` objects.
struct HTTPRequestSnapshotHelper {

private var defaultRecordMode: SnapshotTestingConfiguration.Record? {
let ci = ProcessInfo.processInfo.environment["CI"]
return (ci == nil || ci?.isEmpty == true) ? .missing : .never
}

/// Snapshot test a given request
/// - Parameters:
/// - request: httpRequest to verify
Expand All @@ -40,9 +45,7 @@ struct HTTPRequestSnapshotHelper {
function: String = #function,
line: UInt = #line
) {
let recordEnabled: SnapshotTestingConfiguration.Record? = ProcessInfo.processInfo
.environment["CI"] == "true" ? .never : nil
withSnapshotTesting(record: recordEnabled) {
withSnapshotTesting(record: defaultRecordMode) {
let errorMessage = verifySnapshot(
of: request,
as: .dump,
Expand All @@ -62,7 +65,8 @@ struct HTTPRequestSnapshotHelper {
/// - Parameters:
/// - request: url request to verify
/// - resourceName: name of the file containing the expected request description
/// - record: if true, a new snapshot will be recorded, overwriting an existing snapshot.
/// - record: if true, a new snapshot will be recorded, overwriting an existing snapshot. If false it record only
/// if missing. If nil, it fallbacks to defaultRecordMode
/// - file: The file invoking the test.
/// - function: The method invoking the test.
/// - line: The line invoking the test.
Expand All @@ -71,14 +75,12 @@ struct HTTPRequestSnapshotHelper {
func verifyRequest(
request: URLRequest,
resourceName: String? = nil,
record: Bool = false,
record: Bool? = nil,
file: StaticString = #filePath,
function: String = #function,
line: UInt = #line
) {
let recordEnabled: SnapshotTestingConfiguration.Record? = ProcessInfo.processInfo
.environment["CI"] == "true" ? .never : nil
withSnapshotTesting(record: recordEnabled) {
withSnapshotTesting(record: defaultRecordMode) {
let errorMessage = verifySnapshot(
of: request,
as: .curl,
Expand Down
11 changes: 11 additions & 0 deletions WireAnalytics/Tests/TestPlans/AllTests.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@
}
],
"defaultOptions" : {
"environmentVariableEntries" : [
{
"key" : "CI",
"value" : "${CI}"
}
],
"language" : "en",
"region" : "DE",
"targetForVariableExpansion" : {
"containerPath" : "container:WireAnalytics",
"identifier" : "WireAnalyticsTests",
"name" : "WireAnalyticsTests"
},
"testExecutionOrdering" : "random"
},
"testTargets" : [
Expand Down
16 changes: 16 additions & 0 deletions WireDomain/Project/TestPlans/AllTests.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,24 @@
}
],
"defaultOptions" : {
"commandLineArgumentEntries" : [
{
"argument" : "-com.apple.CoreData.ConcurrencyDebug 1"
}
],
"environmentVariableEntries" : [
{
"key" : "CI",
"value" : "${CI}"
}
],
"language" : "en",
"region" : "DE",
"targetForVariableExpansion" : {
"containerPath" : "container:WireDomain Project.xcodeproj",
"identifier" : "01D0DCAF2C1C8C880076CB1C",
"name" : "WireDomainTests"
},
"testExecutionOrdering" : "random"
},
"testTargets" : [
Expand Down
11 changes: 11 additions & 0 deletions WireDomain/Tests/TestPlans/AllTests.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@
"argument" : "-com.apple.CoreData.ConcurrencyDebug 1"
}
],
"environmentVariableEntries" : [
{
"key" : "CI",
"value" : "${CI}"
}
],
"language" : "en",
"region" : "DE",
"targetForVariableExpansion" : {
"containerPath" : "container:WireDomain",
"identifier" : "WireDomainPkgTests",
"name" : "WireDomainPkgTests"
},
"testExecutionOrdering" : "random"
},
"testTargets" : [
Expand Down
29 changes: 11 additions & 18 deletions WireFoundation/Sources/WireTesting/Utilities/SnapshotHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public struct SnapshotHelper {
/// If empty, the `SNAPSHOT_REFERENCE_DIR` environment variable is read.
private var snapshotReferenceDirectory = ""

private var defaultRecordMode: SnapshotTestingConfiguration.Record? {
let ci = ProcessInfo.processInfo.environment["CI"]
return (ci == nil || ci?.isEmpty == true) ? .missing : .never
}

public init() {}

// MARK: - Create variations
Expand Down Expand Up @@ -143,9 +148,7 @@ public struct SnapshotHelper {

let snapshotDirectory = snapshotDirectory(file: file)
setArtifactsDirectoryIfNeeded(basedOn: snapshotDirectory)
let recordEnabled: SnapshotTestingConfiguration.Record? = ProcessInfo.processInfo
.environment["CI"] == "true" ? .never : nil
withSnapshotTesting(record: recordEnabled) {
withSnapshotTesting(record: defaultRecordMode) {
let failure = verifySnapshot(
of: value,
as: .image(
Expand Down Expand Up @@ -190,9 +193,7 @@ public struct SnapshotHelper {
setArtifactsDirectoryIfNeeded(basedOn: snapshotDirectory)
let config = size.map { ViewImageConfig(safeArea: safeArea, size: $0, traits: traits) }

let recordEnabled: SnapshotTestingConfiguration.Record? = ProcessInfo.processInfo
.environment["CI"] == "true" ? .never : nil
withSnapshotTesting(record: recordEnabled) {
withSnapshotTesting(record: defaultRecordMode) {
let failure = verifySnapshot(
of: value,
as: config.map { .image(on: $0, perceptualPrecision: perceptualPrecision, traits: traits) } ?? .image(
Expand Down Expand Up @@ -229,9 +230,7 @@ public struct SnapshotHelper {
) {
let snapshotDirectory = snapshotDirectory(file: file)
setArtifactsDirectoryIfNeeded(basedOn: snapshotDirectory)
let recordEnabled: SnapshotTestingConfiguration.Record? = ProcessInfo.processInfo
.environment["CI"] == "true" ? .never : nil
withSnapshotTesting(record: recordEnabled) {
withSnapshotTesting(record: defaultRecordMode) {
let failure = verifySnapshot(
of: value,
as: .image(perceptualPrecision: perceptualPrecision, traits: traits),
Expand Down Expand Up @@ -312,9 +311,7 @@ public struct SnapshotHelper {
) {
let snapshotDirectory = snapshotDirectory(file: file)
setArtifactsDirectoryIfNeeded(basedOn: snapshotDirectory)
let recordEnabled: SnapshotTestingConfiguration.Record? = ProcessInfo.processInfo
.environment["CI"] == "true" ? .never : nil
withSnapshotTesting(record: recordEnabled) {
withSnapshotTesting(record: defaultRecordMode) {
for (config, name) in SnapshotHelper.phoneConfigs {

let failure = verifySnapshot(
Expand Down Expand Up @@ -352,9 +349,7 @@ public struct SnapshotHelper {
) {
let snapshotDirectory = snapshotDirectory(file: file)
setArtifactsDirectoryIfNeeded(basedOn: snapshotDirectory)
let recordEnabled: SnapshotTestingConfiguration.Record? = ProcessInfo.processInfo
.environment["CI"] == "true" ? .never : nil
withSnapshotTesting(record: recordEnabled) {
withSnapshotTesting(record: defaultRecordMode) {
let failure = verifySnapshot(
of: value,
as: .image,
Expand Down Expand Up @@ -385,9 +380,7 @@ public struct SnapshotHelper {
testName: String = #function,
line: UInt = #line
) {
let recordEnabled: SnapshotTestingConfiguration.Record? = ProcessInfo.processInfo
.environment["CI"] == "true" ? .never : nil
withSnapshotTesting(record: recordEnabled) {
withSnapshotTesting(record: defaultRecordMode) {

let snapshotDirectory = snapshotDirectory(file: file)
setArtifactsDirectoryIfNeeded(basedOn: snapshotDirectory)
Expand Down
20 changes: 12 additions & 8 deletions WireFoundation/Tests/TestPlans/AllTests.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@
}
],
"defaultOptions" : {
"environmentVariableEntries" : [
{
"key" : "CI",
"value" : "${CI}"
}
],
"language" : "en",
"region" : "DE",
"targetForVariableExpansion" : {
"containerPath" : "container:WireFoundation",
"identifier" : "WireFoundationTests",
"name" : "WireFoundationTests"
},
"testExecutionOrdering" : "random"
},
"testTargets" : [
{
"target" : {
"containerPath" : "container:WireFoundation",
"identifier" : "WireLoggingTests",
"name" : "WireLoggingTests"
}
},
{
"target" : {
"containerPath" : "container:WireFoundation",
"containerPath" : "container:",
"identifier" : "WireFoundationTests",
"name" : "WireFoundationTests"
}
Expand Down
13 changes: 12 additions & 1 deletion WireLogging/Tests/TestPlans/AllTests.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@
],
"defaultOptions" : {
"codeCoverage" : false,
"language" : "en"
"environmentVariableEntries" : [
{
"key" : "CI",
"value" : "${CI}"
}
],
"language" : "en",
"targetForVariableExpansion" : {
"containerPath" : "container:WireLogging",
"identifier" : "WireLoggingTests",
"name" : "WireLoggingTests"
}
},
"testTargets" : [
{
Expand Down
18 changes: 11 additions & 7 deletions WireUI/Tests/TestPlans/AllTests.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@
"argument" : "-NSShowNonLocalizedStrings YES"
}
],
"environmentVariableEntries" : [
{
"key" : "CI",
"value" : "${CI}"
}
],
"language" : "en",
"region" : "DE",
"targetForVariableExpansion" : {
"containerPath" : "container:WireUI",
"identifier" : "WireDesignTests",
"name" : "WireDesignTests"
},
"testExecutionOrdering" : "random"
},
"testTargets" : [
Expand Down Expand Up @@ -81,13 +92,6 @@
"identifier" : "WireReusableUIComponentsTests",
"name" : "WireReusableUIComponentsTests"
}
},
{
"target" : {
"containerPath" : "container:WireUI",
"identifier" : "WireIndividualToTeamMigrationUITests",
"name" : "WireIndividualToTeamMigrationUITests"
}
}
],
"version" : 1
Expand Down
14 changes: 11 additions & 3 deletions fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ platform :ios do

before_all do |lane|
ensure_xcode_version
create_keychain_if_needed unless [:prepare_for_tests, :select_frameworks, :generate_framework_list, :test_frameworks].include?(lane)
create_keychain_if_needed unless [:prepare_for_tests, :select_framework, :select_frameworks, :generate_framework_list, :test_frameworks].include?(lane)
`mkdir -p ./.post_build`
Dir.chdir("..") do
encoded_key = ENV['APPSTORE_API_KEY_BASE64']
Expand Down Expand Up @@ -81,9 +81,14 @@ platform :ios do
desc "Test one framework only"
lane :select_framework do |options|
frameworks = Framework.all.values
framework_options = frameworks.map { |f| f.scheme }

selected_choice = UI.select("Select a framework for testing:", framework_options)
if (!options[:scheme].nil?)
selected_choice = options[:scheme]
else
framework_options = frameworks.map { |f| f.scheme }
selected_choice = UI.select("Select a framework for testing:", framework_options)
end

framework = frameworks.find { |framework| framework.scheme == selected_choice }

run_tests_frameworks([framework.scheme], options)
Expand Down Expand Up @@ -512,6 +517,8 @@ platform :ios do
raise e # Stops execution immediately if the build fails
end

ci_args = is_running_on_ci ? "CI=1" : nil

begin
UI.message "🧪 Running tests for scheme: #{scheme}"
run_tests(
Expand All @@ -532,6 +539,7 @@ platform :ios do
skip_package_dependencies_resolution: true,
number_of_retries: 2,
test_without_building: true,
xcargs: ci_args
)
rescue => e
puts "⚠️ Tests failed for scheme: #{scheme}, but continuing..."
Expand Down
11 changes: 11 additions & 0 deletions scripts/AllTests.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@
}
],
"defaultOptions" : {
"environmentVariableEntries" : [
{
"key" : "CI",
"value" : "${CI}"
}
],
"language" : "en",
"region" : "DE",
"targetForVariableExpansion" : {
"containerPath" : "container:",
"identifier" : "TrimStringCatalogsTests",
"name" : "TrimStringCatalogsTests"
},
"testExecutionOrdering" : "random"
},
"testTargets" : [
Expand Down
11 changes: 11 additions & 0 deletions wire-ios-canvas/WireCanvasTests/TestPlans/AllTests.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@
}
],
"defaultOptions" : {
"environmentVariableEntries" : [
{
"key" : "CI",
"value" : "${CI}"
}
],
"targetForVariableExpansion" : {
"containerPath" : "container:WireCanvas.xcodeproj",
"identifier" : "01E9445F2D01805B006921E4",
"name" : "WireCanvasTests"
},
"testTimeoutsEnabled" : true
},
"testTargets" : [
Expand Down
Loading

0 comments on commit aea89da

Please sign in to comment.