github.com/schumacherfm/hugo@v0.47.1/source/fileInfo_test.go (about)

     1  // Copyright 2017-present 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  	"path/filepath"
    18  	"testing"
    19  
    20  	"github.com/gohugoio/hugo/helpers"
    21  
    22  	"github.com/gohugoio/hugo/hugofs"
    23  	"github.com/spf13/afero"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestFileInfo(t *testing.T) {
    28  	assert := require.New(t)
    29  
    30  	s := newTestSourceSpec()
    31  
    32  	for _, this := range []struct {
    33  		base     string
    34  		filename string
    35  		assert   func(f *FileInfo)
    36  	}{
    37  		{filepath.FromSlash("/a/"), filepath.FromSlash("/a/b/page.md"), func(f *FileInfo) {
    38  			assert.Equal(filepath.FromSlash("/a/b/page.md"), f.Filename())
    39  			assert.Equal(filepath.FromSlash("b/"), f.Dir())
    40  			assert.Equal(filepath.FromSlash("b/page.md"), f.Path())
    41  			assert.Equal("b", f.Section())
    42  			assert.Equal(filepath.FromSlash("page"), f.TranslationBaseName())
    43  			assert.Equal(filepath.FromSlash("page"), f.BaseFileName())
    44  
    45  		}},
    46  		{filepath.FromSlash("/a/"), filepath.FromSlash("/a/b/c/d/page.md"), func(f *FileInfo) {
    47  			assert.Equal("b", f.Section())
    48  
    49  		}},
    50  		{filepath.FromSlash("/a/"), filepath.FromSlash("/a/b/page.en.MD"), func(f *FileInfo) {
    51  			assert.Equal("b", f.Section())
    52  			assert.Equal(filepath.FromSlash("b/page.en.MD"), f.Path())
    53  			assert.Equal(filepath.FromSlash("page"), f.TranslationBaseName())
    54  			assert.Equal(filepath.FromSlash("page.en"), f.BaseFileName())
    55  
    56  		}},
    57  	} {
    58  		f := s.NewFileInfo(this.base, this.filename, false, nil)
    59  		this.assert(f)
    60  	}
    61  
    62  }
    63  
    64  func TestFileInfoLanguage(t *testing.T) {
    65  	assert := require.New(t)
    66  	langs := map[string]bool{
    67  		"sv": true,
    68  		"en": true,
    69  	}
    70  
    71  	m := afero.NewMemMapFs()
    72  	lfs := hugofs.NewLanguageFs("sv", langs, m)
    73  	v := newTestConfig()
    74  
    75  	fs := hugofs.NewFrom(m, v)
    76  
    77  	ps, err := helpers.NewPathSpec(fs, v)
    78  	assert.NoError(err)
    79  	s := SourceSpec{SourceFs: lfs, PathSpec: ps}
    80  	s.Languages = map[string]interface{}{
    81  		"en": true,
    82  	}
    83  
    84  	err = afero.WriteFile(lfs, "page.md", []byte("abc"), 0777)
    85  	assert.NoError(err)
    86  	err = afero.WriteFile(lfs, "page.en.md", []byte("abc"), 0777)
    87  	assert.NoError(err)
    88  
    89  	sv, _ := lfs.Stat("page.md")
    90  	en, _ := lfs.Stat("page.en.md")
    91  
    92  	fiSv := s.NewFileInfo("", "page.md", false, sv)
    93  	fiEn := s.NewFileInfo("", "page.en.md", false, en)
    94  
    95  	assert.Equal("sv", fiSv.Lang())
    96  	assert.Equal("en", fiEn.Lang())
    97  }