hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

client_test.go (6653B)

    1 // Copyright 2019 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 modules
   15 
   16 import (
   17 	"bytes"
   18 	"fmt"
   19 	"os"
   20 	"path/filepath"
   21 	"sync/atomic"
   22 	"testing"
   23 
   24 	"github.com/gohugoio/hugo/common/hexec"
   25 	"github.com/gohugoio/hugo/config/security"
   26 	"github.com/gohugoio/hugo/hugofs/glob"
   27 
   28 	"github.com/gohugoio/hugo/htesting"
   29 
   30 	"github.com/gohugoio/hugo/hugofs"
   31 
   32 	qt "github.com/frankban/quicktest"
   33 )
   34 
   35 func TestClient(t *testing.T) {
   36 	modName := "hugo-modules-basic-test"
   37 	modPath := "github.com/gohugoio/tests/" + modName
   38 	defaultImport := "modh2_2"
   39 	expect := `github.com/gohugoio/tests/hugo-modules-basic-test github.com/gohugoio/hugoTestModules1_darwin/modh2_2@v1.4.0
   40 github.com/gohugoio/hugoTestModules1_darwin/modh2_2@v1.4.0 github.com/gohugoio/hugoTestModules1_darwin/modh2_2_1v@v1.3.0
   41 github.com/gohugoio/hugoTestModules1_darwin/modh2_2@v1.4.0 github.com/gohugoio/hugoTestModules1_darwin/modh2_2_2@v1.3.0
   42 `
   43 
   44 	c := qt.New(t)
   45 	var clientID uint64 // we increment this to get each test in its own directory.
   46 
   47 	newClient := func(c *qt.C, withConfig func(cfg *ClientConfig), imp string) (*Client, func()) {
   48 		atomic.AddUint64(&clientID, uint64(1))
   49 		workingDir, clean, err := htesting.CreateTempDir(hugofs.Os, fmt.Sprintf("%s-%d", modName, clientID))
   50 		c.Assert(err, qt.IsNil)
   51 		themesDir := filepath.Join(workingDir, "themes")
   52 		err = os.Mkdir(themesDir, 0777)
   53 		c.Assert(err, qt.IsNil)
   54 
   55 		ccfg := ClientConfig{
   56 			Fs:         hugofs.Os,
   57 			WorkingDir: workingDir,
   58 			CacheDir:   filepath.Join(workingDir, "modcache"),
   59 			ThemesDir:  themesDir,
   60 			Exec:       hexec.New(security.DefaultConfig),
   61 		}
   62 
   63 		withConfig(&ccfg)
   64 		ccfg.ModuleConfig.Imports = []Import{{Path: "github.com/gohugoio/hugoTestModules1_darwin/" + imp}}
   65 		client := NewClient(ccfg)
   66 
   67 		return client, clean
   68 	}
   69 
   70 	c.Run("All", func(c *qt.C) {
   71 		client, clean := newClient(c, func(cfg *ClientConfig) {
   72 			cfg.ModuleConfig = DefaultModuleConfig
   73 		}, defaultImport)
   74 		defer clean()
   75 
   76 		// Test Init
   77 		c.Assert(client.Init(modPath), qt.IsNil)
   78 
   79 		// Test Collect
   80 		mc, err := client.Collect()
   81 		c.Assert(err, qt.IsNil)
   82 		c.Assert(len(mc.AllModules), qt.Equals, 4)
   83 		for _, m := range mc.AllModules {
   84 			c.Assert(m, qt.Not(qt.IsNil))
   85 		}
   86 
   87 		// Test Graph
   88 		var graphb bytes.Buffer
   89 		c.Assert(client.Graph(&graphb), qt.IsNil)
   90 
   91 		c.Assert(graphb.String(), qt.Equals, expect)
   92 
   93 		// Test Vendor
   94 		c.Assert(client.Vendor(), qt.IsNil)
   95 		graphb.Reset()
   96 		c.Assert(client.Graph(&graphb), qt.IsNil)
   97 
   98 		expectVendored := `project github.com/gohugoio/hugoTestModules1_darwin/modh2_2@v1.4.0+vendor
   99 project github.com/gohugoio/hugoTestModules1_darwin/modh2_2_1v@v1.3.0+vendor
  100 project github.com/gohugoio/hugoTestModules1_darwin/modh2_2_2@v1.3.0+vendor
  101 `
  102 
  103 		c.Assert(graphb.String(), qt.Equals, expectVendored)
  104 
  105 		// Test Tidy
  106 		c.Assert(client.Tidy(), qt.IsNil)
  107 	})
  108 
  109 	c.Run("IgnoreVendor", func(c *qt.C) {
  110 		client, clean := newClient(
  111 			c, func(cfg *ClientConfig) {
  112 				cfg.ModuleConfig = DefaultModuleConfig
  113 				cfg.IgnoreVendor = globAll
  114 			}, defaultImport)
  115 		defer clean()
  116 
  117 		c.Assert(client.Init(modPath), qt.IsNil)
  118 		_, err := client.Collect()
  119 		c.Assert(err, qt.IsNil)
  120 		c.Assert(client.Vendor(), qt.IsNil)
  121 
  122 		var graphb bytes.Buffer
  123 		c.Assert(client.Graph(&graphb), qt.IsNil)
  124 		c.Assert(graphb.String(), qt.Equals, expect)
  125 	})
  126 
  127 	c.Run("NoVendor", func(c *qt.C) {
  128 		mcfg := DefaultModuleConfig
  129 		mcfg.NoVendor = "**"
  130 		client, clean := newClient(
  131 			c, func(cfg *ClientConfig) {
  132 				cfg.ModuleConfig = mcfg
  133 			}, defaultImport)
  134 		defer clean()
  135 
  136 		c.Assert(client.Init(modPath), qt.IsNil)
  137 		_, err := client.Collect()
  138 		c.Assert(err, qt.IsNil)
  139 		c.Assert(client.Vendor(), qt.IsNil)
  140 
  141 		var graphb bytes.Buffer
  142 		c.Assert(client.Graph(&graphb), qt.IsNil)
  143 		c.Assert(graphb.String(), qt.Equals, expect)
  144 	})
  145 
  146 	c.Run("VendorClosest", func(c *qt.C) {
  147 		mcfg := DefaultModuleConfig
  148 		mcfg.VendorClosest = true
  149 
  150 		client, clean := newClient(
  151 			c, func(cfg *ClientConfig) {
  152 				cfg.ModuleConfig = mcfg
  153 				s := "github.com/gohugoio/hugoTestModules1_darwin/modh1_1v"
  154 				g, _ := glob.GetGlob(s)
  155 				cfg.IgnoreVendor = g
  156 			}, "modh1v")
  157 		defer clean()
  158 
  159 		c.Assert(client.Init(modPath), qt.IsNil)
  160 		_, err := client.Collect()
  161 		c.Assert(err, qt.IsNil)
  162 		c.Assert(client.Vendor(), qt.IsNil)
  163 
  164 		var graphb bytes.Buffer
  165 		c.Assert(client.Graph(&graphb), qt.IsNil)
  166 
  167 		c.Assert(graphb.String(), qt.Contains, "github.com/gohugoio/hugoTestModules1_darwin/modh1_1v@v1.3.0 github.com/gohugoio/hugoTestModules1_darwin/modh1_1_1v@v1.1.0+vendor")
  168 	})
  169 
  170 	// https://github.com/gohugoio/hugo/issues/7908
  171 	c.Run("createThemeDirname", func(c *qt.C) {
  172 		mcfg := DefaultModuleConfig
  173 		client, clean := newClient(
  174 			c, func(cfg *ClientConfig) {
  175 				cfg.ModuleConfig = mcfg
  176 			}, defaultImport)
  177 		defer clean()
  178 
  179 		dirname, err := client.createThemeDirname("foo", false)
  180 		c.Assert(err, qt.IsNil)
  181 		c.Assert(dirname, qt.Equals, filepath.Join(client.ccfg.ThemesDir, "foo"))
  182 
  183 		dirname, err = client.createThemeDirname("../../foo", true)
  184 		c.Assert(err, qt.IsNil)
  185 		c.Assert(dirname, qt.Equals, filepath.Join(client.ccfg.ThemesDir, "../../foo"))
  186 
  187 		dirname, err = client.createThemeDirname("../../foo", false)
  188 		c.Assert(err, qt.Not(qt.IsNil))
  189 
  190 		absDir := filepath.Join(client.ccfg.WorkingDir, "..", "..")
  191 		dirname, err = client.createThemeDirname(absDir, true)
  192 		c.Assert(err, qt.IsNil)
  193 		c.Assert(dirname, qt.Equals, absDir)
  194 		dirname, err = client.createThemeDirname(absDir, false)
  195 		fmt.Println(dirname)
  196 		c.Assert(err, qt.Not(qt.IsNil))
  197 	})
  198 }
  199 
  200 var globAll, _ = glob.GetGlob("**")
  201 
  202 func TestGetModlineSplitter(t *testing.T) {
  203 	c := qt.New(t)
  204 
  205 	gomodSplitter := getModlineSplitter(true)
  206 
  207 	c.Assert(gomodSplitter("\tgithub.com/BurntSushi/toml v0.3.1"), qt.DeepEquals, []string{"github.com/BurntSushi/toml", "v0.3.1"})
  208 	c.Assert(gomodSplitter("\tgithub.com/cpuguy83/go-md2man v1.0.8 // indirect"), qt.DeepEquals, []string{"github.com/cpuguy83/go-md2man", "v1.0.8"})
  209 	c.Assert(gomodSplitter("require ("), qt.IsNil)
  210 
  211 	gosumSplitter := getModlineSplitter(false)
  212 	c.Assert(gosumSplitter("github.com/BurntSushi/toml v0.3.1"), qt.DeepEquals, []string{"github.com/BurntSushi/toml", "v0.3.1"})
  213 }