Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dev-dependencies #920

Merged
merged 8 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions scarb/src/core/manifest/toml_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::borrow::Cow;
use std::collections::BTreeMap;
use std::default::Default;
use std::fs;
use std::iter::{repeat, zip};

use anyhow::{anyhow, bail, ensure, Context, Result};
use cairo_lang_filesystem::db::Edition;
Expand All @@ -22,8 +23,8 @@ use crate::core::manifest::{ManifestDependency, ManifestMetadata, Summary, Targe
use crate::core::package::PackageId;
use crate::core::source::{GitReference, SourceId};
use crate::core::{
DependencyVersionReq, ManifestBuilder, ManifestCompilerConfig, PackageName, TargetKind,
TestTargetProps, TestTargetType,
DepKind, DependencyVersionReq, ManifestBuilder, ManifestCompilerConfig, PackageName,
TargetKind, TestTargetProps, TestTargetType,
};
use crate::internal::fsx;
use crate::internal::fsx::PathBufUtf8Ext;
Expand All @@ -42,6 +43,7 @@ pub struct TomlManifest {
pub package: Option<Box<TomlPackage>>,
pub workspace: Option<TomlWorkspace>,
pub dependencies: Option<BTreeMap<PackageName, MaybeTomlWorkspaceDependency>>,
pub dev_dependencies: Option<BTreeMap<PackageName, MaybeTomlWorkspaceDependency>>,
pub lib: Option<TomlTarget<TomlLibTargetParams>>,
pub cairo_plugin: Option<TomlTarget<TomlExternalTargetParams>>,
pub test: Option<Vec<TomlTarget<TomlExternalTargetParams>>>,
Expand Down Expand Up @@ -96,6 +98,7 @@ pub struct TomlWorkspace {
pub members: Option<Vec<String>>,
pub package: Option<PackageInheritableFields>,
pub dependencies: Option<BTreeMap<PackageName, TomlDependency>>,
pub dev_dependencies: Option<BTreeMap<PackageName, TomlDependency>>,
pub scripts: Option<BTreeMap<SmolStr, ScriptDefinition>>,
pub tool: Option<TomlToolsDefinition>,
}
Expand Down Expand Up @@ -410,19 +413,26 @@ impl TomlManifest {
};

let mut dependencies = Vec::new();
for (name, toml_dep) in self.dependencies.iter().flatten() {
let toml_deps = zip(self.dependencies.iter().flatten(), repeat(DepKind::Normal));
let toml_dev_deps = zip(
self.dev_dependencies.iter().flatten(),
repeat(DepKind::Target(TargetKind::TEST)),
);
let all_deps = toml_deps.chain(toml_dev_deps);

for ((name, toml_dep), kind) in all_deps {
let inherit_ws = || {
workspace
.dependencies
.as_ref()
.and_then(|deps| deps.get(name.as_str()))
.cloned()
.ok_or_else(|| anyhow!("dependency `{}` not found in workspace", name.clone()))?
.to_dependency(name.clone(), workspace_manifest_path)
.to_dependency(name.clone(), workspace_manifest_path, kind.clone())
};
let toml_dep = toml_dep
.clone()
.map(|dep| dep.to_dependency(name.clone(), manifest_path))?
.map(|dep| dep.to_dependency(name.clone(), manifest_path, kind.clone()))?
.resolve(name.as_str(), inherit_ws)?;
dependencies.push(toml_dep);
}
Expand Down Expand Up @@ -871,8 +881,9 @@ impl TomlDependency {
&self,
name: PackageName,
manifest_path: &Utf8Path,
dep_kind: DepKind,
) -> Result<ManifestDependency> {
self.resolve().to_dependency(name, manifest_path)
self.resolve().to_dependency(name, manifest_path, dep_kind)
}
}

Expand All @@ -881,6 +892,7 @@ impl DetailedTomlDependency {
&self,
name: PackageName,
manifest_path: &Utf8Path,
dep_kind: DepKind,
) -> Result<ManifestDependency> {
let version_req = self
.version
Expand Down Expand Up @@ -954,6 +966,7 @@ impl DetailedTomlDependency {
.name(name)
.source_id(source_id)
.version_req(version_req)
.kind(dep_kind)
.build())
}
}
1 change: 1 addition & 0 deletions scarb/src/core/publishing/manifest_normalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub fn prepare_manifest_for_publish(pkg: &Package) -> Result<TomlManifest> {
package,
workspace: None,
dependencies,
dev_dependencies: None,
maciektr marked this conversation as resolved.
Show resolved Hide resolved
lib: None,
cairo_plugin,
test: None,
Expand Down
71 changes: 71 additions & 0 deletions scarb/tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,3 +687,74 @@ fn edition_must_exist() {
unknown variant `2021`, expected one of `2023_01`, `2023_10`, `2023_11`
"#});
}

#[test]
fn dev_dep_used_outside_tests() {
let t = TempDir::new().unwrap();
let q = t.child("q");
ProjectBuilder::start()
.name("q")
.lib_cairo("fn dev_dep_function() -> felt252 { 42 }")
.build(&q);
ProjectBuilder::start()
.name("x")
.dev_dep("q", &q)
.lib_cairo(indoc! {r#"
use q::dev_dep_function;

fn not_working() {
dev_dep_function();
}
"#})
.build(&t);

Scarb::quick_snapbox()
.arg("build")
.current_dir(&t)
.assert()
.failure()
.stdout_matches(indoc! {r#"
[..] Compiling x v1.0.0 ([..])
error: Identifier not found.
--> [..]/src/lib.cairo[..]
use q::dev_dep_function;
^


error: could not compile `x` due to previous error
"#});
}
tomek0123456789 marked this conversation as resolved.
Show resolved Hide resolved

#[test]
fn dev_dep_inside_test() {
let t = TempDir::new().unwrap();
let q = t.child("q");
ProjectBuilder::start()
.name("q")
.lib_cairo("fn dev_dep_function() -> felt252 { 42 }")
.build(&q);
ProjectBuilder::start()
.name("x")
.dev_dep("q", &q)
.lib_cairo(indoc! {r#"
#[cfg(test)]
mod tests {
use q::dev_dep_function;

fn it_works() {
dev_dep_function();
}
}
"#})
.build(&t);

Scarb::quick_snapbox()
.arg("build")
.current_dir(&t)
.assert()
.success()
.stdout_matches(indoc! {r#"
[..] Compiling x v1.0.0 ([..])
[..] Finished release target(s) in [..]
"#});
}
37 changes: 37 additions & 0 deletions scarb/tests/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,43 @@ fn local_dependencies() {
)
}

#[test]
fn dev_dependencies() {
let t = assert_fs::TempDir::new().unwrap();
let q = t.child("q");
ProjectBuilder::start().name("q").build(&q);
ProjectBuilder::start()
.name("x")
.dev_dep("q", Dep.path("./q"))
.build(&t);
let meta = Scarb::quick_snapbox()
.arg("--json")
.arg("metadata")
.arg("--format-version")
.arg("1")
.current_dir(&t)
.stdout_json::<Metadata>();
assert_eq!(
packages_and_deps(meta),
BTreeMap::from_iter([
("core".to_string(), vec![]),
("test_plugin".to_string(), vec![]),
(
"x".to_string(),
vec![
"core".to_string(),
"q".to_string(),
"test_plugin".to_string()
]
),
(
"q".to_string(),
vec!["core".to_string(), "test_plugin".to_string()]
)
])
)
}

#[test]
fn no_dep() {
let t = assert_fs::TempDir::new().unwrap();
Expand Down
13 changes: 13 additions & 0 deletions utils/scarb-test-support/src/project_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct ProjectBuilder {
cairo_version: Option<Version>,
src: HashMap<Utf8PathBuf, String>,
deps: Vec<(String, Value)>,
dev_deps: Vec<(String, Value)>,
manifest_extra: String,
}

Expand All @@ -42,6 +43,7 @@ impl ProjectBuilder {
format!(r#"fn f{n}() -> felt252 {{ {n} }}"#),
)]),
deps: Vec::new(),
dev_deps: Vec::new(),
manifest_extra: String::new(),
}
}
Expand Down Expand Up @@ -84,6 +86,11 @@ impl ProjectBuilder {
self
}

pub fn dev_dep(mut self, name: impl ToString, dep: impl DepBuilder) -> Self {
self.dev_deps.push((name.to_string(), dep.build()));
self
}

pub fn dep_starknet(self) -> Self {
self.dep("starknet", Dep.version(CAIRO_VERSION))
}
Expand All @@ -108,6 +115,12 @@ impl ProjectBuilder {
for (name, dep) in &self.deps {
doc["dependencies"][name.clone()] = Item::Value(dep.clone());
}
if !self.dev_deps.is_empty() {
doc["dev-dependencies"] = toml_edit::table();
for (name, dep) in &self.dev_deps {
doc["dev-dependencies"][name.clone()] = Item::Value(dep.clone());
}
}
let mut manifest = doc.to_string();

if !self.manifest_extra.is_empty() {
Expand Down