Implement package tests

This commit is contained in:
raychu86 2020-09-13 21:53:09 -07:00
parent a036dc8ac3
commit ec9daf62e6
7 changed files with 415 additions and 203 deletions

View File

@ -144,8 +144,7 @@ impl TryFrom<&PathBuf> for Manifest {
old_remote_format = Some(remote);
// Retain the old remote format if the `manifest_refactor_remote` is not enabled
#[cfg(not(feature = "manifest_refactor_remote"))]
{
if cfg!(not(feature = "manifest_refactor_remote")) {
refactored_toml += line;
refactored_toml += "\n";
}
@ -163,13 +162,9 @@ impl TryFrom<&PathBuf> for Manifest {
final_toml += "[project]";
// Refactor the old project format if the `manifest_refactor_project` is enabled
#[cfg(feature = "manifest_refactor_project")]
{
refactored_toml += "[project]";
}
#[cfg(not(feature = "manifest_refactor_project"))]
{
refactored_toml += line;
match cfg!(feature = "manifest_refactor_project") {
true => refactored_toml += "[project]",
false => refactored_toml += line,
}
} else {
final_toml += line;
@ -204,8 +199,7 @@ author = "{author}"
final_toml += &new_remote;
// Add the new remote format if the `manifest_refactor_remote` is enabled
#[cfg(feature = "manifest_refactor_remote")]
{
if cfg!(feature = "manifest_refactor_remote") {
refactored_toml += &new_remote;
}
}

View File

@ -1,192 +0,0 @@
// Copyright (C) 2019-2020 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use lazy_static::lazy_static;
use std::{
cell::RefCell,
env,
fs,
path::PathBuf,
sync::atomic::{AtomicUsize, Ordering},
};
const PACKAGE_TEST_DIRECTORY: &str = "package-testing";
thread_local! {
/// Establish a test id for each test.
static TEST_ID: RefCell<Option<usize>> = RefCell::new(None);
}
lazy_static! {
/// Create a testing directory for packages in `target/`
static ref TEST_DIR: PathBuf = {
let mut path = env::current_exe().unwrap();
path.pop(); // Remove executable name
path.pop(); // Remove 'debug'
// Attempt to point at the `target` directory
if path.file_name().and_then(|s| s.to_str()) != Some("target") {
path.pop();
}
path.push(PACKAGE_TEST_DIRECTORY);
fs::create_dir_all(&path).unwrap();
path
};
}
/// Create a new directory for each test based on the ID of the test.
fn test_dir() -> PathBuf {
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
TEST_ID.with(|n| *n.borrow_mut() = Some(id));
let path: PathBuf = TEST_DIR.join(&format!("t{}", id)).into();
if path.exists() {
if let Err(e) = fs::remove_dir_all(&path) {
panic!("failed to remove {:?}: {:?}", &path, e)
}
}
fs::create_dir_all(&path).unwrap();
path
}
// Tests for package initialization
mod initialize_package {
use super::*;
use leo_package::{
inputs::{InputFile, InputsDirectory, StateFile},
package::Package,
root::Manifest,
source::{LibraryFile, MainFile, SourceDirectory},
};
const TEST_PACKAGE_NAME: &str = "test-package";
#[test]
fn initialize_valid_package() {
let test_directory = test_dir();
// 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());
// Ensure a package is initialized at the `test_directory`
assert!(Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
}
#[test]
#[ignore]
fn initialize_fails_with_invalid_package_names() {
unimplemented!()
}
#[test]
fn initialize_fails_with_existing_manifest() {
let test_directory = test_dir();
// 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();
// Attempt to initialize a package at the `test_directory`
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_err());
// 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 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();
LibraryFile::new(TEST_PACKAGE_NAME).write_to(&test_directory).unwrap();
// Attempt to initialize a package at the `test_directory`
assert!(Package::initialize(TEST_PACKAGE_NAME, true, &test_directory).is_err());
// 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 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();
InputFile::new(TEST_PACKAGE_NAME).write_to(&test_directory).unwrap();
// Attempt to initialize a package at the `test_directory`
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_err());
// 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 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();
StateFile::new(TEST_PACKAGE_NAME).write_to(&test_directory).unwrap();
// Attempt to initialize a package at the `test_directory`
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_err());
// 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 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();
MainFile::new(TEST_PACKAGE_NAME).write_to(&test_directory).unwrap();
// Attempt to initialize a package at the `test_directory`
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_err());
// Ensure package is not initialized at the `test_directory`
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
}
}

View File

@ -0,0 +1,134 @@
// Copyright (C) 2019-2020 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::test_dir;
use leo_package::{
inputs::{InputFile, InputsDirectory, StateFile},
package::Package,
root::Manifest,
source::{LibraryFile, MainFile, SourceDirectory},
};
const TEST_PACKAGE_NAME: &str = "test-package";
#[test]
fn initialize_valid_package() {
let test_directory = test_dir();
// 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());
// Ensure a package is initialized at the `test_directory`
assert!(Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
}
#[test]
#[ignore]
fn initialize_fails_with_invalid_package_names() {
unimplemented!()
}
#[test]
fn initialize_fails_with_existing_manifest() {
let test_directory = test_dir();
// 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();
// Attempt to initialize a package at the `test_directory`
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_err());
// 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 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();
LibraryFile::new(TEST_PACKAGE_NAME).write_to(&test_directory).unwrap();
// Attempt to initialize a package at the `test_directory`
assert!(Package::initialize(TEST_PACKAGE_NAME, true, &test_directory).is_err());
// 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 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();
InputFile::new(TEST_PACKAGE_NAME).write_to(&test_directory).unwrap();
// Attempt to initialize a package at the `test_directory`
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_err());
// 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 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();
StateFile::new(TEST_PACKAGE_NAME).write_to(&test_directory).unwrap();
// Attempt to initialize a package at the `test_directory`
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_err());
// 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 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();
MainFile::new(TEST_PACKAGE_NAME).write_to(&test_directory).unwrap();
// Attempt to initialize a package at the `test_directory`
assert!(Package::initialize(TEST_PACKAGE_NAME, false, &test_directory).is_err());
// Ensure package is not initialized at the `test_directory`
assert!(!Package::is_initialized(TEST_PACKAGE_NAME, false, &test_directory));
}

View File

@ -0,0 +1,17 @@
// Copyright (C) 2019-2020 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
pub mod initialize;

View File

@ -0,0 +1,169 @@
// Copyright (C) 2019-2020 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
// Tests for package manifest
use crate::test_dir;
use leo_package::root::{Manifest, MANIFEST_FILENAME};
use std::{
convert::TryFrom,
fs::File,
io::{Read, Write},
path::PathBuf,
};
const OLD_MANIFEST_FORMAT: &str = r#"[package]
name = "test-package"
version = "0.1.0"
description = "Testing manifest updates."
license = "MIT"
remote = "author/test-package"
"#;
const NEW_REMOTE_FORMAT: &str = r#"
[remote]
author = "author"
"#;
const OLD_PROJECT_FORMAT: &str = "[package]";
const NEW_PROJECT_FORMAT: &str = "[project]";
/// Create a manifest file with outdated formatting.
fn create_outdated_manifest_file(path: PathBuf) -> PathBuf {
let mut path = path.to_owned();
if path.is_dir() {
path.push(PathBuf::from(MANIFEST_FILENAME));
}
let mut file = File::create(&path).unwrap();
file.write_all(OLD_MANIFEST_FORMAT.as_bytes()).unwrap();
path
}
/// Read the manifest file into a string.
fn read_manifest_file(path: &PathBuf) -> String {
let mut file = File::open(path.clone()).unwrap();
let size = file.metadata().unwrap().len() as usize;
let mut buffer = String::with_capacity(size);
file.read_to_string(&mut buffer).unwrap();
buffer
}
/// Read the manifest file and check that the remote format is updated.
fn remote_is_updated(path: &PathBuf) -> bool {
let manifest_string = read_manifest_file(&path);
for line in manifest_string.lines() {
if line.starts_with("remote") {
return false;
}
}
manifest_string.contains(NEW_REMOTE_FORMAT)
}
/// Read the manifest file and check that the project format is updated.
fn project_is_updated(path: &PathBuf) -> bool {
let manifest_string = read_manifest_file(&path);
!manifest_string.contains(OLD_PROJECT_FORMAT) && manifest_string.contains(NEW_PROJECT_FORMAT)
}
#[test]
#[cfg_attr(
any(feature = "manifest_refactor_project", feature = "manifest_refactor_remote"),
ignore
)]
fn test_manifest_no_refactors() {
// Create an outdated manifest file.
let test_directory = test_dir();
let manifest_path = create_outdated_manifest_file(test_directory);
// Load the manifest file, and discard the new struct.
let _manifest = Manifest::try_from(&manifest_path).unwrap();
// Check that the manifest file project has NOT been updated.
assert!(!project_is_updated(&manifest_path));
// Check that the manifest file remote has NOT been updated.
assert!(!remote_is_updated(&manifest_path));
}
#[test]
#[cfg_attr(
any(feature = "manifest_refactor_project", not(feature = "manifest_refactor_remote")),
ignore
)]
fn test_manifest_refactor_remote() {
// Create an outdated manifest file.
let test_directory = test_dir();
let manifest_path = create_outdated_manifest_file(test_directory);
// Load the manifest file, and discard the new struct.
let _manifest = Manifest::try_from(&manifest_path).unwrap();
// Check that the manifest file project has NOT been updated.
assert!(!project_is_updated(&manifest_path));
// Check that the manifest file remote has been updated.
assert!(remote_is_updated(&manifest_path));
}
#[test]
#[cfg_attr(
any(not(feature = "manifest_refactor_project"), feature = "manifest_refactor_remote"),
ignore
)]
fn test_manifest_refactor_project() {
// Create an outdated manifest file.
let test_directory = test_dir();
let manifest_path = create_outdated_manifest_file(test_directory);
// Load the manifest file, and discard the new struct.
let _manifest = Manifest::try_from(&manifest_path).unwrap();
// Check that the manifest file project has been updated.
assert!(project_is_updated(&manifest_path));
// Check that the manifest file remote has NOT been updated.
assert!(!remote_is_updated(&manifest_path));
}
#[test]
#[cfg_attr(
any(
not(feature = "manifest_refactor_project"),
not(feature = "manifest_refactor_remote")
),
ignore
)]
fn test_manifest_refactors() {
// Create an outdated manifest file.
let test_directory = test_dir();
let manifest_path = create_outdated_manifest_file(test_directory);
// Load the manifest file, and discard the new struct.
let _manifest = Manifest::try_from(&manifest_path).unwrap();
// Check that the manifest file project has been updated.
assert!(project_is_updated(&manifest_path));
// Check that the manifest file remote has been updated.
assert!(remote_is_updated(&manifest_path));
}

