Skip to content

Commit

Permalink
Refactor subrepoLabel
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatskaari committed Nov 27, 2023
1 parent 8bb8964 commit 50dd97c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
15 changes: 5 additions & 10 deletions src/core/build_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,18 +436,18 @@ func (label BuildLabel) PackageDir() string {

// SubrepoLabel returns a build label corresponding to the subrepo part of this build label.
func (label BuildLabel) SubrepoLabel(state *BuildState) BuildLabel {
pluginName, arch := SplitSubrepoArch(label.Subrepo)
subrepoName, arch := SplitSubrepoArch(label.Subrepo)
if arch == "" && state.Arch != cli.HostArch() {
arch = state.Arch.String()
}

plugin, ok := state.Config.Plugin[pluginName]
plugin, ok := state.Config.Plugin[subrepoName]
if !ok {
return label.subrepoLabel(state)
return subrepoLabel(subrepoName, arch)
}

if plugin.Target.String() == "" {
log.Fatalf("[Plugin \"%v\"] must have Target set in the .plzconfig", pluginName)
log.Fatalf("[Plugin \"%v\"] must have Target set in the .plzconfig", subrepoName)
}

t := plugin.Target
Expand All @@ -461,12 +461,7 @@ func (label BuildLabel) SubrepoLabel(state *BuildState) BuildLabel {
return t
}

func (label BuildLabel) subrepoLabel(state *BuildState) BuildLabel {
subrepoName, arch := SplitSubrepoArch(label.Subrepo)
if arch == "" && state.Arch != cli.HostArch() {
arch = state.Arch.String()
}

func subrepoLabel(subrepoName, arch string) BuildLabel {
if idx := strings.LastIndexByte(subrepoName, '/'); idx != -1 {
return BuildLabel{PackageName: subrepoName[:idx], Name: subrepoName[idx+1:], Subrepo: arch}
}
Expand Down
12 changes: 5 additions & 7 deletions src/core/build_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,17 @@ func TestCompleteError(t *testing.T) {

func TestSubrepoLabel(t *testing.T) {
state := NewDefaultBuildState()
label := BuildLabel{Subrepo: "test"}
assert.EqualValues(t, BuildLabel{PackageName: "", Name: "test"}, label.subrepoLabel(state))
label.Subrepo = "package/test"
assert.EqualValues(t, BuildLabel{PackageName: "package", Name: "test"}, label.subrepoLabel(state))
assert.EqualValues(t, BuildLabel{PackageName: "", Name: "test"}, subrepoLabel("test", ""))
assert.EqualValues(t, BuildLabel{PackageName: "package", Name: "test"}, subrepoLabel("package/test", ""))
// This isn't really valid (the caller shouldn't need to call it in such a case)
// but we want to make sure it doesn't panic.
label.Subrepo = ""
assert.EqualValues(t, BuildLabel{PackageName: "", Name: ""}, label.subrepoLabel(state))
assert.EqualValues(t, BuildLabel{PackageName: "", Name: ""}, subrepoLabel("", ""))
assert.EqualValues(t, BuildLabel{PackageName: "package", Name: "test", Subrepo: "foowin_amd64"}, subrepoLabel("package/test", "foowin_amd64"))

state.Graph.AddSubrepo(&Subrepo{Name: "foowin_amd64", Arch: cli.NewArch("foowin", "amd64")})
state.Graph.AddSubrepo(&Subrepo{Name: "dependant@foowin_amd64", Arch: cli.NewArch("foowin", "amd64")})

label = BuildLabel{PackageName: "build_defs", Subrepo: "dependee@foowin_amd64"}
label := BuildLabel{PackageName: "build_defs", Subrepo: "dependee@foowin_amd64"}
sl := label.SubrepoLabel(state)
assert.Equal(t, BuildLabel{Name: "dependee", Subrepo: "foowin_amd64"}, sl)
}
Expand Down

0 comments on commit 50dd97c

Please sign in to comment.