Skip to content

Commit 2a72c7e

Browse files
committed
fix(ci): keep Android replay shell state in one invocation
1 parent 4bad3f2 commit 2a72c7e

3 files changed

Lines changed: 93 additions & 26 deletions

File tree

‎.github/workflows/replays-manual.yml‎

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,7 @@ jobs:
7171
profile: pixel_7
7272
target: google_apis_playstore
7373
emulator-options: -no-window -gpu swiftshader_indirect -no-snapshot -noaudio -no-boot-anim -no-metrics
74-
script: |
75-
# android-emulator-runner executes this with /usr/bin/sh, which is dash on
76-
# ubuntu runners and rejects `-o pipefail`. The serial assignment below is
77-
# validated by the `test -n` guard instead.
78-
set -eu
79-
ANDROID_SERIAL="$(adb devices | awk 'NR > 1 && $2 == "device" { print $1; exit }')"
80-
test -n "$ANDROID_SERIAL"
81-
BOOT_FINISHED_AT="$(date +%s)"
82-
pnpm gate build
83-
pnpm clean:daemon
84-
if [ "${{ inputs.readiness_wait }}" != "normal" ]; then
85-
node --experimental-strip-types scripts/replays/readiness-experiment.ts begin android
86-
REPLAY_EXIT=0
87-
pnpm gate replay-android --serial "$ANDROID_SERIAL" --debug --retries 0 --artifacts-dir test/artifacts/replays-android --report-junit test/artifacts/replays-android.junit.xml || REPLAY_EXIT=$?
88-
node --experimental-strip-types scripts/replays/readiness-experiment.ts summarize android "$AGENT_DEVICE_STATE_DIR" test/artifacts/replays-android.junit.xml
89-
exit "$REPLAY_EXIT"
90-
fi
91-
adb -s "$ANDROID_SERIAL" install -r "${{ steps.fixture-app.outputs.apk-path }}"
92-
node --experimental-strip-types src/bin.ts replay examples/test-app/replays/drag-android.ad --platform android --serial "$ANDROID_SERIAL" --session nightly-android-target-drag --json
93-
pnpm clean:daemon
94-
FULL_STARTED_AT="$(date +%s)"
95-
AGENT_DEVICE_ANDROID_E2E=1 AGENT_DEVICE_ANDROID_E2E_TIER=full AGENT_DEVICE_ANDROID_SERIAL="$ANDROID_SERIAL" AGENT_DEVICE_FIXTURE_APP_PATH="${{ steps.fixture-app.outputs.apk-path }}" AGENT_DEVICE_FIXTURE_APP_ID="${{ steps.fixture-app.outputs.app-id }}" node --experimental-strip-types scripts/node-test-tmpdir.ts --test test/integration/smoke-android-emulator.test.ts
96-
FULL_FINISHED_AT="$(date +%s)"
97-
echo "Android setup before emulator: $((BOOT_FINISHED_AT - ${{ steps.android-setup.outputs.seconds }}))s"
98-
echo "Android full scenario wall time: $((FULL_FINISHED_AT - FULL_STARTED_AT))s"
99-
pnpm gate replay-android --retries 2 --artifacts-dir test/artifacts/replays-android --report-junit test/artifacts/replays-android.junit.xml
74+
script: sh scripts/replays/android-manual.sh "${{ inputs.readiness_wait }}" "${{ steps.fixture-app.outputs.apk-path }}" "${{ steps.fixture-app.outputs.app-id }}" "${{ steps.android-setup.outputs.seconds }}"
10075

10176
- name: Upload Android artifacts
10277
if: always()

