Skip to content

Commit

Permalink
deb: auto-inject shlibs:depends and misc:depends
Browse files Browse the repository at this point in the history
These are dependencies that will be resolved automatically by debbuild.
If they are not needed then the resulting control file that is output by
debbuild will not include anything extra.
However, when the dependencies are needed it is important to have them.

This helps keep the spec more generic and avoid making mistakes.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
  • Loading branch information
cpuguy83 committed Oct 22, 2024
1 parent 19eedbc commit cd72abc
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 4 deletions.
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)))
}

0 comments on commit cd72abc

Please sign in to comment.