Skip to content

Commit

Permalink
refactor: Rename DumpToYaml -> DumpNormalizedYAML
Browse files Browse the repository at this point in the history
I was surprised that "DumpToYaml" modifies some values in the returned
YAML. The new name should be less surprising.

It also contains the acronym YAML in uppercase, as recommended by the Go
Code Review Comments: https://go.dev/wiki/CodeReviewComments#initialisms

Also improves the doc comment of the function.
  • Loading branch information
adombeck committed Nov 26, 2024
1 parent c1d89fa commit 8f367d9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
6 changes: 3 additions & 3 deletions internal/services/pam/pam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ func TestIsAuthenticated(t *testing.T) {
require.Equal(t, want, got, "IsAuthenticated should return the expected combined data, but did not")

// Check that cache has been updated too.
gotDB, err := cachetestutils.DumpToYaml(userstestutils.GetManagerCache(m))
gotDB, err := cachetestutils.DumpNormalizedYAML(userstestutils.GetManagerCache(m))
require.NoError(t, err, "Setup: failed to dump database for comparing")
wantDB := testutils.LoadWithUpdateFromGolden(t, gotDB, testutils.WithGoldenPath(filepath.Join(testutils.GoldenPath(t), "cache.db")))
require.Equal(t, wantDB, gotDB, "IsAuthenticated should update the cache database as expected")
Expand Down Expand Up @@ -571,7 +571,7 @@ func TestIDGeneration(t *testing.T) {
require.NoError(t, err, "Setup: could not authenticate user")
require.Equal(t, "granted", resp.GetAccess(), "Setup: authentication should be granted")

gotDB, err := cachetestutils.DumpToYaml(userstestutils.GetManagerCache(m))
gotDB, err := cachetestutils.DumpNormalizedYAML(userstestutils.GetManagerCache(m))
require.NoError(t, err, "Setup: failed to dump database for comparing")
wantDB := testutils.LoadWithUpdateFromGolden(t, gotDB, testutils.WithGoldenPath(filepath.Join(testutils.GoldenPath(t), "cache.db")))
require.Equal(t, wantDB, gotDB, "IsAuthenticated should update the cache database as expected")
Expand Down Expand Up @@ -631,7 +631,7 @@ func TestSetDefaultBrokerForUser(t *testing.T) {
require.Equal(t, tc.brokerID, gpbResp.GetPreviousBroker(), "SetDefaultBrokerForUser should set the default broker as expected")

// Check that cache has been updated too.
gotDB, err := cachetestutils.DumpToYaml(userstestutils.GetManagerCache(m))
gotDB, err := cachetestutils.DumpNormalizedYAML(userstestutils.GetManagerCache(m))
require.NoError(t, err, "Setup: failed to dump database for comparing")
wantDB := testutils.LoadWithUpdateFromGolden(t, gotDB, testutils.WithGoldenPath(filepath.Join(testutils.GoldenPath(t), "cache.db")))
require.Equal(t, wantDB, gotDB, "SetDefaultBrokerForUser should update the cache database as expected")
Expand Down
6 changes: 3 additions & 3 deletions internal/users/cache/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestNew(t *testing.T) {
require.NoError(t, err)
defer c.Close()

got, err := cachetestutils.DumpToYaml(c)
got, err := cachetestutils.DumpNormalizedYAML(c)
require.NoError(t, err, "Created database should be valid yaml content")

want := testutils.LoadWithUpdateFromGolden(t, got)
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestUpdateUserEntry(t *testing.T) {
}
require.NoError(t, err)

got, err := cachetestutils.DumpToYaml(c)
got, err := cachetestutils.DumpNormalizedYAML(c)
require.NoError(t, err, "Created database should be valid yaml content")

want := testutils.LoadWithUpdateFromGolden(t, got)
Expand Down Expand Up @@ -480,7 +480,7 @@ func TestDeleteUser(t *testing.T) {
}
require.NoError(t, err)

got, err := cachetestutils.DumpToYaml(c)
got, err := cachetestutils.DumpNormalizedYAML(c)
require.NoError(t, err, "Created database should be valid yaml content")
want := testutils.LoadWithUpdateFromGolden(t, got)
require.Equal(t, want, got, "Did not get expected database content")
Expand Down
5 changes: 3 additions & 2 deletions internal/users/cache/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ func redactTime(line string) string {
return line
}

// dumpToYaml deserializes the cache database as a string in yaml format.
// dumpNormalizedYAML gets the content of the database, normalizes it (so that
// it can be compared with a golden file) and returns it as a YAML string.
//
//nolint:unused // This is used for tests, with go linking. Not part of exported API.
func (c *Cache) dumpToYaml() (string, error) {
func (c *Cache) dumpNormalizedYAML() (string, error) {
testsdetection.MustBeTesting()

d := make(map[string]map[string]string)
Expand Down
7 changes: 4 additions & 3 deletions internal/users/cache/testutils/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ var (
DbName string
)

// DumpToYaml deserializes the cache database as a string in yaml format.
// DumpNormalizedYAML gets the content of the database, normalizes it (so that
// it can be compared with a golden file) and returns it as a YAML string.
//
//go:linkname DumpToYaml github.com/ubuntu/authd/internal/users/cache.(*Cache).dumpToYaml
func DumpToYaml(c *cache.Cache) (string, error)
//go:linkname DumpNormalizedYAML github.com/ubuntu/authd/internal/users/cache.(*Cache).dumpNormalizedYAML
func DumpNormalizedYAML(c *cache.Cache) (string, error)

// DbfromYAML loads a yaml formatted of the buckets from a reader and dump it into destDir, with its dbname.
//
Expand Down
6 changes: 3 additions & 3 deletions internal/users/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestNewManager(t *testing.T) {
}
require.NoError(t, err, "NewManager should not return an error, but did")

got, err := cachetestutils.DumpToYaml(userstestutils.GetManagerCache(m))
got, err := cachetestutils.DumpNormalizedYAML(userstestutils.GetManagerCache(m))
require.NoError(t, err, "Created database should be valid yaml content")

want := testutils.LoadWithUpdateFromGolden(t, got)
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestUpdateUser(t *testing.T) {
require.Equal(t, oldUID, newUser.UID, "UID should not have changed")
}

got, err := cachetestutils.DumpToYaml(userstestutils.GetManagerCache(m))
got, err := cachetestutils.DumpNormalizedYAML(userstestutils.GetManagerCache(m))
require.NoError(t, err, "Created database should be valid yaml content")

want := testutils.LoadWithUpdateFromGoldenYAML(t, got)
Expand Down Expand Up @@ -315,7 +315,7 @@ func TestUpdateBrokerForUser(t *testing.T) {
return
}

got, err := cachetestutils.DumpToYaml(userstestutils.GetManagerCache(m))
got, err := cachetestutils.DumpNormalizedYAML(userstestutils.GetManagerCache(m))
require.NoError(t, err, "Created database should be valid yaml content")

want := testutils.LoadWithUpdateFromGoldenYAML(t, got)
Expand Down

0 comments on commit 8f367d9

Please sign in to comment.