You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Replace the interactive "Accept [Y, n]?" prompt with a deterministic
`--add <name>` flag that bootstraps new regression tests
non-interactively. Tests without expected output are now skipped
(instead of prompting),with a clear message directing users to `--add`.
Also adds `--dry-run` to preview what would happen, `--test-filter` as a
proper named flag (`-t`), inline regression diff output with `-v`, and
auto-detection of the pgrx extension crate in virtual workspaces
(eliminating the need for `--package` when there's only one extension).
Updates README to match the new workflow.
One behavior change here is that `--auto` no longer generates expected
output for tests that don't have them. They're skipped now. User will
need to explicitly `--add` new tests now.
---
This may be pgrx' first PR fully written by an AI bot in order to make
the AI bot's life easier when working with `cargo pgrx` in general and
`cargo pgrx regress` in particular.
I think the changes are good for humans too. The things being addressed
here have tripped me up numerous times too.
---------
Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
pgrx supports a regression test system very similar to the one prescribed by Postgres' `pg_regress` tool. In fact, pgrx uses `pg_regress` to run the regression tests.
531
531
532
-
`cargo pgrx regress` is used to run the regression tests. It has a number of options similar to `cargo pgrx test`:
[PG_VERSION] Do you want to run against pg13, pg14, pg15, pg16, pg17? [env: PG_VERSION=]
542
-
[TEST_FILTER] If specified, only run tests containing this string in their names
543
-
544
-
Options:
545
-
--dbname <DBNAME> If specified, use this database name instead of the auto-generated version of `$extname_regress`
546
-
--resetdb Recreate the test database, even if it already exists
547
-
-v, --verbose... Enable info logs, -vv for debug, -vvv for trace
548
-
-p, --package <PACKAGE> Package to build (see `cargo help pkgid`)
549
-
--manifest-path <MANIFEST_PATH> Path to Cargo.toml
550
-
-r, --release compile for release mode (default is debug)
551
-
--profile <PROFILE> Specific profile to use (conflicts with `--release`)
552
-
-n, --no-schema Don't regenerate the schema
553
-
--runas <USER> Use `sudo` to initialize and run the Postgres test instance as this system user
554
-
--pgdata <DIR> Initialize the test database cluster here, instead of the default location. If used with `--runas`, then it must be writable by the user
555
-
--all-features Activate all available features
556
-
--no-default-features Do not activate the `default` feature
557
-
-F, --features <FEATURES> Space-separated list of features to activate
558
-
--postgresql-conf <POSTGRESQL_CONF> Custom `postgresql.conf` settings in the form of `key=value`, ie `log_min_messages=debug1`
559
-
-a, --auto Automatically accept output for new tests *and* overwrite output for existing-but-failed tests
560
-
-h, --help Print help
561
-
-V, --version Print version
562
-
```
532
+
`cargo pgrx regress` is used to run the regression tests. It has a number of options similar to `cargo pgrx test`.
|`--resetdb`| Drop and recreate the test database (recommended for reproducible runs) |
539
+
|`--add <name>`| Bootstrap a new test — implies `--resetdb`, runs `setup.sql`, promotes output to `expected/`|
540
+
|`-t` / `--test-filter <string>`| Only run tests whose names contain this string |
541
+
|`-a` / `--auto`| Overwrite expected output for failed tests with actual output |
542
+
|`-v` / `--verbose`| Print regression diffs to stderr on failure |
543
+
|`--dry-run`| Print what would happen without doing it |
544
+
|`--repeat <N>`| Run the entire configuration N times (default: 1) |
545
+
|`-p` / `--package <name>`| Package to build (auto-detected in workspaces with a single pgrx extension) |
546
+
|`[PG_VERSION]`| Postgres version (e.g., `pg18`). Optional — defaults to Cargo.toml's default feature |
563
547
564
548
Regression tests are split into `*.sql` files and `*.out` files. The files themselves are organized into separate directories rooted at `./tests/pg_regress`.
565
549
@@ -590,75 +574,51 @@ $ tree
590
574
591
575
`setup.sql` is a special test in that it's run first, by itself, whenever the test database is first created, or reset using the `--resetdb` argument.
592
576
593
-
When creating a new test, first make the `.sql` file in `./tests/pg_regress/sql/` and then run `cargo pgrx regress`. pgrx will detect that the file is new and interactively prompt you to add its output, automatically adding it to git (if the directory is managed by git).
594
-
595
-
For example,
577
+
When creating a new test, first make the `.sql` file in `./tests/pg_regress/sql/` and then use `--add` to bootstrap it:
1. Drop and recreate the test database (`--add` implies `--resetdb`)
586
+
2. Run `setup.sql` to establish schema/data
587
+
3. Run the new test
588
+
4. Copy its output to `./tests/pg_regress/expected/example.out`
589
+
5.`git add` the new expected output file
625
590
626
-
test `example` generated the above output:
627
-
Accept [Y, n]?
591
+
After bootstrapping, verify the test passes by running the full suite:
592
+
593
+
```console
594
+
$ cargo pgrx regress --resetdb
628
595
```
629
596
630
-
Typing `Y` (or just pressing return) will copy the test output to the proper location, `./tests/pg_regress/expected/example.out` and then run the entire test suite:
597
+
Tests without expected output are **skipped** by default during normal runs. If you use `--test-filter` to target a test that hasn't been bootstrapped yet, you'll get a clear error directing you to use `--add` first.
598
+
599
+
#### Updating expected output after code changes
600
+
601
+
When code changes alter query output or EXPLAIN plans, use `--auto` to accept the new output:
631
602
632
603
```console
633
-
...
634
-
test `example` generated the above output:
635
-
Accept [Y, n]? y
636
-
Copying test output to ~/_work/pgrx/tests/pgrx-examples/range/tests/pg_regress/expected/example.out
============== running regression test queries ==============
640
-
test example ... ok 6 ms
641
-
test make_range ... ok 18 ms
642
-
test store_ranges ... ok 20 ms
643
-
644
-
=====================
645
-
All 3 tests passed.
646
-
=====================
604
+
$ cargo pgrx regress --auto --resetdb
647
605
```
648
606
649
-
Alternatively, you can run `cargo pgrx regress --auto` (or `-a`) to **automatically** accept the output generated by a new test.
607
+
`--auto` overwrites expected output for **failed** tests with their actual output. Review the changes with `git diff` to verify they match the intended behavioral change. **Never manually edit `.out` files** — always let `--auto` generate them.
650
608
651
-
`--auto` will **also** copy the output of **failed** tests to the `./tests/pg_regress/expected/` directory, overwriting the existing expected test output. This is an automated version of blindly accepting different test output as the new, expected output.
609
+
When tests fail, the path to the `regression.diffs` file is always shown. Use `-v` to also print the full diff content inline.
652
610
653
611
### Things to Know
654
612
655
-
-`setup.sql` is only executed when tests are run for the first time, or the `--resetdb` argument is used
613
+
-`setup.sql` is only executed when tests are run for the first time, or the `--resetdb` argument is used. This includes when running a single test with `--resetdb` — `setup.sql` will always run first to establish the database schema and data before the filtered test executes.
656
614
- The point of `setup.sql` is to perform some heavy-weight database object creation/data-loading _only_ when the test regression database is created.
657
615
- tests are executed in alphabetical order
616
+
-`.sql` files without a corresponding `expected/*.out` file are **skipped** during normal runs — use `--add <name>` to bootstrap new tests
658
617
- pgrx creates a database named `$extname_regress` unless `--dbname` is used
659
618
- Postgres' documentation for `pg_regress`[begins here](https://www.postgresql.org/docs/current/regress.html). While pgrx does not support every knob and dial, its organization is largely compatible (PRs welcome to enhance features)
660
-
- to regenerate the expected test output, delete the `./tests/pg_regress/expected/TEST_NAME.out` file and run `cargo pgrx regress`. You'll be prompted to accept the new output and it'll automatically be run through `git add`
661
-
-`pg_regress` uses `psql` to run each test and literally diffs the output against the expected output file. pgrx does two things to help eliminate noise in the test output. The first is it sets `client_min_messages=warning` when starting the Postgres instance and it also passes `-v VERBOSITY=terse` through to `psql`.
619
+
- to regenerate the expected test output, delete the `./tests/pg_regress/expected/TEST_NAME.out` file and re-add it with `cargo pgrx regress --add TEST_NAME`
620
+
-`pg_regress` uses `psql` to run each test and literally diffs the output against the expected output file. pgrx does two things to help eliminate noise in the test output. The first is it sets `client_min_messages=warning` when starting the Postgres instance and it also passes `-v VERBOSITY=terse` through to `psql`.
621
+
- In a workspace with a single pgrx extension crate, `--package` is auto-detected — no need to specify it manually
662
622
663
623
### Diffing `psql` Output?
664
624
@@ -682,56 +642,10 @@ An example of an unkind test might be:
============== running regression test queries ==============
721
-
test bad ... FAILED 5 ms
722
-
test make_range ... ok 18 ms
723
-
test store_ranges ... ok 19 ms
724
-
725
-
======================
726
-
1 of 3 tests failed.
727
-
======================
728
-
729
-
The differences that caused some tests to fail can be viewed in the
730
-
file " ~/_work/pgrx/tests/pgrx-examples/range/tests/pg_regress/regression.diffs". A copy of the test summary that you see
731
-
above is saved in the file " ~/_work/pgrx/tests/pgrx-examples/range/tests/pg_regress/regression.out".
645
+
$ cargo pgrx regress --add bad
732
646
```
733
647
734
-
And you see the `bad` test immediately failed! To see how it failed, look at the `./tests/pg_regress/regression.diffs` file:
648
+
Then on a second run (without `--resetdb`), the `CREATE TABLE` will fail because the table already exists. You'd see the regressiondiffs printed inline:
0 commit comments