diff options
author | Mats Wichmann <mats@linux.com> | 2019-03-30 14:53:36 (GMT) |
---|---|---|
committer | Mats Wichmann <mats@linux.com> | 2019-04-25 15:33:22 (GMT) |
commit | a77e14500c3149f0b45b560c8fb8217b7d41bcae (patch) | |
tree | b4976aff55325fabb3ff1d02552c484f32406c9f /test | |
parent | d642ba8c6ee147daa8155b6a354ce66a13bb9188 (diff) | |
download | SCons-a77e14500c3149f0b45b560c8fb8217b7d41bcae.zip SCons-a77e14500c3149f0b45b560c8fb8217b7d41bcae.tar.gz SCons-a77e14500c3149f0b45b560c8fb8217b7d41bcae.tar.bz2 |
[PY 3.8] modernize common test fixtures
Apply changes to quiet open-file warnings. Add docstring to a few.
Switch os.system usage to subprocess.call. Add if-main logic.
Signed-off-by: Mats Wichmann <mats@linux.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/fixture/mycompile.py | 25 | ||||
-rw-r--r-- | test/fixture/mylink.py | 66 | ||||
-rw-r--r-- | test/fixture/myrewrite.py | 21 | ||||
-rw-r--r-- | test/fixture/wrapper.py | 10 | ||||
-rw-r--r-- | test/fixture/wrapper_with_args.py | 10 |
5 files changed, 83 insertions, 49 deletions
diff --git a/test/fixture/mycompile.py b/test/fixture/mycompile.py index 555c2c8..313e4b5 100644 --- a/test/fixture/mycompile.py +++ b/test/fixture/mycompile.py @@ -1,8 +1,19 @@ +r""" +Phony "compiler" for testing SCons. + +Copies its source files to the target file, dropping lines +that match a pattern, so we can recognize the tool +has made a modification. +""" + import sys -line = ('/*' + sys.argv[1] + '*/\n').encode() -outfile = open(sys.argv[2], 'wb') -for f in sys.argv[3:]: - infile = open(f, 'rb') - for l in [l for l in infile.readlines() if l != line]: - outfile.write(l) -sys.exit(0) + +if __name__ == '__main__': + line = '/*' + sys.argv[1] + '*/\n' + with open(sys.argv[2], 'w') as ofp: + for f in sys.argv[3:]: + with open(f, 'r') as ifp: + lines = [l for l in ifp.readlines() if l != line] + for l in lines: + ofp.write(l) + sys.exit(0) diff --git a/test/fixture/mylink.py b/test/fixture/mylink.py index 7c03f00..5006f22 100644 --- a/test/fixture/mylink.py +++ b/test/fixture/mylink.py @@ -1,31 +1,39 @@ +r""" +Phony "linker" for testing SCons. + +Recognizes the option to specify an output file, ignoring +all others; copies input lines to the output file except +ones that contain a pattern, so we can recognize the tool +has made a modification. +""" + import sys -if sys.platform == 'win32': - args = sys.argv[1:] - while args: - a = args[0] - if a == '-o': - out = args[1] - args = args[2:] - continue - if not a[0] in '/-': - break - args = args[1:] - if a[:5].lower() == '/out:': out = a[5:] - infile = open(args[0], 'rb') - outfile = open(out, 'wb') - for l in infile.readlines(): - if l[:5] != b'#link': - outfile.write(l) - sys.exit(0) -else: - import getopt - opts, args = getopt.getopt(sys.argv[1:], 'o:') - for opt, arg in opts: - if opt == '-o': out = arg - infile = open(args[0], 'rb') - outfile = open(out, 'wb') - for l in infile.readlines(): - if l[:5] != b'#link': - outfile.write(l) - sys.exit(0) +if __name__ == '__main__': + if sys.platform == 'win32': + args = sys.argv[1:] + while args: + a = args[0] + if a == '-o': + out = args[1] + args = args[2:] + continue + if not a[0] in '/-': + break + args = args[1:] + if a[:5].lower() == '/out:': out = a[5:] + with open(args[0], 'r') as ifp, open(out, 'w') as ofp: + for l in ifp.readlines(): + if not l.startswith('#link'): + ofp.write(l) + sys.exit(0) + else: + import getopt + opts, args = getopt.getopt(sys.argv[1:], 'o:') + for opt, arg in opts: + if opt == '-o': out = arg + with open(args[0], 'r') as ifp, open(out, 'w') as ofp: + for l in ifp.readlines(): + if not l.startswith('#link'): + ofp.write(l) + sys.exit(0) diff --git a/test/fixture/myrewrite.py b/test/fixture/myrewrite.py index 40bf830..bd90a68 100644 --- a/test/fixture/myrewrite.py +++ b/test/fixture/myrewrite.py @@ -1,7 +1,16 @@ +r""" +Phony tool to modify a file in place for testing SCons. + +Drops lines that match a pattern. Currently used to test +ranlib-related behavior without invoking ranlib. +""" + import sys -line = ('/*' + sys.argv[1] + '*/\n').encode() -lines = open(sys.argv[2], 'rb').readlines() -outfile = open(sys.argv[2], 'wb') -for l in [l for l in lines if l != line]: - outfile.write(l) -sys.exit(0) + +if __name__ == '__main__': + line = ('/*' + sys.argv[1] + '*/\n') + with open(sys.argv[2], 'w') as ofp, open(sys.argv[2], 'r') as ifp: + lines = [l for l in ifp.readlines() if l != line] + for l in lines: + ofp.write(l) + sys.exit(0) diff --git a/test/fixture/wrapper.py b/test/fixture/wrapper.py index f02ea03..8791390 100644 --- a/test/fixture/wrapper.py +++ b/test/fixture/wrapper.py @@ -1,6 +1,10 @@ import os import sys -if '--version' not in sys.argv and '-dumpversion' not in sys.argv: +import subprocess + +if __name__ == '__main__': path = os.path.join(os.path.dirname(os.path.relpath(__file__)), 'wrapper.out') - open(path, 'wb').write(b"wrapper.py\n") -os.system(" ".join(sys.argv[1:])) + if '--version' not in sys.argv and '-dumpversion' not in sys.argv: + with open(path, 'w') as f: + f.write("wrapper.py\n") + subprocess.call(sys.argv[1:]) diff --git a/test/fixture/wrapper_with_args.py b/test/fixture/wrapper_with_args.py index fccab72..f9e4d22 100644 --- a/test/fixture/wrapper_with_args.py +++ b/test/fixture/wrapper_with_args.py @@ -1,7 +1,9 @@ import os import sys +import subprocess -path = os.path.join(os.path.dirname(os.path.relpath(__file__)), 'wrapper.out') - -open(path, 'a').write("wrapper_with_args.py %s\n" % " ".join(sys.argv[1:])) -os.system(" ".join(sys.argv[1:])) +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:]) |