Skip to content
Draft
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
4 changes: 4 additions & 0 deletions crates/sourceorigin-standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ A repository adopts the standard in four steps:
- `docs-summary`: With a `book.toml`, every Markdown page under its `src` is linked from its `SUMMARY.md`, or included in a page with `{{#include …}}`.
- `docs-images`: Every image in a Markdown file, outside code blocks, has alt text: `![what it shows](…)`, or an `<img>` with a non-empty `alt`.
- `wording`: No Markdown file, outside code, holds as a word a spelling `standard.toml`'s `[wording]` rejects, such as `"Source Origin" = "SourceOrigin"`.
- `migration-header`: Every file matching `standard.toml`'s `migrations` globs, such as `crates/*/migrations/*.sql`, starts with a `--` comment.
<!-- rules:end -->

Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and
Expand All @@ -87,6 +88,9 @@ anyhow-crates = ["my-cli"]
# whole, such as a check on the minimum supported Rust version.
cargo-in-ci = ["cargo +1.85 check --locked --workspace"]

# The database migrations, each of which starts with a `--` comment.
migrations = ["crates/*/migrations/*.sql"]

# Rules the repository departs from, each with its reason: everywhere, or
# only in the files `paths` matches and the crates `crates` names. A
# deviation, or an entry of its scope, that excuses no violation is one.
Expand Down
5 changes: 5 additions & 0 deletions crates/sourceorigin-standard/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ pub(crate) struct Config {
/// The `[wording]` table: each rejected spelling, with its line, and
/// the one to write instead.
pub(crate) wording: Vec<(Listed, String)>,
/// The globs of the database migrations, such as
/// `crates/*/migrations/*.sql`.
pub(crate) migrations: Vec<String>,
}

/// The `[comments]` table: what comments the rules allow.
Expand Down Expand Up @@ -235,6 +238,7 @@ struct File {
anyhow_crates: Vec<Spanned<String>>,
cargo_in_ci: Vec<Spanned<String>>,
wording: BTreeMap<Spanned<String>, String>,
migrations: Vec<String>,
}

/// A deviation as written: its reason alone, for everywhere, or a table
Expand Down Expand Up @@ -365,6 +369,7 @@ impl Config {
comments: file.comments,
anyhow_crates: listed(file.anyhow_crates),
cargo_in_ci: listed(file.cargo_in_ci),
migrations: file.migrations,
wording: file
.wording
.into_iter()
Expand Down
21 changes: 21 additions & 0 deletions crates/sourceorigin-standard/src/rules/migration_header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! `migration-header`: each migration says what it changes, and what
//! happens to the data already there.

use super::{Finding, Repository};

impl Repository {
/// Each migration whose first line isn't a `--` comment.
pub(super) fn migration_header(&self) -> Vec<Finding> {
self.migrations
.iter()
.filter(|migration| !migration.text.trim_start().starts_with("--"))
.map(|migration| Finding {
path: migration.path.clone(),
line: 1,
message: "the migration doesn't start with a `--` comment: say what it changes, \
and what happens to the data already there"
.to_owned(),
})
.collect()
}
}
10 changes: 9 additions & 1 deletion crates/sourceorigin-standard/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod justfile;
mod layout;
mod level;
mod manifest;
mod migration_header;
mod modules;
mod nextest_config;
mod pr_template;
Expand Down Expand Up @@ -262,6 +263,9 @@ rules! {
/// `standard.toml`'s `[wording]` rejects, such as
/// `"Source Origin" = "SourceOrigin"`.
Wording = "wording" => wording,
/// Every file matching `standard.toml`'s `migrations` globs, such as
/// `crates/*/migrations/*.sql`, starts with a `--` comment.
MigrationHeader = "migration-header" => migration_header,
}

/// Why the rules couldn't be checked.
Expand Down Expand Up @@ -470,6 +474,8 @@ struct Repository {
book: Option<TomlFile<Book>>,
/// Every Markdown file.
markdown: Vec<TextFile>,
/// The files `migrations` matches.
migrations: Vec<TextFile>,
}

impl Repository {
Expand All @@ -479,8 +485,8 @@ impl Repository {
let metadata = Metadata::load(Workspace::new(root))?;
let (sources, unparsed) = Source::load_all(root)?;
let root_manifest = TomlFile::<RootManifest>::load_root(root)?;
let config = Config::load(root)?;
Ok(Self {
config: Config::load(root)?,
manifests: TomlFile::load_all(&metadata)?,
lint_levels: LintLevel::all(&root_manifest),
root_manifest,
Expand All @@ -501,6 +507,8 @@ impl Repository {
pr_template: TextFile::load_all(root, &[pr_template::PATH])?.pop(),
book: TomlFile::load(root, docs_summary::BOOK)?,
markdown: TextFile::load_all(root, &["*.md"])?,
migrations: TextFile::load_globs(root, &config.migrations)?,
config,
metadata,
sources,
unparsed,
Expand Down
30 changes: 30 additions & 0 deletions crates/sourceorigin-standard/src/rules/tests/migration_header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use super::{Fixture, INHERITS};
use crate::rules::Rule;

#[test]
fn every_migration_says_what_it_changes() {
let fixture = Fixture::new();
fixture
.add_crate("core", INHERITS)
.write(
"standard.toml",
"migrations = [\"crates/*/migrations/*.sql\"]\n",
)
.write(
"crates/core/migrations/0001_apps.sql",
"-- Adds the apps table; no data exists yet.\nCREATE TABLE apps (id INTEGER);\n",
)
.write(
"crates/core/migrations/0002_names.sql",
"ALTER TABLE apps ADD COLUMN name TEXT;\n",
)
.write("crates/core/src/query.sql", "SELECT 1;\n");
assert_eq!(
fixture.violations(Rule::MigrationHeader),
[
"crates/core/migrations/0002_names.sql:1: the migration doesn't start with a `--` \
comment: say what it changes, and what happens to the data already there \
(migration-header)"
]
);
}
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/src/rules/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod framework;
mod generated_lints;
mod job_timeouts;
mod layout;
mod migration_header;
mod nextest_config;
mod pr_template;
mod recipes;
Expand Down
13 changes: 13 additions & 0 deletions crates/sourceorigin-standard/src/rules/text_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,17 @@ impl TextFile {
})
.collect()
}

/// Every file git tracks or would track under `root` matching any of
/// `globs`, such as `crates/*/migrations/*.sql`; none without globs.
pub(super) fn load_globs(root: &Path, globs: &[String]) -> Result<Vec<Self>, Error> {
if globs.is_empty() {
return Ok(Vec::new());
}
let pathspecs: Vec<String> = globs.iter().map(|glob| format!(":(glob){glob}")).collect();
Self::load_all(
root,
&pathspecs.iter().map(String::as_str).collect::<Vec<_>>(),
)
}
}