diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml new file mode 100644 index 00000000..e62840f1 --- /dev/null +++ b/.github/workflows/coverage.yml @@ -0,0 +1,95 @@ +name: Mixed-language coverage + +env: + FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true + +on: + pull_request: + branches: [main] + paths: + - '.github/workflows/coverage.yml' + - 'Cargo.lock' + - 'Cargo.toml' + - 'bindings/js/**' + - 'bindings/py/**' + - 'crates/dynwinrt/**' + - 'eng/coverage/**' + - 'tests/e2e/**' + - 'tools/dynwinrt-codegen/**' + push: + branches: [main] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: coverage-${{ github.ref }} + cancel-in-progress: true + +jobs: + coverage: + runs-on: windows-latest + timeout-minutes: 90 + steps: + - uses: actions/checkout@v4 + + - uses: dtolnay/rust-toolchain@stable + with: + components: llvm-tools-preview + + - uses: actions/setup-node@v4 + with: + node-version: 24 + cache: npm + cache-dependency-path: bindings/js/package-lock.json + + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + cache: pip + + - uses: NuGet/setup-nuget@v2 + + - name: Install cargo-llvm-cov + shell: pwsh + run: cargo install cargo-llvm-cov --version 0.8.7 --locked + + - name: Install Win32 metadata + shell: pwsh + run: | + $root = Join-Path $env:RUNNER_TEMP 'win32metadata' + nuget install Microsoft.Windows.SDK.Win32Metadata ` + -Version 71.0.14-preview ` + -OutputDirectory $root ` + -DirectDownload ` + -NonInteractive + $winmd = Get-ChildItem $root -Filter Windows.Win32.winmd -File -Recurse | + Select-Object -First 1 + if (-not $winmd) { + throw 'Microsoft.Windows.SDK.Win32Metadata did not contain Windows.Win32.winmd' + } + "DYNWINRT_WIN32_WINMD=$($winmd.FullName)" >> $env:GITHUB_ENV + + - name: Run Rust, Python, JavaScript, and E2E coverage + shell: pwsh + run: | + .\eng\coverage\coverage.ps1 ` + -Win32Winmd $env:DYNWINRT_WIN32_WINMD + + - name: Publish coverage summary + if: always() + shell: pwsh + run: | + if (Test-Path artifacts\coverage\summary.md) { + Get-Content artifacts\coverage\summary.md >> $env:GITHUB_STEP_SUMMARY + } + + - name: Upload coverage reports + if: always() + uses: actions/upload-artifact@v4 + with: + name: mixed-language-coverage + path: artifacts/coverage/ + if-no-files-found: warn + retention-days: 14 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 490b421a..db6ce142 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -40,6 +40,25 @@ cargo test -p dynwinrt cargo test -p dynwinrt-codegen ``` +### Coverage + +Install the Rust coverage tool once: + +```powershell +rustup component add llvm-tools-preview +cargo install cargo-llvm-cov --version 0.8.7 --locked +``` + +Run the mixed-language coverage pipeline: + +```powershell +.\eng\coverage\coverage.ps1 +``` + +The script builds instrumented Python and Node native modules, runs the +Rust/Python/JavaScript tests and E2E suite, and writes HTML, LCOV, XML, and JSON +reports under `artifacts\coverage`. + ### Code Style - Run `cargo clippy` before submitting PRs @@ -60,4 +79,3 @@ Please use the provided issue templates when available. 5. Submit your pull request Thank you for contributing! - diff --git a/Cargo.toml b/Cargo.toml index d6db3a6b..aa60b45f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,10 @@ license = "MIT" opt-level = 3 lto = true codegen-units = 1 + +[profile.coverage] +inherits = "dev" +incremental = false +lto = false +codegen-units = 1 +debug = 2 diff --git a/bindings/js/__test__/index.spec.ts b/bindings/js/__test__/index.spec.ts index 058256e6..467b672e 100644 --- a/bindings/js/__test__/index.spec.ts +++ b/bindings/js/__test__/index.spec.ts @@ -160,7 +160,7 @@ test('DynCom rejects SharedArrayBuffer-backed native storage', (t) => { test('DynCom does not adopt borrowed raw pointer bits as owned COM references', (t) => { const borrowed = DynCom.pointer(0n) const error = t.throws(() => DynCom.adoptComPointer(borrowed)) - t.regex(error.message, /only owned native outputs may be consumed/) + t.regex(error.message, /Borrowed provenance; expected ComOutput/) }) test('DynCom exposes HSTRING and semantic HRESULT primitives', (t) => { @@ -382,7 +382,7 @@ test('DynCom Automation wrappers preserve tags, values, bounds, and transfer', ( }) t.throws( () => DynComSafeArray.interface(WinGuid.parse('ffffffff-ffff-ffff-ffff-ffffffffffff'), [activationFactory]), - { message: /interface is not supported|No such interface supported/i }, + { message: /80004002|interface is not supported|No such interface supported/i }, ) const nullSafeArray = DynWinRtValue.nullValue() t.is(DynCom.takeNullableSafeArray(nullSafeArray), null) diff --git a/bindings/js/package-lock.json b/bindings/js/package-lock.json index c35d712a..7b19b7d9 100644 --- a/bindings/js/package-lock.json +++ b/bindings/js/package-lock.json @@ -17,6 +17,7 @@ "@taplo/cli": "^0.7.0", "@tybys/wasm-util": "^0.10.0", "ava": "^6.4.1", + "c8": "12.0.0", "chalk": "^5.6.2", "emnapi": "^1.5.0", "husky": "^9.1.7", @@ -32,6 +33,16 @@ "node": ">= 12.22.0 < 13 || >= 14.17.0 < 15 || >= 15.12.0 < 16 || >= 16.0.0" } }, + "node_modules/@bcoe/v8-coverage": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@bcoe/v8-coverage/-/v8-coverage-1.0.2.tgz", + "integrity": "sha1-u+EtyltO+YOg0K9LB7m8kOoKuro=", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, "node_modules/@emnapi/core": { "version": "1.8.1", "resolved": "https://registry.npmjs.org/@emnapi/core/-/core-1.8.1.tgz", @@ -924,6 +935,44 @@ "node": ">=18.0.0" } }, + "node_modules/@istanbuljs/schema": { + "version": "0.1.6", + "resolved": "https://registry.npmjs.org/@istanbuljs/schema/-/schema-0.1.6.tgz", + "integrity": "sha1-jcmvoqwVBssaWPiZQPHBJERsjfM=", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", + "integrity": "sha1-eg7mAfYPmaIMfHxf8MgDiMEYm9Y=", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.5.5", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.5.5.tgz", + "integrity": "sha1-aRKwDSxjHA0Vzhp6tXzWV/Ko+Lo=", + "dev": true, + "license": "MIT" + }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.31", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.31.tgz", + "integrity": "sha1-2xXWeByTHzolGj2sOVAcmKYIL9A=", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/resolve-uri": "^3.1.0", + "@jridgewell/sourcemap-codec": "^1.4.14" + } + }, "node_modules/@mapbox/node-pre-gyp": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/@mapbox/node-pre-gyp/-/node-pre-gyp-2.0.3.tgz", @@ -2602,6 +2651,13 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/istanbul-lib-coverage": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.6.tgz", + "integrity": "sha1-dznCMqH+6bTTzomF8xTAxtM1Sdc=", + "dev": true, + "license": "MIT" + }, "node_modules/@vercel/nft": { "version": "0.29.4", "resolved": "https://registry.npmjs.org/@vercel/nft/-/nft-0.29.4.tgz", @@ -2892,6 +2948,100 @@ "node": ">=8" } }, + "node_modules/c8": { + "version": "12.0.0", + "resolved": "https://registry.npmjs.org/c8/-/c8-12.0.0.tgz", + "integrity": "sha1-vGnyBGgmUaO7ybO2UB87EwydCzY=", + "dev": true, + "license": "ISC", + "dependencies": { + "@bcoe/v8-coverage": "^1.0.1", + "@istanbuljs/schema": "^0.1.3", + "find-up": "^5.0.0", + "foreground-child": "^3.1.1", + "istanbul-lib-coverage": "^3.2.0", + "istanbul-lib-report": "^3.0.1", + "istanbul-reports": "^3.1.6", + "test-exclude": "^8.0.0", + "v8-to-istanbul": "^9.0.0", + "yargs": "^18.0.0", + "yargs-parser": "^21.1.1" + }, + "bin": { + "c8": "bin/c8.js" + }, + "engines": { + "node": "^20.19.0 || ^22.12.0 || >=23" + }, + "peerDependencies": { + "monocart-coverage-reports": "^2" + }, + "peerDependenciesMeta": { + "monocart-coverage-reports": { + "optional": true + } + } + }, + "node_modules/c8/node_modules/cliui": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-9.0.1.tgz", + "integrity": "sha1-b3iQ84b28feZU63B943sRvzC0pE=", + "dev": true, + "license": "ISC", + "dependencies": { + "string-width": "^7.2.0", + "strip-ansi": "^7.1.0", + "wrap-ansi": "^9.0.0" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/c8/node_modules/yargs": { + "version": "18.1.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-18.1.0.tgz", + "integrity": "sha1-zX6YxwPvUWlbu/Bi7VjyjpQpG1Y=", + "dev": true, + "license": "MIT", + "dependencies": { + "cliui": "^9.0.1", + "escalade": "^3.1.1", + "get-caller-file": "^2.0.5", + "string-width": "^8.2.1", + "y18n": "^5.0.5", + "yargs-parser": "^22.0.0" + }, + "engines": { + "node": "^20.19.0 || ^22.12.0 || >=23" + } + }, + "node_modules/c8/node_modules/yargs/node_modules/string-width": { + "version": "8.2.2", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-8.2.2.tgz", + "integrity": "sha1-cxBRZJPfV1dC/pivb66H2F1e0Kw=", + "dev": true, + "license": "MIT", + "dependencies": { + "get-east-asian-width": "^1.5.0", + "strip-ansi": "^7.1.2" + }, + "engines": { + "node": ">=20" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/c8/node_modules/yargs/node_modules/yargs-parser": { + "version": "22.0.0", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-22.0.0.tgz", + "integrity": "sha1-h7gglAUbBWdxc0bs0A/RSASzV8g=", + "dev": true, + "license": "ISC", + "engines": { + "node": "^20.19.0 || ^22.12.0 || >=23" + } + }, "node_modules/callsites": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/callsites/-/callsites-4.2.0.tgz", @@ -3228,6 +3378,13 @@ "node": "^14.18.0 || >=16.10.0" } }, + "node_modules/convert-source-map": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz", + "integrity": "sha1-S1YPZJ/E6RjdCrdc9JYei8iC2Co=", + "dev": true, + "license": "MIT" + }, "node_modules/convert-to-spaces": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/convert-to-spaces/-/convert-to-spaces-2.0.1.tgz", @@ -3563,6 +3720,23 @@ "node": ">=8" } }, + "node_modules/find-up": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz", + "integrity": "sha1-TJKBnstwg1YeT0okCoa+UZj1Nvw=", + "dev": true, + "license": "MIT", + "dependencies": { + "locate-path": "^6.0.0", + "path-exists": "^4.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/find-up-simple": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/find-up-simple/-/find-up-simple-1.0.1.tgz", @@ -3619,9 +3793,9 @@ } }, "node_modules/get-east-asian-width": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/get-east-asian-width/-/get-east-asian-width-1.4.0.tgz", - "integrity": "sha512-QZjmEOC+IT1uk6Rx0sX22V6uHWVwbdbxf1faPqJ1QhLdGgsRGCZoyaQBm/piRdJy/D2um6hM1UP7ZEeQ4EkP+Q==", + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/get-east-asian-width/-/get-east-asian-width-1.6.0.tgz", + "integrity": "sha1-IWkA+R3xGossGYw+HZPWwDWndrk=", "dev": true, "license": "MIT", "engines": { @@ -3707,6 +3881,23 @@ "dev": true, "license": "ISC" }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha1-lEdx/ZyByBJlxNaUGGDaBrtZR5s=", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/html-escaper": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-2.0.2.tgz", + "integrity": "sha1-39YAJ9o2o238viNiYsAKWCJoFFM=", + "dev": true, + "license": "MIT" + }, "node_modules/https-proxy-agent": { "version": "7.0.6", "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.6.tgz", @@ -3890,6 +4081,45 @@ "dev": true, "license": "ISC" }, + "node_modules/istanbul-lib-coverage": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/istanbul-lib-coverage/-/istanbul-lib-coverage-3.2.2.tgz", + "integrity": "sha1-LRZsSwZE1Do58Ev2wu3R5YXzF1Y=", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">=8" + } + }, + "node_modules/istanbul-lib-report": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/istanbul-lib-report/-/istanbul-lib-report-3.0.1.tgz", + "integrity": "sha1-kIMFusmlvRdaxqdEier9D8JEWn0=", + "dev": true, + "license": "BSD-3-Clause", + "dependencies": { + "istanbul-lib-coverage": "^3.0.0", + "make-dir": "^4.0.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/istanbul-reports": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/istanbul-reports/-/istanbul-reports-3.2.0.tgz", + "integrity": "sha1-y0U1FitXhKpiPO4hpyUs8sgHrJM=", + "dev": true, + "license": "BSD-3-Clause", + "dependencies": { + "html-escaper": "^2.0.0", + "istanbul-lib-report": "^3.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/jackspeak": { "version": "3.4.3", "resolved": "https://registry.npmjs.org/jackspeak/-/jackspeak-3.4.3.tgz", @@ -4062,6 +4292,22 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/locate-path": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz", + "integrity": "sha1-VTIeswn+u8WcSAHZMackUqaB0oY=", + "dev": true, + "license": "MIT", + "dependencies": { + "p-locate": "^5.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/lodash": { "version": "4.17.23", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.23.tgz", @@ -4129,6 +4375,22 @@ "dev": true, "license": "ISC" }, + "node_modules/make-dir": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-4.0.0.tgz", + "integrity": "sha1-w8IwencSd82WODBfkVwprnQbYU4=", + "dev": true, + "license": "MIT", + "dependencies": { + "semver": "^7.5.3" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/matcher": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/matcher/-/matcher-5.0.0.tgz", @@ -4250,11 +4512,11 @@ } }, "node_modules/minipass": { - "version": "7.1.2", - "resolved": "https://registry.npmjs.org/minipass/-/minipass-7.1.2.tgz", - "integrity": "sha512-qOOzS1cBTWYF4BH8fVePDBOO9iptMnGUEZwNc/cMWnTV2nVLZ7VoNWEPHkYczZA0pdoA7dl6e7FL659nX9S2aw==", + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/minipass/-/minipass-7.1.3.tgz", + "integrity": "sha1-eTibTrG7LQA6m7qH1JLyvTe9xls=", "dev": true, - "license": "ISC", + "license": "BlueOak-1.0.0", "engines": { "node": ">=16 || 14 >=14.17" } @@ -4485,6 +4747,38 @@ } } }, + "node_modules/p-limit": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", + "integrity": "sha1-4drMvnjQ0TiMoYxk/qOOPlfjcGs=", + "dev": true, + "license": "MIT", + "dependencies": { + "yocto-queue": "^0.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/p-locate": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-5.0.0.tgz", + "integrity": "sha1-g8gxXGeFAF470CGDlBHJ4RDm2DQ=", + "dev": true, + "license": "MIT", + "dependencies": { + "p-limit": "^3.0.2" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/p-map": { "version": "7.0.4", "resolved": "https://registry.npmjs.org/p-map/-/p-map-7.0.4.tgz", @@ -4535,6 +4829,16 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/path-exists": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", + "integrity": "sha1-UTvb4tO5XXdi6METfvoZXGxhtbM=", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, "node_modules/path-key": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", @@ -5105,6 +5409,19 @@ "js-yaml": "bin/js-yaml.js" } }, + "node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha1-G33NyzK4E4gBs+R4umpRyqiWSNo=", + "dev": true, + "license": "MIT", + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/tar": { "version": "7.5.7", "resolved": "https://registry.npmjs.org/tar/-/tar-7.5.7.tgz", @@ -5132,6 +5449,105 @@ "node": ">=14.16" } }, + "node_modules/test-exclude": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/test-exclude/-/test-exclude-8.0.0.tgz", + "integrity": "sha1-hYka3T+ka7gisbFXnH+Mmj0E69g=", + "dev": true, + "license": "ISC", + "dependencies": { + "@istanbuljs/schema": "^0.1.2", + "glob": "^13.0.6", + "minimatch": "^10.2.2" + }, + "engines": { + "node": "20 || >=22" + } + }, + "node_modules/test-exclude/node_modules/balanced-match": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-4.0.4.tgz", + "integrity": "sha1-v7EGYv7tgZaixi58aOF3IMJ0F5o=", + "dev": true, + "license": "MIT", + "engines": { + "node": "18 || 20 || >=22" + } + }, + "node_modules/test-exclude/node_modules/brace-expansion": { + "version": "5.0.8", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.8.tgz", + "integrity": "sha1-E1rQ2NgI6xjrXg7Joh86C5LvGM8=", + "dev": true, + "license": "MIT", + "dependencies": { + "balanced-match": "^4.0.2" + }, + "engines": { + "node": "20 || >=22" + } + }, + "node_modules/test-exclude/node_modules/glob": { + "version": "13.0.6", + "resolved": "https://registry.npmjs.org/glob/-/glob-13.0.6.tgz", + "integrity": "sha1-B4ZmVmpCUUfMrPvS4zLetmor5x0=", + "dev": true, + "license": "BlueOak-1.0.0", + "dependencies": { + "minimatch": "^10.2.2", + "minipass": "^7.1.3", + "path-scurry": "^2.0.2" + }, + "engines": { + "node": "18 || 20 || >=22" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/test-exclude/node_modules/lru-cache": { + "version": "11.5.2", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-11.5.2.tgz", + "integrity": "sha1-AOFmZckMYg+6FKPDaHMql2ST92A=", + "dev": true, + "license": "BlueOak-1.0.0", + "engines": { + "node": "20 || >=22" + } + }, + "node_modules/test-exclude/node_modules/minimatch": { + "version": "10.2.6", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-10.2.6.tgz", + "integrity": "sha1-/ZVrvgt3JB6fFaxdzLHGOAYJaO8=", + "dev": true, + "license": "BlueOak-1.0.0", + "dependencies": { + "brace-expansion": "^5.0.8" + }, + "engines": { + "node": "18 || 20 || >=22" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/test-exclude/node_modules/path-scurry": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/path-scurry/-/path-scurry-2.0.2.tgz", + "integrity": "sha1-a+DQ7gKhDZ4N56mLrmXhgskGH4U=", + "dev": true, + "license": "BlueOak-1.0.0", + "dependencies": { + "lru-cache": "^11.0.0", + "minipass": "^7.1.2" + }, + "engines": { + "node": "18 || 20 || >=22" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, "node_modules/time-zone": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/time-zone/-/time-zone-1.0.0.tgz", @@ -5256,6 +5672,21 @@ "dev": true, "license": "ISC" }, + "node_modules/v8-to-istanbul": { + "version": "9.3.0", + "resolved": "https://registry.npmjs.org/v8-to-istanbul/-/v8-to-istanbul-9.3.0.tgz", + "integrity": "sha1-uVcqv6Yr1VbBbXX968GkEdX/MXU=", + "dev": true, + "license": "ISC", + "dependencies": { + "@jridgewell/trace-mapping": "^0.3.12", + "@types/istanbul-lib-coverage": "^2.0.1", + "convert-source-map": "^2.0.0" + }, + "engines": { + "node": ">=10.12.0" + } + }, "node_modules/webidl-conversions": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", @@ -5541,6 +5972,19 @@ "engines": { "node": ">=8" } + }, + "node_modules/yocto-queue": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", + "integrity": "sha1-ApTrPe4FAo0x7hpfosVWpqrxChs=", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } } } } diff --git a/bindings/js/package.json b/bindings/js/package.json index 327b84df..c044f491 100644 --- a/bindings/js/package.json +++ b/bindings/js/package.json @@ -92,6 +92,7 @@ "@taplo/cli": "^0.7.0", "@tybys/wasm-util": "^0.10.0", "ava": "^6.4.1", + "c8": "12.0.0", "chalk": "^5.6.2", "emnapi": "^1.5.0", "husky": "^9.1.7", diff --git a/eng/coverage/coverage.ps1 b/eng/coverage/coverage.ps1 new file mode 100644 index 00000000..cfebeddd --- /dev/null +++ b/eng/coverage/coverage.ps1 @@ -0,0 +1,640 @@ +#!/usr/bin/env pwsh +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +param( + [string]$OutputDirectory = "artifacts\coverage", + [string]$Python, + [string]$CargoTarget, + [string]$Win32Winmd = $env:DYNWINRT_WIN32_WINMD, + [switch]$SkipE2E, + [switch]$SkipCom +) + +$ErrorActionPreference = "Stop" +$outputSyntax = $OutputDirectory.Replace("/", "\") +if ( + $outputSyntax.StartsWith("\\?\") -or + $outputSyntax.StartsWith("\\.\") -or + $outputSyntax.StartsWith("\??\") +) { + throw "OutputDirectory cannot use Windows device-path syntax" +} +$root = (Resolve-Path (Join-Path $PSScriptRoot "..\..")).Path +$output = if ([IO.Path]::IsPathRooted($OutputDirectory)) { + [IO.Path]::GetFullPath($OutputDirectory) +} else { + [IO.Path]::GetFullPath((Join-Path $root $OutputDirectory)) +} +$rustReport = Join-Path $output "rust" +$pythonReport = Join-Path $output "python" +$jsReport = Join-Path $output "javascript" +$jsTemp = Join-Path $output "raw\javascript" +$rustRaw = Join-Path $output "raw\rust" +$pythonConfig = Join-Path $PSScriptRoot "python-coveragerc" +$repositoryArtifactRoot = Join-Path $root "artifacts" +$outputMarker = Join-Path $output ".dynwinrt-coverage-output" +$outputMarkerText = "Managed by eng/coverage/coverage.ps1" +$pipelineError = $null +$pythonStartup = $null +$originalLocation = (Get-Location).Path +$scratchRoot = Join-Path ([IO.Path]::GetTempPath()) "dynwinrt-coverage-$PID-$([guid]::NewGuid().ToString('N'))" +$pythonEnvironment = Join-Path $scratchRoot "python" +$jsDist = Join-Path $root "bindings\js\dist" +$jsDistBackup = Join-Path $scratchRoot "js-dist" +$jsDistPrepared = $false +$jsDistWasPresent = $false +$e2eGenerated = Join-Path $root "tests\e2e\e2e_generated" +$e2eGeneratedByCoverage = $false +$originalEnvironment = @{} +Get-ChildItem Env: | ForEach-Object { + $originalEnvironment[$_.Name] = $_.Value +} + +function Get-NormalizedPath { + param([string]$Path) + return [IO.Path]::GetFullPath($Path).TrimEnd( + [IO.Path]::DirectorySeparatorChar, + [IO.Path]::AltDirectorySeparatorChar + ) +} + +function Test-PathContains { + param( + [string]$Container, + [string]$Contained + ) + $containerPath = Get-NormalizedPath $Container + $containedPath = Get-NormalizedPath $Contained + return ( + $containedPath.Equals($containerPath, [StringComparison]::OrdinalIgnoreCase) -or + $containedPath.StartsWith( + "$containerPath$([IO.Path]::DirectorySeparatorChar)", + [StringComparison]::OrdinalIgnoreCase + ) + ) +} + +function Assert-NoReparsePoint { + param([string]$Path) + $current = [IO.Path]::GetFullPath($Path) + while ($current) { + if (Test-Path -LiteralPath $current) { + $item = Get-Item -LiteralPath $current -Force + if (($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) { + throw "OutputDirectory cannot traverse a reparse point: $current" + } + } + $parent = [IO.Directory]::GetParent($current) + $current = if ($parent) { $parent.FullName } else { $null } + } +} + +function Assert-NoReparsePointTree { + param([string]$Path) + if (-not (Test-Path -LiteralPath $Path)) { + return + } + $items = @( + Get-Item -LiteralPath $Path -Force + Get-ChildItem -LiteralPath $Path -Force -Recurse + ) + foreach ($item in $items) { + if (($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) { + throw "Refusing to clean a report tree containing a reparse point: $($item.FullName)" + } + } +} + +Assert-NoReparsePoint $output +$outputCandidates = @($output) +if (Test-Path -LiteralPath $output) { + $outputCandidates += (Resolve-Path -LiteralPath $output).Path +} +foreach ($candidate in ($outputCandidates | Select-Object -Unique)) { + $filesystemRoot = [IO.Path]::GetPathRoot($candidate) + if ((Get-NormalizedPath $candidate).Equals( + (Get-NormalizedPath $filesystemRoot), + [StringComparison]::OrdinalIgnoreCase + )) { + throw "OutputDirectory cannot be a filesystem root: $candidate" + } + foreach ($protectedPath in @($originalLocation, $root)) { + if (Test-PathContains $candidate $protectedPath) { + throw "OutputDirectory cannot contain protected directory: $protectedPath" + } + } + if ( + (Test-PathContains $root $candidate) -and + -not (Test-PathContains $repositoryArtifactRoot $candidate) + ) { + throw "OutputDirectory inside the repository must be under: $repositoryArtifactRoot" + } +} + +function Invoke-Step { + param( + [string]$Name, + [scriptblock]$Action + ) + Write-Host "`n=== $Name ===" -ForegroundColor Cyan + & $Action + if ($LASTEXITCODE -ne 0) { + throw "$Name failed with exit code $LASTEXITCODE" + } +} + +function New-LiteralDirectory { + param([string[]]$Path) + foreach ($directory in $Path) { + [void][IO.Directory]::CreateDirectory($directory) + } +} + +function Resolve-Python { + if ($Python) { + return (Resolve-Path -LiteralPath $Python).Path + } + return (Get-Command python -ErrorAction Stop).Source +} + +function Prepare-JavaScriptOutput { + New-LiteralDirectory $scratchRoot + if (Test-Path -LiteralPath $jsDist) { + Move-Item -LiteralPath $jsDist -Destination $jsDistBackup + $script:jsDistWasPresent = $true + } + $script:jsDistPrepared = $true +} + +function Restore-JavaScriptOutput { + if (-not $jsDistPrepared) { + return + } + if (Test-Path -LiteralPath $jsDist) { + Remove-Item -LiteralPath $jsDist -Recurse -Force + } + if ($jsDistWasPresent) { + Move-Item -LiteralPath $jsDistBackup -Destination $jsDist + } + $script:jsDistPrepared = $false +} + +function Stop-PythonCoverage { + if ($pythonStartup -and (Test-Path -LiteralPath $pythonStartup)) { + Remove-Item -LiteralPath $pythonStartup -Force + } + Remove-Item Env:COVERAGE_PROCESS_START -ErrorAction SilentlyContinue +} + +function Restore-ProcessState { + Get-ChildItem Env: | ForEach-Object { + if (-not $originalEnvironment.ContainsKey($_.Name)) { + [Environment]::SetEnvironmentVariable($_.Name, $null, "Process") + } + } + foreach ($entry in $originalEnvironment.GetEnumerator()) { + [Environment]::SetEnvironmentVariable( + $entry.Key, + [string]$entry.Value, + "Process" + ) + } + Set-Location $originalLocation +} + +function Write-Reports { + Stop-PythonCoverage + Remove-Item Env:NODE_V8_COVERAGE -ErrorAction SilentlyContinue + + $profileDirectory = Split-Path -Parent $env:LLVM_PROFILE_FILE + $profiles = @( + Get-ChildItem -LiteralPath $profileDirectory -Filter "*.profraw" -File -ErrorAction SilentlyContinue + ) + if ($profiles.Count -gt 0) { + New-LiteralDirectory @($rustReport, $rustRaw) + Copy-Item -LiteralPath $profiles.FullName -Destination $rustRaw -Force + + Invoke-Step "Rust HTML coverage report" { + & cargo llvm-cov report --profile coverage --target $script:cargoTarget --failure-mode any ` + --html --output-dir $rustReport + } + Invoke-Step "Rust LCOV coverage report" { + & cargo llvm-cov report --profile coverage --target $script:cargoTarget --failure-mode any ` + --lcov --output-path (Join-Path $rustReport "lcov.info") + } + Invoke-Step "Rust Cobertura coverage report" { + & cargo llvm-cov report --profile coverage --target $script:cargoTarget --failure-mode any ` + --cobertura --output-path (Join-Path $rustReport "cobertura.xml") + } + Invoke-Step "Rust JSON coverage summary" { + & cargo llvm-cov report --profile coverage --target $script:cargoTarget --failure-mode any ` + --json --summary-only --output-path (Join-Path $rustReport "summary.json") + } + + $lcov = Get-Content -LiteralPath (Join-Path $rustReport "lcov.info") -Raw + foreach ($requiredSource in @( + "bindings/js/src/lib.rs", + "bindings/py/src/runtime.rs" + )) { + $pattern = [regex]::Escape($requiredSource).Replace("/", "[\\/]") + if ($lcov -notmatch $pattern) { + throw "Rust coverage did not include native binding source: $requiredSource" + } + } + } else { + throw "No Rust .profraw files were produced" + } + + $pythonData = @(Get-ChildItem -LiteralPath $pythonReport -Filter ".coverage.*" -File -ErrorAction SilentlyContinue) + if ($pythonData.Count -gt 0) { + Invoke-Step "Combine Python coverage" { + & $script:pythonExe -m coverage combine --keep $pythonReport + } + $allPythonData = Join-Path $pythonReport "all-data.json" + & $script:pythonExe -m coverage json --ignore-errors -o $allPythonData + if ($LASTEXITCODE -ne 0) { + throw "Inspecting Python coverage data failed" + } + $coverageJson = Get-Content -LiteralPath $allPythonData -Raw | ConvertFrom-Json -AsHashtable + $measuredFiles = @($coverageJson["files"].Keys) + $productFiles = @($measuredFiles | Where-Object { + $_ -match "tests[\\/]e2e[\\/]e2e_generated[\\/]python_bindings" -or + ($_ -match "dynwinrt_py" -and $_ -notmatch "bindings[\\/]py[\\/]tests") + }) + if ($productFiles.Count -gt 0) { + Invoke-Step "Python HTML coverage report" { + & $script:pythonExe -m coverage html --rcfile $pythonConfig ` + -d (Join-Path $pythonReport "html") + } + Invoke-Step "Python XML coverage report" { + & $script:pythonExe -m coverage xml --rcfile $pythonConfig ` + -o (Join-Path $pythonReport "coverage.xml") + } + Invoke-Step "Python LCOV coverage report" { + & $script:pythonExe -m coverage lcov --rcfile $pythonConfig ` + -o (Join-Path $pythonReport "lcov.info") + } + Invoke-Step "Python JSON coverage summary" { + & $script:pythonExe -m coverage json --rcfile $pythonConfig ` + -o (Join-Path $pythonReport "coverage.json") + } + & $script:pythonExe -m coverage report --rcfile $pythonConfig + } else { + if ($SkipE2E) { + Write-Warning "No generated Python projection files were measured; skipping Python reports" + } else { + throw "No generated Python projection files were measured" + } + } + } else { + throw "No Python coverage data was produced" + } + + $jsData = @(Get-ChildItem -LiteralPath $jsTemp -Filter "*.json" -File -ErrorAction SilentlyContinue) + if ($jsData.Count -gt 0) { + $c8 = Join-Path $root "bindings\js\node_modules\.bin\c8.cmd" + $jsIncludes = @("--include", "bindings/js/dist/**/*.js") + if (-not $SkipE2E) { + $jsIncludes += @("--include", "tests/e2e/e2e_generated/**/*.js") + } + Invoke-Step "JavaScript coverage reports" { + & $c8 report ` + --temp-directory $jsTemp ` + --reports-dir $jsReport ` + --reporter text-summary ` + --reporter html ` + --reporter lcov ` + --reporter json-summary ` + --all ` + @jsIncludes ` + --exclude "node_modules/**" + } + $jsLcov = Get-Content -LiteralPath (Join-Path $jsReport "lcov.info") -Raw + if ($jsLcov -notmatch "bindings[\\/]js[\\/]dist[\\/]index\.js") { + throw "JavaScript coverage did not include the runtime entrypoint" + } + if (-not $SkipE2E -and $jsLcov -notmatch "tests[\\/]e2e[\\/]e2e_generated") { + throw "JavaScript coverage did not include generated E2E projections" + } + } else { + throw "No V8 coverage data was produced" + } +} + +function Write-CoverageSummary { + $rows = @() + + $rustSummary = Join-Path $rustReport "summary.json" + if (Test-Path -LiteralPath $rustSummary) { + $totals = (Get-Content -LiteralPath $rustSummary -Raw | ConvertFrom-Json).data[0].totals + $rows += "| Rust, including native .pyd/.node | $([math]::Round($totals.lines.percent, 2))% | $([math]::Round($totals.functions.percent, 2))% | $([math]::Round($totals.regions.percent, 2))% regions |" + } + + $pythonSummary = Join-Path $pythonReport "coverage.json" + if (Test-Path -LiteralPath $pythonSummary) { + $totals = (Get-Content -LiteralPath $pythonSummary -Raw | ConvertFrom-Json -AsHashtable)["totals"] + $branchPercent = if ($totals["num_branches"]) { + [math]::Round(100 * $totals["covered_branches"] / $totals["num_branches"], 2) + } else { + 100 + } + $rows += "| Generated Python projections | $([math]::Round($totals["percent_covered"], 2))% | n/a | $branchPercent% branches |" + } + + $jsSummary = Join-Path $jsReport "coverage-summary.json" + if (Test-Path -LiteralPath $jsSummary) { + $totals = (Get-Content -LiteralPath $jsSummary -Raw | ConvertFrom-Json).total + $rows += "| JavaScript runtime and generated projections | $($totals.lines.pct)% | $($totals.functions.pct)% | $($totals.branches.pct)% branches |" + } + + if ($rows.Count -gt 0) { + $summary = @( + "# Mixed-language coverage" + "" + "| Layer | Lines | Functions | Branches/regions |" + "| --- | ---: | ---: | ---: |" + ) + $rows + Set-Content -LiteralPath (Join-Path $output "summary.md") -Value $summary -Encoding utf8 + } +} + +$scriptError = $null +try { + Set-Location $root + if (-not $SkipE2E) { + $e2eGeneratedByCoverage = $true + if (Test-Path -LiteralPath $e2eGenerated) { + Remove-Item -LiteralPath $e2eGenerated -Recurse -Force + } + } + if (Test-Path -LiteralPath $output -PathType Leaf) { + throw "OutputDirectory must be a directory: $output" + } + $hasValidOutputMarker = $false + if (Test-Path -LiteralPath $outputMarker) { + $marker = Get-Item -LiteralPath $outputMarker -Force + if ( + $marker.PSIsContainer -or + (($marker.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) + ) { + throw "Coverage output marker must be a regular file: $outputMarker" + } + $markerContent = [string](Get-Content -LiteralPath $outputMarker -Raw) + $hasValidOutputMarker = $markerContent.Trim() -eq $outputMarkerText + } + if (Test-Path -LiteralPath $output) { + $existingOutput = @(Get-ChildItem -LiteralPath $output -Force) + if ( + $existingOutput.Count -gt 0 -and + -not $hasValidOutputMarker -and + -not (Test-PathContains $repositoryArtifactRoot $output) + ) { + throw "Refusing to clean a non-empty, unmanaged output directory: $output" + } + } else { + New-LiteralDirectory $output + } + Set-Content -LiteralPath $outputMarker -Value $outputMarkerText -Encoding ascii + foreach ($ownedOutput in @( + $rustReport, + $pythonReport, + $jsReport, + (Join-Path $output "raw"), + (Join-Path $output "summary.md") + )) { + if (Test-Path -LiteralPath $ownedOutput) { + Assert-NoReparsePointTree $ownedOutput + Remove-Item -LiteralPath $ownedOutput -Recurse -Force + } + } + New-LiteralDirectory @($rustReport, $rustRaw, $pythonReport, $jsTemp) + $env:LLVM_PROFILE_FILE = Join-Path $rustRaw "dynwinrt-%p-%m.profraw" + + $basePython = Resolve-Python + New-LiteralDirectory $scratchRoot + Invoke-Step "Create isolated Python coverage environment" { + & $basePython -m venv $pythonEnvironment + } + $script:pythonExe = (Resolve-Path -LiteralPath (Join-Path $pythonEnvironment "Scripts\python.exe")).Path + $env:VIRTUAL_ENV = $pythonEnvironment + $env:PATH = "$(Join-Path $pythonEnvironment 'Scripts');$env:PATH" + $script:cargoTarget = if ($CargoTarget) { + $CargoTarget + } else { + $hostLine = & rustc -vV | Where-Object { $_ -like "host:*" } + if (-not $hostLine) { throw "Could not determine the Rust host target" } + ($hostLine -split ":", 2)[1].Trim() + } + + try { + if (-not (Get-Command cargo-llvm-cov -ErrorAction SilentlyContinue)) { + throw "cargo-llvm-cov 0.8.7 is required. Run: cargo install cargo-llvm-cov --version 0.8.7 --locked" + } + if (-not (Get-Command node -ErrorAction SilentlyContinue)) { + throw "Node.js is required" + } + + Invoke-Step "Upgrade isolated Python pip" { + & $script:pythonExe -m pip install "pip>=25.1,<27" --quiet + } + Invoke-Step "Install Python coverage dependencies" { + & $script:pythonExe -m pip install ` + "coverage[toml]>=7.15,<8" ` + "maturin>=1.11,<2" ` + "mypy>=1.13,<2" ` + "pytest>=8.3.5" ` + --quiet + } + Invoke-Step "Install JavaScript dependencies" { + Push-Location (Join-Path $root "bindings\js") + try { + & npm ci --no-audit --no-fund + } finally { + Pop-Location + } + } + + if ($Win32Winmd) { + $resolvedWinmd = (Resolve-Path -LiteralPath $Win32Winmd).Path + $env:DYNWINRT_WIN32_WINMD = $resolvedWinmd + $env:DYNWINRT_REQUIRE_WIN32_METADATA = "1" + } + + $env:CARGO_INCREMENTAL = "0" + $env:LLVM_PROFILE_FILE_NAME = "dynwinrt-%p-%m.profraw" + $coverageEnvironment = ( + & cargo llvm-cov show-env --pwsh --target $script:cargoTarget | Out-String + ) + if ($LASTEXITCODE -ne 0) { + throw "cargo llvm-cov show-env failed" + } + Invoke-Expression $coverageEnvironment + + Invoke-Step "Clean previous Rust coverage data" { + & cargo llvm-cov clean --workspace + } + Invoke-Step "Rust workspace tests" { + & cargo test ` + -p dynwinrt ` + -p dynwinrt-codegen ` + -p dynwinrt-py ` + -p jswinrt_rs ` + --profile coverage ` + --target $script:cargoTarget + } + Invoke-Step "Build instrumented Python binding" { + Push-Location (Join-Path $root "bindings\py") + try { + & $script:pythonExe -m maturin develop --profile coverage ` + --target $script:cargoTarget + } finally { + Pop-Location + } + } + Invoke-Step "Build instrumented JavaScript binding" { + Prepare-JavaScriptOutput + Push-Location (Join-Path $root "bindings\js") + try { + $napi = Join-Path $root "bindings\js\node_modules\.bin\napi.cmd" + if (-not (Test-Path -LiteralPath $napi)) { + throw "NAPI CLI is missing: $napi" + } + & $napi build --no-const-enum --profile coverage --platform ` + --target $script:cargoTarget -o dist + if ($LASTEXITCODE -ne 0) { return } + & npm run build:entrypoints --silent + } finally { + Pop-Location + } + } + + $env:COVERAGE_FILE = Join-Path $pythonReport ".coverage" + & $script:pythonExe -m coverage erase + if ($LASTEXITCODE -ne 0) { + throw "coverage erase failed" + } + $env:COVERAGE_PROCESS_START = $pythonConfig + $sitePackages = ( + & $script:pythonExe -c "import sysconfig; print(sysconfig.get_path('purelib'))" + ).Trim() + $pythonStartup = Join-Path $sitePackages "dynwinrt_coverage.pth" + Set-Content -LiteralPath $pythonStartup -Value "import coverage; coverage.process_startup()" -Encoding ascii + + $env:NODE_V8_COVERAGE = $jsTemp + + Invoke-Step "Python binding tests" { + & $script:pythonExe -m pytest bindings\py\tests -q + } + Invoke-Step "Python stubtest" { + & $script:pythonExe -m mypy.stubtest dynwinrt_py ` + --allowlist bindings\py\stubtest_allowlist.txt ` + --ignore-disjoint-bases + } + Invoke-Step "JavaScript binding tests" { + Push-Location (Join-Path $root "bindings\js") + try { + & npm test -- --no-color + } finally { + Pop-Location + } + } + + if (-not $SkipE2E) { + $languages = @("py", "ts") + if (-not $SkipCom -and $env:DYNWINRT_WIN32_WINMD) { + $languages += "com" + } + Invoke-Step "Cross-language E2E tests" { + & (Join-Path $root "tests\e2e\e2e_test.ps1") ` + -SkipBuild ` + -KeepGenerated ` + -CargoProfile coverage ` + -CargoTarget $script:cargoTarget ` + -Python $script:pythonExe ` + -Lang $languages + } + } + } catch { + $pipelineError = $_ + Write-Warning "Coverage test pipeline failed: $($_.Exception.Message)" + } finally { + try { + Write-Reports + } catch { + if (-not $pipelineError) { + $pipelineError = $_ + } else { + Write-Warning "Coverage report generation also failed: $($_.Exception.Message)" + } + } + try { + Stop-PythonCoverage + } catch { + if (-not $pipelineError) { + $pipelineError = $_ + } else { + Write-Warning "Coverage cleanup also failed: $($_.Exception.Message)" + } + } + try { + Write-CoverageSummary + } catch { + if (-not $pipelineError) { + $pipelineError = $_ + } else { + Write-Warning "Coverage summary generation also failed: $($_.Exception.Message)" + } + } + } + + if ($pipelineError) { + throw $pipelineError + } + + Write-Host "`nCoverage reports:" -ForegroundColor Green + Write-Host " Rust: $(Join-Path $rustReport 'html\index.html')" + if (Test-Path -LiteralPath (Join-Path $pythonReport "html\index.html")) { + Write-Host " Python: $(Join-Path $pythonReport 'html\index.html')" + } + Write-Host " JavaScript: $(Join-Path $jsReport 'index.html')" +} catch { + $scriptError = $_ +} finally { + try { + Restore-JavaScriptOutput + } catch { + if (-not $scriptError) { + $scriptError = $_ + } else { + Write-Warning "Restoring the JavaScript output also failed: $($_.Exception.Message)" + } + } + try { + if ($e2eGeneratedByCoverage -and (Test-Path -LiteralPath $e2eGenerated)) { + Remove-Item -LiteralPath $e2eGenerated -Recurse -Force + } + if (Test-Path -LiteralPath $scratchRoot) { + Remove-Item -LiteralPath $scratchRoot -Recurse -Force + } + } catch { + if (-not $scriptError) { + $scriptError = $_ + } else { + Write-Warning "Removing temporary coverage files also failed: $($_.Exception.Message)" + } + } + try { + Restore-ProcessState + } catch { + if (-not $scriptError) { + $scriptError = $_ + } else { + Write-Warning "Restoring the caller process state also failed: $($_.Exception.Message)" + } + } +} + +if ($scriptError) { + throw $scriptError +} diff --git a/eng/coverage/python-coveragerc b/eng/coverage/python-coveragerc new file mode 100644 index 00000000..f9f23c4e --- /dev/null +++ b/eng/coverage/python-coveragerc @@ -0,0 +1,20 @@ +[run] +branch = True +parallel = True +relative_files = True + +[report] +include = + */dynwinrt_py/* + tests/e2e/e2e_generated/python_bindings/* +omit = + */__pycache__/* + */site-packages/coverage/* + bindings/py/tests/* + tests/e2e/runners/* + tests/e2e/typecheck/* +show_missing = True +skip_empty = True + +[html] +show_contexts = True diff --git a/tests/e2e/e2e_test.ps1 b/tests/e2e/e2e_test.ps1 index 9e3ba82f..20e86376 100644 --- a/tests/e2e/e2e_test.ps1 +++ b/tests/e2e/e2e_test.ps1 @@ -14,6 +14,10 @@ param( [switch]$SkipBuild, + [switch]$KeepGenerated, + [string]$CargoProfile = "release", + [string]$CargoTarget, + [string]$Python, [ValidateSet("py", "ts", "com")] [string[]]$Lang = @("py", "ts", "com") ) @@ -31,6 +35,20 @@ $comInteropDir = Join-Path $comBindingsDir "interop" $comWicDir = Join-Path $comBindingsDir "wic" $comStreamDir = Join-Path $comBindingsDir "stream" $comSmtcDir = Join-Path $comBindingsDir "smtc" +[string[]]$cargoProfileArgs = @( + if ($CargoProfile -eq "release") { + "--release" + } else { + "--profile" + $CargoProfile + } +) +[string[]]$cargoTargetArgs = @( + if ($CargoTarget) { + "--target" + $CargoTarget + } +) $env:PATH = "$env:USERPROFILE\.cargo\bin;$env:PATH" @@ -39,7 +57,12 @@ Write-Host "=== dynwinrt E2E Test ===" -ForegroundColor Cyan # -------------------------------------------------------------------------- # Detect available tools # -------------------------------------------------------------------------- -$hasPython = [bool](Get-Command python -ErrorAction SilentlyContinue) +$pythonExe = if ($Python) { + (Resolve-Path -LiteralPath $Python).Path +} else { + (Get-Command python -ErrorAction SilentlyContinue).Source +} +$hasPython = [bool]$pythonExe $hasNode = [bool](Get-Command node -ErrorAction SilentlyContinue) if ("py" -in $Lang -and -not $hasPython) { @@ -53,7 +76,7 @@ if (("ts" -in $Lang -or "com" -in $Lang) -and -not $hasNode) { function Find-Win32Winmd { if ($env:DYNWINRT_WIN32_WINMD -and (Test-Path $env:DYNWINRT_WIN32_WINMD)) { - return (Resolve-Path $env:DYNWINRT_WIN32_WINMD).Path + return (Resolve-Path -LiteralPath $env:DYNWINRT_WIN32_WINMD).Path } $packageRoot = Join-Path $env:USERPROFILE ".nuget\packages\microsoft.windows.sdk.win32metadata" @@ -93,24 +116,35 @@ if ($Lang.Count -eq 0) { Write-Error "No languages available"; exit 1 } if (-not $SkipBuild) { Write-Host "`n--- Build ---" -ForegroundColor Yellow - cargo build -p dynwinrt-codegen --release + & cargo build -p dynwinrt-codegen @cargoProfileArgs @cargoTargetArgs if ($LASTEXITCODE -ne 0) { Write-Error "dynwinrt-codegen build failed"; exit 1 } if ("py" -in $Lang) { Push-Location (Join-Path $root "bindings\py") - if (-not (Test-Path .venv)) { - python -m venv .venv - .\.venv\Scripts\Activate.ps1 - pip install pytest maturin --quiet - } else { - .\.venv\Scripts\Activate.ps1 + if (-not $Python) { + $venvPython = Join-Path (Get-Location) ".venv\Scripts\python.exe" + if (-not (Test-Path $venvPython)) { + & $pythonExe -m venv .venv + if ($LASTEXITCODE -ne 0) { Write-Error "Python virtual environment creation failed"; exit 1 } + & $venvPython -m pip install pytest maturin --quiet + if ($LASTEXITCODE -ne 0) { Write-Error "Python test dependency installation failed"; exit 1 } + } + $pythonExe = (Resolve-Path -LiteralPath $venvPython).Path } - maturin build --quiet 2>&1 | Out-Null + [string[]]$maturinProfileArgs = @( + if ($CargoProfile -eq "release") { + "--release" + } else { + "--profile" + $CargoProfile + } + ) + & $pythonExe -m maturin build @maturinProfileArgs @cargoTargetArgs --quiet 2>&1 | Out-Null if ($LASTEXITCODE -ne 0) { Write-Error "maturin build failed"; exit 1 } $wheelDir = if ($env:CARGO_TARGET_DIR) { Join-Path $env:CARGO_TARGET_DIR "wheels" } else { Join-Path $root "target\wheels" } $whl = (Get-ChildItem (Join-Path $wheelDir "*.whl") | Sort-Object LastWriteTime -Descending | Select-Object -First 1).FullName if (-not $whl) { Write-Error "No wheel found after maturin build"; exit 1 } - pip install $whl --force-reinstall --quiet 2>&1 | Out-Null + & $pythonExe -m pip install $whl --force-reinstall --quiet 2>&1 | Out-Null if ($LASTEXITCODE -ne 0) { Write-Error "pip install failed"; exit 1 } Pop-Location } @@ -119,15 +153,19 @@ if (-not $SkipBuild) { Push-Location (Join-Path $root "bindings\js") npm install --quiet 2>&1 | Out-Null if ($LASTEXITCODE -ne 0) { Write-Error "npm install failed"; exit 1 } - npx napi build --no-const-enum --platform --release -o dist 2>&1 | Out-Null + $napi = Join-Path $root "bindings\js\node_modules\.bin\napi.cmd" + if (-not (Test-Path -LiteralPath $napi)) { Write-Error "NAPI CLI is missing: $napi"; exit 1 } + & $napi build --no-const-enum --platform @cargoProfileArgs @cargoTargetArgs -o dist 2>&1 | Out-Null if ($LASTEXITCODE -ne 0) { Write-Error "napi build failed"; exit 1 } npm run build:entrypoints --silent if ($LASTEXITCODE -ne 0) { Write-Error "runtime entrypoint generation failed"; exit 1 } Pop-Location } } else { - $venvActivate = Join-Path $root "bindings\py\.venv\Scripts\Activate.ps1" - if (Test-Path $venvActivate) { & $venvActivate } + $venvPython = Join-Path $root "bindings\py\.venv\Scripts\python.exe" + if (-not $Python -and (Test-Path $venvPython)) { + $pythonExe = (Resolve-Path -LiteralPath $venvPython).Path + } } # -------------------------------------------------------------------------- @@ -148,8 +186,6 @@ if ($skipped) { # -------------------------------------------------------------------------- if (Test-Path $e2eDir) { Remove-Item -Recurse -Force $e2eDir } -$winrtMeta = "cargo run -p dynwinrt-codegen --release --quiet --" - function Generate($lang, $outDir) { $langSpecs = $active | Where-Object { ($(if ($_.langs) { $_.langs } else { @("py","ts") })) -contains $lang } # codegen now uses "js" instead of "ts" — map accordingly @@ -164,7 +200,11 @@ function Generate($lang, $outDir) { foreach ($ns in ($byNs.Keys | Sort-Object)) { $classes = ($byNs[$ns] | Select-Object -Unique) -join "," Write-Host " $lang`: $ns [$classes]" - Invoke-Expression "$winrtMeta generate --namespace `"$ns`" --class-name `"$classes`" --lang $codegenLang --output `"$outDir`"" + & cargo run -p dynwinrt-codegen @cargoProfileArgs @cargoTargetArgs --quiet -- generate ` + --namespace $ns ` + --class-name $classes ` + --lang $codegenLang ` + --output $outDir if ($LASTEXITCODE -ne 0) { Write-Error "Generation failed: $ns ($lang)"; exit 1 } } } @@ -180,7 +220,7 @@ if ("com" -in $Lang) { $comRuntimeImport = "../../../../../../bindings/js/dist/com-unsafe.js" $winrtRuntimeImport = "../../../../../bindings/js/dist/winrt.js" - & cargo run -p dynwinrt-codegen --release --quiet -- generate ` + & cargo run -p dynwinrt-codegen @cargoProfileArgs @cargoTargetArgs --quiet -- generate ` --winmd $win32Winmd ` --namespace Windows.Win32.UI.Shell ` --class-name "TaskbarList,IShellLinkW,IDataTransferManagerInterop,FileOperation,FileOpenDialog" ` @@ -188,7 +228,7 @@ if ("com" -in $Lang) { --import-name $comRuntimeImport if ($LASTEXITCODE -ne 0) { Write-Error "Classic COM Shell generation failed"; exit 1 } - & cargo run -p dynwinrt-codegen --release --quiet -- generate ` + & cargo run -p dynwinrt-codegen @cargoProfileArgs @cargoTargetArgs --quiet -- generate ` --winmd $win32Winmd ` --namespace Windows.Win32.System.Com ` --class-name IPersistFile ` @@ -196,7 +236,7 @@ if ("com" -in $Lang) { --import-name $comRuntimeImport if ($LASTEXITCODE -ne 0) { Write-Error "Classic COM persistence generation failed"; exit 1 } - & cargo run -p dynwinrt-codegen --release --quiet -- generate ` + & cargo run -p dynwinrt-codegen @cargoProfileArgs @cargoTargetArgs --quiet -- generate ` --winmd $win32Winmd ` --namespace Windows.Win32.System.WinRT ` --class-name ISystemMediaTransportControlsInterop ` @@ -204,7 +244,7 @@ if ("com" -in $Lang) { --import-name $comRuntimeImport if ($LASTEXITCODE -ne 0) { Write-Error "Classic COM interop generation failed"; exit 1 } - & cargo run -p dynwinrt-codegen --release --quiet -- generate ` + & cargo run -p dynwinrt-codegen @cargoProfileArgs @cargoTargetArgs --quiet -- generate ` --winmd $win32Winmd ` --namespace Windows.Win32.Graphics.Imaging ` --class-name IWICImagingFactory ` @@ -212,7 +252,7 @@ if ("com" -in $Lang) { --import-name $comRuntimeImport if ($LASTEXITCODE -ne 0) { Write-Error "Classic COM WIC generation failed"; exit 1 } - & cargo run -p dynwinrt-codegen --release --quiet -- generate ` + & cargo run -p dynwinrt-codegen @cargoProfileArgs @cargoTargetArgs --quiet -- generate ` --winmd $win32Winmd ` --namespace Windows.Win32.System.Com ` --class-name ISequentialStream ` @@ -220,7 +260,7 @@ if ("com" -in $Lang) { --import-name $comRuntimeImport if ($LASTEXITCODE -ne 0) { Write-Error "Classic COM stream generation failed"; exit 1 } - & cargo run -p dynwinrt-codegen --release --quiet -- generate ` + & cargo run -p dynwinrt-codegen @cargoProfileArgs @cargoTargetArgs --quiet -- generate ` --namespace Windows.Media ` --class-name SystemMediaTransportControls ` --output $comSmtcDir ` @@ -240,7 +280,7 @@ if ("py" -in $Lang) { $previousMypyPath = $env:MYPYPATH try { $env:MYPYPATH = $e2eDir - python -m mypy --strict (Join-Path $PSScriptRoot "typecheck\python_generated_api.py") + & $pythonExe -m mypy --strict (Join-Path $PSScriptRoot "typecheck\python_generated_api.py") if ($LASTEXITCODE -ne 0) { Write-Error "Python static type check failed"; exit 1 } } finally { $env:MYPYPATH = $previousMypyPath @@ -248,7 +288,7 @@ if ("py" -in $Lang) { Write-Host "`n--- Python E2E ---" -ForegroundColor Yellow $pyResult = Join-Path $e2eDir "results_py.json" - python (Join-Path $runnersDir "py_runner.py") ` + & $pythonExe (Join-Path $runnersDir "py_runner.py") ` --specs $specsFile ` --generated $pyBindingsDir ` --output $pyResult @@ -317,7 +357,9 @@ foreach ($r in $allResults) { } if ($totalFail -eq 0) { Write-Host "ALL PASSED" -ForegroundColor Green - Remove-Item -Recurse -Force $e2eDir + if (-not $KeepGenerated) { + Remove-Item -Recurse -Force $e2eDir + } exit 0 } else { Write-Host "SOME FAILED" -ForegroundColor Red