Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(core): use Rayon in snapshot tests (#1237)

Elijah Potterd27be05cd56d
diff --git a/Cargo.lock b/Cargo.lock
index e0820b3f9..ac90f87b6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -815,6 +815,7 @@ dependencies = [
"quickcheck",
"quickcheck_macros",
"rand",
+ "rayon",
"serde",
"serde_json",
"smallvec",
diff --git a/Cargo.toml b/Cargo.toml
index a9a573eae..03a4b76ad 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -2,6 +2,10 @@
members = [ "harper-cli", "harper-core", "harper-ls", "harper-comments", "harper-wasm", "harper-tree-sitter", "harper-html", "harper-literate-haskell", "harper-typst" , "harper-stats"]
resolver = "2"
+# Comment out the below lines if you plan to use a debugger.
+[profile.test]
+opt-level = 1
+
[profile.release]
codegen-units = 1
lto = true
diff --git a/harper-core/Cargo.toml b/harper-core/Cargo.toml
index a01da8b9d..157e88b97 100644
--- a/harper-core/Cargo.toml
+++ b/harper-core/Cargo.toml
@@ -37,6 +37,7 @@ rand = "0.8.5"
quickcheck = "1.0.3"
quickcheck_macros = "1.0.0"
once_cell = "1.21.3"
+rayon = "1.10.0"
[[bench]]
name = "parse_demo"
diff --git a/harper-core/tests/snapshot.rs b/harper-core/tests/snapshot.rs
index 287b00d16..2ad6178ac 100644
--- a/harper-core/tests/snapshot.rs
+++ b/harper-core/tests/snapshot.rs
@@ -1,4 +1,9 @@
-use std::path::{Path, PathBuf};
+use std::{
+ marker::Sync,
+ path::{Path, PathBuf},
+};
+
+use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
fn get_tests_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests")
@@ -60,20 +65,24 @@ fn get_snapshot_file(text_file: &Path, snapshot_dir: &Path, ext: &str) -> PathBu
pub fn snapshot_all_text_files(
out_dir: &str,
snapshot_ext: &str,
- create_snapshot: impl Copy + Fn(&str) -> String,
+ create_snapshot: impl Copy + Fn(&str) -> String + 'static + Sync,
) {
let snapshot_dir = get_text_dir().join(out_dir);
std::fs::create_dir_all(&snapshot_dir).expect("Failed to create snapshot directory");
- let mut errors = 0;
- for text_file in get_text_files() {
- println!("Processing {}", text_file.display());
- let snapshot_file = get_snapshot_file(&text_file, &snapshot_dir, snapshot_ext);
- if let Err(e) = tag_file(&text_file, &snapshot_file, create_snapshot) {
- eprintln!("Error processing {}: {}", text_file.display(), e);
- errors += 1;
- }
- }
+ let errors: u64 = get_text_files()
+ .par_iter()
+ .map(|text_file| {
+ println!("Processing {}", text_file.display());
+ let snapshot_file = get_snapshot_file(text_file, &snapshot_dir, snapshot_ext);
+ if let Err(e) = tag_file(text_file, &snapshot_file, create_snapshot) {
+ eprintln!("Error processing {}: {}", text_file.display(), e);
+ 1
+ } else {
+ 0
+ }
+ })
+ .sum();
if errors > 0 {
panic!("{} errors occurred while processing files", errors);
diff --git a/packages/web/src/routes/docs/contributors/tests/+page.md b/packages/web/src/routes/docs/contributors/tests/+page.md
new file mode 100644
index 000000000..46d818e39
--- /dev/null
+++ b/packages/web/src/routes/docs/contributors/tests/+page.md
@@ -0,0 +1,22 @@
+---
+title: About Harper's Test Suite
+---
+
+Harper's goal is to deliver top-tier grammar checking fast, without compromising privacy.
+How do we maintain quality while also iterating quickly on our core engine?
+
+As you know, Harper's core engine is written in Rust.
+As a corollary to that, we use Cargo to pull dependencies, build, and test the system.
+While we do take advantage of snapshot and integration tests, we tend to focus our efforts on unit tests.
+
+## Performance
+
+In the interest of maintaining fast iteration cycles, we run our tests with `opt-level = 1`.
+These optimizations are known to cause issues with debuggers.
+If you plan to use one, you may want to comment them out.
+
+@code(../../../../../../../Cargo.toml)
+
+## Other Reading
+
+- [3 Traits of Good Test Suites](https://elijahpotter.dev/articles/3_traits_of_good_test_suites)
diff --git a/packages/web/vite.config.ts b/packages/web/vite.config.ts
index 8c5b48eb9..2e3da388b 100644
--- a/packages/web/vite.config.ts
+++ b/packages/web/vite.config.ts
@@ -153,6 +153,10 @@ export default defineConfig({
title: 'Dictionary',
to: '/docs/contributors/dictionary',
},
+ {
+ title: 'Test Suite',
+ to: '/docs/contributors/tests',
+ },
{
title: 'Author a Rule',
to: '/docs/contributors/author-a-rule',