github.com/neohugo/neohugo@v0.123.8/hugolib/hugo_sites_multihost_test.go (about)

     1  package hugolib
     2  
     3  import (
     4  	"testing"
     5  
     6  	qt "github.com/frankban/quicktest"
     7  )
     8  
     9  func TestMultihost(t *testing.T) {
    10  	t.Parallel()
    11  
    12  	files := `
    13  -- hugo.toml --
    14  paginate = 1
    15  defaultContentLanguage = "fr"
    16  defaultContentLanguageInSubdir = false
    17  staticDir = ["s1", "s2"]
    18  enableRobotsTXT = true
    19  
    20  [permalinks]
    21  other = "/somewhere/else/:filename"
    22  
    23  [taxonomies]
    24  tag = "tags"
    25  
    26  [languages]
    27  [languages.en]
    28  staticDir2 = ["staticen"]
    29  baseURL = "https://example.com/docs"
    30  weight = 10
    31  title = "In English"
    32  languageName = "English"
    33  [languages.fr]
    34  staticDir2 = ["staticfr"]
    35  baseURL = "https://example.fr"
    36  weight = 20
    37  title = "Le Français"
    38  languageName = "Français"
    39  -- assets/css/main.css --
    40  body { color: red; }
    41  -- content/mysect/mybundle/index.md --
    42  ---
    43  tags: [a, b]
    44  title: "My Bundle fr"
    45  ---
    46  My Bundle
    47  -- content/mysect/mybundle/index.en.md --
    48  ---
    49  tags: [c, d]
    50  title: "My Bundle en"
    51  ---
    52  My Bundle
    53  -- content/mysect/mybundle/foo.txt --
    54  Foo
    55  -- layouts/_default/list.html --
    56  List|{{ .Title }}|{{ .Lang }}|{{ .Permalink}}|{{ .RelPermalink }}|
    57  -- layouts/_default/single.html --
    58  Single|{{ .Title }}|{{ .Lang }}|{{ .Permalink}}|{{ .RelPermalink }}|
    59  {{ $foo := .Resources.Get "foo.txt" | fingerprint }}
    60  Foo: {{ $foo.Permalink }}|
    61  {{ $css := resources.Get "css/main.css" | fingerprint }}
    62  CSS: {{ $css.Permalink }}|{{ $css.RelPermalink }}|
    63  -- layouts/robots.txt --
    64  robots|{{ site.Language.Lang }}
    65  -- layouts/404.html --
    66  404|{{ site.Language.Lang }}
    67  
    68  
    69  	
    70  `
    71  
    72  	b := Test(t, files)
    73  
    74  	b.Assert(b.H.Conf.IsMultiLingual(), qt.Equals, true)
    75  	b.Assert(b.H.Conf.IsMultihost(), qt.Equals, true)
    76  
    77  	// helpers.PrintFs(b.H.Fs.PublishDir, "", os.Stdout)
    78  
    79  	// Check regular pages.
    80  	b.AssertFileContent("public/en/mysect/mybundle/index.html", "Single|My Bundle en|en|https://example.com/docs/mysect/mybundle/|")
    81  	b.AssertFileContent("public/fr/mysect/mybundle/index.html", "Single|My Bundle fr|fr|https://example.fr/mysect/mybundle/|")
    82  
    83  	// Check robots.txt
    84  	b.AssertFileContent("public/en/robots.txt", "robots|en")
    85  	b.AssertFileContent("public/fr/robots.txt", "robots|fr")
    86  
    87  	// Check sitemap.xml
    88  	b.AssertFileContent("public/en/sitemap.xml", "https://example.com/docs/mysect/mybundle/")
    89  	b.AssertFileContent("public/fr/sitemap.xml", "https://example.fr/mysect/mybundle/")
    90  
    91  	// Check 404
    92  	b.AssertFileContent("public/en/404.html", "404|en")
    93  	b.AssertFileContent("public/fr/404.html", "404|fr")
    94  
    95  	// Check tags.
    96  	b.AssertFileContent("public/en/tags/d/index.html", "List|D|en|https://example.com/docs/tags/d/")
    97  	b.AssertFileContent("public/fr/tags/b/index.html", "List|B|fr|https://example.fr/tags/b/")
    98  	b.AssertFileExists("public/en/tags/b/index.html", false)
    99  	b.AssertFileExists("public/fr/tags/d/index.html", false)
   100  
   101  	// en/mysect/mybundle/foo.txt fingerprinted
   102  	b.AssertFileContent("public/en/mysect/mybundle/foo.1cbec737f863e4922cee63cc2ebbfaafcd1cff8b790d8cfd2e6a5d550b648afa.txt", "Foo")
   103  	b.AssertFileContent("public/en/mysect/mybundle/index.html", "Foo: https://example.com/docs/mysect/mybundle/foo.1cbec737f863e4922cee63cc2ebbfaafcd1cff8b790d8cfd2e6a5d550b648afa.txt|")
   104  	b.AssertFileContent("public/fr/mysect/mybundle/foo.1cbec737f863e4922cee63cc2ebbfaafcd1cff8b790d8cfd2e6a5d550b648afa.txt", "Foo")
   105  	b.AssertFileContent("public/fr/mysect/mybundle/index.html", "Foo: https://example.fr/mysect/mybundle/foo.1cbec737f863e4922cee63cc2ebbfaafcd1cff8b790d8cfd2e6a5d550b648afa.txt|")
   106  
   107  	// Assets CSS fingerprinted
   108  	b.AssertFileContent("public/en/mysect/mybundle/index.html", "CSS: https://example.fr/css/main.5de625c36355cce7c1d5408826a0b21abfb49fb6c0e1f16c945a6f2aef38200c.css|")
   109  	b.AssertFileContent("public/en/css/main.5de625c36355cce7c1d5408826a0b21abfb49fb6c0e1f16c945a6f2aef38200c.css", "body { color: red; }")
   110  	b.AssertFileContent("public/fr/mysect/mybundle/index.html", "CSS: https://example.fr/css/main.5de625c36355cce7c1d5408826a0b21abfb49fb6c0e1f16c945a6f2aef38200c.css|")
   111  	b.AssertFileContent("public/fr/css/main.5de625c36355cce7c1d5408826a0b21abfb49fb6c0e1f16c945a6f2aef38200c.css", "body { color: red; }")
   112  }
   113  
   114  func TestMultihostResourcePerLanguageMultihostMinify(t *testing.T) {
   115  	t.Parallel()
   116  	files := `
   117  -- hugo.toml --
   118  disableKinds = ["taxonomy", "term"]
   119  defaultContentLanguage = "en"
   120  defaultContentLanguageInSubDir = true
   121  [languages]
   122  [languages.en]
   123  baseURL = "https://example.en"
   124  weight = 1
   125  contentDir = "content/en"
   126  [languages.fr]
   127  baseURL = "https://example.fr"
   128  weight = 2
   129  contentDir = "content/fr"
   130  -- content/en/section/mybundle/index.md --
   131  ---
   132  title: "Mybundle en"
   133  ---
   134  -- content/fr/section/mybundle/index.md --
   135  ---
   136  title: "Mybundle fr"
   137  ---
   138  -- content/en/section/mybundle/styles.css --
   139  .body {
   140  	color: english;
   141  }
   142  -- content/fr/section/mybundle/styles.css --
   143  .body {
   144  	color: french;
   145  }
   146  -- layouts/_default/single.html --
   147  {{ $data := .Resources.GetMatch "styles*" | minify }}
   148  {{ .Lang }}: {{ $data.Content}}|{{ $data.RelPermalink }}|
   149  
   150  `
   151  	b := Test(t, files)
   152  
   153  	b.AssertFileContent("public/fr/section/mybundle/index.html",
   154  		"fr: .body{color:french}|/section/mybundle/styles.min.css|",
   155  	)
   156  
   157  	b.AssertFileContent("public/en/section/mybundle/index.html",
   158  		"en: .body{color:english}|/section/mybundle/styles.min.css|",
   159  	)
   160  
   161  	b.AssertFileContent("public/en/section/mybundle/styles.min.css", ".body{color:english}")
   162  	b.AssertFileContent("public/fr/section/mybundle/styles.min.css", ".body{color:french}")
   163  }
   164  
   165  func TestResourcePerLanguageIssue12163(t *testing.T) {
   166  	files := `
   167  -- hugo.toml --
   168  defaultContentLanguage = 'de'
   169  disableKinds = ['rss','sitemap','taxonomy','term']
   170  
   171  [languages.de]
   172  baseURL = 'https://de.example.org/'
   173  contentDir = 'content/de'
   174  weight = 1
   175  
   176  [languages.en]
   177  baseURL = 'https://en.example.org/'
   178  contentDir = 'content/en'
   179  weight = 2
   180  -- content/de/mybundle/index.md --
   181  ---
   182  title: mybundle-de
   183  ---
   184  -- content/de/mybundle/pixel.png --
   185  iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
   186  -- content/en/mybundle/index.md --
   187  ---
   188  title: mybundle-en
   189  ---
   190  -- layouts/_default/single.html --
   191  {{ with .Resources.Get "pixel.png" }}
   192    {{ with .Resize "2x2" }}
   193      {{ .RelPermalink }}|
   194    {{ end }}
   195  {{ end }}
   196  `
   197  
   198  	b := Test(t, files)
   199  
   200  	b.AssertFileExists("public/de/mybundle/index.html", true)
   201  	b.AssertFileExists("public/en/mybundle/index.html", true)
   202  
   203  	b.AssertFileExists("public/de/mybundle/pixel.png", true)
   204  	b.AssertFileExists("public/en/mybundle/pixel.png", true)
   205  
   206  	b.AssertFileExists("public/de/mybundle/pixel_hu8aa3346827e49d756ff4e630147c42b5_70_2x2_resize_box_3.png", true)
   207  	// failing test below
   208  	b.AssertFileExists("public/en/mybundle/pixel_hu8aa3346827e49d756ff4e630147c42b5_70_2x2_resize_box_3.png", true)
   209  }
   210  
   211  func TestMultihostResourceOneBaseURLWithSuPath(t *testing.T) {
   212  	files := `
   213  -- hugo.toml --
   214  defaultContentLanguage = "en"
   215  [languages]
   216  [languages.en]
   217  baseURL = "https://example.com/docs"
   218  weight = 1
   219  contentDir = "content/en"
   220  [languages.en.permalinks]
   221  section = "/enpages/:slug/"
   222  [languages.fr]
   223  baseURL = "https://example.fr"
   224  contentDir = "content/fr"
   225  -- content/en/section/mybundle/index.md --
   226  ---
   227  title: "Mybundle en"
   228  ---
   229  -- content/fr/section/mybundle/index.md --
   230  ---
   231  title: "Mybundle fr"
   232  ---
   233  -- content/fr/section/mybundle/file1.txt --
   234  File 1 fr.
   235  -- content/en/section/mybundle/file1.txt --
   236  File 1 en.
   237  -- content/en/section/mybundle/file2.txt --
   238  File 2 en.
   239  -- layouts/_default/single.html --
   240  {{ $files := .Resources.Match "file*" }}
   241  Files: {{ range $files }}{{ .Permalink }}|{{ end }}$
   242  
   243  `
   244  
   245  	b := Test(t, files)
   246  
   247  	b.AssertFileContent("public/en/enpages/mybundle-en/index.html", "Files: https://example.com/docs/enpages/mybundle-en/file1.txt|https://example.com/docs/enpages/mybundle-en/file2.txt|$")
   248  	b.AssertFileContent("public/fr/section/mybundle/index.html", "Files: https://example.fr/section/mybundle/file1.txt|https://example.fr/section/mybundle/file2.txt|$")
   249  
   250  	b.AssertFileContent("public/en/enpages/mybundle-en/file1.txt", "File 1 en.")
   251  	b.AssertFileContent("public/fr/section/mybundle/file1.txt", "File 1 fr.")
   252  	b.AssertFileContent("public/en/enpages/mybundle-en/file2.txt", "File 2 en.")
   253  	b.AssertFileContent("public/fr/section/mybundle/file2.txt", "File 2 en.")
   254  }