@@ -694,5 +694,73 @@ def testRunfilesRepoMappingManifest(self):
694
694
self .Path ('bazel-bin/external/bar~2.0/bar.runfiles_manifest' )) as f :
695
695
self .assertIn ('_repo_mapping ' , f .read ())
696
696
697
+ def testJavaRunfilesLibraryRepoMapping (self ):
698
+ self .main_registry .setModuleBasePath ('projects' )
699
+ projects_dir = self .main_registry .projects
700
+
701
+ self .main_registry .createLocalPathModule ('data' , '1.0' , 'data' )
702
+ projects_dir .joinpath ('data' ).mkdir (exist_ok = True )
703
+ scratchFile (projects_dir .joinpath ('data' , 'WORKSPACE' ))
704
+ scratchFile (projects_dir .joinpath ('data' , 'foo.txt' ), ['hello' ])
705
+ scratchFile (
706
+ projects_dir .joinpath ('data' , 'BUILD' ), ['exports_files(["foo.txt"])' ])
707
+
708
+ self .main_registry .createLocalPathModule ('test' , '1.0' , 'test' ,
709
+ {'data' : '1.0' })
710
+ projects_dir .joinpath ('test' ).mkdir (exist_ok = True )
711
+ scratchFile (projects_dir .joinpath ('test' , 'WORKSPACE' ))
712
+ scratchFile (
713
+ projects_dir .joinpath ('test' , 'BUILD' ), [
714
+ 'java_test(' ,
715
+ ' name = "test",' ,
716
+ ' srcs = ["Test.java"],' ,
717
+ ' main_class = "com.example.Test",' ,
718
+ ' use_testrunner = False,' ,
719
+ ' data = ["@data//:foo.txt"],' ,
720
+ ' args = ["$(rlocationpath @data//:foo.txt)"],' ,
721
+ ' deps = ["@bazel_tools//tools/java/runfiles"],' ,
722
+ ')' ,
723
+ ])
724
+ scratchFile (
725
+ projects_dir .joinpath ('test' , 'Test.java' ), [
726
+ 'package com.example;' ,
727
+ '' ,
728
+ 'import com.google.devtools.build.runfiles.AutoBazelRepository;' ,
729
+ 'import com.google.devtools.build.runfiles.Runfiles;' ,
730
+ '' ,
731
+ 'import java.io.File;' ,
732
+ 'import java.io.IOException;' ,
733
+ '' ,
734
+ '@AutoBazelRepository' ,
735
+ 'public class Test {' ,
736
+ ' public static void main(String[] args) throws IOException {' ,
737
+ ' Runfiles.Preloaded rp = Runfiles.preload();' ,
738
+ ' if (!new File(rp.unmapped().rlocation(args[0])).exists()) {' ,
739
+ ' System.exit(1);' ,
740
+ ' }' ,
741
+ ' if (!new File(rp.withSourceRepository(AutoBazelRepository_Test.NAME).rlocation("data/foo.txt")).exists()) {' ,
742
+ ' System.exit(1);' ,
743
+ ' }' ,
744
+ ' }' ,
745
+ '}' ,
746
+ ])
747
+
748
+ self .ScratchFile ('MODULE.bazel' , ['bazel_dep(name="test",version="1.0")' ])
749
+ self .ScratchFile ('WORKSPACE' )
750
+
751
+ # Run sandboxed on Linux and macOS.
752
+ exit_code , stderr , stdout = self .RunBazel ([
753
+ 'test' , '@test//:test' , '--test_output=errors' ,
754
+ '--test_env=RUNFILES_LIB_DEBUG=1'
755
+ ],
756
+ allow_failure = True )
757
+ self .AssertExitCode (exit_code , 0 , stderr , stdout )
758
+ # Run unsandboxed on all platforms.
759
+ exit_code , stderr , stdout = self .RunBazel (
760
+ ['run' , '@test//:test' ],
761
+ allow_failure = True ,
762
+ env_add = {'RUNFILES_LIB_DEBUG' : '1' })
763
+ self .AssertExitCode (exit_code , 0 , stderr , stdout )
764
+
697
765
if __name__ == '__main__' :
698
766
unittest .main ()
0 commit comments