| 1 |
// Copyright 2024 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 resources_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"encoding/base64" |
| 19 |
"fmt" |
| 20 |
"io" |
| 21 |
"path/filepath" |
| 22 |
"strconv" |
| 23 |
"strings" |
| 24 |
"sync" |
| 25 |
"testing" |
| 26 |
|
| 27 |
"github.com/gohugoio/hugo/htesting" |
| 28 |
"github.com/gohugoio/hugo/identity" |
| 29 |
"github.com/gohugoio/hugo/media" |
| 30 |
"github.com/gohugoio/hugo/resources" |
| 31 |
|
| 32 |
"github.com/gohugoio/hugo/common/herrors" |
| 33 |
"github.com/gohugoio/hugo/common/hugio" |
| 34 |
"github.com/gohugoio/hugo/hugofs" |
| 35 |
|
| 36 |
"github.com/gohugoio/hugo/resources/images" |
| 37 |
"github.com/gohugoio/hugo/resources/internal" |
| 38 |
|
| 39 |
"github.com/gohugoio/hugo/helpers" |
| 40 |
|
| 41 |
"github.com/gohugoio/hugo/resources/resource" |
| 42 |
"github.com/spf13/afero" |
| 43 |
|
| 44 |
qt "github.com/frankban/quicktest" |
| 45 |
) |
| 46 |
|
| 47 |
const gopher = `iVBORw0KGgoAAAANSUhEUgAAAEsAAAA8CAAAAAALAhhPAAAFfUlEQVRYw62XeWwUVRzHf2+OPbo9d7tsWyiyaZti6eWGAhISoIGKECEKCAiJJkYTiUgTMYSIosYYBBIUIxoSPIINEBDi2VhwkQrVsj1ESgu9doHWdrul7ba73WNm3vOPtsseM9MdwvvrzTs+8/t95ze/33sI5BqiabU6m9En8oNjduLnAEDLUsQXFF8tQ5oxK3vmnNmDSMtrncks9Hhtt/qeWZapHb1ha3UqYSWVl2ZmpWgaXMXGohQAvmeop3bjTRtv6SgaK/Pb9/bFzUrYslbFAmHPp+3WhAYdr+7GN/YnpN46Opv55VDsJkoEpMrY/vO2BIYQ6LLvm0ThY3MzDzzeSJeeWNyTkgnIE5ePKsvKlcg/0T9QMzXalwXMlj54z4c0rh/mzEfr+FgWEz2w6uk8dkzFAgcARAgNp1ZYef8bH2AgvuStbc2/i6CiWGj98y2tw2l4FAXKkQBIf+exyRnteY83LfEwDQAYCoK+P6bxkZm/0966LxcAAILHB56kgD95PPxltuYcMtFTWw/FKkY/6Opf3GGd9ZF+Qp6mzJxzuRSractOmJrH1u8XTvWFHINNkLQLMR+XHXvfPPHw967raE1xxwtA36IMRfkAAG29/7mLuQcb2WOnsJReZGfpiHsSBX81cvMKywYZHhX5hFPtOqPGWZCXnhWGAu6lX91ElKXSalcLXu3UaOXVay57ZSe5f6Gpx7J2MXAsi7EqSp09b/MirKSyJfnfEEgeDjl8FgDAfvewP03zZ+AJ0m9aFRM8eEHBDRKjfcreDXnZdQuAxXpT2NRJ7xl3UkLBhuVGU16gZiGOgZmrSbRdqkILuL/yYoSXHHkl9KXgqNu3PB8oRg0geC5vFmLjad6mUyTKLmF3OtraWDIfACyXqmephaDABawfpi6tqqBZytfQMqOz6S09iWXhktrRaB8Xz4Yi/8gyABDm5NVe6qq/3VzPrcjELWrebVuyY2T7ar4zQyybUCtsQ5Es1FGaZVrRVQwAgHGW2ZCRZshI5bGQi7HesyE972pOSeMM0dSktlzxRdrlqb3Osa6CCS8IJoQQQgBAbTAa5l5epO34rJszibJI8rxLfGzcp1dRosutGeb2VDNgqYrwTiPNsLxXiPi3dz7LiS1WBRBDBOnqEjyy3aQb+/bLiJzz9dIkscVBBLxMfSEac7kO4Fpkngi0ruNBeSOal+u8jgOuqPz12nryMLCniEjtOOOmpt+KEIqsEdocJjYXwrh9OZqWJQyPCTo67LNS/TdxLAv6R5ZNK9npEjbYdT33gRo4o5oTqR34R+OmaSzDBWsAIPhuRcgyoteNi9gF0KzNYWVItPf2TLoXEg+7isNC7uJkgo1iQWOfRSP9NR11RtbZZ3OMG/VhL6jvx+J1m87+RCfJChAtEBQkSBX2PnSiihc/Twh3j0h7qdYQAoRVsRGmq7HU2QRbaxVGa1D6nIOqaIWRjyRZpHMQKWKpZM5feA+lzC4ZFultV8S6T0mzQGhQohi5I8iw+CsqBSxhFMuwyLgSwbghGb0AiIKkSDmGZVmJSiKihsiyOAUs70UkywooYP0bii9GdH4sfr1UNysd3fUyLLMQN+rsmo3grHl9VNJHbbwxoa47Vw5gupIqrZcjPh9R4Nye3nRDk199V+aetmvVtDRE8/+cbgAAgMIWGb3UA0MGLE9SCbWX670TDy1y98c3D27eppUjsZ6fql3jcd5rUe7+ZIlLNQny3Rd+E5Tct3WVhTM5RBCEdiEK0b6B+/ca2gYU393nFj/n1AygRQxPIUA043M42u85+z2SnssKrPl8Mx76NL3E6eXc3be7OD+H4WHbJkKI8AU8irbITQjZ+0hQcPEgId/Fn/pl9crKH02+5o2b9T/eMx7pKoskYgAAAABJRU5ErkJggg==` |
| 48 |
|
| 49 |
func gopherPNG() io.Reader { return base64.NewDecoder(base64.StdEncoding, strings.NewReader(gopher)) } |
| 50 |
|
| 51 |
func TestTransform(t *testing.T) { |
| 52 |
createTransformer := func(c *qt.C, spec *resources.Spec, filename, content string) resources.Transformer { |
| 53 |
targetPath := identity.CleanString(filename) |
| 54 |
r, err := spec.NewResource(resources.ResourceSourceDescriptor{ |
| 55 |
TargetPath: targetPath, |
| 56 |
OpenReadSeekCloser: hugio.NewOpenReadSeekCloser(hugio.NewReadSeekerNoOpCloserFromString(content)), |
| 57 |
GroupIdentity: identity.StringIdentity(targetPath), |
| 58 |
}) |
| 59 |
c.Assert(err, qt.IsNil) |
| 60 |
c.Assert(r, qt.Not(qt.IsNil), qt.Commentf(filename)) |
| 61 |
return r.(resources.Transformer) |
| 62 |
} |
| 63 |
|
| 64 |
createContentReplacer := func(name, old, new string) resources.ResourceTransformation { |
| 65 |
return &testTransformation{ |
| 66 |
name: name, |
| 67 |
transform: func(ctx *resources.ResourceTransformationCtx) error { |
| 68 |
in := helpers.ReaderToString(ctx.From) |
| 69 |
in = strings.Replace(in, old, new, 1) |
| 70 |
ctx.AddOutPathIdentifier("." + name) |
| 71 |
fmt.Fprint(ctx.To, in) |
| 72 |
return nil |
| 73 |
}, |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Verify that we publish the same file once only. |
| 78 |
assertNoDuplicateWrites := func(c *qt.C, spec *resources.Spec) { |
| 79 |
c.Helper() |
| 80 |
hugofs.WalkFilesystems(spec.Fs.PublishDir, func(fs afero.Fs) bool { |
| 81 |
if dfs, ok := fs.(hugofs.DuplicatesReporter); ok { |
| 82 |
c.Assert(dfs.ReportDuplicates(), qt.Equals, "") |
| 83 |
} |
| 84 |
return false |
| 85 |
}) |
| 86 |
} |
| 87 |
|
| 88 |
assertShouldExist := func(c *qt.C, spec *resources.Spec, filename string, should bool) { |
| 89 |
c.Helper() |
| 90 |
exists, _ := helpers.Exists(filepath.FromSlash(filename), spec.Fs.WorkingDirReadOnly) |
| 91 |
c.Assert(exists, qt.Equals, should) |
| 92 |
} |
| 93 |
|
| 94 |
c := qt.New(t) |
| 95 |
|
| 96 |
c.Run("All values", func(c *qt.C) { |
| 97 |
c.Parallel() |
| 98 |
|
| 99 |
spec := newTestResourceSpec(specDescriptor{c: c}) |
| 100 |
|
| 101 |
transformation := &testTransformation{ |
| 102 |
name: "test", |
| 103 |
transform: func(ctx *resources.ResourceTransformationCtx) error { |
| 104 |
// Content |
| 105 |
in := helpers.ReaderToString(ctx.From) |
| 106 |
in = strings.Replace(in, "blue", "green", 1) |
| 107 |
fmt.Fprint(ctx.To, in) |
| 108 |
|
| 109 |
// Media type |
| 110 |
ctx.OutMediaType = media.Builtin.CSVType |
| 111 |
|
| 112 |
// Change target |
| 113 |
ctx.ReplaceOutPathExtension(".csv") |
| 114 |
|
| 115 |
// Add some data to context |
| 116 |
ctx.Data["mydata"] = "Hugo Rocks!" |
| 117 |
|
| 118 |
return nil |
| 119 |
}, |
| 120 |
} |
| 121 |
|
| 122 |
r := createTransformer(c, spec, "f1.txt", "color is blue") |
| 123 |
|
| 124 |
tr, err := r.Transform(transformation) |
| 125 |
c.Assert(err, qt.IsNil) |
| 126 |
content, err := tr.(resource.ContentProvider).Content(context.Background()) |
| 127 |
c.Assert(err, qt.IsNil) |
| 128 |
|
| 129 |
c.Assert(content, qt.Equals, "color is green") |
| 130 |
c.Assert(tr.MediaType(), eq, media.Builtin.CSVType) |
| 131 |
c.Assert(tr.RelPermalink(), qt.Equals, "/f1.csv") |
| 132 |
assertShouldExist(c, spec, "public/f1.csv", true) |
| 133 |
|
| 134 |
data := tr.Data().(map[string]any) |
| 135 |
c.Assert(data["mydata"], qt.Equals, "Hugo Rocks!") |
| 136 |
|
| 137 |
assertNoDuplicateWrites(c, spec) |
| 138 |
}) |
| 139 |
|
| 140 |
c.Run("Meta only", func(c *qt.C) { |
| 141 |
c.Parallel() |
| 142 |
|
| 143 |
spec := newTestResourceSpec(specDescriptor{c: c}) |
| 144 |
|
| 145 |
transformation := &testTransformation{ |
| 146 |
name: "test", |
| 147 |
transform: func(ctx *resources.ResourceTransformationCtx) error { |
| 148 |
// Change media type only |
| 149 |
ctx.OutMediaType = media.Builtin.CSVType |
| 150 |
ctx.ReplaceOutPathExtension(".csv") |
| 151 |
|
| 152 |
return nil |
| 153 |
}, |
| 154 |
} |
| 155 |
|
| 156 |
r := createTransformer(c, spec, "f1.txt", "color is blue") |
| 157 |
|
| 158 |
tr, err := r.Transform(transformation) |
| 159 |
c.Assert(err, qt.IsNil) |
| 160 |
content, err := tr.(resource.ContentProvider).Content(context.Background()) |
| 161 |
c.Assert(err, qt.IsNil) |
| 162 |
|
| 163 |
c.Assert(content, qt.Equals, "color is blue") |
| 164 |
c.Assert(tr.MediaType(), eq, media.Builtin.CSVType) |
| 165 |
|
| 166 |
// The transformed file should only be published if RelPermalink |
| 167 |
// or Permalink is called. |
| 168 |
n := htesting.Rnd.Intn(3) |
| 169 |
shouldExist := true |
| 170 |
switch n { |
| 171 |
case 0: |
| 172 |
tr.RelPermalink() |
| 173 |
case 1: |
| 174 |
tr.Permalink() |
| 175 |
default: |
| 176 |
shouldExist = false |
| 177 |
} |
| 178 |
|
| 179 |
assertShouldExist(c, spec, "public/f1.csv", shouldExist) |
| 180 |
assertNoDuplicateWrites(c, spec) |
| 181 |
}) |
| 182 |
|
| 183 |
c.Run("Memory-cached transformation", func(c *qt.C) { |
| 184 |
c.Parallel() |
| 185 |
|
| 186 |
spec := newTestResourceSpec(specDescriptor{c: c}) |
| 187 |
|
| 188 |
// Two transformations with same id, different behavior. |
| 189 |
t1 := createContentReplacer("t1", "blue", "green") |
| 190 |
t2 := createContentReplacer("t1", "color", "car") |
| 191 |
|
| 192 |
for i, transformation := range []resources.ResourceTransformation{t1, t2} { |
| 193 |
r := createTransformer(c, spec, "f1.txt", "color is blue") |
| 194 |
tr, _ := r.Transform(transformation) |
| 195 |
content, err := tr.(resource.ContentProvider).Content(context.Background()) |
| 196 |
c.Assert(err, qt.IsNil) |
| 197 |
c.Assert(content, qt.Equals, "color is green", qt.Commentf("i=%d", i)) |
| 198 |
|
| 199 |
assertShouldExist(c, spec, "public/f1.t1.txt", false) |
| 200 |
} |
| 201 |
|
| 202 |
assertNoDuplicateWrites(c, spec) |
| 203 |
}) |
| 204 |
|
| 205 |
c.Run("File-cached transformation", func(c *qt.C) { |
| 206 |
c.Parallel() |
| 207 |
|
| 208 |
fs := afero.NewMemMapFs() |
| 209 |
|
| 210 |
for i := range 2 { |
| 211 |
spec := newTestResourceSpec(specDescriptor{c: c, fs: fs}) |
| 212 |
|
| 213 |
r := createTransformer(c, spec, "f1.txt", "color is blue") |
| 214 |
|
| 215 |
var transformation resources.ResourceTransformation |
| 216 |
|
| 217 |
if i == 0 { |
| 218 |
// There is currently a hardcoded list of transformations that we |
| 219 |
// persist to disk (tocss, postcss). |
| 220 |
transformation = &testTransformation{ |
| 221 |
name: "tocss", |
| 222 |
transform: func(ctx *resources.ResourceTransformationCtx) error { |
| 223 |
in := helpers.ReaderToString(ctx.From) |
| 224 |
in = strings.Replace(in, "blue", "green", 1) |
| 225 |
ctx.AddOutPathIdentifier("." + "cached") |
| 226 |
ctx.OutMediaType = media.Builtin.CSVType |
| 227 |
ctx.Data = map[string]any{ |
| 228 |
"Hugo": "Rocks!", |
| 229 |
} |
| 230 |
fmt.Fprint(ctx.To, in) |
| 231 |
return nil |
| 232 |
}, |
| 233 |
} |
| 234 |
} else { |
| 235 |
// Force read from file cache. |
| 236 |
transformation = &testTransformation{ |
| 237 |
name: "tocss", |
| 238 |
transform: func(ctx *resources.ResourceTransformationCtx) error { |
| 239 |
return herrors.ErrFeatureNotAvailable |
| 240 |
}, |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
msg := qt.Commentf("i=%d", i) |
| 245 |
|
| 246 |
tr, _ := r.Transform(transformation) |
| 247 |
c.Assert(tr.RelPermalink(), qt.Equals, "/f1.cached.txt", msg) |
| 248 |
content, err := tr.(resource.ContentProvider).Content(context.Background()) |
| 249 |
c.Assert(err, qt.IsNil) |
| 250 |
c.Assert(content, qt.Equals, "color is green", msg) |
| 251 |
c.Assert(tr.MediaType(), eq, media.Builtin.CSVType) |
| 252 |
c.Assert(tr.Data(), qt.DeepEquals, map[string]any{ |
| 253 |
"Hugo": "Rocks!", |
| 254 |
}) |
| 255 |
|
| 256 |
assertNoDuplicateWrites(c, spec) |
| 257 |
assertShouldExist(c, spec, "public/f1.cached.txt", true) |
| 258 |
|
| 259 |
} |
| 260 |
}) |
| 261 |
|
| 262 |
c.Run("Access RelPermalink first", func(c *qt.C) { |
| 263 |
c.Parallel() |
| 264 |
|
| 265 |
spec := newTestResourceSpec(specDescriptor{c: c}) |
| 266 |
|
| 267 |
t1 := createContentReplacer("t1", "blue", "green") |
| 268 |
|
| 269 |
r := createTransformer(c, spec, "f1.txt", "color is blue") |
| 270 |
|
| 271 |
tr, _ := r.Transform(t1) |
| 272 |
|
| 273 |
relPermalink := tr.RelPermalink() |
| 274 |
|
| 275 |
content, err := tr.(resource.ContentProvider).Content(context.Background()) |
| 276 |
c.Assert(err, qt.IsNil) |
| 277 |
|
| 278 |
c.Assert(relPermalink, qt.Equals, "/f1.t1.txt") |
| 279 |
c.Assert(content, qt.Equals, "color is green") |
| 280 |
c.Assert(tr.MediaType(), eq, media.Builtin.TextType) |
| 281 |
|
| 282 |
assertNoDuplicateWrites(c, spec) |
| 283 |
assertShouldExist(c, spec, "public/f1.t1.txt", true) |
| 284 |
}) |
| 285 |
|
| 286 |
c.Run("Content two", func(c *qt.C) { |
| 287 |
c.Parallel() |
| 288 |
|
| 289 |
spec := newTestResourceSpec(specDescriptor{c: c}) |
| 290 |
|
| 291 |
t1 := createContentReplacer("t1", "blue", "green") |
| 292 |
t2 := createContentReplacer("t1", "color", "car") |
| 293 |
|
| 294 |
r := createTransformer(c, spec, "f1.txt", "color is blue") |
| 295 |
|
| 296 |
tr, _ := r.Transform(t1, t2) |
| 297 |
content, err := tr.(resource.ContentProvider).Content(context.Background()) |
| 298 |
c.Assert(err, qt.IsNil) |
| 299 |
|
| 300 |
c.Assert(content, qt.Equals, "car is green") |
| 301 |
c.Assert(tr.MediaType(), eq, media.Builtin.TextType) |
| 302 |
|
| 303 |
assertNoDuplicateWrites(c, spec) |
| 304 |
}) |
| 305 |
|
| 306 |
c.Run("Content two chained", func(c *qt.C) { |
| 307 |
c.Parallel() |
| 308 |
|
| 309 |
spec := newTestResourceSpec(specDescriptor{c: c}) |
| 310 |
|
| 311 |
t1 := createContentReplacer("t1", "blue", "green") |
| 312 |
t2 := createContentReplacer("t2", "color", "car") |
| 313 |
|
| 314 |
r := createTransformer(c, spec, "f1.txt", "color is blue") |
| 315 |
|
| 316 |
tr1, err := r.Transform(t1) |
| 317 |
c.Assert(err, qt.IsNil) |
| 318 |
tr2, err := tr1.Transform(t2) |
| 319 |
c.Assert(err, qt.IsNil) |
| 320 |
|
| 321 |
content1, err := tr1.(resource.ContentProvider).Content(context.Background()) |
| 322 |
c.Assert(err, qt.IsNil) |
| 323 |
content2, err := tr2.(resource.ContentProvider).Content(context.Background()) |
| 324 |
c.Assert(err, qt.IsNil) |
| 325 |
|
| 326 |
c.Assert(content1, qt.Equals, "color is green") |
| 327 |
c.Assert(content2, qt.Equals, "car is green") |
| 328 |
|
| 329 |
assertNoDuplicateWrites(c, spec) |
| 330 |
}) |
| 331 |
|
| 332 |
c.Run("Content many", func(c *qt.C) { |
| 333 |
c.Parallel() |
| 334 |
|
| 335 |
spec := newTestResourceSpec(specDescriptor{c: c}) |
| 336 |
|
| 337 |
const count = 26 // A-Z |
| 338 |
|
| 339 |
transformations := make([]resources.ResourceTransformation, count) |
| 340 |
for i := range count { |
| 341 |
transformations[i] = createContentReplacer(fmt.Sprintf("t%d", i), fmt.Sprint(i), string(rune(i+65))) |
| 342 |
} |
| 343 |
|
| 344 |
var countstr strings.Builder |
| 345 |
for i := range count { |
| 346 |
countstr.WriteString(fmt.Sprint(i)) |
| 347 |
} |
| 348 |
|
| 349 |
r := createTransformer(c, spec, "f1.txt", countstr.String()) |
| 350 |
|
| 351 |
tr, _ := r.Transform(transformations...) |
| 352 |
content, err := tr.(resource.ContentProvider).Content(context.Background()) |
| 353 |
c.Assert(err, qt.IsNil) |
| 354 |
|
| 355 |
c.Assert(content, qt.Equals, "ABCDEFGHIJKLMNOPQRSTUVWXYZ") |
| 356 |
|
| 357 |
assertNoDuplicateWrites(c, spec) |
| 358 |
}) |
| 359 |
|
| 360 |
c.Run("Image", func(c *qt.C) { |
| 361 |
c.Parallel() |
| 362 |
|
| 363 |
spec := newTestResourceSpec(specDescriptor{c: c}) |
| 364 |
|
| 365 |
transformation := &testTransformation{ |
| 366 |
name: "test", |
| 367 |
transform: func(ctx *resources.ResourceTransformationCtx) error { |
| 368 |
ctx.AddOutPathIdentifier(".changed") |
| 369 |
return nil |
| 370 |
}, |
| 371 |
} |
| 372 |
|
| 373 |
r := createTransformer(c, spec, "gopher.png", helpers.ReaderToString(gopherPNG())) |
| 374 |
|
| 375 |
tr, err := r.Transform(transformation) |
| 376 |
c.Assert(err, qt.IsNil) |
| 377 |
c.Assert(tr.MediaType(), eq, media.Builtin.PNGType) |
| 378 |
|
| 379 |
img, ok := tr.(images.ImageResource) |
| 380 |
c.Assert(ok, qt.Equals, true) |
| 381 |
|
| 382 |
c.Assert(img.Width(), qt.Equals, 75) |
| 383 |
c.Assert(img.Height(), qt.Equals, 60) |
| 384 |
|
| 385 |
// RelPermalink called. |
| 386 |
resizedPublished1, err := img.Resize("40x40") |
| 387 |
c.Assert(err, qt.IsNil) |
| 388 |
c.Assert(resizedPublished1.Height(), qt.Equals, 40) |
| 389 |
c.Assert(resizedPublished1.RelPermalink(), qt.Equals, "/gopher.changed_hu_6347c67500afc377.png") |
| 390 |
assertShouldExist(c, spec, "public/gopher.changed_hu_6347c67500afc377.png", true) |
| 391 |
|
| 392 |
// Permalink called. |
| 393 |
resizedPublished2, err := img.Resize("30x30") |
| 394 |
c.Assert(err, qt.IsNil) |
| 395 |
c.Assert(resizedPublished2.Height(), qt.Equals, 30) |
| 396 |
c.Assert(resizedPublished2.Permalink(), qt.Equals, "https://example.com/gopher.changed_hu_2d293650135f63d6.png") |
| 397 |
assertShouldExist(c, spec, "public/gopher.changed_hu_2d293650135f63d6.png", true) |
| 398 |
|
| 399 |
assertNoDuplicateWrites(c, spec) |
| 400 |
}) |
| 401 |
|
| 402 |
c.Run("Concurrent", func(c *qt.C) { |
| 403 |
spec := newTestResourceSpec(specDescriptor{c: c}) |
| 404 |
|
| 405 |
transformers := make([]resources.Transformer, 10) |
| 406 |
transformations := make([]resources.ResourceTransformation, 10) |
| 407 |
|
| 408 |
for i := range 10 { |
| 409 |
transformers[i] = createTransformer(c, spec, fmt.Sprintf("f%d.txt", i), fmt.Sprintf("color is %d", i)) |
| 410 |
transformations[i] = createContentReplacer("test", strconv.Itoa(i), "blue") |
| 411 |
} |
| 412 |
|
| 413 |
var wg sync.WaitGroup |
| 414 |
|
| 415 |
for i := range 13 { |
| 416 |
wg.Add(1) |
| 417 |
go func(i int) { |
| 418 |
defer wg.Done() |
| 419 |
for j := range 23 { |
| 420 |
id := (i + j) % 10 |
| 421 |
tr, err := transformers[id].Transform(transformations[id]) |
| 422 |
c.Assert(err, qt.IsNil) |
| 423 |
content, err := tr.(resource.ContentProvider).Content(context.Background()) |
| 424 |
c.Assert(err, qt.IsNil) |
| 425 |
c.Assert(content, qt.Equals, "color is blue") |
| 426 |
c.Assert(tr.RelPermalink(), qt.Equals, fmt.Sprintf("/f%d.test.txt", id)) |
| 427 |
} |
| 428 |
}(i) |
| 429 |
} |
| 430 |
wg.Wait() |
| 431 |
|
| 432 |
assertNoDuplicateWrites(c, spec) |
| 433 |
}) |
| 434 |
} |
| 435 |
|
| 436 |
type testTransformation struct { |
| 437 |
name string |
| 438 |
transform func(ctx *resources.ResourceTransformationCtx) error |
| 439 |
} |
| 440 |
|
| 441 |
func (t *testTransformation) Key() internal.ResourceTransformationKey { |
| 442 |
return internal.NewResourceTransformationKey(t.name) |
| 443 |
} |
| 444 |
|
| 445 |
func (t *testTransformation) Transform(ctx *resources.ResourceTransformationCtx) error { |
| 446 |
return t.transform(ctx) |
| 447 |
} |