mirror of
https://github.com/vbatts/go-mtree.git
synced 2025-10-04 12:31:00 +00:00
compare: move FreeBSD loose keyword comparisons to gomtree command
FreeBSD has quite unfortunate behaviour when dealing with keywords that are missing in one of the manifests being compared -- namely, they ignore these instances. Commit21723a3974
("*: fix comparison of missing keywords") re-added this behaviour after the introduction of the Compare API, but unfortunately it was implemented in the Compare API itself -- meaning that library users (which didn't want this behaviour) were silently opted into it. This patch moves the behaviour to the command-line, where it belongs (a future patch in this series will allow users to opt-out of this unfortunate behaviour, as well as some other unfortunate FreeBSD compatibility behaviours). Fixes:21723a3974
("*: fix comparison of missing keywords") Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
This commit is contained in:
parent
ae454c4a6f
commit
1ce6aa8db5
3 changed files with 92 additions and 26 deletions
|
@ -7,6 +7,7 @@ import (
|
|||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -230,6 +231,77 @@ func TestCompareKeySubset(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCompareKeyDelta(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
|
||||
// Create a bunch of objects.
|
||||
tmpfile := filepath.Join(dir, "tmpfile")
|
||||
require.NoError(t, os.WriteFile(tmpfile, []byte("some content here"), 0666))
|
||||
|
||||
tmpdir := filepath.Join(dir, "testdir")
|
||||
require.NoError(t, os.Mkdir(tmpdir, 0755))
|
||||
|
||||
tmpsubfile := filepath.Join(tmpdir, "anotherfile")
|
||||
require.NoError(t, os.WriteFile(tmpsubfile, []byte("aaa"), 0666))
|
||||
|
||||
// Walk the current state.
|
||||
manifestKeywords := append(DefaultKeywords[:], "sha1digest")
|
||||
old, err := Walk(dir, nil, manifestKeywords, nil)
|
||||
require.NoErrorf(t, err, "walk %s", dir)
|
||||
|
||||
t.Run("Extra-Key", func(t *testing.T) {
|
||||
extraKeyword := Keyword("sha256digest")
|
||||
newManifestKeywords := append(manifestKeywords[:], extraKeyword)
|
||||
|
||||
new, err := Walk(dir, nil, newManifestKeywords, nil)
|
||||
require.NoErrorf(t, err, "walk %s", dir)
|
||||
|
||||
diffs, err := Compare(old, new, nil)
|
||||
require.NoError(t, err, "compare")
|
||||
|
||||
assert.NotEmpty(t, diffs, "extra keys in manifest should result in deltas")
|
||||
for _, diff := range diffs {
|
||||
if assert.Equal(t, Modified, diff.Type(), "extra keyword diff element should be 'modified'") {
|
||||
kds := diff.Diff()
|
||||
if assert.Len(t, kds, 1, "should only get a single key delta") {
|
||||
kd := kds[0]
|
||||
assert.Equalf(t, Extra, kd.Type(), "key %q", kd.Name())
|
||||
assert.Equal(t, extraKeyword, kd.Name())
|
||||
assert.Nil(t, kd.Old(), "Old for extra keyword delta")
|
||||
assert.NotNil(t, kd.New(), "New for extra keyword delta")
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Missing-Key", func(t *testing.T) {
|
||||
missingKeyword := Keyword("sha1digest")
|
||||
newManifestKeywords := slices.DeleteFunc(manifestKeywords[:], func(kw Keyword) bool {
|
||||
return kw == missingKeyword
|
||||
})
|
||||
|
||||
new, err := Walk(dir, nil, newManifestKeywords, nil)
|
||||
require.NoErrorf(t, err, "walk %s", dir)
|
||||
|
||||
diffs, err := Compare(old, new, nil)
|
||||
require.NoError(t, err, "compare")
|
||||
|
||||
assert.NotEmpty(t, diffs, "missing keys in manifest should result in deltas")
|
||||
for _, diff := range diffs {
|
||||
if assert.Equal(t, Modified, diff.Type(), "missing keyword diff element should be 'modified'") {
|
||||
kds := diff.Diff()
|
||||
if assert.Len(t, kds, 1, "should only get a single key delta") {
|
||||
kd := kds[0]
|
||||
assert.Equalf(t, Missing, kd.Type(), "key %q", kd.Name())
|
||||
assert.Equal(t, missingKeyword, kd.Name())
|
||||
assert.NotNil(t, kd.Old(), "Old for missing keyword delta")
|
||||
assert.Nil(t, kd.New(), "New for missing keyword delta")
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
//gocyclo:ignore
|
||||
func TestTarCompare(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue