Skip to content

Commit f2e7d54

Browse files
refactor: move from io/ioutil to io and os package (go-gitea#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>
1 parent aa631d8 commit f2e7d54

File tree

103 files changed

+261
-314
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+261
-314
lines changed

build/generate-bindata.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"bytes"
1212
"crypto/sha1"
1313
"fmt"
14-
"io/ioutil"
1514
"log"
1615
"net/http"
1716
"os"
@@ -28,7 +27,7 @@ func needsUpdate(dir string, filename string) (bool, []byte) {
2827
needRegen = true
2928
}
3029

31-
oldHash, err := ioutil.ReadFile(filename + ".hash")
30+
oldHash, err := os.ReadFile(filename + ".hash")
3231
if err != nil {
3332
oldHash = []byte{}
3433
}
@@ -83,5 +82,5 @@ func main() {
8382
if err != nil {
8483
log.Fatalf("%v\n", err)
8584
}
86-
_ = ioutil.WriteFile(filename+".hash", newHash, 0666)
85+
_ = os.WriteFile(filename+".hash", newHash, 0666)
8786
}

build/generate-emoji.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import (
1212
"flag"
1313
"fmt"
1414
"go/format"
15-
"io/ioutil"
15+
"io"
1616
"log"
1717
"net/http"
18+
"os"
1819
"regexp"
1920
"sort"
2021
"strconv"
@@ -67,7 +68,7 @@ func main() {
6768
}
6869

6970
// write
70-
err = ioutil.WriteFile(*flagOut, buf, 0644)
71+
err = os.WriteFile(*flagOut, buf, 0644)
7172
if err != nil {
7273
log.Fatal(err)
7374
}
@@ -96,7 +97,7 @@ func generate() ([]byte, error) {
9697
defer res.Body.Close()
9798

9899
// read all
99-
body, err := ioutil.ReadAll(res.Body)
100+
body, err := io.ReadAll(res.Body)
100101
if err != nil {
101102
return nil, err
102103
}
@@ -157,7 +158,7 @@ func generate() ([]byte, error) {
157158

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

162163
// Add skin tones to emoji that support it
163164
var (

build/generate-gitignores.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"flag"
1010
"fmt"
1111
"io"
12-
"io/ioutil"
1312
"log"
1413
"net/http"
1514
"os"
@@ -34,7 +33,7 @@ func main() {
3433
flag.StringVar(&githubApiToken, "token", "", "github api token")
3534
flag.Parse()
3635

37-
file, err := ioutil.TempFile(os.TempDir(), prefix)
36+
file, err := os.CreateTemp(os.TempDir(), prefix)
3837

3938
if err != nil {
4039
log.Fatalf("Failed to create temp file. %s", err)
@@ -114,13 +113,13 @@ func main() {
114113
for dst, src := range filesToCopy {
115114
// Read all content of src to data
116115
src = path.Join(destination, src)
117-
data, err := ioutil.ReadFile(src)
116+
data, err := os.ReadFile(src)
118117
if err != nil {
119118
log.Fatalf("Failed to read src file. %s", err)
120119
}
121120
// Write data to dst
122121
dst = path.Join(destination, dst)
123-
err = ioutil.WriteFile(dst, data, 0644)
122+
err = os.WriteFile(dst, data, 0644)
124123
if err != nil {
125124
log.Fatalf("Failed to write new file. %s", err)
126125
}

build/generate-licenses.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"flag"
1010
"fmt"
1111
"io"
12-
"io/ioutil"
1312
"log"
1413
"net/http"
1514
"os"
@@ -34,7 +33,7 @@ func main() {
3433
flag.StringVar(&githubApiToken, "token", "", "github api token")
3534
flag.Parse()
3635

37-
file, err := ioutil.TempFile(os.TempDir(), prefix)
36+
file, err := os.CreateTemp(os.TempDir(), prefix)
3837

3938
if err != nil {
4039
log.Fatalf("Failed to create temp file. %s", err)

cmd/dump.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package cmd
77

88
import (
99
"fmt"
10-
"io/ioutil"
1110
"os"
1211
"path"
1312
"path/filepath"
@@ -247,7 +246,7 @@ func runDump(ctx *cli.Context) error {
247246
fatal("Path does not exist: %s", tmpDir)
248247
}
249248

250-
dbDump, err := ioutil.TempFile(tmpDir, "gitea-db.sql")
249+
dbDump, err := os.CreateTemp(tmpDir, "gitea-db.sql")
251250
if err != nil {
252251
fatal("Failed to create tmp file: %v", err)
253252
}

contrib/fixtures/fixture_generation.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package main
66

77
import (
88
"fmt"
9-
"io/ioutil"
109
"os"
1110
"path/filepath"
1211

@@ -65,7 +64,7 @@ func generate(name string) error {
6564
return err
6665
}
6766
path := filepath.Join(fixturesDir, name+".yml")
68-
if err := ioutil.WriteFile(path, []byte(data), 0644); err != nil {
67+
if err := os.WriteFile(path, []byte(data), 0644); err != nil {
6968
return fmt.Errorf("%s: %+v", path, err)
7069
}
7170
fmt.Printf("%s created.\n", path)

contrib/pr/checkout.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"context"
1313
"flag"
1414
"fmt"
15-
"io/ioutil"
1615
"log"
1716
"net/http"
1817
"net/url"
@@ -52,11 +51,11 @@ func runPR() {
5251
setting.SetCustomPathAndConf("", "", "")
5352
setting.NewContext()
5453

55-
setting.RepoRootPath, err = ioutil.TempDir(os.TempDir(), "repos")
54+
setting.RepoRootPath, err = os.MkdirTemp(os.TempDir(), "repos")
5655
if err != nil {
5756
log.Fatalf("TempDir: %v\n", err)
5857
}
59-
setting.AppDataPath, err = ioutil.TempDir(os.TempDir(), "appdata")
58+
setting.AppDataPath, err = os.MkdirTemp(os.TempDir(), "appdata")
6059
if err != nil {
6160
log.Fatalf("TempDir: %v\n", err)
6261
}
@@ -181,7 +180,7 @@ func main() {
181180
codeFilePath = filepath.FromSlash(codeFilePath) //Convert to running OS
182181

183182
//Copy this file if it will not exist in the PR branch
184-
dat, err := ioutil.ReadFile(codeFilePath)
183+
dat, err := os.ReadFile(codeFilePath)
185184
if err != nil {
186185
log.Fatalf("Failed to cache this code file : %v", err)
187186
}
@@ -245,7 +244,7 @@ func main() {
245244
if err != nil {
246245
log.Fatalf("Failed to duplicate this code file in PR : %v", err)
247246
}
248-
err = ioutil.WriteFile(codeFilePath, dat, 0644)
247+
err = os.WriteFile(codeFilePath, dat, 0644)
249248
if err != nil {
250249
log.Fatalf("Failed to duplicate this code file in PR : %v", err)
251250
}

integrations/api_helper_for_declarative_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ package integrations
77
import (
88
"context"
99
"fmt"
10-
"io/ioutil"
1110
"net/http"
1211
"net/url"
12+
"os"
1313
"testing"
1414
"time"
1515

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

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

202-
dataPubKey, err := ioutil.ReadFile(keyFile + ".pub")
202+
dataPubKey, err := os.ReadFile(keyFile + ".pub")
203203
assert.NoError(t, err)
204204
req := NewRequestWithJSON(t, "POST", urlStr, api.CreateKeyOption{
205205
Title: keyname,

integrations/api_repo_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ package integrations
66

77
import (
88
"fmt"
9-
"io/ioutil"
109
"net/http"
1110
"net/url"
11+
"os"
1212
"testing"
1313

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

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

421421
httpContext.Reponame = "repo-tmp-17"
422-
dstPath, err := ioutil.TempDir("", httpContext.Reponame)
422+
dstPath, err := os.MkdirTemp("", httpContext.Reponame)
423423
assert.NoError(t, err)
424424
defer util.RemoveAll(dstPath)
425425
t.Run("CreateRepo", doAPICreateRepository(httpContext, false))

integrations/create_no_session_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package integrations
66

77
import (
8-
"io/ioutil"
98
"net/http"
109
"net/http/httptest"
1110
"os"
@@ -69,7 +68,7 @@ func TestSessionFileCreation(t *testing.T) {
6968
config.Provider = "file"
7069

7170
// Now create a temporaryDirectory
72-
tmpDir, err := ioutil.TempDir("", "sessions")
71+
tmpDir, err := os.MkdirTemp("", "sessions")
7372
assert.NoError(t, err)
7473
defer func() {
7574
if _, err := os.Stat(tmpDir); !os.IsNotExist(err) {

integrations/git_clone_wiki_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ package integrations
77
import (
88
"context"
99
"fmt"
10-
"io/ioutil"
1110
"net/url"
11+
"os"
1212
"path/filepath"
1313
"testing"
1414

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

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

36-
dstPath, err := ioutil.TempDir("", "clone_wiki")
36+
dstPath, err := os.MkdirTemp("", "clone_wiki")
3737
assert.NoError(t, err)
3838

3939
r := fmt.Sprintf("%suser2/repo1.wiki.git", u.String())

integrations/git_helper_for_declarative_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package integrations
77
import (
88
"context"
99
"fmt"
10-
"io/ioutil"
1110
"net"
1211
"net/http"
1312
"net/url"
@@ -28,7 +27,7 @@ import (
2827

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

31-
tmpDir, err := ioutil.TempDir("", "key-file")
30+
tmpDir, err := os.MkdirTemp("", "key-file")
3231
assert.NoError(t, err)
3332
defer util.RemoveAll(tmpDir)
3433

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

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

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

126125
func doGitCloneFail(u *url.URL) func(*testing.T) {
127126
return func(t *testing.T) {
128-
tmpDir, err := ioutil.TempDir("", "doGitCloneFail")
127+
tmpDir, err := os.MkdirTemp("", "doGitCloneFail")
129128
assert.NoError(t, err)
130129
defer util.RemoveAll(tmpDir)
131130
assert.Error(t, git.Clone(u.String(), tmpDir, git.CloneRepoOptions{}))
@@ -139,7 +138,7 @@ func doGitInitTestRepository(dstPath string) func(*testing.T) {
139138
return func(t *testing.T) {
140139
// Init repository in dstPath
141140
assert.NoError(t, git.InitRepository(dstPath, false))
142-
assert.NoError(t, ioutil.WriteFile(filepath.Join(dstPath, "README.md"), []byte(fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s", dstPath)), 0644))
141+
assert.NoError(t, os.WriteFile(filepath.Join(dstPath, "README.md"), []byte(fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s", dstPath)), 0644))
143142
assert.NoError(t, git.AddChanges(dstPath, true))
144143
signature := git.Signature{
145144
Email: "test@example.com",

integrations/git_smart_http_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package integrations
66

77
import (
8-
"io/ioutil"
8+
"io"
99
"net/http"
1010
"net/url"
1111
"testing"
@@ -62,7 +62,7 @@ func testGitSmartHTTP(t *testing.T, u *url.URL) {
6262
assert.NoError(t, err)
6363
defer resp.Body.Close()
6464
assert.EqualValues(t, kase.code, resp.StatusCode)
65-
_, err = ioutil.ReadAll(resp.Body)
65+
_, err = io.ReadAll(resp.Body)
6666
assert.NoError(t, err)
6767
})
6868
}

0 commit comments

Comments
 (0)