From 70f6f2a532013d250c9c0c7cb594dc60b060774c Mon Sep 17 00:00:00 2001 From: Dmitry Shibanov Date: Wed, 4 May 2022 12:33:57 +0200 Subject: [PATCH] fix poetry python version --- __tests__/cache-restore.test.ts | 7 +++++++ dist/setup/index.js | 13 +++++++++++++ src/cache-distributions/poetry-cache.ts | 18 +++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/__tests__/cache-restore.test.ts b/__tests__/cache-restore.test.ts index 99b674ef..c8769e44 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'; describe('restore-cache', () => { @@ -35,6 +36,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'; @@ -74,6 +78,9 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py return primaryKey; } ); + + whichSpy = jest.spyOn(io, 'which'); + whichSpy.mockImplementation(() => '/path/to/python'); }); describe('Validate provided package manager', () => { diff --git a/dist/setup/index.js b/dist/setup/index.js index 4afa92b0..fcea6fa1 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -38355,8 +38355,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) { }; Object.defineProperty(exports, "__esModule", { value: true }); const glob = __importStar(__webpack_require__(281)); +const io = __importStar(__webpack_require__(1)); const path = __importStar(__webpack_require__(622)); const exec = __importStar(__webpack_require__(986)); +const core = __importStar(__webpack_require__(470)); const cache_distributor_1 = __importDefault(__webpack_require__(596)); class PoetryCache extends cache_distributor_1.default { constructor(pythonVersion, patterns = '**/poetry.lock') { @@ -38373,6 +38375,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}`); + if (exitCode) { + throw new Error(stderr); + } + } + else { + core.warning('python binaries were not found in PATH.'); + } return paths; }); } diff --git a/src/cache-distributions/poetry-cache.ts b/src/cache-distributions/poetry-cache.ts index 58aa5fd9..6263c494 100644 --- a/src/cache-distributions/poetry-cache.ts +++ b/src/cache-distributions/poetry-cache.ts @@ -1,7 +1,8 @@ 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'; @@ -28,6 +29,21 @@ 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}` + ); + + if (exitCode) { + throw new Error(stderr); + } + } else { + core.warning('python binaries were not found in PATH.'); + } + return paths; }