Skip to content

Commit

Permalink
Add basic tests for local registry
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.

commit-id:f99f264b
  • Loading branch information
mkaput committed Oct 16, 2023
1 parent 631ff83 commit 68be284
Show file tree
Hide file tree
Showing 4 changed files with 159 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,124 @@ 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("build") // TODO(#137): Change build to check for faster and lighter test.
.current_dir(&t)
.assert()
.success()
.stdout_matches(indoc! {r#"
[..] Unpacking bar v1.0.0 ([..])
[..] Compiling foo v0.1.0 ([..])
[..] Finished [..]
"#});
}

#[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://[..]/r/
Caused by:
local registry path is not a directory: [..]/r
"#});

// Prevent the temp directory from being deleted until this point.
drop(registry_t);
}

#[test]
fn publish() {
Expand Down
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;
39 changes: 39 additions & 0 deletions utils/scarb-test-support/src/registry/local.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::fmt;

use assert_fs::TempDir;
use url::Url;

use crate::command::Scarb;

pub struct LocalRegistry {
#[allow(dead_code)]
t: TempDir,
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 68be284

Please sign in to comment.