eaabd3ac29134959681d0dceef6e764a7ce3532d
[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 > with no content
34 run_make_test(q!
35 $(file >4touch)
36 .PHONY:x
37 x:;@cat 4touch
38 !,
39               '', '');
40
41 # Test >> with no content
42 run_make_test(q!
43 $(file >>4touch)
44 .PHONY:x
45 x:;@cat 4touch
46 !,
47               '', '');
48 unlink('4touch');
49
50 # Test > to a read-only file
51 if (defined $ERR_read_only_file) {
52     touch('file.out');
53     chmod(0444, 'file.out');
54
55     run_make_test(q!
56 define A
57 a
58 b
59 endef
60 $(file     >     file.out,$(A))
61 x:;@cat file.out
62 !,
63                   '', "#MAKEFILE#:6: *** open: file.out: $ERR_read_only_file.  Stop.",
64                   512);
65
66     unlink('file.out');
67 }
68
69 # Use variables for operator and filename
70 run_make_test(q!
71 define A
72 a
73 b
74 endef
75 OP = >
76 FN = file.out
77 $(file     $(OP)     $(FN),$(A))
78 x:;@cat file.out
79 !,
80               '', "a\nb");
81
82 unlink('file.out');
83
84 # Don't add newlines if one already exists
85 run_make_test(q!
86 define A
87 a
88 b
89
90 endef
91 $(file >file.out,$(A))
92 x:;@cat file.out
93 !,
94               '', "a\nb");
95
96 unlink('file.out');
97
98 # Empty text
99 run_make_test(q!
100 $(file >file.out,)
101 $(file >>file.out,)
102 x:;@cat file.out
103 !,
104               '', "\n\n");
105
106 unlink('file.out');
107
108 # Reading files
109 run_make_test(q!
110 $(file >file.out,A = foo)
111 X1 := $(file <file.out)
112 $(file >>file.out,B = bar)
113 $(eval $(file <file.out))
114
115 x:;@echo '$(X1)'; echo '$(A)'; echo '$(B)'
116 !,
117               '', "A = foo\nfoo\nbar\n");
118
119 unlink('file.out');
120
121 # Reading from non-existent file
122 run_make_test(q!
123 X1 := $(file <file.out)
124 x:;@echo '$(X1)';
125 !,
126               '', "\n");
127
128 # Extra arguments in read mode
129 run_make_test(q!
130 X1 := $(file <file.out,foo)
131 x:;@echo '$(X1)';
132 !,
133               '', "#MAKEFILE#:2: *** file: too many arguments.  Stop.\n", 512);
134
135
136 # Missing filename
137 run_make_test('$(file >)', '',
138               "#MAKEFILE#:1: *** file: missing filename.  Stop.\n", 512);
139
140 run_make_test('$(file >>)', '',
141               "#MAKEFILE#:1: *** file: missing filename.  Stop.\n", 512);
142
143 run_make_test('$(file <)', '',
144               "#MAKEFILE#:1: *** file: missing filename.  Stop.\n", 512);
145
146 # Bad call
147
148 run_make_test('$(file foo)', '',
149               "#MAKEFILE#:1: *** file: invalid file operation: foo.  Stop.\n", 512);
150
151 1;