Skip to content
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
89 changes: 89 additions & 0 deletions .buildkite/app-store-screenshots.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/buildkite/pipeline-schema/main/schema.json
---

# Takes the Jetpack App Store screenshots: one job per device and language, then one that composes
# them. Start it with `bundle exec fastlane trigger_jetpack_screenshots`. See docs/app-store-screenshots.md.

# Below everything else's default of 0, so that its 50-odd jobs only take Mac agents nothing else
# is waiting for, and never hold up a PR or trunk build. (The release pipelines are at 1.)
priority: -1

agents:
queue: mac
env:
# The iPhone Duo's screenshots need an app built with the iOS 27.1 SDK: built with an older one,
# the app runs letterboxed on the Duo's outer display.
IMAGE_ID: xcode-27.1

# The App Store languages the screenshots are taken in: the folders in fastlane/appstoreres/jetpack_metadata.
x-locales: &locales
- "ar-SA"
- "de-DE"
- "en-US"
- "es-ES"
- "fr-FR"
- "he"
- "id"
- "it"
- "ja"
- "ko"
- "nl-NL"
- "pt-BR"
- "ru"
- "sv"
- "tr"
- "zh-Hans"
- "zh-Hant"

# A step per device, because a matrix can make at most 50 jobs and three devices in 17 languages
# would be 51.
x-screenshots: &screenshots
depends_on: "build_jetpack"
plugins: [$CI_TOOLKIT_PLUGIN]
timeout_in_minutes: 30
artifact_paths:
- "fastlane/jetpack_screenshots/**/*.png"
- "build/results/screenshots/*.log"
retry:
automatic:
- exit_status: "*"
limit: 1

steps:
- label: "🛠 :jetpack: Build for Screenshots"
key: "build_jetpack"
command: ".buildkite/commands/build-for-testing.sh jetpack http_fixtures:true"
plugins: [$CI_TOOLKIT_PLUGIN]

- label: "📸 iPhone 18 Pro Max {{matrix}}"
key: "screenshots_iphone"
command: ".buildkite/commands/take-app-store-screenshots.sh 'iPhone 18 Pro Max' '{{matrix}}'"
matrix: *locales
<<: *screenshots

- label: "📸 iPad Pro 13-inch (M5) {{matrix}}"
key: "screenshots_ipad"
command: ".buildkite/commands/take-app-store-screenshots.sh 'iPad Pro 13-inch (M5)' '{{matrix}}'"
matrix: *locales
<<: *screenshots

- label: "📸 iPhone Duo {{matrix}}"
key: "screenshots_duo"
command: ".buildkite/commands/take-app-store-screenshots.sh 'iPhone Duo' '{{matrix}}'"
matrix: *locales
<<: *screenshots

- label: "🖼️ Compose Screenshots"
command: ".buildkite/commands/compose-app-store-screenshots.sh"
# Compose whatever was taken, so that one failed job doesn't hide all the others' screenshots.
depends_on:
- step: "screenshots_iphone"
allow_failure: true
- step: "screenshots_ipad"
allow_failure: true
- step: "screenshots_duo"
allow_failure: true
plugins: [$CI_TOOLKIT_PLUGIN]
artifact_paths:
- "fastlane/jetpack_promo_screenshots.zip"
- "fastlane/jetpack_screenshots_overview/*.png"
16 changes: 16 additions & 0 deletions .buildkite/commands/compose-app-store-screenshots.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash -eu

# Composes the App Store screenshots from the raw ones every screenshot job uploaded.

echo "--- 📦 Downloading the raw screenshots"
buildkite-agent artifact download "fastlane/jetpack_screenshots/**/*.png" .

echo "--- :rubygems: Setting up Gems"
install_gems

echo "--- 🖼️ Composing"
bundle exec fastlane create_jetpack_promo_screenshots

echo "--- 📦 Zipping the screenshots"
# In the folder layout `update_jetpack_metadata_on_app_store_connect` uploads from.
(cd fastlane && zip -rq jetpack_promo_screenshots.zip jetpack_promo_screenshots)
17 changes: 17 additions & 0 deletions .buildkite/commands/take-app-store-screenshots.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash -eu

# Takes the App Store screenshots for one device and one language, from the app the build step built.
# Usage: take-app-store-screenshots.sh DEVICE LOCALE

DEVICE=${1:?Usage $0 DEVICE LOCALE}
LOCALE=${2:?Usage $0 DEVICE LOCALE}

echo "--- 📦 Downloading Build Artifacts"
download_artifact build-products-jetpack.tar
tar -xf build-products-jetpack.tar

echo "--- :rubygems: Setting up Gems"
install_gems

