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