-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed use of rust-analyzer with rust_static_library and rust_shared_l…
…ibrary (#1482)
- Loading branch information
1 parent
2d7f945
commit 685dfda
Showing
8 changed files
with
177 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
load( | ||
"@rules_rust//rust:defs.bzl", | ||
"rust_shared_library", | ||
"rust_static_library", | ||
"rust_test", | ||
) | ||
|
||
rust_shared_library( | ||
name = "greeter_cdylib", | ||
srcs = [ | ||
"greeter.rs", | ||
"shared_lib.rs", | ||
], | ||
crate_root = "shared_lib.rs", | ||
edition = "2018", | ||
) | ||
|
||
rust_static_library( | ||
name = "greeter_staticlib", | ||
srcs = [ | ||
"greeter.rs", | ||
"static_lib.rs", | ||
], | ||
crate_root = "static_lib.rs", | ||
edition = "2018", | ||
) | ||
|
||
rust_test( | ||
name = "rust_project_json_test", | ||
srcs = ["rust_project_json_test.rs"], | ||
data = [":rust-project.json"], | ||
edition = "2018", | ||
env = {"RUST_PROJECT_JSON": "$(rootpath :rust-project.json)"}, | ||
# This target is tagged as manual since it's not expected to pass in | ||
# contexts outside of `//test/rust_analyzer:rust_analyzer_test`. Run | ||
# that target to execute this test. | ||
tags = ["manual"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/// Object that displays a greeting. | ||
pub struct Greeter { | ||
greeting: String, | ||
} | ||
|
||
/// Implementation of Greeter. | ||
impl Greeter { | ||
/// Constructs a new `Greeter`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use hello_lib::greeter::Greeter; | ||
/// | ||
/// let greeter = Greeter::new("Hello"); | ||
/// ``` | ||
pub fn new(greeting: &str) -> Greeter { | ||
Greeter { | ||
greeting: greeting.to_string(), | ||
} | ||
} | ||
|
||
/// Returns the greeting as a string. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use hello_lib::greeter::Greeter; | ||
/// | ||
/// let greeter = Greeter::new("Hello"); | ||
/// let greeting = greeter.greeting("World"); | ||
/// ``` | ||
pub fn greeting(&self, thing: &str) -> String { | ||
format!("{} {}", &self.greeting, thing) | ||
} | ||
|
||
/// Prints the greeting. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use hello_lib::greeter::Greeter; | ||
/// | ||
/// let greeter = Greeter::new("Hello"); | ||
/// greeter.greet("World"); | ||
/// ``` | ||
pub fn greet(&self, thing: &str) { | ||
println!("{} {}", &self.greeting, thing); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::Greeter; | ||
|
||
#[test] | ||
fn test_greeting() { | ||
let hello = Greeter::new("Hi"); | ||
assert_eq!("Hi Rust", hello.greeting("Rust")); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
test/rust_analyzer/static_and_shared_lib_test/rust_project_json_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use std::env; | ||
use std::path::PathBuf; | ||
|
||
#[test] | ||
fn test_deps_of_crate_and_its_test_are_merged() { | ||
let rust_project_path = PathBuf::from(env::var("RUST_PROJECT_JSON").unwrap()); | ||
|
||
let content = std::fs::read_to_string(&rust_project_path) | ||
.unwrap_or_else(|_| panic!("couldn't open {:?}", &rust_project_path)); | ||
|
||
assert!( | ||
content.contains(r#"{"display_name":"greeter_cdylib","root_module":"shared_lib.rs"#), | ||
"expected rust-project.json to contain a rust_shared_library target." | ||
); | ||
assert!( | ||
content.contains(r#"{"display_name":"greeter_staticlib","root_module":"static_lib.rs"#), | ||
"expected rust-project.json to contain a rust_static_library target." | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod greeter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod greeter; |