On 2019-06-17 18:39, Andres Freund wrote:
> Basically something like:
> 
> \gset SELECT my_encodings_are_compatible() AS compatible
> \if :compatible
> test;
> contents;
> \endif

Cool, that works out quite well.  See attached patch.  I flipped the
logic around to make it \quit if not compatible.  That way the
alternative expected file is shorter and doesn't need to be updated all
the time.  But it gets the job done either way.

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
From 1fd55c0a64b11ea23ab0c3791e21c21879a1b1de Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Sun, 23 Jun 2019 21:40:53 +0200
Subject: [PATCH] Run UTF8-requiring collation tests by default

The tests collate.icu.utf8 and collate.linux.utf8 were previously only
run when explicitly selected via EXTRA_TESTS.  They require a UTF8
database, because the error messages in the expected files refer to
that, and they use some non-ASCII characters in the tests.  Since
users can select any locale and encoding for the regression test run,
it was not possible to include these tests automatically.

To fix, use psql's \if facility to check the server encoding and quit
the tests at the very beginning if a server encoding other than UTF8
is set.  We then need to maintain alternative expected files for these
tests, but they are very tiny and never need to change after this.

These two tests are now run automatically as part of the regression
tests, given an appropriate build environment (Linux or ICU enabled,
respectively).

Discussion: 
https://www.postgresql.org/message-id/flat/052295c2-a2e1-9a21-bd36-8fbff8686cf3%402ndquadrant.com
---
 doc/src/sgml/regress.sgml                          | 8 --------
 src/test/regress/GNUmakefile                       | 8 ++++++++
 src/test/regress/expected/collate.icu.utf8.out     | 5 +++++
 src/test/regress/expected/collate.icu.utf8_1.out   | 7 +++++++
 src/test/regress/expected/collate.linux.utf8.out   | 4 ++++
 src/test/regress/expected/collate.linux.utf8_1.out | 8 ++++++++
 src/test/regress/sql/collate.icu.utf8.sql          | 6 ++++++
 src/test/regress/sql/collate.linux.utf8.sql        | 5 +++++
 8 files changed, 43 insertions(+), 8 deletions(-)
 create mode 100644 src/test/regress/expected/collate.icu.utf8_1.out
 create mode 100644 src/test/regress/expected/collate.linux.utf8_1.out

diff --git a/doc/src/sgml/regress.sgml b/doc/src/sgml/regress.sgml
index 448e2d19f7..101dc659e4 100644
--- a/doc/src/sgml/regress.sgml
+++ b/doc/src/sgml/regress.sgml
@@ -316,14 +316,6 @@ <title>Extra Tests</title>
 <screen>
 make check EXTRA_TESTS=numeric_big
 </screen>
-    To run the collation tests:
-<screen>
-make check EXTRA_TESTS='collate.linux.utf8 collate.icu.utf8' LANG=en_US.utf8
-</screen>
-    The <literal>collate.linux.utf8</literal> test works only on Linux/glibc
-    platforms.  The <literal>collate.icu.utf8</literal> test only works when
-    support for ICU was built.  Both tests will only succeed when run in a
-    database that uses UTF-8 encoding.
    </para>
   </sect2>
 
diff --git a/src/test/regress/GNUmakefile b/src/test/regress/GNUmakefile
index a24cfd4e01..80d515003c 100644
--- a/src/test/regress/GNUmakefile
+++ b/src/test/regress/GNUmakefile
@@ -128,6 +128,14 @@ tablespace-setup:
 
 REGRESS_OPTS = --dlpath=. --max-concurrent-tests=20 $(EXTRA_REGRESS_OPTS)
 
+ifeq ($(PORTNAME),linux)
+EXTRA_TESTS += collate.linux.utf8
+endif
+
+ifeq ($(with_icu),yes)
+EXTRA_TESTS += collate.icu.utf8
+endif
+
 check: all tablespace-setup
        $(pg_regress_check) $(REGRESS_OPTS) 
--schedule=$(srcdir)/parallel_schedule $(MAXCONNOPT) $(EXTRA_TESTS)
 
