Unverified Commit 0ed94a56 authored by Florian Meriaux's avatar Florian Meriaux Committed by GitHub
Browse files

Add SBT in cache managers. (#302)

parent 9519cf13
Loading
Loading
Loading
Loading
+96
Changes for .github/workflows/e2e-cache.yml: 96 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -111,3 +111,99 @@ jobs:
            exit 1
          fi
          ls ~/.m2/repository
  sbt-save:
    runs-on: ${{ matrix.os }}
    defaults:
      run:
        shell: bash
        working-directory: __tests__/cache/sbt
    strategy:
      fail-fast: false
      matrix:
        os: [macos-latest, windows-latest, ubuntu-latest]
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Run setup-java with the cache for sbt
        uses: ./
        id: setup-java
        with:
          distribution: 'adopt'
          java-version: '11'
          cache: sbt
      - name: Create files to cache
        run: sbt update

      - name: Check files to cache on macos-latest
        if: matrix.os == 'macos-latest'
        run: |
          if [ ! -d ~/Library/Caches/Coursier ]; then
            echo "::error::The ~/Library/Caches/Coursier directory does not exist unexpectedly"
            exit 1
          fi

      - name: Check files to cache on windows-latest
        if: matrix.os == 'windows-latest'
        run: |
          if [ ! -d ~/AppData/Local/Coursier/Cache ]; then
            echo "::error::The ~/AppData/Local/Coursier/Cache directory does not exist unexpectedly"
            exit 1
          fi

      
      - name: Check files to cache on ubuntu-latest
        if: matrix.os == 'ubuntu-latest'
        run: |
          if [ ! -d ~/.cache/coursier ]; then
            echo "::error::The ~/.cache/coursier directory does not exist unexpectedly"
            exit 1
          fi

  sbt-restore:
    runs-on: ${{ matrix.os }}
    defaults:
      run:
        shell: bash
        working-directory: __tests__/cache/sbt
    strategy:
      fail-fast: false
      matrix:
        os: [macos-latest, windows-latest, ubuntu-latest]
    needs: sbt-save
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Run setup-java with the cache for sbt
        uses: ./
        id: setup-java
        with:
          distribution: 'adopt'
          java-version: '11'
          cache: sbt

      - name: Confirm that ~/Library/Caches/Coursier directory has been made
        if: matrix.os == 'macos-latest'
        run: |
          if [ ! -d ~/Library/Caches/Coursier ]; then
            echo "::error::The ~/Library/Caches/Coursier directory does not exist unexpectedly"
            exit 1
          fi
          ls ~/Library/Caches/Coursier

      - name: Confirm that ~/AppData/Local/Coursier/Cache directory has been made
        if: matrix.os == 'windows-latest'
        run: |
          if [ ! -d ~/AppData/Local/Coursier/Cache ]; then
            echo "::error::The ~/AppData/Local/Coursier/Cache directory does not exist unexpectedly"
            exit 1
          fi
          ls ~/AppData/Local/Coursier/Cache

      - name: Confirm that ~/.cache/coursier directory has been made
        if: matrix.os == 'ubuntu-latest'
        run: |
          if [ ! -d ~/.cache/coursier ]; then
            echo "::error::The ~/.cache/coursier directory does not exist unexpectedly"
            exit 1
          fi
          ls ~/.cache/coursier
+14
Changes for README.md: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@ Currently, the following distributions are supported:
The action has a built-in functionality for caching and restoring dependencies. It uses [actions/cache](https://github.com/actions/cache) under hood for caching dependencies but requires less configuration settings. Supported package managers are gradle and maven. The format of the used cache key is `setup-java-${{ platform }}-${{ packageManager }}-${{ fileHash }}`, where the hash is based on the following files:
- gradle: `**/*.gradle*`, `**/gradle-wrapper.properties`
- maven: `**/pom.xml`
- sbt:  `**/build.sbt`

The workflow output `cache-hit` is set to indicate if an exact match was found for the key [as actions/cache does](https://github.com/actions/cache/tree/main#outputs).

@@ -99,6 +100,19 @@ steps:
  run: mvn -B package --file pom.xml
```

#### Caching sbt dependencies
```yaml
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
  with:
    distribution: 'temurin'
    java-version: '11'
    cache: 'sbt'
- name: Build with SBT
  run: sbt package
```

### Check latest
In the basic examples above, the `check-latest` flag defaults to `false`. When set to `false`, the action tries to first resolve a version of Java from the local tool cache on the runner. If unable to find a specific version in the cache, the action will download a version of Java. Use the default or set `check-latest` to `false` if you prefer a faster more consistent setup experience that prioritizes trying to use the cached versions at the expense of newer versions sometimes being available for download.

+41
Changes for __tests__/cache.test.ts: 41 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -118,6 +118,23 @@ describe('dependency cache', () => {
        expect(spyInfo).toBeCalledWith('gradle cache is not found');
      });
    });
    describe('for sbt', () => {
      it('throws error if no build.sbt found', async () => {
        await expect(restore('sbt')).rejects.toThrowError(
          `No file in ${projectRoot(
            workspace
          )} matched to [**/*.sbt,**/project/build.properties,**/project/**.{scala,sbt}], make sure you have checked out the target repository`
        );
      });
      it('downloads cache', async () => {
        createFile(join(workspace, 'build.sbt'));

        await restore('sbt');
        expect(spyCacheRestore).toBeCalled();
        expect(spyWarning).not.toBeCalled();
        expect(spyInfo).toBeCalledWith('sbt cache is not found');
      });
    });
  });
  describe('save', () => {
    let spyCacheSave: jest.SpyInstance<
@@ -194,6 +211,30 @@ describe('dependency cache', () => {
        expect(spyInfo).toBeCalledWith(expect.stringMatching(/^Cache saved with the key:.*/));
      });
    });
    describe('for sbt', () => {
      it('uploads cache even if no build.sbt found', async () => {
        createStateForMissingBuildFile();
        await save('sbt');
        expect(spyCacheSave).toBeCalled();
        expect(spyWarning).not.toBeCalled();
      });
      it('does not upload cache if no restore run before', async () => {
        createFile(join(workspace, 'build.sbt'));

        await save('sbt');
        expect(spyCacheSave).not.toBeCalled();
        expect(spyWarning).toBeCalledWith('Error retrieving key from state.');
      });
      it('uploads cache', async () => {
        createFile(join(workspace, 'build.sbt'));
        createStateForSuccessfulRestore();

        await save('sbt');
        expect(spyCacheSave).toBeCalled();
        expect(spyWarning).not.toBeCalled();
        expect(spyInfo).toBeCalledWith(expect.stringMatching(/^Cache saved with the key:.*/));
      });
    });
  });
});

+1
Changes for __tests__/cache/sbt/.gitignore: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
target/
 No newline at end of file
+3
Changes for __tests__/cache/sbt/build.sbt: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
ThisBuild / scalaVersion := "2.12.15"

libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.1.2"
 No newline at end of file
Loading