From 4814146dc3f69729e22f89078a5783a82e9df039 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Wed, 29 Jan 2020 12:37:08 -0700 Subject: test cleanups: use harness python In a few places, a command line was built to execute a wrapper script written in Python, but the Python used was not the one used to invoke the test run (which is made available by TestSCons), but the result of running test.where_is('python'). On a system which still has the thing named 'python' resolve to Python 2, this fails, through no fault of scons itself. The two fixture wrapper scripts used occasionally by the tests used subprocess.call; this is considered "old" though not marked as deprecated at this time. Switched to subprocess.run. See: https://docs.python.org/3/library/subprocess.html#older-high-level-api One of these scripts was doing unnecessary bytes-twiddling. Modernized the inline myswig.py script (in test/SWIG/SWIG.py). This script was reformatted using Black along the way. Signed-off-by: Mats Wichmann --- test/SWIG/SWIG.py | 42 +++++++++++++++++++------------------- test/SWIG/SWIGPATH.py | 13 ++++++------ test/SWIG/implicit-dependencies.py | 13 ++++++------ test/SWIG/noproxy.py | 2 ++ test/fixture/mycompile.py | 2 +- test/fixture/mylink.py | 12 +++++------ test/fixture/myrewrite.py | 2 +- test/fixture/wrapper.py | 6 +++--- test/fixture/wrapper_with_args.py | 2 +- 9 files changed, 47 insertions(+), 47 deletions(-) diff --git a/test/SWIG/SWIG.py b/test/SWIG/SWIG.py index 31bc7d7..625b5f1 100644 --- a/test/SWIG/SWIG.py +++ b/test/SWIG/SWIG.py @@ -35,19 +35,19 @@ _obj = TestSCons._obj test = TestSCons.TestSCons() -python = test.where_is('python') -if not python: - test.skip_test('Can not find installed "python", skipping test.\n') +_python_ = TestSCons._python_ - -test.write('myswig.py', r""" +test.write('myswig.py', """\ import getopt import sys -opts, args = getopt.getopt(sys.argv[1:], 'c:o:v:') + +opts, args = getopt.getopt(sys.argv[1:], "c:o:v:") for opt, arg in opts: - if opt == '-c': pass - elif opt == '-o': out = arg - elif opt == '-v' and arg == 'ersion': + if opt == "-c": + pass + elif opt == "-o": + out = arg + elif opt == "-v" and arg == "ersion": print("") print("SWIG Version 0.1.2") print("") @@ -55,23 +55,23 @@ for opt, arg in opts: print("") print("Configured options: +pcre") print("") - print("Please see http://www.swig.org for reporting bugs and further information") + print("Please see http://www.swig.org for reporting bugs " + "and further information") sys.exit(0) -infile = open(args[0], 'r') -outfile = open(out, 'w') -for l in infile.readlines(): - if l[:4] != 'swig': - outfile.write(l) + +with open(args[0], "r") as ifp, open(out, "w") as ofp: + for line in ifp: + if not line.startswith("swig"): + ofp.write(line) sys.exit(0) """) -test.write('SConstruct', """ -env = Environment(tools=['default', 'swig'], - SWIG = [r'%(python)s', 'myswig.py']) +test.write('SConstruct', """\ +env = Environment(tools=["default", "swig"], SWIG=[r"%(_python_)s", "myswig.py"]) print(env.subst("Using SWIG $SWIGVERSION")) -env.Program(target = 'test1', source = 'test1.i') -env.CFile(target = 'test2', source = 'test2.i') -env.Clone(SWIGFLAGS = '-c++').Program(target = 'test3', source = 'test3.i') +env.Program(target="test1", source="test1.i") +env.CFile(target="test2", source="test2.i") +env.Clone(SWIGFLAGS="-c++").Program(target="test3", source="test3.i") """ % locals()) test.write('test1.i', r""" diff --git a/test/SWIG/SWIGPATH.py b/test/SWIG/SWIGPATH.py index d516c0c..fcce4c2 100644 --- a/test/SWIG/SWIGPATH.py +++ b/test/SWIG/SWIGPATH.py @@ -35,13 +35,12 @@ test = TestSCons.TestSCons() swig = test.where_is('swig') if not swig: test.skip_test('Can not find installed "swig", skipping test.\n') +swig = swig.replace('\\','/') -python = test.where_is('python') -if not python: - test,skip_test('Can not find installed "python", skipping test.\n') +_python_ = TestSCons._python_ + +test.file_fixture('wrapper.py') -swig = swig.replace('\\','/') -python = python.replace('\\','/') test.subdir('inc1', 'inc2') test.write(['inc2', 'dependency.i'], """\ @@ -59,7 +58,7 @@ foo = Environment(SWIGFLAGS='-python', SWIG='%(swig)s', SWIGPATH=['inc1', 'inc2']) swig = foo.Dictionary('SWIG') -bar = foo.Clone(SWIG = [r'%(python)s', 'wrapper.py', swig]) +bar = foo.Clone(SWIG = [r'%(_python_)s', 'wrapper.py', swig]) foo.CFile(target = 'dependent', source = ['dependent.i']) """ % locals()) @@ -96,4 +95,4 @@ test.pass_test() # tab-width:4 # indent-tabs-mode:nil # End: -# vim: set expandtab tabstop=4 shiftwidth=4: \ No newline at end of file +# vim: set expandtab tabstop=4 shiftwidth=4: diff --git a/test/SWIG/implicit-dependencies.py b/test/SWIG/implicit-dependencies.py index 8664bf6..d9eaa8e 100644 --- a/test/SWIG/implicit-dependencies.py +++ b/test/SWIG/implicit-dependencies.py @@ -35,13 +35,12 @@ test = TestSCons.TestSCons() swig = test.where_is('swig') if not swig: test.skip_test('Can not find installed "swig", skipping test.\n') +swig = swig.replace('\\','/') -python = test.where_is('python') -if not python: - test.skip_test('Can not find installed "python", skipping test.\n') +_python_ = TestSCons._python_ + +test.file_fixture('wrapper.py') -swig = swig.replace('\\','/') -python = python.replace('\\','/') test.write("dependency.i", """\ %module dependency """) @@ -55,7 +54,7 @@ test.write("dependent.i", """\ test.write('SConstruct', """ foo = Environment(SWIGFLAGS='-python', SWIG='%(swig)s') swig = foo.Dictionary('SWIG') -bar = foo.Clone(SWIG = [r'%(python)s', r'wrapper.py', swig]) +bar = foo.Clone(SWIG = [r'%(_python_)s', 'wrapper.py', swig]) foo.CFile(target = 'dependent', source = ['dependent.i']) """ % locals()) @@ -76,4 +75,4 @@ test.pass_test() # tab-width:4 # indent-tabs-mode:nil # End: -# vim: set expandtab tabstop=4 shiftwidth=4: \ No newline at end of file +# vim: set expandtab tabstop=4 shiftwidth=4: diff --git a/test/SWIG/noproxy.py b/test/SWIG/noproxy.py index f94f553..e3dde61 100644 --- a/test/SWIG/noproxy.py +++ b/test/SWIG/noproxy.py @@ -52,6 +52,8 @@ python, python_include, python_libpath, python_lib = \ # handle testing on other platforms: ldmodule_prefix = '_' +test.file_fixture('wrapper.py') + test.write("dependency.i", """\ %module dependency """) diff --git a/test/fixture/mycompile.py b/test/fixture/mycompile.py index 27bf840..0f37d19 100644 --- a/test/fixture/mycompile.py +++ b/test/fixture/mycompile.py @@ -13,7 +13,7 @@ if __name__ == '__main__': with open(sys.argv[2], 'wb') as ofp: for f in sys.argv[3:]: with open(f, 'rb') as ifp: - lines = [ln for ln in ifp.readlines() if ln != line] + lines = [ln for ln in ifp if ln != line] for ln in lines: ofp.write(ln) sys.exit(0) diff --git a/test/fixture/mylink.py b/test/fixture/mylink.py index b567118..38d9846 100644 --- a/test/fixture/mylink.py +++ b/test/fixture/mylink.py @@ -23,9 +23,9 @@ if __name__ == '__main__': args = args[1:] if a[:5].lower() == '/out:': out = a[5:] with open(args[0], 'rb') as ifp, open(out, 'wb') as ofp: - for l in ifp.readlines(): - if not l.startswith(b'#link'): - ofp.write(l) + for line in ifp: + if not line.startswith(b'#link'): + ofp.write(line) sys.exit(0) else: import getopt @@ -33,7 +33,7 @@ if __name__ == '__main__': for opt, arg in opts: if opt == '-o': out = arg with open(args[0], 'rb') as ifp, open(out, 'wb') as ofp: - for l in ifp.readlines(): - if not l.startswith(b'#link'): - ofp.write(l) + for line in ifp: + if not line.startswith(b'#link'): + ofp.write(line) sys.exit(0) diff --git a/test/fixture/myrewrite.py b/test/fixture/myrewrite.py index ad46091..6914611 100644 --- a/test/fixture/myrewrite.py +++ b/test/fixture/myrewrite.py @@ -10,7 +10,7 @@ import sys if __name__ == '__main__': line = ('/*' + sys.argv[1] + '*/\n').encode() with open(sys.argv[2], 'rb') as ifp: - lines = [ln for ln in ifp.readlines() if ln != line] + lines = [ln for ln in ifp if ln != line] with open(sys.argv[2], 'wb') as ofp: for ln in lines: ofp.write(ln) diff --git a/test/fixture/wrapper.py b/test/fixture/wrapper.py index a023689..d9bcefe 100644 --- a/test/fixture/wrapper.py +++ b/test/fixture/wrapper.py @@ -5,6 +5,6 @@ import subprocess if __name__ == '__main__': path = os.path.join(os.path.dirname(os.path.relpath(__file__)), 'wrapper.out') if '--version' not in sys.argv and '-dumpversion' not in sys.argv: - with open(path, 'wb') as f: - f.write(b"wrapper.py\n") - subprocess.call(sys.argv[1:]) + with open(path, 'w') as f: + f.write("wrapper.py\n") + subprocess.run(sys.argv[1:]) diff --git a/test/fixture/wrapper_with_args.py b/test/fixture/wrapper_with_args.py index f9e4d22..b8d6ae5 100644 --- a/test/fixture/wrapper_with_args.py +++ b/test/fixture/wrapper_with_args.py @@ -6,4 +6,4 @@ if __name__ == '__main__': path = os.path.join(os.path.dirname(os.path.relpath(__file__)), 'wrapper.out') with open(path, 'a') as f: f.write("wrapper_with_args.py %s\n" % " ".join(sys.argv[1:])) - subprocess.call(sys.argv[1:]) + subprocess.run(sys.argv[1:]) -- cgit v0.12 From e51f211505aa5cdd81c2db6cbcb0b93a73c146e3 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Thu, 13 Feb 2020 06:57:16 -0700 Subject: [PR #3554] trim unused bits from swig tests Three files in the previous fix got an added call to pull in the wrapper.py fixture; it turns out they didn't actually use it - appeared in a dummy call to Clone but the cloned env was never used. Drop those bits. Signed-off-by: Mats Wichmann --- test/SWIG/SWIGPATH.py | 5 ----- test/SWIG/implicit-dependencies.py | 7 +------ test/SWIG/noproxy.py | 3 --- 3 files changed, 1 insertion(+), 14 deletions(-) diff --git a/test/SWIG/SWIGPATH.py b/test/SWIG/SWIGPATH.py index fcce4c2..ef4fb18 100644 --- a/test/SWIG/SWIGPATH.py +++ b/test/SWIG/SWIGPATH.py @@ -37,10 +37,6 @@ if not swig: test.skip_test('Can not find installed "swig", skipping test.\n') swig = swig.replace('\\','/') -_python_ = TestSCons._python_ - -test.file_fixture('wrapper.py') - test.subdir('inc1', 'inc2') test.write(['inc2', 'dependency.i'], """\ @@ -58,7 +54,6 @@ foo = Environment(SWIGFLAGS='-python', SWIG='%(swig)s', SWIGPATH=['inc1', 'inc2']) swig = foo.Dictionary('SWIG') -bar = foo.Clone(SWIG = [r'%(_python_)s', 'wrapper.py', swig]) foo.CFile(target = 'dependent', source = ['dependent.i']) """ % locals()) diff --git a/test/SWIG/implicit-dependencies.py b/test/SWIG/implicit-dependencies.py index d9eaa8e..311ecc3 100644 --- a/test/SWIG/implicit-dependencies.py +++ b/test/SWIG/implicit-dependencies.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/asr/bin/env python # # __COPYRIGHT__ # @@ -37,10 +37,6 @@ if not swig: test.skip_test('Can not find installed "swig", skipping test.\n') swig = swig.replace('\\','/') -_python_ = TestSCons._python_ - -test.file_fixture('wrapper.py') - test.write("dependency.i", """\ %module dependency """) @@ -54,7 +50,6 @@ test.write("dependent.i", """\ test.write('SConstruct', """ foo = Environment(SWIGFLAGS='-python', SWIG='%(swig)s') swig = foo.Dictionary('SWIG') -bar = foo.Clone(SWIG = [r'%(_python_)s', 'wrapper.py', swig]) foo.CFile(target = 'dependent', source = ['dependent.i']) """ % locals()) diff --git a/test/SWIG/noproxy.py b/test/SWIG/noproxy.py index e3dde61..deeaa3a 100644 --- a/test/SWIG/noproxy.py +++ b/test/SWIG/noproxy.py @@ -52,8 +52,6 @@ python, python_include, python_libpath, python_lib = \ # handle testing on other platforms: ldmodule_prefix = '_' -test.file_fixture('wrapper.py') - test.write("dependency.i", """\ %module dependency """) @@ -75,7 +73,6 @@ foo = Environment(SWIGFLAGS=['-python', '-noproxy'], ) swig = foo.Dictionary('SWIG') -bar = foo.Clone(SWIG = [r'%(python)s', 'wrapper.py', swig]) foo.CFile(target = 'dependent', source = ['dependent.i']) """ % locals()) -- cgit v0.12 From 2931eea70b0234cf284c88b9a2a31e1c63bcfcd6 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Thu, 13 Feb 2020 08:16:11 -0700 Subject: [PR #3554] restore wrapper.py for former state Changes didn't work on Windows. Signed-off-by: Mats Wichmann --- test/fixture/wrapper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/fixture/wrapper.py b/test/fixture/wrapper.py index d9bcefe..112186e 100644 --- a/test/fixture/wrapper.py +++ b/test/fixture/wrapper.py @@ -5,6 +5,6 @@ import subprocess if __name__ == '__main__': path = os.path.join(os.path.dirname(os.path.relpath(__file__)), 'wrapper.out') if '--version' not in sys.argv and '-dumpversion' not in sys.argv: - with open(path, 'w') as f: - f.write("wrapper.py\n") + with open(path, 'wb') as f: + f.write(b"wrapper.py\n") subprocess.run(sys.argv[1:]) -- cgit v0.12