xref: /freebsd/usr.bin/mktemp/tests/mktemp_test.sh (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2022 Klara Systems
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD$
28
29atf_test_case tmpdir_env
30tmpdir_env_body()
31{
32
33	tmpdir="$PWD"
34
35	atf_check -o match:"^$tmpdir/foo\..+$" \
36	    env TMPDIR="$tmpdir" mktemp -t foo
37}
38
39atf_test_case tmpdir_pflag
40tmpdir_pflag_body()
41{
42
43	mkdir tmp_p tmp_env
44
45	tmpdir="$PWD/tmp_env"
46	export TMPDIR="$tmpdir"
47
48	pflag="$PWD/tmp_p"
49
50	# Basic usage: just -p specified
51	atf_check -o match:"^$pflag/tmp\..+$" \
52	    env -u TMPDIR mktemp -p "$pflag"
53	atf_check -o match:"^$pflag/tmp\..+$" \
54	    env TMPDIR="$tmpdir" mktemp -p "$pflag"
55
56	# -p with a list of names
57	atf_check -o ignore env -u TMPDIR mktemp -p "$pflag" x y z
58	atf_check test -f "$pflag/x"
59	atf_check test -f "$pflag/y"
60	atf_check test -f "$pflag/z"
61
62	# Checking --tmpdir usage, which should defer to $TMPDIR followed by
63	# /tmp with no value specified.
64	atf_check -o match:"^/tmp/foo\..+$" \
65	    env -u TMPDIR mktemp --tmpdir -t foo
66	atf_check -o match:"^$tmpdir/foo\..+$" \
67	    env TMPDIR="$tmpdir" mktemp --tmpdir -t foo
68
69	# Finally, combined -p -t
70	atf_check -o match:"^$pflag/foo\..+$" \
71	    env -u TMPDIR mktemp -p "$pflag" -t foo
72	atf_check -o match:"^$pflag/foo\..+$" \
73	    env TMPDIR="$tmpdir" mktemp -p "$pflag" -t foo
74}
75
76atf_test_case tmpdir_pflag_dir
77tmpdir_pflag_dir_body()
78{
79
80	tmpdir="$PWD"
81	atf_check -o save:tmpname \
82	    env -u TMPDIR mktemp -d -p "$tmpdir" -t foo
83
84	# Better diagnostics when using -o match: + cat rather than grep.
85	atf_check -o match:"^$tmpdir/foo\..+$" cat tmpname
86	cdir=$(cat tmpname)
87
88	atf_check test -d "$cdir"
89
90	atf_check -o match:"^$tmpdir/footmp$" \
91	    env -u TMPDIR mktemp -d -p "$tmpdir" footmp
92	atf_check test -d "$tmpdir/footmp"
93}
94
95atf_test_case tmpdir_pflag_noarg
96tmpdir_pflag_noarg_body()
97{
98
99	# Without -t, this time; this introduces $TMPDIR without having to use
100	# it.
101	tmpdir="$PWD"
102	atf_check -o save:tmpname \
103	    env TMPDIR="$tmpdir" mktemp --tmpdir foo.XXXXXXXX
104	atf_check -o match:"^$tmpdir/foo\..+$" cat tmpname
105
106	# An empty string gets the same treatment.
107	atf_check -o save:tmpname \
108	    env TMPDIR="$tmpdir" mktemp -p '' foo.XXXXXXXX
109	atf_check -o match:"^$tmpdir/foo\..+$" cat tmpname
110}
111
112atf_test_case tmpdir_tflag_oneslash
113tmpdir_tflag_oneslash_body()
114{
115
116	tmpdir="$PWD"
117
118	# Provided a trailing slash, we shouldn't end up with two trailing
119	# slashes.
120	atf_check -o save:tmpname \
121	    env TMPDIR="$tmpdir/" mktemp -t foo
122	atf_check -o match:"^$tmpdir/foo\..+$" cat tmpname
123}
124
125atf_init_test_cases()
126{
127	atf_add_test_case tmpdir_env
128	atf_add_test_case tmpdir_pflag
129	atf_add_test_case tmpdir_pflag_dir
130	atf_add_test_case tmpdir_pflag_noarg
131	atf_add_test_case tmpdir_tflag_oneslash
132}
133