From 86322ff86aabab35512cbbecaddf047eb3279c68 Mon Sep 17 00:00:00 2001 From: Jorge Rodriguez Date: Thu, 13 Mar 2025 19:35:00 +0100 Subject: [PATCH] feat: support reading mise.toml --- .github/workflows/test-python.yml | 35 +++++++++++++++++++++++++++++++ __tests__/utils.test.ts | 12 +++++++++++ dist/setup/index.js | 6 +++++- docs/advanced-usage.md | 11 +++++++++- src/utils.ts | 10 +++++++-- 5 files changed, 70 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index 8d68df25..b7f444c7 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -278,6 +278,41 @@ jobs: with: python-version-file: .tool-versions +setup-versions-from-mise-toml-file: + name: Setup ${{ matrix.python }} ${{ matrix.os }} mise.toml file + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: + [ + macos-latest, + windows-latest, + ubuntu-20.04, + ubuntu-22.04, + macos-13, + ubuntu-latest + ] + python: [3.13.0, 3.14.0, 3.15.0] + exclude: + - os: windows-latest + python: graalpy-24.1.2 + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: build-mise-toml-file ${{ matrix.python }} + run: | + echo '[tools] + python = "${{ matrix.python }}" + ' > mise.toml + + - name: setup-python using mise.toml ${{ matrix.python }} + id: setup-python-mise-toml + uses: ./ + with: + python-version-file: mise.toml + setup-pre-release-version-from-manifest: name: Setup 3.14.0-alpha.1 ${{ matrix.os }} runs-on: ${{ matrix.os }} diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 6c0f0e13..fddd5964 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -216,6 +216,18 @@ describe('Version from file test', () => { expect(_fn(toolVersionFilePath)).toEqual(['3.14t-dev']); } ); + + it.each([getVersionInputFromTomlFile, getVersionInputFromFile])( + 'Version from mise.toml', + async _fn => { + await io.mkdirP(tempDir); + const filePath = path.join(tempDir, 'mise.toml'); + const pythonVersion = '3.8.0'; + const fileContent = `[tools]\npython = "${pythonVersion}"`; + fs.writeFileSync(filePath, fileContent); + expect(_fn(filePath)).toEqual([pythonVersion]); + } + ); }); describe('getNextPageUrl', () => { diff --git a/dist/setup/index.js b/dist/setup/index.js index 78254955..bcaded1b 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -100726,10 +100726,14 @@ function getVersionInputFromTomlFile(versionFile) { // standard project metadata (PEP 621) keys = ['project', 'requires-python']; } - else { + else if ('tool' in pyprojectConfig) { // python poetry keys = ['tool', 'poetry', 'dependencies', 'python']; } + else if ('tools' in pyprojectConfig) { + // mise + keys = ['tools', 'python']; + } const versions = []; const version = extractValue(pyprojectConfig, keys); if (version !== undefined) { diff --git a/docs/advanced-usage.md b/docs/advanced-usage.md index 72b35016..799f0254 100644 --- a/docs/advanced-usage.md +++ b/docs/advanced-usage.md @@ -280,7 +280,7 @@ jobs: `setup-python` action can read Python or PyPy version from a version file. `python-version-file` input is used for specifying the path to the version file. If the file that was supplied to `python-version-file` input doesn't exist, the action will fail with error. ->In case both `python-version` and `python-version-file` inputs are supplied, the `python-version-file` input will be ignored due to its lower priority. The .tool-versions file supports version specifications in accordance with asdf standards, adhering to Semantic Versioning ([semver](https://semver.org)). +>In case both `python-version` and `python-version-file` inputs are supplied, the `python-version-file` input will be ignored due to its lower priority. The .tool-versions file supports version specifications in accordance with asdf standards, adhering to Semantic Versioning ([semver](https://semver.org)). The `mise.toml` file supports version specifications in accordance with [mise](https://mise.jdx.dev/configuration.html) standards. ```yaml steps: @@ -309,6 +309,15 @@ steps: - run: python my_script.py ``` +```yaml +steps: +- uses: actions/checkout@v4 +- uses: actions/setup-python@v5 + with: + python-version-file: 'mise.toml' # Read python version from a file mise.toml +- run: python my_script.py +``` + ## 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 or PyPy` version is always used. diff --git a/src/utils.ts b/src/utils.ts index 6274895e..9b571145 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -235,15 +235,21 @@ export function getVersionInputFromTomlFile(versionFile: string): string[] { pyprojectFile = pyprojectFile.replace(/\r\n/g, '\n'); const pyprojectConfig = toml.parse(pyprojectFile); - let keys = []; + let keys: string[] = []; if ('project' in pyprojectConfig) { // standard project metadata (PEP 621) keys = ['project', 'requires-python']; - } else { + } + else if ('tool' in pyprojectConfig) { // python poetry keys = ['tool', 'poetry', 'dependencies', 'python']; } + else if ('tools' in pyprojectConfig){ + // mise + keys = ['tools', 'python'] + } + const versions = []; const version = extractValue(pyprojectConfig, keys); if (version !== undefined) {