From 0b650029e30ca99cec7a746fae8ec8621f8af2a9 Mon Sep 17 00:00:00 2001 From: Marek Kaput Date: Fri, 27 Oct 2023 15:02:34 +0200 Subject: [PATCH] Add basic tests for local registry (#798) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These tests depend on both #790 and #793, hence they come in separate PR. --- **Stack**: - #819 - #818 - #809 - #808 - #807 - #806 - #805 - #803 - #802 - #799 - #798 ⬅ ⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do not merge manually using the UI - doing so may have unexpected results.* --- scarb/tests/local_registry.rs | 119 +++++++++++++++++- utils/scarb-test-support/src/lib.rs | 1 + .../scarb-test-support/src/registry/local.rs | 38 ++++++ utils/scarb-test-support/src/registry/mod.rs | 1 + 4 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 utils/scarb-test-support/src/registry/local.rs create mode 100644 utils/scarb-test-support/src/registry/mod.rs diff --git a/scarb/tests/local_registry.rs b/scarb/tests/local_registry.rs index 78419cc3c..62688c983 100644 --- a/scarb/tests/local_registry.rs +++ b/scarb/tests/local_registry.rs @@ -6,7 +6,122 @@ use url::Url; use scarb_test_support::command::Scarb; use scarb_test_support::fsx::ChildPathEx; -use scarb_test_support::project_builder::ProjectBuilder; +use scarb_test_support::project_builder::{Dep, DepBuilder, ProjectBuilder}; +use scarb_test_support::registry::local::LocalRegistry; + +#[test] +fn usage() { + let mut registry = LocalRegistry::create(); + registry.publish(|t| { + ProjectBuilder::start() + .name("bar") + .version("1.0.0") + .lib_cairo(r#"fn f() -> felt252 { 0 }"#) + .build(t); + }); + + let t = TempDir::new().unwrap(); + ProjectBuilder::start() + .name("foo") + .version("0.1.0") + .dep("bar", Dep.version("1").registry(®istry)) + .lib_cairo(r#"fn f() -> felt252 { bar::f() }"#) + .build(&t); + + // FIXME(mkaput): Why are verbose statuses not appearing here? + Scarb::quick_snapbox() + .arg("fetch") + .current_dir(&t) + .assert() + .success() + .stdout_matches(indoc! {r#" + [..] Unpacking bar v1.0.0 ([..]) + "#}); +} + +#[test] +fn not_found() { + let mut registry = LocalRegistry::create(); + registry.publish(|t| { + // Publish a package so that the directory hierarchy is created. + // Note, however, that we declare a dependency on baZ. + ProjectBuilder::start() + .name("bar") + .version("1.0.0") + .lib_cairo(r#"fn f() -> felt252 { 0 }"#) + .build(t); + }); + + let t = TempDir::new().unwrap(); + ProjectBuilder::start() + .name("foo") + .version("0.1.0") + .dep("baz", Dep.version("1").registry(®istry)) + .build(&t); + + Scarb::quick_snapbox() + .arg("fetch") + .current_dir(&t) + .assert() + .failure() + .stdout_matches(indoc! {r#" + error: package not found in registry: baz ^1 (registry+file://[..]) + "#}); +} + +// TODO(mkaput): Test interdependencies. +// TODO(mkaput): Test path dependencies overrides. + +#[test] +fn empty_registry() { + let registry = LocalRegistry::create(); + + let t = TempDir::new().unwrap(); + ProjectBuilder::start() + .name("foo") + .version("0.1.0") + .dep("baz", Dep.version("1").registry(®istry)) + .build(&t); + + Scarb::quick_snapbox() + .arg("fetch") + .current_dir(&t) + .assert() + .failure() + .stdout_matches(indoc! {r#" + error: package not found in registry: baz ^1 (registry+file://[..]) + "#}); +} + +#[test] +fn url_pointing_to_file() { + let registry_t = TempDir::new().unwrap(); + let registry = registry_t.child("r"); + registry.write_str("").unwrap(); + let registry = Url::from_directory_path(®istry).unwrap().to_string(); + + let t = TempDir::new().unwrap(); + ProjectBuilder::start() + .name("foo") + .version("0.1.0") + .dep("baz", Dep.version("1").registry(®istry)) + .build(&t); + + Scarb::quick_snapbox() + .arg("fetch") + .current_dir(&t) + .assert() + .failure() + .stdout_matches(indoc! {r#" + error: failed to load source: registry+file://[..] + + Caused by: + local registry path is not a directory: [..] + "#}); + + // Prevent the temp directory from being deleted until this point. + drop(registry_t); +} #[test] fn publish() { @@ -149,4 +264,6 @@ fn publish_overwrites_existing() { ); } +// TODO(mkaput): Test errors properly when package is in index, but tarball is missing. // TODO(mkaput): Test publishing with target-specific dependencies. +// TODO(mkaput): Test offline mode. diff --git a/utils/scarb-test-support/src/lib.rs b/utils/scarb-test-support/src/lib.rs index 6ecbe61f8..d18498f6c 100644 --- a/utils/scarb-test-support/src/lib.rs +++ b/utils/scarb-test-support/src/lib.rs @@ -6,4 +6,5 @@ pub mod fsx; pub mod gitx; pub mod manifest_edit; pub mod project_builder; +pub mod registry; pub mod workspace_builder; diff --git a/utils/scarb-test-support/src/registry/local.rs b/utils/scarb-test-support/src/registry/local.rs new file mode 100644 index 000000000..553b7dabb --- /dev/null +++ b/utils/scarb-test-support/src/registry/local.rs @@ -0,0 +1,38 @@ +use std::fmt; + +use assert_fs::TempDir; +use url::Url; + +use crate::command::Scarb; + +pub struct LocalRegistry { + pub t: TempDir, + pub url: String, +} + +impl LocalRegistry { + pub fn create() -> Self { + let t = TempDir::new().unwrap(); + let url = Url::from_directory_path(&t).unwrap().to_string(); + Self { t, url } + } + + pub fn publish(&mut self, f: impl FnOnce(&TempDir)) -> &mut Self { + let t = TempDir::new().unwrap(); + f(&t); + Scarb::quick_snapbox() + .arg("publish") + .arg("--index") + .arg(&self.url) + .current_dir(&t) + .assert() + .success(); + self + } +} + +impl fmt::Display for LocalRegistry { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&self.url, f) + } +} diff --git a/utils/scarb-test-support/src/registry/mod.rs b/utils/scarb-test-support/src/registry/mod.rs new file mode 100644 index 000000000..27099624c --- /dev/null +++ b/utils/scarb-test-support/src/registry/mod.rs @@ -0,0 +1 @@ +pub mod local;