Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deb: auto-inject shlibs:depends and misc:depends #411

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions frontend/deb/template_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "embed"
"fmt"
"io"
"maps"
"strings"
"text/template"

Expand Down Expand Up @@ -57,13 +58,46 @@ func appendConstraints(deps map[string]dalec.PackageConstraints) []string {
}

func (w *controlWrapper) depends(buf io.Writer, depsSpec *dalec.PackageDependencies) {
if len(depsSpec.Runtime) == 0 {
return
var (
needsClone bool
rtDeps map[string]dalec.PackageConstraints
)
if depsSpec == nil || depsSpec.Runtime == nil {
rtDeps = make(map[string]dalec.PackageConstraints)
} else {
rtDeps = depsSpec.Runtime
needsClone = true
}

deps := appendConstraints(depsSpec.Runtime)
fmt.Fprintln(buf, multiline("Depends", deps))
// Add in deps vars that will get resolved by debbuild
// In some cases these are not neccessary (maybe even most), but when they are
// it is important.
// When not needed lintian may throw warnings but that's ok.
// If these aren't actually needed they'll resolve to nothing and don't cause
// any changes.
const (
shlibsDeps = "${shlibs:Depends}"
miscDeps = "${misc:Depends}"
)

if _, exists := rtDeps[shlibsDeps]; !exists {
if needsClone {
rtDeps = maps.Clone(rtDeps)
needsClone = false
}

rtDeps[shlibsDeps] = dalec.PackageConstraints{}
}

if _, exists := rtDeps[miscDeps]; !exists {
if needsClone {
rtDeps = maps.Clone(rtDeps)
}
rtDeps[miscDeps] = dalec.PackageConstraints{}
}

deps := appendConstraints(rtDeps)
fmt.Fprintln(buf, multiline("Depends", deps))
}

// multiline attempts to format a field with multiple values in a way that is more human readable
Expand Down
81 changes: 81 additions & 0 deletions frontend/deb/template_rules_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package deb

import (
"bytes"
"strings"
"testing"

"github.com/Azure/dalec"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
)

func TestRules_OverrideSystemd(t *testing.T) {
Expand Down Expand Up @@ -109,3 +112,81 @@ func TestRules_OverrideSystemd(t *testing.T) {
})
})
}

func TestDepends(t *testing.T) {
control := &controlWrapper{}

buf := bytes.NewBuffer(nil)
control.depends(buf, nil)

expect := `
Depends: ${misc:Depends},
${shlibs:Depends}
`
actual := strings.TrimSpace(buf.String())
assert.Check(t, cmp.Equal(actual, strings.TrimSpace(expect)))

buf.Reset()

// Test again with non-nil deps
control.depends(buf, &dalec.PackageDependencies{})
actual = strings.TrimSpace(buf.String())
assert.Check(t, cmp.Equal(actual, strings.TrimSpace(expect)))

buf.Reset()

// Test again with non-nil runtime deps
control.depends(buf, &dalec.PackageDependencies{
Runtime: map[string]dalec.PackageConstraints{},
})
actual = strings.TrimSpace(buf.String())
assert.Check(t, cmp.Equal(actual, strings.TrimSpace(expect)))

buf.Reset()

// Test again with other runtime deps
control.depends(buf, &dalec.PackageDependencies{
Runtime: map[string]dalec.PackageConstraints{
"foo": {},
"bar": {},
},
})

expect = `
Depends: ${misc:Depends},
${shlibs:Depends},
bar,
foo
`
actual = strings.TrimSpace(buf.String())
assert.Check(t, cmp.Equal(actual, strings.TrimSpace(expect)))

buf.Reset()

// Test again with other runtime deps and shlibs specified
control.depends(buf, &dalec.PackageDependencies{
Runtime: map[string]dalec.PackageConstraints{
"foo": {},
"bar": {},
"${shlibs:Depends}": {},
},
})

actual = strings.TrimSpace(buf.String())
assert.Check(t, cmp.Equal(actual, strings.TrimSpace(expect)))

buf.Reset()

// Test again with other runtime deps and shlibs and misc depends specified
control.depends(buf, &dalec.PackageDependencies{
Runtime: map[string]dalec.PackageConstraints{
"foo": {},
"bar": {},
"${shlibs:Depends}": {},
"${misc:Depends}": {},
},
})

actual = strings.TrimSpace(buf.String())
assert.Check(t, cmp.Equal(actual, strings.TrimSpace(expect)))
}