From 970c5f4f29272f9847897d1c10c2bc687b3b9fb9 Mon Sep 17 00:00:00 2001 From: Anton Lindqvist Date: Wed, 20 Feb 2019 20:25:31 +0100 Subject: [PATCH] Get rid of autoconf Relying on external dependencies to build such a small project as pick has always been troublesome to me. Instead, replace auto{conf,make} with a shell-script and plain Makefile(s). --- .gitignore | 28 +----- CONTRIBUTING.md | 31 ++---- GNUmakefile | 4 + INSTALL.md.in | 17 ---- Makefile | 112 +++++++++++++++++++++ Makefile.am | 59 ----------- README.md | 9 +- autogen.sh | 3 - compat-pledge.c | 14 +++ compat-reallocarray.c | 7 -- compat-strtonum.c | 2 - compat.h | 34 ------- config.h.in | 37 ------- configure | 227 ++++++++++++++++++++++++++++++++++++++++++ configure.ac | 34 ------- pick.c | 18 +--- tests/GNUmakefile | 4 + tests/Makefile | 67 +++++++++++++ tests/README.md | 6 +- tests/pick-test.c | 16 ++- tests/pick-test.sh | 15 ++- 21 files changed, 463 insertions(+), 281 deletions(-) create mode 100644 GNUmakefile delete mode 100644 INSTALL.md.in create mode 100644 Makefile delete mode 100644 Makefile.am delete mode 100755 autogen.sh create mode 100644 compat-pledge.c delete mode 100644 compat.h delete mode 100644 config.h.in create mode 100755 configure delete mode 100644 configure.ac create mode 100644 tests/GNUmakefile create mode 100644 tests/Makefile diff --git a/.gitignore b/.gitignore index 906e2bce..15d2d123 100644 --- a/.gitignore +++ b/.gitignore @@ -1,29 +1,3 @@ -*.o -core* -pick -vgcore* - -# Generated by Autotools -.deps -INSTALL.md -Makefile -Makefile.in -aclocal.m4 -autom4te.cache/ -compile -config.guess +Makefile.inc config.h config.log -config.status -config.sub -configure -depcomp -install-sh -missing -stamp-h1 -.dirstamp -test-driver -test-suite.log -tests/pick-test -tests/*.log -tests/*.trs diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4cfc4af2..bc919b76 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -11,9 +11,8 @@ you agree to abide by its [Code of Conduct][conduct]. 2. Build and make sure all tests pass: ```sh - $ ./autogen.sh $ ./configure - $ make check + $ make test ``` 3. Make your changes. @@ -56,45 +55,31 @@ Internal documentation on crafting a release. 2. Update `CHANGELOG.md` and commit the change: ```sh - $ git commit -m 'Update CHANGELOG' + $ git commit -m 'update changelog' ``` -3. Update the version in `configure.ac`: - - ``` - AC_INIT([pick], [0.0.2], [pick-maintainers@calleerlandsson.com]) - ``` - - ... and commit the change: +3. Update the version in `Makefile` and commit the change: ```sh - $ git commit -m 'Update version to 0.0.2' + $ git commit -m 'update version to 0.0.2' ``` 4. Create and verify the tarball: ```sh - $ make distcheck - $ tar tvzf pick-0.0.2.tar.gz - ``` - -5. Create and verify checksum: - - ```sh - $ sha256 pick-0.0.2.tar.gz >pick-0.0.2.sha256 - $ sha256 -c pick-0.0.2.sha256 + $ make dist ``` -6. Tag and push: +5. Tag and push: ```sh $ git tag v0.0.2 $ git push --tags origin master ``` -7. [Announce the release on GitHub][announce]. +6. [Announce the release on GitHub][announce]. -8. Contact package maintainers: +7. Contact package maintainers: * Aggelos Avgerinos (Debian & Ubuntu) * Chunyang Xu (MacPorts) diff --git a/GNUmakefile b/GNUmakefile new file mode 100644 index 00000000..82c9d6af --- /dev/null +++ b/GNUmakefile @@ -0,0 +1,4 @@ +.CURDIR= ${CURDIR} +.OBJDIR= ${CURDIR} + +include ${.CURDIR}/Makefile diff --git a/INSTALL.md.in b/INSTALL.md.in deleted file mode 100644 index 4fe05ea7..00000000 --- a/INSTALL.md.in +++ /dev/null @@ -1,17 +0,0 @@ -pick installation -================= - -1. Download and extract the latest release: - - curl -LO https://github.com/mptre/pick/releases/download/v@PACKAGE_VERSION@/pick-@PACKAGE_VERSION@.tar.gz - tar -zxvf pick-@PACKAGE_VERSION@.tar.gz - cd pick-@PACKAGE_VERSION@ - -2. Configure the distribution: - - ./configure - -3. Build and install pick: - - make - sudo make install diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..cbad7f5b --- /dev/null +++ b/Makefile @@ -0,0 +1,112 @@ +include ${.CURDIR}/Makefile.inc + +VERSION= 2.0.2 + +PROG= pick + +SRCS+= compat-pledge.c +SRCS+= compat-reallocarray.c +SRCS+= compat-strtonum.c +SRCS+= pick.c + +OBJS= ${SRCS:.c=.o} +DEPS= ${SRCS:.c=.d} + +CFLAGS+= ${DEBUG} +CFLAGS+= -DVERSION=\"${VERSION}\" + +DISTFILES+= CHANGELOG.md +DISTFILES+= CODE_OF_CONDUCT.md +DISTFILES+= CONTRIBUTING.md +DISTFILES+= GNUmakefile +DISTFILES+= LICENSE +DISTFILES+= Makefile +DISTFILES+= README.md +DISTFILES+= compat-pledge.c +DISTFILES+= compat-reallocarray.c +DISTFILES+= compat-strtonum.c +DISTFILES+= configure +DISTFILES+= pick.1 +DISTFILES+= pick.c +DISTFILES+= tests/GNUmakefile +DISTFILES+= tests/Makefile +DISTFILES+= tests/README.md +DISTFILES+= tests/key-alt-enter.t +DISTFILES+= tests/key-backspace.t +DISTFILES+= tests/key-ctrl-a.t +DISTFILES+= tests/key-ctrl-c.t +DISTFILES+= tests/key-ctrl-e.t +DISTFILES+= tests/key-ctrl-k.t +DISTFILES+= tests/key-ctrl-l.t +DISTFILES+= tests/key-ctrl-o.t +DISTFILES+= tests/key-ctrl-u.t +DISTFILES+= tests/key-ctrl-w.t +DISTFILES+= tests/key-del.t +DISTFILES+= tests/key-end.t +DISTFILES+= tests/key-enter.t +DISTFILES+= tests/key-home.t +DISTFILES+= tests/key-left.t +DISTFILES+= tests/key-line-down.t +DISTFILES+= tests/key-line-up.t +DISTFILES+= tests/key-page-down.t +DISTFILES+= tests/key-page-up.t +DISTFILES+= tests/key-printable.t +DISTFILES+= tests/key-right.t +DISTFILES+= tests/key-unknown.t +DISTFILES+= tests/misc-match.t +DISTFILES+= tests/misc-realloc.t +DISTFILES+= tests/opt-d.t +DISTFILES+= tests/opt-h.t +DISTFILES+= tests/opt-k.t +DISTFILES+= tests/opt-o.t +DISTFILES+= tests/opt-q.t +DISTFILES+= tests/opt-s.t +DISTFILES+= tests/opt-unknown.t +DISTFILES+= tests/opt-v.t +DISTFILES+= tests/opt-x.t +DISTFILES+= tests/pick-test.c +DISTFILES+= tests/pick-test.sh + +PREFIX= /usr/local + +all: ${PROG} + +${PROG}: ${OBJS} + ${CC} ${DEBUG} -o ${PROG} ${OBJS} ${LDFLAGS} + +clean: + rm -f ${DEPS} ${OBJS} ${PROG} +.PHONY: clean + +dist: + set -e; \ + d=${PROG}-${VERSION}; \ + mkdir $$d; \ + for f in ${DISTFILES}; do \ + mkdir -p $$d/`dirname $$f`; \ + cp ${.CURDIR}/$$f $$d/$$f; \ + done; \ + tar czvf ${.CURDIR}/$$d.tar.gz $$d; \ + (cd ${.CURDIR}; sha256 $$d.tar.gz >$$d.sha256); \ + rm -r $$d +.PHONY: dist + +distclean: clean + rm -f ${.CURDIR}/Makefile.inc ${.CURDIR}/config.h \ + ${.CURDIR}/config.log ${.CURDIR}/${PROG}-${VERSION}.tar.gz \ + ${.CURDIR}/${PROG}-${VERSION}.sha256 +.PHONY: distclean + +install: ${PROG} + @mkdir -p ${DESTDIR}${PREFIX}/bin + ${INSTALL} ${PROG} ${DESTDIR}${PREFIX}/bin + @mkdir -p ${DESTDIR}${PREFIX}/man/man1 + ${INSTALL} ${.CURDIR}/pick.1 ${DESTDIR}${PREFIX}/man/man1 +.PHONY: install + +test: ${PROG} + ${MAKE} -C ${.CURDIR}/tests \ + "MALLOC_OPTIONS=${MALLOC_OPTIONS}" "PICK=${.OBJDIR}/${PROG}" +.PHONY: test + +-include ${DEPS} diff --git a/Makefile.am b/Makefile.am deleted file mode 100644 index 803d026a..00000000 --- a/Makefile.am +++ /dev/null @@ -1,59 +0,0 @@ -AUTOMAKE_OPTIONS=foreign - -AM_CFLAGS=-Wall -Wextra -AM_CPPFLAGS=-D_GNU_SOURCE - -bin_PROGRAMS=pick -pick_SOURCES=pick.c compat-reallocarray.c compat-strtonum.c compat.h -pick_CPPFLAGS=$(AM_CPPFLAGS) $(NCURSES_CFLAGS) -pick_LDADD=$(NCURSES_LIBS) - -dist_man_MANS=pick.1 - -TESTS=tests/key-alt-enter.t \ - tests/key-backspace.t \ - tests/key-ctrl-a.t \ - tests/key-ctrl-c.t \ - tests/key-ctrl-e.t \ - tests/key-ctrl-k.t \ - tests/key-ctrl-l.t \ - tests/key-ctrl-o.t \ - tests/key-ctrl-u.t \ - tests/key-ctrl-w.t \ - tests/key-del.t \ - tests/key-end.t \ - tests/key-enter.t \ - tests/key-home.t \ - tests/key-left.t \ - tests/key-line-down.t \ - tests/key-line-up.t \ - tests/key-page-down.t \ - tests/key-page-up.t \ - tests/key-printable.t \ - tests/key-right.t \ - tests/key-unknown.t \ - tests/misc-match.t \ - tests/misc-realloc.t \ - tests/opt-d.t \ - tests/opt-h.t \ - tests/opt-k.t \ - tests/opt-o.t \ - tests/opt-q.t \ - tests/opt-s.t \ - tests/opt-unknown.t \ - tests/opt-v.t \ - tests/opt-x.t -TEST_EXTENSIONS=.t -T_LOG_COMPILER=$(top_srcdir)/tests/pick-test.sh \ - -e MALLOC_OPTIONS=@MALLOC_OPTIONS@ -AM_COLOR_TESTS=no -check_PROGRAMS=tests/pick-test -tests_pick_test_SOURCES=tests/pick-test.c compat-reallocarray.c -tests_pick_test_CFLAGS=$(AM_CFLAGS) - -EXTRA_DIST=INSTALL.md INSTALL.md.in LICENSE README.md tests/pick-test.sh $(TESTS) -DISTCLEANFILES=INSTALL.md - -INSTALL.md: INSTALL.md.in - sed -e 's|@PACKAGE_VERSION[@]|$(PACKAGE_VERSION)|g' \ - $(top_srcdir)/$@.in > $@ diff --git a/README.md b/README.md index 8827d0b1..cdf2d118 100644 --- a/README.md +++ b/README.md @@ -89,11 +89,10 @@ pkg_add pick ### From source -Download the latest [release] and follow the bundled instructions in -`INSTALL.md`. - -If you want to try the latest unreleased version, -follow the instructions in [CONTRIBUTING.md][current]. +```sh +./configure +make install +``` ## Copyright diff --git a/autogen.sh b/autogen.sh deleted file mode 100755 index 1389803c..00000000 --- a/autogen.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -aclocal && autoconf && automake --add-missing diff --git a/compat-pledge.c b/compat-pledge.c new file mode 100644 index 00000000..26676997 --- /dev/null +++ b/compat-pledge.c @@ -0,0 +1,14 @@ +#include "config.h" + +int unused; + +#ifndef HAVE_PLEDGE + +int +pledge(const char *promises __attribute__((__unused__)), + const char *execpromises __attribute__((__unused__))) +{ + return 0; +} + +#endif /* !HAVE_PLEDGE */ diff --git a/compat-reallocarray.c b/compat-reallocarray.c index bf5018f5..109832df 100644 --- a/compat-reallocarray.c +++ b/compat-reallocarray.c @@ -1,12 +1,5 @@ -#ifdef HAVE_CONFIG_H #include "config.h" -#endif -/* - * This file needs a "translation unit" - it needs anything for the compiler to - * compile. Since the point of this file is to not exist, declare an unused - * variable here. - */ int unused; #ifndef HAVE_REALLOCARRAY diff --git a/compat-strtonum.c b/compat-strtonum.c index ea85a2e8..411de46a 100644 --- a/compat-strtonum.c +++ b/compat-strtonum.c @@ -1,6 +1,4 @@ -#ifdef HAVE_CONFIG_H #include "config.h" -#endif int unused; diff --git a/compat.h b/compat.h deleted file mode 100644 index f4df9e25..00000000 --- a/compat.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef COMPAT_H -#define COMPAT_H - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#ifdef __FreeBSD__ -#define __dead __dead2 -#endif /* __FreeBSD__ */ - -#if defined(__linux__) || defined(__sun__) || defined(__CYGWIN__) -#ifndef __dead -#ifdef __GNUC__ -#define __dead __attribute__((__noreturn__)) -#else -#define __dead -#endif -#endif -#endif /* __linux__ || __sun__ || __CYGWIN__ */ - -#ifndef HAVE_REALLOCARRAY - -void *reallocarray(void *, size_t, size_t); - -#endif /* !HAVE_REALLOCARRAY */ - -#ifndef HAVE_STRTONUM - -long long strtonum(const char *, long long, long long, const char **); - -#endif /* !HAVE_STRTONUM */ - -#endif /* COMPAT_H */ diff --git a/config.h.in b/config.h.in deleted file mode 100644 index 66b3dbd6..00000000 --- a/config.h.in +++ /dev/null @@ -1,37 +0,0 @@ -/* config.h.in. Generated from configure.ac by autoheader. */ - -/* Define if ncursesw is available */ -#undef HAVE_NCURSESW_H - -/* Define to 1 if you have the `pledge' function. */ -#undef HAVE_PLEDGE - -/* Define to 1 if you have the `reallocarray' function. */ -#undef HAVE_REALLOCARRAY - -/* Define to 1 if you have the `strtonum' function. */ -#undef HAVE_STRTONUM - -/* Name of package */ -#undef PACKAGE - -/* Define to the address where bug reports for this package should be sent. */ -#undef PACKAGE_BUGREPORT - -/* Define to the full name of this package. */ -#undef PACKAGE_NAME - -/* Define to the full name and version of this package. */ -#undef PACKAGE_STRING - -/* Define to the one symbol short name of this package. */ -#undef PACKAGE_TARNAME - -/* Define to the home page for this package. */ -#undef PACKAGE_URL - -/* Define to the version of this package. */ -#undef PACKAGE_VERSION - -/* Version number of package */ -#undef VERSION diff --git a/configure b/configure new file mode 100755 index 00000000..a65d736b --- /dev/null +++ b/configure @@ -0,0 +1,227 @@ +#!/bin/sh + +set -e + +atexit() { + err=$? + + # Dump contents of generated files to config.log. + set -x + cat config.h + cat Makefile.inc + rm -f "$@" + [ $err -ne 0 ] && fatal + exit 0 +} + +compile() { + $CC -Werror -o /dev/null -x c - $LDFLAGS +} + +fatal() { + [ $# -gt 0 ] && echo "fatal: ${*}" + exec 1>&3 2>&4 + cat config.log + return 1 +} + +headers() { + cat >"$TMP" + [ -s "$TMP" ] || return 0 + + xargs printf '#include <%s>\n' <"$TMP" +} + +makevar() { + make -sf - < + #include + + int main(void) { + return !(setupterm(NULL, 1, NULL) != ERR); + } + EOF +} + +check_dead() { + compile <<-EOF + #include + + ${1} int dead(void); + + int main(void) { + return 0; + } + EOF +} + +# Check if wcwidth(3) is hidden behind _GNU_SOURCE. +check_gnu_source() { + compile <<-EOF && return 1 + #include + + int main(void) { + wchar_t c = 0; + return !(wcwidth(c) == 0); + } + EOF + + compile <<-EOF + #define _GNU_SOURCE + #include + + int main(void) { + wchar_t c = 0; + return !(wcwidth(c) == 0); + } + EOF +} + + +check_malloc_options() { + case "$(uname -s)" in + OpenBSD) echo "RS";; + esac +} + +check_pledge() { + compile <<-EOF + #include + + int main(void) { + return !(pledge("stdio", NULL) == 0); + } + EOF +} + +check_reallocarray() { + compile <<-EOF + #include + + int main(void) { + return !(reallocarray(NULL, 1, 1) != NULL); + } + EOF +} + +check_strtonum() { + compile <<-EOF + #include + + int main(void) { + return !(strtonum("1", 1, 2, NULL) != 0); + } + EOF +} + +HAVE_CURSES=0 +HAVE_DEAD2=0 +HAVE_DEAD=0 +HAVE_GNU_SOURCE=0 +HAVE_NCURSESW=0 +HAVE_NORETURN=0 +HAVE_PLEDGE=0 +HAVE_REALLOCARRAY=0 +HAVE_STRTONUM=0 + +CC=$(makevar CC) +CFLAGS=$(makevar CFLAGS) +CFLAGS="${CFLAGS} ${DEBUG} -Wall -Wextra -MD -MP" +INSTALL=$(makevar INSTALL) + +: "${CPPFLAGS:=}" +: "${DEBUG:=}" +: "${INSTALL:=install}" +: "${LDFLAGS:=}" + +exec 3>&1 4>&2 +exec 1>config.log 2>&1 + +TMP=$(mktemp -t configure.XXXXXX) +trap "atexit $TMP" EXIT + +# At this point, all variables used must be defined. +set -u +# Enable tracing, will end up in config.log. +set -x + +if (LDFLAGS=-lcurses check_curses); then + HAVE_CURSES=1 + LDFLAGS="${LDFLAGS} -lcurses" +elif (LDFLAGS=-lncursesw check_curses); then + HAVE_NCURSESW=1 + LDFLAGS="${LDFLAGS} -lncursesw" +else + fatal "curses library not found" +fi + +check_dead __dead && HAVE_DEAD=1 +check_dead __dead2 && HAVE_DEAD2=1 +check_dead '__attribute__((__noreturn__))' && HAVE_NORETURN=1 +check_gnu_source && HAVE_GNU_SOURCE=1 +check_pledge && HAVE_PLEDGE=1 +check_reallocarray && HAVE_REALLOCARRAY=1 +check_strtonum && HAVE_STRTONUM=1 + +MALLOC_OPTIONS=$(check_malloc_options) + +# Order is important, must be present before any includes. +( +[ $HAVE_GNU_SOURCE -eq 1 ] && printf '#define _GNU_SOURCE\n' + +# Ensure this subshell always exits 0. +true +) >config.h + +# Headers needed for function prototypes and curses. +( +[ $HAVE_CURSES -eq 1 ] && echo curses.h term.h +[ $HAVE_NCURSESW -eq 1 ] && echo ncursesw/curses.h ncursesw/term.h +[ $HAVE_PLEDGE -eq 0 ] && echo stdlib.h +[ $HAVE_REALLOCARRAY -eq 0 ] && echo stdlib.h +[ $HAVE_STRTONUM -eq 0 ] && echo stdlib.h +) | sort | uniq | headers >>config.h + +( +[ $HAVE_PLEDGE -eq 1 ] && printf '#define HAVE_PLEDGE\t1\n' +[ $HAVE_REALLOCARRAY -eq 1 ] && printf '#define HAVE_REALLOCARRAY\t1\n' +[ $HAVE_STRTONUM -eq 1 ] && printf '#define HAVE_STRTONUM\t1\n' + +if [ $HAVE_DEAD -eq 1 ]; then + : +elif [ $HAVE_DEAD2 -eq 1 ]; then + printf '#define __dead __dead2\n' +elif [ $HAVE_NORETURN -eq 1 ]; then + printf '#define __dead __attribute__((__noreturn__))\n' +else + printf '#define __dead\n' +fi + +[ $HAVE_PLEDGE -eq 0 ] && \ + printf 'int pledge(const char *, const char *);\n' +[ $HAVE_REALLOCARRAY -eq 0 ] && \ + printf 'void *reallocarray(void *, size_t, size_t);\n' +[ $HAVE_STRTONUM -eq 0 ] && \ + printf 'long long strtonum(const char *, long long, long long, const char **);\n' + +# Ensure this subshell always exits 0. +true +) >>config.h + +# Use echo to normalize whitespace. +cat <Makefile.inc +CC= $(echo $CC) +CFLAGS= $(echo $CFLAGS) +CPPFLAGS= $(echo $CPPFLAGS) +DEBUG= $(echo $DEBUG) +INSTALL= $(echo $INSTALL) +LDFLAGS= $(echo $LDFLAGS) +MALLOC_OPTIONS= $(echo $MALLOC_OPTIONS) +EOF diff --git a/configure.ac b/configure.ac deleted file mode 100644 index dfa1fa12..00000000 --- a/configure.ac +++ /dev/null @@ -1,34 +0,0 @@ -AC_PREREQ([2.61]) -AC_INIT([pick], [2.0.2], [pick-maintainers@calleerlandsson.com]) -AM_INIT_AUTOMAKE([subdir-objects]) -AC_CONFIG_HEADERS([config.h]) -AC_PROG_CC -AM_PROG_CC_C_O -AC_CHECK_FUNCS([pledge reallocarray strtonum]) -AC_SEARCH_LIBS([setupterm], [curses], [], [ - AC_SEARCH_LIBS([setupterm], [ncursesw], - [AC_DEFINE([HAVE_NCURSESW_H], [1], [Define if ncursesw is available])], - [PKG_CHECK_MODULES([NCURSES], [ncurses], [], [ - PKG_CHECK_MODULES([NCURSES], [ncursesw], - [AC_DEFINE([HAVE_NCURSESW_H], [1], [Define if ncursesw is available])] - )] - )] - ) -]) -AC_DEFUN([AC_MALLOC_OPTIONS], [ - AC_CANONICAL_HOST - AC_MSG_CHECKING([for $host_os malloc hardening options]) - case "$host_os" in - openbsd*) malloc_options="RS";; - *) malloc_options="";; - esac - if test -n "$malloc_options"; then - AC_MSG_RESULT([$malloc_options]) - else - AC_MSG_RESULT([no]) - fi - AC_SUBST([MALLOC_OPTIONS], [$malloc_options]) -]) -AC_MALLOC_OPTIONS -AC_CONFIG_FILES([Makefile]) -AC_OUTPUT diff --git a/pick.c b/pick.c index 4ba17de3..9a3f4224 100644 --- a/pick.c +++ b/pick.c @@ -1,6 +1,4 @@ -#ifdef HAVE_CONFIG_H #include "config.h" -#endif #include @@ -18,16 +16,6 @@ #include #include -#ifdef HAVE_NCURSESW_H -#include -#include -#else -#include -#include -#endif - -#include "compat.h" - #define tty_putp(capability, fatal) do { \ if (tputs((capability), 1, tty_putc) == ERR && (fatal)) \ errx(1, #capability ": unknown terminfo capability"); \ @@ -124,10 +112,8 @@ main(int argc, char *argv[]) setlocale(LC_CTYPE, ""); -#ifdef HAVE_PLEDGE if (pledge("stdio tty rpath wpath cpath", NULL) == -1) err(1, "pledge"); -#endif while ((c = getopt(argc, argv, "dhoq:KSvxX")) != -1) switch (c) { @@ -156,7 +142,7 @@ main(int argc, char *argv[]) sort = 0; break; case 'v': - puts(PACKAGE_VERSION); + puts(VERSION); exit(0); case 'x': use_alternate_screen = 1; @@ -181,10 +167,8 @@ main(int argc, char *argv[]) input = get_choices(); tty_init(1); -#ifdef HAVE_PLEDGE if (pledge("stdio tty", NULL) == -1) err(1, "pledge"); -#endif choice = selected_choice(); tty_restore(1); diff --git a/tests/GNUmakefile b/tests/GNUmakefile new file mode 100644 index 00000000..82c9d6af --- /dev/null +++ b/tests/GNUmakefile @@ -0,0 +1,4 @@ +.CURDIR= ${CURDIR} +.OBJDIR= ${CURDIR} + +include ${.CURDIR}/Makefile diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 00000000..509242d7 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,67 @@ +include ${.CURDIR}/../Makefile.inc + +PROG= pick-test + +SRCS+= pick-test.c +SRCS+= compat-reallocarray.c + +OBJS= ${SRCS:.c=.o} +DEPS= ${SRCS:.c=.d} + +CFLAGS+= -I${.CURDIR}/.. + +TESTS+= key-alt-enter.t +TESTS+= key-backspace.t +TESTS+= key-ctrl-a.t +TESTS+= key-ctrl-c.t +TESTS+= key-ctrl-e.t +TESTS+= key-ctrl-k.t +TESTS+= key-ctrl-l.t +TESTS+= key-ctrl-o.t +TESTS+= key-ctrl-u.t +TESTS+= key-ctrl-w.t +TESTS+= key-del.t +TESTS+= key-end.t +TESTS+= key-enter.t +TESTS+= key-home.t +TESTS+= key-left.t +TESTS+= key-line-down.t +TESTS+= key-line-up.t +TESTS+= key-page-down.t +TESTS+= key-page-up.t +TESTS+= key-printable.t +TESTS+= key-right.t +TESTS+= key-unknown.t +TESTS+= misc-match.t +TESTS+= misc-realloc.t +TESTS+= opt-d.t +TESTS+= opt-h.t +TESTS+= opt-k.t +TESTS+= opt-o.t +TESTS+= opt-q.t +TESTS+= opt-s.t +TESTS+= opt-unknown.t +TESTS+= opt-v.t +TESTS+= opt-x.t + +all: ${PROG} + ${MAKE} -C ${.CURDIR} test + +.SUFFIXES: .t .out + +.t.out: + sh ${.CURDIR}/pick-test.sh -b ${.OBJDIR}/${PROG} $< + +${PROG}: ${OBJS} + ${CC} ${DEBUG} -o ${PROG} ${OBJS} + +compat-reallocarray.c: ${.CURDIR}/../compat-reallocarray.c + @cp ${.CURDIR}/../compat-reallocarray.c . + +clean: + rm -f ${DEPS} ${OBJS} ${PROG} +.PHONY: clean + +test: ${TESTS:.t=.out} + +-include ${DEPS} diff --git a/tests/README.md b/tests/README.md index e708c816..3ee74cdd 100644 --- a/tests/README.md +++ b/tests/README.md @@ -8,20 +8,20 @@ This allows sending keyboard input sequences and reading the output on exit. The recommended way to run the test suite is using `make(1)`: ```sh -$ make check || cat test-suite.log +$ make test ``` Use the `-j` option with a value matching the number of cores on your machine to speed-up the process: ```sh -$ make -jN check || cat test-suite.log +$ make -jN test ``` To run one or more specific tests: ```sh -$ env TESTS=tests/01.t make -e check || cat test-suite.log +$ env TESTS=01.t make -e test ``` ## Format of test files diff --git a/tests/pick-test.c b/tests/pick-test.c index d7e45c70..033e1b78 100644 --- a/tests/pick-test.c +++ b/tests/pick-test.c @@ -1,3 +1,5 @@ +#include "config.h" + #include #include #include @@ -13,8 +15,6 @@ #include #include -#include "compat.h" - __dead static void child(int, int); static void parent(int, int, const char *); static char *parsekeys(const char *); @@ -36,12 +36,16 @@ static int gotsig; int main(int argc, char *argv[]) { + char *pick = NULL; char *keys = ""; pid_t pid; int c, i, master, slave, status; - while ((c = getopt(argc, argv, "k:")) != -1) + while ((c = getopt(argc, argv, "b:k:")) != -1) switch (c) { + case 'b': + pick = optarg; + break; case 'k': keys = parsekeys(optarg); break; @@ -50,11 +54,13 @@ main(int argc, char *argv[]) } argc -= optind; argv += optind; + if (pick == NULL) + usage(); /* Ensure room for program and null terminator. */ if ((pickargv = calloc(argc + 2, sizeof(const char **))) == NULL) err(1, NULL); - pickargv[0] = "./pick"; + pickargv[0] = pick; for (i = 0; i < argc; i++) pickargv[i + 1] = argv[i]; @@ -92,7 +98,7 @@ main(int argc, char *argv[]) __dead static void usage(void) { - fprintf(stderr, "usage: pick-test [-k path] [-- argument ...]\n"); + fprintf(stderr, "usage: pick-test -b binary [-k path] [-- arg ...]\n"); exit(1); } diff --git a/tests/pick-test.sh b/tests/pick-test.sh index 661b04cc..84adc2b7 100755 --- a/tests/pick-test.sh +++ b/tests/pick-test.sh @@ -10,7 +10,7 @@ strip() { run_test() { local _cause= _diff=$stdout - env $env tests/pick-test -k $input -- $args <$stdin >$out 2>&1; e=$? + env $env "$1" -b "$PICK" -k $input -- $args <$stdin >$out 2>&1; e=$? if [ -s "$stdout" ] && ! cmp -s "$stdout" "$out"; then _cause="wrong output" @@ -30,21 +30,20 @@ run_test() { } usage() { - echo "usage: sh tests/pick-test.sh [-e env] file ..." 1>&2 + echo "usage: sh pick-test.sh -b binary file ..." 1>&2 exit 1 } -# Default environment applied to all test cases. -defenv= +picktest="" -while getopts "e:" opt; do +while getopts "b:" opt; do case "$opt" in - e) defenv="${defenv} ${OPTARG}";; + b) picktest="$OPTARG";; *) usage;; esac done shift $((OPTIND - 1)) -[ $# -eq 0 ] && usage +{ [ $# -eq 0 ] || [ -z "$picktest" ]; } && usage nerr=0 @@ -60,7 +59,7 @@ for f; do while IFS=: read -r key val; do if [ -z "$key" ]; then env="${defenv} ${env}" - run_test || nerr=$((nerr + 1)) + run_test "$picktest" || nerr=$((nerr + 1)) # Reset environment. args= description= env= exit= keys=