Skip to content

Commit

Permalink
Use TESTS_UPDATE_GOLDEN to update golden files
Browse files Browse the repository at this point in the history
This will allow to update all golden files in one go, without having to
endure the update flag installation and potential warning that the flag
is not supported in some packages.
  • Loading branch information
didrocks committed Mar 15, 2024
1 parent f2e4349 commit fa37b17
Show file tree
Hide file tree
Showing 18 changed files with 12 additions and 103 deletions.
7 changes: 0 additions & 7 deletions cmd/adsysd/client/policy_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
"flag"
"os"
"testing"

Expand Down Expand Up @@ -52,9 +51,3 @@ Policies from user configuration:
want := testutils.LoadWithUpdateFromGolden(t, got)
require.Equal(t, want, got, "colorizePolicies returned expected formatted output")
}

func TestMain(m *testing.M) {
testutils.InstallUpdateFlag()
flag.Parse()
m.Run()
}
4 changes: 0 additions & 4 deletions cmd/adsysd/integration_tests/adsys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"crypto/rand"
"errors"
"flag"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -66,9 +65,6 @@ func TestMain(m *testing.M) {
defer testutils.SetupSmb(1446, filepath.Join(cwd, "testdata/AD/SYSVOL"))()
}

testutils.InstallUpdateFlag()
flag.Parse()

m.Run()

if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
Expand Down
8 changes: 0 additions & 8 deletions internal/ad/admxgen/dconf/dconf_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dconf_test

import (
"flag"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -109,10 +108,3 @@ func TestGenerate(t *testing.T) {
})
}
}

func TestMain(m *testing.M) {
testutils.InstallUpdateFlag()
flag.Parse()

m.Run()
}
8 changes: 0 additions & 8 deletions internal/ad/admxgen/internal_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package admxgen

import (
"flag"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -288,10 +287,3 @@ func TestExpandedCategoriesToMD(t *testing.T) {
})
}
}

func TestMain(m *testing.M) {
testutils.InstallUpdateFlag()
flag.Parse()

m.Run()
}
1 change: 0 additions & 1 deletion internal/ad/backends/sss/sss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ func (s sssdbus) IsOnline() (bool, *dbus.Error) {
}

func TestMain(m *testing.M) {
testutils.InstallUpdateFlag()
// TODO: do we want to alway print debug?
debug := flag.Bool("verbose", false, "Print debug log level information within the test")
flag.Parse()
Expand Down
1 change: 0 additions & 1 deletion internal/ad/backends/winbind/winbind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ func TestExecuteKinitCommand(_ *testing.T) {
}

func TestMain(m *testing.M) {
testutils.InstallUpdateFlag()
debug := flag.Bool("verbose", false, "Print debug log level information within the test")
flag.Parse()
if *debug {
Expand Down
3 changes: 0 additions & 3 deletions internal/ad/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,9 +579,6 @@ func TestParseGPOConcurrent(t *testing.T) {
const SmbPort = 1445

func TestMain(m *testing.M) {
testutils.InstallUpdateFlag()
flag.Parse()

// Don’t setup samba or sssd for mock helpers
if strings.Contains(strings.Join(os.Args, " "), "TestMock") {
m.Run()
Expand Down
8 changes: 0 additions & 8 deletions internal/config/watchd/watchd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package watchd_test

import (
"context"
"flag"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -131,10 +130,3 @@ func TestWriteConfig(t *testing.T) {
})
}
}

func TestMain(m *testing.M) {
testutils.InstallUpdateFlag()
flag.Parse()

m.Run()
}
8 changes: 0 additions & 8 deletions internal/policies/apparmor/apparmor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package apparmor_test
import (
"bufio"
"context"
"flag"
"fmt"
"io/fs"
"os"
Expand Down Expand Up @@ -313,10 +312,3 @@ func mockLoadedPoliciesFile(t *testing.T, policies []string) string {
require.NoError(t, err, "Setup: Can't write loaded policies file")
return path
}

func TestMain(m *testing.M) {
testutils.InstallUpdateFlag()
flag.Parse()

m.Run()
}
4 changes: 0 additions & 4 deletions internal/policies/certificate/certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package certificate_test

import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -170,9 +169,6 @@ func TestMockAutoenrollScript(t *testing.T) {
}

func TestMain(m *testing.M) {
testutils.InstallUpdateFlag()
flag.Parse()

m.Run()
testutils.MergeCoverages()
}
8 changes: 0 additions & 8 deletions internal/policies/dconf/dconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dconf_test

import (
"context"
"flag"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -223,10 +222,3 @@ func TestApplyPolicy(t *testing.T) {
})
}
}

func TestMain(m *testing.M) {
testutils.InstallUpdateFlag()
flag.Parse()

m.Run()
}
8 changes: 0 additions & 8 deletions internal/policies/gdm/gdm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gdm_test

import (
"context"
"flag"
"path/filepath"
"testing"

Expand Down Expand Up @@ -51,10 +50,3 @@ func TestApplyPolicy(t *testing.T) {
})
}
}

func TestMain(m *testing.M) {
testutils.InstallUpdateFlag()
flag.Parse()

m.Run()
}
7 changes: 0 additions & 7 deletions internal/policies/mount/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mount

import (
"context"
"flag"
"os"
"os/user"
"path/filepath"
Expand Down Expand Up @@ -158,9 +157,3 @@ func TestCreateUnits(t *testing.T) {
})
}
}

