Skip to content

Commit

Permalink
Add semi_noninvasive_parents test
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Jun 2, 2023
1 parent 9ba87b2 commit 17ee3af
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 57 deletions.
4 changes: 2 additions & 2 deletions core/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct Opts<Identifier: Clone + Send + Sync + ValueEnum + 'static> {
#[clap(long, help = "Dump removal candidates and exit (for debugging)")]
dump_candidates: bool,
#[clap(long, help = "Assume testing framework is <FRAMEWORK>")]
framework: Option<framework::Auto<Identifier>>,
framework: Option<framework::auto::Auto<Identifier>>,
#[clap(long, help = "Do not perform dry runs")]
no_dry_run: bool,
#[clap(long, help = "Do not output to an sqlite database")]
Expand Down Expand Up @@ -64,7 +64,7 @@ pub struct Opts<Identifier: Clone + Send + Sync + ValueEnum + 'static> {
}

impl<Identifier: Clone + Send + Sync + ValueEnum> From<Opts<Identifier>>
for (Necessist, framework::Auto<Identifier>)
for (Necessist, framework::auto::Auto<Identifier>)
{
fn from(opts: Opts<Identifier>) -> Self {
let Opts {
Expand Down
6 changes: 3 additions & 3 deletions core/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub struct Config {
// to parameterize every function that takes a `Necessist` as an argument.
pub fn necessist<Identifier: Applicable + Display + IntoEnumIterator + ToImplementation>(
opts: &Necessist,
framework: framework::Auto<Identifier>,
framework: framework::auto::Auto<Identifier>,
) -> Result<()> {
let mut opts = opts.clone();

Expand Down Expand Up @@ -158,7 +158,7 @@ pub fn necessist<Identifier: Applicable + Display + IntoEnumIterator + ToImpleme
#[allow(clippy::type_complexity)]
fn prepare<Identifier: Applicable + Display + IntoEnumIterator + ToImplementation>(
context: &LightContext,
framework: framework::Auto<Identifier>,
framework: framework::auto::Auto<Identifier>,
) -> Result<
Option<(
Option<sqlite::Sqlite>,
Expand Down Expand Up @@ -387,7 +387,7 @@ fn dump(context: &LightContext, removals: &[Removal]) {

fn find_framework<Identifier: Applicable + Display + IntoEnumIterator + ToImplementation>(
context: &LightContext,
identifier: framework::Auto<Identifier>,
identifier: framework::auto::Auto<Identifier>,
) -> Result<Box<dyn framework::Interface>> {
let implementation = identifier.to_implementation(context)?;

Expand Down
14 changes: 7 additions & 7 deletions core/src/framework/auto.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Applicable, Interface, ToImplementation, Union};
use super::{union, Applicable, Interface, ToImplementation};
use crate::LightContext;
use anyhow::{ensure, Result};
use std::fmt::Display;
Expand All @@ -14,11 +14,11 @@ enum Singleton {
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct Auto<T>(Union<Singleton, T>);
pub struct Auto<T>(union::Union<Singleton, T>);

impl<T> Default for Auto<T> {
fn default() -> Self {
Self(Union::Left(Singleton::Auto))
Self(union::Union::Left(Singleton::Auto))
}
}

Expand All @@ -28,7 +28,7 @@ where
{
fn to_implementation(&self, context: &LightContext) -> Result<Option<Box<dyn Interface>>> {
match &self.0 {
Union::Left(_) => {
union::Union::Left(_) => {
let unflattened_frameworks = T::iter()
.map(|framework| {
if framework.applicable(context)? {
Expand Down Expand Up @@ -61,7 +61,7 @@ where
Ok(None)
}
}
Union::Right(framework) => framework.to_implementation(context),
union::Union::Right(framework) => framework.to_implementation(context),
}
}
}
Expand All @@ -73,7 +73,7 @@ where
{
fn value_variants<'a>() -> &'a [Self] {
Box::leak(
Union::<Singleton, T>::value_variants()
union::Union::<Singleton, T>::value_variants()
.iter()
.cloned()
.map(Self)
Expand All @@ -87,6 +87,6 @@ where
}

fn from_str(input: &str, ignore_case: bool) -> Result<Self, String> {
Union::<Singleton, T>::from_str(input, ignore_case).map(Self)
union::Union::<Singleton, T>::from_str(input, ignore_case).map(Self)
}
}
11 changes: 4 additions & 7 deletions core/src/framework/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ use heck::ToKebabCase;
use std::{any::type_name, path::Path};
use subprocess::{Exec, Popen};

mod auto;
pub use auto::Auto;
pub mod auto;

mod empty;
pub use empty::Empty;
pub mod empty;

mod union;
pub use union::Union;
pub mod union;

#[allow(dead_code)]
type AutoUnion<T, U> = Auto<Union<T, U>>;
type AutoUnion<T, U> = auto::Auto<union::Union<T, U>>;

pub type Postprocess = dyn Fn(&LightContext, Popen) -> Result<bool>;

Expand Down
13 changes: 4 additions & 9 deletions core/src/offset_based_rewriter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,26 @@

mod impls;

use impls::LazyRewriter;

#[cfg(feature = "check-rewrites")]
use impls::EagerRewriter;

pub trait Interface {
fn contents(self) -> String;
fn rewrite(&mut self, start: usize, end: usize, replacement: &str) -> String;
}

#[derive(Debug)]
pub struct OffsetBasedRewriter<'original> {
lazy: LazyRewriter<'original>,
lazy: impls::LazyRewriter<'original>,

#[cfg(feature = "check-rewrites")]
eager: EagerRewriter,
eager: impls::EagerRewriter,
}

impl<'original> OffsetBasedRewriter<'original> {
pub fn new(original: &'original str) -> Self {
Self {
lazy: LazyRewriter::new(original),
lazy: impls::LazyRewriter::new(original),

#[cfg(feature = "check-rewrites")]
eager: EagerRewriter::new(original),
eager: impls::EagerRewriter::new(original),
}
}
}
Expand Down
13 changes: 4 additions & 9 deletions core/src/offset_calculator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,25 @@ use proc_macro2::LineColumn;

mod impls;

use impls::CachingOffsetCalculator;

#[cfg(feature = "check-offsets")]
use impls::StatelessOffsetCalculator;

pub trait Interface {
fn offset_from_line_column(&mut self, line_column: LineColumn) -> (usize, bool);
}

#[derive(Debug)]
pub struct OffsetCalculator<'original> {
caching: CachingOffsetCalculator<'original>,
caching: impls::CachingOffsetCalculator<'original>,

#[cfg(feature = "check-offsets")]
stateless: StatelessOffsetCalculator<'original>,
stateless: impls::StatelessOffsetCalculator<'original>,
}

impl<'original> OffsetCalculator<'original> {
pub fn new(original: &'original str) -> Self {
Self {
caching: CachingOffsetCalculator::new(original),
caching: impls::CachingOffsetCalculator::new(original),

#[cfg(feature = "check-offsets")]
stateless: StatelessOffsetCalculator::new(original),
stateless: impls::StatelessOffsetCalculator::new(original),
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions frameworks/src/foundry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{collections::BTreeMap, fs::read_to_string, path::Path, process::Comman
use walkdir::WalkDir;

mod visitor;
use visitor::visit;

#[derive(Debug)]
pub struct Foundry {
Expand Down Expand Up @@ -52,7 +51,7 @@ impl Low for Foundry {
util::strip_prefix(test_file, context.root).unwrap()
)
})?;
let spans_visited = visit(
let spans_visited = visitor::visit(
self,
context.root.clone(),
test_file,
Expand Down
4 changes: 2 additions & 2 deletions frameworks/src/golang/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use tree_sitter::Parser;
use walkdir::WalkDir;

mod visitor;
use visitor::visit;

#[derive(Debug)]
pub struct Golang {
Expand Down Expand Up @@ -55,7 +54,8 @@ impl Low for Golang {
util::strip_prefix(test_file, context.root).unwrap()
)
})?;
let spans_visited = visit(self, context.root.clone(), test_file, &text, &tree)?;
let spans_visited =
visitor::visit(self, context.root.clone(), test_file, &text, &tree)?;
spans.extend(spans_visited);
Ok(())
};
Expand Down
3 changes: 1 addition & 2 deletions frameworks/src/hardhat_ts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use swc_core::{
use walkdir::WalkDir;

mod visitor;
use visitor::visit;

#[derive(Debug, Eq, PartialEq)]
enum ItMessageState {
Expand Down Expand Up @@ -113,7 +112,7 @@ impl High for HardhatTs {
util::strip_prefix(test_file, context.root).unwrap()
)
})?;
let spans_visited = visit(
let spans_visited = visitor::visit(
config,
self,
source_map,
Expand Down
12 changes: 6 additions & 6 deletions frameworks/src/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ use syn::parse_file;
use walkdir::WalkDir;

mod parsing;
use parsing::{cached_test_file_fs_module_path, cached_test_file_package, Parsing};

mod try_insert;
use try_insert::TryInsert;
#[allow(unused_imports)]
use try_insert::{self as _, TryInsert};

mod visitor;
use visitor::visit;

#[derive(Debug)]
pub struct Rust {
Expand Down Expand Up @@ -59,7 +58,7 @@ impl Low for Rust {
) -> Result<Vec<Span>> {
check_config(context, config)?;

let mut parsing = Parsing::default();
let mut parsing = parsing::Parsing::default();
let mut spans = Vec::new();

#[cfg_attr(
Expand All @@ -77,7 +76,8 @@ impl Low for Rust {
util::strip_prefix(test_file, context.root).unwrap()
)
})?;
let spans_visited = visit(context, config, self, &mut parsing, test_file, &file)?;
let spans_visited =
visitor::visit(context, config, self, &mut parsing, test_file, &file)?;
spans.extend(spans_visited);
Ok(())
};
Expand Down Expand Up @@ -173,7 +173,7 @@ impl Rust {
self.test_file_flags_cache
.entry(test_file.to_path_buf())
.or_try_insert_with(|| {
let package = cached_test_file_package(test_file_package_map, test_file)?;
let package = parsing::cached_test_file_package(test_file_package_map, test_file)?;

let mut flags = vec![
"--manifest-path".to_owned(),
Expand Down
10 changes: 5 additions & 5 deletions frameworks/src/rust/visitor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{cached_test_file_fs_module_path, Parsing, Rust};
use super::{parsing, Rust};
use anyhow::{Error, Result};
use necessist_core::{
warn, Config, LightContext, SourceFile, Span, ToInternalSpan, WarnFlags, Warning,
Expand All @@ -23,7 +23,7 @@ pub(super) fn visit(
context: &LightContext,
config: &Config,
framework: &mut Rust,
parsing: &mut Parsing,
parsing: &mut parsing::Parsing,
test_file: &Path,
file: &File,
) -> Result<Vec<Span>> {
Expand All @@ -40,7 +40,7 @@ struct Visitor<'ast, 'context, 'config, 'framework, 'parsing> {
context: &'context LightContext<'context>,
config: &'config Config,
framework: &'framework mut Rust,
parsing: &'parsing mut Parsing,
parsing: &'parsing mut parsing::Parsing,
source_file: SourceFile,
module_path: Vec<&'ast Ident>,
test_ident: Option<&'ast Ident>,
Expand Down Expand Up @@ -150,7 +150,7 @@ where
context: &'context LightContext,
config: &'config Config,
framework: &'framework mut Rust,
parsing: &'parsing mut Parsing,
parsing: &'parsing mut parsing::Parsing,
test_file: &Path,
) -> Self {
Self {
Expand Down Expand Up @@ -201,7 +201,7 @@ where
}

fn test_path(&mut self, span: &Span, ident: &Ident) -> Result<Vec<String>> {
let mut test_path = cached_test_file_fs_module_path(
let mut test_path = parsing::cached_test_file_fs_module_path(
&mut self.parsing.test_file_fs_module_path_cache,
&mut self.parsing.test_file_package_cache,
&span.source_file,
Expand Down
2 changes: 1 addition & 1 deletion necessist/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use anyhow::Result;
use clap::Parser;
use log::debug;
use necessist_core::{cli, framework::Auto, necessist, Necessist};
use necessist_core::{cli, framework::auto::Auto, necessist, Necessist};
use necessist_frameworks::Identifier;
use std::{
env::{args, var},
Expand Down
40 changes: 38 additions & 2 deletions necessist/tests/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ fn modules() {
}
}

/// `noninvasive_siblings` helps to expose circular module dependencies. See the [`modules`] test
/// within this file.
/// `noninvasive_siblings` and `semi_noninvasive_parents` help to expose circular module
/// dependencies. See the [`modules`] test within this file.
#[test]
fn noninvasive_siblings() {
let re = Regex::new(r"use super::\{([^}]|\}[^;])*::").unwrap();
Expand All @@ -200,12 +200,48 @@ fn noninvasive_siblings() {
}

let contents = read_to_string(path).unwrap();

if contents.contains("use super::{") {
assert!(!re.is_match(&contents), "failed for {path:?}");
}
}
}

#[test]
fn semi_noninvasive_parents() {
let re = Regex::new(r"mod ([^;]*);").unwrap();

for entry in WalkDir::new(Path::new(env!("CARGO_MANIFEST_DIR")).join(".."))
.into_iter()
.filter_entry(|entry| entry.path().file_name() != Some(OsStr::new("target")))
{
let entry = entry.unwrap();
let path = entry.path();

if path.extension() != Some(OsStr::new("rs")) {
continue;
}

// smoelius: `lib.rs` files get a pass.
if path.file_name() == Some(OsStr::new("lib.rs")) {
continue;
}

let contents = read_to_string(path).unwrap();

for captures in re.captures_iter(&contents) {
assert_eq!(2, captures.len());
let name = &captures[1];
let pat = format!("use {name}::");
let pat_self = pat.clone() + "{self";
assert!(
!contents.contains(&pat) || contents.contains(&pat_self),
"{path:?} contains `{pat}` but not `{pat_self}`"
);
}
}
}

#[test]
fn prettier() {
let tempdir = tempdir().unwrap();
Expand Down

0 comments on commit 17ee3af

Please sign in to comment.