github.com/lyeb/hugo@v0.47.1/commands/new_content_test.go (about) 1 // Copyright 2018 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 commands 15 16 import ( 17 "path/filepath" 18 "testing" 19 20 "github.com/gohugoio/hugo/hugofs" 21 "github.com/spf13/viper" 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/require" 24 ) 25 26 // Issue #1133 27 func TestNewContentPathSectionWithForwardSlashes(t *testing.T) { 28 p, s := newContentPathSection("/post/new.md") 29 assert.Equal(t, filepath.FromSlash("/post/new.md"), p) 30 assert.Equal(t, "post", s) 31 } 32 33 func checkNewSiteInited(fs *hugofs.Fs, basepath string, t *testing.T) { 34 35 paths := []string{ 36 filepath.Join(basepath, "layouts"), 37 filepath.Join(basepath, "content"), 38 filepath.Join(basepath, "archetypes"), 39 filepath.Join(basepath, "static"), 40 filepath.Join(basepath, "data"), 41 filepath.Join(basepath, "config.toml"), 42 } 43 44 for _, path := range paths { 45 _, err := fs.Source.Stat(path) 46 require.NoError(t, err) 47 } 48 } 49 50 func TestDoNewSite(t *testing.T) { 51 n := newNewSiteCmd() 52 basepath := filepath.Join("base", "blog") 53 _, fs := newTestCfg() 54 55 require.NoError(t, n.doNewSite(fs, basepath, false)) 56 57 checkNewSiteInited(fs, basepath, t) 58 } 59 60 func TestDoNewSite_noerror_base_exists_but_empty(t *testing.T) { 61 basepath := filepath.Join("base", "blog") 62 _, fs := newTestCfg() 63 n := newNewSiteCmd() 64 65 require.NoError(t, fs.Source.MkdirAll(basepath, 777)) 66 67 require.NoError(t, n.doNewSite(fs, basepath, false)) 68 } 69 70 func TestDoNewSite_error_base_exists(t *testing.T) { 71 basepath := filepath.Join("base", "blog") 72 _, fs := newTestCfg() 73 n := newNewSiteCmd() 74 75 require.NoError(t, fs.Source.MkdirAll(basepath, 777)) 76 _, err := fs.Source.Create(filepath.Join(basepath, "foo")) 77 require.NoError(t, err) 78 // Since the directory already exists and isn't empty, expect an error 79 require.Error(t, n.doNewSite(fs, basepath, false)) 80 81 } 82 83 func TestDoNewSite_force_empty_dir(t *testing.T) { 84 basepath := filepath.Join("base", "blog") 85 _, fs := newTestCfg() 86 n := newNewSiteCmd() 87 88 require.NoError(t, fs.Source.MkdirAll(basepath, 777)) 89 90 require.NoError(t, n.doNewSite(fs, basepath, true)) 91 92 checkNewSiteInited(fs, basepath, t) 93 } 94 95 func TestDoNewSite_error_force_dir_inside_exists(t *testing.T) { 96 basepath := filepath.Join("base", "blog") 97 _, fs := newTestCfg() 98 n := newNewSiteCmd() 99 100 contentPath := filepath.Join(basepath, "content") 101 102 require.NoError(t, fs.Source.MkdirAll(contentPath, 777)) 103 require.Error(t, n.doNewSite(fs, basepath, true)) 104 } 105 106 func TestDoNewSite_error_force_config_inside_exists(t *testing.T) { 107 basepath := filepath.Join("base", "blog") 108 _, fs := newTestCfg() 109 n := newNewSiteCmd() 110 111 configPath := filepath.Join(basepath, "config.toml") 112 require.NoError(t, fs.Source.MkdirAll(basepath, 777)) 113 _, err := fs.Source.Create(configPath) 114 require.NoError(t, err) 115 116 require.Error(t, n.doNewSite(fs, basepath, true)) 117 } 118 119 func newTestCfg() (*viper.Viper, *hugofs.Fs) { 120 121 v := viper.New() 122 fs := hugofs.NewMem(v) 123 124 v.SetFs(fs.Source) 125 126 return v, fs 127 128 }