Skip to content

Commit

Permalink
moved testify compatible code to its own package so it can be reused (#…
Browse files Browse the repository at this point in the history
…620)

* moved testify compatible code to its own package so it can be reused
  • Loading branch information
ldemailly committed Sep 6, 2022
1 parent 3211570 commit cbf33f7
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 265 deletions.
16 changes: 6 additions & 10 deletions dflag/checksum_test.go
Expand Up @@ -8,14 +8,10 @@ import (
"testing"
"time"

"fortio.org/assert"
"fortio.org/fortio/dflag"
)

var (
assert = dflag.Testify{}
require = assert
)

func TestChecksumFlagSet_Differs(t *testing.T) {
set := flag.NewFlagSet("foobar", flag.ContinueOnError)
dflag.DynDuration(set, "some_duration_1", 5*time.Second, "Use it or lose it")
Expand All @@ -30,17 +26,17 @@ func TestChecksumFlagSet_Differs(t *testing.T) {
t.Logf("post init checksum: %x", postInitChecksum)
assert.NotEqual(t, preInitChecksum, postInitChecksum, "checksum must be different init changed 2 flags")

require.NoError(t, set.Set("some_int_1", "1337"))
assert.NoError(t, set.Set("some_int_1", "1337"))
postSet1Checksum := dflag.ChecksumFlagSet(set, nil)
t.Logf("post set1 checksum: %x", postSet1Checksum)
assert.NotEqual(t, postInitChecksum, postSet1Checksum, "checksum must be different after a internal flag change")

require.NoError(t, set.Set("some_duration_1", "4s"))
assert.NoError(t, set.Set("some_duration_1", "4s"))
postSet2Checksum := dflag.ChecksumFlagSet(set, nil)
t.Logf("post set2 checksum: %x", postSet2Checksum)
assert.NotEqual(t, postSet1Checksum, postSet2Checksum, "checksum must be different after a internal flag change")

require.NoError(t, set.Set("some_duration_1", "3s"))
assert.NoError(t, set.Set("some_duration_1", "3s"))
postSet3Checksum := dflag.ChecksumFlagSet(set, nil)
t.Logf("post set3 checksum: %x", postSet3Checksum)
assert.EqualValues(t, postSet1Checksum, postSet3Checksum, "flipping back duration flag to state at set1 should make it equal")
Expand All @@ -56,12 +52,12 @@ func TestChecksumFlagSet_Filters(t *testing.T) {
postInitChecksum := dflag.ChecksumFlagSet(set, filterOnlyDuration)
t.Logf("post init checksum: %x", postInitChecksum)

require.NoError(t, set.Set("some_int_1", "1337"))
assert.NoError(t, set.Set("some_int_1", "1337"))
postSet1Checksum := dflag.ChecksumFlagSet(set, filterOnlyDuration)
t.Logf("post set1 checksum: %x", postSet1Checksum)
assert.EqualValues(t, postInitChecksum, postSet1Checksum, "checksum should not include some_int_1 change")

require.NoError(t, set.Set("some_duration_1", "10s"))
assert.NoError(t, set.Set("some_duration_1", "10s"))
postSet2Checksum := dflag.ChecksumFlagSet(set, filterOnlyDuration)
t.Logf("post set2 checksum: %x", postSet2Checksum)
assert.NotEqual(t, postSet1Checksum, postSet2Checksum, "checksum change when some_duration_1 changes")
Expand Down
39 changes: 17 additions & 22 deletions dflag/configmap/updater_test.go
Expand Up @@ -13,6 +13,7 @@ import (
"testing"
"time"

"fortio.org/assert"
"fortio.org/fortio/dflag"
"fortio.org/fortio/dflag/configmap"
)
Expand All @@ -23,14 +24,8 @@ const (
badStaticDir = "..1289_09_10_03_32_32.039823124"
)

var (
assert = dflag.Testify{}
require = assert
suite = assert
)

type updaterTestSuite struct {
dflag.TestSuite
assert.TestSuite
tempDir string

flagSet *flag.FlagSet
Expand All @@ -43,7 +38,7 @@ type updaterTestSuite struct {
func (s *updaterTestSuite) SetupTest() {
var err error
s.tempDir, err = os.MkdirTemp("/tmp", "updater_test")
require.NoError(s.T(), err, "failed creating temp directory for testing")
assert.NoError(s.T(), err, "failed creating temp directory for testing")
s.copyTestDataToDir()
s.linkDataDirTo(firstGoodDir)

Expand All @@ -52,57 +47,57 @@ func (s *updaterTestSuite) SetupTest() {
s.staticInt = s.flagSet.Int("some_int", 1, "static int for testing")

s.updater, err = configmap.New(s.flagSet, path.Join(s.tempDir, "testdata"))
require.NoError(s.T(), err, "creating a config map must not fail")
assert.NoError(s.T(), err, "creating a config map must not fail")
}

// Tear down the updater.
func (s *updaterTestSuite) TearDownTest() {
require.NoError(s.T(), os.RemoveAll(s.tempDir), "clearing up the test dir must not fail")
assert.NoError(s.T(), os.RemoveAll(s.tempDir), "clearing up the test dir must not fail")
_ = s.updater.Stop()
time.Sleep(100 * time.Millisecond)
}

func (s *updaterTestSuite) copyTestDataToDir() {
copyCmd := exec.Command("cp", "-a", "testdata", s.tempDir)
require.NoError(s.T(), copyCmd.Run(), "copying testdata directory to tempdir must not fail")
assert.NoError(s.T(), copyCmd.Run(), "copying testdata directory to tempdir must not fail")
// We are storing file testdata/9989_09_09_07_32_32.099817316 and renaming it to testdata/..9989_09_09_07_32_32.099817316,
// because go modules don't allow repos with files with .. in their filename. See https://github.com/golang/go/issues/27299.
for _, p := range []string{firstGoodDir, secondGoodDir, badStaticDir} {
pOld := filepath.Join(s.tempDir, "testdata", strings.TrimPrefix(p, ".."))
pNew := filepath.Join(s.tempDir, "testdata", p)
require.NoError(s.T(), os.Rename(pOld, pNew), "renaming %q to %q failed", pOld, pNew)
assert.NoError(s.T(), os.Rename(pOld, pNew), "renaming %q to %q failed", pOld, pNew)
}
}

func (s *updaterTestSuite) linkDataDirTo(newDataDir string) {
copyCmd := exec.Command("ln", "-s", "-n", "-f",
path.Join(s.tempDir, "testdata", newDataDir),
path.Join(s.tempDir, "testdata", "..data"))
require.NoError(s.T(), copyCmd.Run(), "relinking ..data in tempdir tempdir must not fail")
assert.NoError(s.T(), copyCmd.Run(), "relinking ..data in tempdir tempdir must not fail")
}

func (s *updaterTestSuite) TestInitializeFailsOnBadFormedFlag() {
s.linkDataDirTo(badStaticDir)
require.Error(s.T(), s.updater.Initialize(), "the updater initialize should return error on bad flags")
assert.Error(s.T(), s.updater.Initialize(), "the updater initialize should return error on bad flags")
}

func (s *updaterTestSuite) TestSetupFunction() {
tmpU, err := configmap.Setup(s.flagSet, path.Join(s.tempDir, "testdata"))
require.NoError(s.T(), err, "setup for a config map must not fail")
require.Error(s.T(), tmpU.Initialize(), "should error with already started")
require.Error(s.T(), tmpU.Start(), "should error with already started")
require.NoError(s.T(), tmpU.Stop(), "stopping the watcher should succeed")
assert.NoError(s.T(), err, "setup for a config map must not fail")
assert.Error(s.T(), tmpU.Initialize(), "should error with already started")
assert.Error(s.T(), tmpU.Start(), "should error with already started")
assert.NoError(s.T(), tmpU.Stop(), "stopping the watcher should succeed")
}

func (s *updaterTestSuite) TestInitializeSetsValues() {
require.NoError(s.T(), s.updater.Initialize(), "the updater initialize should not return errors on good flags")
assert.NoError(s.T(), s.updater.Initialize(), "the updater initialize should not return errors on good flags")
assert.EqualValues(s.T(), *s.staticInt, 1234, "staticInt should be some_int from first directory")
assert.EqualValues(s.T(), s.dynInt.Get(), int64(10001), "staticInt should be some_int from first directory")
}

func (s *updaterTestSuite) TestDynamicUpdatesPropagate() {
require.NoError(s.T(), s.updater.Initialize(), "the updater initialize should not return errors on good flags")
require.NoError(s.T(), s.updater.Start(), "updater start should not return an error")
assert.NoError(s.T(), s.updater.Initialize(), "the updater initialize should not return errors on good flags")
assert.NoError(s.T(), s.updater.Start(), "updater start should not return an error")
s.linkDataDirTo(secondGoodDir)
eventually(s.T(), 1*time.Second,
assert.ObjectsAreEqualValues, int64(20002),
Expand All @@ -111,7 +106,7 @@ func (s *updaterTestSuite) TestDynamicUpdatesPropagate() {
}

func TestUpdaterSuite(t *testing.T) {
suite.Run(t, &updaterTestSuite{})
assert.Run(t, &updaterTestSuite{})
}

type (
Expand Down
2 changes: 2 additions & 0 deletions dflag/dynbool_test.go
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"testing"
"time"

"fortio.org/assert"
)

func TestDynBool_SetAndGet(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions dflag/dynduration_test.go
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"testing"
"time"

"fortio.org/assert"
)

func TestDynDuration_SetAndGet(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions dflag/dynfloat64_test.go
Expand Up @@ -7,6 +7,8 @@ import (
"flag"
"testing"
"time"

"fortio.org/assert"
)

func TestDynFloat64_SetAndGet(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions dflag/dyngeneric_test.go
Expand Up @@ -6,6 +6,8 @@ package dflag
import (
"flag"
"testing"

"fortio.org/assert"
)

// Additional generic tests, most tests are covered by the old per type tests.
Expand Down
2 changes: 2 additions & 0 deletions dflag/dynint64_test.go
Expand Up @@ -7,6 +7,8 @@ import (
"flag"
"testing"
"time"

"fortio.org/assert"
)

func TestDynInt64_SetAndGet(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions dflag/dynjson_test.go
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"testing"
"time"

"fortio.org/assert"
)

var defaultJSON = &outerJSON{
Expand Down
2 changes: 2 additions & 0 deletions dflag/dynstring_test.go
Expand Up @@ -8,6 +8,8 @@ import (
"regexp"
"testing"
"time"

"fortio.org/assert"
)

const notifierTimeout = 50 * time.Millisecond
Expand Down
2 changes: 2 additions & 0 deletions dflag/dynstringset_test.go
Expand Up @@ -7,6 +7,8 @@ import (
"flag"
"testing"
"time"

"fortio.org/assert"
)

func TestDynStringSet_SetAndGet(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions dflag/dynstringslice_test.go
Expand Up @@ -7,6 +7,8 @@ import (
"flag"
"testing"
"time"

"fortio.org/assert"
)

func TestDynStringSlice_SetAndGet(t *testing.T) {
Expand Down
23 changes: 9 additions & 14 deletions dflag/endpoint/endpoint_test.go
Expand Up @@ -12,23 +12,18 @@ import (
"sort"
"testing"

"fortio.org/assert"
"fortio.org/fortio/dflag"
)

type endpointTestSuite struct {
dflag.TestSuite
assert.TestSuite
flagSet *flag.FlagSet
endpoint *FlagsEndpoint
}

var (
assert = dflag.Testify{}
require = assert
suite = assert
)

func TestEndpointTestSuite(t *testing.T) {
suite.Run(t, &endpointTestSuite{})
assert.Run(t, &endpointTestSuite{})
}

func (s *endpointTestSuite) SetupTest() {
Expand Down Expand Up @@ -117,8 +112,8 @@ func (s *endpointTestSuite) TestServesHTML() {
req.Header.Add("Accept", "application/xhtml+xml")
resp := httptest.NewRecorder()
s.endpoint.ListFlags(resp, req)
require.Equal(s.T(), http.StatusOK, resp.Code, "dflag list request must return 200 OK")
require.Contains(s.T(), resp.Header().Get("Content-Type"), "html", "must indicate html in content type")
assert.Equal(s.T(), http.StatusOK, resp.Code, "dflag list request must return 200 OK")
assert.Contains(s.T(), resp.Header().Get("Content-Type"), "html", "must indicate html in content type")

out := resp.Body.String()
assert.Contains(s.T(), out, "<html>")
Expand All @@ -128,10 +123,10 @@ func (s *endpointTestSuite) TestServesHTML() {
func (s *endpointTestSuite) processFlagSetJSONResponse(req *http.Request) *flagSetJSON {
resp := httptest.NewRecorder()
s.endpoint.ListFlags(resp, req)
require.Equal(s.T(), http.StatusOK, resp.Code, "dflag list request must return 200 OK")
require.Equal(s.T(), "application/json", resp.Header().Get("Content-Type"), "type must be indicated")
assert.Equal(s.T(), http.StatusOK, resp.Code, "dflag list request must return 200 OK")
assert.Equal(s.T(), "application/json", resp.Header().Get("Content-Type"), "type must be indicated")
ret := &flagSetJSON{}
require.NoError(s.T(), json.Unmarshal(resp.Body.Bytes(), ret), "unmarshaling JSON response must succeed")
assert.NoError(s.T(), json.Unmarshal(resp.Body.Bytes(), ret), "unmarshaling JSON response must succeed")
return ret
}

Expand All @@ -141,7 +136,7 @@ func (s *endpointTestSuite) assertListContainsOnly(flagList []string, list *flag
existing = append(existing, f.Name)
}
sort.Strings(flagList)
require.EqualValues(s.T(), flagList, existing, "expected set of listed flags must match")
assert.EqualValues(s.T(), flagList, existing, "expected set of listed flags must match")
}

func findFlagInFlagSetJSON(flagName string, list *flagSetJSON) *flagJSON {
Expand Down
2 changes: 2 additions & 0 deletions dflag/fileread_flag_test.go
Expand Up @@ -6,6 +6,8 @@ package dflag
import (
"flag"
"testing"

"fortio.org/assert"
)

func TestFileFlag_ReadsWithADefault(t *testing.T) {
Expand Down

0 comments on commit cbf33f7

Please sign in to comment.