-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix 'data' field not being made available to rust_test rule. (#59)
* Fix 'data' field not being made available to rust_test rule. * Make data available to rule. * Add example for using data in runfiles.
- Loading branch information
1 parent
3ad4922
commit 2a3f859
Showing
5 changed files
with
88 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
load( | ||
"@io_bazel_rules_rust//rust:rust.bzl", | ||
"rust_library", | ||
"rust_binary", | ||
"rust_test", | ||
) | ||
|
||
rust_library( | ||
name = "runfiles", | ||
srcs = ["src/lib.rs"], | ||
) | ||
|
||
rust_binary( | ||
name = "hello_runfiles", | ||
srcs = ["src/main.rs"], | ||
data = ["data/sample.txt"], | ||
deps = [":runfiles"], | ||
) | ||
|
||
rust_test( | ||
name = "hello_runfiles_test", | ||
data = ["data/sample.txt"], | ||
deps = [":runfiles"], | ||
) |
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 @@ | ||
Example Text! |
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,37 @@ | ||
use std::io; | ||
use std::path::PathBuf; | ||
|
||
/// Returns the .runfiles directory for the currently executing binary. | ||
pub fn get_runfiles_dir() -> io::Result<PathBuf> { | ||
let mut path = std::env::current_exe()?; | ||
|
||
let mut name = path.file_name().unwrap().to_owned(); | ||
name.push(".runfiles"); | ||
|
||
path.pop(); | ||
path.push(name); | ||
|
||
Ok(path) | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
use std::io; | ||
use std::io::prelude::*; | ||
use std::fs::File; | ||
|
||
#[test] | ||
fn test_can_read_data_from_runfiles() { | ||
let runfiles = get_runfiles_dir().unwrap(); | ||
|
||
let mut f = File::open(runfiles.join("examples/hello_runfiles/data/sample.txt")).unwrap(); | ||
let mut buffer = String::new(); | ||
|
||
f.read_to_string(&mut buffer).unwrap(); | ||
|
||
assert_eq!("Example Text!", buffer); | ||
} | ||
} |
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,16 @@ | ||
extern crate runfiles; | ||
|
||
use std::io::prelude::*; | ||
use std::fs::File; | ||
|
||
|
||
fn main() { | ||
let runfiles = runfiles::get_runfiles_dir().unwrap(); | ||
|
||
let mut f = File::open(runfiles.join("examples/hello_runfiles/data/sample.txt")).unwrap(); | ||
let mut buffer = String::new(); | ||
|
||
f.read_to_string(&mut buffer).unwrap(); | ||
|
||
println!("{}", buffer); | ||
} |
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