From 693766847363aa2173a60e76b159dbc6b2cd01fe Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Thu, 4 Jul 2024 10:43:48 -0400 Subject: [PATCH] Don't fail if we're building in a sandbox --- src/cli/cmd/convert.rs | 203 ++++++++++++++++++++------------------- src/cli/cmd/eject/mod.rs | 130 +++++++++++++------------ 2 files changed, 172 insertions(+), 161 deletions(-) diff --git a/src/cli/cmd/convert.rs b/src/cli/cmd/convert.rs index 3c9409d0..185a8e03 100644 --- a/src/cli/cmd/convert.rs +++ b/src/cli/cmd/convert.rs @@ -691,92 +691,95 @@ mod test { #[tokio::test] async fn nixpkgs_to_flakehub() { - let test_server = axum_test::TestServer::new(test_router().into_make_service()).unwrap(); - let server_addr = test_server.server_address(); - let server_url = server_addr.parse().unwrap(); - - let input_url = url::Url::parse("github:someorg/somerepo").unwrap(); - let tarball_url = super::convert_input_to_flakehub(&server_url, input_url) - .await - .ok() - .flatten() - .unwrap(); - assert_eq!(tarball_url.path(), "/f/someorg/somerepo/*.tar.gz"); + if let Ok(test_server) = axum_test::TestServer::new(test_router().into_make_service()) { + let server_addr = test_server.server_address(); + let server_url = server_addr.parse().unwrap(); + + let input_url = url::Url::parse("github:someorg/somerepo").unwrap(); + let tarball_url = super::convert_input_to_flakehub(&server_url, input_url) + .await + .ok() + .flatten() + .unwrap(); + assert_eq!(tarball_url.path(), "/f/someorg/somerepo/*.tar.gz"); + } } #[tokio::test] async fn nixpkgs_release_to_flakehub() { - let test_server = axum_test::TestServer::new(test_router().into_make_service()).unwrap(); - let server_addr = test_server.server_address(); - let server_url = server_addr.parse().unwrap(); - - let input_url = url::Url::parse("github:nixos/nixpkgs/nixos-23.05").unwrap(); - let tarball_url = super::convert_input_to_flakehub(&server_url, input_url) - .await - .ok() - .flatten() - .unwrap(); - assert_eq!(tarball_url.path(), "/f/nixos/nixpkgs/0.2305.0.tar.gz"); + if let Ok(test_server) = axum_test::TestServer::new(test_router().into_make_service()) { + let server_addr = test_server.server_address(); + let server_url = server_addr.parse().unwrap(); + + let input_url = url::Url::parse("github:nixos/nixpkgs/nixos-23.05").unwrap(); + let tarball_url = super::convert_input_to_flakehub(&server_url, input_url) + .await + .ok() + .flatten() + .unwrap(); + assert_eq!(tarball_url.path(), "/f/nixos/nixpkgs/0.2305.0.tar.gz"); + } } #[tokio::test] async fn test_flake1_convert() { - let test_server = axum_test::TestServer::new(test_router().into_make_service()).unwrap(); - let server_addr = test_server.server_address(); - let server_url = server_addr.parse().unwrap(); - - let convert = super::ConvertSubcommand { - flake_path: "".into(), - dry_run: true, - api_addr: server_url, - }; - let flake_contents = include_str!(concat!( - env!("CARGO_MANIFEST_DIR"), - "/samples/flake1.test.nix" - )); - let flake_contents = flake_contents.to_string(); - let parsed = nixel::parse(flake_contents.clone()); + if let Ok(test_server) = axum_test::TestServer::new(test_router().into_make_service()) { + let server_addr = test_server.server_address(); + let server_url = server_addr.parse().unwrap(); + + let convert = super::ConvertSubcommand { + flake_path: "".into(), + dry_run: true, + api_addr: server_url, + }; + let flake_contents = include_str!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/samples/flake1.test.nix" + )); + let flake_contents = flake_contents.to_string(); + let parsed = nixel::parse(flake_contents.clone()); - let (new_flake_contents, flake_compat_input_name) = convert - .convert_inputs_to_flakehub(&parsed.expression, &flake_contents) - .await - .unwrap(); - let new_flake_contents = convert - .make_implicit_nixpkgs_explicit(&parsed.expression, &new_flake_contents) - .await - .unwrap(); - let new_flake_contents = convert - .fixup_flake_compat_input(&new_flake_contents, flake_compat_input_name.unwrap()) - .await - .unwrap(); - - assert!(new_flake_contents.contains( + let (new_flake_contents, flake_compat_input_name) = convert + .convert_inputs_to_flakehub(&parsed.expression, &flake_contents) + .await + .unwrap(); + let new_flake_contents = convert + .make_implicit_nixpkgs_explicit(&parsed.expression, &new_flake_contents) + .await + .unwrap(); + let new_flake_contents = convert + .fixup_flake_compat_input(&new_flake_contents, flake_compat_input_name.unwrap()) + .await + .unwrap(); + + assert!(new_flake_contents.contains( r#"flake-compat.url = "http://flakehub-localhost/f/edolstra/flake-compat/*.tar.gz";"# )); - assert!(new_flake_contents.contains("f/nixos/nixpkgs/0.2305.0.tar.gz")); - - let nixpkgs_url_lines: Vec<_> = new_flake_contents - .lines() - .filter(|line| { - line.contains("nixpkgs.url") && line.contains("f/nixos/nixpkgs/0.2305.0.tar.gz") - }) - .collect(); - let num_nixpkgs_url_lines = nixpkgs_url_lines.len(); - assert_eq!(num_nixpkgs_url_lines, 1); + assert!(new_flake_contents.contains("f/nixos/nixpkgs/0.2305.0.tar.gz")); + + let nixpkgs_url_lines: Vec<_> = new_flake_contents + .lines() + .filter(|line| { + line.contains("nixpkgs.url") && line.contains("f/nixos/nixpkgs/0.2305.0.tar.gz") + }) + .collect(); + let num_nixpkgs_url_lines = nixpkgs_url_lines.len(); + assert_eq!(num_nixpkgs_url_lines, 1); + } } #[tokio::test] async fn test_nixpkgs_from_registry() { - let test_server = axum_test::TestServer::new(test_router().into_make_service()).unwrap(); - let server_addr = test_server.server_address(); - let server_url = server_addr.parse().unwrap(); - - let convert = super::ConvertSubcommand { - flake_path: "".into(), - dry_run: true, - api_addr: server_url, - }; - let flake_contents = r#" + if let Ok(test_server) = axum_test::TestServer::new(test_router().into_make_service()) { + let server_addr = test_server.server_address(); + let server_url = server_addr.parse().unwrap(); + + let convert = super::ConvertSubcommand { + flake_path: "".into(), + dry_run: true, + api_addr: server_url, + }; + let flake_contents = r#" { description = "cole-h's NixOS configuration"; @@ -787,38 +790,42 @@ mod test { outputs = { self, ... } @ tes: { }; } "#; - let flake_contents = flake_contents.to_string(); - let parsed = nixel::parse(flake_contents.clone()); + let flake_contents = flake_contents.to_string(); + let parsed = nixel::parse(flake_contents.clone()); - let (new_flake_contents, _) = convert - .convert_inputs_to_flakehub(&parsed.expression, &flake_contents) - .await - .unwrap(); + let (new_flake_contents, _) = convert + .convert_inputs_to_flakehub(&parsed.expression, &flake_contents) + .await + .unwrap(); - assert!(new_flake_contents - .contains(r#"nixpkgs.url = "http://flakehub-localhost/f/NixOS/nixpkgs/*.tar.gz";"#)); + assert!(new_flake_contents.contains( + r#"nixpkgs.url = "http://flakehub-localhost/f/NixOS/nixpkgs/*.tar.gz";"# + )); + } } #[tokio::test] async fn old_flakehub_to_new_flakehub() { - let test_server = axum_test::TestServer::new(test_router().into_make_service()).unwrap(); - let server_addr = test_server.server_address(); - let server_url = server_addr.parse().unwrap(); - - let input_url = - url::Url::parse("https://api.flakehub.com/f/NixOS/nixpkgs/0.1.514192.tar.gz").unwrap(); - let tarball_url = super::convert_input_to_flakehub(&server_url, input_url) - .await - .ok() - .flatten() - .unwrap(); - assert_eq!( - tarball_url.host().unwrap(), - url::Host::Domain("flakehub.com") - ); - assert_ne!( - tarball_url.host().unwrap(), - url::Host::Domain("api.flakehub.com") - ); + if let Ok(test_server) = axum_test::TestServer::new(test_router().into_make_service()) { + let server_addr = test_server.server_address(); + let server_url = server_addr.parse().unwrap(); + + let input_url = + url::Url::parse("https://api.flakehub.com/f/NixOS/nixpkgs/0.1.514192.tar.gz") + .unwrap(); + let tarball_url = super::convert_input_to_flakehub(&server_url, input_url) + .await + .ok() + .flatten() + .unwrap(); + assert_eq!( + tarball_url.host().unwrap(), + url::Host::Domain("flakehub.com") + ); + assert_ne!( + tarball_url.host().unwrap(), + url::Host::Domain("api.flakehub.com") + ); + } } } diff --git a/src/cli/cmd/eject/mod.rs b/src/cli/cmd/eject/mod.rs index 7b200a2f..a2cbc347 100644 --- a/src/cli/cmd/eject/mod.rs +++ b/src/cli/cmd/eject/mod.rs @@ -336,80 +336,84 @@ mod test { #[tokio::test] async fn flakehub_to_github() { - let test_server = axum_test::TestServer::new(test_router().into_make_service()).unwrap(); - let server_addr = test_server.server_address(); - let server_url = server_addr.parse().unwrap(); - - let input_url = - url::Url::parse("https://flakehub.com/f/someorg/somerepo/*.tar.gz").unwrap(); - let github_url = super::eject_input_to_github(&server_url, input_url) - .await - .ok() - .flatten() - .unwrap(); - assert_eq!(github_url.to_string(), "github:someorg/somerepo"); + if let Ok(test_server) = axum_test::TestServer::new(test_router().into_make_service()) { + let server_addr = test_server.server_address(); + let server_url = server_addr.parse().unwrap(); + + let input_url = + url::Url::parse("https://flakehub.com/f/someorg/somerepo/*.tar.gz").unwrap(); + let github_url = super::eject_input_to_github(&server_url, input_url) + .await + .ok() + .flatten() + .unwrap(); + assert_eq!(github_url.to_string(), "github:someorg/somerepo"); + } } #[tokio::test] async fn versioned_flakehub_to_github() { - let test_server = axum_test::TestServer::new(test_router().into_make_service()).unwrap(); - let server_addr = test_server.server_address(); - let server_url = server_addr.parse().unwrap(); - - let input_url = - url::Url::parse("https://flakehub.com/f/someorg/somerepo/1.0.0.tar.gz").unwrap(); - let github_url = super::eject_input_to_github(&server_url, input_url) - .await - .ok() - .flatten() - .unwrap(); - assert_eq!(github_url.to_string(), "github:someorg/somerepo/1.0.0"); + if let Ok(test_server) = axum_test::TestServer::new(test_router().into_make_service()) { + let server_addr = test_server.server_address(); + let server_url = server_addr.parse().unwrap(); + + let input_url = + url::Url::parse("https://flakehub.com/f/someorg/somerepo/1.0.0.tar.gz").unwrap(); + let github_url = super::eject_input_to_github(&server_url, input_url) + .await + .ok() + .flatten() + .unwrap(); + assert_eq!(github_url.to_string(), "github:someorg/somerepo/1.0.0"); + } } #[tokio::test] async fn flakehub_nixpkgs_to_github() { - let test_server = axum_test::TestServer::new(test_router().into_make_service()).unwrap(); - let server_addr = test_server.server_address(); - let server_url = server_addr.parse().unwrap(); - - let input_url = - url::Url::parse("https://flakehub.com/f/nixos/nixpkgs/0.2311.*.tar.gz").unwrap(); - let github_url = super::eject_input_to_github(&server_url, input_url) - .await - .ok() - .flatten() - .unwrap(); - assert_eq!(github_url.to_string(), "github:nixos/nixpkgs/nixos-23.11"); + if let Ok(test_server) = axum_test::TestServer::new(test_router().into_make_service()) { + let server_addr = test_server.server_address(); + let server_url = server_addr.parse().unwrap(); + + let input_url = + url::Url::parse("https://flakehub.com/f/nixos/nixpkgs/0.2311.*.tar.gz").unwrap(); + let github_url = super::eject_input_to_github(&server_url, input_url) + .await + .ok() + .flatten() + .unwrap(); + assert_eq!(github_url.to_string(), "github:nixos/nixpkgs/nixos-23.11"); + } } #[tokio::test] async fn test_flake8_eject() { - let test_server = axum_test::TestServer::new(test_router().into_make_service()).unwrap(); - let server_addr = test_server.server_address(); - let server_url = server_addr.parse().unwrap(); - - let eject = super::EjectSubcommand { - flake_path: "".into(), - dry_run: true, - api_addr: server_url, - }; - let flake_contents = include_str!(concat!( - env!("CARGO_MANIFEST_DIR"), - "/samples/flake8.test.nix" - )); - let flake_contents = flake_contents.to_string(); - let parsed = nixel::parse(flake_contents.clone()); - - let new_flake_contents = eject - .eject_inputs_to_github(&parsed.expression, &flake_contents) - .await - .unwrap(); - - assert!(new_flake_contents.contains("github:NixOS/nixpkgs/nixos-23.05")); - assert!(new_flake_contents.contains("github:DeterminateSystems/fh")); - assert!(new_flake_contents.contains("github:DeterminateSystems/fh/0.0.0")); - assert!(new_flake_contents.contains("github:edolstra/nix-warez/0.0.0?dir=blender")); - assert!(new_flake_contents.contains("github:edolstra/nix-warez?dir=blender")); - assert!(new_flake_contents.contains("github:nix-community/home-manager/release-23.05")); + if let Ok(test_server) = axum_test::TestServer::new(test_router().into_make_service()) { + let server_addr = test_server.server_address(); + let server_url = server_addr.parse().unwrap(); + + let eject = super::EjectSubcommand { + flake_path: "".into(), + dry_run: true, + api_addr: server_url, + }; + let flake_contents = include_str!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/samples/flake8.test.nix" + )); + let flake_contents = flake_contents.to_string(); + let parsed = nixel::parse(flake_contents.clone()); + + let new_flake_contents = eject + .eject_inputs_to_github(&parsed.expression, &flake_contents) + .await + .unwrap(); + + assert!(new_flake_contents.contains("github:NixOS/nixpkgs/nixos-23.05")); + assert!(new_flake_contents.contains("github:DeterminateSystems/fh")); + assert!(new_flake_contents.contains("github:DeterminateSystems/fh/0.0.0")); + assert!(new_flake_contents.contains("github:edolstra/nix-warez/0.0.0?dir=blender")); + assert!(new_flake_contents.contains("github:edolstra/nix-warez?dir=blender")); + assert!(new_flake_contents.contains("github:nix-community/home-manager/release-23.05")); + } } }