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

Ensure newlines after every load #730

Merged
merged 2 commits into from
May 10, 2021
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
55 changes: 51 additions & 4 deletions crate_universe/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,7 @@ impl Renderer {
crate_data.crate_context.pkg_name, crate_data.crate_context.pkg_version
));
let mut build_file = File::create(&build_file_path).with_context(|| {
format!(
"Could not create BUILD file: {}",
build_file_path.display()
)
format!("Could not create BUILD file: {}", build_file_path.display())
})?;
write!(build_file, "{}\n", &build_file_content)?;
}
Expand Down Expand Up @@ -604,6 +601,56 @@ mod tests {
assert_eq!(output, expected_repository_rule);
}

#[test]
fn render_http_and_git() {
let renderer = {
let mut renderer = mock_renderer(true);
renderer
.context
.transitive_renderable_packages
.push(RenderablePackage {
crate_context: testing::maplit_crate_context(false),
per_triple_metadata: BTreeMap::new(),
is_proc_macro: false,
});
renderer
};

let mut output = Vec::new();

renderer
.render_workspaces(&mut output)
.expect("Error rendering");

let output = String::from_utf8(output).expect("Non-UTF8 output");

let expected_repository_rule = indoc! { r#"
load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

def pinned_rust_install():
new_git_repository(
name = "rule_prefix__lazy_static__1_4_0",
strip_prefix = "",
build_file = Label("//:BUILD.lazy_static-1.4.0.bazel"),
remote = "https://github.com/rust-lang-nursery/lazy-static.rs.git",
commit = "421669662b35fcb455f2902daed2e20bbbba79b6",
)

http_archive(
name = "rule_prefix__maplit__1_0_2",
build_file = Label("//:BUILD.maplit-1.0.2.bazel"),
sha256 = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d",
strip_prefix = "maplit-1.0.2",
type = "tar.gz",
url = "https://crates.io/api/v1/crates/maplit/1.0.2/download",
)

"# };

assert_eq!(output, expected_repository_rule);
}

#[test]
fn render_no_crates() {
let renderer = Renderer::new(
Expand Down
5 changes: 2 additions & 3 deletions crate_universe/src/templates/defs.bzl.template
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
{%- for crate_data in crates -%}
{%- if crate_data.crate_context.source_details.git_data -%}
load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
{%- break %}
{% break %}
{%- endif %}
{%- endfor %}
{%- for crate_data in crates -%}
{%- if not crate_data.crate_context.source_details.git_data -%}
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
{%- break %}
{% break %}
{%- endif %}
{%- endfor %}
{%- if crates %}

{% endif -%}
def pinned_rust_install():
{%- if not crates %}
Expand Down
55 changes: 55 additions & 0 deletions crate_universe/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,58 @@ pub(crate) fn lazy_static_crate_context(git: bool) -> CrateContext {
is_proc_macro: false,
}
}

pub(crate) fn maplit_crate_context(git: bool) -> CrateContext {
let git_data = if git {
Some(GitRepo {
remote: String::from("https://github.com/bluss/maplit.git"),
commit: String::from("04936f703da907bc4ffdaced121e4cfd5ecbaec6"),
path_to_crate_root: None,
})
} else {
None
};

CrateContext {
pkg_name: String::from("maplit"),
pkg_version: Version::parse("1.0.2").unwrap(),
edition: String::from("2015"),
raze_settings: Default::default(),
canonical_additional_build_file: None,
default_deps: CrateDependencyContext {
dependencies: vec![],
proc_macro_dependencies: vec![],
data_dependencies: vec![],
build_dependencies: vec![],
build_proc_macro_dependencies: vec![],
build_data_dependencies: vec![],
dev_dependencies: vec![],
aliased_dependencies: vec![],
},
source_details: SourceDetails { git_data },
sha256: Some(String::from(
"3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d",
)),
registry_url: String::from("https://registry.url/"),
expected_build_path: String::from("UNUSED"),
lib_target_name: Some(String::from("UNUSED")),
license: LicenseData::default(),
features: vec![],
workspace_path_to_crate: String::from("UNUSED"),
workspace_member_dependents: vec![],
workspace_member_dev_dependents: vec![],
workspace_member_build_dependents: vec![],
is_workspace_member_dependency: false,
is_binary_dependency: false,
targets: vec![BuildableTarget {
kind: String::from("lib"),
name: String::from("maplit"),
path: String::from("src/lib.rs"),
edition: String::from("2015"),
}],
build_script_target: None,
targeted_deps: vec![],
links: None,
is_proc_macro: false,
}
}