From 7cb62a8f1ce212986313631a5eaf7b8dc8834fd0 Mon Sep 17 00:00:00 2001 From: showxu <10173746+showxu@users.noreply.github.com> Date: Sat, 24 Oct 2026 00:05:38 +0800 Subject: [PATCH 1/2] refactor(validation): test additional traits on the first compiler --- README.md | 13 ++-- schemas/swift-package.schema.json | 4 ++ scripts/run-validation.py | 14 +++-- tests/test_config.py | 99 +++++++++++++++++++++++++------ 4 files changed, 100 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 12e9f5c..5b0a7a3 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,9 @@ checks are exactly **`policy / policy`** and | Workflow / job | Behavior | | --- | --- | | `swift-package.yml` / `configure` | Schema-validates `.github/swift-package.json` and emits matrices. Unknown or duplicate keys fail. | -| `compiler` | Each compiler runs every test configuration and a release build for every declared trait set. Verifies the selected Swift version. | +| `compiler` | Each compiler runs every test configuration and one release build with default traits. The first compiler also tests each additional trait set in every test configuration, with no additional release build. Verifies the selected Swift version. | | `format` | Strict recursive Swift formatting using the repository's `.swift-format`, on the last compiler. | -| `linux` | Runs tests in each digest-pinned Swift Docker image. | +| `linux` | Runs every test configuration with default traits in each digest-pinned Swift Docker image. | | `apple` | Builds each generic iOS, tvOS, watchOS or visionOS destination, skipping plugin and macro validation for non-interactive CI. Optional tests use the SDK-matching simulator runtime, downloading it only when the runner lacks it, and create/delete a matching simulator. | | `docs` | Builds the whole package once, emitting public symbol graphs with extension blocks, then converts each target's DocC archive with warnings as errors. | | `demo` | Builds the release executable and checks a fresh terminal recording through the presentation action's recording-only mode. | @@ -31,14 +31,14 @@ See [the JSON Schema](schemas/swift-package.schema.json). | Key | Meaning / default | | --- | --- | -| `compilers` (required) | Nonempty `{name, runner, xcode, swift?}` array. Xcode must be installed on the runner. `swift` installs an official release after selecting Xcode. | +| `compilers` (required) | Nonempty `{name, runner, xcode, swift?}` array, with the minimum compiler first by convention. Xcode must be installed on the runner. `swift` installs an official release after selecting Xcode. | | `format` | `{paths}`; defaults to `Package.swift`, `Sources`, `Tests`, relative to the package directory. `.swift-format` lives at the repository root. | | `package_directory` | Package root inside the repository; default `.`. | | `test_configurations` | Nonempty list of `debug` and/or `release`; default `["debug"]`. | | `build_system` | `native` or `xcode`, passed to SwiftPM commands. Omission uses SwiftPM's default. | -| `traits` | List of trait sets, e.g. `[["default"], [], ["Experimental", "default"]]`. `default` enables SwiftPM default traits; an empty set disables them. Omission uses default traits. | +| `traits` | Additional trait sets tested in every test configuration on the first compiler only, e.g. `[["default"], [], ["Experimental", "default"]]`. `default` enables SwiftPM default traits; an empty set disables them. Omission means no additional sets. | | `environment` | String map exported to validation commands. The terminal script declares its own sandbox environment. | -| `platforms.linux` | Nonempty array of `{name, image}` with `@sha256:<64 hex digits>` image references. | +| `platforms.linux` | Nonempty array of `{name, image}` with `@sha256:<64 hex digits>` image references. Tests use default traits only. | | `platforms.apple` | Nonempty array of `{name, platform, scheme, test?, runner?, xcode?}`. Platform is `iOS`, `tvOS`, `watchOS` or `visionOS`; test defaults to false. | | `docs` | `{targets, traits?}` for packages shipping DocC. Catalogs live under each target path reported by SwiftPM. `traits` is one trait set for the documentation build, e.g. `["Experimental", "default"]`; omission uses default traits. | | `demo` | `{product, script?}`; script defaults to `.github/assets/terminal/script.yml`, relative to the repository. `recording.json` and optional `fixture/` live beside the script. | @@ -49,6 +49,9 @@ Apple and command entries default to the last compiler's runner and toolchain. An explicit Xcode override uses that Xcode's bundled Swift. Docs and demo also use the last compiler. Package and script paths must stay inside the checkout. +Every `swift build` and `swift test` invocation uses `--disable-index-store` +alongside any configured build-system flags; CI does not use the index. + The documentation build emits graphs for library and executable modules. Each declared target's DocC conversion receives only its main graph and extension graphs. A missing main graph fails the job and names the target. diff --git a/schemas/swift-package.schema.json b/schemas/swift-package.schema.json index f883784..3dcb9aa 100644 --- a/schemas/swift-package.schema.json +++ b/schemas/swift-package.schema.json @@ -5,6 +5,7 @@ "additionalProperties": false, "properties": { "compilers": { + "description": "Each compiler tests every test configuration and builds release once with default traits. Put the minimum compiler first by convention; only the first compiler tests additional trait sets.", "type": "array", "minItems": 1, "uniqueItems": true, @@ -63,6 +64,7 @@ "pattern": "^(?!/)(?!.*(?:^|/)\\.\\.(?:/|$))[^\\r\\n]+$" }, "test_configurations": { + "description": "Configurations for each test run, including additional trait sets on the first compiler and default traits on Linux. Defaults to debug.", "type": "array", "minItems": 1, "uniqueItems": true, @@ -80,6 +82,7 @@ ] }, "traits": { + "description": "Additional trait sets tested once per test configuration on the first compiler only, with no release build. The default item enables SwiftPM default traits; an empty set disables them. Omission means no additional sets.", "type": "array", "minItems": 1, "uniqueItems": true, @@ -106,6 +109,7 @@ "additionalProperties": false, "properties": { "linux": { + "description": "Digest-pinned Swift images that test every test configuration with default traits only.", "type": "array", "minItems": 1, "uniqueItems": true, diff --git a/scripts/run-validation.py b/scripts/run-validation.py index a95b6dc..e5aefeb 100755 --- a/scripts/run-validation.py +++ b/scripts/run-validation.py @@ -74,11 +74,13 @@ def execute(root, job, entry, config=None): config = config or load(root) package = inside(root, config.get('package_directory', '.')) environment = {**os.environ, **config.get('environment', {})} - traits = config.get('traits', [None]) configurations = config.get('test_configurations', ['debug']) if job in {'compiler', 'linux'}: + traits = [None] + if job == 'compiler' and entry['name'] == config['compilers'][0]['name']: + traits += config.get('traits', []) for trait_set in traits: - flags = swift_flags(config, trait_set) + flags = ['--disable-index-store', *swift_flags(config, trait_set)] for configuration in configurations: command = ['swift', 'test', '-c', configuration, *flags] if job == 'linux': @@ -87,7 +89,7 @@ def execute(root, job, entry, config=None): *[value for key in config.get('environment', {}) for value in ('-e', key)], entry['image'], *command] run(command, package, environment) - if job == 'compiler': + if job == 'compiler' and trait_set is None: run(['swift', 'build', '-c', 'release', *flags], package, environment) elif job == 'format': paths = [inside(package, path) for path in config.get('format', {}).get( @@ -115,7 +117,7 @@ def execute(root, job, entry, config=None): targets = {target['name']: target for target in description['targets']} all_symbols = package / '.build/symbol-graphs' all_symbols.mkdir(parents=True, exist_ok=True) - run(['swift', 'build', *swift_flags(config, config['docs'].get('traits')), + run(['swift', 'build', '--disable-index-store', *swift_flags(config, config['docs'].get('traits')), '-Xswiftc', '-emit-symbol-graph', '-Xswiftc', '-emit-symbol-graph-dir', '-Xswiftc', all_symbols, '-Xswiftc', '-emit-extension-block-symbols'], package, environment) for target in config['docs']['targets']: @@ -139,8 +141,8 @@ def execute(root, job, entry, config=None): '--fallback-bundle-version', '0.0.0', '--warnings-as-errors', '--output-path', output / (target + '.doccarchive')], package, environment) elif job == 'demo': - run(['swift', 'build', '-c', 'release', '--product', config['demo']['product'], *swift_flags(config)], package, environment) - binary = run(['swift', 'build', '-c', 'release', '--show-bin-path', *swift_flags(config)], package, environment, capture=True).strip() + run(['swift', 'build', '-c', 'release', '--product', config['demo']['product'], '--disable-index-store', *swift_flags(config)], package, environment) + binary = run(['swift', 'build', '-c', 'release', '--show-bin-path', '--disable-index-store', *swift_flags(config)], package, environment, capture=True).strip() script = inside(root, config['demo'].get('script', '.github/assets/terminal/script.yml')) assets = prepare_demo(root, script) with open(os.environ['GITHUB_OUTPUT'], 'a') as stream: diff --git a/tests/test_config.py b/tests/test_config.py index f4ee692..8ac891b 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -163,28 +163,89 @@ def respond(command, cwd, environment, capture=False): downloads = [call.args[0] for call in run.call_args_list if '-downloadPlatform' in call.args[0]] self.assertEqual(len(downloads), 1) - def test_compiler_runs_every_test_configuration_and_builds_every_trait(self): - self.config.update(test_configurations=['debug', 'release'], traits=[['default'], ['Feature', 'default'], []], build_system='native', environment={'VALUE': 'declared'}) + def test_compilers_without_traits_test_each_configuration_and_build_release(self): + self.config['test_configurations'] = ['debug', 'release'] + self.config['compilers'].append({'name': 'latest', 'runner': 'macos-26', 'xcode': '26.6'}) + for entry in config.matrices(self.config)['compiler']: + with self.subTest(compiler=entry['name']), patch.object(runner, 'run') as run: + runner.execute(self.root, 'compiler', entry, self.config) + self.assertEqual([call.args[0] for call in run.call_args_list], [ + ['swift', 'test', '-c', 'debug', '--disable-index-store'], + ['swift', 'test', '-c', 'release', '--disable-index-store'], + ['swift', 'build', '-c', 'release', '--disable-index-store'], + ]) + + def test_first_compiler_tests_additional_traits_without_extra_release_builds(self): + self.config.update(test_configurations=['debug', 'release'], + traits=[['default'], ['Feature', 'default'], []], + build_system='native', environment={'VALUE': 'declared'}) + self.config['compilers'].append({'name': 'latest', 'runner': 'macos-26', 'xcode': '26.6'}) + entry = config.matrices(self.config)['compiler'][0] with patch.object(runner, 'run') as run: - runner.execute(self.root, 'compiler', {}, self.config) - commands = [call.args[0] for call in run.call_args_list] - self.assertEqual(len(commands), 9) - self.assertEqual(sum(c[:2] == ['swift', 'test'] for c in commands), 6) - self.assertTrue(all('--build-system' in c for c in commands)) - self.assertIn('Feature,default', commands[3]) - self.assertIn('--disable-default-traits', commands[-1]) - self.assertEqual(run.call_args.args[2]['VALUE'], 'declared') - - def test_linux_uses_digest_and_exports_environment(self): - self.config['environment'] = {'CUSTOM': 'value'} + runner.execute(self.root, 'compiler', entry, self.config) + self.assertEqual([call.args[0] for call in run.call_args_list], [ + ['swift', 'test', '-c', 'debug', '--disable-index-store', '--build-system', 'native'], + ['swift', 'test', '-c', 'release', '--disable-index-store', '--build-system', 'native'], + ['swift', 'build', '-c', 'release', '--disable-index-store', '--build-system', 'native'], + ['swift', 'test', '-c', 'debug', '--disable-index-store', '--build-system', 'native', '--traits', 'default'], + ['swift', 'test', '-c', 'release', '--disable-index-store', '--build-system', 'native', '--traits', 'default'], + ['swift', 'test', '-c', 'debug', '--disable-index-store', '--build-system', 'native', '--traits', 'Feature,default'], + ['swift', 'test', '-c', 'release', '--disable-index-store', '--build-system', 'native', '--traits', 'Feature,default'], + ['swift', 'test', '-c', 'debug', '--disable-index-store', '--build-system', 'native', '--disable-default-traits'], + ['swift', 'test', '-c', 'release', '--disable-index-store', '--build-system', 'native', '--disable-default-traits'], + ]) + self.assertTrue(all(call.args[2]['VALUE'] == 'declared' for call in run.call_args_list)) + + def test_later_compilers_use_default_traits_only(self): + self.config.update(test_configurations=['debug', 'release'], + traits=[['default'], ['Feature', 'default'], []], build_system='native') + self.config['compilers'] += [ + {'name': 'middle', 'runner': 'macos-26', 'xcode': '26.5'}, + {'name': 'latest', 'runner': 'macos-26', 'xcode': '26.6'}, + ] + for entry in config.matrices(self.config)['compiler'][1:]: + with self.subTest(compiler=entry['name']), patch.object(runner, 'run') as run: + runner.execute(self.root, 'compiler', entry, self.config) + self.assertEqual([call.args[0] for call in run.call_args_list], [ + ['swift', 'test', '-c', 'debug', '--disable-index-store', '--build-system', 'native'], + ['swift', 'test', '-c', 'release', '--disable-index-store', '--build-system', 'native'], + ['swift', 'build', '-c', 'release', '--disable-index-store', '--build-system', 'native'], + ]) + + def test_linux_tests_default_traits_only_and_exports_environment(self): + self.config.update(test_configurations=['debug', 'release'], + traits=[['default'], ['Feature', 'default'], []], + build_system='native', environment={'CUSTOM': 'value'}, + package_directory='Sources') entry = self.config['platforms']['linux'][0] with patch.object(runner, 'run') as run: runner.execute(self.root, 'linux', entry, self.config) - command = run.call_args.args[0] - self.assertEqual(command[:3], ['docker', 'run', '--rm']) - self.assertIn(entry['image'], command) - self.assertIn('CUSTOM', command) - self.assertEqual(command[-4:], ['swift', 'test', '-c', 'debug']) + self.assertEqual([call.args[0] for call in run.call_args_list], [ + ['docker', 'run', '--rm', '-v', str(self.root) + ':/workspace', + '-w', '/workspace/Sources', '-e', 'CUSTOM', entry['image'], + 'swift', 'test', '-c', 'debug', '--disable-index-store', '--build-system', 'native'], + ['docker', 'run', '--rm', '-v', str(self.root) + ':/workspace', + '-w', '/workspace/Sources', '-e', 'CUSTOM', entry['image'], + 'swift', 'test', '-c', 'release', '--disable-index-store', '--build-system', 'native'], + ]) + self.assertTrue(all(call.args[2]['CUSTOM'] == 'value' for call in run.call_args_list)) + + def test_demo_build_commands_disable_index_store(self): + parent = ROOT / '.build/demo-tests' + parent.mkdir(parents=True, exist_ok=True) + with tempfile.TemporaryDirectory(dir=parent) as directory: + root = Path(directory) + self.config['build_system'] = 'native' + environment = {'GITHUB_OUTPUT': str(root / 'outputs'), 'GITHUB_PATH': str(root / 'path')} + with patch.dict(os.environ, environment), patch.object(runner, 'prepare_demo'), \ + patch.object(runner, 'run', side_effect=[None, str(root / 'bin')]) as run: + runner.execute(root, 'demo', {}, self.config) + self.assertEqual([call.args[0] for call in run.call_args_list], [ + ['swift', 'build', '-c', 'release', '--product', 'fixture-cli', + '--disable-index-store', '--build-system', 'native'], + ['swift', 'build', '-c', 'release', '--show-bin-path', + '--disable-index-store', '--build-system', 'native'], + ]) def test_command_executes_in_checkout_not_package_subdirectory(self): self.config['package_directory'] = 'Sources' @@ -256,7 +317,7 @@ def execute(command, *args, **kwargs): elif command[:2] == ['swift', 'build']: symbols = root / '.build/symbol-graphs' self.assertEqual(command, [ - 'swift', 'build', '--build-system', 'native', *trait_flags, + 'swift', 'build', '--disable-index-store', '--build-system', 'native', *trait_flags, '-Xswiftc', '-emit-symbol-graph', '-Xswiftc', '-emit-symbol-graph-dir', '-Xswiftc', symbols, '-Xswiftc', '-emit-extension-block-symbols']) builds.append(command) From c7b681a9588d9184f9eb4b710451c4b63c693299 Mon Sep 17 00:00:00 2001 From: showxu <10173746+showxu@users.noreply.github.com> Date: Sat, 24 Oct 2026 00:50:37 +0800 Subject: [PATCH 2/2] fix(linux): keep the index store for test discovery --- README.md | 5 +++-- scripts/run-validation.py | 3 ++- tests/test_config.py | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5b0a7a3..f292565 100644 --- a/README.md +++ b/README.md @@ -49,8 +49,9 @@ Apple and command entries default to the last compiler's runner and toolchain. An explicit Xcode override uses that Xcode's bundled Swift. Docs and demo also use the last compiler. Package and script paths must stay inside the checkout. -Every `swift build` and `swift test` invocation uses `--disable-index-store` -alongside any configured build-system flags; CI does not use the index. +Every macOS `swift build` and `swift test` invocation uses `--disable-index-store` +alongside any configured build-system flags; CI does not use the index. Linux +keeps the index store because XCTest discovers tests through it. The documentation build emits graphs for library and executable modules. Each declared target's DocC conversion receives only its main graph and diff --git a/scripts/run-validation.py b/scripts/run-validation.py index e5aefeb..0747ebf 100755 --- a/scripts/run-validation.py +++ b/scripts/run-validation.py @@ -80,7 +80,8 @@ def execute(root, job, entry, config=None): if job == 'compiler' and entry['name'] == config['compilers'][0]['name']: traits += config.get('traits', []) for trait_set in traits: - flags = ['--disable-index-store', *swift_flags(config, trait_set)] + # Linux XCTest discovers tests through the index store; macOS does not need it. + flags = [*([] if job == 'linux' else ['--disable-index-store']), *swift_flags(config, trait_set)] for configuration in configurations: command = ['swift', 'test', '-c', configuration, *flags] if job == 'linux': diff --git a/tests/test_config.py b/tests/test_config.py index 8ac891b..c45ed86 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -223,10 +223,10 @@ def test_linux_tests_default_traits_only_and_exports_environment(self): self.assertEqual([call.args[0] for call in run.call_args_list], [ ['docker', 'run', '--rm', '-v', str(self.root) + ':/workspace', '-w', '/workspace/Sources', '-e', 'CUSTOM', entry['image'], - 'swift', 'test', '-c', 'debug', '--disable-index-store', '--build-system', 'native'], + 'swift', 'test', '-c', 'debug', '--build-system', 'native'], ['docker', 'run', '--rm', '-v', str(self.root) + ':/workspace', '-w', '/workspace/Sources', '-e', 'CUSTOM', entry['image'], - 'swift', 'test', '-c', 'release', '--disable-index-store', '--build-system', 'native'], + 'swift', 'test', '-c', 'release', '--build-system', 'native'], ]) self.assertTrue(all(call.args[2]['CUSTOM'] == 'value' for call in run.call_args_list))