1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

refactor: Use dedicated flags structures (#13148)

This commit is contained in:
Bartek Iwańczuk 2021-12-20 22:29:02 +01:00 committed by GitHub
parent 04fe513003
commit 1eb78731eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 130 deletions

View file

@ -564,7 +564,6 @@ async fn lsp_command() -> Result<i32, AnyError> {
Ok(0)
}
#[allow(clippy::too_many_arguments)]
async fn lint_command(
flags: Flags,
lint_flags: LintFlags,
@ -915,14 +914,7 @@ async fn doc_command(
flags: Flags,
doc_flags: DocFlags,
) -> Result<i32, AnyError> {
tools::doc::print_docs(
flags,
doc_flags.source_file,
doc_flags.json,
doc_flags.filter,
doc_flags.private,
)
.await?;
tools::doc::print_docs(flags, doc_flags).await?;
Ok(0)
}
@ -1279,15 +1271,7 @@ async fn coverage_command(
return Err(generic_error("No matching coverage profiles found"));
}
tools::coverage::cover_files(
flags.clone(),
coverage_flags.files,
coverage_flags.ignore,
coverage_flags.include,
coverage_flags.exclude,
coverage_flags.lcov,
)
.await?;
tools::coverage::cover_files(flags, coverage_flags).await?;
Ok(0)
}
@ -1304,36 +1288,11 @@ async fn test_command(
}
if flags.watch.is_some() {
tools::test::run_tests_with_watch(
flags,
test_flags.include,
test_flags.ignore,
test_flags.doc,
test_flags.no_run,
test_flags.fail_fast,
test_flags.filter,
test_flags.shuffle,
test_flags.concurrent_jobs,
)
.await?;
return Ok(0);
tools::test::run_tests_with_watch(flags, test_flags).await?;
} else {
tools::test::run_tests(flags, test_flags).await?;
}
tools::test::run_tests(
flags,
test_flags.include,
test_flags.ignore,
test_flags.doc,
test_flags.no_run,
test_flags.fail_fast,
test_flags.allow_none,
test_flags.filter,
test_flags.shuffle,
test_flags.concurrent_jobs,
)
.await?;
Ok(0)
}

View file

