Separate can_initialize and is_initialized in Package

This commit is contained in:
raychu86 2020-08-29 18:04:51 -07:00
parent 10a03aefa0
commit 848660cc01
2 changed files with 65 additions and 31 deletions

View File

@ -26,42 +26,42 @@ impl Package {
}
}
/// Returns `true` if a package is initialized at the given path
pub fn is_initialized(package_name: &str, is_lib: bool, path: &PathBuf) -> bool {
let mut result = false;
/// Returns `true` if a package is can be initialized at a given path.
pub fn can_initialize(package_name: &str, is_lib: bool, path: &PathBuf) -> bool {
let mut result = true;
let mut existing_files = vec![];
// Check if the manifest file already exists.
if Manifest::exists_at(&path) {
existing_files.push(Manifest::filename());
result = true;
result = false;
}
if is_lib {
// Check if the library file already exists.
if LibraryFile::exists_at(&path) {
existing_files.push(LibraryFile::filename());
result = true;
result = false;
}
} else {
// Check if the input file already exists.
let input_file = InputFile::new(&package_name);
if input_file.exists_at(&path) {
existing_files.push(input_file.filename());
result = true;
result = false;
}
// Check if the state file already exists.
let state_file = StateFile::new(&package_name);
if state_file.exists_at(&path) {
existing_files.push(state_file.filename());
result = true;
result = false;
}
// Check if the main file already exists.
if MainFile::exists_at(&path) {
existing_files.push(MainFile::filename());
result = true;
result = false;
}
}
@ -72,11 +72,45 @@ impl Package {
return result;
}
/// Returns `true` if a package is initialized at the given path
pub fn is_initialized(package_name: &str, is_lib: bool, path: &PathBuf) -> bool {
// Check if the manifest file exists.
if !Manifest::exists_at(&path) {
return false;
}
if is_lib {
// Check if the library file exists.
if !LibraryFile::exists_at(&path) {
return false;
}
} else {
// Check if the input file exists.
let input_file = InputFile::new(&package_name);
if !input_file.exists_at(&path) {
return false;
}
// Check if the state file exists.
let state_file = StateFile::new(&package_name);
if !state_file.exists_at(&path) {
return false;
}
// Check if the main file exists.
if !MainFile::exists_at(&path) {
return false;
}
}
return true;
}
/// Creates a package at the given path
pub fn initialize(package_name: &str, is_lib: bool, path: &PathBuf) -> Result<(), PackageError> {
// First, verify that this directory is not already initialized as a Leo package.
{
if Self::is_initialized(package_name, is_lib, path) {
if !Self::can_initialize(package_name, is_lib, path) {
return Err(
PackageError::FailedToInitialize(package_name.to_owned(), path.as_os_str().to_owned()).into(),
);

View File

@ -85,8 +85,8 @@ mod initialize_package {
fn initialize_valid_package() {
let test_directory = test_dir();
// Ensure a package is not initialized at the `test_directory`
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
// Ensure a package can be initialized at the `test_directory`
assert!(Package::can_initialize(TEST_PACKAGE_NAME, false, &test_directory));
// Initialize a package at the `test_directory`
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_ok());
@ -105,8 +105,8 @@ mod initialize_package {
fn initialize_fails_with_existing_manifest() {
let test_directory = test_dir();
// Ensure a package is not initialized at the `test_directory`
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
// Ensure a package can be initialized at the `test_directory`
assert!(Package::can_initialize(TEST_PACKAGE_NAME, false, &test_directory));
// Manually add a manifest file to the `test_directory`
Manifest::new(TEST_PACKAGE_NAME).write_to(&test_directory).unwrap();
@ -114,16 +114,16 @@ mod initialize_package {
// Attempt to initialize a package at the `test_directory`
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_err());
// Package is considered initialized at the `test_directory`
assert!(Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
// Ensure package is not initialized at the `test_directory`
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
}
#[test]
fn initialize_fails_with_existing_library_file() {
let test_directory = test_dir();
// Ensure a package is not initialized at the `test_directory`
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, true, &test_directory));
// Ensure a package can be initialized at the `test_directory`
assert!(Package::can_initialize(TEST_PACKAGE_NAME, true, &test_directory));
// Manually add a source directory and a library file to the `test_directory`
SourceDirectory::create(&test_directory).unwrap();
@ -132,16 +132,16 @@ mod initialize_package {
// Attempt to initialize a package at the `test_directory`
assert!(Package::initialize(TEST_PACKAGE_NAME, true, &test_directory).is_err());
// Package is considered initialized at the `test_directory`
assert!(Package::is_initialized(TEST_PACKAGE_NAME, true, &test_directory));
// Ensure package is not initialized at the `test_directory`
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, true, &test_directory));
}
#[test]
fn initialize_fails_with_existing_input_file() {
let test_directory = test_dir();
// Ensure a package is not initialized at the `test_directory`
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
// Ensure a package can be initialized at the `test_directory`
assert!(Package::can_initialize(TEST_PACKAGE_NAME, false, &test_directory));
// Manually add an inputs directory and an input file to the `test_directory`
InputsDirectory::create(&test_directory).unwrap();
@ -150,16 +150,16 @@ mod initialize_package {
// Attempt to initialize a package at the `test_directory`
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_err());
// Package is considered initialized at the `test_directory`
assert!(Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
// Ensure package is not initialized at the `test_directory`
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
}
#[test]
fn initialize_fails_with_existing_state_file() {
let test_directory = test_dir();
// Ensure a package is not initialized at the `test_directory`
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
// Ensure a package can be initialized at the `test_directory`
assert!(Package::can_initialize(TEST_PACKAGE_NAME, false, &test_directory));
// Manually add an inputs directory and a state file to the `test_directory`
InputsDirectory::create(&test_directory).unwrap();
@ -168,16 +168,16 @@ mod initialize_package {
// Attempt to initialize a package at the `test_directory`
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_err());
// Package is considered initialized at the `test_directory`
assert!(Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
// Ensure package is not initialized at the `test_directory`
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
}
#[test]
fn initialize_fails_with_existing_main_file() {
let test_directory = test_dir();
// Ensure a package is not initialized at the `test_directory`
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
// Ensure a package can be initialized at the `test_directory`
assert!(Package::can_initialize(TEST_PACKAGE_NAME, false, &test_directory));
// Manually add a source directory and a main file to the `test_directory`
SourceDirectory::create(&test_directory).unwrap();
@ -186,7 +186,7 @@ mod initialize_package {
// Attempt to initialize a package at the `test_directory`
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_err());
// Package is considered initialized at the `test_directory`
assert!(Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
// Ensure package is not initialized at the `test_directory`
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
}
}