This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Generate the virtual entries at Config.pm build time, as they
[perl5.git] / lib / Config.t
CommitLineData
a48f8c77
MS
1#!./perl
2
41aba5b7
JH
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require "./test.pl";
7}
8
8d871aab 9plan 'no_plan';
41aba5b7
JH
10
11use_ok('Config');
12
13# Some (safe?) bets.
14
484fdf61 15ok(keys %Config > 500, "Config has more than 500 entries");
41aba5b7
JH
16
17ok(each %Config);
18
19is($Config{PERL_REVISION}, 5, "PERL_REVISION is 5");
20
d10bb151
MS
21# Check that old config variable names are aliased to their new ones.
22my %grandfathers = ( PERL_VERSION => 'PATCHLEVEL',
23 PERL_SUBVERSION => 'SUBVERSION',
24 PERL_CONFIG_SH => 'CONFIG'
25 );
26while( my($new, $old) = each %grandfathers ) {
27 isnt($Config{$new}, undef, "$new is defined");
28 is($Config{$new}, $Config{$old}, "$new is aliased to $old");
29}
30
41aba5b7
JH
31ok( exists $Config{cc}, "has cc");
32
33ok( exists $Config{ccflags}, "has ccflags");
34
35ok(!exists $Config{python}, "has no python");
36
37ok( exists $Config{d_fork}, "has d_fork");
38
39ok(!exists $Config{d_bork}, "has no d_bork");
40
9829f96e 41like($Config{ivsize}, qr/^(4|8)$/, "ivsize is 4 or 8 (it is $Config{ivsize})");
7956ade2 42
8d871aab 43# byteorder is virtual, but it has rules.
41aba5b7 44
9829f96e
NC
45like($Config{byteorder}, qr/^(1234|4321|12345678|87654321)$/,
46 "byteorder is 1234 or 4321 or 12345678 or 87654321 "
47 . "(it is $Config{byteorder})");
41aba5b7 48
9829f96e
NC
49is(length $Config{byteorder}, $Config{ivsize},
50 "byteorder is as long as ivsize (which is $Config{ivsize})");
41aba5b7
JH
51
52# ccflags_nolargefiles is virtual, too.
53
54ok(exists $Config{ccflags_nolargefiles}, "has ccflags_nolargefiles");
55
56# Utility functions.
57
a48f8c77
MS
58{
59 # make sure we can export what we say we can export.
60 package Foo;
61 my @exports = qw(myconfig config_sh config_vars config_re);
62 Config->import(@exports);
63 foreach my $func (@exports) {
64 ::ok( __PACKAGE__->can($func), "$func exported" );
65 }
6664971e 66}
41aba5b7 67
8468119f
NC
68like(Config::myconfig(), qr/osname=\Q$Config{osname}\E/, "myconfig");
69like(Config::config_sh(), qr/osname='\Q$Config{osname}\E'/, "config_sh");
70like(Config::config_sh(), qr/byteorder='[1-8]+'/,
71 "config_sh has a valid byteorder");
8d871aab
NC
72foreach my $line (Config::config_re('c.*')) {
73 like($line, qr/^c.*?=.*$/, 'config_re' );
74}
a48f8c77 75
41aba5b7
JH
76my $out = tie *STDOUT, 'FakeOut';
77
307dc113 78Config::config_vars('cc'); # non-regex test of essential cfg-var
41aba5b7
JH
79my $out1 = $$out;
80$out->clear;
81
307dc113 82Config::config_vars('d_bork'); # non-regex, non-existent cfg-var
41aba5b7
JH
83my $out2 = $$out;
84$out->clear;
85
307dc113 86Config::config_vars('PERL_API_.*'); # regex, tagged multi-line answer
4a305f6a
JC
87my $out3 = $$out;
88$out->clear;
89
307dc113 90Config::config_vars('PERL_API_.*:'); # regex, tagged single-line answer
4a305f6a
JC
91my $out4 = $$out;
92$out->clear;
41aba5b7 93
307dc113 94Config::config_vars(':PERL_API_.*:'); # regex, non-tagged single-line answer
4a305f6a
JC
95my $out5 = $$out;
96$out->clear;
97
307dc113 98Config::config_vars(':PERL_API_.*'); # regex, non-tagged multi-line answer
0c6e7072
RB
99my $out6 = $$out;
100$out->clear;
101
307dc113
JC
102Config::config_vars('PERL_API_REVISION.*:'); # regex, tagged
103my $out7 = $$out;
104$out->clear;
105
9829f96e
NC
106# regex, non-tagged multi-line answer
107Config::config_vars(':PERL_API_REVISION.*');
307dc113
JC
108my $out8 = $$out;
109$out->clear;
110
111Config::config_vars('PERL_EXPENSIVE_.*:'); # non-matching regex
112my $out9 = $$out;
113$out->clear;
114
115Config::config_vars('?flags'); # bogus regex, no explicit warning !
116my $out10 = $$out;
117$out->clear;
118
4a305f6a 119untie *STDOUT;
307dc113
JC
120
121like($out1, qr/^cc='\Q$Config{cc}\E';/, "found config_var cc");
122like($out2, qr/^d_bork='UNKNOWN';/, "config_var d_bork is UNKNOWN");
123
124# test for leading, trailing colon effects
125is(scalar split(/;\n/, $out3), 3, "3 lines found");
126is(scalar split(/;\n/, $out6), 3, "3 lines found");
127
128is($out4 =~ /(;\n)/s, '', "trailing colon gives 1-line response: $out4");
129is($out5 =~ /(;\n)/s, '', "trailing colon gives 1-line response: $out5");
130
131is(scalar split(/=/, $out3), 4, "found 'tag='");
132is(scalar split(/=/, $out4), 4, "found 'tag='");
133
134my @api;
135
136my @rev = @Config{qw(PERL_API_REVISION PERL_API_VERSION PERL_API_SUBVERSION)};
137
138print ("# test tagged responses, multi-line and single-line\n");
139foreach $api ($out3, $out4) {
140 @api = $api =~ /PERL_API_(\w+)=(.*?)(?:;\n|\s)/mg;
141 is($api[0], "REVISION", "REVISION tag");
142 is($api[4], "VERSION", "VERSION tag");
143 is($api[2], "SUBVERSION", "SUBVERSION tag");
144 is($api[1], "'$rev[0]'", "REVISION is $rev[0]");
145 is($api[5], "'$rev[1]'", "VERSION is $rev[1]");
146 is($api[3], "'$rev[2]'", "SUBVERSION is $rev[2]");
147}
148
149print("# test non-tagged responses, multi-line and single-line\n");
150foreach $api ($out5, $out6) {
151 @api = split /(?: |;\n)/, $api;
152 is($api[0], "'$rev[0]'", "revision is $rev[0]");
153 is($api[2], "'$rev[1]'", "version is $rev[1]");
154 is($api[1], "'$rev[2]'", "subversion is $rev[2]");
155}
156
157# compare to each other, the outputs for trailing, leading colon
158$out7 =~ s/ $//;
159is("$out7;\n", "PERL_API_REVISION=$out8", "got expected diffs");
160
161like($out9, qr/\bnot\s+found\b/, "$out9 - perl is FREE !");
162like($out10, qr/\bnot\s+found\b/, "config_vars with invalid regexp");
0c6e7072 163
41aba5b7
JH
164# Read-only.
165
7956ade2 166undef $@;
41aba5b7
JH
167eval { $Config{d_bork} = 'borkbork' };
168like($@, qr/Config is read-only/, "no STORE");
169
7956ade2
JH
170ok(!exists $Config{d_bork}, "still no d_bork");
171
172undef $@;
41aba5b7
JH
173eval { delete $Config{d_fork} };
174like($@, qr/Config is read-only/, "no DELETE");
175
7956ade2
JH
176ok( exists $Config{d_fork}, "still d_fork");
177
178undef $@;
41aba5b7
JH
179eval { %Config = () };
180like($@, qr/Config is read-only/, "no CLEAR");
181
7956ade2
JH
182ok( exists $Config{d_fork}, "still d_fork");
183
059ca955
RGS
184{
185 package FakeOut;
41aba5b7 186
059ca955
RGS
187 sub TIEHANDLE {
188 bless(\(my $text), $_[0]);
189 }
41aba5b7 190
059ca955
RGS
191 sub clear {
192 ${ $_[0] } = '';
193 }
41aba5b7 194
059ca955
RGS
195 sub PRINT {
196 my $self = shift;
197 $$self .= join('', @_);
198 }
41aba5b7
JH
199}
200
059ca955
RGS
201# Signal-related variables
202# (this is actually a regression test for Configure.)
203
b25be8c8
JH
204is($Config{sig_num_init} =~ tr/,/,/, $Config{sig_size}, "sig_num_init size");
205is($Config{sig_name_init} =~ tr/,/,/, $Config{sig_size}, "sig_name_init size");
8468119f
NC
206
207# Test the troublesome virtual stuff
a6ea9771
NC
208my @virtual = qw(byteorder ccflags_nolargefiles ldflags_nolargefiles
209 libs_nolargefiles libswanted_nolargefiles);
a644ec48 210
e9a5b206
NC
211# Also test that the first entry in config.sh is found correctly. Currently
212# there is special casing code for this
213my ($first) = Config::config_sh() =~ /^(\S+)=/m;
214die "Can't find first entry in Config::config_sh()" unless defined $first;
215print "# First entry is '$first'\n";
216
217foreach my $pain ($first, @virtual) {
a644ec48 218 # No config var is named with anything that is a regexp metachar
e89403c1
NC
219 ok(exists $Config{$pain}, "\$config('$pain') exists");
220
a644ec48
NC
221 my @result = $Config{$pain};
222 is (scalar @result, 1, "single result for \$config('$pain')");
223
06482b90
NC
224 @result = Config::config_re($pain);
225 is (scalar @result, 1, "single result for config_re('$pain')");
226 like ($result[0], qr/^$pain=(['"])$Config{$pain}\1$/, # grr '
227 "which is the expected result for $pain");
8468119f 228}
e9a5b206 229