name: Validate cache

on:
  push:
    branches:
      - main
      - releases/*
    paths-ignore:
      - '**.md'
  pull_request:
    paths-ignore:
      - '**.md'

permissions:
  contents: read

defaults:
  run:
    shell: bash

jobs:
  gradle-save:
    runs-on: ${{ matrix.os }}
    cache-mode: write-only
    strategy:
      fail-fast: false
      matrix:
        os: [macos-15-intel, windows-latest, ubuntu-latest]
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with the cache for gradle
        uses: ./
        id: setup-java
        with:
          distribution: 'temurin'
          java-version: '17'
          cache: gradle
      - name: Create files to cache
        # Need to avoid using Gradle daemon to stabilize the save process on Windows
        # https://github.com/actions/cache/issues/454#issuecomment-840493935
        run: |
          gradle downloadDependencies --no-daemon -p __tests__/cache/gradle1
          mkdir -p "$HOME/.gradle/wrapper/dists/setup-java-e2e"
          echo "gradle wrapper cache" > "$HOME/.gradle/wrapper/dists/setup-java-e2e/payload"
          bash __tests__/check-dir.sh "$HOME/.gradle/caches"
          bash __tests__/check-dir.sh "$HOME/.gradle/wrapper/dists"
  gradle-restore:
    runs-on: ${{ matrix.os }}
    cache-mode: read
    strategy:
      fail-fast: false
      matrix:
        os: [macos-15-intel, windows-latest, ubuntu-latest]
    needs: gradle-save
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with the cache for gradle
        uses: ./
        id: setup-java
        with:
          distribution: 'temurin'
          java-version: '11'
          cache: gradle
          cache-read-only: true
      - name: Confirm that ~/.gradle/caches directory has been made
        run: bash __tests__/check-dir.sh "$HOME/.gradle/caches"
      - name: Confirm that the Gradle Wrapper cache has been restored
        run: bash __tests__/check-dir.sh "$HOME/.gradle/wrapper/dists"
  maven-save:
    runs-on: ${{ matrix.os }}
    cache-mode: write-only
    strategy:
      fail-fast: false
      matrix:
        os: [macos-15-intel, windows-latest, ubuntu-latest]
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with the cache for maven
        uses: ./
        id: setup-java
        with:
          distribution: 'temurin'
          java-version: '11'
          cache: maven
      - name: Create files to cache
        run: |
          mvn verify -f __tests__/cache/maven/pom.xml
          mkdir -p "$HOME/.m2/wrapper/dists/setup-java-e2e"
          echo "maven wrapper cache" > "$HOME/.m2/wrapper/dists/setup-java-e2e/payload"
          bash __tests__/check-dir.sh "$HOME/.m2/repository"
          bash __tests__/check-dir.sh "$HOME/.m2/wrapper/dists"
  maven-restore:
    runs-on: ${{ matrix.os }}
    cache-mode: read
    strategy:
      fail-fast: false
      matrix:
        os: [macos-15-intel, windows-latest, ubuntu-latest]
    needs: maven-save
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with the cache for maven
        uses: ./
        id: setup-java
        with:
          distribution: 'temurin'
          java-version: '11'
          cache: maven
          cache-read-only: true
      - name: Confirm that ~/.m2/repository directory has been made
        run: bash __tests__/check-dir.sh "$HOME/.m2/repository"
      - name: Confirm that the Maven Wrapper cache has been restored
        run: bash __tests__/check-dir.sh "$HOME/.m2/wrapper/dists"
  sbt-save:
    runs-on: ${{ matrix.os }}
    cache-mode: write-only
    defaults:
      run:
        shell: bash
        working-directory: __tests__/cache/sbt
    strategy:
      fail-fast: false
      matrix:
        os: [macos-15-intel, windows-latest, ubuntu-22.04]
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with the cache for sbt
        uses: ./
        id: setup-java
        with:
          distribution: 'temurin'
          java-version: '11'
          cache: sbt
      - name: Setup SBT
        if: matrix.os == 'macos-15-intel'
        run: |
          echo ""Installing SBT...""
          brew install sbt
      - name: Create files to cache
        run: sbt update

      - name: Check files to cache on macos-latest
        if: matrix.os == 'macos-15-intel'
        run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/Library/Caches/Coursier"
      - name: Check files to cache on windows-latest
        if: matrix.os == 'windows-latest'
        run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/AppData/Local/Coursier/Cache"
      - name: Check files to cache on ubuntu-latest
        if: matrix.os == 'ubuntu-22.04'
        run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/.cache/coursier"
  sbt-restore:
    runs-on: ${{ matrix.os }}
    cache-mode: read
    defaults:
      run:
        shell: bash
        working-directory: __tests__/cache/sbt
    strategy:
      fail-fast: false
      matrix:
        os: [macos-15-intel, windows-latest, ubuntu-22.04]
    needs: sbt-save
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with the cache for sbt
        uses: ./
        id: setup-java
        with:
          distribution: 'temurin'
          java-version: '11'
          cache: sbt
          cache-read-only: true

      - name: Confirm that ~/Library/Caches/Coursier directory has been made
        if: matrix.os == 'macos-15-intel'
        run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/Library/Caches/Coursier"
      - name: Confirm that ~/AppData/Local/Coursier/Cache directory has been made
        if: matrix.os == 'windows-latest'
        run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/AppData/Local/Coursier/Cache"
      - name: Confirm that ~/.cache/coursier directory has been made
        if: matrix.os == 'ubuntu-22.04'
        run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/.cache/coursier"
  gradle1-save:
    runs-on: ${{ matrix.os }}
    cache-mode: write-only
    strategy:
      fail-fast: false
      matrix:
        os: [macos-latest, windows-latest, ubuntu-latest]
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with the cache for gradle
        uses: ./
        id: setup-java
        with:
          distribution: 'temurin'
          java-version: '17'
          cache: gradle
          cache-dependency-path: __tests__/cache/gradle1/*.gradle*
      - name: Create files to cache
        # Need to avoid using Gradle daemon to stabilize the save process on Windows
        # https://github.com/actions/cache/issues/454#issuecomment-840493935
        run: |
          gradle downloadDependencies --no-daemon -p __tests__/cache/gradle1
          mkdir -p "$HOME/.gradle/wrapper/dists/setup-java-e2e-gradle1"
          echo "gradle wrapper cache gradle1" > "$HOME/.gradle/wrapper/dists/setup-java-e2e-gradle1/payload"
          bash __tests__/check-dir.sh "$HOME/.gradle/caches"
          bash __tests__/check-dir.sh "$HOME/.gradle/wrapper/dists"
  gradle1-restore:
    runs-on: ${{ matrix.os }}
    cache-mode: read
    strategy:
      fail-fast: false
      matrix:
        os: [macos-latest, windows-latest, ubuntu-latest]
    needs: gradle1-save
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with the cache for gradle
        uses: ./
        id: setup-java
        with:
          distribution: 'temurin'
          java-version: '11'
          cache: gradle
          cache-dependency-path: __tests__/cache/gradle1/*.gradle*
      - name: Confirm that ~/.gradle/caches directory has been made
        run: bash __tests__/check-dir.sh "$HOME/.gradle/caches"
      - name: Confirm that the Gradle Wrapper cache has been restored
        run: bash __tests__/check-dir.sh "$HOME/.gradle/wrapper/dists"
  gradle2-restore:
    runs-on: ${{ matrix.os }}
    cache-mode: read
    strategy:
      fail-fast: false
      matrix:
        os: [macos-latest, windows-latest, ubuntu-latest]
    needs: gradle1-save
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with the cache for gradle
        uses: ./
        id: setup-java
        with:
          distribution: 'temurin'
          java-version: '11'
          cache: gradle
          cache-dependency-path: __tests__/cache/gradle2/*.gradle*
      - name: Confirm that ~/.gradle/caches directory has not been made
        run: bash __tests__/check-dir.sh "$HOME/.gradle/caches" absent
  maven1-save:
    runs-on: ${{ matrix.os }}
    cache-mode: write-only
    strategy:
      fail-fast: false
      matrix:
        os: [macos-15-intel, windows-latest, ubuntu-latest]
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with the cache for maven
        uses: ./
        id: setup-java
        with:
          distribution: 'temurin'
          java-version: '11'
          cache: maven
          cache-dependency-path: __tests__/cache/maven/pom.xml
      - name: Create files to cache
        run: |
          mvn verify -f __tests__/cache/maven/pom.xml
          mkdir -p "$HOME/.m2/wrapper/dists/setup-java-e2e-maven1"
          echo "maven wrapper cache maven1" > "$HOME/.m2/wrapper/dists/setup-java-e2e-maven1/payload"
          bash __tests__/check-dir.sh "$HOME/.m2/repository"
          bash __tests__/check-dir.sh "$HOME/.m2/wrapper/dists"
  maven1-restore:
    runs-on: ${{ matrix.os }}
    cache-mode: read
    strategy:
      fail-fast: false
      matrix:
        os: [macos-15-intel, windows-latest, ubuntu-latest]
    needs: maven1-save
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with the cache for maven
        uses: ./
        id: setup-java
        with:
          distribution: 'temurin'
          java-version: '11'
          cache: maven
          cache-dependency-path: __tests__/cache/maven/pom.xml
      - name: Confirm that ~/.m2/repository directory has been made
        run: bash __tests__/check-dir.sh "$HOME/.m2/repository"
      - name: Confirm that the Maven Wrapper cache has been restored
        run: bash __tests__/check-dir.sh "$HOME/.m2/wrapper/dists"
  maven2-restore:
    runs-on: ${{ matrix.os }}
    cache-mode: read
    strategy:
      fail-fast: false
      matrix:
        os: [macos-15-intel, windows-latest, ubuntu-latest]
    needs: maven1-save
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with the cache for maven
        uses: ./
        id: setup-java
        with:
          distribution: 'temurin'
          java-version: '11'
          cache: maven
          cache-dependency-path: |
            __tests__/cache/maven2/pom.xml
            README.md
      - name: Confirm that ~/.m2/repository directory has not been made
        run: bash __tests__/check-dir.sh "$HOME/.m2/repository" absent
  sbt1-save:
    runs-on: ${{ matrix.os }}
    cache-mode: write-only
    defaults:
      run:
        shell: bash
        working-directory: __tests__/cache/sbt
    strategy:
      fail-fast: false
      matrix:
        os: [macos-15-intel, windows-latest, ubuntu-22.04]
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with the cache for sbt
        uses: ./
        id: setup-java
        with:
          distribution: 'temurin'
          java-version: '11'
          cache: sbt
          cache-dependency-path: __tests__/cache/sbt/*.sbt
      - name: Setup SBT
        if: matrix.os == 'macos-15-intel'
        run: |
          echo ""Installing SBT...""
          brew install sbt
      - name: Create files to cache
        run: sbt update

      - name: Check files to cache on macos-latest
        if: matrix.os == 'macos-15-intel'
        run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/Library/Caches/Coursier"
      - name: Check files to cache on windows-latest
        if: matrix.os == 'windows-latest'
        run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/AppData/Local/Coursier/Cache"
      - name: Check files to cache on ubuntu-latest
        if: matrix.os == 'ubuntu-22.04'
        run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/.cache/coursier"
  sbt1-restore:
    runs-on: ${{ matrix.os }}
    cache-mode: read
    defaults:
      run:
        shell: bash
        working-directory: __tests__/cache/sbt
    strategy:
      fail-fast: false
      matrix:
        os: [macos-15-intel, windows-latest, ubuntu-22.04]
    needs: sbt1-save
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with the cache for sbt
        uses: ./
        id: setup-java
        with:
          distribution: 'temurin'
          java-version: '11'
          cache: sbt
          cache-dependency-path: __tests__/cache/sbt/*.sbt

      - name: Confirm that ~/Library/Caches/Coursier directory has been made
        if: matrix.os == 'macos-15-intel'
        run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/Library/Caches/Coursier"
      - name: Confirm that ~/AppData/Local/Coursier/Cache directory has been made
        if: matrix.os == 'windows-latest'
        run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/AppData/Local/Coursier/Cache"
      - name: Confirm that ~/.cache/coursier directory has been made
        if: matrix.os == 'ubuntu-22.04'
        run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/.cache/coursier"
  sbt2-restore:
    runs-on: ${{ matrix.os }}
    cache-mode: read
    defaults:
      run:
        shell: bash
        working-directory: __tests__/cache/sbt2
    strategy:
      fail-fast: false
      matrix:
        os: [macos-15-intel, windows-latest, ubuntu-22.04]
    needs: sbt1-save
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with the cache for sbt
        uses: ./
        id: setup-java
        with:
          distribution: 'temurin'
          java-version: '11'
          cache: sbt
          cache-dependency-path: __tests__/cache/sbt2/*.sbt

      - name: Confirm that ~/Library/Caches/Coursier directory has not been made
        if: matrix.os == 'macos-15-intel'
        run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/Library/Caches/Coursier" absent
      - name: Confirm that ~/AppData/Local/Coursier/Cache directory has not been made
        if: matrix.os == 'windows-latest'
        run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/AppData/Local/Coursier/Cache" absent
      - name: Confirm that ~/.cache/coursier directory has not been made
        if: matrix.os == 'ubuntu-22.04'
        run: bash "$GITHUB_WORKSPACE/__tests__/check-dir.sh" "$HOME/.cache/coursier" absent
  custom-maven-path-save:
    runs-on: ubuntu-latest
    cache-mode: write-only
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with a custom Maven cache path
        uses: ./
        with:
          distribution: 'temurin'
          java-version: '11'
          cache: maven
          cache-dependency-path: |
            __tests__/cache/maven2/pom.xml
            .github/workflows/e2e-cache.yml
          cache-path: |
            ${{ runner.temp }}/setup-java-custom-maven-repository
            !${{ runner.temp }}/setup-java-custom-maven-repository/**/*.lastUpdated
      - name: Populate the custom Maven repository
        run: |
          mvn -Dmaven.repo.local="$RUNNER_TEMP/setup-java-custom-maven-repository" verify -f __tests__/cache/maven2/pom.xml
          touch "$RUNNER_TEMP/setup-java-custom-maven-repository/setup-java-cache-path-marker"
          bash __tests__/check-dir.sh "$RUNNER_TEMP/setup-java-custom-maven-repository"
  custom-maven-path-restore:
    runs-on: ubuntu-latest
    cache-mode: read
    needs: custom-maven-path-save
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Run setup-java with a custom Maven cache path
        uses: ./
        with:
          distribution: 'temurin'
          java-version: '11'
          cache: maven
          cache-dependency-path: |
            __tests__/cache/maven2/pom.xml
            .github/workflows/e2e-cache.yml
          cache-path: |
            ${{ runner.temp }}/setup-java-custom-maven-repository
            !${{ runner.temp }}/setup-java-custom-maven-repository/**/*.lastUpdated
          cache-read-only: true
      - name: Confirm that the custom Maven repository has been restored
        run: test -f "$RUNNER_TEMP/setup-java-custom-maven-repository/setup-java-cache-path-marker"
