From ea6f51124108361d2e9946ba1122ba7384173722 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Tue, 25 Jun 2024 22:25:02 +0300 Subject: refactor(tvix/glue): return a parsed Url in NixFetchArgs The only two consumers (fetchurl, fetchtarball) of these do try to parse it as URL, so do it in the helper. Update url_basename to take a &url::URL, not a &str. Also update the test to use rstest for the fixtures to reduce some boilerplate there. Change-Id: I1f85fe2803060dc4423e673cb7b9f9bf799d09b9 Reviewed-on: https://cl.tvl.fyi/c/depot/+/11875 Reviewed-by: Ilan Joselevich Tested-by: BuildkiteCI Reviewed-by: Connor Brewster --- tvix/glue/src/fetchers/mod.rs | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) (limited to 'tvix/glue/src/fetchers/mod.rs') diff --git a/tvix/glue/src/fetchers/mod.rs b/tvix/glue/src/fetchers/mod.rs index 6cce569e9799..eb035a5a905c 100644 --- a/tvix/glue/src/fetchers/mod.rs +++ b/tvix/glue/src/fetchers/mod.rs @@ -603,7 +603,8 @@ where } /// Attempts to mimic `nix::libutil::baseNameOf` -pub(crate) fn url_basename(s: &str) -> &str { +pub(crate) fn url_basename(url: &Url) -> &str { + let s = url.path(); if s.is_empty() { return ""; } @@ -720,30 +721,18 @@ mod tests { mod url_basename { use super::super::*; + use rstest::rstest; - #[test] - fn empty_path() { - assert_eq!(url_basename(""), ""); - } - - #[test] - fn path_on_root() { - assert_eq!(url_basename("/dir"), "dir"); - } - - #[test] - fn relative_path() { - assert_eq!(url_basename("dir/foo"), "foo"); - } - - #[test] - fn root_with_trailing_slash() { - assert_eq!(url_basename("/"), ""); - } - - #[test] - fn trailing_slash() { - assert_eq!(url_basename("/dir/"), "dir"); + #[rstest] + #[case::empty_path("", "")] + #[case::path_on_root("/dir", "dir")] + #[case::relative_path("dir/foo", "foo")] + #[case::root_with_trailing_slash("/", "")] + #[case::trailing_slash("/dir/", "dir")] + fn test_url_basename(#[case] url_path: &str, #[case] exp_basename: &str) { + let mut url = Url::parse("http://localhost").expect("invalid url"); + url.set_path(url_path); + assert_eq!(url_basename(&url), exp_basename); } } } -- cgit 1.4.1