2016-03-21 19:28:42 -04:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2014-12-26 22:40:10 -05:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-12-26 22:40:10 -05:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-12-26 22:40:10 -05:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
package data
|
2014-12-26 22:40:10 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2023-01-04 12:24:36 -05:00
|
|
|
"path/filepath"
|
2017-03-31 02:54:22 -04:00
|
|
|
"sync"
|
2014-12-26 22:40:10 -05:00
|
|
|
"testing"
|
2017-03-31 02:54:22 -04:00
|
|
|
"time"
|
2014-12-26 22:40:10 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/gohugoio/hugo/config/testconfig"
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
|
2018-11-23 03:16:42 -05:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2018-11-08 04:24:13 -05:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2018-11-08 04:24:13 -05:00
|
|
|
"github.com/gohugoio/hugo/cache/filecache"
|
2018-09-10 15:02:18 -04:00
|
|
|
"github.com/gohugoio/hugo/common/loggers"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
"github.com/gohugoio/hugo/deps"
|
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/afero"
|
2014-12-26 22:40:10 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestScpGetLocal(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2023-01-04 12:24:36 -05:00
|
|
|
v := config.New()
|
|
|
|
workingDir := "/my/working/dir"
|
|
|
|
v.Set("workingDir", workingDir)
|
|
|
|
v.Set("publishDir", "public")
|
|
|
|
fs := hugofs.NewFromOld(afero.NewMemMapFs(), v)
|
2014-12-26 22:40:10 -05:00
|
|
|
ps := helpers.FilePathSeparator
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2014-12-26 22:40:10 -05:00
|
|
|
tests := []struct {
|
|
|
|
path string
|
|
|
|
content []byte
|
|
|
|
}{
|
|
|
|
{"testpath" + ps + "test.txt", []byte(`T€st Content 123 fOO,bar:foo%bAR`)},
|
|
|
|
{"FOo" + ps + "BaR.html", []byte(`FOo/BaR.html T€st Content 123`)},
|
|
|
|
{"трям" + ps + "трям", []byte(`T€st трям/трям Content 123`)},
|
|
|
|
{"은행", []byte(`T€st C은행ontent 123`)},
|
|
|
|
{"Банковский кассир", []byte(`Банковский кассир T€st Content 123`)},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
r := bytes.NewReader(test.content)
|
2023-01-04 12:24:36 -05:00
|
|
|
err := helpers.WriteToDisk(filepath.Join(workingDir, test.path), r, fs.Source)
|
2014-12-26 22:40:10 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
c, err := getLocal(workingDir, test.path, fs.Source)
|
2014-12-26 22:40:10 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error getting resource content: %s", err)
|
|
|
|
}
|
2016-11-23 12:53:50 -05:00
|
|
|
if !bytes.Equal(c, test.content) {
|
2014-12-26 22:40:10 -05:00
|
|
|
t.Errorf("\nExpected: %s\nActual: %s\n", string(test.content), string(c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httptest.Server, *http.Client) {
|
|
|
|
testServer := httptest.NewServer(http.HandlerFunc(handler))
|
|
|
|
client := &http.Client{
|
2015-12-18 09:52:17 -05:00
|
|
|
Transport: &http.Transport{Proxy: func(r *http.Request) (*url.URL, error) {
|
2015-12-18 17:23:37 -05:00
|
|
|
// Remove when https://github.com/golang/go/issues/13686 is fixed
|
2015-12-18 17:09:50 -05:00
|
|
|
r.Host = "gohugo.io"
|
2015-12-18 09:52:17 -05:00
|
|
|
return url.Parse(testServer.URL)
|
|
|
|
}},
|
2014-12-26 22:40:10 -05:00
|
|
|
}
|
|
|
|
return testServer, client
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScpGetRemote(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2014-12-26 22:40:10 -05:00
|
|
|
fs := new(afero.MemMapFs)
|
2021-12-10 03:55:41 -05:00
|
|
|
cache := filecache.NewCache(fs, 100, "")
|
2014-12-26 22:40:10 -05:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
path string
|
|
|
|
content []byte
|
|
|
|
}{
|
2018-11-08 04:24:13 -05:00
|
|
|
{"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`)},
|
|
|
|
{"http://Doppel.Gänger/foo_Bar-Foo", []byte(`T€st Cont€nt 123`)},
|
|
|
|
{"http://Doppel.Gänger/Fizz_Bazz-Foo", []byte(`T€st Банковский кассир Cont€nt 123`)},
|
|
|
|
{"http://Doppel.Gänger/Fizz_Bazz-Bar", []byte(`T€st Банковский кассир Cont€nt 456`)},
|
2014-12-26 22:40:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2019-08-10 15:05:17 -04:00
|
|
|
msg := qt.Commentf("%v", test)
|
2017-05-01 23:41:08 -04:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", test.path, nil)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil, msg)
|
2014-12-26 22:40:10 -05:00
|
|
|
|
|
|
|
srv, cl := getTestServer(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write(test.content)
|
|
|
|
})
|
|
|
|
defer func() { srv.Close() }()
|
|
|
|
|
2018-11-08 04:24:13 -05:00
|
|
|
ns := newTestNs()
|
|
|
|
ns.client = cl
|
2017-02-04 22:20:06 -05:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
var cb []byte
|
2019-03-24 05:11:16 -04:00
|
|
|
f := func(b []byte) (bool, error) {
|
2019-08-10 15:05:17 -04:00
|
|
|
cb = b
|
2019-03-24 05:11:16 -04:00
|
|
|
return false, nil
|
2018-11-08 04:24:13 -05:00
|
|
|
}
|
2017-05-01 23:41:08 -04:00
|
|
|
|
2018-11-08 04:24:13 -05:00
|
|
|
err = ns.getRemote(cache, f, req)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil, msg)
|
|
|
|
c.Assert(string(cb), qt.Equals, string(test.content))
|
2017-05-01 23:41:08 -04:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(string(cb), qt.Equals, string(test.content))
|
2017-05-01 23:41:08 -04:00
|
|
|
|
2014-12-26 22:40:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 02:54:22 -04:00
|
|
|
func TestScpGetRemoteParallel(t *testing.T) {
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2017-05-01 23:41:08 -04:00
|
|
|
|
2017-03-31 02:54:22 -04:00
|
|
|
content := []byte(`T€st Content 123`)
|
|
|
|
srv, cl := getTestServer(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write(content)
|
|
|
|
})
|
2018-11-08 04:24:13 -05:00
|
|
|
|
2017-03-31 02:54:22 -04:00
|
|
|
defer func() { srv.Close() }()
|
|
|
|
|
2017-05-01 23:41:08 -04:00
|
|
|
url := "http://Foo.Bar/foo_Bar-Foo"
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2017-03-31 02:54:22 -04:00
|
|
|
|
2018-11-08 04:24:13 -05:00
|
|
|
for _, ignoreCache := range []bool{false} {
|
2023-01-04 12:24:36 -05:00
|
|
|
cfg := config.New()
|
2017-03-31 02:54:22 -04:00
|
|
|
cfg.Set("ignoreCache", ignoreCache)
|
2018-11-08 04:24:13 -05:00
|
|
|
|
|
|
|
ns := New(newDeps(cfg))
|
|
|
|
ns.client = cl
|
2017-03-31 02:54:22 -04:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2018-11-08 04:24:13 -05:00
|
|
|
for i := 0; i < 1; i++ {
|
2017-03-31 02:54:22 -04:00
|
|
|
wg.Add(1)
|
|
|
|
go func(gor int) {
|
|
|
|
defer wg.Done()
|
|
|
|
for j := 0; j < 10; j++ {
|
2019-08-10 15:05:17 -04:00
|
|
|
var cb []byte
|
2019-03-24 05:11:16 -04:00
|
|
|
f := func(b []byte) (bool, error) {
|
2019-08-10 15:05:17 -04:00
|
|
|
cb = b
|
2019-03-24 05:11:16 -04:00
|
|
|
return false, nil
|
2018-11-08 04:24:13 -05:00
|
|
|
}
|
|
|
|
err := ns.getRemote(ns.cacheGetJSON, f, req)
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
if string(content) != string(cb) {
|
|
|
|
t.Errorf("expected\n%q\ngot\n%q", content, cb)
|
2018-11-08 04:24:13 -05:00
|
|
|
}
|
2017-03-31 02:54:22 -04:00
|
|
|
|
|
|
|
time.Sleep(23 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2015-05-28 01:36:06 -04:00
|
|
|
}
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
func newDeps(cfg config.Provider) *deps.Deps {
|
2023-01-04 12:24:36 -05:00
|
|
|
conf := testconfig.GetTestConfig(nil, cfg)
|
|
|
|
logger := loggers.NewIgnorableLogger(loggers.NewErrorLogger(), nil)
|
|
|
|
fs := hugofs.NewFrom(afero.NewMemMapFs(), conf.BaseConfig())
|
|
|
|
|
|
|
|
d := &deps.Deps{
|
|
|
|
Fs: fs,
|
|
|
|
Log: logger,
|
|
|
|
Conf: conf,
|
2018-11-23 03:16:42 -05:00
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
if err := d.Init(); err != nil {
|
2018-11-14 06:06:46 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
return d
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
2018-03-21 12:21:46 -04:00
|
|
|
|
|
|
|
func newTestNs() *Namespace {
|
2023-01-04 12:24:36 -05:00
|
|
|
return New(newDeps(config.New()))
|
2018-03-21 12:21:46 -04:00
|
|
|
}
|