Переглянути джерело

libcompat: Make the library testable

This will allow us to guarantee the compatibility implementations at
least build, and can eventually be tested.
Guillem Jover 12 роки тому
батько
коміт
f45bc2de51

+ 2 - 0
debian/changelog

@@ -5,6 +5,8 @@ dpkg (1.17.11) UNRELEASED; urgency=low
     latter is available (round two). This includes the quilt patch header
     templates, and examples in man pages.
   * Update some dpkg git URLs to the new and newer (cgit switch) scheme.
+  * Changes to libcompat:
+    - Make the library testable.
 
   [ Updated programs translations ]
   * Danish (Joe Dalton). Closes: #754127

+ 14 - 1
lib/compat/Makefile.am

@@ -5,7 +5,20 @@ AM_CPPFLAGS = \
 	-I$(top_builddir)
 
 
-noinst_LTLIBRARIES = libcompat.la
+noinst_LTLIBRARIES = libcompat-test.la libcompat.la
+
+libcompat_test_la_CPPFLAGS = $(AM_CPPFLAGS) -DTEST_LIBCOMPAT=1
+libcompat_test_la_SOURCES = \
+	compat.h \
+	strnlen.c \
+	strndup.c \
+	strerror.c \
+	strsignal.c \
+	snprintf.c vsnprintf.c \
+	asprintf.c vasprintf.c \
+	alphasort.c \
+	scandir.c \
+	unsetenv.c
 
 libcompat_la_SOURCES = \
 	empty.c \

+ 2 - 0
lib/compat/alphasort.c

@@ -22,6 +22,8 @@
 #include <string.h>
 #include <dirent.h>
 
