Unify Settings and AllSettings (#7532)

This commit is contained in:
Micha Reiser 2023-09-20 15:56:07 +02:00 committed by GitHub
parent ca3c15858d
commit b19eec9b2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 204 additions and 147 deletions

View file

@ -59,21 +59,18 @@ pub(crate) struct Cache {
impl Cache {
/// Open or create a new cache.
///
/// `cache_dir` is considered the root directory of the cache, which can be
/// local to the project, global or otherwise set by the user.
///
/// `package_root` is the path to root of the package that is contained
/// within this cache and must be canonicalized (to avoid considering `./`
/// and `../project` being different).
///
/// Finally `settings` is used to ensure we don't open a cache for different
/// settings.
pub(crate) fn open(cache_dir: &Path, package_root: PathBuf, settings: &Settings) -> Cache {
/// settings. It also defines the directory where to store the cache.
pub(crate) fn open(package_root: PathBuf, settings: &Settings) -> Cache {
debug_assert!(package_root.is_absolute(), "package root not canonicalized");
let mut buf = itoa::Buffer::new();
let key = Path::new(buf.format(cache_key(&package_root, settings)));
let path = PathBuf::from_iter([cache_dir, Path::new("content"), key]);
let path = PathBuf::from_iter([&settings.cache_dir, Path::new("content"), key]);
let file = match File::open(&path) {
Ok(file) => file,
@ -350,7 +347,7 @@ mod tests {
use itertools::Itertools;
use ruff_cache::CACHE_DIR_NAME;
use ruff_linter::settings::{flags, AllSettings, Settings};
use ruff_linter::settings::{flags, Settings};
use crate::cache::RelativePathBuf;
use crate::cache::{self, Cache, FileCache};
@ -371,10 +368,13 @@ mod tests {
let _ = fs::remove_dir_all(&cache_dir);
cache::init(&cache_dir).unwrap();
let settings = Settings::default();
let settings = Settings {
cache_dir,
..Settings::default()
};
let package_root = fs::canonicalize(package_root).unwrap();
let cache = Cache::open(&cache_dir, package_root.clone(), &settings);
let cache = Cache::open(package_root.clone(), &settings);
assert_eq!(cache.new_files.lock().unwrap().len(), 0);
let mut paths = Vec::new();
@ -426,7 +426,7 @@ mod tests {
cache.store().unwrap();
let cache = Cache::open(&cache_dir, package_root.clone(), &settings);
let cache = Cache::open(package_root.clone(), &settings);
assert_ne!(cache.package.files.len(), 0);
parse_errors.sort();
@ -651,9 +651,8 @@ mod tests {
}
struct TestCache {
cache_dir: PathBuf,
package_root: PathBuf,
settings: AllSettings,
settings: Settings,
}
impl TestCache {
@ -672,10 +671,12 @@ mod tests {
cache::init(&cache_dir).unwrap();
fs::create_dir(package_root.clone()).unwrap();
let settings = AllSettings::default();
let settings = Settings {
cache_dir,
..Settings::default()
};
Self {
cache_dir,
package_root,
settings,
}
@ -695,11 +696,7 @@ mod tests {
}
fn open(&self) -> Cache {
Cache::open(
&self.cache_dir,
self.package_root.clone(),
&self.settings.lib,
)
Cache::open(self.package_root.clone(), &self.settings)
}
fn lint_file_with_cache(
@ -710,7 +707,7 @@ mod tests {
lint_path(
&self.package_root.join(path),
Some(&self.package_root),
&self.settings.lib,
&self.settings,
Some(cache),
flags::Noqa::Enabled,
flags::FixMode::Generate,
@ -720,7 +717,7 @@ mod tests {
impl Drop for TestCache {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.cache_dir);
let _ = fs::remove_dir_all(&self.settings.cache_dir);
}
}
}

View file

@ -58,13 +58,13 @@ pub(crate) fn check(
match pyproject_config.strategy {
PyprojectDiscoveryStrategy::Fixed => {
init_cache(&pyproject_config.settings.cli.cache_dir);
init_cache(&pyproject_config.settings.cache_dir);
}
PyprojectDiscoveryStrategy::Hierarchical => {
for settings in
std::iter::once(&pyproject_config.settings).chain(resolver.settings())
{
init_cache(&settings.cli.cache_dir);
init_cache(&settings.cache_dir);
}
}
}
@ -88,12 +88,8 @@ pub(crate) fn check(
.unique()
.par_bridge()
.map(|cache_root| {
let settings = resolver.resolve_all(cache_root, pyproject_config);
let cache = Cache::open(
&settings.cli.cache_dir,
cache_root.to_path_buf(),
&settings.lib,
);
let settings = resolver.resolve(cache_root, pyproject_config);
let cache = Cache::open(cache_root.to_path_buf(), settings);
(cache_root, cache)
})
.collect::<HashMap<&Path, Cache>>()
@ -242,7 +238,7 @@ mod test {
use ruff_linter::message::{Emitter, EmitterContext, TextEmitter};
use ruff_linter::registry::Rule;
use ruff_linter::settings::{flags, AllSettings, CliSettings, Settings};
use ruff_linter::settings::{flags, Settings};
use ruff_workspace::resolver::{PyprojectConfig, PyprojectDiscoveryStrategy};
use crate::args::Overrides;
@ -271,11 +267,8 @@ mod test {
// Configure
let snapshot = format!("{}_{}", rule_code.noqa_code(), path);
let settings = AllSettings {
cli: CliSettings::default(),
// invalid pyproject.toml is not active by default
lib: Settings::for_rules(vec![rule_code, Rule::InvalidPyprojectToml]),
};
// invalid pyproject.toml is not active by default
let settings = Settings::for_rules(vec![rule_code, Rule::InvalidPyprojectToml]);
let pyproject_config =
PyprojectConfig::new(PyprojectDiscoveryStrategy::Fixed, settings, None);

View file

@ -24,14 +24,14 @@ pub(crate) fn check_stdin(
}
}
let package_root = filename.and_then(Path::parent).and_then(|path| {
packaging::detect_package_root(path, &pyproject_config.settings.lib.namespace_packages)
packaging::detect_package_root(path, &pyproject_config.settings.namespace_packages)
});
let stdin = read_from_stdin()?;
let mut diagnostics = lint_stdin(
filename,
package_root,
stdin,
&pyproject_config.settings.lib,
&pyproject_config.settings,
noqa,
autofix,
)?;

View file

@ -39,11 +39,11 @@ pub(crate) fn format_stdin(cli: &FormatArguments, overrides: &Overrides) -> Resu
// Format the file.
let path = cli.stdin_filename.as_deref();
let preview = match pyproject_config.settings.lib.preview {
let preview = match pyproject_config.settings.preview {
PreviewMode::Enabled => ruff_python_formatter::PreviewMode::Enabled,
PreviewMode::Disabled => ruff_python_formatter::PreviewMode::Disabled,
};
let line_length = pyproject_config.settings.lib.line_length;
let line_length = pyproject_config.settings.line_length;
let options = path
.map(PyFormatOptions::from_extension)

View file

@ -11,7 +11,7 @@ use notify::{recommended_watcher, RecursiveMode, Watcher};
use ruff_linter::logging::{set_up_logging, LogLevel};
use ruff_linter::settings::types::SerializationFormat;
use ruff_linter::settings::{flags, CliSettings};
use ruff_linter::settings::{flags, Settings};
use ruff_linter::{fs, warn_user, warn_user_once};
use crate::args::{Args, CheckCommand, Command, FormatCommand};
@ -224,14 +224,14 @@ pub fn check(args: CheckCommand, log_level: LogLevel) -> Result<ExitStatus> {
// Extract options that are included in `Settings`, but only apply at the top
// level.
let CliSettings {
let Settings {
fix,
fix_only,
output_format,
show_fixes,
show_source,
..
} = pyproject_config.settings.cli;
} = pyproject_config.settings;
// Autofix rules are as follows:
// - By default, generate all fixes, but don't apply them to the filesystem.

View file

@ -7,8 +7,7 @@ use path_absolutize::path_dedot;
use ruff_workspace::configuration::Configuration;
use ruff_workspace::pyproject;
use ruff_workspace::resolver::{
resolve_settings_with_processor, ConfigProcessor, PyprojectConfig, PyprojectDiscoveryStrategy,
Relativity,
resolve_root_settings, ConfigProcessor, PyprojectConfig, PyprojectDiscoveryStrategy, Relativity,
};
use crate::args::Overrides;
@ -25,7 +24,7 @@ pub fn resolve(
if isolated {
let mut config = Configuration::default();
overrides.process_config(&mut config);
let settings = config.into_all_settings(&path_dedot::CWD)?;
let settings = config.into_settings(&path_dedot::CWD)?;
debug!("Isolated mode, not reading any pyproject.toml");
return Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Fixed,
@ -42,7 +41,7 @@ pub fn resolve(
.map(|config| shellexpand::full(&config).map(|config| PathBuf::from(config.as_ref())))
.transpose()?
{
let settings = resolve_settings_with_processor(&pyproject, Relativity::Cwd, overrides)?;
let settings = resolve_root_settings(&pyproject, Relativity::Cwd, overrides)?;
debug!(
"Using user specified pyproject.toml at {}",
pyproject.display()
@ -65,7 +64,7 @@ pub fn resolve(
.unwrap_or(&path_dedot::CWD.as_path()),
)? {
debug!("Using pyproject.toml (parent) at {}", pyproject.display());
let settings = resolve_settings_with_processor(&pyproject, Relativity::Parent, overrides)?;
let settings = resolve_root_settings(&pyproject, Relativity::Parent, overrides)?;
return Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Hierarchical,
settings,
@ -79,7 +78,7 @@ pub fn resolve(
// these act as the "default" settings.)
if let Some(pyproject) = pyproject::find_user_settings_toml() {
debug!("Using pyproject.toml (cwd) at {}", pyproject.display());
let settings = resolve_settings_with_processor(&pyproject, Relativity::Cwd, overrides)?;
let settings = resolve_root_settings(&pyproject, Relativity::Cwd, overrides)?;
return Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Hierarchical,
settings,
@ -94,7 +93,7 @@ pub fn resolve(
debug!("Using Ruff default settings");
let mut config = Configuration::default();
overrides.process_config(&mut config);
let settings = config.into_all_settings(&path_dedot::CWD)?;
let settings = config.into_settings(&path_dedot::CWD)?;
Ok(PyprojectConfig::new(
PyprojectDiscoveryStrategy::Hierarchical,
settings,