@@ -22,18 +22,24 @@ var emptyLeaves = &model_filesystem_pb.DirectoryContents_LeavesInline{
2222 LeavesInline : & model_filesystem_pb.Leaves {},
2323}
2424
25+ func directoryNode (name string , contents * model_filesystem_pb.DirectoryContents ) * model_filesystem_pb.DirectoryNode {
26+ return & model_filesystem_pb.DirectoryNode {
27+ Name : name ,
28+ Directory : & model_filesystem_pb.Directory {
29+ Contents : & model_filesystem_pb.Directory_ContentsInline {
30+ ContentsInline : contents ,
31+ },
32+ },
33+ }
34+ }
35+
2536// singleChildDirectoryContents can be used to construct a directory
2637// that only contains a single child that is also a directory.
2738func singleChildDirectoryContents (name string , childContents * model_filesystem_pb.DirectoryContents ) * model_filesystem_pb.DirectoryContents {
2839 return & model_filesystem_pb.DirectoryContents {
29- Directories : []* model_filesystem_pb.DirectoryNode {{
30- Name : name ,
31- Directory : & model_filesystem_pb.Directory {
32- Contents : & model_filesystem_pb.Directory_ContentsInline {
33- ContentsInline : childContents ,
34- },
35- },
36- }},
40+ Directories : []* model_filesystem_pb.DirectoryNode {
41+ directoryNode (name , childContents ),
42+ },
3743 Leaves : emptyLeaves ,
3844 }
3945}
@@ -674,6 +680,201 @@ func TestFileRoot(t *testing.T) {
674680 })
675681
676682 t .Run ("Symlink" , func (t * testing.T ) {
677- // TODO!
683+ // TODO: Test error cases.
684+
685+ t .Run ("Success" , func (t * testing.T ) {
686+ // Simulate the computation of the output of:
687+ //
688+ // output = ctx.actions.declare_file("b")
689+ // ctx.actions.symlink(
690+ // output = output,
691+ // target_file = File("@@myrepo+//:a"),
692+ // )
693+ run := func (t * testing.T , directoryLayout model_analysis_pb.DirectoryLayout , target * model_filesystem_pb.DirectoryContents ) model_analysis.PatchedFileRootValue {
694+ e := NewMockFileRootEnvironmentForTesting (ctrl )
695+ bct .expectCaptureExistingObject (e )
696+ bct .expectGetDirectoryCreationParametersObjectValue (t , e )
697+ bct .expectGetDirectoryReadersValue (t , e )
698+ e .EXPECT ().GetTargetOutputValue (
699+ eqPatchedMessage (func (patcher * model_core.ReferenceMessagePatcher [model_core.CreatedObjectTree ]) * model_analysis_pb.TargetOutput_Key {
700+ return & model_analysis_pb.TargetOutput_Key {
701+ Label : "@@myrepo+//:create_symlink" ,
702+ ConfigurationReference : attachObject (patcher , exampleConfiguration ),
703+ PackageRelativePath : "b" ,
704+ }
705+ }),
706+ ).Return (newMessage (func (patcher * model_core.ReferenceMessagePatcher [model_core.CreatedObjectTree ]) * model_analysis_pb.TargetOutput_Value {
707+ return & model_analysis_pb.TargetOutput_Value {
708+ Definition : & model_analysis_pb.TargetOutputDefinition {
709+ Source : & model_analysis_pb.TargetOutputDefinition_Symlink_ {
710+ Symlink : & model_analysis_pb.TargetOutputDefinition_Symlink {
711+ Target : & model_starlark_pb.File {
712+ Label : "@@myrepo+//:a" ,
713+ Type : model_starlark_pb .File_FILE ,
714+ },
715+ },
716+ },
717+ },
718+ }
719+ }))
720+ e .EXPECT ().GetFileRootValue (
721+ eqPatchedMessage (func (patcher * model_core.ReferenceMessagePatcher [model_core.CreatedObjectTree ]) * model_analysis_pb.FileRoot_Key {
722+ return & model_analysis_pb.FileRoot_Key {
723+ DirectoryLayout : directoryLayout ,
724+ File : & model_starlark_pb.File {
725+ Label : "@@myrepo+//:a" ,
726+ Type : model_starlark_pb .File_FILE ,
727+ },
728+ }
729+ }),
730+ ).Return (newMessage (func (patcher * model_core.ReferenceMessagePatcher [model_core.CreatedObjectTree ]) * model_analysis_pb.FileRoot_Value {
731+ return & model_analysis_pb.FileRoot_Value {
732+ RootDirectory : target ,
733+ }
734+ }))
735+
736+ fileRoot , err := bct .computer .ComputeFileRootValue (
737+ ctx ,
738+ newMessage (func (patcher * model_core.ReferenceMessagePatcher [model_core.CreatedObjectTree ]) * model_analysis_pb.FileRoot_Key {
739+ return & model_analysis_pb.FileRoot_Key {
740+ DirectoryLayout : directoryLayout ,
741+ File : & model_starlark_pb.File {
742+ Label : "@@myrepo+//:b" ,
743+ Type : model_starlark_pb .File_FILE ,
744+ Owner : & model_starlark_pb.File_Owner {
745+ ConfigurationReference : attachObject (patcher , exampleConfiguration ),
746+ TargetName : "create_symlink" ,
747+ },
748+ },
749+ }
750+ }),
751+ e ,
752+ )
753+ require .NoError (t , err )
754+ return fileRoot
755+ }
756+
757+ t .Run ("InputRoot" , func (t * testing.T ) {
758+ fileRoot := run (
759+ t ,
760+ model_analysis_pb .DirectoryLayout_INPUT_ROOT ,
761+ singleChildDirectoryContents (
762+ "external" ,
763+ singleChildDirectoryContents (
764+ "myrepo+" ,
765+ & model_filesystem_pb.DirectoryContents {
766+ Leaves : & model_filesystem_pb.DirectoryContents_LeavesInline {
767+ LeavesInline : & model_filesystem_pb.Leaves {
768+ Files : []* model_filesystem_pb.FileNode {
769+ {
770+ Name : "a" ,
771+ Properties : & model_filesystem_pb.FileProperties {},
772+ },
773+ },
774+ },
775+ },
776+ },
777+ ),
778+ ),
779+ )
780+ requireEqualPatchedMessage (t , func (patcher * model_core.ReferenceMessagePatcher [model_core.CreatedObjectTree ]) * model_analysis_pb.FileRoot_Value {
781+ return & model_analysis_pb.FileRoot_Value {
782+ RootDirectory : & model_filesystem_pb.DirectoryContents {
783+ Directories : []* model_filesystem_pb.DirectoryNode {
784+ directoryNode ("bazel-out" , singleChildDirectoryContents (
785+ "Cg6Kx80o8BPYmGdgWYfRZvbKyWojQ7snQzHOx70XAwRPAAAAAAAAAA." ,
786+ singleChildDirectoryContents (
787+ "bin" ,
788+ singleChildDirectoryContents (
789+ "external" ,
790+ singleChildDirectoryContents (
791+ "myrepo+" ,
792+ & model_filesystem_pb.DirectoryContents {
793+ Leaves : & model_filesystem_pb.DirectoryContents_LeavesInline {
794+ LeavesInline : & model_filesystem_pb.Leaves {
795+ Symlinks : []* model_filesystem_pb.SymlinkNode {
796+ {
797+ Name : "b" ,
798+ Target : "../../../../../external/myrepo+/a" ,
799+ },
800+ },
801+ },
802+ },
803+ },
804+ ),
805+ ),
806+ ),
807+ )),
808+ directoryNode ("external" , singleChildDirectoryContents (
809+ "myrepo+" ,
810+ & model_filesystem_pb.DirectoryContents {
811+ Leaves : & model_filesystem_pb.DirectoryContents_LeavesInline {
812+ LeavesInline : & model_filesystem_pb.Leaves {
813+ Files : []* model_filesystem_pb.FileNode {
814+ {
815+ Name : "a" ,
816+ Properties : & model_filesystem_pb.FileProperties {},
817+ },
818+ },
819+ },
820+ },
821+ },
822+ )),
823+ },
824+ Leaves : emptyLeaves ,
825+ },
826+ }
827+ }, fileRoot )
828+ fileRoot .Discard ()
829+ })
830+
831+ t .Run ("Runfiles" , func (t * testing.T ) {
832+ fileRoot := run (
833+ t ,
834+ model_analysis_pb .DirectoryLayout_RUNFILES ,
835+ singleChildDirectoryContents (
836+ "myrepo+" ,
837+ & model_filesystem_pb.DirectoryContents {
838+ Leaves : & model_filesystem_pb.DirectoryContents_LeavesInline {
839+ LeavesInline : & model_filesystem_pb.Leaves {
840+ Files : []* model_filesystem_pb.FileNode {
841+ {
842+ Name : "a" ,
843+ Properties : & model_filesystem_pb.FileProperties {},
844+ },
845+ },
846+ },
847+ },
848+ },
849+ ),
850+ )
851+ requireEqualPatchedMessage (t , func (patcher * model_core.ReferenceMessagePatcher [model_core.CreatedObjectTree ]) * model_analysis_pb.FileRoot_Value {
852+ return & model_analysis_pb.FileRoot_Value {
853+ RootDirectory : singleChildDirectoryContents (
854+ "myrepo+" ,
855+ & model_filesystem_pb.DirectoryContents {
856+ Leaves : & model_filesystem_pb.DirectoryContents_LeavesInline {
857+ LeavesInline : & model_filesystem_pb.Leaves {
858+ Files : []* model_filesystem_pb.FileNode {
859+ {
860+ Name : "a" ,
861+ Properties : & model_filesystem_pb.FileProperties {},
862+ },
863+ },
864+ Symlinks : []* model_filesystem_pb.SymlinkNode {
865+ {
866+ Name : "b" ,
867+ Target : "a" ,
868+ },
869+ },
870+ },
871+ },
872+ },
873+ ),
874+ }
875+ }, fileRoot )
876+ fileRoot .Discard ()
877+ })
878+ })
678879 })
679880}
0 commit comments