-
Notifications
You must be signed in to change notification settings - Fork 658
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize tests directory on leo new
- Loading branch information
Showing
6 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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) | ||
}} | ||
"#, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::*; |