go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cipkg/base/generators/embed_test.go (about)

     1  // Copyright 2023 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package generators
    16  
    17  import (
    18  	"context"
    19  	"embed"
    20  	"io/fs"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"go.chromium.org/luci/cipkg/core"
    25  	"go.chromium.org/luci/cipkg/internal/testutils"
    26  
    27  	. "github.com/smartystreets/goconvey/convey"
    28  )
    29  
    30  //go:embed embed.go embed_test.go testdata
    31  var embeddedFilesTestEmbed embed.FS
    32  var embeddedFilesTestGen = InitEmbeddedFS(
    33  	"embedded_files_test", embeddedFilesTestEmbed,
    34  ).WithModeOverride(func(info fs.FileInfo) (fs.FileMode, error) {
    35  	if info.Name() == "embed.go" {
    36  		return fs.ModePerm, nil
    37  	}
    38  	return 0o666, nil
    39  })
    40  
    41  func TestEmbeddedFiles(t *testing.T) {
    42  	Convey("Test embedded files", t, func() {
    43  		ctx := context.Background()
    44  		plats := Platforms{}
    45  
    46  		Convey("Test normal", func() {
    47  			a, err := embeddedFilesTestGen.Generate(ctx, plats)
    48  			So(err, ShouldBeNil)
    49  
    50  			embedFiles := testutils.Assert[*core.Action_Copy](t, a.Spec)
    51  			So(embedFiles.Copy.Files, ShouldResemble, map[string]*core.ActionFilesCopy_Source{
    52  				"embed.go": {
    53  					Content: &core.ActionFilesCopy_Source_Embed_{
    54  						Embed: &core.ActionFilesCopy_Source_Embed{Ref: embeddedFilesTestGen.ref, Path: "embed.go"},
    55  					},
    56  					Mode: 0o777,
    57  				},
    58  				"embed_test.go": {
    59  					Content: &core.ActionFilesCopy_Source_Embed_{
    60  						Embed: &core.ActionFilesCopy_Source_Embed{Ref: embeddedFilesTestGen.ref, Path: "embed_test.go"},
    61  					},
    62  					Mode: 0o666,
    63  				},
    64  				filepath.FromSlash("testdata/embed"): {
    65  					Content: &core.ActionFilesCopy_Source_Embed_{
    66  						Embed: &core.ActionFilesCopy_Source_Embed{Ref: embeddedFilesTestGen.ref, Path: "testdata/embed"},
    67  					},
    68  					Mode: 0o666,
    69  				},
    70  			})
    71  		})
    72  		Convey("Test subdir", func() {
    73  			a, err := embeddedFilesTestGen.SubDir("testdata").Generate(ctx, plats)
    74  			So(err, ShouldBeNil)
    75  
    76  			embedFiles := testutils.Assert[*core.Action_Copy](t, a.Spec)
    77  			So(embedFiles.Copy.Files, ShouldResemble, map[string]*core.ActionFilesCopy_Source{
    78  				"embed": {
    79  					Content: &core.ActionFilesCopy_Source_Embed_{
    80  						Embed: &core.ActionFilesCopy_Source_Embed{Ref: embeddedFilesTestGen.ref, Path: "testdata/embed"},
    81  					},
    82  					Mode: 0o666,
    83  				},
    84  			})
    85  		})
    86  	})
    87  }