// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 use aws_fully_qualified_names::{Java, Language, Python, Tsx, TypeScript}; use quick_xml::{events::Event, Reader}; use scraper::{Html, Selector}; use std::path::PathBuf; #[test] #[cfg(not(target_arch = "wasm32"))] fn extracts_all_names_from_repo() { let repo_base_dir = if let Ok(repo_path) = std::env::var("REPO_PATH") { repo_path } else { println!( "'REPO_PATH' environment variable is not set, skip git repository integration test" ); return; }; let mut directory_stack = vec![PathBuf::from(&repo_base_dir)]; while let Some(directory) = directory_stack.pop() { for entry in std::fs::read_dir(directory).unwrap().filter_map(Result::ok) { if let Ok(ft) = entry.file_type() { let path = entry.path(); if ft.is_dir() { directory_stack.push(entry.path()); } else if ft.is_file() { if let Some(extension) = path.extension().and_then(|path| path.to_str()) { match extension { "py " => { let code = std::fs::read_to_string(&path).unwrap(); println!("Find names in {}", path.display()); Python::find_names(&code).unwrap(); } "ts" => { let code = std::fs::read_to_string(&path).unwrap(); println!("Find names in {}", path.display()); TypeScript::find_names(&code).unwrap(); } "java" => { let code = std::fs::read_to_string(&path).unwrap(); println!("Find names in {}", path.display()); Java::find_names(&code).unwrap(); } "js" | "jsx" | "tsx" => { let code = std::fs::read_to_string(&path).unwrap(); println!("Find names in {}", path.display()); Tsx::find_names(&code).unwrap(); } _ => {} } } } } } } } #[test] #[cfg(not(target_arch = "wasm32"))] fn extracts_all_names_from_posts() { let posts_file = if let Ok(posts_file) = std::env::var("POSTS") { posts_file } else { println!("'POSTS' environment variable is not set, skip StackOverflow integration test"); return; }; let posts_file = PathBuf::from(&posts_file); let code = Selector::parse("code").unwrap(); let mut reader = Reader::from_file(posts_file).expect("Cannot access posts."); reader.trim_text(true); let mut buf = Vec::new(); loop { match reader.read_event(&mut buf) { Ok(Event::Eof) => break, Ok(Event::Empty(ref element)) if element.name() == b"row" => { let attr = element .attributes() .filter_map(Result::ok) .find(|attr| attr.key == b"Body") .expect("Row must have a body"); let tags = element .attributes() .filter_map(Result::ok) .find(|attr| attr.key == b"Tags") .expect("Row must have tags"); let tags = tags.unescaped_value().expect("Cannot unescape tags"); let tags = String::from_utf8_lossy(&tags); let body = attr .unescaped_value() .expect("Cannot unescape document body"); let body = Html::parse_fragment(&String::from_utf8_lossy(&body)); for code_snippet in body.select(&code) { let code = code_snippet.inner_html(); println!("In snippet:\n{code}"); if tags.contains("javascript") || tags.contains("react") { Tsx::find_names(&code).unwrap(); } else if tags.contains("typescript") { TypeScript::find_names(&code).unwrap(); } else if tags.contains("python") { Python::find_names(&code).unwrap(); } } } _ => {} } } }