From 9f27cad0b6cf35e3388a7c5c4cf0fa64e7fdda78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Kr=C3=BCger?= Date: Thu, 19 Nov 2026 13:18:45 +0100 Subject: [PATCH 01/14] Add portable CLI contract tests for the Rust port Introduce a Python runner with strict output comparisons, shared offline and emulator cases, independent fixtures, and migration tracking. Wire CI and document the cross-implementation testing workflow while retaining existing C# coverage. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .github/workflows/integration-tests.yml | 101 +++++++ .gitignore | 2 + CONTRIBUTING.md | 14 + docs/cli-contract-tests.md | 196 ++++++++++++++ .../cases/emulator/batch-and-files.json | 73 +++++ tests/cli-contracts/cases/emulator/bulk.json | 21 ++ tests/cli-contracts/cases/emulator/data.json | 148 +++++++++++ .../cases/emulator/partition-keys.json | 71 +++++ .../cases/emulator/resources.json | 103 ++++++++ .../cli-contracts/cases/offline/binding.json | 56 ++++ .../cases/offline/contracts.json | 17 ++ .../cases/offline/control-flow.json | 19 ++ .../cases/offline/echo-and-files.json | 21 ++ tests/cli-contracts/cases/offline/errors.json | 28 ++ tests/cli-contracts/cases/offline/filter.json | 41 +++ tests/cli-contracts/cases/offline/import.json | 106 ++++++++ .../cli-contracts/cases/offline/startup.json | 16 ++ .../cases/offline/value-semantics.json | 79 ++++++ tests/cli-contracts/comparisons.py | 224 ++++++++++++++++ tests/cli-contracts/conftest.py | 32 +++ tests/cli-contracts/emulator.py | 249 ++++++++++++++++++ tests/cli-contracts/inventory.py | 167 ++++++++++++ tests/cli-contracts/migration-map.json | 211 +++++++++++++++ tests/cli-contracts/requirements-emulator.txt | 2 + tests/cli-contracts/requirements.txt | 3 + tests/cli-contracts/runner.py | 175 ++++++++++++ tests/cli-contracts/schema.json | 86 ++++++ tests/cli-contracts/test_comparisons.py | 179 +++++++++++++ tests/cli-contracts/test_contracts.py | 23 ++ tests/cli-contracts/test_emulator_fixtures.py | 181 +++++++++++++ tests/cli-contracts/test_inventory.py | 50 ++++ tests/cli-contracts/test_runner.py | 165 ++++++++++++ 32 files changed, 2859 insertions(+) create mode 100644 docs/cli-contract-tests.md create mode 100644 tests/cli-contracts/cases/emulator/batch-and-files.json create mode 100644 tests/cli-contracts/cases/emulator/bulk.json create mode 100644 tests/cli-contracts/cases/emulator/data.json create mode 100644 tests/cli-contracts/cases/emulator/partition-keys.json create mode 100644 tests/cli-contracts/cases/emulator/resources.json create mode 100644 tests/cli-contracts/cases/offline/binding.json create mode 100644 tests/cli-contracts/cases/offline/contracts.json create mode 100644 tests/cli-contracts/cases/offline/control-flow.json create mode 100644 tests/cli-contracts/cases/offline/echo-and-files.json create mode 100644 tests/cli-contracts/cases/offline/errors.json create mode 100644 tests/cli-contracts/cases/offline/filter.json create mode 100644 tests/cli-contracts/cases/offline/import.json create mode 100644 tests/cli-contracts/cases/offline/startup.json create mode 100644 tests/cli-contracts/cases/offline/value-semantics.json create mode 100644 tests/cli-contracts/comparisons.py create mode 100644 tests/cli-contracts/conftest.py create mode 100644 tests/cli-contracts/emulator.py create mode 100644 tests/cli-contracts/inventory.py create mode 100644 tests/cli-contracts/migration-map.json create mode 100644 tests/cli-contracts/requirements-emulator.txt create mode 100644 tests/cli-contracts/requirements.txt create mode 100644 tests/cli-contracts/runner.py create mode 100644 tests/cli-contracts/schema.json create mode 100644 tests/cli-contracts/test_comparisons.py create mode 100644 tests/cli-contracts/test_contracts.py create mode 100644 tests/cli-contracts/test_emulator_fixtures.py create mode 100644 tests/cli-contracts/test_inventory.py create mode 100644 tests/cli-contracts/test_runner.py diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml index e2e0dc9e..85907290 100644 --- a/.github/workflows/integration-tests.yml +++ b/.github/workflows/integration-tests.yml @@ -14,6 +14,57 @@ permissions: contents: read jobs: + cli-contracts: + name: Portable CLI Contracts (${{ matrix.os }}) + runs-on: ${{ matrix.os }} + timeout-minutes: 20 + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest] + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + + - uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6.1.0 + with: + global-json-file: global.json + + - uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6 + with: + python-version: '3.13' + cache: pip + cache-dependency-path: tests/cli-contracts/requirements*.txt + + - name: Build C# reference + run: dotnet build CosmosDBShell/CosmosDBShell.csproj + + - name: Install contract runner + run: python -m pip install -r tests/cli-contracts/requirements.txt + + - name: Validate migration inventory + run: python tests/cli-contracts/inventory.py --output artifacts/cli-contract-inventory.json + + - name: Run runner self-tests and offline contracts + run: >- + python -m pytest tests/cli-contracts --profile offline + --shell-executable dotnet + --shell-prefix-arg CosmosDBShell/bin/Debug/net10.0/CosmosDBShell.dll + --contract-artifacts artifacts/cli-contract-failures + --junitxml artifacts/cli-contracts.xml -q + + - name: Upload contract report and synthetic failure data + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: cli-contracts-${{ matrix.os }} + path: | + artifacts/cli-contract-inventory.json + artifacts/cli-contracts.xml + artifacts/cli-contract-failures/ + retention-days: 7 + offline-tests: name: Offline Integration Tests runs-on: ubuntu-latest @@ -101,6 +152,31 @@ jobs: # The Windows emulator job below exercises those collisions. run: dotnet test CosmosDBShell.Tests/CosmosDBShell.Tests.csproj --filter "Category=Emulator&Feature!=PartitionKeyCollisions" --no-restore -p:CosmosRequireEmulator=true + - uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6 + with: + python-version: '3.13' + + - name: Install independent emulator fixtures + run: python -m pip install -r tests/cli-contracts/requirements-emulator.txt + + - name: Run portable Linux emulator contracts + run: >- + python -m pytest tests/cli-contracts/test_contracts.py --profile emulator-linux + --shell-executable dotnet + --shell-prefix-arg CosmosDBShell/bin/Debug/net10.0/CosmosDBShell.dll + --contract-artifacts artifacts/cli-contract-failures + --junitxml artifacts/cli-contracts-linux-emulator.xml -q + + - name: Upload emulator contract report + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: cli-contracts-linux-emulator + path: | + artifacts/cli-contracts-linux-emulator.xml + artifacts/cli-contract-failures/ + retention-days: 7 + windows-partition-key-tests: name: Windows Partition-Key Integration Tests runs-on: windows-latest @@ -151,3 +227,28 @@ jobs: - name: Run partition-key integration tests run: dotnet test .\CosmosDBShell.Tests\CosmosDBShell.Tests.csproj --filter "Category=Emulator&FullyQualifiedName~PrintPartitionKeyTests" --no-restore -p:CosmosRequireEmulator=true + + - uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6 + with: + python-version: '3.13' + + - name: Install independent emulator fixtures + run: python -m pip install -r tests\cli-contracts\requirements-emulator.txt + + - name: Run portable Windows emulator contracts + run: >- + python -m pytest tests\cli-contracts\test_contracts.py --profile emulator-windows + --shell-executable dotnet + --shell-prefix-arg CosmosDBShell\bin\Debug\net10.0\CosmosDBShell.dll + --contract-artifacts artifacts\cli-contract-failures + --junitxml artifacts\cli-contracts-windows-emulator.xml -q + + - name: Upload emulator contract report + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: cli-contracts-windows-emulator + path: | + artifacts/cli-contracts-windows-emulator.xml + artifacts/cli-contract-failures/ + retention-days: 7 diff --git a/.gitignore b/.gitignore index c81c327a..e655144c 100644 --- a/.gitignore +++ b/.gitignore @@ -318,6 +318,8 @@ paket-files/ # Python Tools for Visual Studio (PTVS) __pycache__/ *.pyc +.pytest_cache/ +.venv/ # Cake - Uncomment if you are using it # tools/** diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 843fddf4..267b93d6 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -120,6 +120,20 @@ Run the offline regression suite without a database: dotnet test CosmosDBShell.Tests/CosmosDBShell.Tests.csproj --filter "Category!=Emulator" ``` +### Portable CLI Contracts + +The [language-neutral CLI suite](docs/cli-contract-tests.md) runs reviewed +scripts against an executable and compares only declared result files, process +exit codes, and independently verified emulator side effects. Add cases there +for externally observable command/script behavior so the same contracts can +validate the future Rust implementation without a .NET test host. + +Keep the existing xUnit tests for internal assertions and any behavior not yet +fully covered by portable cases. The suite's migration inventory reports links +and remaining coverage; a linked case does not imply a whole theory was migrated. +See the guide for Python setup, launcher selection, output capture, emulator +profiles, and strict comparison rules. + ### Updating Localized Text Edit `CosmosDBShell/lang/en.ftl`, then build normally: diff --git a/docs/cli-contract-tests.md b/docs/cli-contract-tests.md new file mode 100644 index 00000000..f7ce1440 --- /dev/null +++ b/docs/cli-contract-tests.md @@ -0,0 +1,196 @@ +# Portable CLI contract tests + +The [shared suite](../tests/cli-contracts) exercises an executable, not C# objects. +It is the compatibility contract for a future Rust shell. The same case IDs, +scripts, fixtures, exit codes, and expectations apply to every implementation. +No implementation switches, reduced expectations, automatic baseline updates, +`xfail`, or fallback to the C# binary are supported. + +The Rust port itself is not included. Passing the C# reference does not establish +Rust compatibility. A Rust implementation must pass every offline case and both +emulator profiles before it is considered compatible. + +## Run locally + +Use Python 3.11 or newer. The runner itself does not require .NET; building the +C# reference does. From the repository root, in PowerShell: + +```powershell +python -m venv .venv +.\.venv\Scripts\Activate.ps1 +python -m pip install -r tests\cli-contracts\requirements.txt +dotnet build CosmosDBShell\CosmosDBShell.csproj +python -m pytest tests\cli-contracts --profile offline --shell-executable dotnet --shell-prefix-arg CosmosDBShell\bin\Debug\net10.0\CosmosDBShell.dll -q +``` + +On Linux/macOS, activate `.venv/bin/activate` and use `/` in paths. +For a published or Rust executable, substitute only the launcher: + +```powershell +python -m pytest tests\cli-contracts --profile offline --shell-executable C:\path\to\cosmos-shell.exe -q +``` + +`--shell-prefix-arg` is repeatable (use `--shell-prefix-arg=-flag` for a prefix +argument beginning with `-`). Launchers are never evaluated by an OS shell. +Relative executable/assembly paths are resolved before changing directories. + +Run just the runner/comparator self-tests without building a shell: + +```powershell +python -m pytest tests\cli-contracts\test_runner.py tests\cli-contracts\test_comparisons.py -q +``` + +For emulator contracts, install the independent fixture dependencies and start +the appropriate local emulator: + +```powershell +python -m pip install -r tests\cli-contracts\requirements-emulator.txt +python -m pytest tests\cli-contracts\test_contracts.py --profile emulator-windows --shell-executable C:\path\to\cosmos-shell.exe -q +``` + +Use `--profile emulator-linux` for the Linux preview emulator. These are server +profiles, not implementation capabilities; Windows-only partition-key collision +cases must also pass for Rust against the Windows emulator. Missing executables, +an unavailable selected emulator, malformed cases, and missing output files +**fail**; they are not skipped. `--emulator-endpoint` defaults to +`https://localhost:8081` and permits only local loopback endpoints. + +The Python Azure Cosmos SDK creates fresh, uniquely named, owned databases, +seeds data independently of the shell, verifies mutations, and cleans up on +success or failure. Never point this suite at a real account or add real +credentials or production data to a case. + +## Capturing only command output + +Every case runs a fresh process in an isolated working/config directory with +closed stdin and a timeout. Related commands share a script/session. The runner +captures subprocess stdout and stderr into `stdout.txt` and `stderr.txt`. + +Structured contracts normally redirect each command's result inside the shell: + +```text +print {"id":"one","count":9007199254740993} > 'result.json' +``` + +Only declared output files are compared. An interactive prompt cannot contaminate +`result.json`; the runner never guesses or removes prompt text. A prompt, ANSI +escape, or duplicate JSON result *inside* the declared file causes a failure. + +Internal redirection bypasses the human renderer. Human/table cases therefore +use `--output user`/`--output table` without internal result redirection and +assert the captured subprocess files instead. The `-c` invocation exits without +entering the interactive editor. Startup/argument errors also use subprocess +files and assert the process exit code. + +## Case format + +[schema.json](../tests/cli-contracts/schema.json) validates every manifest before +selection. Files contain `schemaVersion: 1` and a `cases` array. IDs are unique +across the entire corpus. Source links reference existing xUnit method names. + +```json +{ + "schemaVersion": 1, + "cases": [{ + "id": "example.structured-result", + "profiles": ["offline"], + "sources": ["CosmosDBShell.Tests/Integration/OutputRedirectionTests.cs::Redirect_StdoutToFile_WritesOutput"], + "script": "print {\"id\":\"one\"} > 'result.json'", + "exitCode": 0, + "files": { + "result.json": {"mode": "json", "expected": {"id": "one"}}, + "stderr.txt": {"mode": "empty"} + } + }] +} +``` + +The example illustrates the format; use the actual method name from the source +test when adding a case. Use either `script` or raw CLI `args`; scripts get +`--output -c