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

Unify Settings and AllSettings #7532

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
67 changes: 49 additions & 18 deletions crates/ruff_cache/tests/cache_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,60 @@ use std::hash::{Hash, Hasher};
use ruff_cache::{CacheKey, CacheKeyHasher};
use ruff_macros::CacheKey;

#[derive(CacheKey, Hash)]
struct UnitStruct;
#[test]
fn unit_struct_cache_key() {
#[derive(CacheKey, Hash)]
struct UnitStruct;

#[derive(CacheKey, Hash)]
struct NamedFieldsStruct {
a: String,
b: String,
}
let mut key = CacheKeyHasher::new();

#[derive(CacheKey, Hash)]
struct UnnamedFieldsStruct(String, String);
UnitStruct.cache_key(&mut key);

#[derive(CacheKey, Hash)]
enum Enum {
Unit,
UnnamedFields(String, String),
NamedFields { a: String, b: String },
let mut hash = CacheKeyHasher::new();
UnitStruct.hash(&mut hash);

assert_eq!(hash.finish(), key.finish());
}

#[test]
fn unit_struct_cache_key() {
fn named_field_struct() {
#[derive(CacheKey, Hash)]
struct NamedFieldsStruct {
a: String,
b: String,
}

let mut key = CacheKeyHasher::new();

UnitStruct.cache_key(&mut key);
let named_fields = NamedFieldsStruct {
a: "Hello".into(),
b: "World".into(),
};

named_fields.cache_key(&mut key);

let mut hash = CacheKeyHasher::new();
UnitStruct.hash(&mut hash);
named_fields.hash(&mut hash);

assert_eq!(hash.finish(), key.finish());
}

#[test]
fn named_field_struct() {
fn struct_ignored_fields() {
#[derive(CacheKey)]
struct NamedFieldsStruct {
a: String,
#[cache_key(ignore)]
#[allow(unused)]
b: String,
}

impl Hash for NamedFieldsStruct {
fn hash<H: Hasher>(&self, state: &mut H) {
self.a.hash(state);
}
}

let mut key = CacheKeyHasher::new();

let named_fields = NamedFieldsStruct {
Expand All @@ -53,6 +74,9 @@ fn named_field_struct() {

#[test]
fn unnamed_field_struct() {
#[derive(CacheKey, Hash)]
struct UnnamedFieldsStruct(String, String);

let mut key = CacheKeyHasher::new();

let unnamed_fields = UnnamedFieldsStruct("Hello".into(), "World".into());
Expand All @@ -65,6 +89,13 @@ fn unnamed_field_struct() {
assert_eq!(hash.finish(), key.finish());
}

#[derive(CacheKey, Hash)]
enum Enum {
Unit,
UnnamedFields(String, String),
NamedFields { a: String, b: String },
}

#[test]
fn enum_unit_variant() {
let mut key = CacheKeyHasher::new();
Expand Down
39 changes: 18 additions & 21 deletions crates/ruff_cli/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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};
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -651,9 +651,8 @@ mod tests {
}

struct TestCache {
cache_dir: PathBuf,
package_root: PathBuf,
settings: AllSettings,
settings: Settings,
}

impl TestCache {
Expand All @@ -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,
}
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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);
}
}
}
21 changes: 7 additions & 14 deletions crates/ruff_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand All @@ -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>>()
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_cli/src/commands/check_stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_cli/src/commands/format_stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 6 additions & 7 deletions crates/ruff_cli/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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()
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_dev/src/format_dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn ruff_check_paths(
cli.stdin_filename.as_deref(),
)?;
// We don't want to format pyproject.toml
pyproject_config.settings.lib.include = FilePatternSet::try_from_vec(vec![
pyproject_config.settings.include = FilePatternSet::try_from_vec(vec![
FilePattern::Builtin("*.py"),
FilePattern::Builtin("*.pyi"),
])
Expand Down
Loading
Loading