Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions apple/macos-helper-tests/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// swift-tools-version: 5.9
import PackageDescription

// Development-only Swift package: never shipped, never built by an install. The npm payload is
// apple/macos-helper, whose manifest declares no test targets because their sources are not
// shipped. `Sources` is a symlink to ../macos-helper/Sources, so the helper's own sources compile
// here as testable modules and `swift test --package-path apple/macos-helper-tests` covers them.
// Keep the target list in step with the shipped manifest.
let package = Package(
name: "agent-device-macos-helper-tests",
platforms: [.macOS(.v13)],
targets: [
.target(
name: "AgentDeviceMacOSInput"
),
.executableTarget(
name: "AgentDeviceMacOSHelper",
dependencies: ["AgentDeviceMacOSInput"]
),
.testTarget(
name: "AgentDeviceMacOSInputTests",
dependencies: ["AgentDeviceMacOSInput"]
),
.testTarget(
name: "AgentDeviceMacOSHelperTests",
dependencies: ["AgentDeviceMacOSHelper"]
),
]
)
1 change: 1 addition & 0 deletions apple/macos-helper-tests/Sources
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private struct HelperOutcomesFixture: Decodable {
let deliveryMechanisms: [String]
}

/// `#filePath` is `<repo>/apple/macos-helper/Tests/AgentDeviceMacOSHelperTests/<file>`.
/// `#filePath` is `<repo>/apple/macos-helper-tests/Tests/AgentDeviceMacOSHelperTests/<file>`.
private let repoRoot = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
.deletingLastPathComponent().deletingLastPathComponent()
Expand Down
12 changes: 4 additions & 8 deletions apple/macos-helper/Package.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// swift-tools-version: 5.9
import PackageDescription

// The npm package ships this directory and builds it on the user's machine, so it declares only
// what the helper binary needs. SwiftPM refuses to build a package whose declared test target has
// no sources, and the tests are not shipped; apple/macos-helper-tests is the manifest that owns
// them and compiles these same sources with them.
let package = Package(
name: "agent-device-macos-helper",
platforms: [.macOS(.v13)],
Expand All @@ -18,13 +22,5 @@ let package = Package(
name: "AgentDeviceMacOSHelper",
dependencies: ["AgentDeviceMacOSInput"]
),
.testTarget(
name: "AgentDeviceMacOSInputTests",
dependencies: ["AgentDeviceMacOSInput"]
),
.testTarget(
name: "AgentDeviceMacOSHelperTests",
dependencies: ["AgentDeviceMacOSHelper"]
),
]
)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"package:android-ime-helper:npm": "rm -rf android/ime-helper/dist && AGENT_DEVICE_ANDROID_HELPER=ime sh ./scripts/package-android-helper.sh $(node -p \"require('./package.json').version\") android/ime-helper/dist",
"build:macos-helper": "node --experimental-strip-types scripts/swift-toolchain-tmpdir.ts swift build -c release --package-path apple/macos-helper",
"build:macos-helper:clean": "node --experimental-strip-types scripts/swift-toolchain-tmpdir.ts swift package --package-path apple/macos-helper clean && pnpm build:macos-helper",
"test:macos-helper": "node --experimental-strip-types scripts/swift-toolchain-tmpdir.ts swift test --package-path apple/macos-helper",
"test:macos-helper": "node --experimental-strip-types scripts/swift-toolchain-tmpdir.ts swift test --package-path apple/macos-helper-tests",
"check:macos-helper": "pnpm build:macos-helper && pnpm test:macos-helper",
"prepare:publish-assets": "node scripts/prepare-publish-assets.mjs",
"build:package": "pnpm build && pnpm package:xcuitest && pnpm build:macos-helper:clean && pnpm prepare:publish-assets",
Expand Down
11 changes: 11 additions & 0 deletions scripts/__tests__/size-report-package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ test('publish package requires both Android helpers and excludes benchmark scrip
]),
/benchmark or build scripts/,
);
assert.throws(
() =>
assertPublishPackageContents([
...fixturePack.files,
{
path: 'apple/macos-helper/Tests/AgentDeviceMacOSInputTests/ScheduleTests.swift',
size: 1,
},
]),
/macOS helper test sources/,
);
});