echo "--- 📸 Taking the screenshots: $DEVICE, $LOCALE"
bundle exec fastlane jetpack_screenshots language:"$LOCALE" device:"$DEVICE" skip_build:true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ fastlane/screenshots
fastlane/jetpack_screenshots
fastlane/promo_screenshots
fastlane/jetpack_promo_screenshots
fastlane/jetpack_screenshots_overview
fastlane/metadata/review_information
default.profraw
derived-data/
Expand Down
58 changes: 55 additions & 3 deletions Modules/Sources/HTTPFixtures/FixtureSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,60 @@ import os
/// directory. See `StubMapping` and `ResponseTemplate` for the parts of the format that are
/// supported.
///
/// A response's `bodyFileName` names a file under the `__files` directory, beside `mappings`,
/// which is answered with as it is. That's how images are served.
///
/// Mappings match on the path, query and body of a request, never its host, so one set answers
/// for every host the app talks to.
public final class FixtureSet: Sendable {
/// The mappings, in the order they're tried.
private let mappings: [StubMapping]
/// The contents of the files responses name in `bodyFileName`, keyed by that name.
private let bodyFiles: [String: Data]
private let scenarioStates = OSAllocatedUnfairLock<[String: String]>(initialState: [:])
private let now: @Sendable () -> Date

/// - Parameters:
/// - directory: The directory that contains the `mappings` directory.
/// - overlay: Another directory with a `mappings` directory, whose files replace the ones at
/// the same paths under `directory`. The App Store screenshots use one to show the fixtures'
/// content translated, without changing the content the UI tests assert on.
/// - now: The clock `{{now}}` template expressions read.
public init(directory: URL, now: @escaping @Sendable () -> Date = { Date() }) throws(FixtureError) {
public init(
directory: URL,
overlay: URL? = nil,
now: @escaping @Sendable () -> Date = { Date() }
) throws(FixtureError) {
let mappingsDirectory = directory.appending(path: "mappings", directoryHint: .isDirectory)
let files = Self.jsonFiles(in: mappingsDirectory)
var files = Self.jsonFiles(in: mappingsDirectory)
if let overlay {
let replacements = Self.jsonFiles(in: overlay.appending(path: "mappings", directoryHint: .isDirectory))
let replaced = Set(replacements.map(\.path))
files = (files.filter { !replaced.contains($0.path) } + replacements).sorted { $0.path < $1.path }
}
guard !files.isEmpty else {
throw FixtureError(file: "mappings", reason: "no mappings found in \(mappingsDirectory.path())")
}

var mappings: [StubMapping] = []
var bodyFiles: [String: Data] = [:]
for file in files {
guard let data = try? Data(contentsOf: file.url) else {
throw FixtureError(file: file.path, reason: "the file can't be read")
}

let mapping = try StubMapping(data: data, source: file.path)
try Self.validateTemplates(of: mapping)
if let name = mapping.response.bodyFileName, bodyFiles[name] == nil {
let bodyFile = directory.appending(path: "__files").appending(path: name)
guard let data = try? Data(contentsOf: bodyFile) else {
throw FixtureError(file: file.path, reason: "its bodyFileName, __files/\(name), can't be read")
}
bodyFiles[name] = data
}
mappings.append(mapping)
}
self.bodyFiles = bodyFiles

self.mappings = mappings.sorted {
($0.priority, -$0.specificity, $0.source) < ($1.priority, -$1.specificity, $1.source)
Expand All @@ -64,9 +90,21 @@ public final class FixtureSet: Sendable {

do throws(ResponseTemplate.Failure) {
var template = ResponseTemplate(request: request, now: now())
let headers = try match.response.headers.mapValues { value throws(ResponseTemplate.Failure) in
var headers = try match.response.headers.mapValues { value throws(ResponseTemplate.Failure) in
try template.render(value)
}
if let name = match.response.bodyFileName, let data = bodyFiles[name] {
if !headers.keys.contains(where: { $0.caseInsensitiveCompare("Content-Type") == .orderedSame }) {
headers["Content-Type"] = Self.contentType(ofFileNamed: name)
}
return StubResponse(
status: match.response.status,
headers: headers,
body: data,
delay: match.response.delay,
source: match.source
)
}
let body =
if let series = match.response.series {
series.body(for: request, now: now())
Expand All @@ -85,6 +123,20 @@ public final class FixtureSet: Sendable {
}
}

/// The content type of a body file, from its extension.
private static func contentType(ofFileNamed name: String) -> String {
switch URL(filePath: name).pathExtension.lowercased() {
case "jpg", "jpeg": "image/jpeg"
case "png": "image/png"
case "gif": "image/gif"
case "webp": "image/webp"
case "svg": "image/svg+xml"
case "json": "application/json"
case "html": "text/html"
default: "application/octet-stream"
}
}

/// Renders a mapping's templates against a placeholder request, so an expression that can't be
/// evaluated is reported when the fixtures load instead of when a test happens to request it.
private static func validateTemplates(of mapping: StubMapping) throws(FixtureError) {
Expand Down
8 changes: 8 additions & 0 deletions Modules/Sources/HTTPFixtures/StubMapping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ struct ResponseDefinition: Sendable {
/// Header values may contain template expressions, like the body.
let headers: [String: String]
let body: String
/// A file under the fixtures' `__files` directory to answer with, as is, in place of `body`:
/// an image, say. WireMock's `bodyFileName`.
let bodyFileName: String?
/// Generates the body in place of `body`, for a mapping that names the `stats-series`
/// transformer.
let series: StatsSeries?
Expand Down Expand Up @@ -297,6 +300,10 @@ extension StubMapping {
if jsonBody != nil, let text = JSONText.value(at: ["response", "jsonBody"], in: document) {
body = String(decoding: text, as: UTF8.self)
}
let bodyFileName: String? = try response.take("bodyFileName")
if bodyFileName != nil, !body.isEmpty || jsonBody != nil || series != nil {
throw UnsupportedMapping("a response with a bodyFileName can't also have a body, jsonBody or transformer")
}
let isJSON = jsonBody != nil || series != nil
if isJSON, !headers.keys.contains(where: { $0.caseInsensitiveCompare("Content-Type") == .orderedSame }) {
headers["Content-Type"] = "application/json"
Expand All @@ -307,6 +314,7 @@ extension StubMapping {
status: try response.take("status") ?? 200,
headers: headers,
body: body,
bodyFileName: bodyFileName,
series: series,
delay: TimeInterval(delay) / 1000
)
Expand Down
89 changes: 89 additions & 0 deletions Modules/Tests/HTTPFixturesTests/FixtureSetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,97 @@ struct FixtureSetTests {
#expect(fixtures.response(for: get("/slow")).delay == 1.5)
}

@Test func bodyFileIsServedAsItIs() throws {
let image = Data([0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10])
let fixtures = try makeFixtureSet(
[
"images/header.json": mapping(
#"{"urlPath": "/2025/08/header.jpg"}"#,
response: #"{"status": 200, "bodyFileName": "images/header.jpg"}"#
)
],
files: ["images/header.jpg": image]
)

let response = fixtures.response(for: get("/2025/08/header.jpg?w=600"))
#expect(response.status == 200)
#expect(response.body == image)
#expect(response.headers["Content-Type"] == "image/jpeg")
}

@Test func bodyFileKeepsTheContentTypeItIsGiven() throws {
let fixtures = try makeFixtureSet(
[
"avatar.json": mapping(
#"{"urlPathPattern": "/avatar/.*"}"#,
response: #"{"status": 200, "bodyFileName": "avatar", "headers": {"Content-Type": "image/png"}}"#
)
],
files: ["avatar": Data([0x89, 0x50])]
)

#expect(fixtures.response(for: get("/avatar/abc")).headers["Content-Type"] == "image/png")
}

// MARK: - Overlays

@Test func overlayReplacesTheMappingAtTheSamePath() throws {
let fixtures = try makeFixtureSet(
[
"wpcom/me.json": mapping(#"{"urlPath": "/me"}"#, response: #"{"status": 200, "body": "English"}"#),
"wpcom/sites.json": mapping(#"{"urlPath": "/sites"}"#, response: #"{"status": 200, "body": "Sites"}"#)
],
overlay: [
"wpcom/me.json": mapping(#"{"urlPath": "/me"}"#, response: #"{"status": 200, "body": "日本語"}"#)
]
)

#expect(fixtures.response(for: get("/me")).text == "日本語")
#expect(fixtures.response(for: get("/sites")).text == "Sites")
}

@Test func overlayCanAddMappings() throws {
let fixtures = try makeFixtureSet(
["me.json": mapping(#"{"urlPath": "/me"}"#)],
overlay: ["extra.json": mapping(#"{"urlPath": "/extra"}"#)]
)

#expect(fixtures.response(for: get("/me")).status == 200)
#expect(fixtures.response(for: get("/extra")).status == 200)
}

// MARK: - Loading

@Test func rejectsAMissingBodyFile() throws {
let error = #expect(throws: FixtureError.self) {
try makeFixtureSet([
"header.json": mapping(
#"{"urlPath": "/header.jpg"}"#,
response: #"{"status": 200, "bodyFileName": "header.jpg"}"#
)
])
}

#expect(error?.file == "header.json")
#expect(error?.reason == "its bodyFileName, __files/header.jpg, can't be read")
}

@Test func rejectsABodyFileWithABody() throws {
let error = #expect(throws: FixtureError.self) {
try makeFixtureSet(
[
"header.json": mapping(
#"{"urlPath": "/h"}"#,
response: #"{"status": 200, "body": "x", "bodyFileName": "h.jpg"}"#
)
],
files: ["h.jpg": Data([1])]
)
}

#expect(error?.file == "header.json")
}

@Test func rejectsAMatcherItDoesNotSupport() throws {
let error = #expect(throws: FixtureError.self) {
try makeFixtureSet([
Expand Down
Loading