@ -2,6 +2,7 @@
use crate::colors;
use crate::emit;
use crate::flags::CoverageFlags;
use crate::flags::Flags;
use crate::fs_util::collect_files;
use crate::proc_state::ProcState;
@ -31,47 +32,47 @@ use uuid::Uuid;
// inspector::protocol.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CoverageRange {
pub start_offset: usize,
pub end_offset: usize,
pub count: usize,
struct CoverageRange {
start_offset: usize,
end_offset: usize,
count: usize,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct FunctionCoverage {
pub function_name: String,
pub ranges: Vec<CoverageRange>,
pub is_block_coverage: bool,
struct FunctionCoverage {
function_name: String,
ranges: Vec<CoverageRange>,
is_block_coverage: bool,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ScriptCoverage {
pub script_id: String,
pub url: String,
pub functions: Vec<FunctionCoverage>,
struct ScriptCoverage {
script_id: String,
url: String,
functions: Vec<FunctionCoverage>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StartPreciseCoverageParameters {
pub call_count: bool,
pub detailed: bool,
pub allow_triggered_updates: bool,
struct StartPreciseCoverageParameters {
call_count: bool,
detailed: bool,
allow_triggered_updates: bool,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StartPreciseCoverageReturnObject {
pub timestamp: f64,
struct StartPreciseCoverageReturnObject {
timestamp: f64,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TakePreciseCoverageReturnObject {
pub result: Vec<ScriptCoverage>,
pub timestamp: f64,
struct TakePreciseCoverageReturnObject {
result: Vec<ScriptCoverage>,
timestamp: f64,
}
pub struct CoverageCollector {
@ -86,25 +87,21 @@ impl CoverageCollector {
async fn enable_debugger(&mut self) -> Result<(), AnyError> {
self.session.post_message("Debugger.enable", None).await?;
Ok(())
}
async fn enable_profiler(&mut self) -> Result<(), AnyError> {
self.session.post_message("Profiler.enable", None).await?;
Ok(())
}
async fn disable_debugger(&mut self) -> Result<(), AnyError> {
self.session.post_message("Debugger.disable", None).await?;
Ok(())
}
async fn disable_profiler(&mut self) -> Result<(), AnyError> {
self.session.post_message("Profiler.disable", None).await?;
Ok(())
}
@ -174,7 +171,7 @@ impl CoverageCollector {
}
}
pub enum CoverageReporterKind {
enum CoverageReporterKind {
Pretty,
Lcov,
}
@ -188,7 +185,7 @@ fn create_reporter(
}
}
pub trait CoverageReporter {
trait CoverageReporter {
fn visit_coverage(
&mut self,
script_coverage: &ScriptCoverage,
@ -200,7 +197,7 @@ pub trait CoverageReporter {
fn done(&mut self);
}
pub struct LcovCoverageReporter {}
struct LcovCoverageReporter {}
impl LcovCoverageReporter {
pub fn new() -> LcovCoverageReporter {
@ -423,7 +420,7 @@ impl CoverageReporter for LcovCoverageReporter {
fn done(&mut self) {}
}
pub struct PrettyCoverageReporter {}
struct PrettyCoverageReporter {}
impl PrettyCoverageReporter {
pub fn new() -> PrettyCoverageReporter {
@ -676,18 +673,19 @@ fn filter_coverages(
pub async fn cover_files(
flags: Flags,
files: Vec<PathBuf>,
ignore: Vec<PathBuf>,
include: Vec<String>,
exclude: Vec<String>,
lcov: bool,
coverage_flags: CoverageFlags,
) -> Result<(), AnyError> {
let ps = ProcState::build(flags).await?;
let script_coverages = collect_coverages(files, ignore)?;
let script_coverages = filter_coverages(script_coverages, include, exclude);
let script_coverages =
collect_coverages(coverage_flags.files, coverage_flags.ignore)?;
let script_coverages = filter_coverages(
script_coverages,
coverage_flags.include,
coverage_flags.exclude,
);
let reporter_kind = if lcov {
let reporter_kind = if coverage_flags.lcov {
CoverageReporterKind::Lcov
} else {
CoverageReporterKind::Pretty

View file

@ -2,6 +2,7 @@
use crate::colors;
use crate::file_fetcher::File;
use crate::flags::DocFlags;
use crate::flags::Flags;
use crate::get_types;
use crate::proc_state::ProcState;
@ -92,13 +93,12 @@ impl Loader for DocLoader {
pub async fn print_docs(
flags: Flags,
source_file: Option<String>,
json: bool,
maybe_filter: Option<String>,
private: bool,
doc_flags: DocFlags,
) -> Result<(), AnyError> {
let ps = ProcState::build(flags.clone()).await?;
let source_file = source_file.unwrap_or_else(|| "--builtin".to_string());
let source_file = doc_flags
.source_file
.unwrap_or_else(|| "--builtin".to_string());
let source_parser = deno_graph::DefaultSourceParser::new();
let parse_result = if source_file == "--builtin" {
@ -115,7 +115,8 @@ pub async fn print_docs(
None,
)
.await;
let doc_parser = doc::DocParser::new(graph, private, &source_parser);
let doc_parser =
doc::DocParser::new(graph, doc_flags.private, &source_parser);
doc_parser.parse_source(
&source_file_specifier,
MediaType::Dts,
@ -153,7 +154,8 @@ pub async fn print_docs(
None,
)
.await;
let doc_parser = doc::DocParser::new(graph, private, &source_parser);
let doc_parser =
doc::DocParser::new(graph, doc_flags.private, &source_parser);
doc_parser.parse_with_reexports(&root_specifier)
};
@ -165,11 +167,11 @@ pub async fn print_docs(
}
};
if json {
if doc_flags.json {
write_json_to_stdout(&doc_nodes)
} else {
doc_nodes.retain(|doc_node| doc_node.kind != doc::DocNodeKind::Import);
let details = if let Some(filter) = maybe_filter {
let details = if let Some(filter) = doc_flags.filter {
let nodes =
doc::find_nodes_by_name_recursively(doc_nodes, filter.clone());
if nodes.is_empty() {
@ -178,12 +180,16 @@ pub async fn print_docs(
}
format!(
"{}",
doc::DocPrinter::new(&nodes, colors::use_color(), private)
doc::DocPrinter::new(&nodes, colors::use_color(), doc_flags.private)
)
} else {
format!(
"{}",
doc::DocPrinter::new(&doc_nodes, colors::use_color(), private)
doc::DocPrinter::new(
&doc_nodes,
colors::use_color(),
doc_flags.private
)
)
};

View file

@ -11,6 +11,7 @@ use crate::file_watcher;
use crate::file_watcher::ResolutionResult;
use crate::flags::CheckFlag;
use crate::flags::Flags;
use crate::flags::TestFlags;
use crate::fs_util::collect_specifiers;
use crate::fs_util::is_supported_test_ext;
use crate::fs_util::is_supported_test_path;
@ -1000,30 +1001,21 @@ async fn fetch_specifiers_with_test_mode(
Ok(specifiers_with_mode)
}
#[allow(clippy::too_many_arguments)]
pub async fn run_tests(
flags: Flags,
include: Option<Vec<String>>,
ignore: Vec<PathBuf>,
doc: bool,
no_run: bool,
fail_fast: Option<NonZeroUsize>,
allow_none: bool,
filter: Option<String>,
shuffle: Option<u64>,
concurrent_jobs: NonZeroUsize,
test_flags: TestFlags,
) -> Result<(), AnyError> {
let ps = ProcState::build(flags.clone()).await?;
let permissions = Permissions::from_options(&flags.clone().into());
let specifiers_with_mode = fetch_specifiers_with_test_mode(
ps.clone(),
include.unwrap_or_else(|| vec![".".to_string()]),
ignore.clone(),
doc,
test_flags.include.unwrap_or_else(|| vec![".".to_string()]),
test_flags.ignore.clone(),
test_flags.doc,
)
.await?;
if !allow_none && specifiers_with_mode.is_empty() {
if !test_flags.allow_none && specifiers_with_mode.is_empty() {
return Err(generic_error("No test modules found"));
}
@ -1041,7 +1033,7 @@ pub async fn run_tests(
)
.await?;
if no_run {
if test_flags.no_run {
return Ok(());
}
@ -1049,27 +1041,19 @@ pub async fn run_tests(
ps,
permissions,
specifiers_with_mode,
fail_fast,
filter,
shuffle,
concurrent_jobs,
test_flags.fail_fast,
test_flags.filter,
test_flags.shuffle,
test_flags.concurrent_jobs,
)
.await?;
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub async fn run_tests_with_watch(
flags: Flags,
include: Option<Vec<String>>,
ignore: Vec<PathBuf>,
doc: bool,
no_run: bool,
fail_fast: Option<NonZeroUsize>,
filter: Option<String>,
shuffle: Option<u64>,
concurrent_jobs: NonZeroUsize,
test_flags: TestFlags,
) -> Result<(), AnyError> {
let ps = ProcState::build(flags.clone()).await?;
let permissions = Permissions::from_options(&flags.clone().into());
@ -1080,7 +1064,8 @@ pub async fn run_tests_with_watch(
emit::TypeLib::DenoWindow
};
let include = include.unwrap_or_else(|| vec![".".to_string()]);
let include = test_flags.include.unwrap_or_else(|| vec![".".to_string()]);
let ignore = test_flags.ignore.clone();
let paths_to_watch: Vec<_> = include.iter().map(PathBuf::from).collect();
let no_check = ps.flags.check == CheckFlag::None;
@ -1115,7 +1100,7 @@ pub async fn run_tests_with_watch(
let ignore = ignore.clone();
async move {
let test_modules = if doc {
let test_modules = if test_flags.doc {
collect_specifiers(include.clone(), &ignore, is_supported_test_ext)
} else {
collect_specifiers(include.clone(), &ignore, is_supported_test_path)
@ -1242,7 +1227,7 @@ pub async fn run_tests_with_watch(
};
let operation = |modules_to_reload: Vec<ModuleSpecifier>| {
let filter = filter.clone();
let filter = test_flags.filter.clone();
let include = include.clone();
let ignore = ignore.clone();
let lib = lib.clone();
@ -1254,7 +1239,7 @@ pub async fn run_tests_with_watch(
ps.clone(),
include.clone(),
ignore.clone(),
doc,
test_flags.doc,
)
.await?
.iter()
@ -1270,7 +1255,7 @@ pub async fn run_tests_with_watch(
)
.await?;
if no_run {
if test_flags.no_run {
return Ok(());
}
@ -1278,10 +1263,10 @@ pub async fn run_tests_with_watch(
ps.clone(),
permissions.clone(),
specifiers_with_mode,
fail_fast,
test_flags.fail_fast,
filter.clone(),
shuffle,
concurrent_jobs,
test_flags.shuffle,
test_flags.concurrent_jobs,
)
.await?;