Skip to content

Commit 4780abe

Browse files
authored
Create assets.json in crate directory if needed (#2218)
Resolves #2041 by creating an empty assets.json file in the crate directory if one doesn't already exist in the crate or ancestor directories (up until the repo root).
1 parent c8bbcb5 commit 4780abe

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

sdk/core/azure_core_test/examples/test_proxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1919
.from_env_lossy();
2020
tracing_subscriber::fmt().with_env_filter(filter).init();
2121

22-
let mut proxy = proxy::start(env!("CARGO_MANIFEST_DIR"), Some(args.into())).await?;
22+
let mut proxy = proxy::start(None, env!("CARGO_MANIFEST_DIR"), Some(args.into())).await?;
2323

2424
let code = tokio::select! {
2525
_ = tokio::signal::ctrl_c() => {

sdk/core/azure_core_test/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ fn find_ancestor_file(dir: impl AsRef<Path>, name: &str) -> azure_core::Result<P
195195
let path = dir.join(".git");
196196
if path.exists() {
197197
return Err(azure_core::Error::message(
198-
ErrorKind::Other,
198+
ErrorKind::Io,
199199
format!("{name} not found under repo {}", dir.display()),
200200
));
201201
}

sdk/core/azure_core_test/src/proxy/mod.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ pub use bootstrap::start;
3636
#[cfg(not(target_arch = "wasm32"))]
3737
mod bootstrap {
3838
use super::*;
39-
use azure_core::Result;
39+
use azure_core::{test::TestMode, Result};
40+
use serde_json::json;
4041
use std::{env, io, path::Path, process::Stdio, time::Duration};
4142
use tokio::{
43+
fs,
4244
io::{AsyncBufReadExt, BufReader},
4345
process::{ChildStdout, Command},
4446
};
@@ -59,6 +61,7 @@ mod bootstrap {
5961
///
6062
/// This is intended for internal use only and should not be called directly in tests.
6163
pub async fn start(
64+
test_mode: Option<TestMode>,
6265
crate_dir: impl AsRef<Path>,
6366
options: Option<ProxyOptions>,
6467
) -> Result<Proxy> {
@@ -68,11 +71,41 @@ mod bootstrap {
6871
}
6972

7073
// Find root of git repo or work tree: a ".git" directory or file will exist either way.
71-
let git_dir = crate::find_ancestor_file(crate_dir, ".git")?;
74+
let git_dir = crate::find_ancestor_file(crate_dir.as_ref(), ".git")?;
7275
let git_dir = git_dir.parent().ok_or_else(|| {
7376
io::Error::new(io::ErrorKind::NotFound, "parent git repository not found")
7477
})?;
7578

79+
// Create an assets.json file in the crate_dir if a parent doesn't already exist.
80+
#[cfg(not(target_arch = "wasm32"))]
81+
if test_mode == Some(TestMode::Record)
82+
&& matches!(crate::find_ancestor_file(crate_dir.as_ref(), "assets.json"), Err(err) if err.kind() == &ErrorKind::Io)
83+
{
84+
let assets_file = crate_dir.as_ref().join("assets.json");
85+
tracing::trace!("creating {path}", path = assets_file.display());
86+
87+
let assets_dir = assets_file
88+
.parent()
89+
.and_then(Path::file_name)
90+
.map(|dir| dir.to_ascii_lowercase())
91+
.ok_or_else(|| {
92+
azure_core::Error::message(
93+
ErrorKind::Io,
94+
"failed to get assets.json parent directory name",
95+
)
96+
})?;
97+
let assets_dir = assets_dir.to_string_lossy();
98+
let assets_content = json!({
99+
"AssetsRepo": "Azure/azure-sdk-assets",
100+
"AssetsRepoPrefixPath": "rust",
101+
"TagPrefix": format!("rust/{assets_dir}"),
102+
"Tag": "",
103+
});
104+
let file = fs::File::create_new(assets_file).await?;
105+
serde_json::to_writer_pretty(file.into_std().await, &assets_content)?;
106+
}
107+
108+
// Construct the command line arguments and start the test-proxy service.
76109
let mut args: Vec<String> = Vec::new();
77110
args.extend_from_slice(&[
78111
"start".into(),

sdk/core/azure_core_test/src/recorded.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ pub async fn start(
4949
.init();
5050
}
5151

52-
crate::proxy::start(crate_dir, options).await.map(Arc::new)
52+
crate::proxy::start(Some(test_mode), crate_dir, options)
53+
.await
54+
.map(Arc::new)
5355
})
5456
.await
5557
.as_ref()

0 commit comments

Comments
 (0)