all: Apply staticcheck recommendations

This commit is contained in:
Bjørn Erik Pedersen 2019-03-24 10:11:16 +01:00
parent 3011f36c27
commit b5f39d23b8
41 changed files with 98 additions and 252 deletions

View file

@ -274,7 +274,7 @@ func (c *Cache) isExpired(modTime time.Time) bool {
if c.maxAge < 0 { if c.maxAge < 0 {
return false return false
} }
return c.maxAge == 0 || time.Now().Sub(modTime) > c.maxAge return c.maxAge == 0 || time.Since(modTime) > c.maxAge
} }
// For testing // For testing

View file

@ -46,7 +46,7 @@ func DoArithmetic(a, b interface{}, op rune) (interface{}, error) {
bu = 0 bu = 0
} }
default: default:
return nil, errors.New("Can't apply the operator to the values") return nil, errors.New("can't apply the operator to the values")
} }
case reflect.Float32, reflect.Float64: case reflect.Float32, reflect.Float64:
af = av.Float() af = av.Float()
@ -58,7 +58,7 @@ func DoArithmetic(a, b interface{}, op rune) (interface{}, error) {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
bf = float64(bv.Uint()) // may overflow bf = float64(bv.Uint()) // may overflow
default: default:
return nil, errors.New("Can't apply the operator to the values") return nil, errors.New("can't apply the operator to the values")
} }
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
au = av.Uint() au = av.Uint()
@ -79,7 +79,7 @@ func DoArithmetic(a, b interface{}, op rune) (interface{}, error) {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
bu = bv.Uint() bu = bv.Uint()
default: default:
return nil, errors.New("Can't apply the operator to the values") return nil, errors.New("can't apply the operator to the values")
} }
case reflect.String: case reflect.String:
as := av.String() as := av.String()
@ -87,9 +87,9 @@ func DoArithmetic(a, b interface{}, op rune) (interface{}, error) {
bs := bv.String() bs := bv.String()
return as + bs, nil return as + bs, nil
} }
return nil, errors.New("Can't apply the operator to the values") return nil, errors.New("can't apply the operator to the values")
default: default:
return nil, errors.New("Can't apply the operator to the values") return nil, errors.New("can't apply the operator to the values")
} }
switch op { switch op {
@ -128,8 +128,8 @@ func DoArithmetic(a, b interface{}, op rune) (interface{}, error) {
} else if bu != 0 { } else if bu != 0 {
return au / bu, nil return au / bu, nil
} }
return nil, errors.New("Can't divide the value by 0") return nil, errors.New("can't divide the value by 0")
default: default:
return nil, errors.New("There is no such an operation") return nil, errors.New("there is no such an operation")
} }
} }

View file

