Skip to content

Commit f3f756c

Browse files
committed
Add testing coverage for ctx.actions.symlink(target_path=...)
1 parent 4e36a9c commit f3f756c

1 file changed

Lines changed: 153 additions & 28 deletions

File tree

pkg/model/analysis/file_root_test.go

Lines changed: 153 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ func TestFileRoot(t *testing.T) {
4242
ctrl, ctx := gomock.WithContext(t.Context(), t)
4343
bct := newBaseComputerTester(ctrl)
4444

45+
exampleConfiguration := newObject(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) model_core.Marshalable {
46+
return model_core.NewProtoListMarshalable([]*model_analysis_pb.BuildSettingOverride{{
47+
Level: &model_analysis_pb.BuildSettingOverride_Leaf_{
48+
Leaf: &model_analysis_pb.BuildSettingOverride_Leaf{
49+
Label: "@@bazel_tools+//command_line_option:platforms",
50+
Value: &model_starlark_pb.Value{
51+
Kind: &model_starlark_pb.Value_List{
52+
List: &model_starlark_pb.List{
53+
Elements: []*model_starlark_pb.List_Element{{
54+
Level: &model_starlark_pb.List_Element_Leaf{
55+
Leaf: &model_starlark_pb.Value{
56+
Kind: &model_starlark_pb.Value_Label{
57+
Label: "@@platforms+//host",
58+
},
59+
},
60+
},
61+
}},
62+
},
63+
},
64+
},
65+
},
66+
},
67+
}})
68+
})
69+
4570
t.Run("MissingFile", func(t *testing.T) {
4671
// Request needs to contain a Starlark File object.
4772
e := NewMockFileRootEnvironmentForTesting(ctrl)
@@ -328,9 +353,10 @@ func TestFileRoot(t *testing.T) {
328353
t.Run("Success", func(t *testing.T) {
329354
// Simulate the computation of the output of:
330355
//
356+
// output = ctx.actions.declare_file("output")
331357
// ctx.actions.expand_template(
332358
// template = File("@@myrepo+//:template"),
333-
// output = File("@@myrepo+//:output"),
359+
// output = output,
334360
// substitutions = {
335361
// "{{first_name}}": "Albert",
336362
// "{{last_name}}": "Einstein",
@@ -344,35 +370,11 @@ func TestFileRoot(t *testing.T) {
344370
bct.expectGetDirectoryReadersValue(t, e)
345371
bct.expectGetFileCreationParametersObjectValue(t, e)
346372
bct.expectGetFileReaderValue(t, e)
347-
configuration := newObject(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) model_core.Marshalable {
348-
return model_core.NewProtoListMarshalable([]*model_analysis_pb.BuildSettingOverride{{
349-
Level: &model_analysis_pb.BuildSettingOverride_Leaf_{
350-
Leaf: &model_analysis_pb.BuildSettingOverride_Leaf{
351-
Label: "@@bazel_tools+//command_line_option:platforms",
352-
Value: &model_starlark_pb.Value{
353-
Kind: &model_starlark_pb.Value_List{
354-
List: &model_starlark_pb.List{
355-
Elements: []*model_starlark_pb.List_Element{{
356-
Level: &model_starlark_pb.List_Element_Leaf{
357-
Leaf: &model_starlark_pb.Value{
358-
Kind: &model_starlark_pb.Value_Label{
359-
Label: "@@platforms+//host",
360-
},
361-
},
362-
},
363-
}},
364-
},
365-
},
366-
},
367-
},
368-
},
369-
}})
370-
})
371373
e.EXPECT().GetTargetOutputValue(
372374
eqPatchedMessage(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.TargetOutput_Key {
373375
return &model_analysis_pb.TargetOutput_Key{
374376
Label: "@@myrepo+//:generate",
375-
ConfigurationReference: attachObject(patcher, configuration),
377+
ConfigurationReference: attachObject(patcher, exampleConfiguration),
376378
PackageRelativePath: "output",
377379
}
378380
}),
@@ -450,7 +452,7 @@ func TestFileRoot(t *testing.T) {
450452
Label: "@@myrepo+//:output",
451453
Type: model_starlark_pb.File_FILE,
452454
Owner: &model_starlark_pb.File_Owner{
453-
ConfigurationReference: attachObject(patcher, configuration),
455+
ConfigurationReference: attachObject(patcher, exampleConfiguration),
454456
TargetName: "generate",
455457
},
456458
},
@@ -545,7 +547,130 @@ func TestFileRoot(t *testing.T) {
545547
})
546548

547549
t.Run("StaticPackageDirectory", func(t *testing.T) {
548-
// TODO!
550+
// TODO: Test error cases.
551+
552+
t.Run("Success", func(t *testing.T) {
553+
// Simulate the computation of the output of:
554+
//
555+
// output = ctx.actions.declare_symlink("passwd")
556+
// ctx.actions.symlink(
557+
// output = output,
558+
// target_path = "/etc/passwd",
559+
// )
560+
run := func(t *testing.T, directoryLayout model_analysis_pb.DirectoryLayout) model_analysis.PatchedFileRootValue {
561+
e := NewMockFileRootEnvironmentForTesting(ctrl)
562+
bct.expectCaptureExistingObject(e)
563+
bct.expectGetDirectoryCreationParametersObjectValue(t, e)
564+
e.EXPECT().GetTargetOutputValue(
565+
eqPatchedMessage(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.TargetOutput_Key {
566+
return &model_analysis_pb.TargetOutput_Key{
567+
Label: "@@myrepo+//:create_symlink",
568+
ConfigurationReference: attachObject(patcher, exampleConfiguration),
569+
PackageRelativePath: "passwd",
570+
}
571+
}),
572+
).Return(newMessage(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.TargetOutput_Value {
573+
return &model_analysis_pb.TargetOutput_Value{
574+
Definition: &model_analysis_pb.TargetOutputDefinition{
575+
Source: &model_analysis_pb.TargetOutputDefinition_StaticPackageDirectory{
576+
StaticPackageDirectory: &model_filesystem_pb.DirectoryContents{
577+
Leaves: &model_filesystem_pb.DirectoryContents_LeavesInline{
578+
LeavesInline: &model_filesystem_pb.Leaves{
579+
Symlinks: []*model_filesystem_pb.SymlinkNode{
580+
{
581+
Name: "passwd",
582+
Target: "/etc/passwd",
583+
},
584+
},
585+
},
586+
},
587+
},
588+
},
589+
},
590+
}
591+
}))
592+
593+
fileRoot, err := bct.computer.ComputeFileRootValue(
594+
ctx,
595+
newMessage(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.FileRoot_Key {
596+
return &model_analysis_pb.FileRoot_Key{
597+
DirectoryLayout: directoryLayout,
598+
File: &model_starlark_pb.File{
599+
Label: "@@myrepo+//:passwd",
600+
Type: model_starlark_pb.File_FILE,
601+
Owner: &model_starlark_pb.File_Owner{
602+
ConfigurationReference: attachObject(patcher, exampleConfiguration),
603+
TargetName: "create_symlink",
604+
},
605+
},
606+
}
607+
}),
608+
e,
609+
)
610+
require.NoError(t, err)
611+
return fileRoot
612+
}
613+
614+
t.Run("InputRoot", func(t *testing.T) {
615+
fileRoot := run(t, model_analysis_pb.DirectoryLayout_INPUT_ROOT)
616+
requireEqualPatchedMessage(t, func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.FileRoot_Value {
617+
return &model_analysis_pb.FileRoot_Value{
618+
RootDirectory: singleChildDirectoryContents(
619+
"bazel-out",
620+
singleChildDirectoryContents(
621+
"Cg6Kx80o8BPYmGdgWYfRZvbKyWojQ7snQzHOx70XAwRPAAAAAAAAAA.",
622+
singleChildDirectoryContents(
623+
"bin",
624+
singleChildDirectoryContents(
625+
"external",
626+
singleChildDirectoryContents(
627+
"myrepo+",
628+
&model_filesystem_pb.DirectoryContents{
629+
Leaves: &model_filesystem_pb.DirectoryContents_LeavesInline{
630+
LeavesInline: &model_filesystem_pb.Leaves{
631+
Symlinks: []*model_filesystem_pb.SymlinkNode{
632+
{
633+
Name: "passwd",
634+
Target: "/etc/passwd",
635+
},
636+
},
637+
},
638+
},
639+
},
640+
),
641+
),
642+
),
643+
),
644+
),
645+
}
646+
}, fileRoot)
647+
fileRoot.Discard()
648+
})
649+
650+
t.Run("Runfiles", func(t *testing.T) {
651+
fileRoot := run(t, model_analysis_pb.DirectoryLayout_RUNFILES)
652+
requireEqualPatchedMessage(t, func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.FileRoot_Value {
653+
return &model_analysis_pb.FileRoot_Value{
654+
RootDirectory: singleChildDirectoryContents(
655+
"myrepo+",
656+
&model_filesystem_pb.DirectoryContents{
657+
Leaves: &model_filesystem_pb.DirectoryContents_LeavesInline{
658+
LeavesInline: &model_filesystem_pb.Leaves{
659+
Symlinks: []*model_filesystem_pb.SymlinkNode{
660+
{
661+
Name: "passwd",
662+
Target: "/etc/passwd",
663+
},
664+
},
665+
},
666+
},
667+
},
668+
),
669+
}
670+
}, fileRoot)
671+
fileRoot.Discard()
672+
})
673+
})
549674
})
550675

551676
t.Run("Symlink", func(t *testing.T) {

0 commit comments

Comments
 (0)