Skip to content

Commit

Permalink
add test case and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
BlaineHeffron committed Jul 23, 2024
1 parent 07bdff2 commit 14afce4
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions FULL_HELP_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ Initialize a Soroban project with an example contract
* `-f`, `--frontend-template <FRONTEND_TEMPLATE>` — An optional flag to pass in a url for a frontend template repository.

Default value: ``
* `-o`, `--overwrite` — Overwrite all existing files.



Expand Down
1 change: 1 addition & 0 deletions cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,4 @@ ureq = { version = "2.9.1", features = ["json"] }
assert_cmd = "2.0.4"
assert_fs = "1.0.7"
predicates = "2.1.5"
walkdir = "2.5.0"
64 changes: 63 additions & 1 deletion cmd/soroban-cli/src/commands/contract/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,13 @@ fn get_merged_file_delimiter(file_path: &Path) -> String {
#[cfg(test)]
mod tests {
use itertools::Itertools;
use std::fs::{self, read_to_string};
use std::{
collections::HashMap,
fs::{self, read_to_string},
path::PathBuf,
time::SystemTime,
};
use walkdir::WalkDir;

use super::*;

Expand Down Expand Up @@ -593,6 +599,62 @@ mod tests {
temp_dir.close().unwrap();
}

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

// First initialization
init(
project_dir.as_path(),
"https://github.com/stellar/soroban-astro-template",
&with_examples,
false,
)
.unwrap();

// Get initial modification times
let initial_mod_times = get_mod_times(&project_dir);

// Second initialization with overwrite
init(
project_dir.as_path(),
"https://github.com/stellar/soroban-astro-template",
&with_examples,
true, // overwrite = true
)
.unwrap();

// Get new modification times
let new_mod_times = get_mod_times(&project_dir);

// Compare modification times
for (path, initial_time) in initial_mod_times {
let new_time = new_mod_times.get(&path).expect("File should still exist");
assert!(
new_time > &initial_time,
"File {} should have a later modification time",
path.display()
);
}

temp_dir.close().unwrap();
}

fn get_mod_times(dir: &Path) -> HashMap<PathBuf, SystemTime> {
let mut mod_times = HashMap::new();
for entry in WalkDir::new(dir) {
let entry = entry.unwrap();
if entry.file_type().is_file() {
let path = entry.path().to_owned();
let metadata = fs::metadata(&path).unwrap();
mod_times.insert(path, metadata.modified().unwrap());
}
}
mod_times
}

#[test]
fn test_init_from_within_an_existing_project() {
let temp_dir = tempfile::tempdir().unwrap();
Expand Down

0 comments on commit 14afce4

Please sign in to comment.