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