Imported Upstream version 4.0
[platform/upstream/make.git] / tests / scripts / functions / file
1 #                                                                    -*-perl-*-
2
3 $description = 'Test the $(file ...) function.';
4
5 # Test > and >>
6 run_make_test(q!
7 define A
8 a
9 b
10 endef
11 B = c d
12 $(file >file.out,$(A))
13 $(foreach L,$(B),$(file >>     file.out,$L))
14 x:;@echo hi; cat file.out
15 !,
16               '', "hi\na\nb\nc\nd");
17
18 unlink('file.out');
19
20 # Test >> to a non-existent file
21 run_make_test(q!
22 define A
23 a
24 b
25 endef
26 $(file     >>     file.out,$(A))
27 x:;@cat file.out
28 !,
29               '', "a\nb");
30
31 unlink('file.out');
32
33 # Test > to a read-only file
34 touch('file.out');
35 chmod(0444, 'file.out');
36
37 # Find the error that will be printed
38 # This seems complicated, but we need the message from the C locale
39 my $loc = undef;
40 if ($has_POSIX) {
41     $loc = POSIX::setlocale(POSIX::LC_MESSAGES);
42     POSIX::setlocale(POSIX::LC_MESSAGES, 'C');
43 }
44 my $e;
45 open(my $F, '>', 'file.out') and die "Opened read-only file!\n";
46 $e = "$!";
47 $loc and POSIX::setlocale(POSIX::LC_MESSAGES, $loc);
48
49 run_make_test(q!
50 define A
51 a
52 b
53 endef
54 $(file     >     file.out,$(A))
55 x:;@cat file.out
56 !,
57               '', "#MAKEFILE#:6: *** open: file.out: $e.  Stop.",
58               512);
59
60 unlink('file.out');
61
62 # Use variables for operator and filename
63 run_make_test(q!
64 define A
65 a
66 b
67 endef
68 OP = >
69 FN = file.out
70 $(file     $(OP)     $(FN),$(A))
71 x:;@cat file.out
72 !,
73               '', "a\nb");
74
75 unlink('file.out');
76
77 # Don't add newlines if one already exists
78 run_make_test(q!
79 define A
80 a
81 b
82
83 endef
84 $(file >file.out,$(A))
85 x:;@cat file.out
86 !,
87               '', "a\nb");
88
89 unlink('file.out');
90
91 # Empty text
92 run_make_test(q!
93 $(file >file.out,)
94 $(file >>file.out,)
95 x:;@cat file.out
96 !,
97               '', "\n\n");
98
99 unlink('file.out');
100
101 1;