diff options
36 files changed, 112 insertions, 191 deletions
diff --git a/cache/filecache/filecache.go b/cache/filecache/filecache.go index 63d939ef6..8723c4bdc 100644 --- a/cache/filecache/filecache.go +++ b/cache/filecache/filecache.go @@ -17,7 +17,6 @@ import ( "bytes" "errors" "io" - "io/ioutil" "os" "path/filepath" "strings" @@ -207,7 +206,7 @@ func (c *Cache) GetOrCreateBytes(id string, create func() ([]byte, error)) (Item if r := c.getOrRemove(id); r != nil { defer r.Close() - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) return info, b, err } @@ -242,7 +241,7 @@ func (c *Cache) GetBytes(id string) (ItemInfo, []byte, error) { if r := c.getOrRemove(id); r != nil { defer r.Close() - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) return info, b, err } @@ -314,7 +313,7 @@ func (c *Cache) getString(id string) string { } defer f.Close() - b, _ := ioutil.ReadAll(f) + b, _ := io.ReadAll(f) return string(b) } diff --git a/cache/filecache/filecache_test.go b/cache/filecache/filecache_test.go index 47b5a7fcf..6b96a8601 100644 --- a/cache/filecache/filecache_test.go +++ b/cache/filecache/filecache_test.go @@ -17,8 +17,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" - "os" "path/filepath" "strings" "sync" @@ -44,13 +42,8 @@ func TestFileCache(t *testing.T) { t.Parallel() c := qt.New(t) - tempWorkingDir, err := ioutil.TempDir("", "hugo_filecache_test_work") - c.Assert(err, qt.IsNil) - defer os.Remove(tempWorkingDir) - - tempCacheDir, err := ioutil.TempDir("", "hugo_filecache_test_cache") - c.Assert(err, qt.IsNil) - defer os.Remove(tempCacheDir) + tempWorkingDir := t.TempDir() + tempCacheDir := t.TempDir() osfs := afero.NewOsFs() @@ -123,7 +116,7 @@ dir = ":cacheDir/c" io.Closer }{ strings.NewReader(s), - ioutil.NopCloser(nil), + io.NopCloser(nil), }, nil } } @@ -138,7 +131,7 @@ dir = ":cacheDir/c" c.Assert(err, qt.IsNil) c.Assert(r, qt.Not(qt.IsNil)) c.Assert(info.Name, qt.Equals, "a") - b, _ := ioutil.ReadAll(r) + b, _ := io.ReadAll(r) r.Close() c.Assert(string(b), qt.Equals, "abc") @@ -154,7 +147,7 @@ dir = ":cacheDir/c" _, r, err = ca.GetOrCreate("a", rf("bcd")) c.Assert(err, qt.IsNil) - b, _ = ioutil.ReadAll(r) + b, _ = io.ReadAll(r) r.Close() c.Assert(string(b), qt.Equals, "abc") } @@ -173,7 +166,7 @@ dir = ":cacheDir/c" c.Assert(err, qt.IsNil) c.Assert(r, qt.Not(qt.IsNil)) c.Assert(info.Name, qt.Equals, "mykey") - b, _ := ioutil.ReadAll(r) + b, _ := io.ReadAll(r) r.Close() c.Assert(string(b), qt.Equals, "Hugo is great!") @@ -233,7 +226,7 @@ dir = "/cache/c" return hugio.ToReadCloser(strings.NewReader(data)), nil }) c.Assert(err, qt.IsNil) - b, _ := ioutil.ReadAll(r) + b, _ := io.ReadAll(r) r.Close() c.Assert(string(b), qt.Equals, data) // Trigger some expiration. @@ -260,7 +253,7 @@ func TestFileCacheReadOrCreateErrorInRead(t *testing.T) { return errors.New("fail") } - b, _ := ioutil.ReadAll(r) + b, _ := io.ReadAll(r) result = string(b) return nil diff --git a/commands/commandeer.go b/commands/commandeer.go index 80d4c55d0..b77e9e908 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -16,7 +16,7 @@ package commands import ( "errors" "fmt" - "io/ioutil" + "io" "net" "os" "path/filepath" @@ -201,7 +201,7 @@ func newCommandeer(mustHaveConfigFile, failOnInitErr, running bool, h *hugoBuild rebuildDebouncer = debounce.New(4 * time.Second) } - out := ioutil.Discard + out := io.Discard if !h.quiet { out = os.Stdout } @@ -221,7 +221,7 @@ func newCommandeer(mustHaveConfigFile, failOnInitErr, running bool, h *hugoBuild running: running, // This will be replaced later, but we need something to log to before the configuration is read. - logger: loggers.NewLogger(jww.LevelWarn, jww.LevelError, out, ioutil.Discard, running), + logger: loggers.NewLogger(jww.LevelWarn, jww.LevelError, out, io.Discard, running), } return c, c.loadConfig() diff --git a/commands/commands_test.go b/commands/commands_test.go index 8f1ef825d..03b59e19e 100644 --- a/commands/commands_test.go +++ b/commands/commands_test.go @@ -15,7 +15,6 @@ package commands import ( "fmt" - "io/ioutil" "os" "path/filepath" "testing" @@ -402,7 +401,7 @@ PostProcess: {{ $foo.RelPermalink }} func writeFile(t testing.TB, filename, content string) { must(t, os.MkdirAll(filepath.Dir(filename), os.FileMode(0755))) - must(t, ioutil.WriteFile(filename, []byte(content), os.FileMode(0755))) + must(t, os.WriteFile(filename, []byte(content), os.FileMode(0755))) } func must(t testing.TB, err error) { diff --git a/commands/hugo.go b/commands/hugo.go index d2d99c7fa..2fa08ec21 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -18,7 +18,7 @@ package commands import ( "context" "fmt" - "io/ioutil" + "io" "os" "os/signal" "path/filepath" @@ -138,10 +138,10 @@ func initializeConfig(mustHaveConfigFile, failOnInitErr, running bool, func (c *commandeer) createLogger(cfg config.Provider) (loggers.Logger, error) { var ( - logHandle = ioutil.Discard + logHandle = io.Discard logThreshold = jww.LevelWarn logFile = cfg.GetString("logFile") - outHandle = ioutil.Discard + outHandle = io.Discard stdoutThreshold = jww.LevelWarn ) @@ -157,7 +157,7 @@ func (c *commandeer) createLogger(cfg config.Provider) (loggers.Logger, error) { return nil, newSystemError("Failed to open log file:", logFile, err) } } else { - logHandle, err = ioutil.TempFile("", "hugo") + logHandle, err = os.CreateTemp("", "hugo") if err != nil { return nil, newSystemError(err) } diff --git a/commands/import_jekyll.go b/commands/import_jekyll.go index 91d5c69fe..93991121d 100644 --- a/commands/import_jekyll.go +++ b/commands/import_jekyll.go @@ -17,7 +17,7 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" + "io" "os" "path/filepath" "regexp" @@ -164,7 +164,7 @@ func (i *importCmd) importFromJekyll(cmd *cobra.Command, args []string) error { func (i *importCmd) getJekyllDirInfo(fs afero.Fs, jekyllRoot string) (map[string]bool, bool) { postDirs := make(map[string]bool) hasAnyPost := false - if entries, err := ioutil.ReadDir(jekyllRoot); err == nil { + if entries, err := os.ReadDir(jekyllRoot); err == nil { for _, entry := range entries { if entry.IsDir() { subDir := filepath.Join(jekyllRoot, entry.Name()) @@ -186,7 +186,7 @@ func (i *importCmd) retrieveJekyllPostDir(fs afero.Fs, dir string) (bool, bool) return true, !isEmpty } - if entries, err := ioutil.ReadDir(dir); err == nil { + if entries, err := os.ReadDir(dir); err == nil { for _, entry := range entries { if entry.IsDir() { subDir := filepath.Join(dir, entry.Name()) @@ -247,7 +247,7 @@ func (i *importCmd) loadJekyllConfig(fs afero.Fs, jekyllRoot string) map[string] defer f.Close() - b, err := ioutil.ReadAll(f) + b, err := io.ReadAll(f) if err != nil { return nil } @@ -310,7 +310,7 @@ func (i *importCmd) copyJekyllFilesAndFolders(jekyllRoot, dest string, jekyllPos if err != nil { return err } - entries, err := ioutil.ReadDir(jekyllRoot) + entries, err := os.ReadDir(jekyllRoot) if err != nil { return err } @@ -386,7 +386,7 @@ func convertJekyllPost(path, relPath, targetDir string, draft bool) error { targetParentDir := filepath.Dir(targetFile) os.MkdirAll(targetParentDir, 0777) - contentBytes, err := ioutil.ReadFile(path) + contentBytes, err := os.ReadFile(path) if err != nil { jww.ERROR.Println("Read file error:", path) return err diff --git a/common/herrors/error_locator.go b/common/herrors/error_locator.go index 18c21e51b..c7e2d2f06 100644 --- a/common/herrors/error_locator.go +++ b/common/herrors/error_locator.go @@ -16,7 +16,6 @@ package herrors import ( "io" - "io/ioutil" "path/filepath" "strings" @@ -114,7 +113,7 @@ func locateError(r io.Reader, le FileError, matches LineMatcherFn) *ErrorContext ectx := &ErrorContext{LinesPos: -1, Position: text.Position{Offset: -1}} - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { return ectx } diff --git a/common/hugio/writers.go b/common/hugio/writers.go index e600c1ebb..729b21715 100644 --- a/common/hugio/writers.go +++ b/common/hugio/writers.go @@ -15,7 +15,6 @@ package hugio import ( "io" - "io/ioutil" ) // As implemented by strings.Builder. @@ -63,7 +62,7 @@ func ToWriteCloser(w io.Writer) io.WriteCloser { io.Closer }{ w, - ioutil.NopCloser(nil), + io.NopCloser(nil), } } @@ -79,6 +78,6 @@ func ToReadCloser(r io.Reader) io.ReadCloser { io.Closer }{ r, - ioutil.NopCloser(nil), + io.NopCloser(nil), } } diff --git a/common/loggers/loggers.go b/common/loggers/loggers.go index 308635fe9..2c331352a 100644 --- a/common/loggers/loggers.go +++ b/common/loggers/loggers.go @@ -17,7 +17,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "log" "os" "regexp" @@ -92,7 +91,7 @@ type logger struct { *jww.Notepad // The writer that represents stdout. - // Will be ioutil.Discard when in quiet mode. + // Will be io.Discard when in quiet mode. out io.Writer logCounters *LogCounters @@ -232,12 +231,12 @@ func NewErrorLogger() Logger { // NewBasicLogger creates a new basic logger writing to Stdout. func NewBasicLogger(t jww.Threshold) Logger { - return newLogger(t, jww.LevelError, os.Stdout, ioutil.Discard, false) + return newLogger(t, jww.LevelError, os.Stdout, io.Discard, false) } // NewBasicLoggerForWriter creates a new basic logger writing to w. func NewBasicLoggerForWriter(t jww.Threshold, w io.Writer) Logger { - return newLogger(t, jww.LevelError, w, ioutil.Discard, false) + return newLogger(t, jww.LevelError, w, io.Discard, false) } // RemoveANSIColours removes all ANSI colours from the given string. @@ -291,7 +290,7 @@ func InitGlobalLogger(stdoutThreshold, logThreshold jww.Threshold, outHandle, lo func getLogWriters(outHandle, logHandle io.Writer) (io.Writer, io.Writer) { isTerm := terminal.PrintANSIColors(os.Stdout) - if logHandle != ioutil.Discard && isTerm { + if logHandle != io.Discard && isTerm { // Remove any Ansi coloring from log output logHandle = ansiCleaner{w: logHandle} } diff --git a/deploy/deploy.go b/deploy/deploy.go index f0a4e0178..5675d7aa0 100644 --- a/deploy/deploy.go +++ b/deploy/deploy.go @@ -24,7 +24,6 @@ import ( "encoding/hex" "fmt" "io" - "io/ioutil" "mime" "os" "path/filepath" @@ -403,7 +402,7 @@ func (lf *localFile) Reader() (io.ReadCloser, error) { // We've got the gzipped contents cached in gzipped. // Note: we can't use lf.gzipped directly as a Reader, since we it discards // data after it is read, and we may read it more than once. - return ioutil.NopCloser(bytes.NewReader(lf.gzipped.Bytes())), nil + return io.NopCloser(bytes.NewReader(lf.gzipped.Bytes())), nil } // Not expected to fail since we did it successfully earlier in newLocalFile, // but could happen due to changes in the underlying filesystem. diff --git a/deploy/deploy_test.go b/deploy/deploy_test.go index a92649b74..5c436abf2 100644 --- a/deploy/deploy_test.go +++ b/deploy/deploy_test.go @@ -23,7 +23,6 @@ import ( "crypto/md5" "fmt" "io" - "io/ioutil" "os" "path" "path/filepath" @@ -407,7 +406,7 @@ func TestLocalFile(t *testing.T) { if err != nil { t.Fatal(err) } - gotContent, err := ioutil.ReadAll(r) + gotContent, err := io.ReadAll(r) if err != nil { t.Fatal(err) } @@ -420,7 +419,7 @@ func TestLocalFile(t *testing.T) { if err != nil { t.Fatal(err) } - gotContent, err = ioutil.ReadAll(r) + gotContent, err = io.ReadAll(r) if err != nil { t.Fatal(err) } @@ -520,47 +519,35 @@ type fsTest struct { // 1. An in-memory afero.Fs paired with an in-memory Go CDK bucket. // 2. A filesystem-based afero.Fs paired with an filesystem-based Go CDK bucket. // It returns the pair of tests and a cleanup function. -func initFsTests() ([]*fsTest, func(), error) { - tmpfsdir, err := ioutil.TempDir("", "fs") - if err != nil { - return nil, nil, err - } - tmpbucketdir, err := ioutil.TempDir("", "bucket") - if err != nil { - return nil, nil, err - } +func initFsTests(t *testing.T) []*fsTest { + t.Helper() + + tmpfsdir := t.TempDir() + tmpbucketdir := t.TempDir() memfs := afero.NewMemMapFs() membucket := memblob.OpenBucket(nil) + t.Cleanup(func() { membucket.Close() }) filefs := afero.NewBasePathFs(afero.NewOsFs(), tmpfsdir) filebucket, err := fileblob.OpenBucket(tmpbucketdir, nil) if err != nil { - return nil, nil, err + t.Fatal(err) } + t.Cleanup(func() { filebucket.Close() }) tests := []*fsTest{ {"mem", memfs, membucket}, {"file", filefs, filebucket}, } - cleanup := func() { - membucket.Close() - filebucket.Close() - os.RemoveAll(tmpfsdir) - os.RemoveAll(tmpbucketdir) - } - return tests, cleanup, nil + return tests } // TestEndToEndSync verifies that basic adds, updates, and deletes are working // correctly. func TestEndToEndSync(t *testing.T) { ctx := context.Background() - tests, cleanup, err := initFsTests() - if err != nil { - t.Fatal(err) - } - defer cleanup() + tests := initFsTests(t) for _, test := range tests { t.Run(test.name, func(t *testing.T) { local, err := initLocalFs(ctx, test.fs) @@ -643,11 +630,7 @@ func TestEndToEndSync(t *testing.T) { // TestMaxDeletes verifies that the "maxDeletes" flag is working correctly. func TestMaxDeletes(t *testing.T) { ctx := context.Background() - tests, cleanup, err := initFsTests() - if err != nil { - t.Fatal(err) - } - defer cleanup() + tests := initFsTests(t) for _, test := range tests { t.Run(test.name, func(t *testing.T) { local, err := initLocalFs(ctx, test.fs) @@ -772,14 +755,10 @@ func TestIncludeExclude(t *testing.T) { } for _, test := range tests { t.Run(fmt.Sprintf("include %q exclude %q", test.Include, test.Exclude), func(t *testing.T) { - fsTests, cleanup, err := initFsTests() - if err != nil { - t.Fatal(err) - } - defer cleanup() + fsTests := initFsTests(t) fsTest := fsTests[1] // just do file-based test - _, err = initLocalFs(ctx, fsTest.fs) + _, err := initLocalFs(ctx, fsTest.fs) if err != nil { t.Fatal(err) } @@ -841,11 +820,7 @@ func TestIncludeExcludeRemoteDelete(t *testing.T) { } for _, test := range tests { t.Run(fmt.Sprintf("include %q exclude %q", test.Include, test.Exclude), func(t *testing.T) { - fsTests, cleanup, err := initFsTests() - if err != nil { - t.Fatal(err) - } - defer cleanup() + fsTests := initFsTests(t) fsTest := fsTests[1] // just do file-based test local, err := initLocalFs(ctx, fsTest.fs) @@ -897,11 +872,7 @@ func TestIncludeExcludeRemoteDelete(t *testing.T) { func TestCompression(t *testing.T) { ctx := context.Background() - tests, cleanup, err := initFsTests() - if err != nil { - t.Fatal(err) - } - defer cleanup() + tests := initFsTests(t) for _, test := range tests { t.Run(test.name, func(t *testing.T) { local, err := initLocalFs(ctx, test.fs) @@ -956,11 +927,7 @@ func TestCompression(t *testing.T) { // attribute for matcher works. func TestMatching(t *testing.T) { ctx := context.Background() - tests, cleanup, err := initFsTests() - if err != nil { - t.Fatal(err) - } - defer cleanup() + tests := initFsTests(t) for _, test := range tests { t.Run(test.name, func(t *testing.T) { _, err := initLocalFs(ctx, test.fs) diff --git a/helpers/path_test.go b/helpers/path_test.go index b8739f37a..1f206a881 100644 --- a/helpers/path_test.go +++ b/helpers/path_test.go @@ -15,7 +15,6 @@ package helpers import ( "fmt" - "io/ioutil" "os" "path/filepath" "reflect" @@ -259,7 +258,7 @@ func TestIsDir(t *testing.T) { func createZeroSizedFileInTempDir() (*os.File, error) { filePrefix := "_path_test_" - f, e := ioutil.TempFile("", filePrefix) // dir is os.TempDir() + f, e := os.CreateTemp("", filePrefix) // dir is os.TempDir() if e != nil { // if there was an error no file was created. // => no requirement to delete the file @@ -275,7 +274,7 @@ func createNonZeroSizedFileInTempDir() (*os.File, error) { return nil, err } byteString := []byte("byteString") - err = ioutil.WriteFile(f.Name(), byteString, 0644) + err = os.WriteFile(f.Name(), byteString, 0644) if err != nil { // delete the file deleteFileInTempDir(f) @@ -288,27 +287,12 @@ func deleteFileInTempDir(f *os.File) { _ = os.Remove(f.Name()) } -func createEmptyTempDir() (string, error) { - dirPrefix := "_dir_prefix_" - d, e := ioutil.TempDir("", dirPrefix) // will be in os.TempDir() - if e != nil { - // no directory to delete - it was never created - return "", e - } - return d, nil -} - -func deleteTempDir(d string) { - _ = os.RemoveAll(d) -} - func TestExists(t *testing.T) { zeroSizedFile, _ := createZeroSizedFileInTempDir() defer deleteFileInTempDir(zeroSizedFile) nonZeroSizedFile, _ := createNonZeroSizedFileInTempDir() defer deleteFileInTempDir(nonZeroSizedFile) - emptyDirectory, _ := createEmptyTempDir() - defer deleteTempDir(emptyDirectory) + emptyDirectory := t.TempDir() nonExistentFile := os.TempDir() + "/this-file-does-not-exist.txt" nonExistentDir := os.TempDir() + "/this/directory/does/not/exist/" @@ -455,8 +439,7 @@ func TestFindCWD(t *testing.T) { func TestSafeWriteToDisk(t *testing.T) { emptyFile, _ := createZeroSizedFileInTempDir() defer deleteFileInTempDir(emptyFile) - tmpDir, _ := createEmptyTempDir() - defer deleteTempDir(tmpDir) + tmpDir := t.TempDir() randomString := "This is a random string!" reader := strings.NewReader(randomString) @@ -485,7 +468,7 @@ func TestSafeWriteToDisk(t *testing.T) { if d.expectedErr != e { t.Errorf("Test %d failed. Expected %q but got %q", i, d.expectedErr, e) } - contents, _ := ioutil.ReadFile(d.filename) + contents, _ := os.ReadFile(d.filename) if randomString != string(contents) { t.Err |