@@ -1763,5 +1763,191 @@ EOF
1763
1763
bazel build //java/main:C2 &>"${TEST_log}" || fail "Expected to build"
1764
1764
}
1765
1765
1766
+ function test_auto_bazel_repository() {
1767
+ cat >> WORKSPACE <<'EOF'
1768
+ local_repository(
1769
+ name = "other_repo",
1770
+ path = "other_repo",
1771
+ )
1772
+ EOF
1773
+
1774
+ mkdir -p pkg
1775
+ cat > pkg/BUILD.bazel <<'EOF'
1776
+ java_library(
1777
+ name = "library",
1778
+ srcs = ["Library.java"],
1779
+ deps = ["@bazel_tools//tools/java/runfiles"],
1780
+ visibility = ["//visibility:public"],
1781
+ )
1782
+
1783
+ java_binary(
1784
+ name = "binary",
1785
+ srcs = ["Binary.java"],
1786
+ main_class = "com.example.Binary",
1787
+ deps = [
1788
+ ":library",
1789
+ "@bazel_tools//tools/java/runfiles",
1790
+ ],
1791
+ )
1792
+
1793
+ java_test(
1794
+ name = "test",
1795
+ srcs = ["Test.java"],
1796
+ main_class = "com.example.Test",
1797
+ use_testrunner = False,
1798
+ deps = [
1799
+ ":library",
1800
+ "@bazel_tools//tools/java/runfiles",
1801
+ ],
1802
+ )
1803
+ EOF
1804
+
1805
+ cat > pkg/Library.java <<'EOF'
1806
+ package com.example;
1807
+
1808
+ import com.google.devtools.build.runfiles.AutoBazelRepository;
1809
+
1810
+ @AutoBazelRepository
1811
+ public class Library {
1812
+ public static void printRepositoryName() {
1813
+ System.out.printf("in pkg/Library.java: '%s'%n", AutoBazelRepository_Library.NAME);
1814
+ }
1815
+ }
1816
+ EOF
1817
+
1818
+ cat > pkg/Binary.java <<'EOF'
1819
+ package com.example;
1820
+
1821
+ import com.google.devtools.build.runfiles.AutoBazelRepository;
1822
+
1823
+ public class Binary {
1824
+ @AutoBazelRepository
1825
+ private static class Class1 {
1826
+ }
1827
+
1828
+ public static void main(String[] args) {
1829
+ System.out.printf("in pkg/Binary.java: '%s'%n", AutoBazelRepository_Binary_Class1.NAME);
1830
+ Library.printRepositoryName();
1831
+ }
1832
+ }
1833
+ EOF
1834
+
1835
+ cat > pkg/Test.java <<'EOF'
1836
+ package com.example;
1837
+
1838
+ import com.google.devtools.build.runfiles.AutoBazelRepository;
1839
+
1840
+ public class Test {
1841
+ private static class Class1 {
1842
+ @AutoBazelRepository
1843
+ private static class Class2 {
1844
+ }
1845
+ }
1846
+
1847
+ public static void main(String[] args) {
1848
+ System.out.printf("in pkg/Test.java: '%s'%n", AutoBazelRepository_Test_Class1_Class2.NAME);
1849
+ Library.printRepositoryName();
1850
+ }
1851
+ }
1852
+ EOF
1853
+
1854
+ mkdir -p other_repo
1855
+ touch other_repo/WORKSPACE
1856
+
1857
+ mkdir -p other_repo/pkg
1858
+ cat > other_repo/pkg/BUILD.bazel <<'EOF'
1859
+ java_library(
1860
+ name = "library2",
1861
+ srcs = ["Library2.java"],
1862
+ deps = ["@bazel_tools//tools/java/runfiles"],
1863
+ )
1864
+
1865
+ java_binary(
1866
+ name = "binary",
1867
+ srcs = ["Binary.java"],
1868
+ main_class = "com.example.Binary",
1869
+ deps = [
1870
+ ":library2",
1871
+ "@//pkg:library",
1872
+ "@bazel_tools//tools/java/runfiles",
1873
+ ],
1874
+ )
1875
+ java_test(
1876
+ name = "test",
1877
+ srcs = ["Test.java"],
1878
+ main_class = "com.example.Test",
1879
+ use_testrunner = False,
1880
+ deps = [
1881
+ ":library2",
1882
+ "@//pkg:library",
1883
+ "@bazel_tools//tools/java/runfiles",
1884
+ ],
1885
+ )
1886
+ EOF
1887
+
1888
+ cat > other_repo/pkg/Library2.java <<'EOF'
1889
+ package com.example;
1890
+
1891
+ import com.google.devtools.build.runfiles.AutoBazelRepository;
1892
+
1893
+ @AutoBazelRepository
1894
+ public class Library2 {
1895
+ public static void printRepositoryName() {
1896
+ System.out.printf("in external/other_repo/pkg/Library2.java: '%s'%n", AutoBazelRepository_Library2.NAME);
1897
+ }
1898
+ }
1899
+ EOF
1900
+
1901
+ cat > other_repo/pkg/Binary.java <<'EOF'
1902
+ package com.example;
1903
+
1904
+ import com.google.devtools.build.runfiles.AutoBazelRepository;
1905
+ import static com.example.AutoBazelRepository_Binary.NAME;
1906
+
1907
+ @AutoBazelRepository
1908
+ public class Binary {
1909
+ public static void main(String[] args) {
1910
+ System.out.printf("in external/other_repo/pkg/Binary.java: '%s'%n", NAME);
1911
+ Library2.printRepositoryName();
1912
+ Library.printRepositoryName();
1913
+ }
1914
+ }
1915
+ EOF
1916
+
1917
+ cat > other_repo/pkg/Test.java <<'EOF'
1918
+ package com.example;
1919
+
1920
+ import com.google.devtools.build.runfiles.AutoBazelRepository;
1921
+
1922
+ @AutoBazelRepository
1923
+ public class Test {
1924
+ public static void main(String[] args) {
1925
+ System.out.printf("in external/other_repo/pkg/Test.java: '%s'%n", AutoBazelRepository_Test.NAME);
1926
+ Library2.printRepositoryName();
1927
+ Library.printRepositoryName();
1928
+ }
1929
+ }
1930
+ EOF
1931
+
1932
+ bazel run //pkg:binary &>"$TEST_log" || fail "Run should succeed"
1933
+ expect_log "in pkg/Binary.java: ''"
1934
+ expect_log "in pkg/Library.java: ''"
1935
+
1936
+ bazel test --test_output=streamed //pkg:test &>"$TEST_log" || fail "Test should succeed"
1937
+ expect_log "in pkg/Test.java: ''"
1938
+ expect_log "in pkg/Library.java: ''"
1939
+
1940
+ bazel run @other_repo//pkg:binary &>"$TEST_log" || fail "Run should succeed"
1941
+ expect_log "in external/other_repo/pkg/Binary.java: 'other_repo'"
1942
+ expect_log "in external/other_repo/pkg/Library2.java: 'other_repo'"
1943
+ expect_log "in pkg/Library.java: ''"
1944
+
1945
+ bazel test --test_output=streamed \
1946
+ @other_repo//pkg:test &>"$TEST_log" || fail "Test should succeed"
1947
+ expect_log "in external/other_repo/pkg/Test.java: 'other_repo'"
1948
+ expect_log "in external/other_repo/pkg/Library2.java: 'other_repo'"
1949
+ expect_log "in pkg/Library.java: ''"
1950
+ }
1951
+
1766
1952
1767
1953
run_suite "Java integration tests"
0 commit comments