2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2018-04-10 13:16:09 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
"github.com/gohugoio/hugo/common/types"
|
|
|
|
|
2018-04-13 02:42:29 -04:00
|
|
|
"github.com/spf13/cobra"
|
2018-04-14 03:17:30 -04:00
|
|
|
"github.com/spf13/viper"
|
2018-04-13 02:42:29 -04:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2018-04-10 13:16:09 -04:00
|
|
|
)
|
|
|
|
|
2018-04-11 14:17:28 -04:00
|
|
|
func TestExecute(t *testing.T) {
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-04-11 14:17:28 -04:00
|
|
|
|
2019-02-01 02:40:53 -05:00
|
|
|
dir, err := createSimpleTestSite(t, testSiteConfig{})
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2018-04-11 14:17:28 -04:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}()
|
|
|
|
|
|
|
|
resp := Execute([]string{"-s=" + dir})
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(resp.Err, qt.IsNil)
|
2018-04-11 14:17:28 -04:00
|
|
|
result := resp.Result
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(len(result.Sites) == 1, qt.Equals, true)
|
|
|
|
c.Assert(len(result.Sites[0].RegularPages()) == 1, qt.Equals, true)
|
2018-04-11 14:17:28 -04:00
|
|
|
}
|
|
|
|
|
2018-04-13 02:42:29 -04:00
|
|
|
func TestCommandsPersistentFlags(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-04-13 02:42:29 -04:00
|
|
|
|
|
|
|
noOpRunE := func(cmd *cobra.Command, args []string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
args []string
|
|
|
|
check func(command []cmder)
|
2018-04-14 03:17:30 -04:00
|
|
|
}{{[]string{"server",
|
|
|
|
"--config=myconfig.toml",
|
2018-11-15 03:28:02 -05:00
|
|
|
"--configDir=myconfigdir",
|
2018-04-14 03:17:30 -04:00
|
|
|
"--contentDir=mycontent",
|
2018-04-14 04:34:02 -04:00
|
|
|
"--disableKinds=page,home",
|
2018-11-15 03:28:02 -05:00
|
|
|
"--environment=testing",
|
|
|
|
"--configDir=myconfigdir",
|
2018-04-14 03:17:30 -04:00
|
|
|
"--layoutDir=mylayouts",
|
|
|
|
"--theme=mytheme",
|
2018-04-14 04:34:02 -04:00
|
|
|
"--gc",
|
2018-04-14 03:17:30 -04:00
|
|
|
"--themesDir=mythemes",
|
|
|
|
"--cleanDestinationDir",
|
|
|
|
"--navigateToChanged",
|
|
|
|
"--disableLiveReload",
|
|
|
|
"--noHTTPCache",
|
|
|
|
"--i18n-warnings",
|
|
|
|
"--destination=/tmp/mydestination",
|
|
|
|
"-b=https://example.com/b/",
|
|
|
|
"--port=1366",
|
|
|
|
"--renderToDisk",
|
|
|
|
"--source=mysource",
|
2019-01-02 06:33:26 -05:00
|
|
|
"--path-warnings",
|
2018-09-21 08:27:35 -04:00
|
|
|
}, func(commands []cmder) {
|
2018-04-14 03:17:30 -04:00
|
|
|
var sc *serverCmd
|
2018-04-13 02:42:29 -04:00
|
|
|
for _, command := range commands {
|
|
|
|
if b, ok := command.(commandsBuilderGetter); ok {
|
2018-11-06 02:45:19 -05:00
|
|
|
v := b.getCommandsBuilder().hugoBuilderCommon
|
2019-08-10 15:05:17 -04:00
|
|
|
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/")
|
2018-04-13 02:42:29 -04:00
|
|
|
}
|
2018-04-14 03:17:30 -04:00
|
|
|
|
|
|
|
if srvCmd, ok := command.(*serverCmd); ok {
|
|
|
|
sc = srvCmd
|
|
|
|
}
|
2018-04-13 02:42:29 -04:00
|
|
|
}
|
2018-04-14 03:17:30 -04:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
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")
|
2018-04-14 03:17:30 -04:00
|
|
|
|
|
|
|
cfg := viper.New()
|
|
|
|
sc.flagsToConfig(cfg)
|
2019-08-10 15:05:17 -04:00
|
|
|
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/")
|
2018-04-14 03:17:30 -04:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(cfg.Get("disableKinds"), qt.DeepEquals, []string{"page", "home"})
|
2018-04-14 04:34:02 -04:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(cfg.GetBool("gc"), qt.Equals, true)
|
2018-04-14 03:17:30 -04:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
// The flag is named path-warnings
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(cfg.GetBool("logPathWarnings"), qt.Equals, true)
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2018-04-14 03:17:30 -04:00
|
|
|
// The flag is named i18n-warnings
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(cfg.GetBool("logI18nWarnings"), qt.Equals, true)
|
2018-04-14 03:17:30 -04:00
|
|
|
|
2018-04-13 02:42:29 -04:00
|
|
|
}}}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
b := newCommandsBuilder()
|
|
|
|
root := b.addAll().build()
|
|
|
|
|
|
|
|
for _, c := range b.commands {
|
2018-04-16 02:23:32 -04:00
|
|
|
if c.getCommand() == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-04-13 02:42:29 -04:00
|
|
|
// We are only intereseted in the flag handling here.
|
|
|
|
c.getCommand().RunE = noOpRunE
|
|
|
|
}
|
|
|
|
rootCmd := root.getCommand()
|
|
|
|
rootCmd.SetArgs(test.args)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(rootCmd.Execute(), qt.IsNil)
|
2018-04-13 02:42:29 -04:00
|
|
|
test.check(b.commands)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandsExecute(t *testing.T) {
|
2018-04-10 13:16:09 -04:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-04-10 13:16:09 -04:00
|
|
|
|
2019-02-01 02:40:53 -05:00
|
|
|
dir, err := createSimpleTestSite(t, testSiteConfig{})
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2018-04-10 13:16:09 -04:00
|
|
|
|
|
|
|
dirOut, err := ioutil.TempDir("", "hugo-cli-out")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2018-04-10 13:16:09 -04:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
os.RemoveAll(dirOut)
|
|
|
|
}()
|
|
|
|
|
|
|
|
sourceFlag := fmt.Sprintf("-s=%s", dir)
|
|
|
|
|
|
|
|
tests := []struct {
|
2018-04-11 02:31:18 -04:00
|
|
|
commands []string
|
|
|
|
flags []string
|
|
|
|
expectErrToContain string
|
2018-04-10 13:16:09 -04:00
|
|
|
}{
|
2018-04-11 02:31:18 -04:00
|
|
|
// TODO(bep) permission issue on my OSX? "operation not permitted" {[]string{"check", "ulimit"}, nil, false},
|
|
|
|
{[]string{"env"}, nil, ""},
|
|
|
|
{[]string{"version"}, nil, ""},
|
2018-04-10 13:16:09 -04:00
|
|
|
// no args = hugo build
|
2018-04-11 02:31:18 -04:00
|
|
|
{nil, []string{sourceFlag}, ""},
|
2018-04-11 02:39:39 -04:00
|
|
|
{nil, []string{sourceFlag, "--renderToMemory"}, ""},
|
2018-04-11 04:09:45 -04:00
|
|
|
{[]string{"config"}, []string{sourceFlag}, ""},
|
2018-04-11 02:31:18 -04:00
|
|
|
{[]string{"convert", "toTOML"}, []string{sourceFlag, "-o=" + filepath.Join(dirOut, "toml")}, ""},
|
|
|
|
{[]string{"convert", "toYAML"}, []string{sourceFlag, "-o=" + filepath.Join(dirOut, "yaml")}, ""},
|
|
|
|
{[]string{"convert", "toJSON"}, []string{sourceFlag, "-o=" + filepath.Join(dirOut, "json")}, ""},
|
|
|
|
{[]string{"gen", "autocomplete"}, []string{"--completionfile=" + filepath.Join(dirOut, "autocomplete.txt")}, ""},
|
|
|
|
{[]string{"gen", "chromastyles"}, []string{"--style=manni"}, ""},
|
|
|
|
{[]string{"gen", "doc"}, []string{"--dir=" + filepath.Join(dirOut, "doc")}, ""},
|
|
|
|
{[]string{"gen", "man"}, []string{"--dir=" + filepath.Join(dirOut, "man")}, ""},
|
|
|
|
{[]string{"list", "drafts"}, []string{sourceFlag}, ""},
|
|
|
|
{[]string{"list", "expired"}, []string{sourceFlag}, ""},
|
|
|
|
{[]string{"list", "future"}, []string{sourceFlag}, ""},
|
|
|
|
{[]string{"new", "new-page.md"}, []string{sourceFlag}, ""},
|
|
|
|
{[]string{"new", "site", filepath.Join(dirOut, "new-site")}, nil, ""},
|
|
|
|
{[]string{"unknowncommand"}, nil, "unknown command"},
|
2018-04-10 13:16:09 -04:00
|
|
|
// TODO(bep) cli refactor fix https://github.com/gohugoio/hugo/issues/4450
|
2018-04-11 02:31:18 -04:00
|
|
|
//{[]string{"new", "theme", filepath.Join(dirOut, "new-theme")}, nil,false},
|
2018-04-10 13:16:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2019-01-02 06:33:26 -05:00
|
|
|
b := newCommandsBuilder().addAll().build()
|
|
|
|
hugoCmd := b.getCommand()
|
2018-04-10 13:16:09 -04:00
|
|
|
test.flags = append(test.flags, "--quiet")
|
|
|
|
hugoCmd.SetArgs(append(test.commands, test.flags...))
|
|
|
|
|
|
|
|
// TODO(bep) capture output and add some simple asserts
|
2018-04-11 02:31:18 -04:00
|
|
|
// TODO(bep) misspelled subcommands does not return an error. We should investigate this
|
|
|
|
// but before that, check for "Error: unknown command".
|
|
|
|
|
|
|
|
_, err := hugoCmd.ExecuteC()
|
|
|
|
if test.expectErrToContain != "" {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
|
|
|
c.Assert(err.Error(), qt.Contains, test.expectErrToContain)
|
2018-04-11 02:31:18 -04:00
|
|
|
} else {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2018-04-11 02:31:18 -04:00
|
|
|
}
|
2018-04-10 13:16:09 -04:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
// Assert that we have not left any development debug artifacts in
|
|
|
|
// the code.
|
|
|
|
if b.c != nil {
|
|
|
|
_, ok := b.c.destinationFs.(types.DevMarker)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(ok, qt.Equals, false)
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
2018-04-10 13:16:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-01 02:40:53 -05:00
|
|
|
type testSiteConfig struct {
|
|
|
|
configTOML string
|
|
|
|
contentDir string
|
|
|
|
}
|
|
|
|
|
|
|
|
func createSimpleTestSite(t *testing.T, cfg testSiteConfig) (string, error) {
|
2018-04-10 13:16:09 -04:00
|
|
|
d, e := ioutil.TempDir("", "hugo-cli")
|
|
|
|
if e != nil {
|
|
|
|
return "", e
|
|
|
|
}
|
|
|
|
|
2019-02-01 02:40:53 -05:00
|
|
|
cfgStr := `
|
2018-04-10 13:16:09 -04:00
|
|
|
|
|
|
|
baseURL = "https://example.org"
|
|
|
|
title = "Hugo Commands"
|
|
|
|
|
2019-02-01 02:40:53 -05:00
|
|
|
`
|
|
|
|
|
|
|
|
contentDir := "content"
|
|
|
|
|
|
|
|
if cfg.configTOML != "" {
|
|
|
|
cfgStr = cfg.configTOML
|
|
|
|
}
|
|
|
|
if cfg.contentDir != "" {
|
|
|
|
contentDir = cfg.contentDir
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just the basic. These are for CLI tests, not site testing.
|
|
|
|
writeFile(t, filepath.Join(d, "config.toml"), cfgStr)
|
2018-04-10 13:16:09 -04:00
|
|
|
|
2019-02-01 02:40:53 -05:00
|
|
|
writeFile(t, filepath.Join(d, contentDir, "p1.md"), `
|
2018-04-10 13:16:09 -04:00
|
|
|
---
|
|
|
|
title: "P1"
|
|
|
|
weight: 1
|
|
|
|
---
|
|
|
|
|
|
|
|
Content
|
|
|
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
writeFile(t, filepath.Join(d, "layouts", "_default", "single.html"), `
|
|
|
|
|
|
|
|
Single: {{ .Title }}
|
|
|
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
writeFile(t, filepath.Join(d, "layouts", "_default", "list.html"), `
|
|
|
|
|
|
|
|
List: {{ .Title }}
|
2018-11-15 03:28:02 -05:00
|
|
|
Environment: {{ hugo.Environment }}
|
2018-04-10 13:16:09 -04:00
|
|
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeFile(t *testing.T, filename, content string) {
|
|
|
|
must(t, os.MkdirAll(filepath.Dir(filename), os.FileMode(0755)))
|
|
|
|
must(t, ioutil.WriteFile(filename, []byte(content), os.FileMode(0755)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func must(t *testing.T, err error) {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|