Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): support configuring the test tool in the config file #15079

Merged
merged 17 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions cli/args/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,28 @@ pub struct FmtConfig {
pub files: FilesConfig,
}

#[derive(Clone, Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
struct SerializedTestConfig {
pub files: SerializedFilesConfig,
}

impl SerializedTestConfig {
pub fn into_resolved(
self,
config_file_specifier: &ModuleSpecifier,
) -> Result<TestConfig, AnyError> {
Ok(TestConfig {
files: self.files.into_resolved(config_file_specifier)?,
})
}
}

#[derive(Clone, Debug, Default)]
pub struct TestConfig {
pub files: FilesConfig,
}

#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigFileJson {
Expand All @@ -427,6 +449,7 @@ pub struct ConfigFileJson {
pub lint: Option<Value>,
pub fmt: Option<Value>,
pub tasks: Option<Value>,
pub test: Option<Value>,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -605,6 +628,16 @@ impl ConfigFile {
}
}

pub fn to_test_config(&self) -> Result<Option<TestConfig>, AnyError> {
if let Some(config) = self.json.test.clone() {
let lint_config: SerializedTestConfig = serde_json::from_value(config)
.context("Failed to parse \"test\" configuration")?;
Ok(Some(lint_config.into_resolved(&self.specifier)?))
} else {
Ok(None)
}
}

/// Return any tasks that are defined in the configuration file as a sequence
/// of JSON objects providing the name of the task and the arguments of the
/// task in a detail field.
Expand Down
25 changes: 12 additions & 13 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub struct TestFlags {
pub no_run: bool,
pub fail_fast: Option<NonZeroUsize>,
pub allow_none: bool,
pub include: Option<Vec<String>>,
pub include: Vec<String>,
pub filter: Option<String>,
pub shuffle: Option<u64>,
pub concurrent_jobs: NonZeroUsize,
Expand Down Expand Up @@ -2674,15 +2674,14 @@ fn test_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
NonZeroUsize::new(1).unwrap()
};