+#include "compat.h"
+
 int
 alphasort(const void *a, const void *b)
 {

+ 2 - 0
lib/compat/asprintf.c

@@ -22,6 +22,8 @@
 #include <stdarg.h>
 #include <stdio.h>
 
+#include "compat.h"
+
 int
 asprintf(char **strp, char const *fmt, ...)
 {

+ 51 - 18
lib/compat/compat.h

@@ -22,6 +22,23 @@
 #ifndef COMPAT_H
 #define COMPAT_H
 
+#ifndef TEST_LIBCOMPAT
+#define TEST_LIBCOMPAT 0
+#endif
+
+#if TEST_LIBCOMPAT || !defined(HAVE_STRNLEN) || !defined(HAVE_STRNDUP) || \
+    !defined(HAVE_C99_SNPRINTF)
+#include <stddef.h>
+#endif
+
+#if TEST_LIBCOMPAT || !defined(HAVE_ASPRINTF) || !defined(HAVE_C99_SNPRINTF)
+#include <stdarg.h>
+#endif
+
+#if  TEST_LIBCOMPAT || !defined(HAVE_VA_COPY)
+#include <string.h>
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -51,56 +68,72 @@ extern "C" {
 #endif
 
 #ifndef HAVE_VA_COPY
-#include <string.h>
 #define va_copy(dest, src) memcpy(&(dest), &(src), sizeof(va_list))
 #endif
 
-#ifndef HAVE_C99_SNPRINTF
-#include <stddef.h>
-#include <stdarg.h>
-
+#if TEST_LIBCOMPAT
+#undef snprintf
+#define snprintf test_snprintf
+#undef vsnprintf
+#define vsnprintf test_vsnprintf
+#undef asprintf
+#define asprintf test_asprintf
+#undef vasprintf
+#define vasprintf test_vasprintf
+#undef strndup
+#define strndup test_strndup
+#undef strnlen
+#define strnlen test_strnlen
+#undef strerror
+#define strerror test_strerror
+#undef strsignal
+#define strsignal test_strsignal
+#undef scandir
+#define scandir test_scandir
+#undef alphasort
+#define alphasort test_alphasort
+#undef unsetenv
+#define unsetenv test_unsetenv
+#endif
+
+#if TEST_LIBCOMPAT || !defined(HAVE_C99_SNPRINTF)
 int snprintf(char *str, size_t n, char const *fmt, ...);
 int vsnprintf(char *buf, size_t maxsize, const char *fmt, va_list args);
 #endif
 
-#ifndef HAVE_ASPRINTF
-#include <stdarg.h>
-
+#if TEST_LIBCOMPAT || !defined(HAVE_ASPRINTF)
 int asprintf(char **str, char const *fmt, ...);
 int vasprintf(char **str, const char *fmt, va_list args);
 #endif
 
-#ifndef HAVE_STRNLEN
+#if TEST_LIBCOMPAT || !defined(HAVE_STRNLEN)
 size_t strnlen(const char *s, size_t n);
 #endif
 
-#ifndef HAVE_STRNDUP
-#include <stddef.h>
-
-#undef strndup
+#if TEST_LIBCOMPAT || !defined(HAVE_STRNDUP)
 char *strndup(const char *s, size_t n);
 #endif
 
-#ifndef HAVE_STRERROR
+#if TEST_LIBCOMPAT || !defined(HAVE_STRERROR)
 const char *strerror(int);
 #endif
 
-#ifndef HAVE_STRSIGNAL
+#if TEST_LIBCOMPAT || !defined(HAVE_STRSIGNAL)
 const char *strsignal(int);
 #endif
 
-#ifndef HAVE_SCANDIR
+#if TEST_LIBCOMPAT || !defined(HAVE_SCANDIR)
 struct dirent;
 int scandir(const char *dir, struct dirent ***namelist,
             int (*filter)(const struct dirent *),
             int (*cmp)(const void *, const void *));
 #endif
 
-#ifndef HAVE_ALPHASORT
+#if TEST_LIBCOMPAT || !defined(HAVE_ALPHASORT)
 int alphasort(const void *a, const void *b);
 #endif
 
-#ifndef HAVE_UNSETENV
+#if TEST_LIBCOMPAT || !defined(HAVE_UNSETENV)
 int unsetenv(const char *x);
 #endif
 

+ 2 - 0
lib/compat/scandir.c

@@ -26,6 +26,8 @@
 #include <dirent.h>
 #include <stdlib.h>
 
+#include "compat.h"
+
 static int
 cleanup(DIR *dir, struct dirent **dirlist, int used)
 {

+ 2 - 1
lib/compat/snprintf.c

@@ -18,12 +18,13 @@
  */
 
 #include <config.h>
-#include <compat.h>
 
 #include <stddef.h>
 #include <stdarg.h>
 #include <stdio.h>
 
+#include "compat.h"
+
 int
 snprintf(char *str, size_t n, char const *fmt, ...)
 {

+ 4 - 0
lib/compat/strerror.c

@@ -22,10 +22,14 @@
 #include <stdio.h>
 #include <gettext.h>
 
+#include "compat.h"
+
 #define _(str) gettext(str)
 
+#ifndef HAVE_STRERROR
 extern const char *const sys_errlist[];
 extern const int sys_nerr;
+#endif
 
 const char *
 strerror(int e)

+ 1 - 2
lib/compat/strndup.c

@@ -18,12 +18,11 @@
  */
 
 #include <config.h>
-#include <compat.h>
 
 #include <string.h>
 #include <stdlib.h>
 
-#undef strndup
+#include "compat.h"
 
 char *
 strndup(const char *s, size_t n)

+ 2 - 0
lib/compat/strsignal.c

@@ -24,6 +24,8 @@
 #include <stdio.h>
 #include <gettext.h>
 
+#include "compat.h"
+
 #define _(str) gettext(str)
 
 #ifndef HAVE_DECL_SYS_SIGLIST

+ 2 - 0
lib/compat/unsetenv.c

@@ -22,6 +22,8 @@
 #include <string.h>
 #include <stdlib.h>
 
+#include "compat.h"
+
 int
 unsetenv(const char *p)
 {

+ 2 - 0
lib/compat/vasprintf.c

@@ -23,6 +23,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "compat.h"
+
 int
 vasprintf(char **strp, char const *fmt, va_list args)
 {

+ 2 - 0
lib/compat/vsnprintf.c

@@ -26,6 +26,8 @@
 #include <stdarg.h>
 #include <stdio.h>
 
+#include "compat.h"
+
 int
 vsnprintf(char *buf, size_t maxsize, const char *fmt, va_list args)
 {