diff --git a/src/test/regress/expected/collate.icu.utf8.out 
b/src/test/regress/expected/collate.icu.utf8.out
index 01bd9fb5dd..2a927771a2 100644
--- a/src/test/regress/expected/collate.icu.utf8.out
+++ b/src/test/regress/expected/collate.icu.utf8.out
@@ -1,6 +1,11 @@
 /*
  * This test is for ICU collations.
  */
+/* skip test if not UTF8 server encoding */
+select getdatabaseencoding() <> 'UTF8' AS server_encoding_incompatible \gset
+\if :server_encoding_incompatible
+\quit
+\endif
 SET client_encoding TO UTF8;
 CREATE SCHEMA collate_tests;
 SET search_path = collate_tests;
diff --git a/src/test/regress/expected/collate.icu.utf8_1.out 
b/src/test/regress/expected/collate.icu.utf8_1.out
new file mode 100644
index 0000000000..a622b35fb4
--- /dev/null
+++ b/src/test/regress/expected/collate.icu.utf8_1.out
@@ -0,0 +1,7 @@
+/*
+ * This test is for ICU collations.
+ */
+/* skip test if not UTF8 server encoding */
+select getdatabaseencoding() <> 'UTF8' AS server_encoding_incompatible \gset
+\if :server_encoding_incompatible
+\quit
diff --git a/src/test/regress/expected/collate.linux.utf8.out 
b/src/test/regress/expected/collate.linux.utf8.out
index 619688f851..000d883e70 100644
--- a/src/test/regress/expected/collate.linux.utf8.out
+++ b/src/test/regress/expected/collate.linux.utf8.out
@@ -3,6 +3,10 @@
  * locales is installed.  It must be run in a database with UTF-8 encoding,
  * because other encodings don't support all the characters used.
  */
+select getdatabaseencoding() <> 'UTF8' AS server_encoding_incompatible \gset
+\if :server_encoding_incompatible
+\quit
+\endif
 SET client_encoding TO UTF8;
 CREATE SCHEMA collate_tests;
 SET search_path = collate_tests;
diff --git a/src/test/regress/expected/collate.linux.utf8_1.out 
b/src/test/regress/expected/collate.linux.utf8_1.out
new file mode 100644
index 0000000000..c613c88d4d
--- /dev/null
+++ b/src/test/regress/expected/collate.linux.utf8_1.out
@@ -0,0 +1,8 @@
+/*
+ * This test is for Linux/glibc systems and assumes that a full set of
+ * locales is installed.  It must be run in a database with UTF-8 encoding,
+ * because other encodings don't support all the characters used.
+ */
+select getdatabaseencoding() <> 'UTF8' AS server_encoding_incompatible \gset
+\if :server_encoding_incompatible
+\quit
diff --git a/src/test/regress/sql/collate.icu.utf8.sql 
b/src/test/regress/sql/collate.icu.utf8.sql
index 2be7759c92..ecbc549006 100644
--- a/src/test/regress/sql/collate.icu.utf8.sql
+++ b/src/test/regress/sql/collate.icu.utf8.sql
@@ -2,6 +2,12 @@
  * This test is for ICU collations.
  */
 
+/* skip test if not UTF8 server encoding */
+select getdatabaseencoding() <> 'UTF8' AS server_encoding_incompatible \gset
+\if :server_encoding_incompatible
+\quit
+\endif
+
 SET client_encoding TO UTF8;
 
 CREATE SCHEMA collate_tests;
diff --git a/src/test/regress/sql/collate.linux.utf8.sql 
b/src/test/regress/sql/collate.linux.utf8.sql
index c009fd2372..13ef659fd6 100644
--- a/src/test/regress/sql/collate.linux.utf8.sql
+++ b/src/test/regress/sql/collate.linux.utf8.sql
@@ -4,6 +4,11 @@
  * because other encodings don't support all the characters used.
  */
 
+select getdatabaseencoding() <> 'UTF8' AS server_encoding_incompatible \gset
+\if :server_encoding_incompatible
+\quit
+\endif
+
 SET client_encoding TO UTF8;
 
 CREATE SCHEMA collate_tests;
-- 
2.22.0

Reply via email to