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);
}
}
}