xref: /freebsd/usr.bin/mktemp/tests/mktemp_test.sh (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
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
28atf_test_case tmpdir_env
29tmpdir_env_body()
30{
31
32	tmpdir="$PWD"
33
34	atf_check -o match:"^$tmpdir/foo\..+$" \
35	    env TMPDIR="$tmpdir" mktemp -t foo
36}
37
38atf_test_case tmpdir_pflag
39tmpdir_pflag_body()
40{
41
42	mkdir tmp_p tmp_env
43
44	tmpdir="$PWD/tmp_env"
45	export TMPDIR="$tmpdir"
46
47	pflag="$PWD/tmp_p"
48
49	# Basic usage: just -p specified
50	atf_check -o match:"^$pflag/tmp\..+$" \
51	    env -u TMPDIR mktemp -p "$pflag"
52	atf_check -o match:"^$pflag/tmp\..+$" \
53	    env TMPDIR="$tmpdir" mktemp -p "$pflag"
54
55	# -p with a list of names
56	atf_check -o ignore env -u TMPDIR mktemp -p "$pflag" x y z
57	atf_check test -f "$pflag/x"
58	atf_check test -f "$pflag/y"
59	atf_check test -f "$pflag/z"
60
61	# Checking --tmpdir usage, which should defer to $TMPDIR followed by
62	# /tmp with no value specified.
63	atf_check -o match:"^/tmp/foo\..+$" \
64	    env -u TMPDIR mktemp --tmpdir -t foo
65	atf_check -o match:"^$tmpdir/foo\..+$" \
66	    env TMPDIR="$tmpdir" mktemp --tmpdir -t foo
67
68	# Finally, combined -p -t
69	atf_check -o match:"^$pflag/foo\..+$" \
70	    env -u TMPDIR mktemp -p "$pflag" -t foo
71	atf_check -o match:"^$pflag/foo\..+$" \
72	    env TMPDIR="$tmpdir" mktemp -p "$pflag" -t foo
73}
74
75atf_test_case tmpdir_pflag_dir
76tmpdir_pflag_dir_body()
77{
78
79	tmpdir="$PWD"
80	atf_check -o save:tmpname \
81	    env -u TMPDIR mktemp -d -p "$tmpdir" -t foo
82
83	# Better diagnostics when using -o match: + cat rather than grep.
84	atf_check -o match:"^$tmpdir/foo\..+$" cat tmpname
85	cdir=$(cat tmpname)
86
87	atf_check test -d "$cdir"
88
89	atf_check -o match:"^$tmpdir/footmp$" \
90	    env -u TMPDIR mktemp -d -p "$tmpdir" footmp
91	atf_check test -d "$tmpdir/footmp"
92}
93
94atf_test_case tmpdir_pflag_noarg
95tmpdir_pflag_noarg_body()
96{
97
98	# Without -t, this time; this introduces $TMPDIR without having to use
99	# it.
100	tmpdir="$PWD"
101	atf_check -o save:tmpname \
102	    env TMPDIR="$tmpdir" mktemp --tmpdir foo.XXXXXXXX
103	atf_check -o match:"^$tmpdir/foo\..+$" cat tmpname
104
105	# An empty string gets the same treatment.
106	atf_check -o save:tmpname \
107	    env TMPDIR="$tmpdir" mktemp -p '' foo.XXXXXXXX
108	atf_check -o match:"^$tmpdir/foo\..+$" cat tmpname
109}
110
111atf_test_case tmpdir_tflag_oneslash
112tmpdir_tflag_oneslash_body()
113{
114
115	tmpdir="$PWD"
116
117	# Provided a trailing slash, we shouldn't end up with two trailing
118	# slashes.
119	atf_check -o save:tmpname \
120	    env TMPDIR="$tmpdir/" mktemp -t foo
121	atf_check -o match:"^$tmpdir/foo\..+$" cat tmpname
122}
123
124atf_init_test_cases()
125{
126	atf_add_test_case tmpdir_env
127	atf_add_test_case tmpdir_pflag
128	atf_add_test_case tmpdir_pflag_dir
129	atf_add_test_case tmpdir_pflag_noarg
130	atf_add_test_case tmpdir_tflag_oneslash
131}
132