Adds structopt

This commit is contained in:
damirka 2021-08-02 17:34:03 +03:00 committed by Eric McCarthy
parent 98ee5af387
commit cb5a5eb0f4
3 changed files with 51 additions and 18 deletions

1
Cargo.lock generated
View File

@ -1386,6 +1386,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"serde_yaml", "serde_yaml",
"structopt",
] ]
[[package]] [[package]]

View File

@ -48,3 +48,6 @@ version = "1.5.2"
[dependencies.leo-compiler] [dependencies.leo-compiler]
path = "../compiler" path = "../compiler"
version = "1.5.2" version = "1.5.2"
[dependencies.structopt]
version = "0.3"

View File

@ -14,36 +14,45 @@
// You should have received a copy of the GNU General Public License // 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/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use leo_asg::Asg;
use leo_compiler::{compiler::thread_leaked_context, TypeInferencePhase};
use leo_imports::ImportParser;
use leo_test_framework::{ use leo_test_framework::{
fetch::find_tests, fetch::find_tests,
test::{extract_test_config, TestExpectationMode as Expectation}, test::{extract_test_config, TestExpectationMode as Expectation},
}; };
// use leo_test_framework::runner::{Runner, Namespace};
use leo_asg::Asg; use std::{error::Error, fs, path::PathBuf};
use leo_compiler::{compiler::thread_leaked_context, TypeInferencePhase}; use structopt::{clap::AppSettings, StructOpt};
use leo_imports::ImportParser;
use std::{ #[derive(StructOpt)]
error::Error, #[structopt(name = "ast-stages-generator", author = "The Aleo Team <hello@aleo.org>", setting = AppSettings::ColoredHelp)]
path::{Path, PathBuf}, struct Opt {
}; #[structopt(
short,
long,
help = "Path to the output folder (auto generated)",
default_value = "tmp/tgc"
)]
path: PathBuf,
use std::fs; #[structopt(short, long, help = "Filter test names and run only matching")]
filter: Option<String>,
}
fn main() -> Result<(), Box<dyn Error>> { fn main() {
handle_error(run_with_args(Opt::from_args()));
}
fn run_with_args(opt: Opt) -> Result<(), Box<dyn Error>> {
// Variable that stores all the tests.
let mut tests = Vec::new(); let mut tests = Vec::new();
let mut test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); let mut test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_dir.push("../tests/"); test_dir.push("../tests/");
find_tests(&test_dir, &mut tests); find_tests(&test_dir, &mut tests);
if !Path::new("tmp").exists() { if !opt.path.exists() {
fs::create_dir("tmp")?; fs::create_dir_all(&opt.path)?;
}
if !Path::new("tmp/tgc").exists() {
fs::create_dir("tmp/tgc")?;
} }
// Prepare directory for placing results. // Prepare directory for placing results.
@ -60,12 +69,22 @@ fn main() -> Result<(), Box<dyn Error>> {
.unwrap() .unwrap()
.replace(".leo", ""); .replace(".leo", "");
// Filter out the tests that do not match pattern, if pattern is set.
if let Some(filter) = &opt.filter {
if !test_name.contains(filter) {
continue;
}
}
test_name.push_str(&format!("_{}", index)); test_name.push_str(&format!("_{}", index));
// Create directory for this file. // Create directory for this file.
let mut target = PathBuf::from("tmp/tgc"); let mut target = PathBuf::from("tmp/tgc");
target.push(test_name); target.push(test_name);
fs::create_dir(target.clone())?;
if !target.exists() {
fs::create_dir_all(target.clone())?;
}
// Write all files into the directory. // Write all files into the directory.
let (initial, canonicalized, _type_inferenced) = generate_asts(path, text)?; let (initial, canonicalized, _type_inferenced) = generate_asts(path, text)?;
@ -103,3 +122,13 @@ fn generate_asts(path: &String, text: &String) -> Result<(String, String, String
Ok((initial, canonicalized, type_inferenced)) Ok((initial, canonicalized, type_inferenced))
} }
fn handle_error(res: Result<(), Box<dyn Error>>) {
match res {
Ok(_) => (),
Err(err) => {
eprintln!("Error: {}", err);
std::process::exit(1);
}
}
}