Debian Codegraph @

internal/js/esbuild/resolve_test.go

raw
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 esbuild
15
16 import (
17 "path"
18 "path/filepath"
19 "testing"
20
21 qt "github.com/frankban/quicktest"
22 "github.com/gohugoio/hugo/config"
23 "github.com/gohugoio/hugo/config/testconfig"
24 "github.com/gohugoio/hugo/hugofs"
25 "github.com/gohugoio/hugo/hugolib/filesystems"
26 "github.com/gohugoio/hugo/hugolib/paths"
27 "github.com/spf13/afero"
28 )
29
30 func TestResolveComponentInAssets(t *testing.T) {
31 c := qt.New(t)
32
33 for _, test := range []struct {
34 name string
35 files []string
36 impPath string
37 expect string
38 }{
39 {"Basic, extension", []string{"foo.js", "bar.js"}, "foo.js", "foo.js"},
40 {"Basic, no extension", []string{"foo.js", "bar.js"}, "foo", "foo.js"},
41 {"Basic, no extension, typescript", []string{"foo.ts", "bar.js"}, "foo", "foo.ts"},
42 {"Not found", []string{"foo.js", "bar.js"}, "moo.js", ""},
43 {"Not found, double js extension", []string{"foo.js.js", "bar.js"}, "foo.js", ""},
44 {"Index file, folder only", []string{"foo/index.js", "bar.js"}, "foo", "foo/index.js"},
45 {"Index file, folder and index", []string{"foo/index.js", "bar.js"}, "foo/index", "foo/index.js"},
46 {"Index file, folder and index and suffix", []string{"foo/index.js", "bar.js"}, "foo/index.js", "foo/index.js"},
47 {"Index ESM file, folder only", []string{"foo/index.esm.js", "bar.js"}, "foo", "foo/index.esm.js"},
48 {"Index ESM file, folder and index", []string{"foo/index.esm.js", "bar.js"}, "foo/index", "foo/index.esm.js"},
49 {"Index ESM file, folder and index and suffix", []string{"foo/index.esm.js", "bar.js"}, "foo/index.esm.js", "foo/index.esm.js"},
50 // We added these index.esm.js cases in v0.101.0. The case below is unlikely to happen in the wild, but add a test
51 // to document Hugo's behavior. We pick the file with the name index.js; anything else would be breaking.
52 {"Index and Index ESM file, folder only", []string{"foo/index.esm.js", "foo/index.js", "bar.js"}, "foo", "foo/index.js"},
53
54 // Issue #8949
55 {"Check file before directory", []string{"foo.js", "foo/index.js"}, "foo", "foo.js"},
56 } {
57 c.Run(test.name, func(c *qt.C) {
58 baseDir := "assets"
59 mfs := afero.NewMemMapFs()
60
61 for _, filename := range test.files {
62 c.Assert(afero.WriteFile(mfs, filepath.Join(baseDir, filename), []byte("let foo='bar';"), 0o777), qt.IsNil)
63 }
64
65 conf := testconfig.GetTestConfig(mfs, config.New())
66 fs := hugofs.NewFrom(mfs, conf.BaseConfig())
67
68 p, err := paths.New(fs, conf)
69 c.Assert(err, qt.IsNil)
70 bfs, err := filesystems.NewBase(p, nil)
71 c.Assert(err, qt.IsNil)
72 resolver := newFSResolver(bfs.Assets.Fs)
73
74 got := resolver.resolveComponent(test.impPath, false)
75
76 gotPath := ""
77 expect := test.expect
78 if got != nil {
79 gotPath = filepath.ToSlash(got.Filename)
80 expect = path.Join(baseDir, test.expect)
81 }
82
83 c.Assert(gotPath, qt.Equals, expect)
84 })
85 }
86 }