Skip to content

Commit

Permalink
Don't fail if we're building in a sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamc committed Jul 4, 2024
1 parent 5b7e079 commit 6937668
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 161 deletions.
203 changes: 105 additions & 98 deletions src/cli/cmd/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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")
);
}
}
}
130 changes: 67 additions & 63 deletions src/cli/cmd/eject/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
}

0 comments on commit 6937668

Please sign in to comment.