我正在编写一个从文件中读取文件内容的服务,该文件的位置是从 env 变量中读取的FILE_LOCATION
。
fn my_function(){
let path = match env::var("FILE_LOCATION") {
...
let contents = match fs::read(&path) {
}
Run Code Online (Sandbox Code Playgroud)
我有一个测试文件test_file.json
:
proj
resources
test
test_file.json
src
main.rs
tests
unit_tests.rs
Run Code Online (Sandbox Code Playgroud)
在测试中我可以设置FILE_LOCATION
:
env::set_var("FILE_LOCATION", "path/to/test/file");
Run Code Online (Sandbox Code Playgroud)
问题是 的值应该是path/to/test/file
多少?
或者换句话说:
如何在 Rust 测试中访问外部文件?
Cargo 将环境变量CARGO_MANIFEST_DIR
设置为包含Cargo.toml
文件的目录。所以你可以得到那个环境变量,并附resources/test/test_file.json
加到它。
有关Cargo 设置的环境变量,请参阅https://doc.rust-lang.org/cargo/reference/environment-variables.html。
归档时间: |
|
查看次数: |
754 次 |
最近记录: |