hugo_test.go (4932B)
1 // Copyright 2019 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package commands 15 16 import ( 17 "bytes" 18 "fmt" 19 "math/rand" 20 "path/filepath" 21 "strings" 22 "testing" 23 24 "github.com/bep/clock" 25 qt "github.com/frankban/quicktest" 26 "github.com/gohugoio/hugo/common/htime" 27 "github.com/gohugoio/hugo/hugofs" 28 "github.com/spf13/afero" 29 "golang.org/x/tools/txtar" 30 ) 31 32 // Issue #5662 33 func TestHugoWithContentDirOverride(t *testing.T) { 34 t.Parallel() 35 c := qt.New(t) 36 37 files := ` 38 -- config.toml -- 39 baseURL = "https://example.org" 40 title = "Hugo Commands" 41 -- mycontent/p1.md -- 42 --- 43 title: "P1" 44 --- 45 -- layouts/_default/single.html -- 46 Page: {{ .Title }}| 47 48 ` 49 s := newTestHugoCmdBuilder(c, files, []string{"-c", "mycontent"}).Build() 50 s.AssertFileContent("public/p1/index.html", `Page: P1|`) 51 52 } 53 54 // Issue #9794 55 func TestHugoStaticFilesMultipleStaticAndManyFolders(t *testing.T) { 56 t.Parallel() 57 c := qt.New(t) 58 59 files := ` 60 -- config.toml -- 61 baseURL = "https://example.org" 62 theme = "mytheme" 63 -- layouts/index.html -- 64 Home. 65 66 ` 67 const ( 68 numDirs = 33 69 numFilesMax = 12 70 ) 71 72 r := rand.New(rand.NewSource(32)) 73 74 for i := 0; i < numDirs; i++ { 75 for j := 0; j < r.Intn(numFilesMax); j++ { 76 if j%3 == 0 { 77 files += fmt.Sprintf("-- themes/mytheme/static/d%d/f%d.txt --\nHellot%d-%d\n", i, j, i, j) 78 files += fmt.Sprintf("-- themes/mytheme/static/d%d/ft%d.txt --\nHellot%d-%d\n", i, j, i, j) 79 } 80 files += fmt.Sprintf("-- static/d%d/f%d.txt --\nHello%d-%d\n", i, j, i, j) 81 } 82 } 83 84 r = rand.New(rand.NewSource(32)) 85 86 s := newTestHugoCmdBuilder(c, files, []string{"-c", "mycontent"}).Build() 87 for i := 0; i < numDirs; i++ { 88 for j := 0; j < r.Intn(numFilesMax); j++ { 89 if j%3 == 0 { 90 if j%3 == 0 { 91 s.AssertFileContent(fmt.Sprintf("public/d%d/ft%d.txt", i, j), fmt.Sprintf("Hellot%d-%d", i, j)) 92 } 93 s.AssertFileContent(fmt.Sprintf("public/d%d/f%d.txt", i, j), fmt.Sprintf("Hello%d-%d", i, j)) 94 } 95 } 96 } 97 98 } 99 100 // Issue #8787 101 func TestHugoListCommandsWithClockFlag(t *testing.T) { 102 t.Cleanup(func() { htime.Clock = clock.System() }) 103 104 c := qt.New(t) 105 106 files := ` 107 -- config.toml -- 108 baseURL = "https://example.org" 109 title = "Hugo Commands" 110 timeZone = "UTC" 111 -- content/past.md -- 112 --- 113 title: "Past" 114 date: 2000-11-06 115 --- 116 -- content/future.md -- 117 --- 118 title: "Future" 119 date: 2200-11-06 120 --- 121 -- layouts/_default/single.html -- 122 Page: {{ .Title }}| 123 124 ` 125 s := newTestHugoCmdBuilder(c, files, []string{"list", "future"}) 126 s.captureOut = true 127 s.Build() 128 p := filepath.Join("content", "future.md") 129 s.AssertStdout(p + ",2200-11-06T00:00:00Z") 130 131 s = newTestHugoCmdBuilder(c, files, []string{"list", "future", "--clock", "2300-11-06"}).Build() 132 s.AssertStdout("") 133 } 134 135 type testHugoCmdBuilder struct { 136 *qt.C 137 138 fs afero.Fs 139 dir string 140 files string 141 args []string 142 143 captureOut bool 144 out string 145 } 146 147 func newTestHugoCmdBuilder(c *qt.C, files string, args []string) *testHugoCmdBuilder { 148 s := &testHugoCmdBuilder{C: c, files: files, args: args} 149 s.dir = s.TempDir() 150 s.fs = afero.NewBasePathFs(hugofs.Os, s.dir) 151 152 return s 153 } 154 155 func (s *testHugoCmdBuilder) Build() *testHugoCmdBuilder { 156 data := txtar.Parse([]byte(s.files)) 157 158 for _, f := range data.Files { 159 filename := filepath.Clean(f.Name) 160 data := bytes.TrimSuffix(f.Data, []byte("\n")) 161 s.Assert(s.fs.MkdirAll(filepath.Dir(filename), 0777), qt.IsNil) 162 s.Assert(afero.WriteFile(s.fs, filename, data, 0666), qt.IsNil) 163 } 164 165 hugoCmd := newCommandsBuilder().addAll().build() 166 cmd := hugoCmd.getCommand() 167 args := append(s.args, "-s="+s.dir, "--quiet") 168 cmd.SetArgs(args) 169 170 if s.captureOut { 171 out, err := captureStdout(func() error { 172 _, err := cmd.ExecuteC() 173 return err 174 }) 175 s.Assert(err, qt.IsNil) 176 s.out = out 177 } else { 178 _, err := cmd.ExecuteC() 179 s.Assert(err, qt.IsNil) 180 } 181 182 return s 183 } 184 185 func (s *testHugoCmdBuilder) AssertFileContent(filename string, matches ...string) { 186 s.Helper() 187 data, err := afero.ReadFile(s.fs, filename) 188 s.Assert(err, qt.IsNil) 189 content := strings.TrimSpace(string(data)) 190 for _, m := range matches { 191 lines := strings.Split(m, "\n") 192 for _, match := range lines { 193 match = strings.TrimSpace(match) 194 if match == "" || strings.HasPrefix(match, "#") { 195 continue 196 } 197 s.Assert(content, qt.Contains, match, qt.Commentf(m)) 198 } 199 } 200 } 201 202 func (s *testHugoCmdBuilder) AssertStdout(match string) { 203 s.Helper() 204 content := strings.TrimSpace(s.out) 205 s.Assert(content, qt.Contains, strings.TrimSpace(match)) 206 }