Skip to content

Commit 4e36a9c

Browse files
committed
Also text ctx.actions.expand_template() output in runfiles directories
1 parent 3e8b371 commit 4e36a9c

1 file changed

Lines changed: 170 additions & 129 deletions

File tree

pkg/model/analysis/file_root_test.go

Lines changed: 170 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -337,169 +337,210 @@ func TestFileRoot(t *testing.T) {
337337
// },
338338
// is_executable = True,
339339
// )
340-
e := NewMockFileRootEnvironmentForTesting(ctrl)
341-
bct.expectCaptureExistingObject(e)
342-
bct.expectGetDirectoryCreationParametersObjectValue(t, e)
343-
bct.expectGetDirectoryReadersValue(t, e)
344-
bct.expectGetFileCreationParametersObjectValue(t, e)
345-
bct.expectGetFileReaderValue(t, e)
346-
configuration := newObject(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) model_core.Marshalable {
347-
return model_core.NewProtoListMarshalable([]*model_analysis_pb.BuildSettingOverride{{
348-
Level: &model_analysis_pb.BuildSettingOverride_Leaf_{
349-
Leaf: &model_analysis_pb.BuildSettingOverride_Leaf{
350-
Label: "@@bazel_tools+//command_line_option:platforms",
351-
Value: &model_starlark_pb.Value{
352-
Kind: &model_starlark_pb.Value_List{
353-
List: &model_starlark_pb.List{
354-
Elements: []*model_starlark_pb.List_Element{{
355-
Level: &model_starlark_pb.List_Element_Leaf{
356-
Leaf: &model_starlark_pb.Value{
357-
Kind: &model_starlark_pb.Value_Label{
358-
Label: "@@platforms+//host",
340+
run := func(t *testing.T, directoryLayout model_analysis_pb.DirectoryLayout) model_analysis.PatchedFileRootValue {
341+
e := NewMockFileRootEnvironmentForTesting(ctrl)
342+
bct.expectCaptureExistingObject(e)
343+
bct.expectGetDirectoryCreationParametersObjectValue(t, e)
344+
bct.expectGetDirectoryReadersValue(t, e)
345+
bct.expectGetFileCreationParametersObjectValue(t, e)
346+
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+
},
359361
},
360362
},
361-
},
362-
}},
363+
}},
364+
},
363365
},
364366
},
365367
},
366368
},
367-
},
368-
}})
369-
})
370-
e.EXPECT().GetTargetOutputValue(
371-
eqPatchedMessage(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.TargetOutput_Key {
372-
return &model_analysis_pb.TargetOutput_Key{
373-
Label: "@@myrepo+//:generate",
374-
ConfigurationReference: attachObject(patcher, configuration),
375-
PackageRelativePath: "output",
376-
}
377-
}),
378-
).Return(newMessage(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.TargetOutput_Value {
379-
return &model_analysis_pb.TargetOutput_Value{
380-
Definition: &model_analysis_pb.TargetOutputDefinition{
381-
Source: &model_analysis_pb.TargetOutputDefinition_ExpandTemplate_{
382-
ExpandTemplate: &model_analysis_pb.TargetOutputDefinition_ExpandTemplate{
383-
Template: &model_starlark_pb.File{
384-
Label: "@@myrepo+//:template",
385-
Type: model_starlark_pb.File_FILE,
386-
},
387-
IsExecutable: true,
388-
Substitutions: []*model_analysis_pb.TargetOutputDefinition_ExpandTemplate_Substitution{
389-
{Needle: []byte("{{first_name}}"), Replacement: []byte("Albert")},
390-
{Needle: []byte("{{last_name}}"), Replacement: []byte("Einstein")},
369+
}})
370+
})
371+
e.EXPECT().GetTargetOutputValue(
372+
eqPatchedMessage(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.TargetOutput_Key {
373+
return &model_analysis_pb.TargetOutput_Key{
374+
Label: "@@myrepo+//:generate",
375+
ConfigurationReference: attachObject(patcher, configuration),
376+
PackageRelativePath: "output",
377+
}
378+
}),
379+
).Return(newMessage(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.TargetOutput_Value {
380+
return &model_analysis_pb.TargetOutput_Value{
381+
Definition: &model_analysis_pb.TargetOutputDefinition{
382+
Source: &model_analysis_pb.TargetOutputDefinition_ExpandTemplate_{
383+
ExpandTemplate: &model_analysis_pb.TargetOutputDefinition_ExpandTemplate{
384+
Template: &model_starlark_pb.File{
385+
Label: "@@myrepo+//:template",
386+
Type: model_starlark_pb.File_FILE,
387+
},
388+
IsExecutable: true,
389+
Substitutions: []*model_analysis_pb.TargetOutputDefinition_ExpandTemplate_Substitution{
390+
{Needle: []byte("{{first_name}}"), Replacement: []byte("Albert")},
391+
{Needle: []byte("{{last_name}}"), Replacement: []byte("Einstein")},
392+
},
391393
},
392394
},
393395
},
394-
},
395-
}
396-
}))
397-
e.EXPECT().GetFileRootValue(
398-
eqPatchedMessage(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.FileRoot_Key {
399-
return &model_analysis_pb.FileRoot_Key{
400-
DirectoryLayout: model_analysis_pb.DirectoryLayout_INPUT_ROOT,
401-
File: &model_starlark_pb.File{
402-
Label: "@@myrepo+//:template",
403-
Type: model_starlark_pb.File_FILE,
404-
},
405396
}
406-
}),
407-
).Return(newMessage(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.FileRoot_Value {
408-
return &model_analysis_pb.FileRoot_Value{
409-
RootDirectory: singleChildDirectoryContents(
410-
"external",
411-
singleChildDirectoryContents(
412-
"myrepo+",
413-
&model_filesystem_pb.DirectoryContents{
414-
Leaves: &model_filesystem_pb.DirectoryContents_LeavesInline{
415-
LeavesInline: &model_filesystem_pb.Leaves{
416-
Files: []*model_filesystem_pb.FileNode{
417-
{
418-
Name: "template",
419-
Properties: &model_filesystem_pb.FileProperties{
420-
Contents: &model_filesystem_pb.FileContents{
421-
Level: &model_filesystem_pb.FileContents_ChunkReference{
422-
ChunkReference: attachObject(patcher, newObject(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) model_core.Marshalable {
423-
return model_core.NewRawMarshalable([]byte("{{first_name}} {{last_name}}"))
424-
})),
397+
}))
398+
e.EXPECT().GetFileRootValue(
399+
eqPatchedMessage(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.FileRoot_Key {
400+
return &model_analysis_pb.FileRoot_Key{
401+
DirectoryLayout: model_analysis_pb.DirectoryLayout_INPUT_ROOT,
402+
File: &model_starlark_pb.File{
403+
Label: "@@myrepo+//:template",
404+
Type: model_starlark_pb.File_FILE,
405+
},
406+
}
407+
}),
408+
).Return(newMessage(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.FileRoot_Value {
409+
return &model_analysis_pb.FileRoot_Value{
410+
RootDirectory: singleChildDirectoryContents(
411+
"external",
412+
singleChildDirectoryContents(
413+
"myrepo+",
414+
&model_filesystem_pb.DirectoryContents{
415+
Leaves: &model_filesystem_pb.DirectoryContents_LeavesInline{
416+
LeavesInline: &model_filesystem_pb.Leaves{
417+
Files: []*model_filesystem_pb.FileNode{
418+
{
419+
Name: "template",
420+
Properties: &model_filesystem_pb.FileProperties{
421+
Contents: &model_filesystem_pb.FileContents{
422+
Level: &model_filesystem_pb.FileContents_ChunkReference{
423+
ChunkReference: attachObject(patcher, newObject(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) model_core.Marshalable {
424+
return model_core.NewRawMarshalable([]byte("{{first_name}} {{last_name}}"))
425+
})),
426+
},
427+
TotalSizeBytes: 28,
425428
},
426-
TotalSizeBytes: 28,
427429
},
428430
},
429431
},
430432
},
431433
},
432434
},
433-
},
435+
),
434436
),
435-
),
436-
}
437-
}))
438-
expandedTemplateFile := NewMockFileReadWriter(ctrl)
439-
bct.filePool.EXPECT().NewFile().Return(expandedTemplateFile, nil)
440-
expandedTemplateFile.EXPECT().WriteAt([]byte("Albert Einstein"), int64(0)).Return(15, nil)
441-
expandedTemplateFile.EXPECT().Close()
437+
}
438+
}))
439+
expandedTemplateFile := NewMockFileReadWriter(ctrl)
440+
bct.filePool.EXPECT().NewFile().Return(expandedTemplateFile, nil)
441+
expandedTemplateFile.EXPECT().WriteAt([]byte("Albert Einstein"), int64(0)).Return(15, nil)
442+
expandedTemplateFile.EXPECT().Close()
442443

443-
fileRoot, err := bct.computer.ComputeFileRootValue(
444-
ctx,
445-
newMessage(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.FileRoot_Key {
446-
return &model_analysis_pb.FileRoot_Key{
447-
DirectoryLayout: model_analysis_pb.DirectoryLayout_INPUT_ROOT,
448-
File: &model_starlark_pb.File{
449-
Label: "@@myrepo+//:output",
450-
Type: model_starlark_pb.File_FILE,
451-
Owner: &model_starlark_pb.File_Owner{
452-
ConfigurationReference: attachObject(patcher, configuration),
453-
TargetName: "generate",
444+
fileRoot, err := bct.computer.ComputeFileRootValue(
445+
ctx,
446+
newMessage(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.FileRoot_Key {
447+
return &model_analysis_pb.FileRoot_Key{
448+
DirectoryLayout: directoryLayout,
449+
File: &model_starlark_pb.File{
450+
Label: "@@myrepo+//:output",
451+
Type: model_starlark_pb.File_FILE,
452+
Owner: &model_starlark_pb.File_Owner{
453+
ConfigurationReference: attachObject(patcher, configuration),
454+
TargetName: "generate",
455+
},
454456
},
455-
},
456-
}
457-
}),
458-
e,
459-
)
460-
require.NoError(t, err)
461-
requireEqualPatchedMessage(t, func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.FileRoot_Value {
462-
return &model_analysis_pb.FileRoot_Value{
463-
RootDirectory: singleChildDirectoryContents(
464-
"bazel-out",
465-
singleChildDirectoryContents(
466-
"Cg6Kx80o8BPYmGdgWYfRZvbKyWojQ7snQzHOx70XAwRPAAAAAAAAAA.",
457+
}
458+
}),
459+
e,
460+
)
461+
require.NoError(t, err)
462+
return fileRoot
463+
}
464+
465+
t.Run("InputRoot", func(t *testing.T) {
466+
fileRoot := run(t, model_analysis_pb.DirectoryLayout_INPUT_ROOT)
467+
requireEqualPatchedMessage(t, func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.FileRoot_Value {
468+
return &model_analysis_pb.FileRoot_Value{
469+
RootDirectory: singleChildDirectoryContents(
470+
"bazel-out",
467471
singleChildDirectoryContents(
468-
"bin",
472+
"Cg6Kx80o8BPYmGdgWYfRZvbKyWojQ7snQzHOx70XAwRPAAAAAAAAAA.",
469473
singleChildDirectoryContents(
470-
"external",
474+
"bin",
471475
singleChildDirectoryContents(
472-
"myrepo+",
473-
&model_filesystem_pb.DirectoryContents{
474-
Leaves: &model_filesystem_pb.DirectoryContents_LeavesInline{
475-
LeavesInline: &model_filesystem_pb.Leaves{
476-
Files: []*model_filesystem_pb.FileNode{
477-
{
478-
Name: "output",
479-
Properties: &model_filesystem_pb.FileProperties{
480-
Contents: &model_filesystem_pb.FileContents{
481-
Level: &model_filesystem_pb.FileContents_ChunkReference{
482-
ChunkReference: attachObject(patcher, newObject(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) model_core.Marshalable {
483-
return model_core.NewRawMarshalable([]byte("Albert Einstein"))
484-
})),
476+
"external",
477+
singleChildDirectoryContents(
478+
"myrepo+",
479+
&model_filesystem_pb.DirectoryContents{
480+
Leaves: &model_filesystem_pb.DirectoryContents_LeavesInline{
481+
LeavesInline: &model_filesystem_pb.Leaves{
482+
Files: []*model_filesystem_pb.FileNode{
483+
{
484+
Name: "output",
485+
Properties: &model_filesystem_pb.FileProperties{
486+
Contents: &model_filesystem_pb.FileContents{
487+
Level: &model_filesystem_pb.FileContents_ChunkReference{
488+
ChunkReference: attachObject(patcher, newObject(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) model_core.Marshalable {
489+
return model_core.NewRawMarshalable([]byte("Albert Einstein"))
490+
})),
491+
},
492+
TotalSizeBytes: 15,
485493
},
486-
TotalSizeBytes: 15,
494+
IsExecutable: true,
487495
},
488-
IsExecutable: true,
489496
},
490497
},
491498
},
492499
},
493500
},
494-
},
501+
),
495502
),
496503
),
497504
),
498505
),
499-
),
500-
}
501-
}, fileRoot)
502-
fileRoot.Discard()
506+
}
507+
}, fileRoot)
508+
fileRoot.Discard()
509+
})
510+
511+
t.Run("Runfiles", func(t *testing.T) {
512+
fileRoot := run(t, model_analysis_pb.DirectoryLayout_RUNFILES)
513+
requireEqualPatchedMessage(t, func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) *model_analysis_pb.FileRoot_Value {
514+
return &model_analysis_pb.FileRoot_Value{
515+
RootDirectory: singleChildDirectoryContents(
516+
"myrepo+",
517+
&model_filesystem_pb.DirectoryContents{
518+
Leaves: &model_filesystem_pb.DirectoryContents_LeavesInline{
519+
LeavesInline: &model_filesystem_pb.Leaves{
520+
Files: []*model_filesystem_pb.FileNode{
521+
{
522+
Name: "output",
523+
Properties: &model_filesystem_pb.FileProperties{
524+
Contents: &model_filesystem_pb.FileContents{
525+
Level: &model_filesystem_pb.FileContents_ChunkReference{
526+
ChunkReference: attachObject(patcher, newObject(func(patcher *model_core.ReferenceMessagePatcher[model_core.CreatedObjectTree]) model_core.Marshalable {
527+
return model_core.NewRawMarshalable([]byte("Albert Einstein"))
528+
})),
529+
},
530+
TotalSizeBytes: 15,
531+
},
532+
IsExecutable: true,
533+
},
534+
},
535+
},
536+
},
537+
},
538+
},
539+
),
540+
}
541+
}, fileRoot)
542+
fileRoot.Discard()
543+
})
503544
})
504545
})
505546

0 commit comments

Comments
 (0)