From 79d7b22fa9ee498e40e5e8d5db8e4b1a1466c275 Mon Sep 17 00:00:00 2001 From: Arnaud Rebillout Date: Fri, 8 May 2026 15:37:37 +0700 Subject: [PATCH 1/4] Use os.CreateTemp instead of ioutil.TempFile io/ioutil was deprecated in Go 1.16, let's stop using it. This is a drop-in replacement, cf: * https://pkg.go.dev/io/ioutil#TempFile * https://pkg.go.dev/os#CreateTemp --- cli/commands.go | 2 +- logs/logs_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cli/commands.go b/cli/commands.go index 5980194f..cc2b116a 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -656,7 +656,7 @@ func (c *cli) CmdEdit(args ...string) error { out, err := yaml.Marshal(mirror) // Open a temporary file - f, err := ioutil.TempFile(os.TempDir(), "edit") + f, err := os.CreateTemp("", "edit") if err != nil { log.Fatal("Cannot create temporary file:", err) } diff --git a/logs/logs_test.go b/logs/logs_test.go index ea2176a0..b4cabd55 100644 --- a/logs/logs_test.go +++ b/logs/logs_test.go @@ -62,7 +62,7 @@ func TestIsTerminal(t *testing.T) { t.Fatalf("The current terminal is supposed to support colors") } - f, err := ioutil.TempFile("", "mirrorbits-tests") + f, err := os.CreateTemp("", "mirrorbits-tests") if err != nil { t.Errorf("Unable to create a temporary file: %s", err.Error()) return @@ -103,7 +103,7 @@ func TestReloadRuntimeLogs(t *testing.T) { } /* */ - f, err := ioutil.TempFile("", "mirrorbits-tests") + f, err := os.CreateTemp("", "mirrorbits-tests") if err != nil { t.Errorf("Unable to create a temporary file: %s", err.Error()) return From b21a3088aa9b39e08cdcbe98a31d1132959c62ce Mon Sep 17 00:00:00 2001 From: Arnaud Rebillout Date: Fri, 8 May 2026 15:40:39 +0700 Subject: [PATCH 2/4] Use os.ReadFile/WriteFile instead of ioutil.ReadFile/WriteFile io/ioutil was deprecated in Go 1.16, let's stop using it. This is a drop-in replacement, cf: * https://pkg.go.dev/io/ioutil#ReadFile * https://pkg.go.dev/io/ioutil#WriteFile * https://pkg.go.dev/os#ReadFile * https://pkg.go.dev/os#WriteFile --- cli/commands.go | 3 +-- config/config.go | 3 +-- process/process.go | 5 ++--- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/cli/commands.go b/cli/commands.go index cc2b116a..d9e2937c 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -9,7 +9,6 @@ import ( "context" "flag" "fmt" - "io/ioutil" "net/url" "os" "os/exec" @@ -683,7 +682,7 @@ reopen: } // Read the file back - out, err = ioutil.ReadFile(f.Name()) + out, err = os.ReadFile(f.Name()) if err != nil { log.Fatal("Cannot read file", f.Name()) } diff --git a/config/config.go b/config/config.go index e264798d..b09c4218 100644 --- a/config/config.go +++ b/config/config.go @@ -5,7 +5,6 @@ package config import ( "fmt" - "io/ioutil" "os" "path/filepath" "sync" @@ -141,7 +140,7 @@ func ReloadConfig() error { } } - content, err := ioutil.ReadFile(core.ConfigFile) + content, err := os.ReadFile(core.ConfigFile) if err != nil { fmt.Println("Configuration could not be found.\n\tUse -config ") os.Exit(1) diff --git a/process/process.go b/process/process.go index 7a26b195..4c4286ad 100644 --- a/process/process.go +++ b/process/process.go @@ -6,7 +6,6 @@ package process import ( "errors" "fmt" - "io/ioutil" "net" "os" "os/exec" @@ -170,7 +169,7 @@ func WritePidFile() { // Get our own PID and write it pid := strconv.Itoa(os.Getpid()) - if err := ioutil.WriteFile(p, []byte(pid), 0644); err != nil { + if err := os.WriteFile(p, []byte(pid), 0644); err != nil { log.Errorf("Unable to write pid file: %v", err) } } @@ -191,7 +190,7 @@ func RemovePidFile() { // GetRemoteProcPid gets the pid as it appears in the pid file (maybe not ours) func GetRemoteProcPid() int { - b, err := ioutil.ReadFile(GetPidLocation()) + b, err := os.ReadFile(GetPidLocation()) if err != nil { return -1 } From b2d2d138ca2f1103e0ea812b1bcdbccc8967f459 Mon Sep 17 00:00:00 2001 From: Arnaud Rebillout Date: Fri, 8 May 2026 15:41:55 +0700 Subject: [PATCH 3/4] Use io.ReadAll instead of ioutil.ReadAll io/ioutil was deprecated in Go 1.16, let's stop using it. This is a drop-in replacement, cf: * https://pkg.go.dev/io/ioutil#ReadAll * https://pkg.go.dev/io#ReadAll --- logs/logs_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/logs/logs_test.go b/logs/logs_test.go index b4cabd55..8e2cde47 100644 --- a/logs/logs_test.go +++ b/logs/logs_test.go @@ -6,6 +6,7 @@ package logs import ( "bytes" "errors" + "io" "io/ioutil" "os" "reflect" @@ -127,7 +128,7 @@ func TestReloadRuntimeLogs(t *testing.T) { testString := "Testing42" log.Error(testString) - buf, _ := ioutil.ReadAll(f) + buf, _ := io.ReadAll(f) if !strings.Contains(string(buf), testString) { t.Fatalf("The log doesn't contain the string %s", testString) } From ea1f870376ea3a3e0a27379dc0b2e68534cf16cc Mon Sep 17 00:00:00 2001 From: Arnaud Rebillout Date: Fri, 8 May 2026 15:43:31 +0700 Subject: [PATCH 4/4] Use testing.TempDir instead of ioutil.TempDir io/ioutil was deprecated in Go 1.16, let's stop using it. The function testing.TempDir was added in Go 1.15, cf: * https://pkg.go.dev/io/ioutil#TempDir * https://pkg.go.dev/testing#T.TempDir --- http/http_test.go | 29 +++++------------------------ logs/logs_test.go | 8 +------- 2 files changed, 6 insertions(+), 31 deletions(-) diff --git a/http/http_test.go b/http/http_test.go index 87254be9..d0cffe4b 100644 --- a/http/http_test.go +++ b/http/http_test.go @@ -5,7 +5,6 @@ package http import ( "errors" - "io/ioutil" "net" "net/http" "net/http/httptest" @@ -233,22 +232,13 @@ type testContext struct { } // Prepare a test, return the context -func prepareTest(filenames []string) (testContext, error) { +func prepareTest(t *testing.T, filenames []string) (testContext, error) { // Create a temporary directory for test data - testDir, err := ioutil.TempDir("", "mirrorbits-tests") - if err != nil { - return testContext{}, err - } - - defer func() { - if err != nil { - os.RemoveAll(testDir) - } - }() + testDir := t.TempDir() // Create the repo directory, along with dummy files repoDir := testDir + "/repo" - err = os.Mkdir(repoDir, 0755) + err := os.Mkdir(repoDir, 0755) if err != nil { return testContext{}, err } @@ -310,13 +300,6 @@ func prepareTest(filenames []string) (testContext, error) { }, nil } -// Cleanup after a test is done -func cleanupTest(ctx testContext) { - if ctx.TestDir != "" { - os.RemoveAll(ctx.TestDir) - } -} - // Test 4xx return codes from MirrorHandler. // // Those HTTP codes are triggered when the file requested doesn't even exist in @@ -324,11 +307,10 @@ func cleanupTest(ctx testContext) { // there's no need to mock redis commands. func TestMirrorHandler4xx(t *testing.T) { // Prepare - ctx, err := prepareTest([]string{}) + ctx, err := prepareTest(t, []string{}) if err != nil { t.Fatal(err) } - defer cleanupTest(ctx) noHeader := map[string]string{} @@ -460,11 +442,10 @@ var mockedCmds304 = [][]mockedCmd{ // unexpected. func TestMirrorHandler3xx(t *testing.T) { // Prepare - ctx, err := prepareTest([]string{testFile}) + ctx, err := prepareTest(t, []string{testFile}) if err != nil { t.Fatal(err) } - defer cleanupTest(ctx) // Define tests tests := map[string]struct { diff --git a/logs/logs_test.go b/logs/logs_test.go index 8e2cde47..5b169841 100644 --- a/logs/logs_test.go +++ b/logs/logs_test.go @@ -7,7 +7,6 @@ import ( "bytes" "errors" "io" - "io/ioutil" "os" "reflect" "strings" @@ -144,12 +143,7 @@ func TestReloadRuntimeLogs(t *testing.T) { } func TestOpenLogFile(t *testing.T) { - path, err := ioutil.TempDir("", "mirrorbits-tests") - if err != nil { - t.Errorf("Unable to create temporary directory: %s", err.Error()) - return - } - defer os.RemoveAll(path) + path := t.TempDir() f, newfile, err := openLogFile(path + "/test1.log") if err != nil {