clean.rs
1 // Copyright (c) 2025 ADnet Contributors 2 // This file is part of the AlphaVM library. 3 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 8 // http://www.apache.org/licenses/LICENSE-2.0 9 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 use super::*; 17 18 impl<N: Network> Package<N> { 19 /// Removes the build directory for the package. 20 pub fn clean(directory: &Path) -> Result<()> { 21 // Ensure the directory path exists. 22 ensure!(directory.exists(), "The program directory does not exist: {}", directory.display()); 23 // Ensure the manifest file exists. 24 ensure!( 25 Manifest::<N>::exists_at(directory), 26 "Missing '{}' at '{}'", 27 Manifest::<N>::file_name(), 28 directory.display() 29 ); 30 // Ensure the main program file exists. 31 ensure!( 32 AleoFile::<N>::main_exists_at(directory), 33 "Missing '{}' at '{}'", 34 AleoFile::<N>::main_file_name(), 35 directory.display() 36 ); 37 38 // Prepare the build directory. 39 let build_directory = directory.join("build"); 40 // Remove the build directory if it exists. 41 if build_directory.exists() { 42 std::fs::remove_dir_all(&build_directory)?; 43 } 44 45 Ok(()) 46 } 47 } 48 49 #[cfg(test)] 50 mod tests { 51 use super::*; 52 53 type CurrentNetwork = alphavm_console::network::MainnetV0; 54 type CurrentAleo = alphavm_circuit::network::AleoV0; 55 56 #[test] 57 #[ignore] // TODO: Requires credits.alpha → credits.alpha migration 58 fn test_clean() { 59 // Samples a new package at a temporary directory. 60 let (directory, package) = crate::package::test_helpers::sample_token_package(); 61 62 // Ensure the build directory does *not* exist. 63 assert!(!package.build_directory().exists()); 64 // Clean the package. 65 Package::<CurrentNetwork>::clean(&directory).unwrap(); 66 // Ensure the build directory still does *not* exist. 67 assert!(!package.build_directory().exists()); 68 69 // Build the package. 70 package.build::<CurrentAleo>().unwrap(); 71 72 // Ensure the build directory exists. 73 assert!(package.build_directory().exists()); 74 // Clean the package. 75 Package::<CurrentNetwork>::clean(&directory).unwrap(); 76 // Ensure the build directory does *not* exist. 77 assert!(!package.build_directory().exists()); 78 79 // Proactively remove the temporary directory (to conserve space). 80 std::fs::remove_dir_all(directory).unwrap(); 81 } 82 83 #[test] 84 #[ignore] // TODO: Requires credits.alpha → credits.alpha migration 85 fn test_clean_with_import() { 86 // Samples a new package at a temporary directory. 87 let (directory, package) = crate::package::test_helpers::sample_wallet_package(); 88 89 // Ensure the build directory does *not* exist. 90 assert!(!package.build_directory().exists()); 91 // Clean the package. 92 Package::<CurrentNetwork>::clean(&directory).unwrap(); 93 // Ensure the build directory still does *not* exist. 94 assert!(!package.build_directory().exists()); 95 96 // Build the package. 97 package.build::<CurrentAleo>().unwrap(); 98 99 // Ensure the build directory exists. 100 assert!(package.build_directory().exists()); 101 // Clean the package. 102 Package::<CurrentNetwork>::clean(&directory).unwrap(); 103 // Ensure the build directory does *not* exist. 104 assert!(!package.build_directory().exists()); 105 106 // Proactively remove the temporary directory (to conserve space). 107 std::fs::remove_dir_all(directory).unwrap(); 108 } 109 }