Imported Upstream version 4.4
[platform/upstream/make.git] / tests / scripts / features / temp_stdin
1 #                                                              -*-mode: perl-*-
2
3 $description = "Test handling of temporary file created from stdin.";
4
5 # These tests rely on the test_driver checking for leftover temporary content
6
7 create_file('input.mk', "world:=1\n");
8 create_file('bye.mk', "moon:=2\n");
9
10 # sv 62118,62145.
11 # Test that makes leaves no temp file when make code is piped to stdin and -v,
12 # -h or an invalid option is specified.
13 my @opts = ('-v', '-h', '--nosuchopt');
14 my @exit_codes = (0, 0, 512);
15 for my $i (0 .. $#opts) {
16     close(STDIN);
17     open(STDIN, "<", 'input.mk') || die "$0: cannot open input.mk for reading: $!";
18     run_make_test(q!
19 all:; $(info hello world)
20 !,
21                   "$opts[$i] -f-", "/uilt for /", $exit_codes[$i]);
22 }
23
24 # sv 62118,62145.
25 # Test that a stdin temp file is removed.
26 close(STDIN);
27 open(STDIN, "<", 'input.mk') || die "$0: cannot open input.mk for reading: $!";
28 run_make_test(q!
29 all:; $(info world=$(world))
30 !,
31               '-f-', "world=1\n#MAKE#: 'all' is up to date.\n");
32
33 # sv 62118,62145.
34 # Test that a stdin temp file is removed, even when make re-execs.
35 # Also test that make honors TMPDIR to create the temp file.
36 # Ensure touching bye.mk causes re-exec.
37 &utouch(-600, 'bye.mk');
38 close(STDIN);
39 open(STDIN, "<", 'input.mk') || die "$0: cannot open input.mk for reading: $!";
40 run_make_test(q!
41 include bye.mk
42 all:; $(info hello)
43 $(MAKE_RESTARTS)bye.mk: force; touch $@
44 force:
45 !,
46               '-R --debug=b -f-', "/Re-executing.+?--temp-stdin=\Q$temppath\E/");
47
48 if ($port_type eq 'UNIX') {
49 # POSIX doesn't require sh to set PPID so test this
50 my $cmd = create_command();
51 add_options($cmd, '-f', '/dev/null', '-E', q!all:;@echo $$PPID!);
52 my $fout = 'ppidtest.out';
53 run_command_with_output($fout, @$cmd);
54 $_ = read_file_into_string($fout);
55 chomp($_);
56 if (/^[0-9]+$/) {
57 use POSIX ();
58
59 # sv 63157.
60 # Test that make removes the temporary file which holds make code from stdin,
61 # even when a signal is received.
62 # include bye.mk and bye.mk: rule is needed to cause make to keep the temporary
63 # file for re-exec. Without re-exec make will remove the file before the signal
64 # arrives.
65 # sleep is needed to let make write its "... Terminated" message to the log
66 # file.
67 &utouch(-600, 'bye.mk');
68 close(STDIN);
69 open(STDIN, "<", 'input.mk') || die "$0: cannot open input.mk for reading: $!";
70 run_make_test(q!
71 include bye.mk
72 pid:=$(shell echo $$PPID)
73 all:;
74 bye.mk: force; @kill -TERM $(pid) && sleep 16
75 force:
76 !, '-f-', '/#MAKE#: \*\*\* \[#MAKEFILE#:5: bye.mk] Terminated/', POSIX::SIGTERM);
77 }
78 unlink($fout);
79
80 # sv 62118,62145.
81 # Test that a stdin temp file is removed, when execvp fails to re-exec make.
82 # In order to cause execvp to fail, copy the tested make binary to the temp
83 # directory and take away the 'x' bit.
84 use File::Spec;
85 use File::Copy;
86
87 my $tmakedir = File::Spec->catfile($cwdpath, 'tmakedir');
88 mkdir($tmakedir, 0770);
89 my $makecopy = File::Spec->catfile($tmakedir, 'make');
90 copy("$mkpath", $makecopy);
91 # Set file mode bits, because perl copy won't.
92 chmod 0700, $makecopy;
93
94 my @make_orig = @make_command;
95 @make_command = ($makecopy);
96
97 # Ensure touching bye.mk causes re-exec.
98 &utouch(-600, 'bye.mk');
99 close(STDIN);
100 open(STDIN, "<", 'input.mk') || die "$0: cannot open input.mk for reading: $!";
101 run_make_test("
102 include bye.mk
103 all:; \$(info hello)
104 \$(MAKE_RESTARTS)bye.mk: force; touch \$@ && chmod u-x $makecopy
105 force:
106 ",
107               "-f-", "touch bye.mk && chmod u-x $makecopy\nmake: $makecopy: $ERR_nonexe_file\n", 32512);
108
109 @make_command = @make_orig;
110 unlink($makecopy);
111 rmdir($tmakedir);
112 }
113
114 close(STDIN);
115 unlink('input.mk', 'bye.mk');
116
117 # This tells the test driver that the perl test script executed properly.
118 1;