3434import com .google .devtools .build .lib .buildtool .util .BuildIntegrationTestCase ;
3535import com .google .devtools .build .lib .skyframe .ActionExecutionValue ;
3636import com .google .devtools .build .lib .skyframe .TreeArtifactValue ;
37+ import com .google .devtools .build .lib .testutil .TestUtils ;
3738import com .google .devtools .build .lib .util .CommandBuilder ;
3839import com .google .devtools .build .lib .util .OS ;
3940import com .google .devtools .build .lib .util .io .RecordingOutErr ;
@@ -1032,9 +1033,10 @@ public void downloadToplevel_symlinkToDirectory() throws Exception {
10321033 }
10331034
10341035 @ Test
1035- public void downloadToplevel_unresolvedSymlink () throws Exception {
1036- // Dangling symlink would require developer mode to be enabled in the CI environment .
1036+ public void downloadToplevel_unresolvedSymlink_unspecified () throws Exception {
1037+ // Windows cannot create dangling symlinks without knowing the target type .
10371038 assumeFalse (OS .getCurrent () == OS .WINDOWS );
1039+ Path targetPath = TestUtils .createUniqueTmpDir (null ).getChild ("target" );
10381040
10391041 setDownloadToplevel ();
10401042 writeSymlinkRule ();
@@ -1043,18 +1045,83 @@ public void downloadToplevel_unresolvedSymlink() throws Exception {
10431045 "load(':symlink.bzl', 'symlink')" ,
10441046 "symlink(" ,
10451047 " name = 'foo-link'," ,
1046- " target_path = '/some/path '," ,
1048+ " target_path = '" + targetPath . getPathString () + " '," ,
10471049 ")" );
10481050
10491051 buildTarget ("//:foo-link" );
10501052
1051- assertSymlink ("foo-link" , PathFragment . create ( "/some/path" ));
1053+ assertSymlink ("foo-link" , targetPath . asFragment ( ));
10521054
10531055 // Delete link, re-plant symlink
10541056 getOutputPath ("foo-link" ).delete ();
10551057 buildTarget ("//:foo-link" );
10561058
1057- assertSymlink ("foo-link" , PathFragment .create ("/some/path" ));
1059+ assertSymlink ("foo-link" , targetPath .asFragment ());
1060+ }
1061+
1062+ @ Test
1063+ public void downloadToplevel_unresolvedSymlink_file () throws Exception {
1064+ // File symlinks on Windows require Developer Mode or admin privileges.
1065+ assumeFalse (OS .getCurrent () == OS .WINDOWS );
1066+ Path targetPath = TestUtils .createUniqueTmpDir (null ).getChild ("target" );
1067+
1068+ setDownloadToplevel ();
1069+ writeSymlinkRule ();
1070+ write (
1071+ "BUILD" ,
1072+ "load(':symlink.bzl', 'symlink')" ,
1073+ "symlink(" ,
1074+ " name = 'foo-link'," ,
1075+ " target_path = '" + targetPath .getPathString () + "'," ,
1076+ " target_type = 'file'," ,
1077+ ")" );
1078+
1079+ buildTarget ("//:foo-link" );
1080+
1081+ assertSymlink ("foo-link" , targetPath .asFragment ());
1082+
1083+ // Delete link, re-plant symlink
1084+ getOutputPath ("foo-link" ).delete ();
1085+ buildTarget ("//:foo-link" );
1086+
1087+ assertSymlink ("foo-link" , targetPath .asFragment ());
1088+
1089+ // Assert that the symlink works after planting the target.
1090+ FileSystemUtils .writeContent (targetPath , UTF_8 , "hello world" );
1091+ assertThat (FileSystemUtils .readContent (getOutputPath ("foo-link" ), UTF_8 ))
1092+ .isEqualTo ("hello world" );
1093+ }
1094+
1095+ @ Test
1096+ public void downloadToplevel_unresolvedSymlink_directory () throws Exception {
1097+ Path targetPath = TestUtils .createUniqueTmpDir (null ).getChild ("target" );
1098+
1099+ setDownloadToplevel ();
1100+ writeSymlinkRule ();
1101+ write (
1102+ "BUILD" ,
1103+ "load(':symlink.bzl', 'symlink')" ,
1104+ "symlink(" ,
1105+ " name = 'foo-link'," ,
1106+ " target_path = '" + targetPath .getPathString () + "'," ,
1107+ " target_type = 'directory'," ,
1108+ ")" );
1109+
1110+ buildTarget ("//:foo-link" );
1111+
1112+ assertSymlink ("foo-link" , targetPath .asFragment ());
1113+
1114+ // Delete link, re-plant symlink
1115+ getOutputPath ("foo-link" ).delete ();
1116+ buildTarget ("//:foo-link" );
1117+
1118+ assertSymlink ("foo-link" , targetPath .asFragment ());
1119+
1120+ // Assert that the symlink works after planting the target.
1121+ targetPath .createDirectory ();
1122+ FileSystemUtils .writeContent (targetPath .getChild ("file.txt" ), UTF_8 , "hello world" );
1123+ assertThat (FileSystemUtils .readContent (getOutputPath ("foo-link/file.txt" ), UTF_8 ))
1124+ .isEqualTo ("hello world" );
10581125 }
10591126
10601127 @ Test
0 commit comments