‎scripts/replays/android-manual.sh‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
READINESS_MODE="$1"
5+
FIXTURE_APK="$2"
6+
FIXTURE_APP_ID="$3"
7+
ANDROID_SETUP_SECONDS="$4"
8+
ANDROID_SERIAL="$(adb devices | awk 'NR > 1 && $2 == "device" { print $1; exit }')"
9+
test -n "$ANDROID_SERIAL"
10+
BOOT_FINISHED_AT="$(date +%s)"
11+
pnpm gate build
12+
pnpm clean:daemon
13+
14+
if [ "$READINESS_MODE" != normal ]; then
15+
node --experimental-strip-types scripts/replays/readiness-experiment.ts begin android
16+
REPLAY_EXIT=0
17+
pnpm gate replay-android --serial "$ANDROID_SERIAL" --debug --retries 0 --artifacts-dir test/artifacts/replays-android --report-junit test/artifacts/replays-android.junit.xml || REPLAY_EXIT=$?
18+
node --experimental-strip-types scripts/replays/readiness-experiment.ts summarize android "$AGENT_DEVICE_STATE_DIR" test/artifacts/replays-android.junit.xml
19+
exit "$REPLAY_EXIT"
20+
fi
21+
22+
adb -s "$ANDROID_SERIAL" install -r "$FIXTURE_APK"
23+
node --experimental-strip-types src/bin.ts replay examples/test-app/replays/drag-android.ad --platform android --serial "$ANDROID_SERIAL" --session nightly-android-target-drag --json
24+
pnpm clean:daemon
25+
FULL_STARTED_AT="$(date +%s)"
26+
AGENT_DEVICE_ANDROID_E2E=1 AGENT_DEVICE_ANDROID_E2E_TIER=full AGENT_DEVICE_ANDROID_SERIAL="$ANDROID_SERIAL" AGENT_DEVICE_FIXTURE_APP_PATH="$FIXTURE_APK" AGENT_DEVICE_FIXTURE_APP_ID="$FIXTURE_APP_ID" node --experimental-strip-types scripts/node-test-tmpdir.ts --test test/integration/smoke-android-emulator.test.ts
27+
FULL_FINISHED_AT="$(date +%s)"
28+
echo "Android setup before emulator: $((BOOT_FINISHED_AT - ANDROID_SETUP_SECONDS))s"
29+
echo "Android full scenario wall time: $((FULL_FINISHED_AT - FULL_STARTED_AT))s"
30+
pnpm gate replay-android --retries 2 --artifacts-dir test/artifacts/replays-android --report-junit test/artifacts/replays-android.junit.xml
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import { parse } from 'yaml';
4+
import { expect, test } from 'vitest';
5+
import { runCmdSync } from '@agent-device/host-kit/command';
6+
import { mkdtempForTest } from '../../src/__tests__/test-utils/tmp-dir.ts';
7+
8+
test('emulator action invocation keeps shell state and summarizes a failed replay before returning its status', async () => {
9+
const cwd = await mkdtempForTest('android-manual-invocation-');
10+
const bin = path.join(cwd, 'bin');
11+
fs.mkdirSync(bin);
12+
const writeTool = (name: string, script: string) =>
13+
fs.writeFileSync(path.join(bin, name), `#!/bin/sh\n${script}\n`, { mode: 0o755 });
14+
writeTool('adb', String.raw`printf "List of devices attached\nemulator-7000\tdevice\n"`);
15+
writeTool(
16+
'pnpm',
17+
'printf "%s\\n" "$*" >> "$READINESS_TEST_LOG"\nif [ "$1 $2" = "gate replay-android" ]; then exit 7; fi',
18+
);
19+
writeTool(
20+
'node',
21+
'if [ "$3" = begin ]; then touch started; fi\nif [ "$3" = summarize ]; then touch summarized; fi',
22+
);
23+
fs.mkdirSync(path.join(cwd, 'scripts/replays'), { recursive: true });
24+
fs.copyFileSync(
25+
'scripts/replays/android-manual.sh',
26+
path.join(cwd, 'scripts/replays/android-manual.sh'),
27+
);
28+
const workflow = parse(fs.readFileSync('.github/workflows/replays-manual.yml', 'utf8'));
29+
const runner = workflow.jobs['manual-android'].steps.find((step: { uses?: string }) =>
30+
step.uses?.startsWith('reactivecircus/android-emulator-runner@'),
31+
);
32+
const script = runner.with.script
33+
.replaceAll(/\$\{\{ inputs.readiness_wait \}\}/g, 'enabled')
34+
.replaceAll(/\$\{\{ steps.fixture-app.outputs.apk-path \}\}/g, '')
35+
.replaceAll(/\$\{\{ steps.fixture-app.outputs.app-id \}\}/g, '')
36+
.replaceAll(/\$\{\{ steps.android-setup.outputs.seconds \}\}/g, '0');
37+
const commands = script
38+
.split('\n')
39+
.map((line: string) => line.trim())
40+
.filter((line: string) => line.length > 0 && !line.startsWith('#'));
41+
const log = path.join(cwd, 'calls.log');
42+
let exitCode = 0;
43+
for (const command of commands) {
44+
exitCode = runCmdSync('sh', ['-c', command], {
45+
cwd,
46+
allowFailure: true,
47+
env: {
48+
PATH: `${bin}:${process.env.PATH}`,
49+
ANDROID_SERIAL: 'emulator-legacy',
50+
AGENT_DEVICE_STATE_DIR: path.join(cwd, 'state'),
51+
READINESS_TEST_LOG: log,
52+
},
53+
}).exitCode;
54+
if (exitCode !== 0) break;
55+
}
56+
expect(exitCode).toBe(7);
57+
expect(fs.existsSync(path.join(cwd, 'started'))).toBe(true);
58+
expect(fs.existsSync(path.join(cwd, 'summarized'))).toBe(true);
59+
expect(fs.readFileSync(log, 'utf8')).toContain(
60+
'replay-android --serial emulator-7000 --debug --retries 0',
61+
);
62+
});

0 commit comments

Comments
 (0)