Skip to content

Commit

Permalink
pkg: test that "empty" RepoConfig marshal to "empty" json
Browse files Browse the repository at this point in the history
This is a regression test for an issue found during the review
of PR#284 [0]. The issue was that
```
	ModuleHotfixes *bool    `json:"module_hotfixes"`
```
lacked the required `omitempty` which may cause issues
with dnfjson.

As a quick and easy way to prevent this in the future
this commit adds a small test that ensures that we notice
any missing `omitempty`.

Note that the `rpmmd` part is not strictly needed but it
feels more correct to do the check in both places that
use a repoConfig.

[0] osbuild#284 (comment)
  • Loading branch information
mvo5 committed Dec 5, 2023
1 parent 11ef46d commit 37443fc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pkg/dnfjson/dnfjson_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dnfjson

import (
"encoding/json"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -662,3 +663,10 @@ func TestRequestHash(t *testing.T) {
assert.Equal(t, 64, len(req.Hash()))
assert.NotEqual(t, hash, req.Hash())
}

func TestRepoConfigMarshalAlsmostEmpty(t *testing.T) {
repoCfg := &repoConfig{}
js, _ := json.Marshal(repoCfg)
// double check that anything that uses pointers has "omitempty" set
assert.Equal(t, string(js), `{"id":"","gpgcheck":false,"check_repogpg":false,"ignoressl":false}`)
}
7 changes: 6 additions & 1 deletion pkg/rpmmd/repository_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rpmmd

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -26,7 +27,6 @@ func TestPackageSpecGetEVRA(t *testing.T) {

assert.Equal(t, "3.3a-3.fc38.x86_64", specs[0].GetEVRA())
assert.Equal(t, "1:2.06-94.fc38.noarch", specs[1].GetEVRA())

}

func TestPackageSpecGetNEVRA(t *testing.T) {
Expand All @@ -49,5 +49,10 @@ func TestPackageSpecGetNEVRA(t *testing.T) {

assert.Equal(t, "tmux-3.3a-3.fc38.x86_64", specs[0].GetNEVRA())
assert.Equal(t, "grub2-1:2.06-94.fc38.noarch", specs[1].GetNEVRA())
}

func TestRepoConfigMarshalEmpty(t *testing.T) {
repoCfg := &RepoConfig{}
js, _ := json.Marshal(repoCfg)
assert.Equal(t, string(js), `{}`)
}

0 comments on commit 37443fc

Please sign in to comment.