Skip to content

Commit

Permalink
Initialize tests directory on leo new
Browse files Browse the repository at this point in the history
  • Loading branch information
d0cd committed Nov 23, 2024
1 parent dc60453 commit 520f1f8
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 0 deletions.
7 changes: 7 additions & 0 deletions errors/src/errors/package/package_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,11 @@ create_messages!(
msg: format!("Failed to load leo project at path {path}"),
help: Some("Make sure that the path is correct and that the project exists.".to_string()),
}

@backtraced
failed_to_create_test_directory {
args: (error: impl ErrorArg),
msg: format!("Failed to create test directory {error}."),
help: None,
}
);
1 change: 1 addition & 0 deletions leo/package/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod outputs;
pub mod package;
pub mod root;
pub mod source;
pub mod tst;

use leo_errors::{PackageError, Result};

Expand Down
8 changes: 8 additions & 0 deletions leo/package/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
use crate::{
root::{Env, Gitignore},
source::{MainFile, SourceDirectory},
tst::TestDirectory,
};
use leo_errors::{PackageError, Result};

use crate::tst::DefaultTestFile;
use leo_retriever::{Manifest, NetworkName};
use serde::Deserialize;
use snarkvm::prelude::{Network, PrivateKey};
Expand Down Expand Up @@ -174,6 +176,12 @@ impl Package {
.into());
}

// Create the test directory.
TestDirectory::create(&path)?;

// Create the default test file.
DefaultTestFile::write_to(&path)?;

Ok(())
}
}
Expand Down
57 changes: 57 additions & 0 deletions leo/package/src/tst/default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (C) 2019-2024 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/>.

//! The default file provided when invoking `leo new` to create a new package.
use crate::tst::directory::TEST_DIRECTORY_NAME;
use leo_errors::{PackageError, Result};

use serde::Deserialize;

Check warning on line 22 in leo/package/src/tst/default.rs

View workflow job for this annotation

GitHub Actions / Code Coverage

unused import: `serde::Deserialize`
use std::{borrow::Cow, fs::File, io::Write, path::Path};

pub static DEFAULT_TEST_FILENAME: &str = "test.leo";

pub struct DefaultTestFile;

impl DefaultTestFile {
pub fn write_to(path: &Path) -> Result<()> {
let mut path = Cow::from(path);
if path.is_dir() {
if !path.ends_with(TEST_DIRECTORY_NAME) {
path.to_mut().push(TEST_DIRECTORY_NAME);
}
path.to_mut().push(DEFAULT_TEST_FILENAME);
}

let mut file = File::create(&path).map_err(PackageError::io_error_main_file)?;
Ok(file.write_all(Self::template().as_bytes()).map_err(PackageError::io_error_main_file)?)
}

fn template() -> String {
format!(
r#"// A default Leo test file.
// To learn more about testing your program, see the documentation at https://docs.leo-lang.org
@native_test
@interpreted_test
transition test_helloworld() {{
let result: u32 = helloworld.aleo/main(1u32, 2u32)
assert_eq!(result, 3u32)
}}
"#,
)
}
}
58 changes: 58 additions & 0 deletions leo/package/src/tst/directory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (C) 2019-2024 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::parse_file_paths;

use leo_errors::{PackageError, Result};

use crate::source::MAIN_FILENAME;

Check warning on line 21 in leo/package/src/tst/directory.rs

View workflow job for this annotation

GitHub Actions / Code Coverage

unused import: `crate::source::MAIN_FILENAME`
use std::{
borrow::Cow,
fs,
path::{Path, PathBuf},
};

pub static TEST_DIRECTORY_NAME: &str = "tests/";

pub struct TestDirectory;

impl TestDirectory {
/// Creates a directory at the provided path with the default directory name.
pub fn create(path: &Path) -> Result<()> {
let mut path = Cow::from(path);
if path.is_dir() && !path.ends_with(TEST_DIRECTORY_NAME) {
path.to_mut().push(TEST_DIRECTORY_NAME);
}

fs::create_dir_all(&path).map_err(PackageError::failed_to_create_test_directory)?;
Ok(())
}

/// Returns a list of files in the test directory.
pub fn files(path: &Path) -> Result<Vec<PathBuf>> {
let mut path = Cow::from(path);
if path.is_dir() && !path.ends_with(TEST_DIRECTORY_NAME) {
path.to_mut().push(TEST_DIRECTORY_NAME);
}

let directory = fs::read_dir(&path).map_err(|err| PackageError::failed_to_read_file(path.display(), err))?;
let mut file_paths = Vec::new();

parse_file_paths(directory, &mut file_paths)?;

Ok(file_paths)
}
}
21 changes: 21 additions & 0 deletions leo/package/src/tst/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2019-2024 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 default;
pub use default::*;

pub mod directory;
pub use directory::*;

0 comments on commit 520f1f8

Please sign in to comment.