Skip to content

Commit

Permalink
refactor: move from io/ioutil to io and os package (#17109)
Browse files Browse the repository at this point in the history
The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <[email protected]>

Co-authored-by: techknowlogick <[email protected]>
  • Loading branch information
Juneezee and techknowlogick authored Sep 22, 2021
1 parent aa631d8 commit f2e7d54
Show file tree
Hide file tree
Showing 103 changed files with 261 additions and 314 deletions.
5 changes: 2 additions & 3 deletions build/generate-bindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"bytes"
"crypto/sha1"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
Expand All @@ -28,7 +27,7 @@ func needsUpdate(dir string, filename string) (bool, []byte) {
needRegen = true
}

oldHash, err := ioutil.ReadFile(filename + ".hash")
oldHash, err := os.ReadFile(filename + ".hash")
if err != nil {
oldHash = []byte{}
}
Expand Down Expand Up @@ -83,5 +82,5 @@ func main() {
if err != nil {
log.Fatalf("%v\n", err)
}
_ = ioutil.WriteFile(filename+".hash", newHash, 0666)
_ = os.WriteFile(filename+".hash", newHash, 0666)
}
9 changes: 5 additions & 4 deletions build/generate-emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
"flag"
"fmt"
"go/format"
"io/ioutil"
"io"
"log"
"net/http"
"os"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -67,7 +68,7 @@ func main() {
}

// write
err = ioutil.WriteFile(*flagOut, buf, 0644)
err = os.WriteFile(*flagOut, buf, 0644)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -96,7 +97,7 @@ func generate() ([]byte, error) {
defer res.Body.Close()

// read all
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -157,7 +158,7 @@ func generate() ([]byte, error) {

// write a JSON file to use with tribute (write before adding skin tones since we can't support them there yet)
file, _ := json.Marshal(data)
_ = ioutil.WriteFile("assets/emoji.json", file, 0644)
_ = os.WriteFile("assets/emoji.json", file, 0644)

// Add skin tones to emoji that support it
var (
Expand Down
7 changes: 3 additions & 4 deletions build/generate-gitignores.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand All @@ -34,7 +33,7 @@ func main() {
flag.StringVar(&githubApiToken, "token", "", "github api token")
flag.Parse()

file, err := ioutil.TempFile(os.TempDir(), prefix)
file, err := os.CreateTemp(os.TempDir(), prefix)

if err != nil {
log.Fatalf("Failed to create temp file. %s", err)
Expand Down Expand Up @@ -114,13 +113,13 @@ func main() {
for dst, src := range filesToCopy {
// Read all content of src to data
src = path.Join(destination, src)
data, err := ioutil.ReadFile(src)
data, err := os.ReadFile(src)
if err != nil {
log.Fatalf("Failed to read src file. %s", err)
}
// Write data to dst
dst = path.Join(destination, dst)
err = ioutil.WriteFile(dst, data, 0644)
err = os.WriteFile(dst, data, 0644)
if err != nil {
log.Fatalf("Failed to write new file. %s", err)
}
Expand Down
3 changes: 1 addition & 2 deletions build/generate-licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand All @@ -34,7 +33,7 @@ func main() {
flag.StringVar(&githubApiToken, "token", "", "github api token")
flag.Parse()

file, err := ioutil.TempFile(os.TempDir(), prefix)
file, err := os.CreateTemp(os.TempDir(), prefix)

if err != nil {
log.Fatalf("Failed to create temp file. %s", err)
Expand Down
3 changes: 1 addition & 2 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -247,7 +246,7 @@ func runDump(ctx *cli.Context) error {
fatal("Path does not exist: %s", tmpDir)
}

dbDump, err := ioutil.TempFile(tmpDir, "gitea-db.sql")
dbDump, err := os.CreateTemp(tmpDir, "gitea-db.sql")
if err != nil {
fatal("Failed to create tmp file: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions contrib/fixtures/fixture_generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -65,7 +64,7 @@ func generate(name string) error {
return err
}
path := filepath.Join(fixturesDir, name+".yml")
if err := ioutil.WriteFile(path, []byte(data), 0644); err != nil {
if err := os.WriteFile(path, []byte(data), 0644); err != nil {
return fmt.Errorf("%s: %+v", path, err)
}
fmt.Printf("%s created.\n", path)
Expand Down
9 changes: 4 additions & 5 deletions contrib/pr/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -52,11 +51,11 @@ func runPR() {
setting.SetCustomPathAndConf("", "", "")
setting.NewContext()

setting.RepoRootPath, err = ioutil.TempDir(os.TempDir(), "repos")
setting.RepoRootPath, err = os.MkdirTemp(os.TempDir(), "repos")
if err != nil {
log.Fatalf("TempDir: %v\n", err)
}
setting.AppDataPath, err = ioutil.TempDir(os.TempDir(), "appdata")
setting.AppDataPath, err = os.MkdirTemp(os.TempDir(), "appdata")
if err != nil {
log.Fatalf("TempDir: %v\n", err)
}
Expand Down Expand Up @@ -181,7 +180,7 @@ func main() {
codeFilePath = filepath.FromSlash(codeFilePath) //Convert to running OS

//Copy this file if it will not exist in the PR branch
dat, err := ioutil.ReadFile(codeFilePath)
dat, err := os.ReadFile(codeFilePath)
if err != nil {
log.Fatalf("Failed to cache this code file : %v", err)
}
Expand Down Expand Up @@ -245,7 +244,7 @@ func main() {
if err != nil {
log.Fatalf("Failed to duplicate this code file in PR : %v", err)
}
err = ioutil.WriteFile(codeFilePath, dat, 0644)
err = os.WriteFile(codeFilePath, dat, 0644)
if err != nil {
log.Fatalf("Failed to duplicate this code file in PR : %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions integrations/api_helper_for_declarative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package integrations
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"testing"
"time"

Expand Down Expand Up @@ -163,7 +163,7 @@ func doAPICreateUserKey(ctx APITestContext, keyname, keyFile string, callback ..
return func(t *testing.T) {
urlStr := fmt.Sprintf("/api/v1/user/keys?token=%s", ctx.Token)

dataPubKey, err := ioutil.ReadFile(keyFile + ".pub")
dataPubKey, err := os.ReadFile(keyFile + ".pub")
assert.NoError(t, err)
req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateKeyOption{
Title: keyname,
Expand Down Expand Up @@ -199,7 +199,7 @@ func doAPICreateDeployKey(ctx APITestContext, keyname, keyFile string, readOnly
return func(t *testing.T) {
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/keys?token=%s", ctx.Username, ctx.Reponame, ctx.Token)

dataPubKey, err := ioutil.ReadFile(keyFile + ".pub")
dataPubKey, err := os.ReadFile(keyFile + ".pub")
assert.NoError(t, err)
req := NewRequestWithJSON(t, "POST", urlStr, api.CreateKeyOption{
Title: keyname,
Expand Down
6 changes: 3 additions & 3 deletions integrations/api_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package integrations

import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"testing"

"code.gitea.io/gitea/models"
Expand Down Expand Up @@ -356,7 +356,7 @@ func testAPIRepoMigrateConflict(t *testing.T, u *url.URL) {
httpContext := baseAPITestContext

httpContext.Reponame = "repo-tmp-17"
dstPath, err := ioutil.TempDir("", httpContext.Reponame)
dstPath, err := os.MkdirTemp("", httpContext.Reponame)
assert.NoError(t, err)
defer util.RemoveAll(dstPath)
t.Run("CreateRepo", doAPICreateRepository(httpContext, false))
Expand Down Expand Up @@ -419,7 +419,7 @@ func testAPIRepoCreateConflict(t *testing.T, u *url.URL) {
httpContext := baseAPITestContext

httpContext.Reponame = "repo-tmp-17"
dstPath, err := ioutil.TempDir("", httpContext.Reponame)
dstPath, err := os.MkdirTemp("", httpContext.Reponame)
assert.NoError(t, err)
defer util.RemoveAll(dstPath)
t.Run("CreateRepo", doAPICreateRepository(httpContext, false))
Expand Down
3 changes: 1 addition & 2 deletions integrations/create_no_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package integrations

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -69,7 +68,7 @@ func TestSessionFileCreation(t *testing.T) {
config.Provider = "file"

// Now create a temporaryDirectory
tmpDir, err := ioutil.TempDir("", "sessions")
tmpDir, err := os.MkdirTemp("", "sessions")
assert.NoError(t, err)
defer func() {
if _, err := os.Stat(tmpDir); !os.IsNotExist(err) {
Expand Down
6 changes: 3 additions & 3 deletions integrations/git_clone_wiki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package integrations
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"testing"

Expand All @@ -24,7 +24,7 @@ func assertFileExist(t *testing.T, p string) {
}

func assertFileEqual(t *testing.T, p string, content []byte) {
bs, err := ioutil.ReadFile(p)
bs, err := os.ReadFile(p)
assert.NoError(t, err)
assert.EqualValues(t, content, bs)
}
Expand All @@ -33,7 +33,7 @@ func TestRepoCloneWiki(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
defer prepareTestEnv(t)()

dstPath, err := ioutil.TempDir("", "clone_wiki")
dstPath, err := os.MkdirTemp("", "clone_wiki")
assert.NoError(t, err)

r := fmt.Sprintf("%suser2/repo1.wiki.git", u.String())
Expand Down
9 changes: 4 additions & 5 deletions integrations/git_helper_for_declarative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package integrations
import (
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand All @@ -28,7 +27,7 @@ import (

func withKeyFile(t *testing.T, keyname string, callback func(string)) {

tmpDir, err := ioutil.TempDir("", "key-file")
tmpDir, err := os.MkdirTemp("", "key-file")
assert.NoError(t, err)
defer util.RemoveAll(tmpDir)

Expand All @@ -39,7 +38,7 @@ func withKeyFile(t *testing.T, keyname string, callback func(string)) {
err = ssh.GenKeyPair(keyFile)
assert.NoError(t, err)

err = ioutil.WriteFile(path.Join(tmpDir, "ssh"), []byte("#!/bin/bash\n"+
err = os.WriteFile(path.Join(tmpDir, "ssh"), []byte("#!/bin/bash\n"+
"ssh -o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\" -o \"IdentitiesOnly=yes\" -i \""+keyFile+"\" \"$@\""), 0700)
assert.NoError(t, err)

Expand Down Expand Up @@ -125,7 +124,7 @@ func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {

func doGitCloneFail(u *url.URL) func(*testing.T) {
return func(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "doGitCloneFail")
tmpDir, err := os.MkdirTemp("", "doGitCloneFail")
assert.NoError(t, err)
defer util.RemoveAll(tmpDir)
assert.Error(t, git.Clone(u.String(), tmpDir, git.CloneRepoOptions{}))
Expand All @@ -139,7 +138,7 @@ func doGitInitTestRepository(dstPath string) func(*testing.T) {
return func(t *testing.T) {
// Init repository in dstPath
assert.NoError(t, git.InitRepository(dstPath, false))
assert.NoError(t, ioutil.WriteFile(filepath.Join(dstPath, "README.md"), []byte(fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s", dstPath)), 0644))
assert.NoError(t, os.WriteFile(filepath.Join(dstPath, "README.md"), []byte(fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s", dstPath)), 0644))
assert.NoError(t, git.AddChanges(dstPath, true))
signature := git.Signature{
Email: "[email protected]",
Expand Down
4 changes: 2 additions & 2 deletions integrations/git_smart_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package integrations

import (
"io/ioutil"
"io"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -62,7 +62,7 @@ func testGitSmartHTTP(t *testing.T, u *url.URL) {
assert.NoError(t, err)
defer resp.Body.Close()
assert.EqualValues(t, kase.code, resp.StatusCode)
_, err = ioutil.ReadAll(resp.Body)
_, err = io.ReadAll(resp.Body)
assert.NoError(t, err)
})
}
Expand Down
Loading

0 comments on commit f2e7d54

Please sign in to comment.