Skip to content

Commit cd0180a

Browse files
committed
Use a mocked pkg-config in test
Signed-off-by: Mats Wichmann <mats@linux.com>
1 parent 917c770 commit cd0180a

1 file changed

Lines changed: 30 additions & 89 deletions

File tree

test/CPPDEFINES/pkg-config.py

Lines changed: 30 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -26,70 +26,42 @@
2626
"""
2727
Verify merging with MergeFlags to CPPPDEFINES with various data types.
2828
29-
Uses system pkg-config with a stub pc file to generate the data.
30-
Not entirely sure the motivation for this - it would be more "isolated"
31-
to just paste in the data and not depend on pkg-config, but presumably
32-
this test exists because of observed bugs, so leaving alone for now.
33-
34-
Ran into an interesting problem when we spun up a GitHub Windows Action:
35-
it finds pre-installed Strawberry Perl's pkg-config over the mingw one,
36-
which is also pre-installed. The Strawberry one is written in pure
37-
Perl (after getting past the initial batch file), so requires perl.exe,
38-
which won't be in the SCons env['ENV']['PATH']. We try to detect it,
39-
by somewhat hokey means: if pkg-config's path had "strawberry' in it,
40-
assume we need to add the directory path to our execution env.
41-
42-
It ended up being more portable to use pkg-config's --with-path than
43-
to set PKG_CONFIG_PATH, because the Windows cmd.exe can't accept
44-
the standard calling style.
29+
Now uses a mocked pkg-config to avoid some weird problems,
30+
but is still a "live" test because we do expansion on $_CPPDEFFLAGS,
31+
which needs to have been set up with a compiler tool so have something
32+
to subst into.
4533
"""
4634

4735
import pathlib
36+
import sys
4837

4938
import TestSCons
5039
import TestCmd
5140

5241
test = TestSCons.TestSCons()
42+
_python_ = TestSCons._python_
5343

