diff options
author | Mats Wichmann <mats@linux.com> | 2022-01-24 16:40:48 (GMT) |
---|---|---|
committer | Mats Wichmann <mats@linux.com> | 2022-01-24 19:40:25 (GMT) |
commit | ba5915c606c29d2490ce65782e4db861249a9b78 (patch) | |
tree | d358648cd1ef5d7ff7bdf12e74a984b1537d7a0f /test/Java | |
parent | 7b3960d9865ead96d5c541be231ec7f62d8b6ab7 (diff) | |
download | SCons-ba5915c606c29d2490ce65782e4db861249a9b78.zip SCons-ba5915c606c29d2490ce65782e4db861249a9b78.tar.gz SCons-ba5915c606c29d2490ce65782e4db861249a9b78.tar.bz2 |
A little java/jar cleanup
* The Jar test is split into requires-jdk and not.
* Move some inline tool definitions to fixture files.
* Reformat some stuff.
* Some linting and commenting in Jar tool.
* Enabled test for a non-java file when Jar source is a directory
(was commented out) - needed to add a Command to copy the file
to the directory where the class files were generated.
Signed-off-by: Mats Wichmann <mats@linux.com>
Diffstat (limited to 'test/Java')
-rw-r--r-- | test/Java/JAR.py | 252 | ||||
-rw-r--r-- | test/Java/JAVAC.py | 42 | ||||
-rw-r--r-- | test/Java/Java-fixture/.exclude_tests | 2 | ||||
-rw-r--r-- | test/Java/Java-fixture/myjar.py | 19 | ||||
-rw-r--r-- | test/Java/Java-fixture/myjavac.py | 22 | ||||
-rw-r--r-- | test/Java/jar_not_in_PATH.py | 31 |
6 files changed, 159 insertions, 209 deletions
diff --git a/test/Java/JAR.py b/test/Java/JAR.py index bc52a89..2d67717 100644 --- a/test/Java/JAR.py +++ b/test/Java/JAR.py @@ -23,6 +23,12 @@ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +""" +Test Jar builder. + +These tests require a findable/working Java subsystem. +""" + import os import TestSCons @@ -30,115 +36,23 @@ _python_ = TestSCons._python_ test = TestSCons.TestSCons() -# Keep this logic because it skips the test if javac or jar not found. where_javac, java_version = test.java_where_javac() where_jar = test.java_where_jar() -test.write('myjar.py', r""" -import sys -args = sys.argv[1:] -while args: - a = args[0] - if a == 'cf': - out = args[1] - args = args[1:] - else: - break - args = args[1:] -outfile = open(out, 'w') -for file in args: - infile = open(file, 'r') - for l in infile.readlines(): - if l[:7] != '/*jar*/': - outfile.write(l) -sys.exit(0) -""") - -test.write('SConstruct', """ -DefaultEnvironment(tools=[]) -env = Environment(tools = ['jar'], - JAR = r'%(_python_)s myjar.py') -env.Jar(target = 'test1.jar', source = 'test1.class') -""" % locals()) - -test.write('test1.class', """\ -test1.class -/*jar*/ -line 3 -""") - -test.run(arguments='.', stderr=None) - -test.must_match('test1.jar', "test1.class\nline 3\n", mode='r') - -if os.path.normcase('.class') == os.path.normcase('.CLASS'): - - test.write('SConstruct', """ -DefaultEnvironment(tools=[]) -env = Environment(tools = ['jar'], - JAR = r'%(_python_)s myjar.py') -env.Jar(target = 'test2.jar', source = 'test2.CLASS') -""" % locals()) - - test.write('test2.CLASS', """\ -test2.CLASS -/*jar*/ -line 3 -""") - - test.run(arguments='.', stderr=None) - - test.must_match('test2.jar', "test2.CLASS\nline 3\n", mode='r') - -test.write('myjar2.py', r""" -import sys -f=open(sys.argv[2], 'w') -f.write(" ".join(sys.argv[1:])) -f.write("\n") -f.close() -sys.exit(0) -""") - -test.write('SConstruct', """ -DefaultEnvironment(tools=[]) -env = Environment(tools = ['jar'], - JAR = r'%(_python_)s myjar2.py', - JARFLAGS='cvf') -env.Jar(target = 'classes.jar', source = [ 'testdir/bar.class', - 'foo.mf' ], - TESTDIR='testdir', - JARCHDIR='$TESTDIR') -""" % locals()) - -test.subdir('testdir') -test.write(['testdir', 'bar.class'], 'foo') -test.write('foo.mf', - """Manifest-Version : 1.0 - blah - blah - blah - """) -test.run(arguments='classes.jar') -test.must_match('classes.jar', - 'cvfm classes.jar foo.mf -C testdir bar.class\n', mode='r') - test.file_fixture('wrapper_with_args.py') -test.write('SConstruct', """ +test.write('SConstruct', """\ DefaultEnvironment(tools=[]) -foo = Environment(tools = ['javac', 'jar']) +foo = Environment(tools=['javac', 'jar']) # jar = foo.Dictionary('JAR') -bar = foo.Clone(JAR = r'%(_python_)s wrapper_with_args.py jar') -foo.Java(target = 'classes', source = 'com/sub/foo') -bar.Java(target = 'classes', source = 'com/sub/bar') -foo.Jar(target = 'foo', source = 'classes/com/sub/foo') -bar.Jar(target = 'bar', source = Dir('classes/com/sub/bar')) +bar = foo.Clone(JAR=r'%(_python_)s wrapper_with_args.py jar') +foo.Java(target='classes', source='com/sub/foo') +bar.Java(target='classes', source='com/sub/bar') +foo.Jar(target='foo', source='classes/com/sub/foo') +bar.Jar(target='bar', source=Dir('classes/com/sub/bar')) """ % locals()) -test.subdir('com', - ['com', 'sub'], - ['com', 'sub', 'foo'], - ['com', 'sub', 'bar']) +test.subdir('com', ['com', 'sub'], ['com', 'sub', 'foo'], ['com', 'sub', 'bar']) test.write(['com', 'sub', 'foo', 'Example1.java'], """\ package com.sub.foo; @@ -228,13 +142,12 @@ test.run(arguments = '.') expected_wrapper_out = "wrapper_with_args.py jar cf bar.jar classes/com/sub/bar\n" expected_wrapper_out = expected_wrapper_out.replace('/', os.sep) -test.must_match('wrapper.out', - expected_wrapper_out % locals(), mode='r') +test.must_match('wrapper.out', expected_wrapper_out % locals(), mode='r') test.must_exist('foo.jar') test.must_exist('bar.jar') -test.up_to_date(arguments = '.') +test.up_to_date(arguments='.') ####### # test java source files as source to Jar builder @@ -250,22 +163,43 @@ test.write(['testdir2', 'SConstruct'], """ DefaultEnvironment(tools=[]) foo = Environment() -foo.Jar(target = 'foobar', source = [ - 'com/javasource/JavaFile1.java', - 'com/javasource/JavaFile2.java', - 'com/javasource/JavaFile3.java' -]) -foo.Jar(target = ['foo', 'bar'], source = [ - 'com/javasource/JavaFile1.java', - 'com/javasource/JavaFile2.java', - 'com/javasource/JavaFile3.java' -]) -foo.Command("foobarTest", [], Mkdir("foobarTest") ) -foo.Command('foobarTest/com/javasource/JavaFile3.java', 'foobar.jar', foo['JAR'] + ' xvf ../foobar.jar', chdir='foobarTest') -foo.Command("fooTest", [], Mkdir("fooTest") ) -foo.Command('fooTest/com/javasource/JavaFile3.java', 'foo.jar', foo['JAR'] + ' xvf ../foo.jar', chdir='fooTest') -foo.Command("barTest", [], Mkdir("barTest") ) -foo.Command('barTest/com/javasource/JavaFile3.java', 'bar.jar', foo['JAR'] + ' xvf ../bar.jar', chdir='barTest') +foo.Jar( + target='foobar', + source=[ + 'com/javasource/JavaFile1.java', + 'com/javasource/JavaFile2.java', + 'com/javasource/JavaFile3.java', + ], +) +foo.Jar( + target=['foo', 'bar'], + source=[ + 'com/javasource/JavaFile1.java', + 'com/javasource/JavaFile2.java', + 'com/javasource/JavaFile3.java', + ], +) +foo.Command("foobarTest", [], Mkdir("foobarTest")) +foo.Command( + 'foobarTest/com/javasource/JavaFile3.java', + 'foobar.jar', + foo['JAR'] + ' xvf ../foobar.jar', + chdir='foobarTest', +) +foo.Command("fooTest", [], Mkdir("fooTest")) +foo.Command( + 'fooTest/com/javasource/JavaFile3.java', + 'foo.jar', + foo['JAR'] + ' xvf ../foo.jar', + chdir='fooTest', +) +foo.Command("barTest", [], Mkdir("barTest")) +foo.Command( + 'barTest/com/javasource/JavaFile3.java', + 'bar.jar', + foo['JAR'] + ' xvf ../bar.jar', + chdir='barTest', +) """) test.write(['testdir2', 'com', 'javasource', 'JavaFile1.java'], """\ @@ -309,11 +243,7 @@ public class JavaFile3 # use regex . for dirsep so this will work on both windows and other platforms. expect = ".*jar cf foo.jar -C com.javasource.JavaFile1 com.javasource.JavaFile1.class -C com.javasource.JavaFile2 com.javasource.JavaFile2.class -C com.javasource.JavaFile3 com.javasource.JavaFile3.class.*" -test.run(chdir='testdir2', - match=TestSCons.match_re_dotall, - stdout = expect) - - +test.run(chdir='testdir2', match=TestSCons.match_re_dotall, stdout=expect) #test single target jar test.must_exist(['testdir2','foobar.jar']) @@ -338,12 +268,14 @@ test.must_exist(['testdir2', 'barTest', 'com', 'javasource', 'JavaFile3.class']) # test list of lists # make some directories to test in -test.subdir('listOfLists', - ['manifest_dir'], - ['listOfLists', 'src'], - ['listOfLists', 'src', 'com'], - ['listOfLists', 'src', 'com', 'javasource'], - ['listOfLists', 'src', 'com', 'resource']) +test.subdir( + 'listOfLists', + ['manifest_dir'], + ['listOfLists', 'src'], + ['listOfLists', 'src', 'com'], + ['listOfLists', 'src', 'com', 'javasource'], + ['listOfLists', 'src', 'com', 'resource'], +) # test varient dir and lists of lists test.write(['listOfLists', 'SConstruct'], """ @@ -352,15 +284,26 @@ DefaultEnvironment(tools=[]) foo = Environment() foo.VariantDir('build', 'src', duplicate=0) foo.VariantDir('test', '../manifest_dir', duplicate=0) -sourceFiles = ["src/com/javasource/JavaFile1.java", "src/com/javasource/JavaFile2.java", "src/com/javasource/JavaFile3.java",] +sourceFiles = [ + "src/com/javasource/JavaFile1.java", + "src/com/javasource/JavaFile2.java", + "src/com/javasource/JavaFile3.java", +] list_of_class_files = foo.Java('build', source=sourceFiles) resources = ['build/com/resource/resource1.txt', 'build/com/resource/resource2.txt'] for resource in resources: - foo.Command(resource, list_of_class_files, Copy(resource, resource.replace('build','src'))) + foo.Command( + resource, list_of_class_files, Copy(resource, resource.replace('build', 'src')) + ) contents = [list_of_class_files, resources] -foo.Jar(target = 'lists', source = contents + ['test/MANIFEST.mf'], JARCHDIR='build') -foo.Command("listsTest", [], Mkdir("listsTest") ) -foo.Command('listsTest/src/com/javasource/JavaFile3.java', 'lists.jar', foo['JAR'] + ' xvf ../lists.jar', chdir='listsTest') +foo.Jar(target='lists', source=contents + ['test/MANIFEST.mf'], JARCHDIR='build') +foo.Command("listsTest", [], Mkdir("listsTest")) +foo.Command( + 'listsTest/src/com/javasource/JavaFile3.java', + 'lists.jar', + foo['JAR'] + ' xvf ../lists.jar', + chdir='listsTest', +) """) test.write(['listOfLists', 'src', 'com', 'javasource', 'JavaFile1.java'], """\ @@ -431,25 +374,34 @@ test.must_contain(['listOfLists', 'listsTest', 'META-INF', 'MANIFEST.MF'], b"MyM # test different style of passing in dirs # make some directories to test in -test.subdir('testdir3', - ['testdir3', 'com'], - ['testdir3', 'com', 'sub'], - ['testdir3', 'com', 'sub', 'foo'], - ['testdir3', 'com', 'sub', 'bar']) +test.subdir( + 'testdir3', + ['testdir3', 'com'], + ['testdir3', 'com', 'sub'], + ['testdir3', 'com', 'sub', 'foo'], + ['testdir3', 'com', 'sub', 'bar'], +) # Create the jars then extract them back to check contents test.write(['testdir3', 'SConstruct'], """ DefaultEnvironment(tools=[]) foo = Environment() -bar = foo.Clone() -foo.Java(target = 'classes', source = 'com/sub/foo') -bar.Java(target = 'classes', source = 'com/sub/bar') -foo.Jar(target = 'foo', source = 'classes/com/sub/foo', JARCHDIR='classes') -bar.Jar(target = 'bar', source = Dir('classes/com/sub/bar'), JARCHDIR='classes') -foo.Command("fooTest", 'foo.jar', Mkdir("fooTest") ) +foo_cls = foo.Java(target='classes', source='com/sub/foo') +foo_res = 'classes/com/sub/foo/NonJava.txt' +foo_res_src = 'com/sub/foo/NonJava.txt' +foo.Command(foo_res, foo_cls, Copy(foo_res, foo_res_src)) +foo.Jar(target='foo', source='classes/com/sub/foo', JARCHDIR='classes') +foo.Command("fooTest", 'foo.jar', Mkdir("fooTest")) foo.Command('doesnt_exist1', "fooTest", foo['JAR'] + ' xvf ../foo.jar', chdir='fooTest') -bar.Command("barTest", 'bar.jar', Mkdir("barTest") ) + +bar = foo.Clone() +bar_cls = bar.Java(target='classes', source='com/sub/bar') +bar_res = 'classes/com/sub/bar/NonJava.txt' +bar_res_src = 'com/sub/bar/NonJava.txt' +bar.Command(bar_res, bar_cls, Copy(bar_res, bar_res_src)) +bar.Jar(target='bar', source=Dir('classes/com/sub/bar'), JARCHDIR='classes') +bar.Command("barTest", 'bar.jar', Mkdir("barTest")) bar.Command('doesnt_exist2', 'barTest', bar['JAR'] + ' xvf ../bar.jar', chdir='barTest') """) @@ -549,7 +501,6 @@ test.run(chdir='testdir3') # check the output and make sure the java files got converted to classes - # make sure there are class in the jar test.must_exist(['testdir3','foo.jar']) test.must_exist(['testdir3', 'fooTest', 'com', 'sub', 'foo', 'Example1.class']) @@ -557,7 +508,7 @@ test.must_exist(['testdir3', 'fooTest', 'com', 'sub', 'foo', 'Example2.class']) test.must_exist(['testdir3', 'fooTest', 'com', 'sub', 'foo', 'Example3.class']) # TODO: determine expected behavior with resource files, should they be # automatically copied in or specified in seperate commands -#test.must_exist(['testdir3', 'fooTest', 'com', 'sub', 'foo', 'NonJava.txt']) +test.must_exist(['testdir3', 'fooTest', 'com', 'sub', 'foo', 'NonJava.txt']) # make sure both jars got createds test.must_exist(['testdir3','bar.jar']) @@ -566,11 +517,10 @@ test.must_exist(['testdir3', 'barTest', 'com', 'sub', 'bar', 'Example5.class']) test.must_exist(['testdir3', 'barTest', 'com', 'sub', 'bar', 'Example6.class']) # TODO: determine expected behavior with resource files, should they be # automatically copied in or specified in seperate commands -#test.must_exist(['testdir3', 'fooTest', 'com', 'sub', 'bar', 'NonJava.txt']) +test.must_exist(['testdir3', 'barTest', 'com', 'sub', 'bar', 'NonJava.txt']) test.pass_test() - # Local Variables: # tab-width:4 # indent-tabs-mode:nil diff --git a/test/Java/JAVAC.py b/test/Java/JAVAC.py index 20f7f96..f303e87 100644 --- a/test/Java/JAVAC.py +++ b/test/Java/JAVAC.py @@ -25,6 +25,8 @@ """ Test setting the JAVAC variable. + +This test does not require a JDK to operate. """ import os @@ -35,33 +37,11 @@ _python_ = TestSCons._python_ test = TestSCons.TestSCons() +test.file_fixture(['Java-fixture', 'myjavac.py']) - -test.write('myjavac.py', r""" -import sys -args = sys.argv[1:] -while args: - a = args[0] - if a == '-d': - args = args[1:] - elif a == '-sourcepath': - args = args[1:] - else: - break - args = args[1:] -for file in args: - infile = open(file, 'r') - outfile = open(file[:-5] + '.class', 'w') - for l in infile.readlines(): - if l[:9] != '/*javac*/': - outfile.write(l) -sys.exit(0) -""") - -test.write('SConstruct', """ -env = Environment(tools = ['javac'], - JAVAC = r'%(_python_)s myjavac.py') -env.Java(target = '.', source = '.') +test.write('SConstruct', """\ +env = Environment(tools=['javac'], JAVAC=r'%(_python_)s myjavac.py') +env.Java(target='.', source='.') """ % locals()) test.write('test1.java', """\ @@ -71,15 +51,12 @@ line 3 """) test.run(arguments='.', stderr=None) - test.must_match('test1.class', "test1.java\nline 3\n", mode='r') if os.path.normcase('.java') == os.path.normcase('.JAVA'): - test.write('SConstruct', """\ -env = Environment(tools = ['javac'], - JAVAC = r'%(_python_)s myjavac.py') -env.Java(target = '.', source = '.') +env = Environment(tools=['javac'], JAVAC=r'%(_python_)s myjavac.py') +env.Java(target='.', source='.') """ % locals()) test.write('test2.JAVA', """\ @@ -89,11 +66,8 @@ line 3 """) test.run(arguments='.', stderr=None) - test.must_match('test2.class', "test2.JAVA\nline 3\n", mode='r') - - test.pass_test() # Local Variables: diff --git a/test/Java/Java-fixture/.exclude_tests b/test/Java/Java-fixture/.exclude_tests new file mode 100644 index 0000000..37004e6 --- /dev/null +++ b/test/Java/Java-fixture/.exclude_tests @@ -0,0 +1,2 @@ +myjar.py +myjavac.py diff --git a/test/Java/Java-fixture/myjar.py b/test/Java/Java-fixture/myjar.py new file mode 100644 index 0000000..a47e3b0 --- /dev/null +++ b/test/Java/Java-fixture/myjar.py @@ -0,0 +1,19 @@ +import fileinput +import sys + +args = sys.argv[1:] +while args: + arg = args[0] + if arg == 'cf': + out = args[1] + args = args[1:] + else: + break + args = args[1:] + +with open(out, 'wb') as ofp, fileinput.input(files=args, mode='rb') as ifp: + for line in ifp: + if not line.startswith(b'/*jar*/'): + ofp.write(line) + +sys.exit(0) diff --git a/test/Java/Java-fixture/myjavac.py b/test/Java/Java-fixture/myjavac.py new file mode 100644 index 0000000..0177c51 --- /dev/null +++ b/test/Java/Java-fixture/myjavac.py @@ -0,0 +1,22 @@ +import fileinput +import sys + +args = sys.argv[1:] +while args: + arg = args[0] + if arg == '-d': + args = args[1:] + elif arg == '-sourcepath': + args = args[1:] + else: + break + args = args[1:] + +for file in args: + out = file.lower().replace('.java', '.class') + with open(file, 'rb') as infile, open(out, 'wb') as outfile: + for line in infile: + if not line.startswith(b'/*javac*/'): + outfile.write(line) + +sys.exit(0) diff --git a/test/Java/jar_not_in_PATH.py b/test/Java/jar_not_in_PATH.py index 26e92ce..05bb9c8 100644 --- a/test/Java/jar_not_in_PATH.py +++ b/test/Java/jar_not_in_PATH.py @@ -35,33 +35,16 @@ _python_ = TestSCons._python_ test = TestSCons.TestSCons() -test.write('myjar.py', r""" -import sys -args = sys.argv[1:] -while args: - a = args[0] - if a == 'cf': - out = args[1] - args = args[1:] - else: - break - args = args[1:] -outfile = open(out, 'wb') -for file in args: - infile = open(file, 'rb') - for l in infile.readlines(): - if l[:7] != '/*jar*/': - outfile.write(l) -sys.exit(0) -""") +test.file_fixture(['Java-fixture', 'myjar.py']) -test.write('SConstruct', """ +test.write('SConstruct', """\ import os -oldpath = os.environ.get('PATH','') -env = Environment(ENV = {'PATH' : ['.']}) + +oldpath = os.environ.get('PATH', '') +env = Environment(ENV={'PATH': ['.']}) env['ENV']['PATH'] = oldpath env['JAR'] = r'%(_python_)s ./myjar.py' -env.Jar(target = 'test1.jar', source = 'test1.class') +env.Jar(target='test1.jar', source='test1.class') """ % locals()) test.write('test1.class', """\ @@ -70,7 +53,7 @@ test1.class line 3 """) -test.run(arguments = '.', stderr = None) +test.run(arguments='.', stderr=None) test.must_exist('test1.jar') |