diff --git a/.github/workflows/flatpak-oci.yml b/.github/workflows/flatpak-oci.yml new file mode 100644 index 0000000..05e72e9 --- /dev/null +++ b/.github/workflows/flatpak-oci.yml @@ -0,0 +1,74 @@ +name: "Flatpak OCI" + +# Reusable workflow to package and publish a Flatpak application as an OCI image to GHCR. +# Pairs with the Flatpak Registry Index Protocol implemented in artcraft-services. +on: + workflow_call: + inputs: + app-name: + description: "Application repository name (e.g. pdfcraft, cadcraft)" + type: string + required: true + version: + description: "Release version string (e.g. 0.16.1)" + type: string + required: true + registry: + description: "Target container registry repository (defaults to ghcr.io/)" + type: string + required: false + default: "" + +permissions: + contents: read + packages: write + +jobs: + publish-oci: + name: Publish OCI ${{ matrix.arch }} + runs-on: ${{ matrix.runner }} + strategy: + fail-fast: false + matrix: + include: + - { arch: x86_64, runner: ubuntu-24.04 } + - { arch: aarch64, runner: ubuntu-24.04-arm } + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: release-linux-${{ matrix.arch }} + path: dist/linux + + - name: Install Flatpak and Skopeo + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends flatpak flatpak-builder librsvg2-common skopeo + sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0 + + - name: Add Flathub Remote + run: flatpak remote-add --user --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo + + - name: Publish Flatpak OCI layout to GHCR + env: + RELEASE_VERSION: ${{ inputs.version }} + ARCH: ${{ matrix.arch }} + REGISTRY: ${{ inputs.registry || format('ghcr.io/{0}', github.repository) }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + shopt -s nullglob + tarballs=() + for f in dist/linux/*-"${RELEASE_VERSION}"-linux-"${ARCH}".tar.gz; do + case "${f##*/}" in *-cli-*|*-server-*) ;; *) tarballs+=("$f") ;; esac + done + if [ "${#tarballs[@]}" -ne 1 ]; then + echo "::error::expected one app tarball for ${RELEASE_VERSION} in release-linux-${ARCH}, found ${#tarballs[@]}: ${tarballs[*]}" + exit 1 + fi + tarball="${tarballs[0]}" + if [ -x packaging/linux/flatpak-oci.sh ]; then + packaging/linux/flatpak-oci.sh --push "$tarball" + else + echo "::warning::packaging/linux/flatpak-oci.sh not found or not executable" + fi diff --git a/.github/workflows/freebsd.yml b/.github/workflows/freebsd.yml index c01ac31..3cf787e 100644 --- a/.github/workflows/freebsd.yml +++ b/.github/workflows/freebsd.yml @@ -110,7 +110,7 @@ jobs: printf 'FreeBSD-ports: { url: "https://pkg.FreeBSD.org/${ABI}/quarterly" }\n' > /usr/local/etc/pkg/repos/FreeBSD.conf pkg update -f -q fi - pkg install -y bash git curl pkgconf cmake python3 libxkbcommon wayland libX11 libXcursor libXrandr libXi libxcb mesa-libs vulkan-loader gtk3 fontconfig freetype2 alsa-lib dejavu + pkg install -y bash git curl pkgconf cmake-core python3 libxkbcommon wayland libX11 libXcursor libXrandr libXi libxcb mesa-libs vulkan-loader gtk3 fontconfig freetype2 alsa-lib dejavu # aarch64-unknown-freebsd is a tier-3 Rust target: rustup publishes no toolchain # or standard library for it ("installer for platform not found"), so use # FreeBSD's lang/rust package. diff --git a/.github/workflows/gate.yml b/.github/workflows/gate.yml index 24e6ee4..9017b77 100644 --- a/.github/workflows/gate.yml +++ b/.github/workflows/gate.yml @@ -1,15 +1,17 @@ name: "Gate" # The quality gate the craft apps run on pull requests and pushes to main: one job per runner in -# `os`, each running `command` (by default `cargo xtask ci`) with the suite's shared setup. App-only -# jobs (fuzzing, corpora, web builds) stay in the app's own workflow next to the call. +# `os`, each running `command` (by default `cargo xtask ci`) with the suite's shared setup. +# App-specific checks (fuzzing, corpora, web builds) belong here too, as shared workflows or +# inputs: an app's workflows only call this repository's workflows. on: workflow_call: inputs: packages: - description: "Ubuntu packages the build needs, separated by spaces or newlines; installed on Linux legs through the cached install-apt-packages action" + description: "Ubuntu packages the build needs, separated by spaces or newlines; installed on Linux legs through the cached install-apt-packages action. Empty installs none (Windows-only calls, wasm builds)" type: string - required: true + required: false + default: "" command: description: "The gate, run with bash from the repository root" type: string @@ -81,6 +83,11 @@ on: type: string required: false default: "v1" + cache-save-on-dispatch: + description: "Allow manually dispatched branch runs to save their branch-scoped dependency cache; pull requests never write" + type: boolean + required: false + default: false timeout-minutes: description: "Timeout of each leg" type: number @@ -167,7 +174,7 @@ jobs: /opt/hostedtoolcache/CodeQL /dev/null 2>&1 & - name: Install Linux build dependencies - if: runner.os == 'Linux' + if: runner.os == 'Linux' && inputs.packages != '' uses: storytold/craft-common-actions/actions/install-apt-packages@main with: packages: ${{ inputs.packages }} @@ -193,18 +200,38 @@ jobs: - name: Install cargo tools if: inputs.tools != '' - uses: taiki-e/install-action@8cca0918f8471c33515202b73f56ab27f2bed283 # v2.87.29 + uses: taiki-e/install-action@86f7a0ffb25f40114606501a311ebcfaf882ea32 # v2.88.0 with: tool: ${{ inputs.tools }} - # Only the default branch writes, so PR caches never evict its entry; a failing gate still - # saves. rust-cache hashes only the members' manifests, so the root Cargo.toml goes in the key - # or a profile change in a virtual root keeps restoring the old cache. + # Root workspace profiles are not among rust-cache's member manifests. Hash only + # those profiles here: a dependency/version/comment change must not discard the + # fallback cache for every other dependency. rust-cache still hashes the lockfile, + # toolchain, member manifests and build environment, and Cargo checks fingerprints. + - name: Cache profile key + id: cache-profile + shell: bash + run: | + python - <<'PYTHON' + import hashlib + import json + import os + import tomllib + from pathlib import Path + manifest = tomllib.loads(Path("Cargo.toml").read_text(encoding="utf-8")) + profiles = json.dumps(manifest.get("profile", {}), sort_keys=True, separators=(",", ":")) + key = hashlib.sha256(profiles.encode("utf-8")).hexdigest() + with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output: + output.write(f"key={key}\n") + PYTHON + + # Default-branch writes remain the default. Maintainers can explicitly opt manual + # branch runs into branch-scoped writes; pull-request runs never save a cache. - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: prefix-key: ${{ inputs.cache-version }}-rust - key: ${{ hashFiles('Cargo.toml') }} - save-if: ${{ github.event_name != 'pull_request' && github.ref_name == github.event.repository.default_branch }} + key: ${{ steps.cache-profile.outputs.key }} + save-if: ${{ github.event_name != 'pull_request' && (github.ref_name == github.event.repository.default_branch || (inputs.cache-save-on-dispatch && github.event_name == 'workflow_dispatch')) }} cache-on-failure: true - name: Gate diff --git a/.github/workflows/refresh-contributors.yml b/.github/workflows/refresh-contributors.yml new file mode 100644 index 0000000..e281e05 --- /dev/null +++ b/.github/workflows/refresh-contributors.yml @@ -0,0 +1,50 @@ +name: Refresh release contributors + +on: + workflow_call: + outputs: + artifact-name: + description: Contributor snapshot to pass to the release workflow + value: ${{ jobs.refresh.outputs.artifact-name }} + +permissions: + contents: read + pull-requests: read + +jobs: + refresh: + runs-on: ubuntu-24.04 + timeout-minutes: 15 + outputs: + artifact-name: contributor-snapshot-${{ github.run_id }}-${{ github.run_attempt }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + persist-credentials: false + - name: Refresh contributor credits for this release commit + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + python3 scripts/refresh-contributors.py --repo "$GITHUB_REPOSITORY" + python3 - <<'PY' + import json + import os + from pathlib import Path + + credits = json.loads(Path("contributors/contributors.json").read_text()) + if credits.get("head") != os.environ["GITHUB_SHA"]: + raise SystemExit("Contributor snapshot does not match the release commit") + if credits.get("repo") != os.environ["GITHUB_REPOSITORY"]: + raise SystemExit("Contributor snapshot does not match the release repository") + if not credits.get("contributors"): + raise SystemExit("Contributor snapshot is empty") + PY + - name: Save the refreshed snapshot for all release builds + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: contributor-snapshot-${{ github.run_id }}-${{ github.run_attempt }} + path: contributors/contributors.json + if-no-files-found: error + retention-days: 7 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1136be8..2e95326 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -27,6 +27,11 @@ on: type: string required: false default: "" + contributors-artifact: + description: "Optional contributor snapshot artifact from refresh-contributors.yml in this workflow run; installed before every source build" + type: string + required: false + default: "" has-web: description: "Whether to build the WebAssembly (wasm) release" type: boolean @@ -70,10 +75,10 @@ on: freebsd-architectures: # aarch64 is cross-compiled in the x86_64 VM, so packaging/freebsd/package.sh must build # with CARGO_BUILD_TARGET and name its archive after it, not after uname -m (#39). - description: 'JSON array of FreeBSD release architectures; add "aarch64" once packaging/freebsd/package.sh honors CARGO_BUILD_TARGET' + description: 'JSON array of FreeBSD release architectures; packaging/freebsd/package.sh must honor CARGO_BUILD_TARGET for each listed architecture' type: string required: false - default: '["x86_64"]' + default: '["x86_64","aarch64"]' has-server: description: "Whether to build and release the headless Linux server" type: boolean @@ -264,6 +269,12 @@ jobs: exit 1 fi - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Load refreshed contributor credits + if: inputs.contributors-artifact != '' + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: ${{ inputs.contributors-artifact }} + path: contributors - name: Check out craft-fonts if: inputs.craft-fonts-ref != '' uses: storytold/craft-common-actions/actions/checkout-craft-fonts@main @@ -301,7 +312,17 @@ jobs: APPLE_ID: ${{ secrets.APPLE_ID }} APPLE_PASSWORD: ${{ secrets.APPLE_PASSWORD }} APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }} - run: packaging/macos/verify.sh --arch universal + run: | + for attempt in 1 2 3 4; do + if packaging/macos/verify.sh --arch universal; then + exit 0 + fi + if [ "$attempt" -eq 4 ]; then + exit 1 + fi + echo "macOS verification failed (attempt $attempt of 4); retrying in 20 seconds" >&2 + sleep 20 + done - name: Note missing macOS verification if: github.ref == 'refs/heads/release' && hashFiles('packaging/macos/verify.sh') == '' run: echo "::warning::packaging/macos/verify.sh is missing, so this release was not checked for a signature or notarization ticket." @@ -404,6 +425,12 @@ jobs: exit 1 fi - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Load refreshed contributor credits + if: inputs.contributors-artifact != '' + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: ${{ inputs.contributors-artifact }} + path: contributors - name: Check out craft-fonts if: inputs.craft-fonts-ref != '' uses: storytold/craft-common-actions/actions/checkout-craft-fonts@main @@ -509,6 +536,12 @@ jobs: INPUT_VERSION: ${{ inputs.version }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Load refreshed contributor credits + if: inputs.contributors-artifact != '' + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: ${{ inputs.contributors-artifact }} + path: contributors - name: Check out craft-fonts if: inputs.craft-fonts-ref != '' uses: storytold/craft-common-actions/actions/checkout-craft-fonts@main @@ -555,6 +588,9 @@ jobs: name: Flatpak ${{ matrix.arch }} needs: [version, linux] runs-on: ${{ matrix.runner }} + permissions: + contents: read + packages: write strategy: fail-fast: false matrix: @@ -619,9 +655,12 @@ jobs: - name: Install flatpak-builder run: | sudo apt-get update - sudo apt-get install -y --no-install-recommends flatpak flatpak-builder librsvg2-common + # elfutils is only a Recommends of flatpak-builder, but flatpak-builder runs its eu-strip + # on every module it builds with debuginfo, such as the apps' cups-client. + sudo apt-get install -y --no-install-recommends flatpak flatpak-builder elfutils librsvg2-common skopeo sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0 - name: Build the Flatpak bundle + id: flatpak_bundle env: RELEASE_VERSION: ${{ needs.version.outputs.version }} ARCH: ${{ matrix.arch }} @@ -640,6 +679,19 @@ jobs: fi tarball="${tarballs[0]}" packaging/linux/flatpak-bundle.sh "$tarball" + printf 'tarball=%s\n' "$tarball" >> "$GITHUB_OUTPUT" + - name: Publish Flatpak OCI layout to GHCR + if: github.event_name != 'pull_request' + env: + RELEASE_VERSION: ${{ needs.version.outputs.version }} + ARCH: ${{ matrix.arch }} + REGISTRY: ghcr.io/${{ github.repository }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + TARBALL: ${{ steps.flatpak_bundle.outputs.tarball }} + run: | + if [ -x packaging/linux/flatpak-oci.sh ]; then + packaging/linux/flatpak-oci.sh --push "$TARBALL" + fi - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: release-flatpak-${{ matrix.arch }} @@ -700,7 +752,7 @@ jobs: strategy: fail-fast: false matrix: - # aarch64 is opt-in until each app's package.sh can package a cross build (#39). + # aarch64 is in the default freebsd-architectures; callers can narrow it (#39). arch: ${{ fromJSON(inputs.freebsd-architectures) }} env: APP_NAME: ${{ inputs.app-name }} @@ -771,6 +823,12 @@ jobs: ;; esac - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Load refreshed contributor credits + if: inputs.contributors-artifact != '' + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: ${{ inputs.contributors-artifact }} + path: contributors - name: Check out craft-fonts if: inputs.craft-fonts-ref != '' uses: storytold/craft-common-actions/actions/checkout-craft-fonts@main @@ -1014,6 +1072,12 @@ jobs: INPUT_VERSION: ${{ inputs.version }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Load refreshed contributor credits + if: inputs.contributors-artifact != '' + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: ${{ inputs.contributors-artifact }} + path: contributors - uses: dtolnay/rust-toolchain@683b588c487a69d01f6c6ddf40f036ff682cc2c8 # v1 with: toolchain: stable @@ -1112,6 +1176,12 @@ jobs: INPUT_VERSION: ${{ inputs.version }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Load refreshed contributor credits + if: inputs.contributors-artifact != '' + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: ${{ inputs.contributors-artifact }} + path: contributors - name: Check out craft-fonts if: inputs.craft-fonts-ref != '' uses: storytold/craft-common-actions/actions/checkout-craft-fonts@main @@ -1183,6 +1253,12 @@ jobs: FIGCRAFT_BUILD_SHA: ${{ github.sha }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Load refreshed contributor credits + if: inputs.contributors-artifact != '' + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: ${{ inputs.contributors-artifact }} + path: contributors - name: Check out craft-i18n uses: storytold/craft-common-actions/actions/checkout-craft-i18n@main - uses: dtolnay/rust-toolchain@683b588c487a69d01f6c6ddf40f036ff682cc2c8 # v1 diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index 39f7301..40f28bf 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -49,7 +49,10 @@ permissions: contents: read env: - COVERAGE_IGNORE: '(^|/)(vendor|xtask)/' + # Vendored code and xtask, plus the test code cargo llvm-cov leaves out on its own (tests/, + # examples/, benches/, tests.rs, *_tests.rs): the sharded merge calls llvm-cov export directly, + # which would otherwise report that test code as covered source. + COVERAGE_IGNORE: '(^|/)(vendor|xtask|tests|examples|benches)/|/(tests\.rs|[0-9a-zA-Z_-]+[_-]tests\.rs)$' jobs: plan: @@ -119,7 +122,7 @@ jobs: run: | rustup toolchain install stable --profile minimal --component llvm-tools-preview rustup default stable - - uses: taiki-e/install-action@8cca0918f8471c33515202b73f56ab27f2bed283 # v2.87.29 + - uses: taiki-e/install-action@86f7a0ffb25f40114606501a311ebcfaf882ea32 # v2.88.0 with: tool: cargo-llvm-cov,cargo-nextest@0.9.150 - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 @@ -184,7 +187,7 @@ jobs: pkg-config libxkbcommon-dev libwayland-dev libx11-dev libxcursor-dev libxrandr-dev libxi-dev libgtk-3-dev libfontconfig1-dev libfreetype-dev libasound2-dev libgl1-mesa-dev mesa-vulkan-drivers - - uses: taiki-e/install-action@8cca0918f8471c33515202b73f56ab27f2bed283 # v2.87.29 + - uses: taiki-e/install-action@86f7a0ffb25f40114606501a311ebcfaf882ea32 # v2.88.0 with: tool: cargo-nextest@0.9.150 - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 @@ -292,7 +295,7 @@ jobs: run: | rustup toolchain install stable --profile minimal --component llvm-tools-preview rustup default stable - - uses: taiki-e/install-action@8cca0918f8471c33515202b73f56ab27f2bed283 # v2.87.29 + - uses: taiki-e/install-action@86f7a0ffb25f40114606501a311ebcfaf882ea32 # v2.88.0 if: steps.token.outputs.available == 'true' && hashFiles('Cargo.toml') != '' && inputs.shards <= 1 with: tool: cargo-llvm-cov @@ -405,7 +408,7 @@ jobs: exit "$status" - name: SonarCloud Scan if: steps.token.outputs.available == 'true' - uses: SonarSource/sonarqube-scan-action@d209202bc7d53ff1cc128f7f907dac145c9d6ae9 # v8.3.0 + uses: SonarSource/sonarqube-scan-action@4d1dc0c91ee400dcf8a6dddf2f705ac36542e304 # v8.4.0 env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} SONAR_HOST_URL: https://sonarcloud.io diff --git a/.github/workflows/supply-chain.yml b/.github/workflows/supply-chain.yml index d08d302..47b168c 100644 --- a/.github/workflows/supply-chain.yml +++ b/.github/workflows/supply-chain.yml @@ -72,6 +72,8 @@ jobs: - name: Attach the SBOM to the draft release env: GH_TOKEN: ${{ github.token }} + # No checkout here, so gh has no git remote to find the repository from. + GH_REPO: ${{ github.repository }} VERSION: ${{ inputs.version }} run: | tag="v${VERSION#v}" diff --git a/.github/workflows/test-freebsd.yml b/.github/workflows/test-freebsd.yml index 805dcf0..86a0255 100644 --- a/.github/workflows/test-freebsd.yml +++ b/.github/workflows/test-freebsd.yml @@ -5,6 +5,7 @@ on: paths: - .github/workflows/freebsd.yml - .github/workflows/release.yml + - .github/workflows/refresh-contributors.yml - .github/workflows/gate.yml - actions/freebsd-cargo-cache/** - actions/checkout-craft-i18n/** @@ -17,6 +18,7 @@ on: paths: - .github/workflows/freebsd.yml - .github/workflows/release.yml + - .github/workflows/refresh-contributors.yml - .github/workflows/gate.yml - actions/freebsd-cargo-cache/** - actions/checkout-craft-i18n/** diff --git a/.github/workflows/windows-arm64.yml b/.github/workflows/windows-arm64.yml index 049d727..4fadd6e 100644 --- a/.github/workflows/windows-arm64.yml +++ b/.github/workflows/windows-arm64.yml @@ -11,6 +11,16 @@ on: description: "Installation folder name inside ProgramFiles or LocalAppData (e.g. PhotoCraft)" type: string required: true + install-relative-path: + description: "Install path below LocalAppData (per-user) or ProgramFiles (per-machine), independent of app-dir" + type: string + required: false + default: "" + install-scope: + description: "Explicit perUser or perMachine context; empty preserves legacy installer defaults" + type: string + required: false + default: "" main-binary: description: "Name of the main executable file" type: string @@ -52,9 +62,13 @@ jobs: with: toolchain: stable targets: aarch64-pc-windows-msvc + # The nightly run on main saves; pull requests and `release` restore it. A pull request's + # cache (about 0.7 GB) is visible to that pull request alone, and the apps' caches sit at + # the 10 GB limit, so each one evicted a cache main's gate restores. - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: key: windows-arm64-package + save-if: ${{ github.ref == 'refs/heads/main' }} - name: Install WiX uses: storytold/craft-common-actions/actions/install-wix@main with: @@ -89,6 +103,8 @@ jobs: MAIN_BINARY: ${{ inputs.main-binary }} CLI_BINARY: ${{ inputs.cli-binary }} IS_PER_USER: ${{ inputs.is-per-user }} + INSTALL_RELATIVE_PATH: ${{ inputs.install-relative-path }} + INSTALL_SCOPE: ${{ inputs.install-scope }} run: | $ErrorActionPreference = 'Stop' $mainExeName = if ($env:MAIN_BINARY -ne '') { $env:MAIN_BINARY } else { "$env:APP_NAME.exe" } @@ -99,6 +115,25 @@ jobs: throw 'app-dir and executable names must be nonempty names without paths, invalid Windows filename characters, or trailing dots/spaces' } } + # Keep display/registry/shortcut names separate from the nested install path. + $scope = [string]$env:INSTALL_SCOPE + if ($scope -notin '', 'perUser', 'perMachine') { throw 'install-scope must be perUser or perMachine' } + if ($env:IS_PER_USER -eq 'true' -and $scope -eq 'perMachine') { throw 'A per-user-only installer cannot request machine scope' } + $perUser = $scope -eq 'perUser' -or ($scope -eq '' -and $env:IS_PER_USER -eq 'true') + $relativePath = [string]$env:INSTALL_RELATIVE_PATH + if (-not $relativePath) { $relativePath = if ($perUser) { "Programs\$env:APP_DIR" } else { $env:APP_DIR } } + # Split with StringSplitOptions.None so leading, trailing and doubled separators fail. + $parts = $relativePath.Split([char[]]@('/', '\'), [StringSplitOptions]::None) + foreach ($part in $parts) { + if ([string]::IsNullOrEmpty($part) -or $part -in '.', '..' -or $part -match '[<>:"|?*\x00-\x1F]' -or $part -match '[. ]\z') { + throw 'install-relative-path must contain only safe relative Windows path components' + } + } + $installDirectory = if ($perUser) { $env:LOCALAPPDATA } else { $env:ProgramFiles } + foreach ($part in $parts) { $installDirectory = Join-Path $installDirectory $part } + $scopeProperties = @() + if ($scope -eq 'perUser') { $scopeProperties = @('ALLUSERS=2', 'MSIINSTALLPERUSER=1') } + if ($scope -eq 'perMachine') { $scopeProperties = @('ALLUSERS=1', 'MSIINSTALLPERUSER=""') } $msi = (Get-ChildItem dist/*.msi | Select-Object -First 1).FullName if ($env:IS_PER_USER -eq "true") { @@ -106,7 +141,7 @@ jobs: $deskLnk = Join-Path ([Environment]::GetFolderPath('Desktop')) "$env:APP_DIR.lnk" $key = "HKCU:\Software\Learning Machines LLC\$env:APP_DIR" function Install([string[]] $props) { - $p = Start-Process msiexec.exe -ArgumentList (@('/i', "`"$msi`"", '/qn', '/l*v', 'install.log') + $props) -Wait -PassThru + $p = Start-Process msiexec.exe -ArgumentList (@('/i', "`"$msi`"", '/qn', '/l*v', 'install.log') + $scopeProperties + $props) -Wait -PassThru if ($p.ExitCode -ne 0) { Get-Content install.log -Tail 60; throw "msiexec /i exited $($p.ExitCode)" } } function Uninstall([string] $exe) { @@ -119,7 +154,7 @@ jobs: function Expect([bool] $ok, [string] $what) { if (-not $ok) { Get-Content install.log -Tail 60; throw $what }; Write-Output "ok $what" } Install @() - $exe = Join-Path $env:LOCALAPPDATA "Programs\$env:APP_DIR\$mainExeName" + $exe = Join-Path $installDirectory $mainExeName Expect (Test-Path -LiteralPath $exe) "installed in default folder: $exe" $bytes = [System.IO.File]::ReadAllBytes($exe) $machine = [BitConverter]::ToUInt16($bytes, [BitConverter]::ToInt32($bytes, 0x3C) + 4) @@ -137,9 +172,9 @@ jobs: Uninstall $exe Expect (-not (Test-Path -LiteralPath $dir)) 'uninstall removed chosen folder' } else { - $p = Start-Process msiexec.exe -ArgumentList '/i', "`"$msi`"", '/qn', '/l*v', 'install.log' -Wait -PassThru + $p = Start-Process msiexec.exe -ArgumentList (@('/i', "`"$msi`"", '/qn', '/l*v', 'install.log') + $scopeProperties) -Wait -PassThru if ($p.ExitCode -ne 0) { Get-Content install.log -Tail 60; throw "msiexec /i exited $($p.ExitCode)" } - $dir = Join-Path $env:ProgramFiles $env:APP_DIR + $dir = $installDirectory $exeList = @($mainExeName) if ($cliExeName -ne '' -and $cliExeName -ne $mainExeName) { diff --git a/README.md b/README.md index 8542c1c..c086108 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Full multi-platform release pipeline for Craft apps: - **Windows** (x64, x86, ARM64 MSI installer with WiX v5, CLI, Azure Trusted Signing) - **Linux** (x86_64, aarch64 AppImage, deb, rpm, tar.gz via nfpm) - **Flatpak** (x86_64, aarch64 bundle generation) -- **FreeBSD** (FreeBSD 15.1-STABLE VM build & packaging for x86_64; aarch64 is cross-compiled in the same x86_64 VM and opt-in through `freebsd-architectures`, see #39) +- **FreeBSD** (FreeBSD 15.1-STABLE VM build & packaging for x86_64 and aarch64; aarch64 is cross-compiled in the same x86_64 VM and set by default through `freebsd-architectures`, see #39) - **Arch Linux** (x86_64, aarch64 `.pkg.tar.zst` built via `makepkg` from release tarball, named by bundle id; set `has-arch: false` to skip) - **WebAssembly** (wasm32-unknown-unknown with Trunk) - **Linux RISC-V 64** (cross-compilation for riscv64gc-unknown-linux-gnu, producing deb, rpm, and tar.gz via SHA256-verified nfpm; built on Ubuntu 24.04, so it sets `GLIBC_FLOOR=2.39` for the app's `nfpm.yaml` to declare, see #44) @@ -65,6 +65,48 @@ jobs: AZURE_CERT_PROFILE: ${{ secrets.AZURE_CERT_PROFILE }} ``` +#### Automatic contributor credits (opt-in) + +Apps with `scripts/refresh-contributors.py` can refresh bundled credits once before a release. +Grant the caller workflow `contents: write` for release publishing, and keep the preparatory +`credits` job read-only with its job-level override. A called workflow cannot increase the +token permissions granted by its caller; see [reusable-workflow permissions](https://docs.github.com/en/actions/reference/workflows-and-actions/reusing-workflow-configurations#supported-keywords-for-jobs-that-call-a-reusable-workflow). +Call the preparatory workflow, then pass its output to the release workflow: + +```yaml +permissions: + contents: write + +jobs: + credits: + permissions: + contents: read + pull-requests: read + uses: storytold/craft-common-actions/.github/workflows/refresh-contributors.yml@main + release: + needs: credits + permissions: + contents: write + uses: storytold/craft-common-actions/.github/workflows/release.yml@main + with: + app-name: effectcraft + app-title: EffectCraft + contributors-artifact: ${{ needs.credits.outputs.artifact-name }} + # Include the app's existing signing secrets here. +``` + +The preparatory workflow checks out full history, runs the app's script with `--repo` set to +`github.repository` and a read-only `GH_TOKEN`, and validates that the resulting nonempty +`contributors/contributors.json` belongs to `github.sha`. It uploads that file only after a +successful refresh. The intermediate `contributor-snapshot-*` artifact stays outside the +`release-*` collection used for release assets and checksums, including on reruns. +Every source-build job installs the same artifact after checkout, including +FreeBSD before copying the workspace to its VM. Flatpak and Arch inherit the refreshed credits +from the Linux binaries. The generated snapshot is not committed or pushed back to the app. + +An unsuccessful refresh or missing artifact blocks the release. Existing callers that omit +`contributors-artifact` keep their current behavior, permissions, and build paths. + List every signing secret explicitly. The signing jobs run in the caller repo's `release` environment and read the values from there, but only for the names listed here: an unlisted name is empty. On the `release` branch the macOS and Windows jobs fail when their signing secrets are empty, rather than shipping unsigned builds. macOS and Windows packages are signed by the app's own packaging scripts. `release.yml` does not request `id-token` or `attestations`, so existing callers keep building as they do now. @@ -145,6 +187,15 @@ jobs: ### 3. `windows-arm64.yml` Cross-compiles ARM64 MSI on x64 Windows runner, then installs, verifies native ARM64 architecture (`0xAA64`), smoke tests the CLI, and uninstalls on a Windows 11 ARM64 runner. +`app-dir` stays a single display/shortcut/registration name. Set `install-relative-path` +separately for nested folders, for example `Programs/ArtCraft/PhotoCraft` below LocalAppData +with `install-scope: perUser`, or `ArtCraft/PhotoCraft` below ProgramFiles with +`install-scope: perMachine`. An empty scope retains the legacy installer defaults and +`is-per-user` behaviour. Explicit scope is passed to msiexec as well as used for path checks. +For the launcher's per-user-only installer, keep `is-per-user: true` to retain its shortcut, +registry and custom-folder checks. Path components are validated before starting msiexec. + + #### Caller Example: ```yaml name: Windows ARM64 @@ -187,14 +238,14 @@ cross-compiled in it with stable Rust (the standard library comes from FreeBSD's aarch64 `lang/rust` package, against an arm64 sysroot built from `base.txz`). Its tests are built but not run, and its package check is skipped. -The release workflow builds FreeBSD aarch64 the same way, but only when the caller -sets `freebsd-architectures: '["x86_64","aarch64"]'` (the default is `'["x86_64"]'`). -Opt in only once the app's `packaging/freebsd/package.sh` builds with -`CARGO_BUILD_TARGET`, reads binaries from -`$CARGO_TARGET_DIR/$CARGO_BUILD_TARGET/release` and names its archive -`*-freebsd-aarch64.tar.gz`: the guest's `uname -m` is always `amd64`. The release -job fails if an archive does not match its matrix architecture. Apps without a -`package.sh` use the built-in packager, which already handles the cross target. +The release workflow builds FreeBSD for `freebsd-architectures`, which defaults to +`'["x86_64","aarch64"]'`; callers can narrow it, for example `'["x86_64"]'`. An app's +`packaging/freebsd/package.sh` must build with `CARGO_BUILD_TARGET`, read binaries +from `$CARGO_TARGET_DIR/$CARGO_BUILD_TARGET/release` and name its archive +`*-freebsd-.tar.gz` for each listed architecture: the guest's `uname -m` is +always `amd64`. The release job fails if an archive does not match its matrix +architecture. Apps without a `package.sh` use the built-in packager, which already +handles the cross target. The optional `architectures` input overrides the check matrix with a JSON array, for example `'["aarch64"]'` for a dedicated ARM check or @@ -338,18 +389,34 @@ The quality gate for an app's pull requests and pushes to main. It runs `command - the app and craft-fonts (at the suite's pin, with `CRAFT_FONTS_REQUIRED=1`) checked out without persisted credentials, and craft-i18n when the app has `craft-i18n.rev`; -- `packages` installed through `actions/install-apt-packages` on Linux legs; +- `packages` installed through `actions/install-apt-packages` on Linux legs (none when empty); - the channel from `rust-toolchain.toml`, or `toolchain`, with clippy, rustfmt and `targets`; - prebuilt cargo tools from `tools` (by default `cargo-nextest@0.9.150`); - `CARGO_PROFILE_DEV_DEBUG=line-tables-only` (set `dev-debug: ""` to keep the repository's profile) and the caller's `env`, `linux-env` and `windows-env` lines, each `KEY=VALUE`; -- a rust-cache dependency cache that only the default branch saves, so pull requests restore - main's entry and never evict it. The key includes the root `Cargo.toml`, since rust-cache hashes - only the members' manifests. Bump `cache-version` to start afresh. +- a rust-cache dependency cache saved by the default branch by default. Pull requests only + restore caches; callers may opt manual branch runs into branch-scoped writes with + `cache-save-on-dispatch: true`. The restore prefix includes the canonical root `[profile]` + table; dependency, version and comment edits do not discard reusable dependency artifacts. + rust-cache still keys on lockfiles, member manifests, toolchain and build environment, and + Cargo validates restored fingerprints. Bump `cache-version` to start afresh. After the gate, the job fails if `Cargo.lock` changed. Legs run with `fail-fast: false`; each has -`timeout-minutes` (default 60). Jobs only some apps need, such as fuzzing or web builds, stay in -the app's own workflow next to the call. +`timeout-minutes` (default 60). A job only some apps need, such as a web build or a Windows-only +check, can be a second call with its own `command`, `os`, `tools` and `targets`, and its own +`cache-version` so its cache (another profile or target) doesn't share the main gate's key: + +```yaml + web: + uses: storytold/craft-common-actions/.github/workflows/gate.yml@main + with: + cache-version: v1-web + os: '["ubuntu-24.04"]' + targets: wasm32-unknown-unknown + craft-fonts-ref: "" + tools: trunk@0.21.14 + command: packaging/web/package.sh +``` #### Caller Example: ```yaml @@ -484,8 +551,14 @@ Keep app-specific packaging assertions in the caller repository and pass their Bash script to `packaging-lint.yml` with `extra-checks-script: packaging/check-app-specific.sh`. It runs after the shared lint action, with the checkout and lint tools available. A missing or failing -requested script fails the job. Native Windows installer probes remain separate -caller jobs; Linux static lint does not replace them. +requested script fails the job. Linux static lint does not replace native Windows +installer probes; those belong in a shared workflow here as well. + +An app's `.github/workflows/` only calls this repository's workflows at `@main`, +with inputs and secrets: no caller jobs with their own `runs-on`/`steps`. App-specific +checks (fuzzing, corpora, web builds, installer probes) become shared workflows or +inputs here. Jobs that apps still run themselves move here without being dropped: +an app removes one only once a shared workflow runs it. ## Job time limits @@ -550,3 +623,17 @@ The tests execute the FreeBSD run template under the host's POSIX shell with fixture build/test commands and real tar archives. They cover failures in each custom test group, default build/test failures, missing/corrupt packages and font environment propagation. They do not replace native FreeBSD caller runs. + +### Gate cache reuse + +The gate cache key includes the canonical root `[profile]` table, including per-package +profile overrides. Dependency and version edits no longer change the restore prefix; +`rust-cache` still keys on the lockfile, toolchain, environment and member manifests, +and Cargo validates restored fingerprints. The first run after adopting this key +is cold. + +Only the default branch saves by default. A caller may set +`cache-save-on-dispatch: true` for repeated manual integration testing. This permits +`workflow_dispatch` runs to save a cache scoped by GitHub to that branch, never a +pull-request run. Use this selectively to avoid storage churn; sibling branches cannot +restore each other's caches. Do not pass the input until this workflow change has landed. diff --git a/actions/arch-package/arch-package.sh b/actions/arch-package/arch-package.sh index f786e31..734f95a 100755 --- a/actions/arch-package/arch-package.sh +++ b/actions/arch-package/arch-package.sh @@ -181,6 +181,9 @@ COMPRESSZST=(zstd -c -T0 --ultra -20 -) EOF # Write PKGBUILD +# libxkbcommon-x11 is a hard dependency next to libxkbcommon: the apps build winit's X11 backend, +# which loads it at startup on X11 and XWayland, and panics without it ("Library +# libxkbcommon-x11.so could not be loaded"; photocraft#201). Arch packages it separately. cat > "$work/PKGBUILD" <&2 + exit 2 + ;; + *) + TARGETS+=("$1") + shift + ;; + esac +done + +if [[ "$SCAN_ALL" -eq 1 ]]; then + shopt -s nullglob + for candidate in "$TARGET_DIR"/*; do + if [[ -d "$candidate" && -f "$candidate/packaging/linux/flatpak-bundle.sh" ]]; then + TARGETS+=("$candidate") + fi + done +fi + +if [[ ${#TARGETS[@]} -eq 0 ]]; then + echo "error: no target repositories specified (use --all or pass repo paths)" >&2 + exit 1 +fi + +scaffold_repo() { + local repo="$1" + local bundle_sh="$repo/packaging/linux/flatpak-bundle.sh" + local oci_sh="$repo/packaging/linux/flatpak-oci.sh" + + if [[ ! -f "$bundle_sh" ]]; then + echo "[-] $(basename "$repo"): no packaging/linux/flatpak-bundle.sh found, skipping" + return 0 + fi + + local app_id + app_id="$(grep -m1 '^APP_ID=' "$bundle_sh" | cut -d= -f2- | tr -d '"' | tr -d "'" || true)" + if [[ -z "$app_id" ]]; then + echo "[!] $(basename "$repo"): could not find APP_ID in $bundle_sh" + return 1 + fi + + local pkg + pkg="$(grep -m1 '^PKG=' "$bundle_sh" | cut -d= -f2- | tr -d '"' | tr -d "'" || true)" + if [[ -z "$pkg" ]]; then + pkg="$(basename "$repo")" + fi + + local has_oci_flag=0 + if grep -F -q -- '--oci' "$bundle_sh"; then + has_oci_flag=1 + fi + + local has_oci_script=0 + if [[ -f "$oci_sh" ]]; then + has_oci_script=1 + fi + + if [[ "$VERIFY_ONLY" -eq 1 ]]; then + if [[ "$has_oci_flag" -eq 1 && "$has_oci_script" -eq 1 ]]; then + echo "[✓] $(basename "$repo") ($app_id): fully scaffolded for Flatpak OCI" + else + echo "[✗] $(basename "$repo") ($app_id): missing (flag=$has_oci_flag, script=$has_oci_script)" + fi + return 0 + fi + + # 1. Update flatpak-bundle.sh if --oci is missing + if [[ "$has_oci_flag" -eq 0 ]]; then + echo "[+] $(basename "$repo"): adding --oci support to flatpak-bundle.sh" + python3 - "$bundle_sh" << 'EOF' +import sys + +path = sys.argv[1] +with open(path, 'r', encoding='utf-8') as f: + content = f.read() + +# Add OCI=0 after TEST=1 if not present +if "OCI=0" not in content and "TEST=1" in content: + content = content.replace("TEST=1\n", "TEST=1\nOCI=0\n", 1) + +# Add --oci option parsing +if "--oci)" not in content: + needle = '--no-test) TEST=0; shift ;;' + if needle in content: + content = content.replace(needle, needle + '\n --oci) OCI=1; shift ;;', 1) + +# Add OCI bundle export block after flatpak build-bundle +oci_block = """ +if [[ "$OCI" = 1 ]]; then + OUT_OCI="$DIST/$BASENAME-oci" + rm -rf "$OUT_OCI" + flatpak build-bundle --oci "$WORK/repo" "$OUT_OCI" "$APP_ID" stable + echo "wrote OCI layout to $OUT_OCI" +fi +""" + +if 'flatpak build-bundle --oci' not in content: + lines = content.split('\n') + new_lines = [] + inserted = False + for line in lines: + new_lines.append(line) + if not inserted and line.startswith('flatpak build-bundle ') and 'stable' in line: + # Look for the echo "wrote $OUT" line or append after + pass + if not inserted and 'echo "wrote $OUT"' in line: + new_lines.append(oci_block.strip('\n')) + inserted = True + content = '\n'.join(new_lines) + +with open(path, 'w', encoding='utf-8') as f: + f.write(content) +EOF + bash -n "$bundle_sh" || { echo "error: syntax error in modified $bundle_sh" >&2; return 1; } + fi + + # 2. Create flatpak-oci.sh if missing + if [[ "$has_oci_script" -eq 0 ]]; then + echo "[+] $(basename "$repo"): creating packaging/linux/flatpak-oci.sh" + cat > "$oci_sh" << EOF +#!/usr/bin/env bash +# Build $pkg as an OCI container layout and push it to GitHub Container Registry (GHCR) +# for consumption by Flatpak OCI clients (flatpak-oci-specs). +# +# Usage: +# packaging/linux/flatpak-oci.sh [--push] [TARBALL] +# +# Environment variables: +# REGISTRY Target registry image (default: ghcr.io/storytold/$pkg) +# TAG Target image tag (default: \$VERSION-\$ARCH, also tags stable-\$ARCH) +# GITHUB_TOKEN Token for GHCR authentication when --push is used +set -euo pipefail +# shellcheck source=../env.sh +. "\$(dirname "\${BASH_SOURCE[0]}")/../env.sh" +HERE="\$ROOT/packaging/linux" + +PUSH=0 +TARBALL="" +while [[ \$# -gt 0 ]]; do + case "\$1" in + --push) PUSH=1; shift ;; + -h | --help) sed -n '2,12p' "\$0"; exit 0 ;; + -*) echo "unknown argument: \$1" >&2; exit 2 ;; + *) TARBALL="\$1"; shift ;; + esac +done + +ARCH="\$(uname -m)" +case "\$ARCH" in + x86_64) ;; + aarch64 | arm64) ARCH=aarch64 ;; + *) echo "unsupported architecture \$ARCH" >&2; exit 2 ;; +esac + +BASENAME="$pkg-\$VERSION-linux-\$ARCH" +OUT_OCI="\$DIST/\$BASENAME-oci" + +echo "==> Building Flatpak OCI layout for \$BASENAME" +"\$HERE/flatpak-bundle.sh" --oci --no-test "\${TARBALL:-}" + +[[ -d "\$OUT_OCI" ]] || { echo "error: \$OUT_OCI layout not generated" >&2; exit 1; } + +REGISTRY="\${REGISTRY:-ghcr.io/storytold/$pkg}" +TAG="\${TAG:-\$VERSION-\$ARCH}" + +echo "==> OCI layout ready at \$OUT_OCI (Target: \$REGISTRY:\$TAG)" + +if [[ "\$PUSH" = 1 ]]; then + command -v skopeo >/dev/null || { echo "error: skopeo is required to push OCI images (apt install skopeo)" >&2; exit 1; } + + AUTH_ARGS=() + if [[ -n "\${GITHUB_TOKEN:-}" ]]; then + AUTH_ARGS=(--dest-creds "\${GITHUB_ACTOR:-storytold}:\$GITHUB_TOKEN") + fi + + echo "==> Pushing to \$REGISTRY:\$TAG" + skopeo copy --format oci "\${AUTH_ARGS[@]}" "oci:\$OUT_OCI" "docker://\$REGISTRY:\$TAG" + + # Also update architecture-specific stable tag + echo "==> Pushing to \$REGISTRY:stable-\$ARCH" + skopeo copy --format oci "\${AUTH_ARGS[@]}" "oci:\$OUT_OCI" "docker://\$REGISTRY:stable-\$ARCH" + + echo "==> Push complete" +fi +EOF + chmod +x "$oci_sh" + bash -n "$oci_sh" || { echo "error: syntax error in $oci_sh" >&2; return 1; } + fi + + echo "[✓] $(basename "$repo") ($app_id): Flatpak OCI ready" +} + +for target in "${TARGETS[@]}"; do + scaffold_repo "$target" +done diff --git a/tests/test_arch.py b/tests/test_arch.py index 3b0878a..45cfda7 100644 --- a/tests/test_arch.py +++ b/tests/test_arch.py @@ -237,6 +237,32 @@ exit 0 self.assertEqual([p.name for p in (tmp_path / "out").iterdir()], [f"myapp-{version}-linux-x86_64.pkg.tar.zst"]) + def test_pkgbuild_depends_on_the_x11_keyboard_library(self): + """The PKGBUILD depends on libxkbcommon-x11 as well as libxkbcommon. + + winit's X11 backend loads libxkbcommon-x11 at startup and panics without it, and Arch + ships it apart from libxkbcommon: a package without it could not open its window on + X11 or XWayland. + """ + with tempfile.TemporaryDirectory() as tmpdir: + tmp_path = Path(tmpdir) + tarball = self._create_fixture_tarball(tmp_path) + tools = self._setup_mock_tools(tmp_path) + capture = tmp_path / "capture" + capture.mkdir() + res = subprocess.run( + [str(ARCH_SCRIPT), str(tarball), "x86_64", str(tmp_path / "out")], + capture_output=True, + text=True, + env={**os.environ, "PATH": f"{tools}:{os.environ['PATH']}", "MOCK_CAPTURE": str(capture)}, + ) + self.assertEqual(res.returncode, 0, f"{res.stderr}\n{res.stdout}") + depends = next( + line for line in (capture / "PKGBUILD").read_text().splitlines() if line.startswith("depends=(") + ) + for package in ("'libxkbcommon'", "'libxkbcommon-x11'"): + self.assertIn(package, depends) + def test_package_function_rejects_symlinked_license_dir(self): with tempfile.TemporaryDirectory() as tmpdir: tmp_path = Path(tmpdir) diff --git a/tests/test_contributor_refresh.py b/tests/test_contributor_refresh.py new file mode 100644 index 0000000..12e8fc7 --- /dev/null +++ b/tests/test_contributor_refresh.py @@ -0,0 +1,194 @@ +"""Exercise release credit refresh failure handling and artifact distribution.""" +import fnmatch +import hashlib +import json +import os +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest + +import yaml + +ROOT = Path(__file__).resolve().parents[1] + + +def load(name): + """Read workflow scalars as strings, including GitHub's `on` key.""" + return yaml.load((ROOT / '.github/workflows' / name).read_text(), Loader=yaml.BaseLoader) + + +class ContributorRefreshTests(unittest.TestCase): + def run_refresh(self, mode): + """Execute the real validation step with a fixture refresh script.""" + workflow = load('refresh-contributors.yml') + step = next(s for s in workflow['jobs']['refresh']['steps'] if 'run' in s) + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + (root / 'scripts').mkdir() + (root / 'contributors').mkdir() + (root / 'scripts/refresh-contributors.py').write_text('''import json, os, sys +from pathlib import Path +assert sys.argv[1:] == ['--repo', 'owner/app'] +mode = os.environ['TEST_MODE'] +if mode == 'failed': + raise SystemExit(23) +credits = dict(head=os.environ['GITHUB_SHA'], repo='owner/app', contributors=[dict(login='person')]) +if mode == 'stale': + credits['head'] = 'old-commit' +if mode == 'wrong-repo': + credits['repo'] = 'other/app' +if mode == 'empty': + credits['contributors'] = [] +Path('contributors/contributors.json').write_text(json.dumps(credits)) +''') + result = subprocess.run(['bash', '-c', step['run']], cwd=root, + env=dict(os.environ, GITHUB_REPOSITORY='owner/app', + GITHUB_SHA='release-commit', TEST_MODE=mode), + text=True, capture_output=True, timeout=10) + snapshot = root / 'contributors/contributors.json' + return result, json.loads(snapshot.read_text()) if snapshot.exists() else None + + def test_refresh_runs_against_the_release_repository_and_commit(self): + """Accept credits generated for the caller's exact repository and head.""" + result, snapshot = self.run_refresh('valid') + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(snapshot['head'], 'release-commit') + + def test_failed_refresh_stops_before_validation_or_upload(self): + """Propagate refresh failures before any snapshot can be uploaded.""" + result, snapshot = self.run_refresh('failed') + self.assertEqual(result.returncode, 23) + self.assertIsNone(snapshot) + + def test_rejects_stale_wrong_repository_and_empty_snapshots(self): + """Reject unusable credits even when the refresh command succeeds.""" + for mode in ('stale', 'wrong-repo', 'empty'): + with self.subTest(mode=mode): + result, _ = self.run_refresh(mode) + self.assertNotEqual(result.returncode, 0) + self.assertIn('Contributor snapshot', result.stderr) + + def test_refresh_has_full_history_read_only_token_and_required_artifact(self): + """Keep refresh read-only and publish the validated file under its output name.""" + workflow = load('refresh-contributors.yml') + self.assertEqual(workflow['permissions'], {'contents': 'read', 'pull-requests': 'read'}) + steps = workflow['jobs']['refresh']['steps'] + checkout = steps[0] + self.assertEqual(checkout['with']['fetch-depth'], '0') + self.assertEqual(checkout['with']['persist-credentials'], 'false') + refresh = next(s for s in steps if 'run' in s) + self.assertEqual(refresh['env']['GH_TOKEN'], '${{ github.token }}') + upload = steps[-1] + self.assertIn('actions/upload-artifact@', upload['uses']) + self.assertEqual(upload['with']['if-no-files-found'], 'error') + self.assertEqual(upload['with']['path'], 'contributors/contributors.json') + self.assertNotIn('continue-on-error', refresh) + self.assertNotIn('continue-on-error', upload) + self.assertEqual(upload['with']['name'], workflow['jobs']['refresh']['outputs']['artifact-name']) + + def test_old_callers_keep_the_same_permissions_and_skip_artifact_download(self): + """Leave permissions and checked-in credits unchanged unless callers opt in.""" + workflow = load('release.yml') + self.assertEqual(workflow['permissions'], {'contents': 'read'}) + self.assertEqual(workflow['on']['workflow_call']['inputs']['contributors-artifact']['default'], '') + for job in workflow['jobs'].values(): + for step in job.get('steps', []): + if step.get('name') == 'Load refreshed contributor credits': + self.assertEqual(step['if'], "inputs.contributors-artifact != ''") + + def test_every_source_build_consumes_one_snapshot_before_compilation(self): + """Install the named snapshot before all seven source builds start.""" + jobs = load('release.yml')['jobs'] + for name in ('macos', 'windows', 'linux', 'freebsd', 'web', 'linux-riscv64', 'server'): + with self.subTest(job=name): + steps = jobs[name]['steps'] + download = next(i for i, s in enumerate(steps) if s.get('name') == 'Load refreshed contributor credits') + self.assertIn('actions/checkout@', steps[download - 1]['uses']) + settings = steps[download]['with'] + self.assertEqual(settings['name'], '${{ inputs.contributors-artifact }}') + self.assertEqual(settings['path'], 'contributors') + build_steps = [i for i, s in enumerate(steps) + if 'cargo ' in s.get('run', '') or 'packaging/' in s.get('run', '') + or 'vmactions/freebsd-vm@' in s.get('uses', '')] + self.assertTrue(build_steps) + self.assertTrue(all(download < i for i in build_steps)) + self.assertNotIn('continue-on-error', steps[download]) + # These package already-built Linux binaries rather than compiling them again. + for name in ('flatpak', 'arch'): + self.assertIn('linux', jobs[name]['needs']) + + def test_snapshots_stay_out_of_draft_release_assets_and_checksums(self): + """Exclude both rerun snapshots while collecting, checksumming and uploading packages.""" + refresh = load('refresh-contributors.yml')['jobs']['refresh'] + snapshot_name = refresh['steps'][-1]['with']['name'] + artifacts = { + snapshot_name.replace('${{ github.run_id }}', '123') + .replace('${{ github.run_attempt }}', str(attempt)): + {'contributors.json': f'credits from attempt {attempt}'.encode()} + for attempt in (1, 2) + } + packages = {} + for job, settings in load('release.yml')['jobs'].items(): + for step in settings.get('steps', []): + if step.get('uses', '').startswith('actions/upload-artifact@'): + name = step['with']['name'].replace('${{ matrix.arch }}', 'x86_64') + filename = f'fixture-{job}.tar.gz' + packages[filename] = f'package from {job}'.encode() + artifacts[name] = {filename: packages[filename]} + self.assertTrue(packages, 'The fixture must include real release artifact producers') + + action = yaml.load((ROOT / 'actions/draft-release/action.yml').read_text(), + Loader=yaml.BaseLoader) + steps = action['runs']['steps'] + download = next(s for s in steps if s.get('uses', '').startswith('actions/download-artifact@')) + self.assertEqual(download['with']['merge-multiple'], 'true') + expected_checksums = {name: hashlib.sha256(data).hexdigest() + for name, data in packages.items()} + for is_draft in (False, True): + with self.subTest(existing_draft=is_draft), tempfile.TemporaryDirectory() as directory: + root = Path(directory) + destination = root / download['with']['path'] + destination.mkdir() + # Model the action's actual glob and flattened artifact download. + for name, files in artifacts.items(): + if fnmatch.fnmatchcase(name, download['with']['pattern']): + for filename, data in files.items(): + (destination / filename).write_bytes(data) + tools = root / 'tools' + tools.mkdir() + gh = tools / 'gh' + gh.write_text(f'#!{sys.executable}\n' + '''"""Capture release uploads without contacting GitHub.""" +import json, os, sys +from pathlib import Path +args = sys.argv[1:] +if args[:2] == ['release', 'view']: + if args[args.index('--json') + 1] == 'isDraft': + if os.environ['EXISTING_DRAFT'] != 'true': + raise SystemExit(1) + print('true') +elif args[:2] in (['release', 'create'], ['release', 'upload']): + Path(os.environ['UPLOAD_LOG']).write_text(json.dumps(args)) +''') + gh.chmod(0o755) + env = dict(os.environ, PATH=str(tools) + os.pathsep + os.environ['PATH'], + VERSION='1.2.3', APP_TITLE='Fixture', PRERELEASE='false', + GITHUB_SHA='release-commit', GITHUB_STEP_SUMMARY=str(root / 'summary'), + EXISTING_DRAFT=str(is_draft).lower(), UPLOAD_LOG=str(root / 'upload.json')) + for step in steps: + if 'run' in step: + result = subprocess.run(['bash', '-e', '-o', 'pipefail', '-c', step['run']], + cwd=root / step.get('working-directory', '.'), + env=env, text=True, capture_output=True, timeout=10) + self.assertEqual(result.returncode, 0, result.stderr) + checksums = dict((line.split()[1], line.split()[0]) + for line in (destination / 'SHA256SUMS.txt').read_text().splitlines()) + self.assertEqual(checksums, expected_checksums) + args = json.loads((root / 'upload.json').read_text()) + self.assertEqual(args[:2], ['release', 'upload' if is_draft else 'create']) + self.assertEqual(set(args[args.index('--') + 1:]), set(packages) | {'SHA256SUMS.txt'}) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_freebsd.py b/tests/test_freebsd.py index 36e26eb..44b04ee 100644 --- a/tests/test_freebsd.py +++ b/tests/test_freebsd.py @@ -373,9 +373,9 @@ fi release_data = yaml.load(release_path.read_text(), Loader=yaml.BaseLoader) job = release_data["jobs"]["freebsd"] self.assertEqual(job["strategy"]["matrix"]["arch"], "${{ fromJSON(inputs.freebsd-architectures) }}") - # aarch64 is opt-in until each app's package.sh can package a cross build (#39). + # aarch64 is in the default now that app package.sh scripts honor CARGO_BUILD_TARGET (#39). default = release_data["on"]["workflow_call"]["inputs"]["freebsd-architectures"]["default"] - self.assertEqual(json.loads(default), ["x86_64"]) + self.assertEqual(json.loads(default), ["x86_64", "aarch64"]) def test_release_workflow_cross_compiles_aarch64_like_ci(self): release = yaml.load((ROOT / ".github/workflows/release.yml").read_text(), Loader=yaml.BaseLoader) diff --git a/tests/test_gate.py b/tests/test_gate.py index 14dce95..848c5cb 100644 --- a/tests/test_gate.py +++ b/tests/test_gate.py @@ -1,4 +1,4 @@ -"""The shared gate workflow: pinned actions, a cache only the default branch writes, and caller +"""The shared gate workflow: pinned actions, a cache with explicit manual-branch opt-in, and caller input that reaches the shell only through the environment.""" from pathlib import Path import os @@ -27,12 +27,21 @@ class GateWorkflowTests(unittest.TestCase): if not OWN_ACTION.match(s['uses']): self.assertRegex(s['uses'], r'@[0-9a-f]{40}$') - def test_only_pushes_to_the_default_branch_save_the_cache(self): + def test_manual_branch_saves_require_explicit_opt_in(self): cache = next(s for s in STEPS if 'rust-cache' in s.get('uses', '')) condition = re.sub(r'\s+', ' ', cache['with']['save-if']) self.assertIn("github.event_name != 'pull_request'", condition) self.assertIn('github.ref_name == github.event.repository.default_branch', condition) - self.assertNotIn('||', condition) + self.assertIn("inputs.cache-save-on-dispatch && github.event_name == 'workflow_dispatch'", condition) + self.assertEqual(WORKFLOW['on']['workflow_call']['inputs']['cache-save-on-dispatch']['default'], 'false') + + def test_packages_are_optional_and_empty_installs_none(self): + packages = WORKFLOW['on']['workflow_call']['inputs']['packages'] + self.assertEqual(packages['required'], 'false') + self.assertEqual(packages['default'], '') + condition = step('Install Linux build dependencies')['if'] + self.assertIn("runner.os == 'Linux'", condition) + self.assertIn("inputs.packages != ''", condition) def test_inputs_reach_scripts_only_through_env(self): for s in STEPS: @@ -71,5 +80,34 @@ class CallerEnvironmentTests(unittest.TestCase): self.assertEqual(self.run_step(ALL=f'GOOD=1\n{line}')[0], 1) +class ProfileCacheTests(unittest.TestCase): + def key(self, manifest): + with tempfile.TemporaryDirectory() as directory: + Path(directory, 'Cargo.toml').write_text(manifest) + output = Path(directory, 'output') + subprocess.run(['bash', '-euo', 'pipefail', '-c', step('Cache profile key')['run']], + cwd=directory, check=True, capture_output=True, + env={**os.environ, 'GITHUB_OUTPUT': str(output)}) + return output.read_text() + + def test_dependency_revision_does_not_discard_fallback(self): + profile = '[profile.dev]\nopt-level = 1\n' + self.assertEqual(self.key('[workspace.dependencies]\na = "1"\n' + profile), + self.key('[workspace.dependencies]\na = "2"\n' + profile)) + + def test_profile_changes_invalidate(self): + self.assertNotEqual(self.key('[profile.dev]\nopt-level = 1'), + self.key('[profile.dev]\nopt-level = 2')) + self.assertNotEqual(self.key('[profile.dev.package.foo]\nopt-level = 1'), + self.key('[profile.dev.package.foo]\nopt-level = 2')) + + def test_order_and_comments_do_not_invalidate(self): + self.assertEqual(self.key('[profile.dev]\ndebug = false\nopt-level = 1'), + self.key('# comment\n[profile.dev]\nopt-level = 1\ndebug = false')) + + def test_no_profiles_is_supported(self): + self.assertRegex(self.key('[workspace]\nmembers = []'), r'^key=[a-f0-9]{64}\n$') + + if __name__ == '__main__': unittest.main() diff --git a/tests/test_install_apt_packages.py b/tests/test_install_apt_packages.py index 4c6178f..f507f51 100644 --- a/tests/test_install_apt_packages.py +++ b/tests/test_install_apt_packages.py @@ -94,5 +94,69 @@ class InstallAptPackagesTests(unittest.TestCase): self.assertIn("::error::install-apt-packages:", res.stdout + res.stderr) + +# apt-get that logs its operation and stalls (sends nothing, never exits) while STALLS says so. +FAKE_APT = """#!/bin/bash +op=update +for arg in "$@"; do case "$arg" in update|install|autoclean) op=$arg ;; --download-only) op=download ;; esac; done +echo "$op" >> "$APT_LOG" +n=$(grep -c "^$op$" "$APT_LOG") +if [ "$op" = "${STALL_OP:-}" ] && [ "$n" -le "${STALLS:-0}" ]; then exec /usr/bin/sleep 60; fi +""" + + +class StalledMirrorTests(unittest.TestCase): + """The install step against a fake apt-get, with a one-second limit per mirror download.""" + + def run_install(self, stall_op='', stalls=0): + install_step = next(s for s in steps() if s.get('name') == 'Install packages') + self.assertEqual(install_step['env']['APT_NETWORK_TIMEOUT'], '300') + with tempfile.TemporaryDirectory() as tmpdir: + bin_dir = Path(tmpdir) / 'bin' + bin_dir.mkdir() + for name, body in [('apt-get', FAKE_APT), ('sudo', '#!/bin/bash\nexec "$@"\n'), ('sleep', '#!/bin/bash\n')]: + (bin_dir / name).write_text(body, newline='\n') + (bin_dir / name).chmod(0o755) + log = Path(tmpdir) / 'apt.log' + log.touch() + res = subprocess.run( + ['bash', '--noprofile', '--norc', '-e', '-o', 'pipefail', '-c', install_step['run']], + env={**os.environ, 'PATH': f"{bin_dir}{os.pathsep}{os.environ['PATH']}", 'HOME': tmpdir, + 'PACKAGE_LIST': 'curl git ', 'APT_NETWORK_TIMEOUT': '1', 'APT_NETWORK_ATTEMPTS': '3', + 'APT_LOG': str(log), 'STALL_OP': stall_op, 'STALLS': str(stalls)}, + capture_output=True, + text=True, + timeout=120, + ) + return res, log.read_text().split() + + def test_a_stalled_update_is_retried(self): + res, ops = self.run_install('update', stalls=1) + self.assertEqual(res.returncode, 0, res.stdout + res.stderr) + self.assertEqual(ops, ['update', 'update', 'download', 'install', 'autoclean']) + self.assertIn('apt-get update attempt 1 exited 124', res.stdout) + + def test_a_stalled_download_is_retried(self): + res, ops = self.run_install('download', stalls=2) + self.assertEqual(res.returncode, 0, res.stdout + res.stderr) + self.assertEqual(ops, ['update', 'download', 'download', 'download', 'install', 'autoclean']) + + def test_an_update_that_keeps_failing_continues_with_a_warning(self): + res, ops = self.run_install('update', stalls=99) + self.assertEqual(res.returncode, 0, res.stdout + res.stderr) + self.assertEqual(ops, ['update', 'update', 'update', 'download', 'install', 'autoclean']) + self.assertIn('::warning::install-apt-packages: apt-get update failed 3 times', res.stdout) + + def test_a_download_that_keeps_stalling_fails_before_installing(self): + res, ops = self.run_install('download', stalls=99) + self.assertNotEqual(res.returncode, 0) + self.assertEqual(ops, ['update', 'download', 'download', 'download']) + self.assertIn('::error::install-apt-packages: downloading the packages failed 3 times', res.stdout) + + def test_a_partly_failed_update_is_an_error_so_it_is_retried(self): + install_step = next(s for s in steps() if s.get('name') == 'Install packages') + self.assertIn('network update -o APT::Update::Error-Mode=any', install_step['run']) + + if __name__ == '__main__': unittest.main() diff --git a/tests/test_scaffold_flatpak_oci.py b/tests/test_scaffold_flatpak_oci.py new file mode 100644 index 0000000..6da69a4 --- /dev/null +++ b/tests/test_scaffold_flatpak_oci.py @@ -0,0 +1,100 @@ +"""Tests for the Flatpak OCI scaffolding tool.""" +from pathlib import Path +import os +import subprocess +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] +SCRIPT = ROOT / "scripts/scaffold-flatpak-oci.sh" + +SAMPLE_BUNDLE_SH = """#!/usr/bin/env bash +set -euo pipefail +HERE="$ROOT/packaging/linux" +APP_ID=ai.storyteller.sampleapp +PKG=sampleapp +FLATHUB=https://dl.flathub.org/repo/flathub.flatpakrepo + +TEST=1 +TARBALL="" +while [[ $# -gt 0 ]]; do + case "$1" in + --no-test) TEST=0; shift ;; + -h | --help) sed -n '2,14p' "$0"; exit 0 ;; + -*) echo "unknown argument: $1" >&2; exit 2 ;; + *) TARBALL="$1"; shift ;; + esac +done + +ARCH="$(uname -m)" +BASENAME="$PKG-$VERSION-linux-$ARCH" +WORK="/tmp/bundle" +OUT="$DIST/$BASENAME.flatpak" +flatpak build-bundle --runtime-repo="$FLATHUB" "$WORK/repo" "$OUT" "$APP_ID" stable +echo "wrote $OUT" + +if [[ "$TEST" = 1 ]]; then + echo "test" +fi +""" + + +class ScaffoldFlatpakOciTests(unittest.TestCase): + def setUp(self): + self.temp_dir = tempfile.TemporaryDirectory() + self.repo_dir = Path(self.temp_dir.name) / "sampleapp" + self.packaging_dir = self.repo_dir / "packaging/linux" + self.packaging_dir.mkdir(parents=True) + self.bundle_file = self.packaging_dir / "flatpak-bundle.sh" + self.bundle_file.write_text(SAMPLE_BUNDLE_SH, encoding="utf-8") + self.bundle_file.chmod(0o755) + + def tearDown(self): + self.temp_dir.cleanup() + + def test_scaffolding_adds_oci_support(self): + # Initial verify should show missing + res = subprocess.run( + [str(SCRIPT), "--verify", str(self.repo_dir)], + capture_output=True, + text=True, + check=True, + ) + self.assertIn("missing (flag=0, script=0)", res.stdout) + + # Run scaffolding + res_scaffold = subprocess.run( + [str(SCRIPT), str(self.repo_dir)], + capture_output=True, + text=True, + check=True, + ) + self.assertIn("Flatpak OCI ready", res_scaffold.stdout) + + # Check bundle_sh has --oci flag and export block + updated_bundle = self.bundle_file.read_text(encoding="utf-8") + self.assertIn("OCI=0", updated_bundle) + self.assertIn("--oci) OCI=1; shift ;;", updated_bundle) + self.assertIn("flatpak build-bundle --oci", updated_bundle) + + # Check flatpak-oci.sh is created and executable + oci_script = self.packaging_dir / "flatpak-oci.sh" + self.assertTrue(oci_script.exists()) + self.assertTrue(os.access(oci_script, os.X_OK)) + + oci_content = oci_script.read_text(encoding="utf-8") + self.assertIn("ghcr.io/storytold/sampleapp", oci_content) + self.assertIn("skopeo copy", oci_content) + + # Final verify should pass + res_final = subprocess.run( + [str(SCRIPT), "--verify", str(self.repo_dir)], + capture_output=True, + text=True, + check=True, + ) + self.assertIn("fully scaffolded for Flatpak OCI", res_final.stdout) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_sonarcloud.py b/tests/test_sonarcloud.py index d704db7..f3a49ba 100644 --- a/tests/test_sonarcloud.py +++ b/tests/test_sonarcloud.py @@ -1,6 +1,7 @@ """Exercise the reusable SonarCloud workflow's project-key trust boundary.""" import os +import re from pathlib import Path import subprocess import tempfile @@ -228,6 +229,26 @@ class SonarCloudTests(unittest.TestCase): self.assertTrue(any('rustc-release.txt' in step.get('with', {}).get('path', '') for step in build)) + def test_sharded_export_leaves_out_the_test_code_cargo_llvm_cov_does(self): + # cargo llvm-cov adds these exclusions to --ignore-filename-regex on its own; the sharded + # merge calls llvm-cov export directly, so the shared regex has to carry them. + workflow = yaml.load( + (ROOT / '.github/workflows/sonarcloud.yml').read_text(), Loader=yaml.BaseLoader) + ignore = re.compile(workflow['env']['COVERAGE_IGNORE']) + combine = next(step for step in workflow['jobs']['sonarcloud']['steps'] + if step.get('name') == 'Combine shard coverage') + self.assertIn('-ignore-filename-regex="$COVERAGE_IGNORE"', combine['run']) + ws = '/home/runner/work/app/app' + for path in ('tests/it.rs', 'crates/fonts-tests/tests/fonts.rs', 'crates/ui/src/i18n/tests.rs', + 'crates/ui/src/parser_tests.rs', 'crates/ui/src/io-tests.rs', + 'examples/demo.rs', 'benches/render.rs', 'vendor/zlib/lib.rs', 'xtask/src/main.rs'): + with self.subTest(excluded=path): + self.assertTrue(ignore.search(f'{ws}/{path}')) + for path in ('src/lib.rs', 'crates/fonts-tests/src/lib.rs', 'crates/ui/src/contests.rs', + 'crates/ui/src/testsuite.rs', 'apps/app-cli/src/main.rs'): + with self.subTest(kept=path): + self.assertFalse(ignore.search(f'{ws}/{path}')) + def test_shard_masks_only_test_failures(self): workflow = yaml.load( (ROOT / '.github/workflows/sonarcloud.yml').read_text(), Loader=yaml.BaseLoader) diff --git a/tests/test_supply_chain.py b/tests/test_supply_chain.py index 420000b..1c53f5c 100644 --- a/tests/test_supply_chain.py +++ b/tests/test_supply_chain.py @@ -1,5 +1,6 @@ """The supply-chain workflow stays opt-in so current release callers keep working.""" +import os from pathlib import Path import subprocess import tempfile @@ -107,6 +108,34 @@ class SupplyChainOptInTests(unittest.TestCase): self.assertIn("::error::no release files were downloaded", result.stdout) self.assertEqual(subjects, {}) + def test_sbom_upload_names_the_repository_without_a_checkout(self): + # The job has no checkout, so gh cannot read the repository from a git remote; like the + # real gh, the fixture fails unless GH_REPO or --repo names it. + steps = self.supply["jobs"]["attest"]["steps"] + self.assertFalse(any(step.get("uses", "").startswith("actions/checkout@") for step in steps)) + attach = next(step for step in steps if step.get("name") == "Attach the SBOM to the draft release") + context = {"${{ github.token }}": "token", "${{ github.repository }}": "storytold/photocraft", + "${{ inputs.version }}": "1.2.3"} + with tempfile.TemporaryDirectory(prefix="supply-chain-test-") as directory: + work = Path(directory) + (work / "bin").mkdir() + gh = work / "bin/gh" + gh.write_text('#!/bin/sh\n' + 'repo="${GH_REPO:-}"\n' + 'for arg; do case "$prev" in --repo|-R) repo="$arg" ;; esac; prev="$arg"; done\n' + 'if [ -z "$repo" ]; then\n' + ' echo "failed to run git: fatal: not a git repository" >&2; exit 1\n' + 'fi\n' + 'echo "$repo $*" > "$GH_LOG"\n') + gh.chmod(0o755) + env = {name: context[value] for name, value in attach["env"].items()} + env.update(PATH=f"{work / 'bin'}{os.pathsep}{os.environ['PATH']}", GH_LOG=str(work / "gh.log")) + result = subprocess.run(["bash", "-e", "-c", attach["run"]], cwd=work, env=env, + text=True, capture_output=True, timeout=15) + self.assertEqual(result.returncode, 0, result) + self.assertEqual((work / "gh.log").read_text().split(), + ["storytold/photocraft", "release", "upload", "v1.2.3", "sbom.cyclonedx.json", "--clobber"]) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_windows_arm64.py b/tests/test_windows_arm64.py index 2c06e03..8c1931a 100644 --- a/tests/test_windows_arm64.py +++ b/tests/test_windows_arm64.py @@ -21,20 +21,26 @@ STEP = next(step for step in DATA['jobs']['install']['steps'] if 'run' in step) INSTALLER = r''' import json, os, sys from pathlib import Path -args = sys.argv[1:] +args = json.loads(Path('process-arguments.json').read_text(encoding='utf-8-sig')) with open('processes.jsonl', 'a') as log: log.write(json.dumps(args) + '\n') if args[0] == '/i': Path('install.log').touch() - per_user = os.environ['TEST_PER_USER'] == 'true' - directory = Path(os.environ['LOCALAPPDATA']) / 'Programs' if per_user else Path(os.environ['ProgramFiles']) - directory /= os.environ['TEST_APP_DIR'] + launcher = os.environ['TEST_PER_USER'] == 'true' + scope = os.environ.get('TEST_SCOPE', '') + per_user = scope == 'perUser' or (not scope and launcher) + expected = ['ALLUSERS=2', 'MSIINSTALLPERUSER=1'] if scope == 'perUser' else ['ALLUSERS=1', 'MSIINSTALLPERUSER=""'] if scope else [] + if any(value not in args for value in expected): + raise SystemExit('explicit install scope was not passed to msiexec') + relative = os.environ.get('TEST_INSTALL_PATH', '') or ('Programs/' if per_user else '') + os.environ['TEST_APP_DIR'] + directory = Path.cwd() / ('local' if per_user else 'program-files') + directory = directory.joinpath(*relative.replace(chr(92), '/').split('/')) custom = next((arg for arg in args if arg.startswith('INSTALLFOLDER=')), None) if custom: directory = Path(custom[len('INSTALLFOLDER='):].strip('"').rstrip('\\')) directory.mkdir(parents=True) names = [os.environ['TEST_MAIN_BINARY']] - if not per_user: + if not launcher: names.append(os.environ['TEST_CLI_BINARY']) files = [directory / name for name in set(names)] for path in files: @@ -43,7 +49,7 @@ if args[0] == '/i': data[68:70] = (0xaa64).to_bytes(2, 'little') path.write_bytes(data) path.chmod(0o755) - if per_user: + if launcher: for folder in ('programs', 'desktop'): if folder == 'desktop' and 'INSTALLDESKTOPSHORTCUT=0' in args: continue @@ -59,10 +65,14 @@ else: ''' PRELUDE = r''' +[Console]::OutputEncoding = [Text.UTF8Encoding]::new($false) +$OutputEncoding = [Console]::OutputEncoding function Start-Process { param($FilePath, [string[]] $ArgumentList, [switch] $Wait, [switch] $PassThru) if ($FilePath -ne 'msiexec.exe') { throw "unexpected process $FilePath" } - & $env:TEST_PYTHON fixture.py @ArgumentList + # Preserve the Start-Process argument array across the Python fixture boundary. + ConvertTo-Json -InputObject @($ArgumentList) -Compress | Set-Content process-arguments.json -Encoding UTF8 + & $env:TEST_PYTHON fixture.py if ($LASTEXITCODE -ne 0) { throw 'fixture installer failed' } [pscustomobject]@{ ExitCode = 0 } } @@ -76,11 +86,16 @@ function Get-ItemProperty { ''' -@unittest.skipUnless(shutil.which('pwsh') and os.name == 'posix', 'requires PowerShell on POSIX') +SHELL = shutil.which('pwsh') or shutil.which('powershell') + + +@unittest.skipUnless(SHELL, 'requires PowerShell') class WindowsArm64Tests(unittest.TestCase): def run_install(self, per_user=False, **overrides): + """Exercise the workflow with fixture process and filesystem boundaries.""" inputs = {'app-name': 'pdfcraft', 'app-dir': 'PDFCraft', 'main-binary': '', 'cli-binary': '', 'is-per-user': str(per_user).lower(), + 'install-relative-path': '', 'install-scope': '', **overrides} with tempfile.TemporaryDirectory() as directory: root = Path(directory) @@ -91,6 +106,7 @@ class WindowsArm64Tests(unittest.TestCase): env = {**os.environ, 'LOCALAPPDATA': str(root / 'local'), 'ProgramFiles': str(root / 'program-files'), 'TEST_PYTHON': sys.executable, 'TEST_PER_USER': inputs['is-per-user'], 'TEST_APP_DIR': inputs['app-dir'], + 'TEST_SCOPE': inputs['install-scope'], 'TEST_INSTALL_PATH': inputs['install-relative-path'], 'TEST_MAIN_BINARY': inputs['main-binary'] or inputs['app-name'] + '.exe', 'TEST_CLI_BINARY': inputs['cli-binary'] or inputs['app-name'] + '-cli.exe'} script = STEP['run'] @@ -107,14 +123,30 @@ class WindowsArm64Tests(unittest.TestCase): for folder in ('Programs', 'Desktop'): script = script.replace(f"([Environment]::GetFolderPath('{folder}'))", f"'{root / folder.lower()}'") - (root / 'install.ps1').write_text(PRELUDE + script) - result = subprocess.run(['pwsh', '-NoLogo', '-NoProfile', '-File', 'install.ps1'], + # The POSIX fixture is a shell executable. On Windows mock only that + # native CLI boundary; retain path checks, invocation and exit handling. + prelude = PRELUDE + if os.name == 'nt': + prelude += r""" +$env:ProgramFiles = Join-Path $PWD 'program-files' +function Invoke-FixtureCli([string] $Path, [string] $Argument) { + if (-not (Test-Path -LiteralPath $Path) -or $Argument -ne '--version') { throw 'invalid fixture CLI call' } + Set-Content cli-ran 'called' + $global:LASTEXITCODE = 0 + 'fixture 1.0' +} +""" + script = script.replace('$out = & $cli --version 2>&1 | Out-String', + "$out = Invoke-FixtureCli $cli '--version' | Out-String") + (root / 'install.ps1').write_text(prelude + script, encoding='utf-8-sig') + result = subprocess.run([SHELL, '-NoLogo', '-NoProfile', '-File', 'install.ps1'], cwd=root, env=env, capture_output=True, text=True, timeout=20) log = root / 'processes.jsonl' processes = [json.loads(line) for line in log.read_text().splitlines()] if log.exists() else [] return result, processes, (root / 'injected').exists(), (root / 'cli-ran').exists() def test_defaults_and_custom_names_in_both_branches(self): + """Defaults and custom names in both branches.""" for per_user in (False, True): for inputs in ({}, {'app-name': 'craft-launcher', 'app-dir': 'ArtCraft Launcher', 'main-binary': 'artcraft-launcher.exe', 'cli-binary': 'artcraft-cli.exe'}, @@ -127,7 +159,24 @@ class WindowsArm64Tests(unittest.TestCase): self.assertFalse(injected) self.assertEqual(cli_ran, not per_user) + def test_explicit_context_and_nested_path_reach_the_installer(self): + """Explicit context and nested path reach the installer.""" + for launcher, scope, relative in ( + (False, 'perUser', 'Programs/ArtCraft/PhotoCraft'), + (False, 'perMachine', 'ArtCraft/PhotoCraft'), + (True, 'perUser', 'Programs/ArtCraft/Launcher'), + ): + with self.subTest(launcher=launcher, scope=scope): + result, processes, injected, cli_ran = self.run_install( + launcher, **{'install-scope': scope, 'install-relative-path': relative}) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual([args[0] for args in processes], + ['/i', '/x', '/i', '/x'] if launcher else ['/i', '/x']) + self.assertEqual(cli_ran, not launcher) + self.assertFalse(injected) + def test_powershell_syntax_in_names_stays_literal(self): + """Powershell syntax in names stays literal.""" for value in ("Craft'; New-Item injected; #", 'Craft$(New-Item injected)', 'Craft`$(New-Item injected)', "Craft's [ARM64] é"): for name in ('app-name', 'app-dir', 'main-binary', 'cli-binary'): @@ -138,6 +187,7 @@ class WindowsArm64Tests(unittest.TestCase): self.assertFalse(injected) def test_invalid_names_fail_before_installing(self): + """Invalid names fail before installing.""" for value in ('../outside', '..\\outside', 'C:outside', 'C:\\outside', '\\\\host\\share', '.', '..', 'trailing.', 'trailing ', 'bad*', 'bad?', 'bad|', 'bad<', 'bad>', 'bad\x01', 'bad\nNew-Item injected', 'bad"; New-Item injected; #'): @@ -156,5 +206,11 @@ class WindowsArm64Tests(unittest.TestCase): self.assertEqual(processes, []) +class PackageCacheTests(unittest.TestCase): + def test_only_main_saves_the_package_cache(self): + cache = next(step for step in DATA['jobs']['package']['steps'] if 'rust-cache' in step.get('uses', '')) + self.assertEqual(cache['with']['save-if'], "${{ github.ref == 'refs/heads/main' }}") + + if __name__ == '__main__': unittest.main() diff --git a/tests/test_windows_install_contract.py b/tests/test_windows_install_contract.py new file mode 100644 index 0000000..d40e3f7 --- /dev/null +++ b/tests/test_windows_install_contract.py @@ -0,0 +1,92 @@ +"""Run the workflow's real input resolution without starting an installer.""" +import json +import os +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + +import yaml + +ROOT = Path(__file__).resolve().parents[1] +DATA = yaml.load((ROOT / '.github/workflows/windows-arm64.yml').read_text(), Loader=yaml.BaseLoader) +STEP = next(step for step in DATA['jobs']['install']['steps'] if 'run' in step) +SHELL = shutil.which('pwsh') or shutil.which('powershell') + + +@unittest.skipUnless(SHELL, 'requires PowerShell') +class InstallContractTests(unittest.TestCase): + def resolve(self, relative='', scope='', launcher=False, app='PhotoCraft'): + """Resolve only the workflow input contract without invoking msiexec.""" + inputs = {'app-name': 'photocraft', 'app-dir': app, 'main-binary': 'photocraft.exe', + 'cli-binary': 'photocraft-cli.exe', 'is-per-user': str(launcher).lower(), + 'install-relative-path': relative, 'install-scope': scope} + env = {key: value for key, value in os.environ.items() + if key.lower() not in ('localappdata', 'programfiles')} + for name, expression in STEP['env'].items(): + for key, value in inputs.items(): + expression = expression.replace('${{ inputs.' + key + ' }}', value) + env[name] = expression + with tempfile.TemporaryDirectory() as directory: + env['LOCALAPPDATA'] = str(Path(directory) / 'local') + env['ProgramFiles'] = str(Path(directory) / 'machine') + env['TEST_PROGRAM_FILES'] = env['ProgramFiles'] + prefix = STEP['run'].split('$msi =', 1)[0] + # Windows PowerShell resets ProgramFiles for its own process architecture. + script = '$env:ProgramFiles = $env:TEST_PROGRAM_FILES\n' + prefix + '\n@{directory=$installDirectory; properties=@($scopeProperties); perUser=$perUser} | ConvertTo-Json -Compress\n' + file = Path(directory) / 'contract.ps1' + file.write_text(script, encoding='utf-8') + result = subprocess.run([SHELL, '-NoProfile', '-File', str(file)], env=env, + capture_output=True, text=True, timeout=20) + data = json.loads(result.stdout) if result.returncode == 0 else None + return result, data, env + + def test_nested_per_user_path_and_explicit_scope(self): + """Nested per user path and explicit scope.""" + result, data, env = self.resolve('Programs/ArtCraft/PhotoCraft', 'perUser') + self.assertEqual(result.returncode, 0, result.stderr) + self.assertIsInstance(data['directory'], str, 'The workflow must resolve the requested install directory.') + self.assertEqual(Path(data['directory']), Path(env['LOCALAPPDATA']) / 'Programs/ArtCraft/PhotoCraft') + self.assertTrue(data['perUser']) + self.assertEqual(data['properties'], ['ALLUSERS=2', 'MSIINSTALLPERUSER=1']) + + def test_machine_scope_and_legacy_defaults(self): + """Machine scope and legacy defaults.""" + for launcher, scope, relative, suffix, user in ( + (False, '', '', 'PhotoCraft', False), + (True, '', '', 'Programs/PhotoCraft', True), + (False, 'perMachine', 'ArtCraft/PhotoCraft', 'ArtCraft/PhotoCraft', False), + ): + with self.subTest(launcher=launcher, scope=scope): + result, data, env = self.resolve(relative, scope, launcher) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(Path(data['directory']), Path(env['LOCALAPPDATA' if user else 'ProgramFiles']) / suffix) + self.assertEqual(data['perUser'], user) + self.assertEqual(data['properties'], [] if not scope else ['ALLUSERS=1', 'MSIINSTALLPERUSER=""']) + + def test_invalid_relative_paths_and_scope_fail_before_install(self): + """Invalid relative paths and scope fail before install.""" + for relative in ('../outside', 'ArtCraft/../outside', '/absolute', 'C:relative', + 'C:/absolute', '//host/share', 'ArtCraft//PhotoCraft', + 'ArtCraft/./PhotoCraft', 'ArtCraft/trailing.', 'ArtCraft/trailing ', + 'ArtCraft/bad*', 'ArtCraft/bad"', 'ArtCraft/bad\nname'): + with self.subTest(relative=relative): + result, _, _ = self.resolve(relative, 'perUser') + self.assertNotEqual(result.returncode, 0) + result, _, _ = self.resolve('ArtCraft/PhotoCraft', 'invalid') + self.assertNotEqual(result.returncode, 0) + result, _, _ = self.resolve('', 'perMachine', True) + self.assertNotEqual(result.returncode, 0) + + def test_path_metacharacters_stay_literal(self): + """Path metacharacters stay literal.""" + for name in ("Craft's [ARM64]", 'Craft$(Write-Output injected)', 'Craft`name'): + with self.subTest(name=name): + result, data, env = self.resolve('Programs/ArtCraft/' + name, 'perUser') + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(Path(data['directory']), Path(env['LOCALAPPDATA']) / 'Programs/ArtCraft' / name) + + +if __name__ == '__main__': + unittest.main()