filesystem_test.go (2563B)
1 // Copyright 2015 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 source
15
16 import (
17 "fmt"
18 "path/filepath"
19 "runtime"
20 "testing"
21
22 "github.com/gohugoio/hugo/config"
23
24 "github.com/gohugoio/hugo/modules"
25
26 "github.com/gohugoio/hugo/langs"
27
28 "github.com/spf13/afero"
29
30 qt "github.com/frankban/quicktest"
31 "github.com/gohugoio/hugo/helpers"
32 "github.com/gohugoio/hugo/hugofs"
33 )
34
35 func TestEmptySourceFilesystem(t *testing.T) {
36 c := qt.New(t)
37 ss := newTestSourceSpec()
38 src := ss.NewFilesystem("")
39 files, err := src.Files()
40 c.Assert(err, qt.IsNil)
41 if len(files) != 0 {
42 t.Errorf("new filesystem should contain 0 files.")
43 }
44 }
45
46 func TestUnicodeNorm(t *testing.T) {
47 if runtime.GOOS != "darwin" {
48 // Normalization code is only for Mac OS, since it is not necessary for other OSes.
49 return
50 }
51
52 c := qt.New(t)
53
54 paths := []struct {
55 NFC string
56 NFD string
57 }{
58 {NFC: "å", NFD: "\x61\xcc\x8a"},
59 {NFC: "é", NFD: "\x65\xcc\x81"},
60 }
61
62 ss := newTestSourceSpec()
63 fi := hugofs.NewFileMetaInfo(nil, hugofs.NewFileMeta())
64
65 for i, path := range paths {
66 base := fmt.Sprintf("base%d", i)
67 c.Assert(afero.WriteFile(ss.Fs.Source, filepath.Join(base, path.NFD), []byte("some data"), 0777), qt.IsNil)
68 src := ss.NewFilesystem(base)
69 _ = src.add(path.NFD, fi)
70 files, err := src.Files()
71 c.Assert(err, qt.IsNil)
72 f := files[0]
73 if f.BaseFileName() != path.NFC {
74 t.Fatalf("file %q name in NFD form should be normalized (%s)", f.BaseFileName(), path.NFC)
75 }
76 }
77 }
78
79 func newTestConfig() config.Provider {
80 v := config.NewWithTestDefaults()
81 _, err := langs.LoadLanguageSettings(v, nil)
82 if err != nil {
83 panic(err)
84 }
85 mod, err := modules.CreateProjectModule(v)
86 if err != nil {
87 panic(err)
88 }
89 v.Set("allModules", modules.Modules{mod})
90
91 return v
92 }
93
94 func newTestSourceSpec() *SourceSpec {
95 v := newTestConfig()
96 fs := hugofs.NewFrom(hugofs.NewBaseFileDecorator(afero.NewMemMapFs()), v)
97 ps, err := helpers.NewPathSpec(fs, v, nil)
98 if err != nil {
99 panic(err)
100 }
101 return NewSourceSpec(ps, nil, fs.Source)
102 }