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