Skip to content

Commit

Permalink
make LocateManifestSlices public and tested
Browse files Browse the repository at this point in the history
  • Loading branch information
letFunny committed Sep 26, 2024
1 parent 293fd06 commit 9b03b95
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 22 deletions.
18 changes: 18 additions & 0 deletions internal/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package manifest
import (
"fmt"
"io"
"path/filepath"
"slices"
"strings"

"github.com/canonical/chisel/internal/jsonwall"
"github.com/canonical/chisel/internal/setup"
Expand Down Expand Up @@ -158,6 +160,22 @@ func Validate(manifest *Manifest) (err error) {
return nil
}

// LocateManifestSlices finds the paths marked with "generate:manifest" and
// returns a map from the manifest path to all the slices that declare it.
func LocateManifestSlices(slices []*setup.Slice, manifestFileName string) map[string][]*setup.Slice {
manifestSlices := make(map[string][]*setup.Slice)
for _, slice := range slices {
for path, info := range slice.Contents {
if info.Generate == setup.GenerateManifest {
dir := strings.TrimSuffix(path, "**")
path = filepath.Join(dir, manifestFileName)
manifestSlices[path] = append(manifestSlices[path], slice)
}
}
}
return manifestSlices
}

type prefixable interface {
Path | Content | Package | Slice
}
Expand Down
102 changes: 101 additions & 1 deletion internal/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import (
. "gopkg.in/check.v1"

"github.com/canonical/chisel/internal/manifest"
"github.com/canonical/chisel/internal/setup"
"github.com/canonical/chisel/internal/testutil"
)

var testKey = testutil.PGPKeys["key1"]

type manifestContents struct {
Paths []*manifest.Path
Packages []*manifest.Package
Expand Down Expand Up @@ -123,7 +127,7 @@ var manifestTests = []struct {
readError: `cannot read manifest: unknown schema version "2.0"`,
}}

func (s *S) TestRun(c *C) {
func (s *S) TestManifestReadValidate(c *C) {
for _, test := range manifestTests {
c.Logf("Summary: %s", test.summary)

Expand Down Expand Up @@ -169,6 +173,102 @@ func (s *S) TestRun(c *C) {
}
}

var locateManifestSlicesTests = []struct {
summary string
slices []*setup.Slice
filename string
expected map[string][]string
}{{
summary: "Single slice",
slices: []*setup.Slice{{
Name: "slice1",
Contents: map[string]setup.PathInfo{
"/folder/**": {
Kind: "generate",
Generate: "manifest",
},
},
}},
filename: "manifest.wall",
expected: map[string][]string{
"/folder/manifest.wall": []string{"slice1"},
},
}, {
summary: "No slice matched",
slices: []*setup.Slice{{
Name: "slice1",
Contents: map[string]setup.PathInfo{},
}},
filename: "manifest.wall",
expected: map[string][]string{},
}, {
summary: "Several matches with several groups",
slices: []*setup.Slice{{
Name: "slice1",
Contents: map[string]setup.PathInfo{
"/folder/**": {
Kind: "generate",
Generate: "manifest",
},
},
}, {
Name: "slice2",
Contents: map[string]setup.PathInfo{
"/folder/**": {
Kind: "generate",
Generate: "manifest",
},
},
}, {
Name: "slice3",
Contents: map[string]setup.PathInfo{},
}, {
Name: "slice4",
Contents: map[string]setup.PathInfo{
"/other-folder/**": {
Kind: "generate",
Generate: "manifest",
},
},
}, {
Name: "slice5",
Contents: map[string]setup.PathInfo{
"/other-folder/**": {
Kind: "generate",
Generate: "manifest",
},
},
}},
filename: "mfest.wall",
expected: map[string][]string{
"/folder/mfest.wall": {"slice1", "slice2"},
"/other-folder/mfest.wall": {"slice4", "slice5"},
},
}}

func (s *S) TestLocateManifestSlices(c *C) {
for _, test := range locateManifestSlicesTests {
c.Logf("Summary: %s", test.summary)

manifestSlices := manifest.LocateManifestSlices(test.slices, test.filename)

slicesByName := map[string]*setup.Slice{}
for _, slice := range test.slices {
_, ok := slicesByName[slice.Name]
c.Assert(ok, Equals, false, Commentf("duplicated slice name"))
slicesByName[slice.Name] = slice
}

c.Assert(manifestSlices, HasLen, len(test.expected))
for path, slices := range manifestSlices {
c.Assert(slices, HasLen, len(test.expected[path]))
for i, sliceName := range test.expected[path] {
c.Assert(slicesByName[sliceName], DeepEquals, slices[i])
}
}
}
}

func dumpManifestContents(c *C, mfest *manifest.Manifest) *manifestContents {
var slices []*manifest.Slice
err := mfest.IterateSlices("", func(slice *manifest.Slice) error {
Expand Down
23 changes: 2 additions & 21 deletions internal/slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,7 @@ type generateManifestsOptions struct {
}

func generateManifests(options *generateManifestsOptions) error {
manifestSlices, err := locateManifestSlices(options.selection)
if err != nil {
return err
}
manifestSlices := manifest.LocateManifestSlices(options.selection, manifestFileName)
if len(manifestSlices) == 0 {
// Nothing to do.
return nil
Expand All @@ -451,7 +448,7 @@ func generateManifests(options *generateManifestsOptions) error {
Schema: manifest.Schema,
})

err = manifestAddPackages(dbw, options.packageInfo)
err := manifestAddPackages(dbw, options.packageInfo)
if err != nil {
return err
}
Expand Down Expand Up @@ -590,19 +587,3 @@ func unixPerm(mode fs.FileMode) (perm uint32) {
}
return perm
}

// locateManifestSlices finds the paths marked with "generate:manifest" and
// returns a map from the manifest path to all the slices that declare it.
func locateManifestSlices(slices []*setup.Slice) (map[string][]*setup.Slice, error) {
manifestSlices := make(map[string][]*setup.Slice)
for _, slice := range slices {
for path, info := range slice.Contents {
if info.Generate == setup.GenerateManifest {
dir := strings.TrimSuffix(path, "**")
path = filepath.Join(dir, manifestFileName)
manifestSlices[path] = append(manifestSlices[path], slice)
}
}
}
return manifestSlices, nil
}

0 comments on commit 9b03b95

Please sign in to comment.