let include = if matches.is_present("files") {
let files: Vec<String> = matches
let include: Vec<String> = if matches.is_present("files") {
matches
.values_of("files")
.unwrap()
.map(String::from)
.collect();
Some(files)
.collect::<Vec<_>>()
} else {
None
Vec::new()
};

flags.coverage_dir = matches.value_of("coverage").map(String::from);
Expand Down Expand Up @@ -4987,7 +4986,7 @@ mod tests {
fail_fast: None,
filter: Some("- foo".to_string()),
allow_none: true,
include: Some(svec!["dir1/", "dir2/"]),
include: svec!["dir1/", "dir2/"],
ignore: vec![],
shuffle: None,
concurrent_jobs: NonZeroUsize::new(1).unwrap(),
Expand Down Expand Up @@ -5059,7 +5058,7 @@ mod tests {
filter: None,
allow_none: false,
shuffle: None,
include: None,
include: vec![],
ignore: vec![],
concurrent_jobs: NonZeroUsize::new(4).unwrap(),
trace_ops: false,
Expand Down Expand Up @@ -5087,7 +5086,7 @@ mod tests {
filter: None,
allow_none: false,
shuffle: None,
include: None,
include: vec![],
ignore: vec![],
concurrent_jobs: NonZeroUsize::new(1).unwrap(),
trace_ops: false,
Expand Down Expand Up @@ -5119,7 +5118,7 @@ mod tests {
filter: None,
allow_none: false,
shuffle: None,
include: None,
include: vec![],
ignore: vec![],
concurrent_jobs: NonZeroUsize::new(1).unwrap(),
trace_ops: false,
Expand All @@ -5145,7 +5144,7 @@ mod tests {
filter: None,
allow_none: false,
shuffle: Some(1),
include: None,
include: vec![],
ignore: vec![],
concurrent_jobs: NonZeroUsize::new(1).unwrap(),
trace_ops: false,
Expand All @@ -5171,7 +5170,7 @@ mod tests {
filter: None,
allow_none: false,
shuffle: None,
include: None,
include: vec![],
ignore: vec![],
concurrent_jobs: NonZeroUsize::new(1).unwrap(),
trace_ops: false,
Expand All @@ -5198,7 +5197,7 @@ mod tests {
filter: None,
allow_none: false,
shuffle: None,
include: None,
include: vec![],
ignore: vec![],
concurrent_jobs: NonZeroUsize::new(1).unwrap(),
trace_ops: false,
Expand Down
9 changes: 9 additions & 0 deletions cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub use config_file::LintConfig;
pub use config_file::LintRulesConfig;
pub use config_file::MaybeImportsResult;
pub use config_file::ProseWrap;
pub use config_file::TestConfig;
pub use config_file::TsConfig;
pub use flags::*;

Expand Down Expand Up @@ -244,6 +245,14 @@ impl CliOptions {
}
}

pub fn to_test_config(&self) -> Result<Option<TestConfig>, AnyError> {
if let Some(config_file) = &self.maybe_config_file {
config_file.to_test_config()
} else {
Ok(None)
}
}

pub fn to_fmt_config(&self) -> Result<Option<FmtConfig>, AnyError> {
if let Some(config) = &self.maybe_config_file {
config.to_fmt_config()
Expand Down
11 changes: 11 additions & 0 deletions cli/lsp/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ use crate::args::ConfigFile;
use crate::args::Flags;
use crate::args::FmtConfig;
use crate::args::LintConfig;
use crate::args::TestConfig;
use crate::args::TsConfig;
use crate::deno_dir;
use crate::file_fetcher::get_source_from_data_url;
Expand Down Expand Up @@ -121,6 +122,8 @@ pub struct Inner {
maybe_import_map_uri: Option<Url>,
/// An optional configuration for linter which has been taken from specified config file.
pub maybe_lint_config: Option<LintConfig>,
/// An optional configuration for the test subcommand which has been taken from specified config file.
pub maybe_test_config: Option<TestConfig>,
rojvv marked this conversation as resolved.
Show resolved Hide resolved
/// A lazily create "server" for handling test run requests.
maybe_testing_server: Option<testing::TestServer>,
/// A collection of measurements which instrument that performance of the LSP.
Expand Down Expand Up @@ -255,6 +258,7 @@ impl Inner {
maybe_import_map_uri: None,
maybe_lint_config: None,
maybe_fmt_config: None,
maybe_test_config: None,
maybe_testing_server: None,
module_registries,
module_registries_location,
Expand Down Expand Up @@ -631,10 +635,17 @@ impl Inner {
anyhow!("Unable to update formatter configuration: {:?}", err)
})?
.unwrap_or_default();
let test_config = config_file
.to_test_config()
.map_err(|err| {
anyhow!("Unable to update test configuration: {:?}", err)
})?
.unwrap_or_default();

self.maybe_config_file = Some(config_file);
self.maybe_lint_config = Some(lint_config);
self.maybe_fmt_config = Some(fmt_config);
self.maybe_test_config = Some(test_config);
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion cli/tools/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub async fn lint(flags: Flags, lint_flags: LintFlags) -> Result<(), AnyError> {
} = lint_flags;
// First, prepare final configuration.
// Collect included and ignored files. CLI flags take precendence
// over config file, ie. if there's `files.ignore` in config file
// over config file, i.e. if there's `files.ignore` in config file
// and `--ignore` CLI flag, only the flag value is taken into account.
let mut include_files = args.clone();
let mut exclude_files = ignore.clone();
Expand Down
41 changes: 37 additions & 4 deletions cli/tools/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::fmt_errors::format_js_error;
use crate::fs_util::collect_specifiers;
use crate::fs_util::is_supported_test_ext;
use crate::fs_util::is_supported_test_path;
use crate::fs_util::specifier_to_file_path;
use crate::graph_util::contains_specifier;
use crate::graph_util::graph_valid;
use crate::located_script_name;
Expand Down Expand Up @@ -1313,8 +1314,40 @@ async fn fetch_specifiers_with_test_mode(
ignore: Vec<PathBuf>,
include_inline: bool,
) -> Result<Vec<(ModuleSpecifier, TestMode)>, AnyError> {
let mut specifiers_with_mode =
collect_specifiers_with_test_mode(include, ignore, include_inline)?;
let maybe_test_config = ps.options.to_test_config()?;

let mut include_files = include.clone();
let mut exclude_files = ignore.clone();

if let Some(test_config) = maybe_test_config.as_ref() {
if include_files.is_empty() {
include_files = test_config
.files
.include
.iter()
.map(|s| s.to_string())
.collect::<Vec<_>>();
}

if exclude_files.is_empty() {
exclude_files = test_config
.files
.exclude
.iter()
.filter_map(|s| specifier_to_file_path(s).ok())
.collect::<Vec<_>>();
}
}

if include_files.is_empty() {
include_files.push(".".to_string());
}

let mut specifiers_with_mode = collect_specifiers_with_test_mode(
include_files,
exclude_files,
include_inline,
)?;
for (specifier, mode) in &mut specifiers_with_mode {
let file = ps
.file_fetcher
Expand All @@ -1340,7 +1373,7 @@ pub async fn run_tests(
Permissions::from_options(&ps.options.permissions_options());
let specifiers_with_mode = fetch_specifiers_with_test_mode(
&ps,
test_flags.include.unwrap_or_else(|| vec![".".to_string()]),
test_flags.include,
test_flags.ignore.clone(),
test_flags.doc,
)
Expand Down Expand Up @@ -1384,7 +1417,7 @@ pub async fn run_tests_with_watch(
let permissions =
Permissions::from_options(&ps.options.permissions_options());

let include = test_flags.include.unwrap_or_else(|| vec![".".to_string()]);
let include = test_flags.include;
let ignore = test_flags.ignore.clone();
let paths_to_watch: Vec<_> = include.iter().map(PathBuf::from).collect();
let no_check = ps.options.type_check_mode() == TypeCheckMode::None;
Expand Down