rrubocop 0.8.15

10x faster RuboCop drop-in. Same output format and configs, without maintaining RuboCop gems.
//! Detect a Rails application root when no `.rubocop.yml` is present, so
//! rrubocop matches what `rails new` (Rails 7.2+) generates by default:
//! `inherit_gem: rubocop-rails-omakase: rubocop.yml`. Detection is purely
//! filesystem-based — it works even when `Gemfile.lock` has no `rails` or
//! `rubocop` entry (e.g. no lockfile at all).

use std::path::Path;

/// Synthetic project config used when a Rails app has no `.rubocop.yml`.
pub(crate) const OMAKASE_DEFAULT_YAML: &str =
    "inherit_gem:\n  rubocop-rails-omakase:\n    - rubocop.yml\n";

/// Whether `dir` looks like a Rails application root: presence of
/// `config/application.rb` or `bin/rails`, both generated by `rails new`
/// and present regardless of what's locked in `Gemfile.lock`.
pub(crate) fn is_rails_app(dir: &Path) -> bool {
    dir.join("config/application.rb").is_file() || dir.join("bin/rails").is_file()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn detects_application_rb() {
        let dir = tempfile::tempdir().unwrap();
        assert!(!is_rails_app(dir.path()));
        std::fs::create_dir_all(dir.path().join("config")).unwrap();
        std::fs::write(dir.path().join("config/application.rb"), "").unwrap();
        assert!(is_rails_app(dir.path()));
    }

    #[test]
    fn detects_bin_rails() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(dir.path().join("bin")).unwrap();
        std::fs::write(dir.path().join("bin/rails"), "").unwrap();
        assert!(is_rails_app(dir.path()));
    }

    #[test]
    fn plain_ruby_project_is_not_rails() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("Gemfile"), "gem \"rake\"\n").unwrap();
        assert!(!is_rails_app(dir.path()));
    }
}