test('Markdown emphasizes total install size and startup without duplicate breakdowns', () => {
Expand Down
3 changes: 2 additions & 1 deletion scripts/check-affected/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ const BUILD_OWNERSHIP: ReadonlyArray<{
check: 'macos-helper',
rule: 'own:macos-helper',
detail: 'macOS helper is a separate Swift package build',
owns: (file) => file.startsWith('apple/macos-helper/'),
owns: (file) =>
file.startsWith('apple/macos-helper/') || file.startsWith('apple/macos-helper-tests/'),
},
{
check: 'mcp-metadata',
Expand Down
10 changes: 10 additions & 0 deletions scripts/size-report-package.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ export function assertPublishPackageContents(entries, options = {}) {
if (forbiddenPaths.length > 0) {
throw new Error(`npm pack includes benchmark or build scripts: ${forbiddenPaths.join(', ')}`);
}
// The macOS helper ships as buildable source and the helper's own manifest declares no test
// targets, so anything under its Tests/ directory could only ever be dead weight the install
// never compiles. Its tests live in apple/macos-helper-tests, which `files` does not list.
const shippedSwiftTests = paths.filter(
(entryPath) =>
entryPath === 'apple/macos-helper/Tests' || entryPath.startsWith('apple/macos-helper/Tests/'),
);
if (shippedSwiftTests.length > 0) {
throw new Error(`npm pack includes macOS helper test sources: ${shippedSwiftTests.join(', ')}`);
}
}

function parseNpmPackOutput(stdout) {
Expand Down
72 changes: 72 additions & 0 deletions test/repo-guards/macos-helper-package-contents.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { test } from 'vitest';

// The npm package ships `apple/macos-helper` and builds it on the user's machine, so its manifest
// may declare only the targets whose sources ship. SwiftPM refuses any build of such a package —
// including the install-time `swift build` — once a declared test target's directory is missing,
// which is what keeps the helper's tests from leaving the tarball through `files` alone. The test
// targets belong to `apple/macos-helper-tests`, a development package that compiles the same
// sources through its `Sources` symlink. Nothing in the repo compiles the shipped manifest's
// target list — its first consumer is a user's machine — so these guards hold the split.
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
const shippedManifestPath = path.join(repoRoot, 'apple/macos-helper/Package.swift');
const testPackageRoot = path.join(repoRoot, 'apple/macos-helper-tests');

function declaredTargetNames(
manifest: string,
kind: 'target' | 'executableTarget' | 'testTarget',
): string[] {
return [
...manifest.matchAll(new RegExp(String.raw`\.${kind}\(\s*name:\s*"([^"]+)"`, 'g')),
].flatMap((match) => (match[1] === undefined ? [] : [match[1]]));
}

function shippedManifest(): string {
return fs.readFileSync(shippedManifestPath, 'utf8');
}

function testManifest(): string {
return fs.readFileSync(path.join(testPackageRoot, 'Package.swift'), 'utf8');
}

test('the shipped macOS helper manifest declares no test targets', () => {
const manifest = shippedManifest();
assert.doesNotMatch(manifest, /\.testTarget\(/);
assert.deepEqual(declaredTargetNames(manifest, 'target'), ['AgentDeviceMacOSInput']);
assert.deepEqual(declaredTargetNames(manifest, 'executableTarget'), ['AgentDeviceMacOSHelper']);
assert.match(manifest, /\.executable\(\s*name:\s*"agent-device-macos-helper"/);
});

test('the macOS helper test package compiles the shipped sources and owns the test targets', () => {
const manifest = testManifest();
assert.equal(
fs.readlinkSync(path.join(testPackageRoot, 'Sources')),
path.join('..', 'macos-helper', 'Sources'),
'the test package must compile the shipped sources, not a copy',
);
assert.ok(
fs.existsSync(path.join(testPackageRoot, 'Sources/AgentDeviceMacOSHelper/main.swift')),
'the Sources symlink must resolve into the shipped helper sources',
);

// Every target the shipped manifest builds must exist here too, or the tests would cover a
// tree users never compile.
const shippedTargets = [
...declaredTargetNames(shippedManifest(), 'target'),
...declaredTargetNames(shippedManifest(), 'executableTarget'),
];
const testPackageTargets = [
...declaredTargetNames(manifest, 'target'),
...declaredTargetNames(manifest, 'executableTarget'),
];
for (const name of shippedTargets) {
assert.ok(testPackageTargets.includes(name), `the test package must build ${name}`);
}
assert.deepEqual(
declaredTargetNames(manifest, 'testTarget').sort(),
['AgentDeviceMacOSHelperTests', 'AgentDeviceMacOSInputTests'].sort(),
);
});
Loading