integration_test.go (3602B)
1 // Copyright 2022s 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 "testing"
18
19 qt "github.com/frankban/quicktest"
20 "github.com/gohugoio/hugo/hugolib"
21 )
22
23 func TestCopy(t *testing.T) {
24 t.Parallel()
25
26 files := `
27 -- config.toml --
28 baseURL = "http://example.com/blog"
29 -- assets/images/pixel.png --
30 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
31 -- layouts/index.html --
32 {{/* Image resources */}}
33 {{ $img := resources.Get "images/pixel.png" }}
34 {{ $imgCopy1 := $img | resources.Copy "images/copy.png" }}
35 {{ $imgCopy1 = $imgCopy1.Resize "3x4"}}
36 {{ $imgCopy2 := $imgCopy1 | resources.Copy "images/copy2.png" }}
37 {{ $imgCopy3 := $imgCopy1 | resources.Copy "images/copy3.png" }}
38 Image Orig: {{ $img.RelPermalink}}|{{ $img.MediaType }}|{{ $img.Width }}|{{ $img.Height }}|
39 Image Copy1: {{ $imgCopy1.RelPermalink}}|{{ $imgCopy1.MediaType }}|{{ $imgCopy1.Width }}|{{ $imgCopy1.Height }}|
40 Image Copy2: {{ $imgCopy2.RelPermalink}}|{{ $imgCopy2.MediaType }}|{{ $imgCopy2.Width }}|{{ $imgCopy2.Height }}|
41 Image Copy3: {{ $imgCopy3.MediaType }}|{{ $imgCopy3.Width }}|{{ $imgCopy3.Height }}|
42
43 {{/* Generic resources */}}
44 {{ $targetPath := "js/vars.js" }}
45 {{ $orig := "let foo;" | resources.FromString "js/foo.js" }}
46 {{ $copy1 := $orig | resources.Copy "js/copies/bar.js" }}
47 {{ $copy2 := $orig | resources.Copy "js/copies/baz.js" | fingerprint "md5" }}
48 {{ $copy3 := $copy2 | resources.Copy "js/copies/moo.js" | minify }}
49
50 Orig: {{ $orig.RelPermalink}}|{{ $orig.MediaType }}|{{ $orig.Content | safeJS }}|
51 Copy1: {{ $copy1.RelPermalink}}|{{ $copy1.MediaType }}|{{ $copy1.Content | safeJS }}|
52 Copy2: {{ $copy2.RelPermalink}}|{{ $copy2.MediaType }}|{{ $copy2.Content | safeJS }}|
53 Copy3: {{ $copy3.RelPermalink}}|{{ $copy3.MediaType }}|{{ $copy3.Content | safeJS }}|
54
55 `
56
57 b := hugolib.NewIntegrationTestBuilder(
58 hugolib.IntegrationTestConfig{
59 T: t,
60 TxtarString: files,
61 }).Build()
62
63 b.AssertFileContent("public/index.html", `
64 Image Orig: /blog/images/pixel.png|image/png|1|1|
65 Image Copy1: /blog/images/copy_hu8aa3346827e49d756ff4e630147c42b5_70_3x4_resize_box_3.png|image/png|3|4|
66 Image Copy2: /blog/images/copy2.png|image/png|3|4
67 Image Copy3: image/png|3|4|
68 Orig: /blog/js/foo.js|application/javascript|let foo;|
69 Copy1: /blog/js/copies/bar.js|application/javascript|let foo;|
70 Copy2: /blog/js/copies/baz.a677329fc6c4ad947e0c7116d91f37a2.js|application/javascript|let foo;|
71 Copy3: /blog/js/copies/moo.a677329fc6c4ad947e0c7116d91f37a2.min.js|application/javascript|let foo|
72
73 `)
74
75 b.AssertDestinationExists("images/copy2.png", true)
76 // No permalink used.
77 b.AssertDestinationExists("images/copy3.png", false)
78
79 }
80
81 func TestCopyPageShouldFail(t *testing.T) {
82 t.Parallel()
83
84 files := `
85 -- config.toml --
86 -- layouts/index.html --
87 {{/* This is currently not supported. */}}
88 {{ $copy := .Copy "copy.md" }}
89
90 `
91
92 b, err := hugolib.NewIntegrationTestBuilder(
93 hugolib.IntegrationTestConfig{
94 T: t,
95 TxtarString: files,
96 }).BuildE()
97
98 b.Assert(err, qt.IsNotNil)
99
100 }