diff options
author | Bjørn Erik Pedersen <[email protected]> | 2019-08-10 21:05:17 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2019-08-12 13:26:32 +0200 |
commit | 9e571827055dedb46b78c5db3d17d6913f14870b (patch) | |
tree | f5f0108afe0c9385ff6dc27664943d9f719f57ad /commands | |
parent | 6027ee11082d0b9d72de1d4d1980a702be294ad2 (diff) | |
download | hugo-9e571827055dedb46b78c5db3d17d6913f14870b.tar.gz hugo-9e571827055dedb46b78c5db3d17d6913f14870b.zip |
tests: Convert from testify to quicktest
Diffstat (limited to 'commands')
-rw-r--r-- | commands/commands_test.go | 72 | ||||
-rw-r--r-- | commands/hugo_test.go | 8 | ||||
-rw-r--r-- | commands/import_jekyll_test.go | 20 | ||||
-rw-r--r-- | commands/list_test.go | 21 | ||||
-rw-r--r-- | commands/new_content_test.go | 46 | ||||
-rw-r--r-- | commands/server_test.go | 18 |
6 files changed, 98 insertions, 87 deletions
diff --git a/commands/commands_test.go b/commands/commands_test.go index a1c6cdd76..565736793 100644 --- a/commands/commands_test.go +++ b/commands/commands_test.go @@ -25,29 +25,29 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" - "github.com/stretchr/testify/require" + qt "github.com/frankban/quicktest" ) func TestExecute(t *testing.T) { - assert := require.New(t) + c := qt.New(t) dir, err := createSimpleTestSite(t, testSiteConfig{}) - assert.NoError(err) + c.Assert(err, qt.IsNil) defer func() { os.RemoveAll(dir) }() resp := Execute([]string{"-s=" + dir}) - assert.NoError(resp.Err) + c.Assert(resp.Err, qt.IsNil) result := resp.Result - assert.True(len(result.Sites) == 1) - assert.True(len(result.Sites[0].RegularPages()) == 1) + c.Assert(len(result.Sites) == 1, qt.Equals, true) + c.Assert(len(result.Sites[0].RegularPages()) == 1, qt.Equals, true) } func TestCommandsPersistentFlags(t *testing.T) { - assert := require.New(t) + c := qt.New(t) noOpRunE := func(cmd *cobra.Command, args []string) error { return nil @@ -83,10 +83,10 @@ func TestCommandsPersistentFlags(t *testing.T) { for _, command := range commands { if b, ok := command.(commandsBuilderGetter); ok { v := b.getCommandsBuilder().hugoBuilderCommon - assert.Equal("myconfig.toml", v.cfgFile) - assert.Equal("myconfigdir", v.cfgDir) - assert.Equal("mysource", v.source) - assert.Equal("https://example.com/b/", v.baseURL) + c.Assert(v.cfgFile, qt.Equals, "myconfig.toml") + c.Assert(v.cfgDir, qt.Equals, "myconfigdir") + c.Assert(v.source, qt.Equals, "mysource") + c.Assert(v.baseURL, qt.Equals, "https://example.com/b/") } if srvCmd, ok := command.(*serverCmd); ok { @@ -94,32 +94,32 @@ func TestCommandsPersistentFlags(t *testing.T) { } } - assert.NotNil(sc) - assert.True(sc.navigateToChanged) - assert.True(sc.disableLiveReload) - assert.True(sc.noHTTPCache) - assert.True(sc.renderToDisk) - assert.Equal(1366, sc.serverPort) - assert.Equal("testing", sc.environment) + c.Assert(sc, qt.Not(qt.IsNil)) + c.Assert(sc.navigateToChanged, qt.Equals, true) + c.Assert(sc.disableLiveReload, qt.Equals, true) + c.Assert(sc.noHTTPCache, qt.Equals, true) + c.Assert(sc.renderToDisk, qt.Equals, true) + c.Assert(sc.serverPort, qt.Equals, 1366) + c.Assert(sc.environment, qt.Equals, "testing") cfg := viper.New() sc.flagsToConfig(cfg) - assert.Equal("/tmp/mydestination", cfg.GetString("publishDir")) - assert.Equal("mycontent", cfg.GetString("contentDir")) - assert.Equal("mylayouts", cfg.GetString("layoutDir")) - assert.Equal([]string{"mytheme"}, cfg.GetStringSlice("theme")) - assert.Equal("mythemes", cfg.GetString("themesDir")) - assert.Equal("https://example.com/b/", cfg.GetString("baseURL")) + c.Assert(cfg.GetString("publishDir"), qt.Equals, "/tmp/mydestination") + c.Assert(cfg.GetString("contentDir"), qt.Equals, "mycontent") + c.Assert(cfg.GetString("layoutDir"), qt.Equals, "mylayouts") + c.Assert(cfg.GetStringSlice("theme"), qt.DeepEquals, []string{"mytheme"}) + c.Assert(cfg.GetString("themesDir"), qt.Equals, "mythemes") + c.Assert(cfg.GetString("baseURL"), qt.Equals, "https://example.com/b/") - assert.Equal([]string{"page", "home"}, cfg.Get("disableKinds")) + c.Assert(cfg.Get("disableKinds"), qt.DeepEquals, []string{"page", "home"}) - assert.True(cfg.GetBool("gc")) + c.Assert(cfg.GetBool("gc"), qt.Equals, true) // The flag is named path-warnings - assert.True(cfg.GetBool("logPathWarnings")) + c.Assert(cfg.GetBool("logPathWarnings"), qt.Equals, true) // The flag is named i18n-warnings - assert.True(cfg.GetBool("logI18nWarnings")) + c.Assert(cfg.GetBool("logI18nWarnings"), qt.Equals, true) }}} @@ -136,7 +136,7 @@ func TestCommandsPersistentFlags(t *testing.T) { } rootCmd := root.getCommand() rootCmd.SetArgs(test.args) - assert.NoError(rootCmd.Execute()) + c.Assert(rootCmd.Execute(), qt.IsNil) test.check(b.commands) } @@ -144,13 +144,13 @@ func TestCommandsPersistentFlags(t *testing.T) { func TestCommandsExecute(t *testing.T) { - assert := require.New(t) + c := qt.New(t) dir, err := createSimpleTestSite(t, testSiteConfig{}) - assert.NoError(err) + c.Assert(err, qt.IsNil) dirOut, err := ioutil.TempDir("", "hugo-cli-out") - assert.NoError(err) + c.Assert(err, qt.IsNil) defer func() { os.RemoveAll(dir) @@ -200,17 +200,17 @@ func TestCommandsExecute(t *testing.T) { _, err := hugoCmd.ExecuteC() if test.expectErrToContain != "" { - assert.Error(err, fmt.Sprintf("%v", test.commands)) - assert.Contains(err.Error(), test.expectErrToContain) + c.Assert(err, qt.Not(qt.IsNil)) + c.Assert(err.Error(), qt.Contains, test.expectErrToContain) } else { - assert.NoError(err, fmt.Sprintf("%v", test.commands)) + c.Assert(err, qt.IsNil) } // Assert that we have not left any development debug artifacts in // the code. if b.c != nil { _, ok := b.c.destinationFs.(types.DevMarker) - assert.False(ok) + c.Assert(ok, qt.Equals, false) } } diff --git a/commands/hugo_test.go b/commands/hugo_test.go index db6961b66..6a666ff4a 100644 --- a/commands/hugo_test.go +++ b/commands/hugo_test.go @@ -17,12 +17,12 @@ import ( "os" "testing" - "github.com/stretchr/testify/require" + qt "github.com/frankban/quicktest" ) // Issue #5662 func TestHugoWithContentDirOverride(t *testing.T) { - assert := require.New(t) + c := qt.New(t) hugoCmd := newCommandsBuilder().addAll().build() cmd := hugoCmd.getCommand() @@ -38,7 +38,7 @@ contentDir = "thisdoesnotexist" ` dir, err := createSimpleTestSite(t, testSiteConfig{configTOML: cfgStr, contentDir: contentDir}) - assert.NoError(err) + c.Assert(err, qt.IsNil) defer func() { os.RemoveAll(dir) @@ -47,6 +47,6 @@ contentDir = "thisdoesnotexist" cmd.SetArgs([]string{"-s=" + dir, "-c=" + contentDir}) _, err = cmd.ExecuteC() - assert.NoError(err) + c.Assert(err, qt.IsNil) } diff --git a/commands/import_jekyll_test.go b/commands/import_jekyll_test.go index e0402a7a6..4ae26b95c 100644 --- a/commands/import_jekyll_test.go +++ b/commands/import_jekyll_test.go @@ -15,12 +15,14 @@ package commands import ( "encoding/json" - "github.com/stretchr/testify/assert" "testing" "time" + + qt "github.com/frankban/quicktest" ) func TestParseJekyllFilename(t *testing.T) { + c := qt.New(t) filenameArray := []string{ "2015-01-02-test.md", "2012-03-15-中文.markup", @@ -36,13 +38,14 @@ func TestParseJekyllFilename(t *testing.T) { for i, filename := range filenameArray { postDate, postName, err := parseJekyllFilename(filename) - assert.Equal(t, err, nil) - assert.Equal(t, expectResult[i].postDate.Format("2006-01-02"), postDate.Format("2006-01-02")) - assert.Equal(t, expectResult[i].postName, postName) + c.Assert(err, qt.IsNil) + c.Assert(expectResult[i].postDate.Format("2006-01-02"), qt.Equals, postDate.Format("2006-01-02")) + c.Assert(expectResult[i].postName, qt.Equals, postName) } } func TestConvertJekyllMetadata(t *testing.T) { + c := qt.New(t) testDataList := []struct { metadata interface{} postName string @@ -73,14 +76,15 @@ func TestConvertJekyllMetadata(t *testing.T) { for _, data := range testDataList { result, err := convertJekyllMetaData(data.metadata, data.postName, data.postDate, data.draft) - assert.Equal(t, nil, err) + c.Assert(err, qt.IsNil) jsonResult, err := json.Marshal(result) - assert.Equal(t, nil, err) - assert.Equal(t, data.expect, string(jsonResult)) + c.Assert(err, qt.IsNil) + c.Assert(string(jsonResult), qt.Equals, data.expect) } } func TestConvertJekyllContent(t *testing.T) { + c := qt.New(t) testDataList := []struct { metadata interface{} content string @@ -124,6 +128,6 @@ func TestConvertJekyllContent(t *testing.T) { for _, data := range testDataList { result := convertJekyllContent(data.metadata, data.content) - assert.Equal(t, data.expect, result) + c.Assert(data.expect, qt.Equals, result) } } diff --git a/commands/list_test.go b/commands/list_test.go index 55a067444..bfc280679 100644 --- a/commands/list_test.go +++ b/commands/list_test.go @@ -9,8 +9,8 @@ import ( "strings" "testing" + qt "github.com/frankban/quicktest" "github.com/spf13/cobra" - "github.com/stretchr/testify/require" ) func captureStdout(f func() (*cobra.Command, error)) (string, error) { @@ -33,10 +33,10 @@ func captureStdout(f func() (*cobra.Command, error)) (string, error) { } func TestListAll(t *testing.T) { - assert := require.New(t) + c := qt.New(t) dir, err := createSimpleTestSite(t, testSiteConfig{}) - assert.NoError(err) + c.Assert(err, qt.IsNil) hugoCmd := newCommandsBuilder().addAll().build() cmd := hugoCmd.getCommand() @@ -48,24 +48,25 @@ func TestListAll(t *testing.T) { cmd.SetArgs([]string{"-s=" + dir, "list", "all"}) out, err := captureStdout(cmd.ExecuteC) - assert.NoError(err) + c.Assert(err, qt.IsNil) r := csv.NewReader(strings.NewReader(out)) header, err := r.Read() - assert.NoError(err) - assert.Equal([]string{ + c.Assert(err, qt.IsNil) + c.Assert(header, qt.DeepEquals, []string{ "path", "slug", "title", "date", "expiryDate", "publishDate", "draft", "permalink", - }, header) + }) record, err := r.Read() - assert.NoError(err) - assert.Equal([]string{ + + c.Assert(err, qt.IsNil) + c.Assert(record, qt.DeepEquals, []string{ filepath.Join("content", "p1.md"), "", "P1", "0001-01-01T00:00:00Z", "0001-01-01T00:00:00Z", "0001-01-01T00:00:00Z", "false", "https://example.org/p1/", - }, record) + }) } diff --git a/commands/new_content_test.go b/commands/new_content_test.go index 5a55094d6..36726e37a 100644 --- a/commands/new_content_test.go +++ b/commands/new_content_test.go @@ -19,19 +19,20 @@ import ( "github.com/gohugoio/hugo/hugofs" "github.com/spf13/viper" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" + + qt "github.com/frankban/quicktest" ) // Issue #1133 func TestNewContentPathSectionWithForwardSlashes(t *testing.T) { + c := qt.New(t) p, s := newContentPathSection(nil, "/post/new.md") - assert.Equal(t, filepath.FromSlash("/post/new.md"), p) - assert.Equal(t, "post", s) + c.Assert(p, qt.Equals, filepath.FromSlash("/post/new.md")) + c.Assert(s, qt.Equals, "post") } func checkNewSiteInited(fs *hugofs.Fs, basepath string, t *testing.T) { - + c := qt.New(t) paths := []string{ filepath.Join(basepath, "layouts"), filepath.Join(basepath, "content"), @@ -43,77 +44,82 @@ func checkNewSiteInited(fs *hugofs.Fs, basepath string, t *testing.T) { for _, path := range paths { _, err := fs.Source.Stat(path) - require.NoError(t, err) + c.Assert(err, qt.IsNil) } } func TestDoNewSite(t *testing.T) { + c := qt.New(t) n := newNewSiteCmd() basepath := filepath.Join("base", "blog") _, fs := newTestCfg() - require.NoError(t, n.doNewSite(fs, basepath, false)) + c.Assert(n.doNewSite(fs, basepath, false), qt.IsNil) checkNewSiteInited(fs, basepath, t) } func TestDoNewSite_noerror_base_exists_but_empty(t *testing.T) { + c := qt.New(t) basepath := filepath.Join("base", "blog") _, fs := newTestCfg() n := newNewSiteCmd() - require.NoError(t, fs.Source.MkdirAll(basepath, 0777)) + c.Assert(fs.Source.MkdirAll(basepath, 0777), qt.IsNil) - require.NoError(t, n.doNewSite(fs, basepath, false)) + c.Assert(n.doNewSite(fs, basepath, false), qt.IsNil) } func TestDoNewSite_error_base_exists(t *testing.T) { + c := qt.New(t) basepath := filepath.Join("base", "blog") _, fs := newTestCfg() n := newNewSiteCmd() - require.NoError(t, fs.Source.MkdirAll(basepath, 0777)) + c.Assert(fs.Source.MkdirAll(basepath, 0777), qt.IsNil) _, err := fs.Source.Create(filepath.Join(basepath, "foo")) - require.NoError(t, err) + c.Assert(err, qt.IsNil) // Since the directory already exists and isn't empty, expect an error - require.Error(t, n.doNewSite(fs, basepath, false)) + c.Assert(n.doNewSite(fs, basepath, false), qt.Not(qt.IsNil)) } func TestDoNewSite_force_empty_dir(t *testing.T) { + c := qt.New(t) basepath := filepath.Join("base", "blog") _, fs := newTestCfg() n := newNewSiteCmd() - require.NoError(t, fs.Source.MkdirAll(basepath, 0777)) - - require.NoError(t, n.doNewSite(fs, basepath, true)) + c.Assert(fs.Source.MkdirAll(basepath, 0777), qt.IsNil) + c.Assert(n.doNewSite(fs, basepath, true), qt.IsNil) checkNewSiteInited(fs, basepath, t) } func TestDoNewSite_error_force_dir_inside_exists(t *testing.T) { + c := qt.New(t) basepath := filepath.Join("base", "blog") _, fs := newTestCfg() n := newNewSiteCmd() contentPath := filepath.Join(basepath, "content") - require.NoError(t, fs.Source.MkdirAll(contentPath, 0777)) - require.Error(t, n.doNewSite(fs, basepath, true)) + c.Assert(fs.Source.MkdirAll(contentPath, 0777), qt.IsNil) + c.Assert(n.doNewSite(fs, basepath, true), qt.Not(qt.IsNil)) } func TestDoNewSite_error_force_config_inside_exists(t *testing.T) { + c := qt.New(t) basepath := filepath.Join("base", "blog") _, fs := newTestCfg() n := newNewSiteCmd() configPath := filepath.Join(basepath, "config.toml") - require.NoError(t, fs.Source.MkdirAll(basepath, 0777)) + c.Assert(fs.Source.MkdirAll(basepath, 0777), qt.IsNil) _, err := fs.Source.Create(configPath) - require.NoError(t, err) + c.Assert(err, qt.IsNil) - require.Error(t, n.doNewSite(fs, basepath, true)) + c.Assert(n.doNewSite(fs, basepath, true), qt.Not(qt.IsNil)) } func newTestCfg() (*viper.Viper, *hugofs.Fs) { diff --git a/commands/server_test.go b/commands/server_test.go index acee19cb8..8bd96c6ec 100644 --- a/commands/server_test.go +++ b/commands/server_test.go @@ -24,8 +24,8 @@ import ( "github.com/gohugoio/hugo/helpers" + qt "github.com/frankban/quicktest" "github.com/spf13/viper" - "github.com/stretchr/testify/require" ) func TestServer(t *testing.T) { @@ -33,9 +33,9 @@ func TestServer(t *testing.T) { // TODO(bep) not sure why server tests have started to fail on the Windows CI server. t.Skip("Skip server test on appveyor") } - assert := require.New(t) + c := qt.New(t) dir, err := createSimpleTestSite(t, testSiteConfig{}) - assert.NoError(err) + c.Assert(err, qt.IsNil) // Let us hope that this port is available on all systems ... port := 1331 @@ -54,7 +54,7 @@ func TestServer(t *testing.T) { go func() { _, err = cmd.ExecuteC() - assert.NoError(err) + c.Assert(err, qt.IsNil) }() // There is no way to know exactly when the server is ready for connections. @@ -63,12 +63,12 @@ func TestServer(t *testing.T) { time.Sleep(2 * time.Second) resp, err := http.Get("http://localhost:1331/") - assert.NoError(err) + c.Assert(err, qt.IsNil) defer resp.Body.Close() homeContent := helpers.ReaderToString(resp.Body) - assert.Contains(homeContent, "List: Hugo Commands") - assert.Contains(homeContent, "Environment: development") + c.Assert(homeContent, qt.Contains, "List: Hugo Commands") + c.Assert(homeContent, qt.Contains, "Environment: development") // Stop the server. stop <- true @@ -118,14 +118,14 @@ func TestFixURL(t *testing.T) { } func TestRemoveErrorPrefixFromLog(t *testing.T) { - assert := require.New(t) + c := qt.New(t) content := `ERROR 2018/10/07 13:11:12 Error while rendering "home": template: _default/baseof.html:4:3: executing "main" at <partial "logo" .>: error calling partial: template: partials/logo.html:5:84: executing "partials/logo.html" at <$resized.AHeight>: can't evaluate field AHeight in type *resource.Image ERROR 2018/10/07 13:11:12 Rebuild failed: logged 1 error(s) ` withoutError := removeErrorPrefixFromLog(content) - assert.False(strings.Contains(withoutError, "ERROR"), withoutError) + c.Assert(strings.Contains(withoutError, "ERROR"), qt.Equals, false) } |