1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

refactor: move from io/ioutil to io and os package (#17109)

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 <engzerjun@gmail.com>

Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
Eng Zer Jun
2021-09-22 13:38:34 +08:00
committed by GitHub
parent aa631d8cd1
commit f2e7d5477f
103 changed files with 261 additions and 314 deletions

View File

@@ -7,9 +7,9 @@ package integrations
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"testing"
"time"
@@ -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,
@@ -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,

View File

@@ -6,9 +6,9 @@ package integrations
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"testing"
"code.gitea.io/gitea/models"
@@ -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))
@@ -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))

View File

@@ -5,7 +5,6 @@
package integrations
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
@@ -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) {

View File

@@ -7,8 +7,8 @@ package integrations
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"testing"
@@ -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)
}
@@ -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())

View File

@@ -7,7 +7,6 @@ package integrations
import (
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
@@ -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)
@@ -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)
@@ -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{}))
@@ -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: "test@example.com",

View File

@@ -5,7 +5,7 @@
package integrations
import (
"io/ioutil"
"io"
"net/http"
"net/url"
"testing"
@@ -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)
})
}

View File

@@ -7,10 +7,10 @@ package integrations
import (
"encoding/hex"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
@@ -52,7 +52,7 @@ func testGit(t *testing.T, u *url.URL) {
httpContext.Reponame = "repo-tmp-17"
forkedUserCtx.Reponame = httpContext.Reponame
dstPath, err := ioutil.TempDir("", httpContext.Reponame)
dstPath, err := os.MkdirTemp("", httpContext.Reponame)
assert.NoError(t, err)
defer util.RemoveAll(dstPath)
@@ -102,7 +102,7 @@ func testGit(t *testing.T, u *url.URL) {
sshURL := createSSHUrl(sshContext.GitPath(), u)
//Setup clone folder
dstPath, err := ioutil.TempDir("", sshContext.Reponame)
dstPath, err := os.MkdirTemp("", sshContext.Reponame)
assert.NoError(t, err)
defer util.RemoveAll(dstPath)
@@ -128,7 +128,7 @@ func testGit(t *testing.T, u *url.URL) {
}
func ensureAnonymousClone(t *testing.T, u *url.URL) {
dstLocalPath, err := ioutil.TempDir("", "repo1")
dstLocalPath, err := os.MkdirTemp("", "repo1")
assert.NoError(t, err)
defer util.RemoveAll(dstLocalPath)
t.Run("CloneAnonymous", doGitClone(dstLocalPath, u))
@@ -311,7 +311,7 @@ func generateCommitWithNewData(size int, repoPath, email, fullName, prefix strin
buffer := make([]byte, bufSize)
tmpFile, err := ioutil.TempFile(repoPath, prefix)
tmpFile, err := os.CreateTemp(repoPath, prefix)
if err != nil {
return "", err
}
@@ -558,7 +558,7 @@ func doPushCreate(ctx APITestContext, u *url.URL) func(t *testing.T) {
u.Path = ctx.GitPath()
// Create a temporary directory
tmpDir, err := ioutil.TempDir("", ctx.Reponame)
tmpDir, err := os.MkdirTemp("", ctx.Reponame)
assert.NoError(t, err)
defer util.RemoveAll(tmpDir)
@@ -636,7 +636,7 @@ func doCreateAgitFlowPull(dstPath string, ctx *APITestContext, baseBranch, headB
t.Run("CreateHeadBranch", doGitCreateBranch(dstPath, headBranch))
t.Run("AddCommit", func(t *testing.T) {
err := ioutil.WriteFile(path.Join(dstPath, "test_file"), []byte("## test content"), 0666)
err := os.WriteFile(path.Join(dstPath, "test_file"), []byte("## test content"), 0666)
if !assert.NoError(t, err) {
return
}
@@ -710,7 +710,7 @@ func doCreateAgitFlowPull(dstPath string, ctx *APITestContext, baseBranch, headB
}
t.Run("AddCommit2", func(t *testing.T) {
err := ioutil.WriteFile(path.Join(dstPath, "test_file"), []byte("## test content \n ## test content 2"), 0666)
err := os.WriteFile(path.Join(dstPath, "test_file"), []byte("## test content \n ## test content 2"), 0666)
if !assert.NoError(t, err) {
return
}

View File

@@ -7,7 +7,6 @@ package integrations
import (
"encoding/base64"
"fmt"
"io/ioutil"
"net/url"
"os"
"testing"
@@ -28,7 +27,7 @@ func TestGPGGit(t *testing.T) {
username := "user2"
// OK Set a new GPG home
tmpDir, err := ioutil.TempDir("", "temp-gpg")
tmpDir, err := os.MkdirTemp("", "temp-gpg")
assert.NoError(t, err)
defer util.RemoveAll(tmpDir)

View File

@@ -7,7 +7,7 @@ package integrations
import (
"archive/zip"
"bytes"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
@@ -74,7 +74,7 @@ func checkResponseTestContentEncoding(t *testing.T, content *[]byte, resp *httpt
assert.Contains(t, contentEncoding, "gzip")
gzippReader, err := gzipp.NewReader(resp.Body)
assert.NoError(t, err)
result, err := ioutil.ReadAll(gzippReader)
result, err := io.ReadAll(gzippReader)
assert.NoError(t, err)
assert.Equal(t, *content, result)
}

View File

@@ -6,7 +6,6 @@ package integrations
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
@@ -25,14 +24,14 @@ func str2url(raw string) *url.URL {
func TestDetermineLocalEndpoint(t *testing.T) {
defer prepareTestEnv(t)()
root, _ := ioutil.TempDir("", "lfs_test")
root, _ := os.MkdirTemp("", "lfs_test")
defer os.RemoveAll(root)
rootdotgit, _ := ioutil.TempDir("", "lfs_test")
rootdotgit, _ := os.MkdirTemp("", "lfs_test")
defer os.RemoveAll(rootdotgit)
os.Mkdir(filepath.Join(rootdotgit, ".git"), 0700)
lfsroot, _ := ioutil.TempDir("", "lfs_test")
lfsroot, _ := os.MkdirTemp("", "lfs_test")
defer os.RemoveAll(lfsroot)
// Test cases

View File

@@ -5,7 +5,6 @@
package integrations
import (
"io/ioutil"
"os"
"testing"
@@ -25,14 +24,14 @@ func TestMigrateLocalPath(t *testing.T) {
old := setting.ImportLocalPaths
setting.ImportLocalPaths = true
lowercasePath, err := ioutil.TempDir("", "lowercase") // may not be lowercase because TempDir creates a random directory name which may be mixedcase
lowercasePath, err := os.MkdirTemp("", "lowercase") // may not be lowercase because MkdirTemp creates a random directory name which may be mixedcase
assert.NoError(t, err)
defer os.RemoveAll(lowercasePath)
err = migrations.IsMigrateURLAllowed(lowercasePath, adminUser)
assert.NoError(t, err, "case lowercase path")
mixedcasePath, err := ioutil.TempDir("", "mIxeDCaSe")
mixedcasePath, err := os.MkdirTemp("", "mIxeDCaSe")
assert.NoError(t, err)
defer os.RemoveAll(mixedcasePath)

View File

@@ -9,7 +9,7 @@ import (
"context"
"database/sql"
"fmt"
"io/ioutil"
"io"
"os"
"path"
"path/filepath"
@@ -113,7 +113,7 @@ func readSQLFromFile(version string) (string, error) {
}
defer gr.Close()
bytes, err := ioutil.ReadAll(gr)
bytes, err := io.ReadAll(gr)
if err != nil {
return "", err
}

View File

@@ -6,7 +6,7 @@ package integrations
import (
"bytes"
"io/ioutil"
"io"
"testing"
"code.gitea.io/gitea/modules/json"
@@ -240,20 +240,20 @@ func TestRefreshTokenInvalidation(t *testing.T) {
"refresh_token": parsed.RefreshToken,
})
bs, err := ioutil.ReadAll(refreshReq.Body)
bs, err := io.ReadAll(refreshReq.Body)
assert.NoError(t, err)
refreshReq.Body = ioutil.NopCloser(bytes.NewReader(bs))
refreshReq.Body = io.NopCloser(bytes.NewReader(bs))
MakeRequest(t, refreshReq, 200)
refreshReq.Body = ioutil.NopCloser(bytes.NewReader(bs))
refreshReq.Body = io.NopCloser(bytes.NewReader(bs))
MakeRequest(t, refreshReq, 200)
// test with invalidation
setting.OAuth2.InvalidateRefreshTokens = true
refreshReq.Body = ioutil.NopCloser(bytes.NewReader(bs))
refreshReq.Body = io.NopCloser(bytes.NewReader(bs))
MakeRequest(t, refreshReq, 200)
refreshReq.Body = ioutil.NopCloser(bytes.NewReader(bs))
refreshReq.Body = io.NopCloser(bytes.NewReader(bs))
MakeRequest(t, refreshReq, 400)
}

View File

@@ -5,8 +5,8 @@
package integrations
import (
"io/ioutil"
"net/url"
"os"
"testing"
"code.gitea.io/gitea/models"
@@ -55,7 +55,7 @@ func TestCreateNewTagProtected(t *testing.T) {
username := "user2"
httpContext := NewAPITestContext(t, username, "repo1")
dstPath, err := ioutil.TempDir("", httpContext.Reponame)
dstPath, err := os.MkdirTemp("", httpContext.Reponame)
assert.NoError(t, err)
defer util.RemoveAll(dstPath)

View File

@@ -6,9 +6,9 @@ package integrations
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"testing"
"time"
@@ -28,7 +28,7 @@ func doCheckRepositoryEmptyStatus(ctx APITestContext, isEmpty bool) func(*testin
func doAddChangesToCheckout(dstPath, filename string) func(*testing.T) {
return func(t *testing.T) {
assert.NoError(t, ioutil.WriteFile(filepath.Join(dstPath, filename), []byte(fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s at time: %v", dstPath, time.Now())), 0644))
assert.NoError(t, os.WriteFile(filepath.Join(dstPath, filename), []byte(fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s at time: %v", dstPath, time.Now())), 0644))
assert.NoError(t, git.AddChanges(dstPath, true))
signature := git.Signature{
Email: "test@example.com",
@@ -61,7 +61,7 @@ func testPushDeployKeyOnEmptyRepo(t *testing.T, u *url.URL) {
t.Run("CreatePushDeployKey", doAPICreateDeployKey(ctx, keyname, keyFile, false))
// Setup the testing repository
dstPath, err := ioutil.TempDir("", "repo-tmp-deploy-key-empty-repo-1")
dstPath, err := os.MkdirTemp("", "repo-tmp-deploy-key-empty-repo-1")
assert.NoError(t, err)
defer util.RemoveAll(dstPath)
@@ -107,7 +107,7 @@ func testKeyOnlyOneType(t *testing.T, u *url.URL) {
withKeyFile(t, keyname, func(keyFile string) {
var userKeyPublicKeyID int64
t.Run("KeyCanOnlyBeUser", func(t *testing.T) {
dstPath, err := ioutil.TempDir("", ctx.Reponame)
dstPath, err := os.MkdirTemp("", ctx.Reponame)
assert.NoError(t, err)
defer util.RemoveAll(dstPath)
@@ -133,7 +133,7 @@ func testKeyOnlyOneType(t *testing.T, u *url.URL) {
})
t.Run("KeyCanBeAnyDeployButNotUserAswell", func(t *testing.T) {
dstPath, err := ioutil.TempDir("", ctx.Reponame)
dstPath, err := os.MkdirTemp("", ctx.Reponame)
assert.NoError(t, err)
defer util.RemoveAll(dstPath)
@@ -151,7 +151,7 @@ func testKeyOnlyOneType(t *testing.T, u *url.URL) {
t.Run("FailToPush", doGitPushTestRepositoryFail(dstPath, "origin", "master"))
otherSSHURL := createSSHUrl(otherCtx.GitPath(), u)
dstOtherPath, err := ioutil.TempDir("", otherCtx.Reponame)
dstOtherPath, err := os.MkdirTemp("", otherCtx.Reponame)
assert.NoError(t, err)
defer util.RemoveAll(dstOtherPath)
@@ -168,7 +168,7 @@ func testKeyOnlyOneType(t *testing.T, u *url.URL) {
t.Run("DeleteRepositoryShouldReleaseKey", func(t *testing.T) {
otherSSHURL := createSSHUrl(otherCtx.GitPath(), u)
dstOtherPath, err := ioutil.TempDir("", otherCtx.Reponame)
dstOtherPath, err := os.MkdirTemp("", otherCtx.Reponame)
assert.NoError(t, err)
defer util.RemoveAll(dstOtherPath)
@@ -190,7 +190,7 @@ func testKeyOnlyOneType(t *testing.T, u *url.URL) {
userKeyPublicKeyID = publicKey.ID
}))
dstPath, err := ioutil.TempDir("", ctx.Reponame)
dstPath, err := os.MkdirTemp("", ctx.Reponame)
assert.NoError(t, err)
defer util.RemoveAll(dstPath)