From f4b85ae24e3deb0b31366c598a66e08bb4d4bc32 Mon Sep 17 00:00:00 2001 From: Sven Serlier <85389871+wrt54g@users.noreply.github.com> Date: Sun, 3 Jul 2022 20:10:08 +0200 Subject: [PATCH 01/10] Update actions --- .github/workflows/check-dist.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/check-dist.yml b/.github/workflows/check-dist.yml index 4c3d69e..6274fd2 100644 --- a/.github/workflows/check-dist.yml +++ b/.github/workflows/check-dist.yml @@ -21,7 +21,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set Node.js 16.x uses: actions/setup-node@v3 @@ -45,7 +45,7 @@ jobs: id: diff # If index.js was different than expected, upload the expected version as an artifact - - uses: actions/upload-artifact@v2 + - uses: actions/upload-artifact@v3 if: ${{ failure() && steps.diff.conclusion == 'failure' }} with: name: dist From 5a1dd6b34d3ec33151c395cf8fc433629fdcc47c Mon Sep 17 00:00:00 2001 From: Sven Serlier <85389871+wrt54g@users.noreply.github.com> Date: Sun, 3 Jul 2022 20:11:27 +0200 Subject: [PATCH 02/10] Update actions --- .github/workflows/codeql-analysis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index c30fab4..3ea240d 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -18,11 +18,11 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v3 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v1 + uses: github/codeql-action/init@v2 # Override language selection by uncommenting this and choosing your languages # with: # languages: go, javascript, csharp, python, cpp, java @@ -30,7 +30,7 @@ jobs: # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). # If this step fails, then you should remove it and run the build manually (see below) - name: Autobuild - uses: github/codeql-action/autobuild@v1 + uses: github/codeql-action/autobuild@v2 # ℹī¸ Command-line programs to run using the OS shell. # 📚 https://git.io/JvXDl @@ -44,4 +44,4 @@ jobs: # make release - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 + uses: github/codeql-action/analyze@v2 From 7d9c63da1be5117e942432dfba15cb5583b6f811 Mon Sep 17 00:00:00 2001 From: Sven Serlier <85389871+wrt54g@users.noreply.github.com> Date: Sun, 3 Jul 2022 20:13:21 +0200 Subject: [PATCH 03/10] Update actions/checkout to v3 --- .github/workflows/licensed.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/licensed.yml b/.github/workflows/licensed.yml index a78560b..6f4cd92 100644 --- a/.github/workflows/licensed.yml +++ b/.github/workflows/licensed.yml @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-latest name: Check licenses steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set Node.js 16.x uses: actions/setup-node@v3 with: From d08a9d79f10546626afc655b2a5376f88e0126ff Mon Sep 17 00:00:00 2001 From: Sven Serlier <85389871+wrt54g@users.noreply.github.com> Date: Sun, 3 Jul 2022 20:13:56 +0200 Subject: [PATCH 04/10] Update actions --- .github/workflows/release-new-action-version.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release-new-action-version.yml b/.github/workflows/release-new-action-version.yml index 955ced7..968c77f 100644 --- a/.github/workflows/release-new-action-version.yml +++ b/.github/workflows/release-new-action-version.yml @@ -21,7 +21,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Update the ${{ env.TAG_NAME }} tag - uses: actions/publish-action@v0.1.0 + uses: actions/publish-action@v0.2.0 with: source-tag: ${{ env.TAG_NAME }} - slack-webhook: ${{ secrets.SLACK_WEBHOOK }} \ No newline at end of file + slack-webhook: ${{ secrets.SLACK_WEBHOOK }} From 4a7ca55b4090b249d2dbdb572c6bf83ca085ae44 Mon Sep 17 00:00:00 2001 From: Sven Serlier <85389871+wrt54g@users.noreply.github.com> Date: Sun, 3 Jul 2022 20:14:29 +0200 Subject: [PATCH 05/10] Update actions --- .github/workflows/test-pypy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test-pypy.yml b/.github/workflows/test-pypy.yml index e4a53bb..4800662 100644 --- a/.github/workflows/test-pypy.yml +++ b/.github/workflows/test-pypy.yml @@ -34,7 +34,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: setup-python ${{ matrix.pypy }} id: setup-python From b318cecd93bd700ebeebeba61bcae2f0231111ce Mon Sep 17 00:00:00 2001 From: Sven Serlier <85389871+wrt54g@users.noreply.github.com> Date: Sun, 3 Jul 2022 20:15:21 +0200 Subject: [PATCH 06/10] Update actions/checkout to v3 --- .github/workflows/workflow.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 171e205..5c8cc77 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -17,7 +17,7 @@ jobs: operating-system: [ubuntu-latest, windows-latest] steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Set Node.js 16.x uses: actions/setup-node@v3 From b88a682917db311eac9aab40569d7ee9db572924 Mon Sep 17 00:00:00 2001 From: IvanZosimov Date: Fri, 15 Jul 2022 16:52:20 +0200 Subject: [PATCH 07/10] Fix resolveVersionInput() logic --- dist/setup/index.js | 15 +++++++++------ src/setup-python.ts | 22 ++++++++++++---------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/dist/setup/index.js b/dist/setup/index.js index 9ac89a4..57bd35f 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -65262,17 +65262,20 @@ function resolveVersionInput() { } if (versionFile) { if (!fs_1.default.existsSync(versionFile)) { - logWarning(`The specified python version file at: ${versionFile} doesn't exist. Attempting to find .python-version file.`); - versionFile = '.python-version'; - if (!fs_1.default.existsSync(versionFile)) { - throw new Error(`The ${versionFile} doesn't exist.`); - } + throw new Error(`The specified python version file at: ${versionFile} doesn't exist.`); } version = fs_1.default.readFileSync(versionFile, 'utf8'); core.info(`Resolved ${versionFile} as ${version}`); return version; } - core.warning("Neither 'python-version' nor 'python-version-file' inputs were supplied."); + logWarning("Neither 'python-version' nor 'python-version-file' inputs were supplied. Attempting to find '.python-version' file."); + versionFile = '.python-version'; + if (fs_1.default.existsSync(versionFile)) { + version = fs_1.default.readFileSync(versionFile, 'utf8'); + core.info(`Resolved ${versionFile} as ${version}`); + return version; + } + logWarning(`${versionFile} doesn't exist.`); return version; } function run() { diff --git a/src/setup-python.ts b/src/setup-python.ts index db17735..2335688 100644 --- a/src/setup-python.ts +++ b/src/setup-python.ts @@ -38,24 +38,26 @@ function resolveVersionInput(): string { if (versionFile) { if (!fs.existsSync(versionFile)) { - logWarning( - `The specified python version file at: ${versionFile} doesn't exist. Attempting to find .python-version file.` + throw new Error( + `The specified python version file at: ${versionFile} doesn't exist.` ); - versionFile = '.python-version'; - if (!fs.existsSync(versionFile)) { - throw new Error(`The ${versionFile} doesn't exist.`); - } } - version = fs.readFileSync(versionFile, 'utf8'); core.info(`Resolved ${versionFile} as ${version}`); - return version; } - core.warning( - "Neither 'python-version' nor 'python-version-file' inputs were supplied." + logWarning( + "Neither 'python-version' nor 'python-version-file' inputs were supplied. Attempting to find '.python-version' file." ); + versionFile = '.python-version'; + if (fs.existsSync(versionFile)) { + version = fs.readFileSync(versionFile, 'utf8'); + core.info(`Resolved ${versionFile} as ${version}`); + return version; + } + + logWarning(`${versionFile} doesn't exist.`); return version; } From 592a7a7a45a48972db5452787b01b1f407e38948 Mon Sep 17 00:00:00 2001 From: Milos Pantic <101411245+panticmilos@users.noreply.github.com> Date: Tue, 19 Jul 2022 14:20:19 +0200 Subject: [PATCH 08/10] Add linux os release info to primary key (#467) --- __tests__/cache-restore.test.ts | 23 +++++++++++++++--- dist/setup/index.js | 36 +++++++++++++++++++++++++--- src/cache-distributions/pip-cache.ts | 15 +++++++++--- src/utils.ts | 17 +++++++++++++ 4 files changed, 82 insertions(+), 9 deletions(-) diff --git a/__tests__/cache-restore.test.ts b/__tests__/cache-restore.test.ts index 573f36a..b0d44b8 100644 --- a/__tests__/cache-restore.test.ts +++ b/__tests__/cache-restore.test.ts @@ -2,6 +2,7 @@ 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'; +import * as utils from './../src/utils'; describe('restore-cache', () => { const pipFileLockHash = @@ -28,6 +29,7 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py let saveSatetSpy: jest.SpyInstance; let getStateSpy: jest.SpyInstance; let setOutputSpy: jest.SpyInstance; + let getLinuxOSReleaseInfoSpy: jest.SpyInstance; // cache spy let restoreCacheSpy: jest.SpyInstance; @@ -74,6 +76,8 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py return primaryKey; } ); + + getLinuxOSReleaseInfoSpy = jest.spyOn(utils, 'getLinuxOSReleaseInfo'); }); describe('Validate provided package manager', () => { @@ -109,11 +113,24 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py pythonVersion, dependencyFile ); + + if (process.platform === 'linux') { + getLinuxOSReleaseInfoSpy.mockImplementation(() => + Promise.resolve('Ubuntu-20.4') + ); + } + await cacheDistributor.restoreCache(); - expect(infoSpy).toHaveBeenCalledWith( - `Cache restored from key: setup-python-${process.env['RUNNER_OS']}-python-${pythonVersion}-${packageManager}-${fileHash}` - ); + if (process.platform === 'linux' && packageManager === 'pip') { + expect(infoSpy).toHaveBeenCalledWith( + `Cache restored from key: setup-python-${process.env['RUNNER_OS']}-Ubuntu-20.4-python-${pythonVersion}-${packageManager}-${fileHash}` + ); + } else { + expect(infoSpy).toHaveBeenCalledWith( + `Cache restored from key: setup-python-${process.env['RUNNER_OS']}-python-${pythonVersion}-${packageManager}-${fileHash}` + ); + } }, 30000 ); diff --git a/dist/setup/index.js b/dist/setup/index.js index 57bd35f..dec7444 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -64430,8 +64430,17 @@ class PipCache extends cache_distributor_1.default { computeKeys() { return __awaiter(this, void 0, void 0, function* () { const hash = yield glob.hashFiles(this.cacheDependencyPath); - const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`; - const restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}`; + let primaryKey = ''; + let restoreKey = ''; + if (utils_1.IS_LINUX) { + const osRelease = yield utils_1.getLinuxOSReleaseInfo(); + primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}-${hash}`; + restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}`; + } + else { + primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`; + restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}`; + } return { primaryKey, restoreKey: [restoreKey] @@ -65357,16 +65366,26 @@ var __importStar = (this && this.__importStar) || function (mod) { __setModuleDefault(result, mod); return result; }; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.isCacheFeatureAvailable = exports.isGhes = exports.validatePythonVersionFormatForPyPy = exports.writeExactPyPyVersionFile = exports.readExactPyPyVersionFile = exports.getPyPyVersionFromPath = exports.isNightlyKeyword = exports.validateVersion = exports.createSymlinkInFolder = exports.WINDOWS_PLATFORMS = exports.WINDOWS_ARCHS = exports.IS_LINUX = exports.IS_WINDOWS = void 0; +exports.getLinuxOSReleaseInfo = exports.isCacheFeatureAvailable = exports.isGhes = exports.validatePythonVersionFormatForPyPy = exports.writeExactPyPyVersionFile = exports.readExactPyPyVersionFile = exports.getPyPyVersionFromPath = exports.isNightlyKeyword = exports.validateVersion = exports.createSymlinkInFolder = exports.WINDOWS_PLATFORMS = exports.WINDOWS_ARCHS = exports.IS_LINUX = exports.IS_WINDOWS = void 0; const cache = __importStar(__nccwpck_require__(7799)); const core = __importStar(__nccwpck_require__(2186)); const fs_1 = __importDefault(__nccwpck_require__(7147)); const path = __importStar(__nccwpck_require__(1017)); const semver = __importStar(__nccwpck_require__(1383)); +const exec = __importStar(__nccwpck_require__(1514)); exports.IS_WINDOWS = process.platform === 'win32'; exports.IS_LINUX = process.platform === 'linux'; exports.WINDOWS_ARCHS = ['x86', 'x64']; @@ -65450,6 +65469,17 @@ function isCacheFeatureAvailable() { return true; } exports.isCacheFeatureAvailable = isCacheFeatureAvailable; +function getLinuxOSReleaseInfo() { + return __awaiter(this, void 0, void 0, function* () { + const { stdout, stderr, exitCode } = yield exec.getExecOutput('lsb_release', ['-i', '-r', '-s'], { + silent: true + }); + const [osRelease, osVersion] = stdout.trim().split('\n'); + core.debug(`OS Release: ${osRelease}, Version: ${osVersion}`); + return `${osVersion}-${osRelease}`; + }); +} +exports.getLinuxOSReleaseInfo = getLinuxOSReleaseInfo; /***/ }), diff --git a/src/cache-distributions/pip-cache.ts b/src/cache-distributions/pip-cache.ts index 17055ea..460b097 100644 --- a/src/cache-distributions/pip-cache.ts +++ b/src/cache-distributions/pip-cache.ts @@ -7,7 +7,7 @@ import * as path from 'path'; import os from 'os'; import CacheDistributor from './cache-distributor'; -import {IS_WINDOWS} from '../utils'; +import {getLinuxOSReleaseInfo, IS_LINUX, IS_WINDOWS} from '../utils'; class PipCache extends CacheDistributor { constructor( @@ -57,8 +57,17 @@ class PipCache extends CacheDistributor { protected async computeKeys() { const hash = await glob.hashFiles(this.cacheDependencyPath); - const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`; - const restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}`; + let primaryKey = ''; + let restoreKey = ''; + + if (IS_LINUX) { + const osRelease = await getLinuxOSReleaseInfo(); + primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}-${hash}`; + restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}`; + } else { + primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`; + restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}`; + } return { primaryKey, diff --git a/src/utils.ts b/src/utils.ts index eb3a1ba..6eb1388 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,6 +3,7 @@ import * as core from '@actions/core'; import fs from 'fs'; import * as path from 'path'; import * as semver from 'semver'; +import * as exec from '@actions/exec'; export const IS_WINDOWS = process.platform === 'win32'; export const IS_LINUX = process.platform === 'linux'; @@ -119,3 +120,19 @@ export function isCacheFeatureAvailable(): boolean { return true; } + +export async function getLinuxOSReleaseInfo() { + const {stdout, stderr, exitCode} = await exec.getExecOutput( + 'lsb_release', + ['-i', '-r', '-s'], + { + silent: true + } + ); + + const [osRelease, osVersion] = stdout.trim().split('\n'); + + core.debug(`OS Release: ${osRelease}, Version: ${osVersion}`); + + return `${osVersion}-${osRelease}`; +} From 49a521fa062d323a7f2391290b18cf928ddfe90a Mon Sep 17 00:00:00 2001 From: Dmitry Shibanov Date: Mon, 25 Jul 2022 15:02:06 +0200 Subject: [PATCH 09/10] Fix poetry version (#445) --- .github/workflows/e2e-cache.yml | 6 ++--- __tests__/cache-restore.test.ts | 6 +++++ __tests__/data/pyproject.toml | 15 ++++++++++++ dist/setup/index.js | 31 ++++++++++++++++++------- src/cache-distributions/poetry-cache.ts | 24 ++++++++++++++++++- src/setup-python.ts | 12 +++++----- src/utils.ts | 5 ++++ 7 files changed, 80 insertions(+), 19 deletions(-) create mode 100644 __tests__/data/pyproject.toml diff --git a/.github/workflows/e2e-cache.yml b/.github/workflows/e2e-cache.yml index 0aea4b7..2e903a4 100644 --- a/.github/workflows/e2e-cache.yml +++ b/.github/workflows/e2e-cache.yml @@ -72,15 +72,15 @@ jobs: - uses: actions/checkout@v3 - name: Install poetry run: pipx install poetry + - name: Init pyproject.toml + run: mv ./__tests__/data/pyproject.toml . - name: Setup Python uses: ./ with: python-version: ${{ matrix.python-version }} cache: 'poetry' - - name: Init pyproject.toml - run: poetry init -n - name: Install dependencies - run: poetry add flake8 + run: poetry install python-pip-dependencies-caching-path: name: Test pip (Python ${{ matrix.python-version}}, ${{ matrix.os }}) diff --git a/__tests__/cache-restore.test.ts b/__tests__/cache-restore.test.ts index b0d44b8..8331b81 100644 --- a/__tests__/cache-restore.test.ts +++ b/__tests__/cache-restore.test.ts @@ -1,6 +1,7 @@ import * as core from '@actions/core'; import * as cache from '@actions/cache'; import * as exec from '@actions/exec'; +import * as io from '@actions/io'; import {getCacheDistributor} from '../src/cache-distributions/cache-factory'; import * as utils from './../src/utils'; @@ -37,6 +38,9 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py // exec spy let getExecOutputSpy: jest.SpyInstance; + // io spy + let whichSpy: jest.SpyInstance; + beforeEach(() => { process.env['RUNNER_OS'] = process.env['RUNNER_OS'] ?? 'linux'; @@ -77,6 +81,8 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py } ); + whichSpy = jest.spyOn(io, 'which'); + whichSpy.mockImplementation(() => '/path/to/python'); getLinuxOSReleaseInfoSpy = jest.spyOn(utils, 'getLinuxOSReleaseInfo'); }); diff --git a/__tests__/data/pyproject.toml b/__tests__/data/pyproject.toml new file mode 100644 index 0000000..ff4d8bd --- /dev/null +++ b/__tests__/data/pyproject.toml @@ -0,0 +1,15 @@ +[tool.poetry] +name = "testactiontasks" +version = "0.1.0" +description = "" +authors = ["Your Name "] + +[tool.poetry.dependencies] +python = "^3.8" +flake8 = "^4.0.1" + +[tool.poetry.dev-dependencies] + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" diff --git a/dist/setup/index.js b/dist/setup/index.js index dec7444..3d37346 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -64573,9 +64573,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) { }; Object.defineProperty(exports, "__esModule", ({ value: true })); const glob = __importStar(__nccwpck_require__(8090)); +const io = __importStar(__nccwpck_require__(7436)); const path = __importStar(__nccwpck_require__(1017)); const exec = __importStar(__nccwpck_require__(1514)); +const core = __importStar(__nccwpck_require__(2186)); const cache_distributor_1 = __importDefault(__nccwpck_require__(8953)); +const utils_1 = __nccwpck_require__(1314); class PoetryCache extends cache_distributor_1.default { constructor(pythonVersion, patterns = '**/poetry.lock') { super('poetry', patterns); @@ -64591,6 +64594,17 @@ class PoetryCache extends cache_distributor_1.default { if (poetryConfig['virtualenvs.in-project'] === true) { paths.push(path.join(process.cwd(), '.venv')); } + const pythonLocation = yield io.which('python'); + if (pythonLocation) { + core.debug(`pythonLocation is ${pythonLocation}`); + const { exitCode, stderr } = yield exec.getExecOutput(`poetry env use ${pythonLocation}`, undefined, { ignoreReturnCode: true }); + if (exitCode) { + utils_1.logWarning(stderr); + } + } + else { + utils_1.logWarning('python binaries were not found in PATH'); + } return paths; }); } @@ -65241,7 +65255,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.logWarning = void 0; const core = __importStar(__nccwpck_require__(2186)); const finder = __importStar(__nccwpck_require__(9996)); const finderPyPy = __importStar(__nccwpck_require__(4003)); @@ -65277,14 +65290,14 @@ function resolveVersionInput() { core.info(`Resolved ${versionFile} as ${version}`); return version; } - logWarning("Neither 'python-version' nor 'python-version-file' inputs were supplied. Attempting to find '.python-version' file."); + utils_1.logWarning("Neither 'python-version' nor 'python-version-file' inputs were supplied. Attempting to find '.python-version' file."); versionFile = '.python-version'; if (fs_1.default.existsSync(versionFile)) { version = fs_1.default.readFileSync(versionFile, 'utf8'); core.info(`Resolved ${versionFile} as ${version}`); return version; } - logWarning(`${versionFile} doesn't exist.`); + utils_1.logWarning(`${versionFile} doesn't exist.`); return version; } function run() { @@ -65332,11 +65345,6 @@ function run() { } }); } -function logWarning(message) { - const warningPrefix = '[warning]'; - core.info(`${warningPrefix}${message}`); -} -exports.logWarning = logWarning; run(); @@ -65379,7 +65387,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.getLinuxOSReleaseInfo = exports.isCacheFeatureAvailable = exports.isGhes = exports.validatePythonVersionFormatForPyPy = exports.writeExactPyPyVersionFile = exports.readExactPyPyVersionFile = exports.getPyPyVersionFromPath = exports.isNightlyKeyword = exports.validateVersion = exports.createSymlinkInFolder = exports.WINDOWS_PLATFORMS = exports.WINDOWS_ARCHS = exports.IS_LINUX = exports.IS_WINDOWS = void 0; +exports.logWarning = exports.getLinuxOSReleaseInfo = exports.isCacheFeatureAvailable = exports.isGhes = exports.validatePythonVersionFormatForPyPy = exports.writeExactPyPyVersionFile = exports.readExactPyPyVersionFile = exports.getPyPyVersionFromPath = exports.isNightlyKeyword = exports.validateVersion = exports.createSymlinkInFolder = exports.WINDOWS_PLATFORMS = exports.WINDOWS_ARCHS = exports.IS_LINUX = exports.IS_WINDOWS = void 0; const cache = __importStar(__nccwpck_require__(7799)); const core = __importStar(__nccwpck_require__(2186)); const fs_1 = __importDefault(__nccwpck_require__(7147)); @@ -65480,6 +65488,11 @@ function getLinuxOSReleaseInfo() { }); } exports.getLinuxOSReleaseInfo = getLinuxOSReleaseInfo; +function logWarning(message) { + const warningPrefix = '[warning]'; + core.info(`${warningPrefix}${message}`); +} +exports.logWarning = logWarning; /***/ }), diff --git a/src/cache-distributions/poetry-cache.ts b/src/cache-distributions/poetry-cache.ts index 5e22b5d..60ecea2 100644 --- a/src/cache-distributions/poetry-cache.ts +++ b/src/cache-distributions/poetry-cache.ts @@ -1,9 +1,11 @@ import * as glob from '@actions/glob'; -import * as os from 'os'; +import * as io from '@actions/io'; import * as path from 'path'; import * as exec from '@actions/exec'; +import * as core from '@actions/core'; import CacheDistributor from './cache-distributor'; +import {logWarning} from '../utils'; class PoetryCache extends CacheDistributor { constructor( @@ -28,6 +30,26 @@ class PoetryCache extends CacheDistributor { paths.push(path.join(process.cwd(), '.venv')); } + const pythonLocation = await io.which('python'); + + if (pythonLocation) { + core.debug(`pythonLocation is ${pythonLocation}`); + const { + exitCode, + stderr + } = await exec.getExecOutput( + `poetry env use ${pythonLocation}`, + undefined, + {ignoreReturnCode: true} + ); + + if (exitCode) { + logWarning(stderr); + } + } else { + logWarning('python binaries were not found in PATH'); + } + return paths; } diff --git a/src/setup-python.ts b/src/setup-python.ts index 2335688..1ebcbac 100644 --- a/src/setup-python.ts +++ b/src/setup-python.ts @@ -5,7 +5,12 @@ import * as path from 'path'; import * as os from 'os'; import fs from 'fs'; import {getCacheDistributor} from './cache-distributions/cache-factory'; -import {isCacheFeatureAvailable, IS_LINUX, IS_WINDOWS} from './utils'; +import { + isCacheFeatureAvailable, + logWarning, + IS_LINUX, + IS_WINDOWS +} from './utils'; function isPyPyVersion(versionSpec: string) { return versionSpec.startsWith('pypy'); @@ -115,9 +120,4 @@ async function run() { } } -export function logWarning(message: string): void { - const warningPrefix = '[warning]'; - core.info(`${warningPrefix}${message}`); -} - run(); diff --git a/src/utils.ts b/src/utils.ts index 6eb1388..7d4fa02 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -136,3 +136,8 @@ export async function getLinuxOSReleaseInfo() { return `${osVersion}-${osRelease}`; } + +export function logWarning(message: string): void { + const warningPrefix = '[warning]'; + core.info(`${warningPrefix}${message}`); +} From 2f06e9da25e11351919e97ebc9d3cd20dc7208ab Mon Sep 17 00:00:00 2001 From: Dmitry Shibanov Date: Mon, 25 Jul 2022 16:54:04 +0200 Subject: [PATCH 10/10] Add check-latest functionality (#406) --- .github/workflows/test-pypy.yml | 33 ++++++ .github/workflows/test-python.yml | 24 +++++ .licenses/npm/@actions/http-client.dep.yml | Bin 1387 -> 0 bytes README.md | 18 ++++ __tests__/find-pypy.test.ts | 117 +++++++++++++++++++-- __tests__/finder.test.ts | 87 +++++++++++++-- __tests__/install-pypy.test.ts | 34 +++++- action.yml | 3 + dist/setup/index.js | 66 +++++++++--- src/find-pypy.ts | 34 +++++- src/find-python.ts | 29 ++++- src/install-pypy.ts | 8 +- src/install-python.ts | 29 +++-- src/setup-python.ts | 8 +- 14 files changed, 440 insertions(+), 50 deletions(-) delete mode 100644 .licenses/npm/@actions/http-client.dep.yml diff --git a/.github/workflows/test-pypy.yml b/.github/workflows/test-pypy.yml index 4800662..de9ba6b 100644 --- a/.github/workflows/test-pypy.yml +++ b/.github/workflows/test-pypy.yml @@ -91,3 +91,36 @@ jobs: - name: Run simple code run: ${{ steps.setup-python.outputs.python-path }} -c 'import math; print(math.factorial(5))' + + check-latest: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + steps: + - uses: actions/checkout@v3 + - name: Setup PyPy and check latest + uses: ./ + with: + python-version: 'pypy-3.7-v7.3.x' + check-latest: true + - name: PyPy and Python version + run: python --version + + - name: Run simple code + run: python -c 'import math; print(math.factorial(5))' + + - name: Assert PyPy is running + run: | + import platform + assert platform.python_implementation().lower() == "pypy" + shell: python + + - name: Assert expected binaries (or symlinks) are present + run: | + EXECUTABLE="pypy-3.7-v7.3.x" + EXECUTABLE=${EXECUTABLE/-/} # remove the first '-' in "pypy-X.Y" -> "pypyX.Y" to match executable name + EXECUTABLE=${EXECUTABLE%%-*} # remove any -* suffixe + ${EXECUTABLE} --version + shell: bash \ No newline at end of file diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index 17709ec..921449f 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -172,3 +172,27 @@ jobs: - name: Run simple code run: ${{ steps.setup-python.outputs.python-path }} -c 'import math; print(math.factorial(5))' + + check-latest: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + python-version: ["3.8", "3.9", "3.10"] + steps: + - uses: actions/checkout@v3 + - name: Setup Python and check latest + uses: ./ + with: + python-version: ${{ matrix.python-version }} + check-latest: true + - name: Validate version + run: | + $pythonVersion = (python --version) + if ("$pythonVersion" -NotMatch "${{ matrix.python }}"){ + Write-Host "The current version is $pythonVersion; expected version is ${{ matrix.python }}" + exit 1 + } + $pythonVersion + shell: pwsh \ No newline at end of file diff --git a/.licenses/npm/@actions/http-client.dep.yml b/.licenses/npm/@actions/http-client.dep.yml deleted file mode 100644 index 43316cbc6aca3d15b7f778da1837f5f3894d7832..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1387 zcmZuxOK;ma5WeeI4AM(e$aZt}X&IT0h{}{f(ai>n9`v9s29juqRI<9A5eS+wZM z=5XeF4eiBZ5e;Rp)^PV%Svze8yK0>~FX~RK!QDk~YP48g!^86H^5G$JmoqjG=RUGi z-5!6z3obb5U!0buLfX>e1|awK{3nPSvtD{pt@ypGH+Sy^1vk0#Bdyy0&|4A zEyt6J!5Jur3!E`a959tD^`M6nlu)B2-|1R}vF7OB%TZx-69kvmnp&5L35}_z9ye0@ z9l=p|%ED*YD!41IQ+M})s8MAHdO$=4w<~zZ@Xh4BEjQ}0uQ)L1q3))}Yq_;_8l#_C zJj68*bTCtuKaU&P?cOg$uC?Lq-&h`h)NP zRmR@8&kA!w4cEA=ue{@X`qdG}b)3clX)zq^Xbi?TGa>V`6c~imH{4@sRQ1&dvFC~bXH+{XbZHXQ#g(>^+kRRjrrJF5uIaSu-W6-#Z zvl#>D?{=?GOQsZxd@G-cpuh^)i~Kv=&<%Vc1)jge@XTbIA0%LhAY8scz6HWx;2Yzc z80g=7L5l)%0c^L=7{wanNp{#U{sfOW$8#BWoiTjI2rP5(q*pjbadrzkDv}h@$s@~{ ze2HPpB=@*m1SYU2LbBwL5dr&y*yjaB2^)mwjBf=p(H-S-iOhkD=Rm(>0g9AlS)d~a zbRmMyB;UUX_LNFU^K3)0_(<`0O&&8k)1kQ}BWxGLhV00bS12Gy(IT|X3Iow|N<*1( z)B_}uEa%=%l5;8W7^7PuKgOR~L1Q3-6+WD;$dSTFixWOS?&mn2QGD!PdEoij5Vra7 e4j90PrVS+-;^De@xV$wlqxr?)zt%rLqW=K%u(U4# diff --git a/README.md b/README.md index 6e178e2..cb17004 100644 --- a/README.md +++ b/README.md @@ -259,6 +259,24 @@ pypy3.7-nightly or pypy-3.7-nightly # Python 3.7 and nightly PyPy Note: `pypy2` and `pypy3` have been removed in v3. Use the format above instead. +# Check latest version + +The `check-latest` flag defaults to `false`. Use the default or set `check-latest` to `false` if you prefer stability and if you want to ensure a specific `Python/PyPy` version is always used. + +If `check-latest` is set to `true`, the action first checks if the cached version is the latest one. If the locally cached version is not the most up-to-date, a `Python/PyPy` version will then be downloaded. Set `check-latest` to `true` if you want the most up-to-date `Python/PyPy` version to always be used. + +> Setting `check-latest` to `true` has performance implications as downloading `Python/PyPy` versions is slower than using cached versions. + +```yaml +steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v3 + with: + python-version: '3.7' + check-latest: true + - run: python my_script.py +``` + # Caching packages dependencies The action has built-in functionality for caching and restoring dependencies. It uses [actions/cache](https://github.com/actions/toolkit/tree/main/packages/cache) under the hood for caching dependencies but requires less configuration settings. Supported package managers are `pip`, `pipenv` and `poetry`. The `cache` input is optional, and caching is turned off by default. diff --git a/__tests__/find-pypy.test.ts b/__tests__/find-pypy.test.ts index 3da9a22..660f23d 100644 --- a/__tests__/find-pypy.test.ts +++ b/__tests__/find-pypy.test.ts @@ -14,7 +14,6 @@ import * as finder from '../src/find-pypy'; import { IPyPyManifestRelease, IS_WINDOWS, - validateVersion, getPyPyVersionFromPath } from '../src/utils'; @@ -82,6 +81,12 @@ describe('findPyPyToolCache', () => { const pypyPath = path.join('PyPy', actualPythonVersion, architecture); let tcFind: jest.SpyInstance; let spyReadExactPyPyVersion: jest.SpyInstance; + let infoSpy: jest.SpyInstance; + let warningSpy: jest.SpyInstance; + let debugSpy: jest.SpyInstance; + let addPathSpy: jest.SpyInstance; + let exportVariableSpy: jest.SpyInstance; + let setOutputSpy: jest.SpyInstance; beforeEach(() => { tcFind = jest.spyOn(tc, 'find'); @@ -94,6 +99,24 @@ describe('findPyPyToolCache', () => { spyReadExactPyPyVersion = jest.spyOn(utils, 'readExactPyPyVersionFile'); spyReadExactPyPyVersion.mockImplementation(() => actualPyPyVersion); + + infoSpy = jest.spyOn(core, 'info'); + infoSpy.mockImplementation(() => null); + + warningSpy = jest.spyOn(core, 'warning'); + warningSpy.mockImplementation(() => null); + + debugSpy = jest.spyOn(core, 'debug'); + debugSpy.mockImplementation(() => null); + + addPathSpy = jest.spyOn(core, 'addPath'); + addPathSpy.mockImplementation(() => null); + + exportVariableSpy = jest.spyOn(core, 'exportVariable'); + exportVariableSpy.mockImplementation(() => null); + + setOutputSpy = jest.spyOn(core, 'setOutput'); + setOutputSpy.mockImplementation(() => null); }); afterEach(() => { @@ -136,6 +159,13 @@ describe('findPyPyToolCache', () => { }); describe('findPyPyVersion', () => { + let getBooleanInputSpy: jest.SpyInstance; + let warningSpy: jest.SpyInstance; + let debugSpy: jest.SpyInstance; + let infoSpy: jest.SpyInstance; + let addPathSpy: jest.SpyInstance; + let exportVariableSpy: jest.SpyInstance; + let setOutputSpy: jest.SpyInstance; let tcFind: jest.SpyInstance; let spyExtractZip: jest.SpyInstance; let spyExtractTar: jest.SpyInstance; @@ -154,6 +184,27 @@ describe('findPyPyVersion', () => { const env = process.env; beforeEach(() => { + getBooleanInputSpy = jest.spyOn(core, 'getBooleanInput'); + getBooleanInputSpy.mockImplementation(() => false); + + infoSpy = jest.spyOn(core, 'info'); + infoSpy.mockImplementation(() => {}); + + warningSpy = jest.spyOn(core, 'warning'); + warningSpy.mockImplementation(() => null); + + debugSpy = jest.spyOn(core, 'debug'); + debugSpy.mockImplementation(() => null); + + addPathSpy = jest.spyOn(core, 'addPath'); + addPathSpy.mockImplementation(() => null); + + exportVariableSpy = jest.spyOn(core, 'exportVariable'); + exportVariableSpy.mockImplementation(() => null); + + setOutputSpy = jest.spyOn(core, 'setOutput'); + setOutputSpy.mockImplementation(() => null); + jest.resetModules(); process.env = {...env}; tcFind = jest.spyOn(tc, 'find'); @@ -222,7 +273,7 @@ describe('findPyPyVersion', () => { it('found PyPy in toolcache', async () => { await expect( - finder.findPyPyVersion('pypy-3.6-v7.3.x', architecture, true) + finder.findPyPyVersion('pypy-3.6-v7.3.x', architecture, true, false) ).resolves.toEqual({ resolvedPythonVersion: '3.6.12', resolvedPyPyVersion: '7.3.3' @@ -240,13 +291,13 @@ describe('findPyPyVersion', () => { it('throw on invalid input format', async () => { await expect( - finder.findPyPyVersion('pypy3.7-v7.3.x', architecture, true) + finder.findPyPyVersion('pypy3.7-v7.3.x', architecture, true, false) ).rejects.toThrow(); }); it('throw on invalid input format pypy3.7-7.3.x', async () => { await expect( - finder.findPyPyVersion('pypy3.7-v7.3.x', architecture, true) + finder.findPyPyVersion('pypy3.7-v7.3.x', architecture, true, false) ).rejects.toThrow(); }); @@ -258,7 +309,7 @@ describe('findPyPyVersion', () => { spyChmodSync = jest.spyOn(fs, 'chmodSync'); spyChmodSync.mockImplementation(() => undefined); await expect( - finder.findPyPyVersion('pypy-3.7-v7.3.x', architecture, true) + finder.findPyPyVersion('pypy-3.7-v7.3.x', architecture, true, false) ).resolves.toEqual({ resolvedPythonVersion: '3.7.9', resolvedPyPyVersion: '7.3.3' @@ -282,7 +333,7 @@ describe('findPyPyVersion', () => { spyChmodSync = jest.spyOn(fs, 'chmodSync'); spyChmodSync.mockImplementation(() => undefined); await expect( - finder.findPyPyVersion('pypy-3.7-v7.3.x', architecture, false) + finder.findPyPyVersion('pypy-3.7-v7.3.x', architecture, false, false) ).resolves.toEqual({ resolvedPythonVersion: '3.7.9', resolvedPyPyVersion: '7.3.3' @@ -293,9 +344,61 @@ describe('findPyPyVersion', () => { it('throw if release is not found', async () => { await expect( - finder.findPyPyVersion('pypy-3.7-v7.5.x', architecture, true) + finder.findPyPyVersion('pypy-3.7-v7.5.x', architecture, true, false) ).rejects.toThrowError( `PyPy version 3.7 (v7.5.x) with arch ${architecture} not found` ); }); + + it('check-latest enabled version found and used from toolcache', async () => { + await expect( + finder.findPyPyVersion('pypy-3.6-v7.3.x', architecture, false, true) + ).resolves.toEqual({ + resolvedPythonVersion: '3.6.12', + resolvedPyPyVersion: '7.3.3' + }); + + expect(infoSpy).toHaveBeenCalledWith( + 'Resolved as PyPy 7.3.3 with Python (3.6.12)' + ); + }); + + it('check-latest enabled version found and install successfully', async () => { + spyCacheDir = jest.spyOn(tc, 'cacheDir'); + spyCacheDir.mockImplementation(() => + path.join(toolDir, 'PyPy', '3.7.7', architecture) + ); + spyChmodSync = jest.spyOn(fs, 'chmodSync'); + spyChmodSync.mockImplementation(() => undefined); + await expect( + finder.findPyPyVersion('pypy-3.7-v7.3.x', architecture, false, true) + ).resolves.toEqual({ + resolvedPythonVersion: '3.7.9', + resolvedPyPyVersion: '7.3.3' + }); + expect(infoSpy).toHaveBeenCalledWith( + 'Resolved as PyPy 7.3.3 with Python (3.7.9)' + ); + }); + + it('check-latest enabled version is not found and used from toolcache', async () => { + tcFind.mockImplementationOnce((tool: string, version: string) => { + const semverRange = new semver.Range(version); + let pypyPath = ''; + if (semver.satisfies('3.8.8', semverRange)) { + pypyPath = path.join(toolDir, 'PyPy', '3.8.8', architecture); + } + return pypyPath; + }); + await expect( + finder.findPyPyVersion('pypy-3.8-v7.3.x', architecture, false, true) + ).resolves.toEqual({ + resolvedPythonVersion: '3.8.8', + resolvedPyPyVersion: '7.3.3' + }); + + expect(infoSpy).toHaveBeenCalledWith( + 'Failed to resolve PyPy v7.3.x with Python (3.8) from manifest' + ); + }); }); diff --git a/__tests__/finder.test.ts b/__tests__/finder.test.ts index 3bd279f..d2fe775 100644 --- a/__tests__/finder.test.ts +++ b/__tests__/finder.test.ts @@ -1,6 +1,7 @@ -import io = require('@actions/io'); -import fs = require('fs'); -import path = require('path'); +import * as io from '@actions/io'; +import os from 'os'; +import fs from 'fs'; +import path from 'path'; const toolDir = path.join( __dirname, @@ -26,11 +27,14 @@ import * as installer from '../src/install-python'; const manifestData = require('./data/versions-manifest.json'); describe('Finder tests', () => { + let writeSpy: jest.SpyInstance; let spyCoreAddPath: jest.SpyInstance; let spyCoreExportVariable: jest.SpyInstance; const env = process.env; beforeEach(() => { + writeSpy = jest.spyOn(process.stdout, 'write'); + writeSpy.mockImplementation(() => {}); jest.resetModules(); process.env = {...env}; spyCoreAddPath = jest.spyOn(core, 'addPath'); @@ -45,11 +49,14 @@ describe('Finder tests', () => { }); it('Finds Python if it is installed', async () => { + const getBooleanInputSpy = jest.spyOn(core, 'getBooleanInput'); + getBooleanInputSpy.mockImplementation(input => false); + const pythonDir: string = path.join(toolDir, 'Python', '3.0.0', 'x64'); await io.mkdirP(pythonDir); fs.writeFileSync(`${pythonDir}.complete`, 'hello'); // This will throw if it doesn't find it in the cache and in the manifest (because no such version exists) - await finder.useCpythonVersion('3.x', 'x64', true); + await finder.useCpythonVersion('3.x', 'x64', true, false); expect(spyCoreAddPath).toHaveBeenCalled(); expect(spyCoreExportVariable).toHaveBeenCalledWith( 'pythonLocation', @@ -66,7 +73,7 @@ describe('Finder tests', () => { await io.mkdirP(pythonDir); fs.writeFileSync(`${pythonDir}.complete`, 'hello'); // This will throw if it doesn't find it in the cache and in the manifest (because no such version exists) - await finder.useCpythonVersion('3.x', 'x64', false); + await finder.useCpythonVersion('3.x', 'x64', false, false); expect(spyCoreAddPath).not.toHaveBeenCalled(); expect(spyCoreExportVariable).not.toHaveBeenCalled(); }); @@ -75,6 +82,9 @@ describe('Finder tests', () => { const findSpy: jest.SpyInstance = jest.spyOn(tc, 'getManifestFromRepo'); findSpy.mockImplementation(() => manifestData); + const getBooleanInputSpy = jest.spyOn(core, 'getBooleanInput'); + getBooleanInputSpy.mockImplementation(input => false); + const installSpy: jest.SpyInstance = jest.spyOn( installer, 'installCpythonFromRelease' @@ -85,7 +95,7 @@ describe('Finder tests', () => { fs.writeFileSync(`${pythonDir}.complete`, 'hello'); }); // This will throw if it doesn't find it in the cache and in the manifest (because no such version exists) - await finder.useCpythonVersion('1.2.3', 'x64', true); + await finder.useCpythonVersion('1.2.3', 'x64', true, false); expect(spyCoreAddPath).toHaveBeenCalled(); expect(spyCoreExportVariable).toHaveBeenCalledWith( 'pythonLocation', @@ -101,6 +111,9 @@ describe('Finder tests', () => { const findSpy: jest.SpyInstance = jest.spyOn(tc, 'getManifestFromRepo'); findSpy.mockImplementation(() => manifestData); + const getBooleanInputSpy = jest.spyOn(core, 'getBooleanInput'); + getBooleanInputSpy.mockImplementation(input => false); + const installSpy: jest.SpyInstance = jest.spyOn( installer, 'installCpythonFromRelease' @@ -116,7 +129,65 @@ describe('Finder tests', () => { fs.writeFileSync(`${pythonDir}.complete`, 'hello'); }); // This will throw if it doesn't find it in the manifest (because no such version exists) - await finder.useCpythonVersion('1.2.3-beta.2', 'x64', true); + await finder.useCpythonVersion('1.2.3-beta.2', 'x64', false, false); + }); + + it('Check-latest true, finds the latest version in the manifest', async () => { + const findSpy: jest.SpyInstance = jest.spyOn(tc, 'getManifestFromRepo'); + findSpy.mockImplementation(() => manifestData); + + const getBooleanInputSpy = jest.spyOn(core, 'getBooleanInput'); + getBooleanInputSpy.mockImplementation(input => true); + + const cnSpy: jest.SpyInstance = jest.spyOn(process.stdout, 'write'); + cnSpy.mockImplementation(line => { + // uncomment to debug + // process.stderr.write('write:' + line + '\n'); + }); + + const addPathSpy: jest.SpyInstance = jest.spyOn(core, 'addPath'); + addPathSpy.mockImplementation(() => null); + + const infoSpy: jest.SpyInstance = jest.spyOn(core, 'info'); + infoSpy.mockImplementation(() => {}); + + const debugSpy: jest.SpyInstance = jest.spyOn(core, 'debug'); + debugSpy.mockImplementation(() => {}); + + const pythonDir: string = path.join(toolDir, 'Python', '1.2.2', 'x64'); + const expPath: string = path.join(toolDir, 'Python', '1.2.3', 'x64'); + + const installSpy: jest.SpyInstance = jest.spyOn( + installer, + 'installCpythonFromRelease' + ); + installSpy.mockImplementation(async () => { + await io.mkdirP(expPath); + fs.writeFileSync(`${expPath}.complete`, 'hello'); + }); + + const tcFindSpy: jest.SpyInstance = jest.spyOn(tc, 'find'); + tcFindSpy + .mockImplementationOnce(() => '') + .mockImplementationOnce(() => expPath); + + await io.mkdirP(pythonDir); + await io.rmRF(path.join(toolDir, 'Python', '1.2.3')); + + fs.writeFileSync(`${pythonDir}.complete`, 'hello'); + // This will throw if it doesn't find it in the cache and in the manifest (because no such version exists) + await finder.useCpythonVersion('1.2', 'x64', true, true); + + expect(infoSpy).toHaveBeenCalledWith("Resolved as '1.2.3'"); + expect(infoSpy).toHaveBeenCalledWith( + 'Version 1.2.3 was not found in the local cache' + ); + expect(infoSpy).toBeCalledWith( + 'Version 1.2.3 is available for downloading' + ); + expect(installSpy).toHaveBeenCalled(); + expect(addPathSpy).toHaveBeenCalledWith(expPath); + await finder.useCpythonVersion('1.2.3-beta.2', 'x64', false, true); expect(spyCoreAddPath).toHaveBeenCalled(); expect(spyCoreExportVariable).toHaveBeenCalledWith( 'pythonLocation', @@ -132,7 +203,7 @@ describe('Finder tests', () => { // This will throw if it doesn't find it in the cache and in the manifest (because no such version exists) let thrown = false; try { - await finder.useCpythonVersion('3.300000', 'x64', true); + await finder.useCpythonVersion('3.300000', 'x64', true, false); } catch { thrown = true; } diff --git a/__tests__/install-pypy.test.ts b/__tests__/install-pypy.test.ts index cffc90e..ae7fb4a 100644 --- a/__tests__/install-pypy.test.ts +++ b/__tests__/install-pypy.test.ts @@ -4,6 +4,7 @@ import {HttpClient} from '@actions/http-client'; import * as ifm from '@actions/http-client/interfaces'; import * as tc from '@actions/tool-cache'; import * as exec from '@actions/exec'; +import * as core from '@actions/core'; import * as path from 'path'; import * as installer from '../src/install-pypy'; @@ -51,6 +52,22 @@ describe('findRelease', () => { download_url: `https://test.download.python.org/pypy/pypy3.6-v7.3.3-${extensionName}` }; + let getBooleanInputSpy: jest.SpyInstance; + let warningSpy: jest.SpyInstance; + let debugSpy: jest.SpyInstance; + let infoSpy: jest.SpyInstance; + + beforeEach(() => { + infoSpy = jest.spyOn(core, 'info'); + infoSpy.mockImplementation(() => {}); + + warningSpy = jest.spyOn(core, 'warning'); + warningSpy.mockImplementation(() => null); + + debugSpy = jest.spyOn(core, 'debug'); + debugSpy.mockImplementation(() => null); + }); + it("Python version is found, but PyPy version doesn't match", () => { const pythonVersion = '3.6'; const pypyVersion = '7.3.7'; @@ -133,6 +150,10 @@ describe('findRelease', () => { describe('installPyPy', () => { let tcFind: jest.SpyInstance; + let getBooleanInputSpy: jest.SpyInstance; + let warningSpy: jest.SpyInstance; + let debugSpy: jest.SpyInstance; + let infoSpy: jest.SpyInstance; let spyExtractZip: jest.SpyInstance; let spyExtractTar: jest.SpyInstance; let spyFsReadDir: jest.SpyInstance; @@ -158,6 +179,15 @@ describe('installPyPy', () => { spyExtractTar = jest.spyOn(tc, 'extractTar'); spyExtractTar.mockImplementation(() => tempDir); + infoSpy = jest.spyOn(core, 'info'); + infoSpy.mockImplementation(() => {}); + + warningSpy = jest.spyOn(core, 'warning'); + warningSpy.mockImplementation(() => null); + + debugSpy = jest.spyOn(core, 'debug'); + debugSpy.mockImplementation(() => null); + spyFsReadDir = jest.spyOn(fs, 'readdirSync'); spyFsReadDir.mockImplementation(() => ['PyPyTest']); @@ -194,7 +224,7 @@ describe('installPyPy', () => { it('throw if release is not found', async () => { await expect( - installer.installPyPy('7.3.3', '3.6.17', architecture) + installer.installPyPy('7.3.3', '3.6.17', architecture, undefined) ).rejects.toThrowError( `PyPy version 3.6.17 (7.3.3) with arch ${architecture} not found` ); @@ -214,7 +244,7 @@ describe('installPyPy', () => { spyChmodSync.mockImplementation(() => undefined); await expect( - installer.installPyPy('7.3.x', '3.6.12', architecture) + installer.installPyPy('7.3.x', '3.6.12', architecture, undefined) ).resolves.toEqual({ installDir: path.join(toolDir, 'PyPy', '3.6.12', architecture), resolvedPythonVersion: '3.6.12', diff --git a/action.yml b/action.yml index de2a4fe..f4aeb35 100644 --- a/action.yml +++ b/action.yml @@ -12,6 +12,9 @@ inputs: required: false architecture: description: 'The target architecture (x86, x64) of the Python interpreter.' + check-latest: + description: 'Set this option if you want the action to check for the latest available version that satisfies the version spec.' + default: false token: description: Used to pull python distributions from actions/python-versions. Since there's a default, this is typically not supplied by the user. default: ${{ github.token }} diff --git a/dist/setup/index.js b/dist/setup/index.js index 3d37346..f9cb76f 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -64685,19 +64685,34 @@ const utils_1 = __nccwpck_require__(1314); const semver = __importStar(__nccwpck_require__(1383)); const core = __importStar(__nccwpck_require__(2186)); const tc = __importStar(__nccwpck_require__(7784)); -function findPyPyVersion(versionSpec, architecture, updateEnvironment) { +function findPyPyVersion(versionSpec, architecture, updateEnvironment, checkLatest) { return __awaiter(this, void 0, void 0, function* () { let resolvedPyPyVersion = ''; let resolvedPythonVersion = ''; let installDir; + let releases; const pypyVersionSpec = parsePyPyVersion(versionSpec); + if (checkLatest) { + releases = yield pypyInstall.getAvailablePyPyVersions(); + if (releases && releases.length > 0) { + const releaseData = pypyInstall.findRelease(releases, pypyVersionSpec.pythonVersion, pypyVersionSpec.pypyVersion, architecture); + if (releaseData) { + core.info(`Resolved as PyPy ${releaseData.resolvedPyPyVersion} with Python (${releaseData.resolvedPythonVersion})`); + pypyVersionSpec.pythonVersion = releaseData.resolvedPythonVersion; + pypyVersionSpec.pypyVersion = releaseData.resolvedPyPyVersion; + } + else { + core.info(`Failed to resolve PyPy ${pypyVersionSpec.pypyVersion} with Python (${pypyVersionSpec.pythonVersion}) from manifest`); + } + } + } ({ installDir, resolvedPythonVersion, resolvedPyPyVersion } = findPyPyToolCache(pypyVersionSpec.pythonVersion, pypyVersionSpec.pypyVersion, architecture)); if (!installDir) { ({ installDir, resolvedPythonVersion, resolvedPyPyVersion - } = yield pypyInstall.installPyPy(pypyVersionSpec.pypyVersion, pypyVersionSpec.pythonVersion, architecture)); + } = yield pypyInstall.installPyPy(pypyVersionSpec.pypyVersion, pypyVersionSpec.pythonVersion, architecture, releases)); } const pipDir = utils_1.IS_WINDOWS ? 'Scripts' : 'bin'; const _binDir = path.join(installDir, pipDir); @@ -64847,15 +64862,28 @@ function binDir(installDir) { return path.join(installDir, 'bin'); } } -function useCpythonVersion(version, architecture, updateEnvironment) { +function useCpythonVersion(version, architecture, updateEnvironment, checkLatest) { + var _a; return __awaiter(this, void 0, void 0, function* () { + let manifest = null; const desugaredVersionSpec = desugarDevVersion(version); - const semanticVersionSpec = pythonVersionToSemantic(desugaredVersionSpec); + let semanticVersionSpec = pythonVersionToSemantic(desugaredVersionSpec); core.debug(`Semantic version spec of ${version} is ${semanticVersionSpec}`); + if (checkLatest) { + manifest = yield installer.getManifest(); + const resolvedVersion = (_a = (yield installer.findReleaseFromManifest(semanticVersionSpec, architecture, manifest))) === null || _a === void 0 ? void 0 : _a.version; + if (resolvedVersion) { + semanticVersionSpec = resolvedVersion; + core.info(`Resolved as '${semanticVersionSpec}'`); + } + else { + core.info(`Failed to resolve version ${semanticVersionSpec} from manifest`); + } + } let installDir = tc.find('Python', semanticVersionSpec, architecture); if (!installDir) { core.info(`Version ${semanticVersionSpec} was not found in the local cache`); - const foundRelease = yield installer.findReleaseFromManifest(semanticVersionSpec, architecture); + const foundRelease = yield installer.findReleaseFromManifest(semanticVersionSpec, architecture, manifest); if (foundRelease && foundRelease.files && foundRelease.files.length > 0) { core.info(`Version ${semanticVersionSpec} is available for downloading`); yield installer.installCpythonFromRelease(foundRelease); @@ -64974,7 +65002,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.findAssetForMacOrLinux = exports.findAssetForWindows = exports.isArchPresentForMacOrLinux = exports.isArchPresentForWindows = exports.pypyVersionToSemantic = exports.getPyPyBinaryPath = exports.findRelease = exports.installPyPy = void 0; +exports.findAssetForMacOrLinux = exports.findAssetForWindows = exports.isArchPresentForMacOrLinux = exports.isArchPresentForWindows = exports.pypyVersionToSemantic = exports.getPyPyBinaryPath = exports.findRelease = exports.getAvailablePyPyVersions = exports.installPyPy = void 0; const path = __importStar(__nccwpck_require__(1017)); const core = __importStar(__nccwpck_require__(2186)); const tc = __importStar(__nccwpck_require__(7784)); @@ -64983,10 +65011,10 @@ const httpm = __importStar(__nccwpck_require__(9925)); const exec = __importStar(__nccwpck_require__(1514)); const fs_1 = __importDefault(__nccwpck_require__(7147)); const utils_1 = __nccwpck_require__(1314); -function installPyPy(pypyVersion, pythonVersion, architecture) { +function installPyPy(pypyVersion, pythonVersion, architecture, releases) { return __awaiter(this, void 0, void 0, function* () { let downloadDir; - const releases = yield getAvailablePyPyVersions(); + releases = releases !== null && releases !== void 0 ? releases : (yield getAvailablePyPyVersions()); if (!releases || releases.length === 0) { throw new Error('No release was found in PyPy version.json'); } @@ -65032,6 +65060,7 @@ function getAvailablePyPyVersions() { return response.result; }); } +exports.getAvailablePyPyVersions = getAvailablePyPyVersions; function createPyPySymlink(pypyBinaryPath, pythonVersion) { return __awaiter(this, void 0, void 0, function* () { const version = semver.coerce(pythonVersion); @@ -65154,7 +65183,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.installCpythonFromRelease = exports.findReleaseFromManifest = exports.MANIFEST_URL = void 0; +exports.installCpythonFromRelease = exports.getManifest = exports.findReleaseFromManifest = exports.MANIFEST_URL = void 0; const path = __importStar(__nccwpck_require__(1017)); const core = __importStar(__nccwpck_require__(2186)); const tc = __importStar(__nccwpck_require__(7784)); @@ -65166,13 +65195,21 @@ const MANIFEST_REPO_OWNER = 'actions'; const MANIFEST_REPO_NAME = 'python-versions'; const MANIFEST_REPO_BRANCH = 'main'; exports.MANIFEST_URL = `https://raw.githubusercontent.com/${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}/${MANIFEST_REPO_BRANCH}/versions-manifest.json`; -function findReleaseFromManifest(semanticVersionSpec, architecture) { +function findReleaseFromManifest(semanticVersionSpec, architecture, manifest) { return __awaiter(this, void 0, void 0, function* () { - const manifest = yield tc.getManifestFromRepo(MANIFEST_REPO_OWNER, MANIFEST_REPO_NAME, AUTH, MANIFEST_REPO_BRANCH); - return yield tc.findFromManifest(semanticVersionSpec, false, manifest, architecture); + if (!manifest) { + manifest = yield getManifest(); + } + const foundRelease = yield tc.findFromManifest(semanticVersionSpec, false, manifest, architecture); + return foundRelease; }); } exports.findReleaseFromManifest = findReleaseFromManifest; +function getManifest() { + core.debug(`Getting manifest from ${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}@${MANIFEST_REPO_BRANCH}`); + return tc.getManifestFromRepo(MANIFEST_REPO_OWNER, MANIFEST_REPO_NAME, AUTH, MANIFEST_REPO_BRANCH); +} +exports.getManifest = getManifest; function installPython(workingDirectory) { return __awaiter(this, void 0, void 0, function* () { const options = { @@ -65315,17 +65352,18 @@ function run() { core.debug(`Python is expected to be installed into RUNNER_TOOL_CACHE=${process.env['RUNNER_TOOL_CACHE']}`); try { const version = resolveVersionInput(); + const checkLatest = core.getBooleanInput('check-latest'); if (version) { let pythonVersion; const arch = core.getInput('architecture') || os.arch(); const updateEnvironment = core.getBooleanInput('update-environment'); if (isPyPyVersion(version)) { - const installed = yield finderPyPy.findPyPyVersion(version, arch, updateEnvironment); + const installed = yield finderPyPy.findPyPyVersion(version, arch, updateEnvironment, checkLatest); pythonVersion = `${installed.resolvedPyPyVersion}-${installed.resolvedPythonVersion}`; core.info(`Successfully set up PyPy ${installed.resolvedPyPyVersion} with Python (${installed.resolvedPythonVersion})`); } else { - const installed = yield finder.useCpythonVersion(version, arch, updateEnvironment); + const installed = yield finder.useCpythonVersion(version, arch, updateEnvironment, checkLatest); pythonVersion = installed.version; core.info(`Successfully set up ${installed.impl} (${pythonVersion})`); } diff --git a/src/find-pypy.ts b/src/find-pypy.ts index 3cc3fdd..20b9821 100644 --- a/src/find-pypy.ts +++ b/src/find-pypy.ts @@ -6,7 +6,8 @@ import { validateVersion, getPyPyVersionFromPath, readExactPyPyVersionFile, - validatePythonVersionFormatForPyPy + validatePythonVersionFormatForPyPy, + IPyPyManifestRelease } from './utils'; import * as semver from 'semver'; @@ -21,14 +22,40 @@ interface IPyPyVersionSpec { export async function findPyPyVersion( versionSpec: string, architecture: string, - updateEnvironment: boolean + updateEnvironment: boolean, + checkLatest: boolean ): Promise<{resolvedPyPyVersion: string; resolvedPythonVersion: string}> { let resolvedPyPyVersion = ''; let resolvedPythonVersion = ''; let installDir: string | null; + let releases: IPyPyManifestRelease[] | undefined; const pypyVersionSpec = parsePyPyVersion(versionSpec); + if (checkLatest) { + releases = await pypyInstall.getAvailablePyPyVersions(); + if (releases && releases.length > 0) { + const releaseData = pypyInstall.findRelease( + releases, + pypyVersionSpec.pythonVersion, + pypyVersionSpec.pypyVersion, + architecture + ); + + if (releaseData) { + core.info( + `Resolved as PyPy ${releaseData.resolvedPyPyVersion} with Python (${releaseData.resolvedPythonVersion})` + ); + pypyVersionSpec.pythonVersion = releaseData.resolvedPythonVersion; + pypyVersionSpec.pypyVersion = releaseData.resolvedPyPyVersion; + } else { + core.info( + `Failed to resolve PyPy ${pypyVersionSpec.pypyVersion} with Python (${pypyVersionSpec.pythonVersion}) from manifest` + ); + } + } + } + ({installDir, resolvedPythonVersion, resolvedPyPyVersion} = findPyPyToolCache( pypyVersionSpec.pythonVersion, pypyVersionSpec.pypyVersion, @@ -43,7 +70,8 @@ export async function findPyPyVersion( } = await pypyInstall.installPyPy( pypyVersionSpec.pypyVersion, pypyVersionSpec.pythonVersion, - architecture + architecture, + releases )); } diff --git a/src/find-python.ts b/src/find-python.ts index e1add95..4e54a94 100644 --- a/src/find-python.ts +++ b/src/find-python.ts @@ -33,12 +33,34 @@ function binDir(installDir: string): string { export async function useCpythonVersion( version: string, architecture: string, - updateEnvironment: boolean + updateEnvironment: boolean, + checkLatest: boolean ): Promise { + let manifest: tc.IToolRelease[] | null = null; const desugaredVersionSpec = desugarDevVersion(version); - const semanticVersionSpec = pythonVersionToSemantic(desugaredVersionSpec); + let semanticVersionSpec = pythonVersionToSemantic(desugaredVersionSpec); core.debug(`Semantic version spec of ${version} is ${semanticVersionSpec}`); + if (checkLatest) { + manifest = await installer.getManifest(); + const resolvedVersion = ( + await installer.findReleaseFromManifest( + semanticVersionSpec, + architecture, + manifest + ) + )?.version; + + if (resolvedVersion) { + semanticVersionSpec = resolvedVersion; + core.info(`Resolved as '${semanticVersionSpec}'`); + } else { + core.info( + `Failed to resolve version ${semanticVersionSpec} from manifest` + ); + } + } + let installDir: string | null = tc.find( 'Python', semanticVersionSpec, @@ -50,7 +72,8 @@ export async function useCpythonVersion( ); const foundRelease = await installer.findReleaseFromManifest( semanticVersionSpec, - architecture + architecture, + manifest ); if (foundRelease && foundRelease.files && foundRelease.files.length > 0) { diff --git a/src/install-pypy.ts b/src/install-pypy.ts index c3718b7..4c49e11 100644 --- a/src/install-pypy.ts +++ b/src/install-pypy.ts @@ -19,11 +19,13 @@ import { export async function installPyPy( pypyVersion: string, pythonVersion: string, - architecture: string + architecture: string, + releases: IPyPyManifestRelease[] | undefined ) { let downloadDir; - const releases = await getAvailablePyPyVersions(); + releases = releases ?? (await getAvailablePyPyVersions()); + if (!releases || releases.length === 0) { throw new Error('No release was found in PyPy version.json'); } @@ -78,7 +80,7 @@ export async function installPyPy( return {installDir, resolvedPythonVersion, resolvedPyPyVersion}; } -async function getAvailablePyPyVersions() { +export async function getAvailablePyPyVersions() { const url = 'https://downloads.python.org/pypy/versions.json'; const http: httpm.HttpClient = new httpm.HttpClient('tool-cache'); diff --git a/src/install-python.ts b/src/install-python.ts index 397da0c..6e5c851 100644 --- a/src/install-python.ts +++ b/src/install-python.ts @@ -14,20 +14,33 @@ export const MANIFEST_URL = `https://raw.githubusercontent.com/${MANIFEST_REPO_O export async function findReleaseFromManifest( semanticVersionSpec: string, - architecture: string + architecture: string, + manifest: tc.IToolRelease[] | null ): Promise { - const manifest: tc.IToolRelease[] = await tc.getManifestFromRepo( - MANIFEST_REPO_OWNER, - MANIFEST_REPO_NAME, - AUTH, - MANIFEST_REPO_BRANCH - ); - return await tc.findFromManifest( + if (!manifest) { + manifest = await getManifest(); + } + + const foundRelease = await tc.findFromManifest( semanticVersionSpec, false, manifest, architecture ); + + return foundRelease; +} + +export function getManifest(): Promise { + core.debug( + `Getting manifest from ${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}@${MANIFEST_REPO_BRANCH}` + ); + return tc.getManifestFromRepo( + MANIFEST_REPO_OWNER, + MANIFEST_REPO_NAME, + AUTH, + MANIFEST_REPO_BRANCH + ); } async function installPython(workingDirectory: string) { diff --git a/src/setup-python.ts b/src/setup-python.ts index 1ebcbac..11ea405 100644 --- a/src/setup-python.ts +++ b/src/setup-python.ts @@ -80,6 +80,8 @@ async function run() { ); try { const version = resolveVersionInput(); + const checkLatest = core.getBooleanInput('check-latest'); + if (version) { let pythonVersion: string; const arch: string = core.getInput('architecture') || os.arch(); @@ -88,7 +90,8 @@ async function run() { const installed = await finderPyPy.findPyPyVersion( version, arch, - updateEnvironment + updateEnvironment, + checkLatest ); pythonVersion = `${installed.resolvedPyPyVersion}-${installed.resolvedPythonVersion}`; core.info( @@ -98,7 +101,8 @@ async function run() { const installed = await finder.useCpythonVersion( version, arch, - updateEnvironment + updateEnvironment, + checkLatest ); pythonVersion = installed.version; core.info(`Successfully set up ${installed.impl} (${pythonVersion})`);