From d85fadfb3918ba9c64b028d8102949a9835c008b Mon Sep 17 00:00:00 2001 From: Sergio <77530549+sergi0g@users.noreply.github.com> Date: Thu, 21 Nov 2024 19:26:17 +0200 Subject: [PATCH] Test split --- src/structs/version.rs | 4 ++-- src/utils/reference.rs | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/structs/version.rs b/src/structs/version.rs index 6c24cde..b090cc4 100644 --- a/src/structs/version.rs +++ b/src/structs/version.rs @@ -5,7 +5,7 @@ use regex::Regex; use super::status::Status; -/// Heavily modified version of the official semver regex based on common tagging schemes for container images. Sometimes it matches more than once, but we'll try to select the best match. Yes, there _will_ be errors. +/// Heavily modified version of the official semver regex based on common tagging schemes for container images. Sometimes it matches more than once, but we'll try to select the best match. static SEMVER_REGEX: Lazy = Lazy::new(|| { Regex::new(r#"(?P0|[1-9]\d*)(?:\.(?P0|[1-9]\d*))?(?:\.(?P0|[1-9]\d*)+)?"#) .unwrap() @@ -20,7 +20,7 @@ pub struct Version { } impl Version { - /// Tries to parse the tag into semver-like parts. Should have been included in impl Image, but that would make the tests more complicated + /// Tries to parse the tag into semver-like parts. pub fn from_tag(tag: &str) -> Option { let captures = SEMVER_REGEX.captures_iter(tag); // And now... terrible best match selection for everyone! diff --git a/src/utils/reference.rs b/src/utils/reference.rs index 2bc4764..4fdd7b3 100644 --- a/src/utils/reference.rs +++ b/src/utils/reference.rs @@ -44,3 +44,25 @@ static REFERENCE_REGEX: Lazy = Lazy::new(|| { ) .unwrap() }); + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[rustfmt::skip] + fn reference() { + assert_eq!(split("alpine"), (String::from(DEFAULT_REGISTRY), String::from("library/alpine"), String::from("latest"))); + assert_eq!(split("alpine:latest"), (String::from(DEFAULT_REGISTRY), String::from("library/alpine"), String::from("latest"))); + assert_eq!(split("library/alpine"), (String::from(DEFAULT_REGISTRY), String::from("library/alpine"), String::from("latest"))); + assert_eq!(split("localhost/test"), (String::from("localhost"), String::from("test"), String::from("latest"))); + assert_eq!(split("localhost:1234/test"), (String::from("localhost:1234"), String::from("test"), String::from("latest"))); + assert_eq!(split("test:1234/idk"), (String::from("test:1234"), String::from("idk"), String::from("latest"))); + assert_eq!(split("alpine:3.7"), (String::from(DEFAULT_REGISTRY), String::from("library/alpine"), String::from("3.7"))); + assert_eq!(split("docker.example.com/examplerepo/alpine:3.7"), (String::from("docker.example.com"), String::from("examplerepo/alpine"), String::from("3.7"))); + assert_eq!(split("docker.example.com/examplerepo/alpine/test2:3.7"), (String::from("docker.example.com"), String::from("examplerepo/alpine/test2"), String::from("3.7"))); + assert_eq!(split("docker.example.com/examplerepo/alpine/test2/test3:3.7"), (String::from("docker.example.com"), String::from("examplerepo/alpine/test2/test3"), String::from("3.7"))); + assert_eq!(split("docker.example.com:5000/examplerepo/alpine:latest"), (String::from("docker.example.com:5000"), String::from("examplerepo/alpine"), String::from("latest"))); + assert_eq!(split("portainer/portainer:latest"), (String::from(DEFAULT_REGISTRY), String::from("portainer/portainer"), String::from("latest"))); + } +} \ No newline at end of file