func TestMain(m *testing.M) {
testutils.InstallUpdateFlag()
flag.Parse()
m.Run()
}
4 changes: 0 additions & 4 deletions internal/policies/policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package policies_test
import (
"context"
"errors"
"flag"
"fmt"
"io/fs"
"log"
Expand Down Expand Up @@ -905,9 +904,6 @@ func equalPoliciesToGolden(t *testing.T, got policies.Policies, golden string, u
}

func TestMain(m *testing.M) {
testutils.InstallUpdateFlag()
flag.Parse()

// Don’t setup samba or sssd for mock helpers
if !strings.Contains(strings.Join(os.Args, " "), "TestMock") {
// Ubuntu Advantage
Expand Down
8 changes: 0 additions & 8 deletions internal/policies/privilege/privilege_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package privilege_test

import (
"context"
"flag"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -132,10 +131,3 @@ func TestApplyPolicy(t *testing.T) {
})
}
}

func TestMain(m *testing.M) {
testutils.InstallUpdateFlag()
flag.Parse()

m.Run()
}
8 changes: 0 additions & 8 deletions internal/policies/scripts/scripts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package scripts_test
import (
"context"
"errors"
"flag"
"io/fs"
"os"
"os/user"
Expand Down Expand Up @@ -294,10 +293,3 @@ func (s mockUnitStarter) StartUnit(_ context.Context, _ string) error {
}
return nil
}

func TestMain(m *testing.M) {
testutils.InstallUpdateFlag()
flag.Parse()

m.Run()
}
19 changes: 12 additions & 7 deletions internal/testutils/golden.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package testutils

import (
"flag"
"os"
"path/filepath"
"strings"
Expand All @@ -13,6 +12,18 @@ import (

var update bool

const (
// UpdateGoldenFilesEnv is the environment variable used to indicate go test that
// the golden files should be overwritten with the current test results.
UpdateGoldenFilesEnv = `TESTS_UPDATE_GOLDEN`
)

func init() {
if os.Getenv(UpdateGoldenFilesEnv) != "" {
update = true
}
}

type goldenOptions struct {
goldenPath string
}
Expand Down Expand Up @@ -107,12 +118,6 @@ func GoldenPath(t *testing.T) string {
return path
}

// InstallUpdateFlag install an update flag referenced in this package.
// The flags need to be parsed before running the tests.
func InstallUpdateFlag() {
flag.BoolVar(&update, "update", false, "update golden files")
}

// UpdateEnabled returns true if updating the golden files is requested.
func UpdateEnabled() bool {
return update
Expand Down
1 change: 0 additions & 1 deletion internal/watchdtui/watchdtui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,6 @@ func TestMain(m *testing.M) {
// Simulate a color terminal
lipgloss.SetColorProfile(termenv.ANSI256)

testutils.InstallUpdateFlag()
flag.BoolVar(&stdout, "stdout", false, "print output to stdout for debugging purposes")
flag.Parse()

Expand Down

0 comments on commit fa37b17

Please sign in to comment.