Skip to content

Commit

Permalink
Add basic tests for local registry (#798)
Browse files Browse the repository at this point in the history
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.*
  • Loading branch information
mkaput authored Oct 27, 2023
1 parent 5745ace commit 0b65002
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 1 deletion.
119 changes: 118 additions & 1 deletion scarb/tests/local_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&registry))
.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(&registry))
.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(&registry))
.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(&registry).unwrap().to_string();

let t = TempDir::new().unwrap();
ProjectBuilder::start()
.name("foo")
.version("0.1.0")
.dep("baz", Dep.version("1").registry(&registry))
.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() {
Expand Down Expand Up @@ -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.
1 change: 1 addition & 0 deletions utils/scarb-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
38 changes: 38 additions & 0 deletions utils/scarb-test-support/src/registry/local.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions utils/scarb-test-support/src/registry/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod local;

0 comments on commit 0b65002

Please sign in to comment.