Skip to content

Commit 65d7f2d

Browse files
Add range validation for toml files (#726)
1 parent f97b831 commit 65d7f2d

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

.github/workflows/test-python.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ jobs:
157157
fail-fast: false
158158
matrix:
159159
os: [macos-latest, windows-latest, ubuntu-20.04, ubuntu-22.04]
160-
python: [3.5.4, 3.6.7, 3.7.5, 3.8.15, 3.9.13]
160+
python: [3.5.4, 3.6.7, 3.7.5, 3.8.15, 3.9.13, '==3.10.10']
161161
exclude:
162162
- os: ubuntu-22.04
163163
python: 3.5.4
@@ -190,7 +190,7 @@ jobs:
190190
- name: Validate version
191191
run: |
192192
$pythonVersion = (python --version)
193-
if ("Python ${{ matrix.python }}" -ne "$pythonVersion"){
193+
if ("Python ${{ matrix.python }}".replace("==", "") -ne "$pythonVersion"){
194194
Write-Host "The current version is $pythonVersion; expected version is ${{ matrix.python }}"
195195
exit 1
196196
}

__tests__/utils.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe('Version from file test', () => {
107107
await io.mkdirP(tempDir);
108108
const pythonVersionFileName = 'pyproject.toml';
109109
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
110-
const pythonVersion = '>=3.7';
110+
const pythonVersion = '>=3.7.0';
111111
const pythonVersionFileContent = `[project]\nrequires-python = "${pythonVersion}"`;
112112
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
113113
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
@@ -119,7 +119,7 @@ describe('Version from file test', () => {
119119
await io.mkdirP(tempDir);
120120
const pythonVersionFileName = 'pyproject.toml';
121121
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
122-
const pythonVersion = '>=3.7';
122+
const pythonVersion = '>=3.7.0';
123123
const pythonVersionFileContent = `[tool.poetry.dependencies]\npython = "${pythonVersion}"`;
124124
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
125125
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);

dist/setup/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -70143,7 +70143,16 @@ function getVersionInputFromTomlFile(versionFile) {
7014370143
versions.push(version);
7014470144
}
7014570145
core.info(`Extracted ${versions} from ${versionFile}`);
70146-
return Array.from(versions, version => version.split(',').join(' '));
70146+
const rawVersions = Array.from(versions, version => version.split(',').join(' '));
70147+
const validatedVersions = rawVersions
70148+
.map(item => semver.validRange(item, true))
70149+
.filter((versionRange, index) => {
70150+
if (!versionRange) {
70151+
core.debug(`The version ${rawVersions[index]} is not valid SemVer range`);
70152+
}
70153+
return !!versionRange;
70154+
});
70155+
return validatedVersions;
7014770156
}
7014870157
exports.getVersionInputFromTomlFile = getVersionInputFromTomlFile;
7014970158
/**

src/utils.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,21 @@ export function getVersionInputFromTomlFile(versionFile: string): string[] {
229229
}
230230

231231
core.info(`Extracted ${versions} from ${versionFile}`);
232-
return Array.from(versions, version => version.split(',').join(' '));
232+
const rawVersions = Array.from(versions, version =>
233+
version.split(',').join(' ')
234+
);
235+
const validatedVersions = rawVersions
236+
.map(item => semver.validRange(item, true))
237+
.filter((versionRange, index) => {
238+
if (!versionRange) {
239+
core.debug(
240+
`The version ${rawVersions[index]} is not valid SemVer range`
241+
);
242+
}
243+
244+
return !!versionRange;
245+
}) as string[];
246+
return validatedVersions;
233247
}
234248

235249
/**

0 commit comments

Comments
 (0)