Skip to content

Commit

Permalink
Change init http client from blocking to async
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Oct 8, 2024
1 parent 054cc1f commit d9f27c5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
1 change: 0 additions & 1 deletion cmd/crates/soroban-test/tests/it/integration/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ fn init() {
table["workspace"]["dependencies"]["soroban-sdk"].as_str()
== Some(&format!("{major}.0.0"))
}));
// this is true, that is does create the dir, but it also panics
}
64 changes: 32 additions & 32 deletions cmd/soroban-cli/src/commands/contract/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ pub enum Error {

impl Cmd {
#[allow(clippy::unused_self)]
pub fn run(&self, global_args: &global::Args) -> Result<(), Error> {
pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
let runner = Runner {
args: self.clone(),
print: print::Print::new(global_args.quiet),
};

runner.run()
runner.run().await
}
}

Expand All @@ -103,7 +103,7 @@ struct Runner {
}

impl Runner {
fn run(&self) -> Result<(), Error> {
async fn run(&self) -> Result<(), Error> {
let project_path = PathBuf::from(&self.args.project_path);
self.print
.infoln(format!("Initializing project at {project_path:?}"));
Expand All @@ -112,7 +112,7 @@ impl Runner {
Self::create_dir_all(&project_path)?;
self.copy_template_files()?;

if !Self::check_internet_connection() {
if !Self::check_internet_connection().await {
self.print.warnln("It doesn't look like you're connected to the internet. We're still able to initialize a new project, but additional examples and the frontend template will not be included.");
return Ok(());
}
Expand Down Expand Up @@ -260,8 +260,8 @@ impl Runner {
.unwrap_or(false)
}

fn check_internet_connection() -> bool {
if let Ok(_req) = http::blocking_client().get(GITHUB_URL).send() {
async fn check_internet_connection() -> bool {
if let Ok(_req) = http::client().get(GITHUB_URL).send().await {
return true;
}

Expand Down Expand Up @@ -454,8 +454,8 @@ mod tests {

const TEST_PROJECT_NAME: &str = "test-project";

#[test]
fn test_init() {
#[tokio::test]
async fn test_init() {
let temp_dir = tempfile::tempdir().unwrap();
let project_dir = temp_dir.path().join(TEST_PROJECT_NAME);
let runner = Runner {
Expand All @@ -467,7 +467,7 @@ mod tests {
},
print: print::Print::new(false),
};
runner.run().unwrap();
runner.run().await.unwrap();

assert_base_template_files_exist(&project_dir);
assert_default_hello_world_contract_files_exist(&project_dir);
Expand All @@ -480,8 +480,8 @@ mod tests {
temp_dir.close().unwrap();
}

#[test]
fn test_init_including_example_contract() {
#[tokio::test]
async fn test_init_including_example_contract() {
let temp_dir = tempfile::tempdir().unwrap();
let project_dir = temp_dir.path().join(TEST_PROJECT_NAME);
let runner = Runner {
Expand All @@ -493,7 +493,7 @@ mod tests {
},
print: print::Print::new(false),
};
runner.run().unwrap();
runner.run().await.unwrap();

assert_base_template_files_exist(&project_dir);
assert_default_hello_world_contract_files_exist(&project_dir);
Expand All @@ -511,8 +511,8 @@ mod tests {
temp_dir.close().unwrap();
}

#[test]
fn test_init_including_multiple_example_contracts() {
#[tokio::test]
async fn test_init_including_multiple_example_contracts() {
let temp_dir = tempfile::tempdir().unwrap();
let project_dir = temp_dir.path().join("project");
let runner = Runner {
Expand All @@ -524,7 +524,7 @@ mod tests {
},
print: print::Print::new(false),
};
runner.run().unwrap();
runner.run().await.unwrap();

assert_base_template_files_exist(&project_dir);
assert_default_hello_world_contract_files_exist(&project_dir);
Expand All @@ -543,8 +543,8 @@ mod tests {
temp_dir.close().unwrap();
}

#[test]
fn test_init_with_invalid_example_contract() {
#[tokio::test]
async fn test_init_with_invalid_example_contract() {
let temp_dir = tempfile::tempdir().unwrap();
let project_dir = temp_dir.path().join("project");
let runner = Runner {
Expand All @@ -556,13 +556,13 @@ mod tests {
},
print: print::Print::new(false),
};
assert!(runner.run().is_err());
assert!(runner.run().await.is_err());

temp_dir.close().unwrap();
}

#[test]
fn test_init_with_frontend_template() {
#[tokio::test]
async fn test_init_with_frontend_template() {
let temp_dir = tempfile::tempdir().unwrap();
let project_dir = temp_dir.path().join(TEST_PROJECT_NAME);
let runner = Runner {
Expand All @@ -574,7 +574,7 @@ mod tests {
},
print: print::Print::new(false),
};
runner.run().unwrap();
runner.run().await.unwrap();

assert_base_template_files_exist(&project_dir);
assert_default_hello_world_contract_files_exist(&project_dir);
Expand All @@ -592,8 +592,8 @@ mod tests {
temp_dir.close().unwrap();
}

#[test]
fn test_init_with_overwrite() {
#[tokio::test]
async fn test_init_with_overwrite() {
let temp_dir = tempfile::tempdir().unwrap();
let project_dir = temp_dir.path().join(TEST_PROJECT_NAME);

Expand All @@ -607,7 +607,7 @@ mod tests {
},
print: print::Print::new(false),
};
runner.run().unwrap();
runner.run().await.unwrap();

// Get initial modification times
let initial_mod_times = get_mod_times(&project_dir);
Expand All @@ -622,7 +622,7 @@ mod tests {
},
print: print::Print::new(false),
};
runner.run().unwrap();
runner.run().await.unwrap();

// Get new modification times
let new_mod_times = get_mod_times(&project_dir);
Expand Down Expand Up @@ -653,8 +653,8 @@ mod tests {
mod_times
}

#[test]
fn test_init_from_within_an_existing_project() {
#[tokio::test]
async fn test_init_from_within_an_existing_project() {
let temp_dir = tempfile::tempdir().unwrap();
let project_dir = temp_dir.path().join("./");
let runner = Runner {
Expand All @@ -666,7 +666,7 @@ mod tests {
},
print: print::Print::new(false),
};
runner.run().unwrap();
runner.run().await.unwrap();

assert_base_template_files_exist(&project_dir);
assert_default_hello_world_contract_files_exist(&project_dir);
Expand All @@ -686,8 +686,8 @@ mod tests {
temp_dir.close().unwrap();
}

#[test]
fn test_init_does_not_duplicate_frontend_readme_contents_when_run_more_than_once() {
#[tokio::test]
async fn test_init_does_not_duplicate_frontend_readme_contents_when_run_more_than_once() {
let temp_dir = tempfile::tempdir().unwrap();
let project_dir = temp_dir.path().join(TEST_PROJECT_NAME);
let runner = Runner {
Expand All @@ -699,7 +699,7 @@ mod tests {
},
print: print::Print::new(false),
};
runner.run().unwrap();
runner.run().await.unwrap();

// call init again to make sure the README.md's contents are not duplicated
let runner = Runner {
Expand All @@ -711,7 +711,7 @@ mod tests {
},
print: print::Print::new(false),
};
runner.run().unwrap();
runner.run().await.unwrap();

assert_base_template_files_exist(&project_dir);
assert_default_hello_world_contract_files_exist(&project_dir);
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/commands/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl Cmd {
Cmd::Deploy(deploy) => deploy.run(global_args).await?,
Cmd::Id(id) => id.run()?,
Cmd::Info(info) => info.run().await?,
Cmd::Init(init) => init.run(global_args)?,
Cmd::Init(init) => init.run(global_args).await?,
Cmd::Inspect(inspect) => inspect.run(global_args)?,
Cmd::Install(install) => install.run(global_args).await?,
Cmd::Invoke(invoke) => invoke.run(global_args).await?,
Expand Down

0 comments on commit d9f27c5

Please sign in to comment.