Skip to content

Commit 7c4613b

Browse files
committed
Add tests and CLAUDE.md
1 parent 75add8f commit 7c4613b

10 files changed

Lines changed: 218 additions & 22 deletions

File tree

CLAUDE.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# dprint-plugin-sql
2+
3+
Wrapper around [sqlformat-rs](https://github.com/shssoichiro/sqlformat-rs) for use as a dprint formatting plugin.
4+
5+
## Commands
6+
7+
- `cargo test` — run all tests
8+
- `cargo build --target wasm32-unknown-unknown --features wasm --release` — build the wasm plugin
9+
- `cd deployment/npm && node setup.js && npm test` — test the npm package (requires wasm build first)
10+
11+
## Architecture
12+
13+
- `src/format_text.rs` — core formatting logic, delegates to `sqlformat::format()`
14+
- `src/wasm_plugin.rs` — dprint wasm plugin interface (compiled only for wasm32 target with `wasm` feature)
15+
- `src/configuration/` — config types, resolution, and builder
16+
- `deployment/npm/` — npm package that ships `plugin.wasm`
17+
- `deployment/schema.json` — JSON schema for plugin configuration
18+
- `scripts/update.ts` — Deno script that checks crates.io for new sqlformat versions and auto-publishes
19+
- `scripts/generateReleaseNotes.ts` — generates changelog for GitHub releases
20+
21+
## Adding a new config option
22+
23+
When sqlformat-rs adds new options:
24+
25+
1. Add the field to `Configuration` in `src/configuration/configuration.rs` (add enums with `generate_str_to_from!` if needed)
26+
2. Add resolution in `src/configuration/resolve_config.rs` using `get_value()`
27+
3. Add a builder method in `src/configuration/builder.rs` and update the `check_all_values_set` test
28+
4. Wire it through in `src/format_text.rs` when constructing `FormatOptions`
29+
5. Add the property to `deployment/schema.json`
30+
6. Add a spec test file in `tests/specs/Config/`
31+
32+
## Test specs
33+
34+
Spec files live in `tests/specs/` and use the dprint-development format:
35+
36+
- `== message ==` starts a test case
37+
- `[expect]` separates input from expected output
38+
- `~~ key: value, key: value ~~` at the top of a file sets config for all specs in that file
39+
- Each config variation needs its own file since config is file-level
40+
41+
## Releases
42+
43+
- Manual: trigger the `release` workflow with patch/minor
44+
- Automatic: `check_updates` workflow runs daily, detects new sqlformat versions, and auto-publishes
45+
- On tag push: CI creates a GitHub release with `plugin.wasm`, and `publish_npm` publishes to npm

tests/specs/Basic/Basic_All.txt

Lines changed: 84 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,84 @@
1-
== should format basic ==
2-
SELECT * FROM dbo.Test WHERE test = 'asdf'
3-
GO
4-
5-
6-
7-
update dbo.Test Set test=1
8-
GO
9-
10-
[expect]
11-
SELECT
12-
*
13-
FROM
14-
dbo.Test
15-
WHERE
16-
test = 'asdf'
17-
GO
18-
update
19-
dbo.Test
20-
Set
21-
test = 1
22-
GO
1+
== should format basic ==
2+
SELECT * FROM dbo.Test WHERE test = 'asdf'
3+
GO
4+
5+
6+
7+
update dbo.Test Set test=1
8+
GO
9+
10+
[expect]
11+
SELECT
12+
*
13+
FROM
14+
dbo.Test
15+
WHERE
16+
test = 'asdf'
17+
GO
18+
update
19+
dbo.Test
20+
Set
21+
test = 1
22+
GO
23+
24+
== should format subquery ==
25+
SELECT * FROM (SELECT id, name FROM users WHERE active = 1) AS t WHERE t.id > 10
26+
27+
[expect]
28+
SELECT
29+
*
30+
FROM
31+
(
32+
SELECT
33+
id,
34+
name
35+
FROM
36+
users
37+
WHERE
38+
active = 1
39+
) AS t
40+
WHERE
41+
t.id > 10
42+
43+
== should format insert ==
44+
INSERT INTO users (id, name, email) VALUES (1, 'test', 'test@example.com')
45+
46+
[expect]
47+
INSERT INTO
48+
users (id, name, email)
49+
VALUES
50+
(1, 'test', 'test@example.com')
51+
52+
== should format join ==
53+
SELECT u.name, o.total FROM users u INNER JOIN orders o ON u.id = o.user_id WHERE o.total > 100
54+
55+
[expect]
56+
SELECT
57+
u.name,
58+
o.total
59+
FROM
60+
users u
61+
INNER JOIN orders o ON u.id = o.user_id
62+
WHERE
63+
o.total > 100
64+
65+
== should format create table ==
66+
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) UNIQUE)
67+
68+
[expect]
69+
CREATE TABLE users (
70+
id INT PRIMARY KEY,
71+
name VARCHAR(255) NOT NULL,
72+
email VARCHAR(255) UNIQUE
73+
)
74+
75+
== should format multiple queries ==
76+
SELECT 1; SELECT 2; SELECT 3
77+
78+
[expect]
79+
SELECT
80+
1;
81+
SELECT
82+
2;
83+
SELECT
84+
3

tests/specs/Config/Casing.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
== should preserve casing by default ==
2+
SELECT * FROM users WHERE id = 1
3+
4+
[expect]
5+
SELECT
6+
*
7+
FROM
8+
users
9+
WHERE
10+
id = 1

tests/specs/Config/CasingLower.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
~~ casing: lower ~~
2+
== should lowercase with casing lower ==
3+
SELECT * FROM users WHERE id = 1
4+
5+
[expect]
6+
select
7+
*
8+
from
9+
users
10+
where
11+
id = 1

tests/specs/Config/CasingUpper.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
~~ casing: upper ~~
2+
== should uppercase with casing upper ==
3+
select * from users where id = 1
4+
5+
[expect]
6+
SELECT
7+
*
8+
FROM
9+
users
10+
WHERE
11+
id = 1

tests/specs/Config/Indent.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
~~ useTabs: true ~~
2+
== should use tabs ==
3+
SELECT * FROM users WHERE id = 1
4+
5+
[expect]
6+
SELECT
7+
*
8+
FROM
9+
users
10+
WHERE
11+
id = 1

tests/specs/Config/IndentWidth.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
~~ indentWidth: 4 ~~
2+
== should use 4 space indent ==
3+
SELECT * FROM users WHERE id = 1
4+
5+
[expect]
6+
SELECT
7+
*
8+
FROM
9+
users
10+
WHERE
11+
id = 1
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
~~ joinsAsTopLevel: true ~~
2+
== should treat joins as top level ==
3+
SELECT u.name, o.total FROM users u INNER JOIN orders o ON u.id = o.user_id LEFT JOIN payments p ON o.id = p.order_id
4+
5+
[expect]
6+
SELECT
7+
u.name,
8+
o.total
9+
FROM
10+
users u
11+
INNER JOIN
12+
orders o ON u.id = o.user_id
13+
LEFT JOIN
14+
payments p ON o.id = p.order_id
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
~~ linesBetweenQueries: 2 ~~
2+
== should use 2 lines between queries ==
3+
SELECT 1; SELECT 2
4+
5+
[expect]
6+
SELECT
7+
1;
8+
9+
SELECT
10+
2
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
~~ uppercase: true ~~
2+
== should support legacy uppercase boolean ==
3+
select * from users where id = 1
4+
5+
[expect]
6+
SELECT
7+
*
8+
FROM
9+
users
10+
WHERE
11+
id = 1

0 commit comments

Comments
 (0)