From 1d4d7015b582405bdcc5bc4968b98d1bcbaa7527 Mon Sep 17 00:00:00 2001
From: Juan Jose Santamaria Flecha <juanjo.santamaria@gmail.com>
Date: Fri, 26 Nov 2021 05:08:48 -0500
Subject: [PATCH] Test for Windows stat emulation working with streams

---
 src/test/Makefile           |  2 +-
 src/test/copy/.gitignore    |  2 ++
 src/test/copy/Makefile      | 23 +++++++++++++++++++++++
 src/test/copy/README        | 25 +++++++++++++++++++++++++
 src/test/copy/t/001_copy.pl | 32 ++++++++++++++++++++++++++++++++
 5 files changed, 83 insertions(+), 1 deletion(-)
 create mode 100644 src/test/copy/.gitignore
 create mode 100644 src/test/copy/Makefile
 create mode 100644 src/test/copy/README
 create mode 100644 src/test/copy/t/001_copy.pl

diff --git a/src/test/Makefile b/src/test/Makefile
index 4627591..dc3134f 100644
--- a/src/test/Makefile
+++ b/src/test/Makefile
@@ -12,7 +12,7 @@ subdir = src/test
 top_builddir = ../..
 include $(top_builddir)/src/Makefile.global
 
-SUBDIRS = perl regress isolation modules authentication recovery subscription
+SUBDIRS = perl regress isolation modules authentication recovery subscription copy
 
 # Test suites that are not safe by default but can be run if selected
 # by the user via the whitespace-separated list in variable
diff --git a/src/test/copy/.gitignore b/src/test/copy/.gitignore
new file mode 100644
index 0000000..871e943
--- /dev/null
+++ b/src/test/copy/.gitignore
@@ -0,0 +1,2 @@
+# Generated by test suite
+/tmp_check/
diff --git a/src/test/copy/Makefile b/src/test/copy/Makefile
new file mode 100644
index 0000000..64e99b7
--- /dev/null
+++ b/src/test/copy/Makefile
@@ -0,0 +1,23 @@
+#-------------------------------------------------------------------------
+#
+# Makefile for src/test/copy
+#
+# Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
+# Portions Copyright (c) 1994, Regents of the University of California
+#
+# src/test/copy/Makefile
+#
+#-------------------------------------------------------------------------
+
+subdir = src/test/copy
+top_builddir = ../../..
+include $(top_builddir)/src/Makefile.global
+
+check:
+	$(prove_check)
+
+installcheck:
+	$(prove_installcheck)
+
+clean distclean maintainer-clean:
+	rm -rf tmp_check
diff --git a/src/test/copy/README b/src/test/copy/README
new file mode 100644
index 0000000..9617db1
--- /dev/null
+++ b/src/test/copy/README
@@ -0,0 +1,25 @@
+src/test/recovery/README
+
+Regression tests for copy
+=============================================
+
+This directory contains a test suite for copy without redirecting the data
+streams on Windows.
+
+Running the tests
+=================
+
+NOTE: You must have given the --enable-tap-tests argument to configure.
+Also, to use "make installcheck", you must have built and installed
+contrib/test_decoding in addition to the core code.
+
+Run
+    make check
+or
+    make installcheck
+You can use "make installcheck" if you previously did "make install".
+In that case, the code in the installation tree is tested.  With
+"make check", a temporary installation tree is built from the current
+sources and then tested.
+
+See src/test/perl/README for more info about running these tests.
diff --git a/src/test/copy/t/001_copy.pl b/src/test/copy/t/001_copy.pl
new file mode 100644
index 0000000..07c7811
--- /dev/null
+++ b/src/test/copy/t/001_copy.pl
@@ -0,0 +1,32 @@
+
+# Copyright (c) 2021, PostgreSQL Global Development Group
+
+use strict;
+use warnings;
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More tests => 1;
+use Config;
+
+SKIP:
+{
+	skip "Test stat emulation on Windows", 1 if $Config{osname} ne 'MSWin32';
+
+	my $node = PostgreSQL::Test::Cluster->new('main');
+	$node->init;
+	$node->start;
+	my $port   = $node->port;
+	my $result;
+
+	$node->safe_psql('postgres','CREATE TABLE public.test (a INT)');
+
+	eval {
+		$result = IPC::Run::run [ 'psql', '-p', $port, '-c', "\\copy public.test from stdin", 'postgres' ], 
+			IPC::Run::timeout( 1, name => "stall timeout" );
+	};
+	
+	like( $@, qr/IPC::Run: timeout on stall timeout/,
+			'copy from stdin' );
+
+	$node->stop('fast');
+}
\ No newline at end of file
-- 
2.11.0

