modules: Add ignoreImports to module imports config
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 9 Sep 2020 17:08:03 +0000 (19:08 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 10 Sep 2020 06:47:05 +0000 (08:47 +0200)
Fixes #7646

hugolib/hugo_modules_test.go
modules/client.go
modules/collect.go
modules/config.go

index 037684862031a775319e047a40dff82841dc7808..0ed4fceb01d185c74f29d9f789f1d7629e7edbf6 100644 (file)
@@ -39,11 +39,11 @@ import (
 )
 
 // https://github.com/gohugoio/hugo/issues/6730
-func TestHugoModulesTargetInSubFolder(t *testing.T) {
+func TestHugoModulesVariants(t *testing.T) {
        if !isCI() {
-               // TODO(bep) investigate why this fails when running in LiteIDE (it works from the shell).
                t.Skip("skip (relative) long running modules test when running locally")
        }
+
        config := `
 baseURL="https://example.org"
 workingDir = %q
@@ -51,45 +51,88 @@ workingDir = %q
 [module]
 [[module.imports]]
 path="github.com/gohugoio/hugoTestModule2"
-  [[module.imports.mounts]]
-    source = "templates/hooks"
-    target = "layouts/_default/_markup"
-    
+%s
 `
 
-       b := newTestSitesBuilder(t)
-       workingDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-modules-target-in-subfolder-test")
-       b.Assert(err, qt.IsNil)
-       defer clean()
-       b.Fs = hugofs.NewDefault(viper.New())
-       b.WithWorkingDir(workingDir).WithConfigFile("toml", fmt.Sprintf(config, workingDir))
-       b.WithTemplates("_default/single.html", `{{ .Content }}`)
-       b.WithContent("p1.md", `---
+       createConfig := func(workingDir, moduleOpts string) string {
+               return fmt.Sprintf(config, workingDir, moduleOpts)
+       }
+
+       newTestBuilder := func(t testing.TB, moduleOpts string) (*sitesBuilder, func()) {
+               b := newTestSitesBuilder(t)
+               workingDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-modules-variants")
+               b.Assert(err, qt.IsNil)
+               b.Fs = hugofs.NewDefault(viper.New())
+               b.WithWorkingDir(workingDir).WithConfigFile("toml", createConfig(workingDir, moduleOpts))
+               b.WithTemplates(
+                       "index.html", `
+Param from module: {{ site.Params.Hugo }}|
+{{ $js := resources.Get "jslibs/alpinejs/alpine.js" }}
+JS imported in module: {{ with $js }}{{ .RelPermalink }}{{ end }}|
+`,
+                       "_default/single.html", `{{ .Content }}`)
+               b.WithContent("p1.md", `---
 title: "Page"
 ---
 
 [A link](https://bep.is)
 
 `)
-       b.WithSourceFile("go.mod", `
+               b.WithSourceFile("go.mod", `
 module github.com/gohugoio/tests/testHugoModules
 
 
 `)
 
-       b.WithSourceFile("go.sum", `
+               b.WithSourceFile("go.sum", `
 github.com/gohugoio/hugoTestModule2 v0.0.0-20200131160637-9657d7697877 h1:WLM2bQCKIWo04T6NsIWsX/Vtirhf0TnpY66xyqGlgVY=
 github.com/gohugoio/hugoTestModule2 v0.0.0-20200131160637-9657d7697877/go.mod h1:CBFZS3khIAXKxReMwq0le8sEl/D8hcXmixlOHVv+Gd0=
 `)
 
-       b.Build(BuildCfg{})
+               return b, clean
+
+       }
 
-       b.AssertFileContent("public/p1/index.html", `<p>Page|https://bep.is|Title: |Text: A link|END</p>`)
+       t.Run("Target in subfolder", func(t *testing.T) {
+
+               b, clean := newTestBuilder(t, "ignoreImports=true")
+               defer clean()
+
+               b.Build(BuildCfg{})
+
+               b.AssertFileContent("public/p1/index.html", `<p>Page|https://bep.is|Title: |Text: A link|END</p>`)
+       })
+
+       t.Run("Ignore config", func(t *testing.T) {
+
+               b, clean := newTestBuilder(t, "ignoreConfig=true")
+               defer clean()
+
+               b.Build(BuildCfg{})
+
+               b.AssertFileContent("public/index.html", `
+Param from module: |
+JS imported in module: |
+`)
+       })
+
+       t.Run("Ignore imports", func(t *testing.T) {
+
+               b, clean := newTestBuilder(t, "ignoreImports=true")
+               defer clean()
+
+               b.Build(BuildCfg{})
+
+               b.AssertFileContent("public/index.html", `
+Param from module: Rocks|
+JS imported in module: |
+`)
+       })
 
 }
 
 // TODO(bep) this fails when testmodBuilder is also building ...
-func TestHugoModules(t *testing.T) {
+func TestHugoModulesMatrix(t *testing.T) {
        if !isCI() {
                t.Skip("skip (relative) long running modules test when running locally")
        }
index 914d06a4e653dd8987a84fea38f5bf939a1a0fc2..86f8a2caaf82447ad521a5c9105dc6259731909b 100644 (file)
@@ -219,7 +219,7 @@ func (c *Client) Vendor() error {
                        // This is the project.
                        continue
                }
-               // We respect the --ignoreVendor flag even for the vendor command.
+
                if !t.IsGoMod() && !t.Vendor() {
                        // We currently do not vendor components living in the
                        // theme directory, see https://github.com/gohugoio/hugo/issues/5993
index f87ed248429bef9dd76624ad936506a07ad426d8..b82d395fd0ec741a8d3591f9959c40a4da43ad11 100644 (file)
@@ -339,7 +339,7 @@ func (c *collector) addAndRecurse(owner *moduleAdapter, disabled bool) error {
                        if err != nil {
                                return err
                        }
-                       if tc == nil {
+                       if tc == nil || moduleImport.IgnoreImports {
                                continue
                        }
                        if err := c.addAndRecurse(tc, disabled); err != nil {
index a50845df30981dc420ffe8f605c4d0159c3c0929..5e95f0ac1038555992e18cd6ebd801b67e0c9a77 100644 (file)
@@ -301,10 +301,12 @@ func (v HugoVersion) IsValid() bool {
 }
 
 type Import struct {
-       Path         string // Module path
-       IgnoreConfig bool   // Ignore any config.toml found.
-       Disable      bool   // Turn off this module.
-       Mounts       []Mount
+       Path          string // Module path
+       IgnoreConfig  bool   // Ignore any config in config.toml (will still folow imports).
+       IgnoreImports bool   // Do not follow any configured imports.
+       NoVendor      bool   // Never vendor this import (only allowed in main project).
+       Disable       bool   // Turn off this module.
+       Mounts        []Mount
 }
 
 type Mount struct {