@ -142,6 +142,9 @@ func newContentFromDir(
} }
out, err := targetFs.Create(targetFilename) out, err := targetFs.Create(targetFilename)
if err != nil {
return err
}
_, err = io.Copy(out, in) _, err = io.Copy(out, in)
if err != nil { if err != nil {

View file

@ -147,11 +147,9 @@ func newBlackfriday(config map[string]interface{}) *BlackFriday {
siteConfig[k] = v siteConfig[k] = v
} }
if config != nil {
for k, v := range config { for k, v := range config {
siteConfig[k] = v siteConfig[k] = v
} }
}
combinedConfig := &BlackFriday{} combinedConfig := &BlackFriday{}
if err := mapstructure.Decode(siteConfig, combinedConfig); err != nil { if err := mapstructure.Decode(siteConfig, combinedConfig); err != nil {
@ -755,7 +753,7 @@ func externallyRenderContent(ctx *RenderingContext, path string, args []string)
err := cmd.Run() err := cmd.Run()
// Most external helpers exit w/ non-zero exit code only if severe, i.e. // Most external helpers exit w/ non-zero exit code only if severe, i.e.
// halting errors occurred. -> log stderr output regardless of state of err // halting errors occurred. -> log stderr output regardless of state of err
for _, item := range strings.Split(string(cmderr.Bytes()), "\n") { for _, item := range strings.Split(cmderr.String(), "\n") {
item := strings.TrimSpace(item) item := strings.TrimSpace(item)
if item != "" { if item != "" {
jww.ERROR.Printf("%s: %s", ctx.DocumentName, item) jww.ERROR.Printf("%s: %s", ctx.DocumentName, item)

View file

@ -80,7 +80,7 @@ func BenchmarkEmojiKyokomiFprint(b *testing.B) {
defer bufferpool.PutBuffer(buff) defer bufferpool.PutBuffer(buff)
emoji.Fprint(buff, string(in)) emoji.Fprint(buff, string(in))
bc := make([]byte, buff.Len(), buff.Len()) bc := make([]byte, buff.Len())
copy(bc, buff.Bytes()) copy(bc, buff.Bytes())
return bc return bc
} }

View file

@ -57,7 +57,7 @@ func FindAvailablePort() (*net.TCPAddr, error) {
if a, ok := addr.(*net.TCPAddr); ok { if a, ok := addr.(*net.TCPAddr); ok {
return a, nil return a, nil
} }
return nil, fmt.Errorf("Unable to obtain a valid tcp port. %v", addr) return nil, fmt.Errorf("unable to obtain a valid tcp port: %v", addr)
} }
return nil, err return nil, err
} }
@ -128,7 +128,7 @@ func ReaderToBytes(lines io.Reader) []byte {
b.ReadFrom(lines) b.ReadFrom(lines)
bc := make([]byte, b.Len(), b.Len()) bc := make([]byte, b.Len())
copy(bc, b.Bytes()) copy(bc, b.Bytes())
return bc return bc
} }
@ -417,10 +417,8 @@ func NormalizeHugoFlags(f *pflag.FlagSet, name string) pflag.NormalizedName {
switch name { switch name {
case "baseUrl": case "baseUrl":
name = "baseURL" name = "baseURL"
break
case "uglyUrls": case "uglyUrls":
name = "uglyURLs" name = "uglyURLs"
break
} }
return pflag.NormalizedName(name) return pflag.NormalizedName(name)
} }

View file

@ -29,6 +29,7 @@ import (
"github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/common/hugio"
_errors "github.com/pkg/errors" _errors "github.com/pkg/errors"
"github.com/spf13/afero" "github.com/spf13/afero"
"golang.org/x/text/runes"
"golang.org/x/text/transform" "golang.org/x/text/transform"
"golang.org/x/text/unicode/norm" "golang.org/x/text/unicode/norm"
) )
@ -155,7 +156,7 @@ func (p *PathSpec) UnicodeSanitize(s string) string {
if p.RemovePathAccents { if p.RemovePathAccents {
// remove accents - see https://blog.golang.org/normalization // remove accents - see https://blog.golang.org/normalization
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC) t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
result, _, _ = transform.String(t, string(target)) result, _, _ = transform.String(t, string(target))
} else { } else {
result = string(target) result = string(target)
@ -164,10 +165,6 @@ func (p *PathSpec) UnicodeSanitize(s string) string {
return result return result
} }
func isMn(r rune) bool {
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
}
// ReplaceExtension takes a path and an extension, strips the old extension // ReplaceExtension takes a path and an extension, strips the old extension
// and returns the path with the new extension. // and returns the path with the new extension.
func ReplaceExtension(path string, newExt string) string { func ReplaceExtension(path string, newExt string) string {
@ -208,7 +205,7 @@ func makePathRelative(inPath string, possibleDirectories ...string) (string, err
return strings.TrimPrefix(inPath, currentPath), nil return strings.TrimPrefix(inPath, currentPath), nil
} }
} }
return inPath, errors.New("Can't extract relative path, unknown prefix") return inPath, errors.New("can't extract relative path, unknown prefix")
} }
// Should be good enough for Hugo. // Should be good enough for Hugo.
@ -403,15 +400,13 @@ func ExtractRootPaths(paths []string) []string {
} }
var numInPathRe = regexp.MustCompile("\\.(\\d+)\\.")
// FindCWD returns the current working directory from where the Hugo // FindCWD returns the current working directory from where the Hugo
// executable is run. // executable is run.
func FindCWD() (string, error) { func FindCWD() (string, error) {
serverFile, err := filepath.Abs(os.Args[0]) serverFile, err := filepath.Abs(os.Args[0])
if err != nil { if err != nil {
return "", fmt.Errorf("Can't get absolute path for executable: %v", err) return "", fmt.Errorf("can't get absolute path for executable: %v", err)
} }
path := filepath.Dir(serverFile) path := filepath.Dir(serverFile)
@ -437,7 +432,7 @@ func SymbolicWalk(fs afero.Fs, root string, walker filepath.WalkFunc) error {
// Sanity check // Sanity check
if root != "" && len(root) < 4 { if root != "" && len(root) < 4 {
return errors.New("Path is too short") return errors.New("path is too short")
} }
// Handle the root first // Handle the root first
@ -448,7 +443,7 @@ func SymbolicWalk(fs afero.Fs, root string, walker filepath.WalkFunc) error {
} }
if !fileInfo.IsDir() { if !fileInfo.IsDir() {
return fmt.Errorf("Cannot walk regular file %s", root) return fmt.Errorf("cannot walk regular file %s", root)
} }
if err := walker(realPath, fileInfo, err); err != nil && err != filepath.SkipDir { if err := walker(realPath, fileInfo, err); err != nil && err != filepath.SkipDir {

View file

@ -171,7 +171,7 @@ func TestGetRealPath(t *testing.T) {
t.Skip("Skip TestGetRealPath as os.Symlink needs administrator rights on Windows") t.Skip("Skip TestGetRealPath as os.Symlink needs administrator rights on Windows")
} }
d1, err := ioutil.TempDir("", "d1") d1, _ := ioutil.TempDir("", "d1")
defer os.Remove(d1) defer os.Remove(d1)
fs := afero.NewOsFs() fs := afero.NewOsFs()
@ -418,6 +418,7 @@ func createNonZeroSizedFileInTempDir() (*os.File, error) {
f, err := createZeroSizedFileInTempDir() f, err := createZeroSizedFileInTempDir()
if err != nil { if err != nil {
// no file ?? // no file ??
return nil, err
} }
byteString := []byte("byteString") byteString := []byte("byteString")
err = ioutil.WriteFile(f.Name(), byteString, 0644) err = ioutil.WriteFile(f.Name(), byteString, 0644)
@ -430,10 +431,7 @@ func createNonZeroSizedFileInTempDir() (*os.File, error) {
} }
func deleteFileInTempDir(f *os.File) { func deleteFileInTempDir(f *os.File) {
err := os.Remove(f.Name()) _ = os.Remove(f.Name())
if err != nil {
// now what?
}
} }
func createEmptyTempDir() (string, error) { func createEmptyTempDir() (string, error) {
@ -449,7 +447,7 @@ func createEmptyTempDir() (string, error) {
func createTempDirWithZeroLengthFiles() (string, error) { func createTempDirWithZeroLengthFiles() (string, error) {
d, dirErr := createEmptyTempDir() d, dirErr := createEmptyTempDir()
if dirErr != nil { if dirErr != nil {
//now what? return "", dirErr
} }
filePrefix := "_path_test_" filePrefix := "_path_test_"
_, fileErr := ioutil.TempFile(d, filePrefix) // dir is os.TempDir() _, fileErr := ioutil.TempFile(d, filePrefix) // dir is os.TempDir()
@ -467,7 +465,7 @@ func createTempDirWithZeroLengthFiles() (string, error) {
func createTempDirWithNonZeroLengthFiles() (string, error) { func createTempDirWithNonZeroLengthFiles() (string, error) {
d, dirErr := createEmptyTempDir() d, dirErr := createEmptyTempDir()
if dirErr != nil { if dirErr != nil {
//now what? return "", dirErr
} }
filePrefix := "_path_test_" filePrefix := "_path_test_"
f, fileErr := ioutil.TempFile(d, filePrefix) // dir is os.TempDir() f, fileErr := ioutil.TempFile(d, filePrefix) // dir is os.TempDir()
@ -494,10 +492,7 @@ func createTempDirWithNonZeroLengthFiles() (string, error) {
} }
func deleteTempDir(d string) { func deleteTempDir(d string) {
err := os.RemoveAll(d) _ = os.RemoveAll(d)
if err != nil {
// now what?
}
} }
func TestExists(t *testing.T) { func TestExists(t *testing.T) {

View file

@ -153,7 +153,7 @@ func (h highlighters) pygmentsHighlight(code, lang, optsStr string) (string, err
return code, err return code, err
} }
str := string(normalizeExternalHelperLineFeeds([]byte(out.String()))) str := string(normalizeExternalHelperLineFeeds(out.Bytes()))
str = h.injectCodeTag(str, lang) str = h.injectCodeTag(str, lang)
@ -235,11 +235,9 @@ func parseOptions(defaults map[string]string, in string) (map[string]string, err
in = strings.Trim(in, " ") in = strings.Trim(in, " ")
opts := make(map[string]string) opts := make(map[string]string)
if defaults != nil {
for k, v := range defaults { for k, v := range defaults {
opts[k] = v opts[k] = v
} }
}
if in == "" { if in == "" {
return opts, nil return opts, nil

View file

@ -142,7 +142,7 @@ func MakePermalink(host, plink string) *url.URL {
} }
if p.Host != "" { if p.Host != "" {
panic(fmt.Errorf("Can't make permalink from absolute link %q", plink)) panic(fmt.Errorf("can't make permalink from absolute link %q", plink))
} }
base.Path = path.Join(base.Path, p.Path) base.Path = path.Join(base.Path, p.Path)

View file

@ -70,6 +70,7 @@ func TestCompositeLanguagFsTest(t *testing.T) {
assert.NoError(err) assert.NoError(err)
defer f.Close() defer f.Close()
files, err := f.Readdir(-1) files, err := f.Readdir(-1)
assert.NoError(err)
assert.Equal(4, len(files)) assert.Equal(4, len(files))
expected := map[string]bool{ expected := map[string]bool{
filepath.FromSlash("/content/en/f1.txt"): true, filepath.FromSlash("/content/en/f1.txt"): true,

View file

@ -17,7 +17,6 @@ package filesystems
import ( import (
"errors" "errors"
"io"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -759,22 +758,3 @@ func removeDuplicatesKeepRight(in []string) []string {
return out return out
} }
func printFs(fs afero.Fs, path string, w io.Writer) {
if fs == nil {
return
}
afero.Walk(fs, path, func(path string, info os.FileInfo, err error) error {
if info != nil && !info.IsDir() {
s := path
if lang, ok := info.(hugofs.LanguageAnnouncer); ok {
s = s + "\tLANG: " + lang.Lang()
}
if fp, ok := info.(hugofs.FilePather); ok {
s = s + "\tRF: " + fp.Filename() + "\tBP: " + fp.BaseDir()
}
fmt.Fprintln(w, " ", s)
}
return nil
})
}

View file

@ -20,12 +20,10 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"runtime"
"sort" "sort"
"strings" "strings"
"github.com/bep/gitmap" "github.com/bep/gitmap"
"github.com/spf13/cast"
"github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/helpers"
@ -831,19 +829,6 @@ func (ps pageStatePages) findPagePosByFilnamePrefix(prefix string) int {
return currPos return currPos
} }
func content(c resource.ContentProvider) string {
cc, err := c.Content()
if err != nil {
panic(err)
}
ccs, err := cast.ToStringE(cc)
if err != nil {
panic(err)
}
return ccs
}
func (s *Site) sectionsFromFile(fi source.File) []string { func (s *Site) sectionsFromFile(fi source.File) []string {
dirname := fi.Dir() dirname := fi.Dir()
dirname = strings.Trim(dirname, helpers.FilePathSeparator) dirname = strings.Trim(dirname, helpers.FilePathSeparator)
@ -861,9 +846,3 @@ func (s *Site) sectionsFromFile(fi source.File) []string {
return parts return parts
} }
func printStackTrace(length int) string {
trace := make([]byte, length)
runtime.Stack(trace, true)
return string(trace)
}

View file

@ -62,7 +62,7 @@ func (b BaseURL) WithProtocol(protocol string) (string, error) {
if isFullProtocol && u.Opaque != "" { if isFullProtocol && u.Opaque != "" {
u.Opaque = "//" + u.Opaque u.Opaque = "//" + u.Opaque
} else if isOpaqueProtocol && u.Opaque == "" { } else if isOpaqueProtocol && u.Opaque == "" {
return "", fmt.Errorf("Cannot determine BaseURL for protocol %q", protocol) return "", fmt.Errorf("cannot determine BaseURL for protocol %q", protocol)
} }
return u.String(), nil return u.String(), nil

View file

@ -67,9 +67,6 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
// used to indicate if run as a test.
var testMode bool
// Site contains all the information relevant for constructing a static // Site contains all the information relevant for constructing a static
// site. The basic flow of information is as follows: // site. The basic flow of information is as follows:
// //

View file

@ -35,10 +35,6 @@ const (
templateWithURLAbs = "<a href=\"/foobar.jpg\">Going</a>" templateWithURLAbs = "<a href=\"/foobar.jpg\">Going</a>"
) )
func init() {
testMode = true
}
func TestRenderWithInvalidTemplate(t *testing.T) { func TestRenderWithInvalidTemplate(t *testing.T) {
t.Parallel() t.Parallel()
cfg, fs := newTestCfg() cfg, fs := newTestCfg()

View file

@ -20,6 +20,7 @@ import (
"github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/resources/page"
"github.com/sanity-io/litter" "github.com/sanity-io/litter"
"github.com/spf13/afero" "github.com/spf13/afero"
"github.com/spf13/cast"
"github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/tpl" "github.com/gohugoio/hugo/tpl"
@ -27,6 +28,8 @@ import (
"os" "os"
"github.com/gohugoio/hugo/resources/resource"
"github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/hugofs"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -672,6 +675,19 @@ func getPage(in page.Page, ref string) page.Page {
return p return p
} }
func content(c resource.ContentProvider) string {
cc, err := c.Content()
if err != nil {
panic(err)
}
ccs, err := cast.ToStringE(cc)
if err != nil {
panic(err)
}
return ccs
}
func dumpPages(pages ...page.Page) { func dumpPages(pages ...page.Page) {
fmt.Println("---------") fmt.Println("---------")
for i, p := range pages { for i, p := range pages {
@ -726,11 +742,3 @@ func parallel(t *testing.T) {
t.Parallel() t.Parallel()
} }
} }
// Useful to debug nilpointers/panics in templates.
// Put "defer recoverStack()" in top of the failing function.
func recoverStack() {
if r := recover(); r != nil {
fmt.Println(printStackTrace(1000))
}
}

View file

@ -80,7 +80,7 @@ func New(mediaTypes media.Types, outputFormats output.Formats) Client {
addMinifier(m, mediaTypes, "css", cssMin) addMinifier(m, mediaTypes, "css", cssMin)
addMinifierFunc(m, mediaTypes, "js", js.Minify) addMinifierFunc(m, mediaTypes, "js", js.Minify)
m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify) m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-|ld\\+)?json$"), json.Minify) m.AddFuncRegexp(regexp.MustCompile(`^(application|text)/(x-|ld\+)?json$`), json.Minify)
addMinifierFunc(m, mediaTypes, "json", json.Minify) addMinifierFunc(m, mediaTypes, "json", json.Minify)
addMinifierFunc(m, mediaTypes, "svg", svg.Minify) addMinifierFunc(m, mediaTypes, "svg", svg.Minify)
addMinifierFunc(m, mediaTypes, "xml", xml.Minify) addMinifierFunc(m, mediaTypes, "xml", xml.Minify)

View file

@ -22,7 +22,7 @@ import (
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
) )
const ( const (
@ -62,7 +62,7 @@ func InterfaceToConfig(in interface{}, format metadecoders.Format, w io.Writer)
return err return err
default: default:
return errors.New("Unsupported Format provided") return errors.New("unsupported Format provided")
} }
} }

View file

@ -442,13 +442,6 @@ func lexMainSection(l *pageLexer) stateFunc {
} }
func (l *pageLexer) posFirstNonWhiteSpace() int {
f := func(c rune) bool {
return !unicode.IsSpace(c)
}
return bytes.IndexFunc(l.input[l.pos:], f)
}
func lexDone(l *pageLexer) stateFunc { func lexDone(l *pageLexer) stateFunc {
// Done! // Done!
@ -477,14 +470,6 @@ func (l *pageLexer) hasPrefix(prefix []byte) bool {
return bytes.HasPrefix(l.input[l.pos:], prefix) return bytes.HasPrefix(l.input[l.pos:], prefix)
} }
func (l *pageLexer) hasPrefixByte(prefix byte) bool {
b := l.input[l.pos:]
if len(b) == 0 {
return false
}
return b[0] == prefix
}
// helper functions // helper functions
// returns the min index >= 0 // returns the min index >= 0

View file

@ -102,7 +102,7 @@ func (r *ReleaseHandler) Run() error {
} }
if exists { if exists {
return fmt.Errorf("Tag %q already exists", tag) return fmt.Errorf("tag %q already exists", tag)
} }
var changeLogFromTag string var changeLogFromTag string

View file

@ -171,21 +171,21 @@ func TestImageTransformConcurrent(t *testing.T) {
for k := 0; k < 2; k++ { for k := 0; k < 2; k++ {
r1, err := img.Resize(fmt.Sprintf("%dx", id-k)) r1, err := img.Resize(fmt.Sprintf("%dx", id-k))
if err != nil { if err != nil {
t.Fatal(err) t.Error(err)
} }
if r1.Width() != id-k { if r1.Width() != id-k {
t.Fatalf("Width: %d:%d", r1.Width(), j) t.Errorf("Width: %d:%d", r1.Width(), j)
} }
r2, err := r1.Resize(fmt.Sprintf("%dx", id-k-1)) r2, err := r1.Resize(fmt.Sprintf("%dx", id-k-1))
if err != nil { if err != nil {
t.Fatal(err) t.Error(err)
} }
_, err = r2.decodeSource() _, err = r2.decodeSource()
if err != nil { if err != nil {
t.Fatal("Err decode:", err) t.Error("Err decode:", err)
} }
img = r1 img = r1

View file

@ -14,8 +14,6 @@
package source package source
import ( import (
"fmt"
"io"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -23,8 +21,6 @@ import (
"github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/common/hugio"
"github.com/spf13/afero"
"github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/helpers"
@ -286,24 +282,3 @@ func (sp *SourceSpec) NewFileInfo(baseDir, filename string, isLeafBundle bool, f
return f return f
} }
func printFs(fs afero.Fs, path string, w io.Writer) {
if fs == nil {
return
}
afero.Walk(fs, path, func(path string, info os.FileInfo, err error) error {
if info != nil && !info.IsDir() {
s := path
if lang, ok := info.(hugofs.LanguageAnnouncer); ok {
s = s + "\t" + lang.Lang()
}
if fp, ok := info.(hugofs.FilePather); ok {
s = s + "\t" + fp.Filename()
}
fmt.Fprintln(w, " ", s)
}
return nil
})
}

View file

@ -32,14 +32,6 @@ func TestEmptySourceFilesystem(t *testing.T) {
} }
} }
type TestPath struct {
filename string
logical string
content string
section string
dir string
}
func TestUnicodeNorm(t *testing.T) { func TestUnicodeNorm(t *testing.T) {
if runtime.GOOS != "darwin" { if runtime.GOOS != "darwin" {
// Normalization code is only for Mac OS, since it is not necessary for other OSes. // Normalization code is only for Mac OS, since it is not necessary for other OSes.

View file

@ -1,28 +0,0 @@
// Copyright 2015 The Hugo Authors. All rights reserved.
//
// 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.
// +build linux darwin !windows
package source
//
// NOTE, any changes here need to be reflected in filesystem_windows_test.go
//
var platformBase = "/base/"
var platformPaths = []TestPath{
{"foobar", "foobar", "aaa", "", ""},
{"b/1file", "1file", "aaa", "b", "b/"},
{"c/d/2file", "2file", "aaa", "c", "c/d/"},
{"/base/e/f/3file", "3file", "aaa", "e", "e/f/"},
{"section/foo.rss", "foo.rss", "aaa", "section", "section/"},
}

View file

@ -1,28 +0,0 @@
// Copyright 2015 The Hugo Authors. All rights reserved.
//
// 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 source
//
// NOTE, any changes here need to be reflected in filesystem_linux_test.go
//
// Note the case of the volume drive. It must be the same in all examples.
var platformBase = "C:\\foo\\"
var platformPaths = []TestPath{
{"foobar", "foobar", "aaa", "", ""},
{"b\\1file", "1file", "aaa", "b", "b\\"},
{"c\\d\\2file", "2file", "aaa", "c", "c\\d\\"},
{"C:\\foo\\e\\f\\3file", "3file", "aaa", "e", "e\\f\\"}, // note volume case is equal to platformBase
{"section\\foo.rss", "foo.rss", "aaa", "section", "section\\"},
}

View file

@ -124,6 +124,10 @@ func (s *SourceSpec) IsRegularSourceFile(filename string) (bool, error) {
if fi.Mode()&os.ModeSymlink == os.ModeSymlink { if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
link, err := filepath.EvalSymlinks(filename) link, err := filepath.EvalSymlinks(filename)
if err != nil {
return false, err
}
fi, err = helpers.LstatIfPossible(s.SourceFs, link) fi, err = helpers.LstatIfPossible(s.SourceFs, link)
if err != nil { if err != nil {
return false, err return false, err

View file

@ -38,7 +38,7 @@ func numberToFloat(v reflect.Value) (float64, error) {
case kind == reflect.Interface: case kind == reflect.Interface:
return numberToFloat(v.Elem()) return numberToFloat(v.Elem())
default: default:
return 0, fmt.Errorf("Invalid kind %s in numberToFloat", kind) return 0, fmt.Errorf("invalid kind %s in numberToFloat", kind)
} }
} }

View file

@ -58,18 +58,18 @@ func (ns *Namespace) GetCSV(sep string, urlParts ...string) (d [][]string, err e
url := strings.Join(urlParts, "") url := strings.Join(urlParts, "")
cache := ns.cacheGetCSV cache := ns.cacheGetCSV
unmarshal := func(b []byte) (error, bool) { unmarshal := func(b []byte) (bool, error) {
if !bytes.Contains(b, []byte(sep)) { if !bytes.Contains(b, []byte(sep)) {
return _errors.Errorf("cannot find separator %s in CSV for %s", sep, url), false return false, _errors.Errorf("cannot find separator %s in CSV for %s", sep, url)
} }
if d, err = parseCSV(b, sep); err != nil { if d, err = parseCSV(b, sep); err != nil {
err = _errors.Wrapf(err, "failed to parse CSV file %s", url) err = _errors.Wrapf(err, "failed to parse CSV file %s", url)
return err, true return true, err
} }
return nil, false return false, nil
} }
var req *http.Request var req *http.Request
@ -103,12 +103,12 @@ func (ns *Namespace) GetJSON(urlParts ...string) (interface{}, error) {
return nil, _errors.Wrapf(err, "Failed to create request for getJSON resource %s", url) return nil, _errors.Wrapf(err, "Failed to create request for getJSON resource %s", url)
} }
unmarshal := func(b []byte) (error, bool) { unmarshal := func(b []byte) (bool, error) {
err := json.Unmarshal(b, &v) err := json.Unmarshal(b, &v)
if err != nil { if err != nil {
return err, true return true, err
} }
return nil, false return false, nil
} }
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")

View file

@ -34,7 +34,7 @@ var (
) )
// getRemote loads the content of a remote file. This method is thread safe. // getRemote loads the content of a remote file. This method is thread safe.
func (ns *Namespace) getRemote(cache *filecache.Cache, unmarshal func([]byte) (error, bool), req *http.Request) error { func (ns *Namespace) getRemote(cache *filecache.Cache, unmarshal func([]byte) (bool, error), req *http.Request) error {
url := req.URL.String() url := req.URL.String()
id := helpers.MD5String(url) id := helpers.MD5String(url)
var handled bool var handled bool
@ -63,7 +63,7 @@ func (ns *Namespace) getRemote(cache *filecache.Cache, unmarshal func([]byte) (e
} }
res.Body.Close() res.Body.Close()
err, retry = unmarshal(b) retry, err = unmarshal(b)
if err == nil { if err == nil {
// Return it so it can be cached. // Return it so it can be cached.
@ -85,7 +85,7 @@ func (ns *Namespace) getRemote(cache *filecache.Cache, unmarshal func([]byte) (e
if !handled { if !handled {
// This is cached content and should be correct. // This is cached content and should be correct.
err, _ = unmarshal(b) _, err = unmarshal(b)
} }
return err return err
@ -104,14 +104,14 @@ func getLocal(url string, fs afero.Fs, cfg config.Provider) ([]byte, error) {
// getResource loads the content of a local or remote file and returns its content and the // getResource loads the content of a local or remote file and returns its content and the
// cache ID used, if relevant. // cache ID used, if relevant.
func (ns *Namespace) getResource(cache *filecache.Cache, unmarshal func(b []byte) (error, bool), req *http.Request) error { func (ns *Namespace) getResource(cache *filecache.Cache, unmarshal func(b []byte) (bool, error), req *http.Request) error {
switch req.URL.Scheme { switch req.URL.Scheme {
case "": case "":
b, err := getLocal(req.URL.String(), ns.deps.Fs.Source, ns.deps.Cfg) b, err := getLocal(req.URL.String(), ns.deps.Fs.Source, ns.deps.Cfg)
if err != nil { if err != nil {
return err return err
} }
err, _ = unmarshal(b) _, err = unmarshal(b)
return err return err
default: default:
return ns.getRemote(cache, unmarshal, req) return ns.getRemote(cache, unmarshal, req)

View file

@ -114,9 +114,9 @@ func TestScpGetRemote(t *testing.T) {
ns.client = cl ns.client = cl
var c []byte var c []byte
f := func(b []byte) (error, bool) { f := func(b []byte) (bool, error) {
c = b c = b
return nil, false return false, nil
} }
err = ns.getRemote(cache, f, req) err = ns.getRemote(cache, f, req)
@ -158,15 +158,15 @@ func TestScpGetRemoteParallel(t *testing.T) {
defer wg.Done() defer wg.Done()
for j := 0; j < 10; j++ { for j := 0; j < 10; j++ {
var c []byte var c []byte
f := func(b []byte) (error, bool) { f := func(b []byte) (bool, error) {
c = b c = b
return nil, false return false, nil
} }
err := ns.getRemote(ns.cacheGetJSON, f, req) err := ns.getRemote(ns.cacheGetJSON, f, req)
assert.NoError(t, err) assert.NoError(t, err)
if string(content) != string(c) { if string(content) != string(c) {
t.Fatalf("expected\n%q\ngot\n%q", content, c) t.Errorf("expected\n%q\ngot\n%q", content, c)
} }
time.Sleep(23 * time.Millisecond) time.Sleep(23 * time.Millisecond)

View file

@ -78,11 +78,11 @@ func (ns *Namespace) Mod(a, b interface{}) (int64, error) {
bi, errb := cast.ToInt64E(b) bi, errb := cast.ToInt64E(b)
if erra != nil || errb != nil { if erra != nil || errb != nil {
return 0, errors.New("Modulo operator can't be used with non integer value") return 0, errors.New("modulo operator can't be used with non integer value")
} }
if bi == 0 { if bi == 0 {
return 0, errors.New("The number can't be divided by zero at modulo operation") return 0, errors.New("the number can't be divided by zero at modulo operation")
} }
return ai % bi, nil return ai % bi, nil

View file

@ -73,7 +73,7 @@ func readFile(fs afero.Fs, filename string) (string, error) {
if info, err := fs.Stat(filename); err == nil { if info, err := fs.Stat(filename); err == nil {
if info.Size() > 1000000 { if info.Size() > 1000000 {
return "", fmt.Errorf("File %q is too big", filename) return "", fmt.Errorf("file %q is too big", filename)
} }
} else { } else {
return "", err return "", err
@ -108,7 +108,7 @@ func (ns *Namespace) ReadDir(i interface{}) ([]_os.FileInfo, error) {
list, err := afero.ReadDir(ns.deps.Fs.WorkingDir, path) list, err := afero.ReadDir(ns.deps.Fs.WorkingDir, path)
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to read Directory %s with error message %s", path, err) return nil, fmt.Errorf("failed to read directory %q: %s", path, err)
} }
return list, nil return list, nil

View file

@ -107,7 +107,7 @@ func (ns *Namespace) Include(name string, contextList ...interface{}) (interface
} }
return "", fmt.Errorf("Partial %q not found", name) return "", fmt.Errorf("partial %q not found", name)
} }
// IncludeCached executes and caches partial templates. An optional variant // IncludeCached executes and caches partial templates. An optional variant

View file

@ -152,7 +152,7 @@ func (t *TemplateAdapter) Execute(w io.Writer, data interface{}) (execErr error)
// Panics in templates are a little bit too common (nil pointers etc.) // Panics in templates are a little bit too common (nil pointers etc.)
// See https://github.com/gohugoio/hugo/issues/5327 // See https://github.com/gohugoio/hugo/issues/5327
if r := recover(); r != nil { if r := recover(); r != nil {
execErr = t.addFileContext(t.Name(), fmt.Errorf(`panic in Execute: %s. See "https://github.com/gohugoio/hugo/issues/5327" for the reason why we cannot provide a better error message for this.`, r)) execErr = t.addFileContext(t.Name(), fmt.Errorf(`panic in Execute: %s. See "https://github.com/gohugoio/hugo/issues/5327" for the reason why we cannot provide a better error message for this`, r))
} }
}() }()
@ -174,7 +174,7 @@ func (t *TemplateAdapter) TemplateInfo() Info {
// The identifiers may be truncated in the log, e.g. // The identifiers may be truncated in the log, e.g.
// "executing "main" at <$scaled.SRelPermalin...>: can't evaluate field SRelPermalink in type *resource.Image" // "executing "main" at <$scaled.SRelPermalin...>: can't evaluate field SRelPermalink in type *resource.Image"
var identifiersRe = regexp.MustCompile("at \\<(.*?)(\\.{3})?\\>:") var identifiersRe = regexp.MustCompile(`at \<(.*?)(\.{3})?\>:`)
func (t *TemplateAdapter) extractIdentifiers(line string) []string { func (t *TemplateAdapter) extractIdentifiers(line string) []string {
m := identifiersRe.FindAllStringSubmatch(line, -1) m := identifiersRe.FindAllStringSubmatch(line, -1)

View file

@ -39,6 +39,9 @@ func main() {
var nameValues []string var nameValues []string
err = filepath.Walk(temlatePath, func(path string, info os.FileInfo, err error) error { err = filepath.Walk(temlatePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() { if info.IsDir() {
return nil return nil

View file

@ -41,6 +41,9 @@ func (ns *Namespace) Remarshal(format string, data interface{}) (string, error)
} }
meta, err := metadecoders.Default.UnmarshalToMap([]byte(from), fromFormat) meta, err := metadecoders.Default.UnmarshalToMap([]byte(from), fromFormat)
if err != nil {
return "", err
}
var result bytes.Buffer var result bytes.Buffer
if err := parser.InterfaceToConfig(meta, mark, &result); err != nil { if err := parser.InterfaceToConfig(meta, mark, &result); err != nil {

View file

@ -58,8 +58,8 @@ func TestChaingMultipleTransformers(t *testing.T) {
expected := "Test: f4r f3r f1r f2r f1r The End." expected := "Test: f4r f3r f1r f2r f1r The End."
if string(out.Bytes()) != expected { if out.String() != expected {
t.Errorf("Expected %s got %s", expected, string(out.Bytes())) t.Errorf("Expected %s got %s", expected, out.String())
} }
} }

View file

@ -35,7 +35,7 @@ func doTestLiveReloadInject(t *testing.T, bodyEndTag string) {
tr.Apply(out, in) tr.Apply(out, in)
expected := fmt.Sprintf(`<script data-no-instant>document.write('<script src="/livereload.js?port=1313&mindelay=10"></' + 'script>')</script>%s`, bodyEndTag) expected := fmt.Sprintf(`<script data-no-instant>document.write('<script src="/livereload.js?port=1313&mindelay=10"></' + 'script>')</script>%s`, bodyEndTag)
if string(out.Bytes()) != expected { if out.String() != expected {
t.Errorf("Expected %s got %s", expected, string(out.Bytes())) t.Errorf("Expected %s got %s", expected, out.String())
} }
} }

View file

@ -37,8 +37,6 @@ type absurllexer struct {
quotes [][]byte quotes [][]byte
} }
type stateFunc func(*absurllexer) stateFunc
type prefix struct { type prefix struct {
disabled bool disabled bool
b []byte b []byte
@ -54,11 +52,6 @@ func newPrefixState() []*prefix {
} }
} }
type absURLMatcher struct {
match []byte
quote []byte
}
func (l *absurllexer) emit() { func (l *absurllexer) emit() {
l.w.Write(l.content[l.start:l.pos]) l.w.Write(l.content[l.start:l.pos])
l.start = l.pos l.start = l.pos

View file

@ -41,7 +41,6 @@ const (
replace1 = "No replacements." replace1 = "No replacements."
replace2 = "ᚠᛇᚻ ᛒᛦᚦ ᚠᚱᚩᚠᚢᚱ\nᚠᚱᚪ ᚷᛖᚻᚹᛦᛚᚳᚢᛗ" replace2 = "ᚠᛇᚻ ᛒᛦᚦ ᚠᚱᚩᚠᚢᚱ\nᚠᚱᚪ ᚷᛖᚻᚹᛦᛚᚳᚢᛗ"
replace3 = `End of file: src="/` replace3 = `End of file: src="/`
replace4 = `End of file: srcset="/`
replace5 = `Srcsett with no closing quote: srcset="/img/small.jpg do be do be do.` replace5 = `Srcsett with no closing quote: srcset="/img/small.jpg do be do be do.`
// Issue: 816, schemaless links combined with others // Issue: 816, schemaless links combined with others