-
Notifications
You must be signed in to change notification settings - Fork 690
Implementation of python's caching #266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MaksimZhukov
merged 13 commits into
actions:main
from
dmitry-shibanov:v-dmshib/add-cache-support
Nov 17, 2021
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
952fef3
add support for caching
dmitry-shibanov c98dcde
resolving comments
dmitry-shibanov b321c7b
add unit and e2e tests
dmitry-shibanov 0860a2d
work on fixing errors
dmitry-shibanov 3a7c99c
try to use pipx and inline pip tools installation
dmitry-shibanov 73eda7c
remove pypy-2.7 from e2e-cache
dmitry-shibanov 87683fb
add documentation
dmitry-shibanov f3bb768
remove async from getCacheDistributor
dmitry-shibanov 07c09af
fix cache restore tests
dmitry-shibanov 1918fc7
resolving comments
dmitry-shibanov f823876
fixing docs
dmitry-shibanov aaeea74
resolve comments and add licenses
dmitry-shibanov 204896d
format code
dmitry-shibanov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| name: e2e-cache | ||
|
|
||
| on: | ||
| pull_request: | ||
| paths-ignore: | ||
| - '**.md' | ||
| push: | ||
| branches: | ||
| - main | ||
| - releases/* | ||
| paths-ignore: | ||
| - '**.md' | ||
|
|
||
| permissions: | ||
| contents: read | ||
| jobs: | ||
| python-pip-depencies-caching: | ||
| name: Test pip (Python ${{ matrix.python-version}}, ${{ matrix.os }}) | ||
| runs-on: ${{ matrix.os }} | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| os: [ubuntu-latest, windows-latest, macos-latest] | ||
| python-version: ['3.7', '3.8', '3.9', 'pypy-3.7-v7.3.5', 'pypy-3.7-v7.x'] | ||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Setup Python | ||
| uses: ./ | ||
| with: | ||
| python-version: ${{ matrix.python-version }} | ||
| cache: 'pip' | ||
| - name: Install dependencies | ||
| run: pip install numpy pandas requests | ||
|
|
||
| python-pipenv-dependencies-caching: | ||
| name: Test pipenv (Python ${{ matrix.python-version}}, ${{ matrix.os }}) | ||
| runs-on: ${{ matrix.os }} | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| os: [ubuntu-latest, windows-latest, macos-latest] | ||
| python-version: ['3.7', '3.8', '3.9', 'pypy-3.7-v7.3.5', 'pypy-3.7-v7.x'] | ||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Install pipenv | ||
| run: pipx install pipenv | ||
| - name: Setup Python | ||
| uses: ./ | ||
| with: | ||
| python-version: ${{ matrix.python-version }} | ||
| cache: 'pipenv' | ||
| - name: Install dependencies | ||
| run: pipenv install flake8 | ||
|
|
||
| python-pip-dependencies-caching-path: | ||
| name: Test pip (Python ${{ matrix.python-version}}, ${{ matrix.os }}) | ||
| runs-on: ${{ matrix.os }} | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| os: [ubuntu-latest, windows-latest, macos-latest] | ||
| python-version: ['3.7', '3.8', '3.9', 'pypy-3.7-v7.3.5', 'pypy-3.7-v7.x'] | ||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Setup Python | ||
| uses: ./ | ||
| with: | ||
| python-version: ${{ matrix.python-version }} | ||
| cache: 'pip' | ||
| cache-dependency-path: __tests__/data/requirements.txt | ||
| - name: Install dependencies | ||
| run: pip install numpy pandas requests | ||
|
|
||
| python-pipenv-dependencies-caching-path: | ||
| name: Test pipenv (Python ${{ matrix.python-version}}, ${{ matrix.os }}) | ||
| runs-on: ${{ matrix.os }} | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| os: [ubuntu-latest, windows-latest, macos-latest] | ||
| python-version: ['3.7', '3.8', '3.9', 'pypy-3.7-v7.3.5', 'pypy-3.7-v7.x'] | ||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Install pipenv | ||
| run: pipx install pipenv | ||
| - name: Setup Python | ||
| uses: ./ | ||
| with: | ||
| python-version: ${{ matrix.python-version }} | ||
| cache: 'pipenv' | ||
| cache-dependency-path: '**/requirements-linux.txt' | ||
| - name: Install dependencies | ||
| run: pipenv install flake8 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| import * as core from '@actions/core'; | ||
| import * as cache from '@actions/cache'; | ||
| import * as exec from '@actions/exec'; | ||
| import {getCacheDistributor} from '../src/cache-distributions/cache-factory'; | ||
|
|
||
| describe('restore-cache', () => { | ||
| const pipFileLockHash = | ||
| '67d817abcde9c72da0ed5b8f235647cb14638b9ff9d742b42e4406d2eb16fe3c'; | ||
| const requirementsHash = | ||
| 'd8110e0006d7fb5ee76365d565eef9d37df1d11598b912d3eb66d398d57a1121'; | ||
| const requirementsLinuxHash = | ||
| '2d0ff7f46b0e120e3d3294db65768b474934242637b9899b873e6283dfd16d7c'; | ||
|
|
||
| // core spy | ||
| let infoSpy: jest.SpyInstance; | ||
| let warningSpy: jest.SpyInstance; | ||
| let debugSpy: jest.SpyInstance; | ||
| let saveSatetSpy: jest.SpyInstance; | ||
| let getStateSpy: jest.SpyInstance; | ||
|
|
||
| // cache spy | ||
| let restoreCacheSpy: jest.SpyInstance; | ||
|
|
||
| // exec spy | ||
| let getExecOutputSpy: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| process.env['RUNNER_OS'] = process.env['RUNNER_OS'] ?? 'linux'; | ||
|
|
||
| infoSpy = jest.spyOn(core, 'info'); | ||
| infoSpy.mockImplementation(input => undefined); | ||
|
|
||
| warningSpy = jest.spyOn(core, 'warning'); | ||
| warningSpy.mockImplementation(input => undefined); | ||
|
|
||
| debugSpy = jest.spyOn(core, 'debug'); | ||
| debugSpy.mockImplementation(input => undefined); | ||
|
|
||
| saveSatetSpy = jest.spyOn(core, 'saveState'); | ||
| saveSatetSpy.mockImplementation(input => undefined); | ||
|
|
||
| getStateSpy = jest.spyOn(core, 'getState'); | ||
| getStateSpy.mockImplementation(input => undefined); | ||
|
|
||
| getExecOutputSpy = jest.spyOn(exec, 'getExecOutput'); | ||
| getExecOutputSpy.mockImplementation((input: string) => { | ||
| if (input.includes('pip')) { | ||
| return {stdout: 'pip', stderr: '', exitCode: 0}; | ||
| } | ||
|
|
||
| return {stdout: '', stderr: 'Error occured', exitCode: 2}; | ||
| }); | ||
|
|
||
| restoreCacheSpy = jest.spyOn(cache, 'restoreCache'); | ||
| restoreCacheSpy.mockImplementation( | ||
| (cachePaths: string[], primaryKey: string, restoreKey?: string) => { | ||
| return primaryKey; | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| describe('Validate provided package manager', () => { | ||
| it.each(['npm', 'pip2', 'pip21', 'pip21.3', 'pipenv32'])( | ||
| 'Throw an error because %s is not supported', | ||
| async packageManager => { | ||
| expect(() => | ||
| getCacheDistributor(packageManager, '3.8.12', undefined) | ||
| ).toThrowError(`Caching for '${packageManager}' is not supported`); | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| describe('Restore dependencies', () => { | ||
| it.each([ | ||
| ['pip', '3.8.12', undefined, requirementsHash], | ||
| ['pip', '3.8.12', '**/requirements-linux.txt', requirementsLinuxHash], | ||
| [ | ||
| 'pip', | ||
| '3.8.12', | ||
| '__tests__/data/requirements-linux.txt', | ||
| requirementsLinuxHash | ||
| ], | ||
| ['pip', '3.8.12', '__tests__/data/requirements.txt', requirementsHash], | ||
| ['pipenv', '3.9.1', undefined, pipFileLockHash], | ||
| ['pipenv', '3.9.12', '__tests__/data/requirements.txt', requirementsHash] | ||
| ])( | ||
| 'restored dependencies for %s by primaryKey', | ||
| async (packageManager, pythonVersion, dependencyFile, fileHash) => { | ||
| const cacheDistributor = await getCacheDistributor( | ||
| packageManager, | ||
| pythonVersion, | ||
| dependencyFile | ||
| ); | ||
| await cacheDistributor.restoreCache(); | ||
| let pythonKey = ''; | ||
| if (packageManager === 'pipenv') { | ||
| pythonKey = `python-${pythonVersion}-`; | ||
| } | ||
|
|
||
| expect(infoSpy).toHaveBeenCalledWith( | ||
| `Cache restored from key: setup-python-${process.env['RUNNER_OS']}-${pythonKey}${packageManager}-${fileHash}` | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| it.each([ | ||
| ['pip', '3.8.12', 'requirements-linux.txt', 'requirements-linux.txt'], | ||
| ['pip', '3.8.12', 'requirements.txt', 'requirements.txt'], | ||
| ['pipenv', '3.9.12', 'requirements.txt', 'requirements.txt'] | ||
| ])( | ||
| 'Should throw an error because dependency file is not found', | ||
| async ( | ||
| packageManager, | ||
| pythonVersion, | ||
| dependencyFile, | ||
| cacheDependencyPath | ||
| ) => { | ||
| const cacheDistributor = await getCacheDistributor( | ||
| packageManager, | ||
| pythonVersion, | ||
| dependencyFile | ||
| ); | ||
| await expect(cacheDistributor.restoreCache()).rejects.toThrowError( | ||
| `No file in ${process.cwd()} matched to [${cacheDependencyPath | ||
| .split('\n') | ||
| .join(',')}], make sure you have checked out the target repository` | ||
| ); | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| describe('Dependencies changed', () => { | ||
| it.each([ | ||
| ['pip', '3.8.12', undefined, pipFileLockHash], | ||
| ['pip', '3.8.12', '**/requirements-linux.txt', pipFileLockHash], | ||
| [ | ||
| 'pip', | ||
| '3.8.12', | ||
| '__tests__/data/requirements-linux.txt', | ||
| pipFileLockHash | ||
| ], | ||
| ['pip', '3.8.12', '__tests__/data/requirements.txt', pipFileLockHash], | ||
| ['pipenv', '3.9.1', undefined, requirementsHash], | ||
| ['pipenv', '3.9.12', '__tests__/data/requirements.txt', requirementsHash] | ||
| ])( | ||
| 'restored dependencies for %s by primaryKey', | ||
| async (packageManager, pythonVersion, dependencyFile, fileHash) => { | ||
| restoreCacheSpy.mockImplementation( | ||
| (cachePaths: string[], primaryKey: string, restoreKey?: string) => { | ||
| return primaryKey !== fileHash && restoreKey ? pipFileLockHash : ''; | ||
| } | ||
| ); | ||
| const cacheDistributor = await getCacheDistributor( | ||
| packageManager, | ||
| pythonVersion, | ||
| dependencyFile | ||
| ); | ||
| await cacheDistributor.restoreCache(); | ||
| let result = ''; | ||
| if (packageManager !== 'pipenv') { | ||
| result = `Cache restored from key: ${fileHash}`; | ||
| } else { | ||
| result = 'pipenv cache is not found'; | ||
| } | ||
|
|
||
| expect(infoSpy).toHaveBeenCalledWith(result); | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.resetAllMocks(); | ||
| jest.clearAllMocks(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.