git: 96da41b6dbf3 - main - lorder: Add unit tests.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Wed, 28 Feb 2024 15:44:34 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=96da41b6dbf383436018f11ad8a672faab2d3789

commit 96da41b6dbf383436018f11ad8a672faab2d3789
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2024-02-28 15:37:41 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-02-28 15:37:41 +0000

    lorder: Add unit tests.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    allanjude
    Differential Revision:  https://reviews.freebsd.org/D44134
---
 etc/mtree/BSD.tests.dist            |   2 +
 usr.bin/lorder/Makefile             |   4 ++
 usr.bin/lorder/tests/Makefile       |   4 ++
 usr.bin/lorder/tests/lorder_test.sh | 111 ++++++++++++++++++++++++++++++++++++
 4 files changed, 121 insertions(+)

diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist
index 2f7d74cb58f8..64b5f88ff2cc 100644
--- a/etc/mtree/BSD.tests.dist
+++ b/etc/mtree/BSD.tests.dist
@@ -1137,6 +1137,8 @@
         ..
         lockf
         ..
+        lorder
+        ..
         m4
         ..
         mkimg
diff --git a/usr.bin/lorder/Makefile b/usr.bin/lorder/Makefile
index a7bf77dc263a..5dc868f96c7e 100644
--- a/usr.bin/lorder/Makefile
+++ b/usr.bin/lorder/Makefile
@@ -1,5 +1,9 @@
+.include <src.opts.mk>
 
 SCRIPTS=lorder.sh
 MAN=	lorder.1
 
+HAS_TESTS=
+SUBDIR.${MK_TESTS}=	tests
+
 .include <bsd.prog.mk>
diff --git a/usr.bin/lorder/tests/Makefile b/usr.bin/lorder/tests/Makefile
new file mode 100644
index 000000000000..21207f413a8d
--- /dev/null
+++ b/usr.bin/lorder/tests/Makefile
@@ -0,0 +1,4 @@
+PACKAGE=	tests
+ATF_TESTS_SH=	lorder_test
+
+.include <bsd.test.mk>
diff --git a/usr.bin/lorder/tests/lorder_test.sh b/usr.bin/lorder/tests/lorder_test.sh
new file mode 100644
index 000000000000..449cda29f825
--- /dev/null
+++ b/usr.bin/lorder/tests/lorder_test.sh
@@ -0,0 +1,111 @@
+#
+# Copyright (c) 2024 Klara, Inc.
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+
+atf_test_case noargs
+noargs_head() {
+	atf_set descr "No arguments"
+}
+noargs_body() {
+	atf_check -s exit:1 -e match:"^usage:" \
+		  lorder
+}
+
+atf_test_case onearg
+onearg_head() {
+	atf_set descr "One argument"
+}
+onearg_body() {
+	echo "void a(void) { }" >a.c
+	cc -o a.o -c a.c
+	echo "a.o a.o" >output
+	atf_check -o file:output \
+		  lorder *.o
+}
+
+atf_test_case dashdash
+dashdash_head() {
+	atf_set descr "One argument"
+}
+dashdash_body() {
+	echo "void a(void) { }" >a.c
+	cc -o a.o -c a.c
+	echo "a.o a.o" >output
+	atf_check -o file:output \
+		  lorder -- *.o
+}
+
+atf_test_case nonexistent
+nonexistent_head() {
+	atf_set descr "Nonexistent file"
+}
+nonexistent_body() {
+	atf_check -s not-exit:0 -e match:"No such file" -o empty \
+		  lorder nonexistent.o
+}
+
+atf_test_case invalid
+invalid_head() {
+	atf_set descr "Invalid file"
+}
+invalid_body() {
+	echo "not an object file" >invalid.o
+	atf_check -s not-exit:0 -e match:"File format not" -o empty \
+		  lorder invalid.o
+}
+
+atf_test_case objects
+objects_head() {
+	atf_set descr "Order objects"
+}
+objects_body() {
+	echo "void a(void) { }" >a.c
+	echo "void a(void); void b(void) { a(); }" >b.c
+	echo "void b(void); void c(void) { b(); }" >c.c
+	for n in a b c ; do
+		cc -o $n.o -c $n.c
+		echo "$n.o $n.o"
+	done >output
+	echo "b.o a.o" >>output
+	echo "c.o b.o" >>output
+	atf_check -o file:output \
+		  lorder *.o
+}
+
+atf_test_case archives
+archives_head() {
+	atf_set descr "Order archives"
+}
+archives_body() {
+	echo "void a(void) { }" >a.c
+	echo "void a(void); void b(void) { a(); }" >b.c
+	echo "void b(void); void c(void) { b(); }" >c.c
+	echo "void e(void); void d(void) { e(); }" >d.c
+	echo "void d(void); void e(void) { d(); }" >e.c
+	for n in a b c d e ; do
+		cc -o $n.o -c $n.c
+	done
+	for n in a b c ; do
+		ar -crs $n.a $n.o
+		echo "$n.a $n.a"
+	done >output
+	ar -crs z.a d.o e.o
+	echo "z.a z.a" >>output
+	echo "b.a a.a" >>output
+	echo "c.a b.a" >>output
+	atf_check -o file:output \
+		  lorder *.a
+}
+
+atf_init_test_cases()
+{
+	atf_add_test_case noargs
+	atf_add_test_case onearg
+	atf_add_test_case dashdash
+	atf_add_test_case nonexistent
+	atf_add_test_case invalid
+	atf_add_test_case objects
+	atf_add_test_case archives
+}