Skip to content

Commit 2c89c9e

Browse files
authored
Improvements to Python version unit tests (#546)
Improves unit test coverage of this file, and refactors to reduce repetition and boilerplate. GUS-W-21931549.
1 parent 0b5d74d commit 2c89c9e

5 files changed

Lines changed: 68 additions & 103 deletions

File tree

src/python_version.rs

Lines changed: 68 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ pub(crate) enum ResolvePythonVersionError {
191191
#[cfg(test)]
192192
mod tests {
193193
use super::*;
194+
use crate::package_manager::SUPPORTED_PACKAGE_MANAGERS;
194195

195196
#[test]
196197
fn requested_python_version_display() {
@@ -216,9 +217,22 @@ mod tests {
216217
);
217218
}
218219

220+
#[test]
221+
fn python_version_origin_display() {
222+
assert_eq!(
223+
PythonVersionOrigin::BuildpackDefault.to_string(),
224+
"buildpack default"
225+
);
226+
assert_eq!(
227+
PythonVersionOrigin::PythonVersionFile.to_string(),
228+
".python-version"
229+
);
230+
}
231+
219232
#[test]
220233
fn python_version_display() {
221-
assert_eq!(PythonVersion::new(3, 12, 0).to_string(), "3.12.0");
234+
assert_eq!(PythonVersion::new(2, 7, 18).to_string(), "2.7.18");
235+
assert_eq!(PythonVersion::new(3, 14, 0).to_string(), "3.14.0");
222236
}
223237

224238
#[test]
@@ -247,30 +261,16 @@ mod tests {
247261

248262
#[test]
249263
fn read_requested_python_version_runtime_txt() {
250-
assert!(matches!(
251-
read_requested_python_version(
252-
Path::new("tests/fixtures/runtime_txt_and_python_version_file"),
253-
PackageManager::Pip
254-
)
255-
.unwrap_err(),
256-
RequestedPythonVersionError::RuntimeTxtNotSupported(PackageManager::Pip)
257-
));
258-
assert!(matches!(
259-
read_requested_python_version(
260-
Path::new("tests/fixtures/runtime_txt_invalid_unicode"),
261-
PackageManager::Poetry
262-
)
263-
.unwrap_err(),
264-
RequestedPythonVersionError::RuntimeTxtNotSupported(PackageManager::Poetry)
265-
));
266-
assert!(matches!(
267-
read_requested_python_version(
268-
Path::new("tests/fixtures/runtime_txt_invalid_version"),
269-
PackageManager::Uv
270-
)
271-
.unwrap_err(),
272-
RequestedPythonVersionError::RuntimeTxtNotSupported(PackageManager::Uv)
273-
));
264+
for &package_manager in SUPPORTED_PACKAGE_MANAGERS {
265+
assert!(matches!(
266+
read_requested_python_version(
267+
Path::new("tests/fixtures/runtime_txt_and_python_version_file"),
268+
package_manager
269+
)
270+
.unwrap_err(),
271+
RequestedPythonVersionError::RuntimeTxtNotSupported(pm) if pm == package_manager
272+
));
273+
}
274274
// We pass a path containing a NUL byte as an easy way to trigger an I/O error.
275275
assert!(matches!(
276276
read_requested_python_version(Path::new("\0/invalid"), PackageManager::Pip).unwrap_err(),
@@ -385,85 +385,52 @@ mod tests {
385385

386386
#[test]
387387
fn resolve_python_version_eol() {
388-
let requested_python_version = RequestedPythonVersion {
389-
major: 3,
390-
minor: OLDEST_SUPPORTED_PYTHON_3_MINOR_VERSION - 1,
391-
patch: None,
392-
origin: PythonVersionOrigin::PythonVersionFile,
393-
};
394-
assert_eq!(
395-
resolve_python_version(&requested_python_version),
396-
Err(ResolvePythonVersionError::EolVersion(
397-
requested_python_version
398-
))
399-
);
400-
401-
let requested_python_version = RequestedPythonVersion {
402-
major: 3,
403-
minor: OLDEST_SUPPORTED_PYTHON_3_MINOR_VERSION - 1,
404-
patch: Some(0),
405-
origin: PythonVersionOrigin::PythonVersionFile,
406-
};
407-
assert_eq!(
408-
resolve_python_version(&requested_python_version),
409-
Err(ResolvePythonVersionError::EolVersion(
410-
requested_python_version
411-
))
412-
);
413-
414-
let requested_python_version = RequestedPythonVersion {
415-
major: 2,
416-
minor: 7,
417-
patch: Some(18),
418-
origin: PythonVersionOrigin::PythonVersionFile,
419-
};
420-
assert_eq!(
421-
resolve_python_version(&requested_python_version),
422-
Err(ResolvePythonVersionError::EolVersion(
423-
requested_python_version
424-
))
425-
);
388+
for (major, minor) in [
389+
(0, 1),
390+
(1, 0),
391+
(2, 7),
392+
(3, 0),
393+
(3, OLDEST_SUPPORTED_PYTHON_3_MINOR_VERSION - 1),
394+
] {
395+
for patch in [None, Some(0)] {
396+
let requested_python_version = RequestedPythonVersion {
397+
major,
398+
minor,
399+
patch,
400+
origin: PythonVersionOrigin::PythonVersionFile,
401+
};
402+
assert_eq!(
403+
resolve_python_version(&requested_python_version),
404+
Err(ResolvePythonVersionError::EolVersion(
405+
requested_python_version
406+
))
407+
);
408+
}
409+
}
426410
}
427411

428412
#[test]
429413
fn resolve_python_version_unsupported() {
430-
let requested_python_version = RequestedPythonVersion {
431-
major: 3,
432-
minor: NEXT_UNRELEASED_PYTHON_3_MINOR_VERSION,
433-
patch: None,
434-
origin: PythonVersionOrigin::PythonVersionFile,
435-
};
436-
assert_eq!(
437-
resolve_python_version(&requested_python_version),
438-
Err(ResolvePythonVersionError::UnknownVersion(
439-
requested_python_version
440-
))
441-
);
442-
443-
let requested_python_version = RequestedPythonVersion {
444-
major: 3,
445-
minor: NEXT_UNRELEASED_PYTHON_3_MINOR_VERSION,
446-
patch: Some(0),
447-
origin: PythonVersionOrigin::PythonVersionFile,
448-
};
449-
assert_eq!(
450-
resolve_python_version(&requested_python_version),
451-
Err(ResolvePythonVersionError::UnknownVersion(
452-
requested_python_version
453-
))
454-
);
455-
456-
let requested_python_version = RequestedPythonVersion {
457-
major: 4,
458-
minor: 0,
459-
patch: Some(0),
460-
origin: PythonVersionOrigin::PythonVersionFile,
461-
};
462-
assert_eq!(
463-
resolve_python_version(&requested_python_version),
464-
Err(ResolvePythonVersionError::UnknownVersion(
465-
requested_python_version
466-
))
467-
);
414+
for (major, minor) in [
415+
(3, NEXT_UNRELEASED_PYTHON_3_MINOR_VERSION),
416+
(3, 999),
417+
(4, 0),
418+
(5, 0),
419+
] {
420+
for patch in [None, Some(0)] {
421+
let requested_python_version = RequestedPythonVersion {
422+
major,
423+
minor,
424+
patch,
425+
origin: PythonVersionOrigin::PythonVersionFile,
426+
};
427+
assert_eq!(
428+
resolve_python_version(&requested_python_version),
429+
Err(ResolvePythonVersionError::UnknownVersion(
430+
requested_python_version
431+
))
432+
);
433+
}
434+
}
468435
}
469436
}

tests/fixtures/runtime_txt_invalid_unicode/requirements.txt

Whitespace-only changes.

tests/fixtures/runtime_txt_invalid_unicode/runtime.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/fixtures/runtime_txt_invalid_version/requirements.txt

Whitespace-only changes.

tests/fixtures/runtime_txt_invalid_version/runtime.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)