View File

@ -0,0 +1,17 @@
// Copyright (C) 2019-2020 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
pub mod manifest;

73
package/tests/mod.rs Normal file
View File

@ -0,0 +1,73 @@
// Copyright (C) 2019-2020 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
pub mod initialize;
pub mod manifest;
use lazy_static::lazy_static;
use std::{
cell::RefCell,
env,
fs,
path::PathBuf,
sync::atomic::{AtomicUsize, Ordering},
};
const PACKAGE_TEST_DIRECTORY: &str = "package-testing";
thread_local! {
/// Establish a test id for each test.
pub static TEST_ID: RefCell<Option<usize>> = RefCell::new(None);
}
lazy_static! {
/// Create a testing directory for packages in `target/`
pub static ref TEST_DIR: PathBuf = {
let mut path = env::current_exe().unwrap();
path.pop(); // Remove executable name
path.pop(); // Remove 'debug'
// Attempt to point at the `target` directory
if path.file_name().and_then(|s| s.to_str()) != Some("target") {
path.pop();
}
path.push(PACKAGE_TEST_DIRECTORY);
fs::create_dir_all(&path).unwrap();
path
};
}
/// Create a new directory for each test based on the ID of the test.
pub(crate) fn test_dir() -> PathBuf {
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
TEST_ID.with(|n| *n.borrow_mut() = Some(id));
let path: PathBuf = TEST_DIR.join(&format!("t{}", id)).into();
if path.exists() {
if let Err(e) = fs::remove_dir_all(&path) {
panic!("failed to remove {:?}: {:?}", &path, e)
}
}
fs::create_dir_all(&path).unwrap();
path
}