54-
pkg_config_path = test.where_is('pkg-config')
55-
if not pkg_config_path:
56-
test.skip_test("Could not find 'pkg-config' in PATH, skipping test.\n")
57-
pkg_config_path = pkg_config_path.replace("\\", "/")
58-
59-
# Try to guess if this was Strawberry Perl's pkg-config
60-
if 'strawberry' in pkg_config_path.lower():
61-
# as_posix() or the pasted paths in SConstruct will have escape problems
62-
# (or need to be raw strings)
63-
strawberry_perl_path = pathlib.PurePath(pkg_config_path).parent.as_posix()
64-
else:
65-
strawberry_perl_path = ""
66-
67-
test.write('bug.pc', """\
68-
prefix=/usr
69-
exec_prefix=${prefix}
70-
libdir=${exec_prefix}/lib
71-
includedir=${prefix}/include
72-
73-
Name: bug
74-
Description: A test case .pc file
75-
Version: 1.2
76-
Cflags: -DSOMETHING -DVARIABLE=2
44+
mock_pkg_config = test.workpath('mock-pkg-config.py')
45+
test.write(mock_pkg_config, f"""\
46+
import sys
47+
if '--cflags' in sys.argv:
48+
print("-DSOMETHING -DVARIABLE=2")
49+
sys.exit(0)
7750
""")
7851

79-
test.write('main.c', """\
80-
int main(int argc, char *argv[])
81-
{
82-
return 0;
83-
}
84-
""")
52+
# Since the mock pkg-config is a Python script, use the selected Python to
53+
# run it to avoid execution problems (esp. on Windows)
54+
pc_path = (_python_ + ' ' + mock_pkg_config).replace("\\", "/")
8555

56+
# We need a toolset:
8657
if TestCmd.IS_WINDOWS:
87-
pkg_config_file = 'bug'
88-
pkg_config_tools = 'mingw'
58+
pc_tools = 'mingw'
8959
else:
90-
pkg_config_file = 'bug.pc'
91-
pkg_config_tools = 'default'
92-
pkg_config_cl_path = '--with-path=.'
60+
pc_tools = 'default'
61+
62+
# these are no longer really needed, leave so it looks like a "real" pkg-config
63+
pc_file = 'bug'
64+
pc_cl_path = ""
9365

9466
test.write('SConstruct', f"""\
9567
import os
@@ -98,57 +70,26 @@
9870
DefaultEnvironment(tools=[])
9971
# https://github.com/SCons/scons/issues/2671
10072
# Passing test cases
101-
env_1 = Environment(
102-
CPPDEFINES=[('DEBUG', '1'), 'TEST'],
103-
tools=['{pkg_config_tools}']
104-
)
105-
if '{strawberry_perl_path}':
106-
env_1.AppendENVPath('PATH', '{strawberry_perl_path}')
107-
if sys.platform == 'win32':
108-
os.environ['PKG_CONFIG_PATH'] = env_1.Dir('.').abspath.replace("\\\\", "/")
109-
env_1.ParseConfig('"{pkg_config_path}" {pkg_config_cl_path} --cflags {pkg_config_file}')
73+
env_1 = Environment(CPPDEFINES=[('DEBUG', '1'), 'TEST'], tools=['{pc_tools}'])
74+
env_1.ParseConfig('{pc_path} {pc_cl_path} --cflags {pc_file}')
11075
print(env_1.subst('$_CPPDEFFLAGS'))
11176
112-
env_2 = Environment(
113-
CPPDEFINES=[('DEBUG', '1'), 'TEST'],
114-
tools=['{pkg_config_tools}']
115-
)
116-
if '{strawberry_perl_path}':
117-
env_2.AppendENVPath('PATH', '{strawberry_perl_path}')
77+
env_2 = Environment(CPPDEFINES=[('DEBUG', '1'), 'TEST'], tools=['{pc_tools}'])
11878
env_2.MergeFlags('-DSOMETHING -DVARIABLE=2')
11979
print(env_2.subst('$_CPPDEFFLAGS'))
12080
12181
# Failing test cases
122-
env_3 = Environment(
123-
CPPDEFINES=dict([('DEBUG', 1), ('TEST', None)]),
124-
tools=['{pkg_config_tools}'],
125-
)
126-
if '{strawberry_perl_path}':
127-
env_3.AppendENVPath('PATH', '{strawberry_perl_path}')
128-
env_3.ParseConfig('"{pkg_config_path}" {pkg_config_cl_path} --cflags {pkg_config_file}')
82+
env_3 = Environment(CPPDEFINES=dict([('DEBUG', 1), ('TEST', None)]), tools=['{pc_tools}'])
83+
env_3.ParseConfig('{pc_path} {pc_cl_path} --cflags {pc_file}')
12984
print(env_3.subst('$_CPPDEFFLAGS'))
13085
131-
env_4 = Environment(
132-
CPPDEFINES=dict([('DEBUG', 1), ('TEST', None)]),
133-
tools=['{pkg_config_tools}'],
134-
)
135-
if '{strawberry_perl_path}':
136-
env_4.AppendENVPath('PATH', '{strawberry_perl_path}')
86+
env_4 = Environment(CPPDEFINES=dict([('DEBUG', 1), ('TEST', None)]), tools=['{pc_tools}'])
13787
env_4.MergeFlags('-DSOMETHING -DVARIABLE=2')
13888
print(env_4.subst('$_CPPDEFFLAGS'))
13989
14090
# https://github.com/SCons/scons/issues/1738
141-
# TODO: the Perl pkg-config doesn't work right with both --cflags --libs
142-
# e.g.: pkg-config --with-path=. --libs --cflags bug
143-
# Exepct: -DSOMETHING -DVARIABLE=2
144-
# Strawberry: (blank)
145-
# We don't have any libs in the stub pc file, so just drop that for now.
146-
env_1738_1 = Environment(tools=['{pkg_config_tools}'])
147-
if '{strawberry_perl_path}':
148-
env_1738_1.AppendENVPath('PATH', '{strawberry_perl_path}')
149-
env_1738_1.ParseConfig(
150-
'"{pkg_config_path}" {pkg_config_cl_path} --cflags {pkg_config_file}'
151-
)
91+
env_1738_1 = Environment(tools=['{pc_tools}'])
92+
env_1738_1.ParseConfig('{pc_path} {pc_cl_path} --cflags {pc_file}')
15293
env_1738_1.Append(CPPDEFINES={{'value': '1'}})
15394
print(env_1738_1.subst('$_CPPDEFFLAGS'))
15495
""")

0 commit comments

Comments
 (0)