diff options
25 files changed, 108 insertions, 59 deletions
diff --git a/run_tests.py b/run_tests.py index 3e49662..48f1230 100755 --- a/run_tests.py +++ b/run_tests.py @@ -156,8 +156,9 @@ def validate_install(srcdir, installdir): if os.path.exists(os.path.join(installdir, noinst_file)): expected[noinst_file] = False elif os.path.exists(info_file): - for line in open(info_file): - expected[platform_fix_exe_name(line.strip())] = False + with open(info_file) as f: + for line in f: + expected[platform_fix_exe_name(line.strip())] = False # Check if expected files were found for fname in expected: if os.path.exists(os.path.join(installdir, fname)): @@ -249,7 +250,8 @@ def _run_test(testdir, test_build_dir, install_dir, extra_args, flags, compile_c (returncode, stdo, stde) = run_configure_inprocess(gen_command) try: logfile = os.path.join(test_build_dir, 'meson-logs/meson-log.txt') - mesonlog = open(logfile, errors='ignore').read() + with open(logfile, errors='ignore') as f: + mesonlog = f.read() except Exception: mesonlog = 'No meson-log.txt found.' gen_time = time.time() - gen_start @@ -401,7 +403,9 @@ def run_tests(extra_args): def check_file(fname): linenum = 1 - for line in open(fname, 'rb').readlines(): + with open(fname, 'rb') as f: + lines = f.readlines() + for line in lines: if b'\t' in line: print("File %s contains a literal tab on line %d. Only spaces are permitted." % (fname, linenum)) sys.exit(1) diff --git a/test cases/common/103 manygen/subdir/manygen.py b/test cases/common/103 manygen/subdir/manygen.py index 4411183..c233ae6 100755 --- a/test cases/common/103 manygen/subdir/manygen.py +++ b/test cases/common/103 manygen/subdir/manygen.py @@ -6,7 +6,8 @@ import sys, os import shutil, subprocess -funcname = open(sys.argv[1]).readline().strip() +with open(sys.argv[1]) as f: + funcname = f.readline().strip() outdir = sys.argv[2] if not os.path.isdir(outdir): @@ -44,19 +45,22 @@ outc = os.path.join(outdir, funcname + '.c') tmpc = 'diibadaaba.c' tmpo = 'diibadaaba' + objsuffix -open(outc, 'w').write('''#include"%s.h" +with open(outc, 'w') as f: + f.write('''#include"%s.h" int %s_in_src() { return 0; } ''' % (funcname, funcname)) -open(outh, 'w').write('''#pragma once +with open(outh, 'w') as f: + f.write('''#pragma once int %s_in_lib(); int %s_in_obj(); int %s_in_src(); ''' % (funcname, funcname, funcname)) -open(tmpc, 'w').write('''int %s_in_obj() { +with open(tmpc, 'w') as f: + f.write('''int %s_in_obj() { return 0; } ''' % funcname) @@ -66,7 +70,8 @@ if is_vs: else: subprocess.check_call([compiler, '-c', '-o', outo, tmpc]) -open(tmpc, 'w').write('''int %s_in_lib() { +with open(tmpc, 'w') as f: + f.write('''int %s_in_lib() { return 0; } ''' % funcname) diff --git a/test cases/common/107 postconf/postconf.py b/test cases/common/107 postconf/postconf.py index 209b7af..50c91ca 100644 --- a/test cases/common/107 postconf/postconf.py +++ b/test cases/common/107 postconf/postconf.py @@ -7,5 +7,10 @@ template = '''#pragma once #define THE_NUMBER {} ''' -data = open(os.path.join(os.environ['MESON_SOURCE_ROOT'], 'raw.dat')).readline().strip() -open(os.path.join(os.environ['MESON_BUILD_ROOT'], 'generated.h'), 'w').write(template.format(data)) +input_file = os.path.join(os.environ['MESON_SOURCE_ROOT'], 'raw.dat') +output_file = os.path.join(os.environ['MESON_BUILD_ROOT'], 'generated.h') + +with open(input_file) as f: + data = f.readline().strip() +with open(output_file, 'w') as f: + f.write(template.format(data)) diff --git a/test cases/common/108 postconf with args/postconf.py b/test cases/common/108 postconf with args/postconf.py index 4cfbb7c..cef7f79 100644 --- a/test cases/common/108 postconf with args/postconf.py +++ b/test cases/common/108 postconf with args/postconf.py @@ -9,5 +9,10 @@ template = '''#pragma once #define THE_ARG2 {} ''' -data = open(os.path.join(os.environ['MESON_SOURCE_ROOT'], 'raw.dat')).readline().strip() -open(os.path.join(os.environ['MESON_BUILD_ROOT'], 'generated.h'), 'w').write(template.format(data, sys.argv[1], sys.argv[2])) +input_file = os.path.join(os.environ['MESON_SOURCE_ROOT'], 'raw.dat') +output_file = os.path.join(os.environ['MESON_BUILD_ROOT'], 'generated.h') + +with open(input_file) as f: + data = f.readline().strip() +with open(output_file, 'w') as f: + f.write(template.format(data, sys.argv[1], sys.argv[2])) diff --git a/test cases/common/113 generatorcustom/catter.py b/test cases/common/113 generatorcustom/catter.py index 354d6e0..7a6c085 100755 --- a/test cases/common/113 generatorcustom/catter.py +++ b/test cases/common/113 generatorcustom/catter.py @@ -8,6 +8,7 @@ inputs = sys.argv[1:-1] with open(output, 'w') as ofile: ofile.write('#pragma once\n') for i in inputs: - content = open(i, 'r').read() + with open(i, 'r') as ifile: + content = ifile.read() ofile.write(content) ofile.write('\n') diff --git a/test cases/common/113 generatorcustom/gen.py b/test cases/common/113 generatorcustom/gen.py index ba02e3f..c843497 100755 --- a/test cases/common/113 generatorcustom/gen.py +++ b/test cases/common/113 generatorcustom/gen.py @@ -5,7 +5,9 @@ import sys, os ifile = sys.argv[1] ofile = sys.argv[2] -resname = open(ifile, 'r').readline().strip() +with open(ifile, 'r') as f: + resname = f.readline().strip() templ = 'const char %s[] = "%s";\n' -open(ofile, 'w').write(templ % (resname, resname)) +with open(ofile, 'w') as f: + f.write(templ % (resname, resname)) diff --git a/test cases/common/117 custom target capture/my_compiler.py b/test cases/common/117 custom target capture/my_compiler.py index 3e9ec23..b60722a 100755 --- a/test cases/common/117 custom target capture/my_compiler.py +++ b/test cases/common/117 custom target capture/my_compiler.py @@ -6,7 +6,8 @@ if __name__ == '__main__': if len(sys.argv) != 2: print(sys.argv[0], 'input_file') sys.exit(1) - ifile = open(sys.argv[1]).read() + with open(sys.argv[1]) as f: + ifile = f.read() if ifile != 'This is a text only input file.\n': print('Malformed input') sys.exit(1) diff --git a/test cases/common/16 configure file/generator.py b/test cases/common/16 configure file/generator.py index de9a423..90223f0 100755 --- a/test cases/common/16 configure file/generator.py +++ b/test cases/common/16 configure file/generator.py @@ -6,8 +6,8 @@ if len(sys.argv) != 3: print("Wrong amount of parameters.") # Just test that it exists. -ifile = open(sys.argv[1], 'r') +with open(sys.argv[1], 'r') as ifile: + pass -ofile = open(sys.argv[2], 'w') -ofile.write("#define ZERO_RESULT 0\n") -ofile.close() +with open(sys.argv[2], 'w') as ofile: + ofile.write("#define ZERO_RESULT 0\n") diff --git a/test cases/common/48 test args/tester.py b/test cases/common/48 test args/tester.py index 36e510d..0b4010a 100755 --- a/test cases/common/48 test args/tester.py +++ b/test cases/common/48 test args/tester.py @@ -2,5 +2,6 @@ import sys -if open(sys.argv[1]).read() != 'contents\n': - sys.exit(1) +with open(sys.argv[1]) as f: + if f.read() != 'contents\n': + sys.exit(1) diff --git a/test cases/common/56 custom target/depfile/dep.py b/test cases/common/56 custom target/depfile/dep.py index 3a772ec..585e192 100755 --- a/test cases/common/56 custom target/depfile/dep.py +++ b/test cases/common/56 custom target/depfile/dep.py @@ -9,5 +9,7 @@ depfiles = glob(os.path.join(srcdir, '*')) quoted_depfiles = [x.replace(' ', '\ ') for x in depfiles] -open(output, 'w').write('I am the result of globbing.') -open(depfile, 'w').write('%s: %s\n' % (output, ' '.join(quoted_depfiles))) +with open(output, 'w') as f: + f.write('I am the result of globbing.') +with open(depfile, 'w') as f: + f.write('%s: %s\n' % (output, ' '.join(quoted_depfiles))) diff --git a/test cases/common/56 custom target/my_compiler.py b/test cases/common/56 custom target/my_compiler.py index 43e7143..d99029b 100755 --- a/test cases/common/56 custom target/my_compiler.py +++ b/test cases/common/56 custom target/my_compiler.py @@ -6,9 +6,10 @@ if __name__ == '__main__': if len(sys.argv) != 3: print(sys.argv[0], 'input_file output_file') sys.exit(1) - ifile = open(sys.argv[1]).read() + with open(sys.argv[1]) as f: + ifile = f.read() if ifile != 'This is a text only input file.\n': print('Malformed input') sys.exit(1) - ofile = open(sys.argv[2], 'w') - ofile.write('This is a binary output file.\n') + with open(sys.argv[2], 'w') as ofile: + ofile.write('This is a binary output file.\n') diff --git a/test cases/common/57 custom target chain/my_compiler.py b/test cases/common/57 custom target chain/my_compiler.py index 43e7143..d99029b 100755 --- a/test cases/common/57 custom target chain/my_compiler.py +++ b/test cases/common/57 custom target chain/my_compiler.py @@ -6,9 +6,10 @@ if __name__ == '__main__': if len(sys.argv) != 3: print(sys.argv[0], 'input_file output_file') sys.exit(1) - ifile = open(sys.argv[1]).read() + with open(sys.argv[1]) as f: + ifile = f.read() if ifile != 'This is a text only input file.\n': print('Malformed input') sys.exit(1) - ofile = open(sys.argv[2], 'w') - ofile.write('This is a binary output file.\n') + with open(sys.argv[2], 'w') as ofile: + ofile.write('This is a binary output file.\n') diff --git a/test cases/common/57 custom target chain/my_compiler2.py b/test cases/common/57 custom target chain/my_compiler2.py index 22a4160..22ec789 100755 --- a/test cases/common/57 custom target chain/my_compiler2.py +++ b/test cases/common/57 custom target chain/my_compiler2.py @@ -6,9 +6,10 @@ if __name__ == '__main__': if len(sys.argv) != 3: print(sys.argv[0], 'input_file output_file') sys.exit(1) - ifile = open(sys.argv[1]).read() + with open(sys.argv[1]) as f: + ifile = f.read() if ifile != 'This is a binary output file.\n': print('Malformed input') sys.exit(1) - ofile = open(sys.argv[2], 'w') - ofile.write('This is a different binary output file.\n') + with open(sys.argv[2], 'w') as ofile: + ofile.write('This is a different binary output file.\n') diff --git a/test cases/common/57 custom target chain/usetarget/subcomp.py b/test cases/common/57 custom target chain/usetarget/subcomp.py index 207a8ad..6f4b686 100755 --- a/test cases/common/57 custom target chain/usetarget/subcomp.py +++ b/test cases/common/57 custom target chain/usetarget/subcomp.py @@ -3,5 +3,5 @@ import sys, os with open(sys.argv[1], 'rb') as ifile: - open(sys.argv[2], 'w').write('Everything ok.\n') - + with open(sys.argv[2], 'w') as ofile: + ofile.write('Everything ok.\n') diff --git a/test cases/common/58 run target/converter.py b/test cases/common/58 run target/converter.py index 6acbc84..8dd31fe 100644 --- a/test cases/common/58 run target/converter.py +++ b/test cases/common/58 run target/converter.py @@ -2,4 +2,5 @@ import sys -open(sys.argv[2], 'wb').write(open(sys.argv[1], 'rb').read()) +with open(sys.argv[1], 'rb') as ifile, open(sys.argv[2], 'wb') as ofile: + ofile.write(ifile.read()) diff --git a/test cases/common/58 run target/fakeburner.py b/test cases/common/58 run target/fakeburner.py index a100a6f..5728002 100755 --- a/test cases/common/58 run target/fakeburner.py +++ b/test cases/common/58 run target/fakeburner.py @@ -5,7 +5,8 @@ import sys plain_arg = sys.argv[1] _, filename, _ = plain_arg.split(':') try: - content = open(filename, 'rb').read() + with open(filename, 'rb') as f: + content = f.read() except FileNotFoundError: print('Could not open file. Missing dependency?') sys.exit(1) diff --git a/test cases/common/61 custom target source output/generator.py b/test cases/common/61 custom target source output/generator.py index 4bf5c84..3464b0a 100755 --- a/test cases/common/61 custom target source output/generator.py +++ b/test cases/common/61 custom target source output/generator.py @@ -7,8 +7,10 @@ if len(sys.argv) != 2: odir = sys.argv[1] -open(os.path.join(odir, 'mylib.h'), 'w').write('int func();\n') -open(os.path.join(odir, 'mylib.c'), 'w').write('''int func() { +with open(os.path.join(odir, 'mylib.h'), 'w') as f: + f.write('int func();\n') +with open(os.path.join(odir, 'mylib.c'), 'w') as f: + f.write('''int func() { return 0; } ''') diff --git a/test cases/common/64 custom header generator/makeheader.py b/test cases/common/64 custom header generator/makeheader.py index 9ef2bd5..f156834 100644 --- a/test cases/common/64 custom header generator/makeheader.py +++ b/test cases/common/64 custom header generator/makeheader.py @@ -6,5 +6,7 @@ import sys template = '#define RET_VAL %s\n' -output = template % (open(sys.argv[1]).readline().strip()) -open(sys.argv[2], 'w').write(output) +with open(sys.argv[1]) as f: + output = template % (f.readline().strip(), ) +with open(sys.argv[2], 'w') as f: + f.write(output) diff --git a/test cases/common/65 multiple generators/mygen.py b/test cases/common/65 multiple generators/mygen.py index cd786ea..99dc331 100755 --- a/test cases/common/65 multiple generators/mygen.py +++ b/test cases/common/65 multiple generators/mygen.py @@ -6,14 +6,17 @@ if len(sys.argv) != 3: print("You is fail.") sys.exit(1) -val = open(sys.argv[1]).read().strip() +with open(sys.argv[1]) as f: + val = f.read().strip() outdir = sys.argv[2] outhdr = os.path.join(outdir, 'source%s.h' % val) outsrc = os.path.join(outdir, 'source%s.cpp' % val) -open(outhdr, 'w').write('int func%s();\n' % val) -open(outsrc, 'w').write('''int func%s() { +with open(outhdr, 'w') as f: + f.write('int func%s();\n' % val) +with open(outsrc, 'w') as f: + f.write('''int func%s() { return 0; } ''' % val) diff --git a/test cases/common/72 build always/version_gen.py b/test cases/common/72 build always/version_gen.py index c82678d..d7b01ca 100755 --- a/test cases/common/72 build always/version_gen.py +++ b/test cases/common/72 build always/version_gen.py @@ -14,14 +14,17 @@ def generate(infile, outfile, fallback): version = stdo.decode().strip() except: pass - newdata = open(infile).read().replace('@VERSION@', version) + with open(infile) as f: + newdata = f.read().replace('@VERSION@', version) try: - olddata = open(outfile).read() + with open(outfile) as f: + olddata = f.read() if olddata == newdata: return except: pass - open(outfile, 'w').write(newdata) + with open(outfile, 'w') as f: + f.write(newdata) if __name__ == '__main__': infile = sys.argv[1] diff --git a/test cases/common/76 configure file in custom target/src/mycompiler.py b/test cases/common/76 configure file in custom target/src/mycompiler.py index d5dcab5..b00c862 100644 --- a/test cases/common/76 configure file in custom target/src/mycompiler.py +++ b/test cases/common/76 configure file in custom target/src/mycompiler.py @@ -2,7 +2,8 @@ import sys -ifile = open(sys.argv[1]) -if ifile.readline().strip() != '42': - print('Incorrect input') -open(sys.argv[2], 'w').write('Success\n') +with open(sys.argv[1]) as ifile: + if ifile.readline().strip() != '42': + print('Incorrect input') +with open(sys.argv[2], 'w') as ofile: + ofile.write('Success\n') diff --git a/test cases/common/78 ctarget dependency/gen1.py b/test cases/common/78 ctarget dependency/gen1.py index 64b8e6d..0fa6ea1 100755 --- a/test cases/common/78 ctarget dependency/gen1.py +++ b/test cases/common/78 ctarget dependency/gen1.py @@ -6,5 +6,7 @@ import time, sys # is missing. time.sleep(0.5) -contents = open(sys.argv[1], 'r').read() -open(sys.argv[2], 'w').write(contents) +with open(sys.argv[1], 'r') as f: + contents = f.read() +with open(sys.argv[2], 'w') as f: + f.write(contents) diff --git a/test cases/common/78 ctarget dependency/gen2.py b/test cases/common/78 ctarget dependency/gen2.py index 3a8be7d..b087b02 100755 --- a/test cases/common/78 ctarget dependency/gen2.py +++ b/test cases/common/78 ctarget dependency/gen2.py @@ -6,4 +6,5 @@ from glob import glob files = glob(os.path.join(sys.argv[1], '*.tmp')) assert(len(files) == 1) -open(sys.argv[2], 'w').write(open(files[0], 'r').read()) +with open(files[0], 'r') as ifile, open(sys.argv[2], 'w') as ofile: + ofile.write(ifile.read()) diff --git a/test cases/common/93 private include/stlib/compiler.py b/test cases/common/93 private include/stlib/compiler.py index 3e74c88..98dbe46 100755 --- a/test cases/common/93 private include/stlib/compiler.py +++ b/test cases/common/93 private include/stlib/compiler.py @@ -26,5 +26,7 @@ hfile = os.path.join(outdir, base + '.h') c_code = c_templ % (base, base) h_code = h_templ % base -open(cfile, 'w').write(c_code) -open(hfile, 'w').write(h_code) +with open(cfile, 'w') as f: + f.write(c_code) +with open(hfile, 'w') as f: + f.write(h_code) diff --git a/test cases/common/98 gen extra/srcgen.py b/test cases/common/98 gen extra/srcgen.py index 55e777e..73bc337 100755 --- a/test cases/common/98 gen extra/srcgen.py +++ b/test cases/common/98 gen extra/srcgen.py @@ -19,8 +19,10 @@ c_templ = '''int %s() { options = parser.parse_args(sys.argv[1:]) -funcname = open(options.input).readline().strip() +with open(options.input) as f: + funcname = f.readline().strip() if options.upper: funcname = funcname.upper() -open(options.output, 'w').write(c_templ % funcname) +with open(options.output, 'w') as f: + f.write(c_templ % funcname) |