| 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 |
# Read an empty file. |
| 122 |
touch("file.out"); |
| 123 |
run_make_test(q!# empty file |
| 124 |
X1 := x$(file <file.out)y |
| 125 |
x:;@echo '$(X1)' |
| 126 |
!, |
| 127 |
'', "xy\n"); |
| 128 |
|
| 129 |
unlink('file.out'); |
| 130 |
|
| 131 |
# Read a file whose full contents is a newline. |
| 132 |
create_file('file.out', "\n"); |
| 133 |
run_make_test(q!# <nl> |
| 134 |
X1 := x$(file <file.out)y |
| 135 |
x:;@echo '$(X1)' |
| 136 |
!, |
| 137 |
'', "xy\n"); |
| 138 |
|
| 139 |
unlink('file.out'); |
| 140 |
|
| 141 |
# Read a file which does not end with a newline. |
| 142 |
create_file('file.out', "hello"); |
| 143 |
# echo prints a trailig newline, because run_make_test appends a newline. |
| 144 |
run_make_test(q!# hello |
| 145 |
X1 := x$(file <file.out)y |
| 146 |
x:;@echo $(X1) |
| 147 |
!, |
| 148 |
'', "xhelloy\n"); |
| 149 |
|
| 150 |
unlink('file.out'); |
| 151 |
|
| 152 |
# Read a file which ends with a newline. |
| 153 |
create_file('file.out', "hello\n"); |
| 154 |
# echo prints a trailig newline, because run_make_test appends a newline. |
| 155 |
run_make_test(q!# hello<nl> |
| 156 |
X1 := x$(file <file.out)y |
| 157 |
x:;@echo '$(X1)' |
| 158 |
!, |
| 159 |
'', "xhelloy\n"); |
| 160 |
|
| 161 |
|
| 162 |
unlink('file.out'); |
| 163 |
|
| 164 |
# Read a file which ends with multiple newlines. |
| 165 |
create_file('file.out', "hello\n\n"); |
| 166 |
run_make_test(q!# hello<nl><nl> |
| 167 |
X1 := x$(file <file.out)y |
| 168 |
export X1 |
| 169 |
x:;@echo "$$X1" |
| 170 |
!, |
| 171 |
'', "xhello\ny\n"); |
| 172 |
|
| 173 |
unlink('file.out'); |
| 174 |
|
| 175 |
# Read a file whose contents exceed 200 bytes. |
| 176 |
# 200 is the initial size of variable_buffer. |
| 177 |
# File bigger than 200 bytes causes a realloc. |
| 178 |
# The size of the file in this test not only exceeds 200 bytes, it exceeds 65k. |
| 179 |
# Use $(info ...) here to avoid command line limitations |
| 180 |
|
| 181 |
my $s = "hello world, hello world, hello world, hello world, hello world"; |
| 182 |
my $answer = $s x 2000; |
| 183 |
create_file('file.out', $answer); |
| 184 |
run_make_test(q!# <hugestring> |
| 185 |
X1 := x$(file <file.out)y |
| 186 |
x:;@$(info $(X1)) |
| 187 |
!, |
| 188 |
'', "x${answer}y\n#MAKE#: 'x' is up to date.\n"); |
| 189 |
|
| 190 |
unlink('file.out'); |
| 191 |
|
| 192 |
# Reading from non-existent file |
| 193 |
run_make_test(q! |
| 194 |
X1 := $(file <file.out) |
| 195 |
x:;@echo '$(X1)'; |
| 196 |
!, |
| 197 |
'', "\n"); |
| 198 |
|
| 199 |
# Extra arguments in read mode |
| 200 |
run_make_test(q! |
| 201 |
X1 := $(file <file.out,foo) |
| 202 |
x:;@echo '$(X1)'; |
| 203 |
!, |
| 204 |
'', "#MAKEFILE#:2: *** file: too many arguments. Stop.\n", 512); |
| 205 |
|
| 206 |
|
| 207 |
# Missing filename |
| 208 |
run_make_test('$(file >)', '', |
| 209 |
"#MAKEFILE#:1: *** file: missing filename. Stop.\n", 512); |
| 210 |
|
| 211 |
run_make_test('$(file >>)', '', |
| 212 |
"#MAKEFILE#:1: *** file: missing filename. Stop.\n", 512); |
| 213 |
|
| 214 |
run_make_test('$(file <)', '', |
| 215 |
"#MAKEFILE#:1: *** file: missing filename. Stop.\n", 512); |
| 216 |
|
| 217 |
# Bad call |
| 218 |
|
| 219 |
run_make_test('$(file foo)', '', |
| 220 |
"#MAKEFILE#:1: *** file: invalid file operation: foo. Stop.\n", 512); |
| 221 |
|
| 222 |
# SV 17448: check whitespace |
| 223 |
create_file('out1', "1\n"); |
| 224 |
|
| 225 |
run_make_test(q! |
| 226 |
all:;$(info $(file < out1 )) |
| 227 |
!, |
| 228 |
'', "1\n#MAKE#: 'all' is up to date."); |
| 229 |
|
| 230 |
unlink('out1'); |
| 231 |
|
| 232 |
1; |