Skip to content

Commit

Permalink
Remove ioutil usage (bazel-contrib#1707)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky authored Jan 2, 2024
1 parent 90ab756 commit 030dba3
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 105 deletions.
3 changes: 1 addition & 2 deletions cmd/gazelle/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand All @@ -35,7 +34,7 @@ func fixFile(c *config.Config, f *rule.File) error {
if err := os.MkdirAll(filepath.Dir(outPath), 0o777); err != nil {
return err
}
if err := ioutil.WriteFile(outPath, newContent, 0o666); err != nil {
if err := os.WriteFile(outPath, newContent, 0o666); err != nil {
return err
}
f.Content = newContent
Expand Down
25 changes: 12 additions & 13 deletions cmd/gazelle/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand All @@ -40,7 +39,7 @@ func TestMain(m *testing.M) {
flag.Parse()

var err error
tmpDir, err := ioutil.TempDir(os.Getenv("TEST_TMPDIR"), "gazelle_test")
tmpDir, err := os.MkdirTemp(os.Getenv("TEST_TMPDIR"), "gazelle_test")
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
Expand Down Expand Up @@ -105,14 +104,14 @@ func defaultArgs(dir string) []string {
func TestCreateFile(t *testing.T) {
// Create a directory with a simple .go file.
tmpdir := os.Getenv("TEST_TMPDIR")
dir, err := ioutil.TempDir(tmpdir, "")
dir, err := os.MkdirTemp(tmpdir, "")
if err != nil {
t.Fatalf("ioutil.TempDir(%q, %q) failed with %v; want success", tmpdir, "", err)
t.Fatalf("os.MkdirTemp(%q, %q) failed with %v; want success", tmpdir, "", err)
}
defer os.RemoveAll(dir)

goFile := filepath.Join(dir, "main.go")
if err = ioutil.WriteFile(goFile, []byte("package main"), 0o600); err != nil {
if err = os.WriteFile(goFile, []byte("package main"), 0o600); err != nil {
t.Fatalf("error writing file %q: %v", goFile, err)
}

Expand All @@ -130,19 +129,19 @@ func TestCreateFile(t *testing.T) {
func TestUpdateFile(t *testing.T) {
// Create a directory with a simple .go file and an empty BUILD file.
tmpdir := os.Getenv("TEST_TMPDIR")
dir, err := ioutil.TempDir(tmpdir, "")
dir, err := os.MkdirTemp(tmpdir, "")
if err != nil {
t.Fatalf("ioutil.TempDir(%q, %q) failed with %v; want success", tmpdir, "", err)
t.Fatalf("os.MkdirTemp(%q, %q) failed with %v; want success", tmpdir, "", err)
}
defer os.RemoveAll(dir)

goFile := filepath.Join(dir, "main.go")
if err = ioutil.WriteFile(goFile, []byte("package main"), 0o600); err != nil {
if err = os.WriteFile(goFile, []byte("package main"), 0o600); err != nil {
t.Fatalf("error writing file %q: %v", goFile, err)
}

buildFile := filepath.Join(dir, "BUILD")
if err = ioutil.WriteFile(buildFile, nil, 0o600); err != nil {
if err = os.WriteFile(buildFile, nil, 0o600); err != nil {
t.Fatalf("error writing file %q: %v", buildFile, err)
}

Expand All @@ -165,19 +164,19 @@ func TestUpdateFile(t *testing.T) {
func TestNoChanges(t *testing.T) {
// Create a directory with a BUILD file that doesn't need any changes.
tmpdir := os.Getenv("TEST_TMPDIR")
dir, err := ioutil.TempDir(tmpdir, "")
dir, err := os.MkdirTemp(tmpdir, "")
if err != nil {
t.Fatalf("ioutil.TempDir(%q, %q) failed with %v; want success", tmpdir, "", err)
t.Fatalf("os.MkdirTemp(%q, %q) failed with %v; want success", tmpdir, "", err)
}
defer os.RemoveAll(dir)

goFile := filepath.Join(dir, "main.go")
if err = ioutil.WriteFile(goFile, []byte("package main\n\nfunc main() {}"), 0o600); err != nil {
if err = os.WriteFile(goFile, []byte("package main\n\nfunc main() {}"), 0o600); err != nil {
t.Fatalf("error writing file %q: %v", goFile, err)
}

buildFile := filepath.Join(dir, "BUILD")
if err = ioutil.WriteFile(buildFile, []byte(`load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
if err = os.WriteFile(buildFile, []byte(`load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
go_library(
name = "go_default_library",
Expand Down
14 changes: 6 additions & 8 deletions cmd/gazelle/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -211,7 +209,7 @@ go_library(
if err := runGazelle(dir, []string{"-go_prefix", "example.com/foo"}); err != nil {
t.Fatal(err)
}
if got, err := ioutil.ReadFile(filepath.Join(dir, "BUILD")); err != nil {
if got, err := os.ReadFile(filepath.Join(dir, "BUILD")); err != nil {
t.Fatal(err)
} else if string(got) != want {
t.Fatalf("got %s ; want %s", string(got), want)
Expand Down Expand Up @@ -310,7 +308,7 @@ go_library(
if err := runGazelle(dir, []string{c.cmd}); err != nil {
t.Fatal(err)
}
if got, err := ioutil.ReadFile(filepath.Join(dir, "BUILD")); err != nil {
if got, err := os.ReadFile(filepath.Join(dir, "BUILD")); err != nil {
t.Fatal(err)
} else if string(got) != c.want {
t.Fatalf("got %s ; want %s", string(got), c.want)
Expand Down Expand Up @@ -360,7 +358,7 @@ go_library(
if err := runGazelle(dir, []string{"fix", "-go_prefix", "example.com/foo"}); err != nil {
t.Fatal(err)
}
if got, err := ioutil.ReadFile(filepath.Join(dir, "BUILD")); err != nil {
if got, err := os.ReadFile(filepath.Join(dir, "BUILD")); err != nil {
t.Fatal(err)
} else if string(got) != want {
t.Fatalf("got %s ; want %s", string(got), want)
Expand Down Expand Up @@ -2038,7 +2036,7 @@ my_go_binary(
`,
},
{
Path: "enabled/multiple_mappings/multiple_mappings.go",
Path: "enabled/multiple_mappings/multiple_mappings.go",
Content: `
package main
Expand Down Expand Up @@ -3046,7 +3044,7 @@ github.com/selvatico/go-mocket v1.0.7/go.mod h1:7bSWzuNieCdUlanCVu3w0ppS0LvDtPAZ
if err := runGazelle(dir, args); err == nil {
t.Fatal("expected error, got nil")
} else if err.Error() != errMsg {
t.Error(fmt.Sprintf("want %s, got %s", errMsg, err.Error()))
t.Errorf("want %s, got %s", errMsg, err.Error())
}
}

Expand Down Expand Up @@ -4417,7 +4415,7 @@ go_library(
if err := runGazelle(dir, []string{"-go_prefix", "example.com/foo"}); err != nil {
t.Fatal(err)
}
if got, err := ioutil.ReadFile(filepath.Join(dir, "BUILD")); err != nil {
if got, err := os.ReadFile(filepath.Join(dir, "BUILD")); err != nil {
t.Fatal(err)
} else if string(got) != want {
t.Fatalf("got %s ; want %s; diff %s", string(got), want, cmp.Diff(string(got), want))
Expand Down
3 changes: 1 addition & 2 deletions cmd/generate_repo_config/generate_repo_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -127,7 +126,7 @@ func generateRepoConfig(configDest, configSource string) ([]string, error) {

buf.WriteString("\n")
buf.Write(destFile.Format())
if err := ioutil.WriteFile(configDest, buf.Bytes(), 0o666); err != nil {
if err := os.WriteFile(configDest, buf.Bytes(), 0o666); err != nil {
return nil, err
}

Expand Down
5 changes: 2 additions & 3 deletions cmd/move_labels/move_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
Expand Down Expand Up @@ -64,7 +63,7 @@ func run(args []string) error {
var errs errorList
for _, file := range files {
content := build.Format(file)
if err := ioutil.WriteFile(file.Path, content, 0o666); err != nil {
if err := os.WriteFile(file.Path, content, 0o666); err != nil {
errs = append(errs, err)
}
}
Expand All @@ -90,7 +89,7 @@ func moveLabelsInDir(c *configuration) ([]*build.File, error) {
if name := info.Name(); name != "BUILD" && name != "BUILD.bazel" {
return nil
}
content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err != nil {
errors = append(errors, err)
return nil
Expand Down
5 changes: 2 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package config

import (
"flag"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand All @@ -27,7 +26,7 @@ import (
)

func TestCommonConfigurerFlags(t *testing.T) {
dir, err := ioutil.TempDir(os.Getenv("TEST_TEMPDIR"), "config_test")
dir, err := os.MkdirTemp(os.Getenv("TEST_TEMPDIR"), "config_test")
if err != nil {
t.Fatal(err)
}
Expand All @@ -36,7 +35,7 @@ func TestCommonConfigurerFlags(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(dir, "WORKSPACE"), nil, 0o666); err != nil {
if err := os.WriteFile(filepath.Join(dir, "WORKSPACE"), nil, 0o666); err != nil {
t.Fatal(err)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/go_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package bazel_test

import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -188,7 +187,7 @@ func TestRepoCacheContainsGoEnv(t *testing.T) {
t.Fatal(err)
}
goEnvPath := filepath.Join(outputBase, "external/bazel_gazelle_go_repository_cache", "go.env")
gotBytes, err := ioutil.ReadFile(goEnvPath)
gotBytes, err := os.ReadFile(goEnvPath)
if err != nil {
t.Fatalf("could not read file %s: %v", goEnvPath, err)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/list_repository_tools_srcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -98,11 +97,11 @@ func main() {
fmt.Fprintln(buf, "]")

if *generate != "" {
if err := ioutil.WriteFile(*generate, buf.Bytes(), 0o666); err != nil {
if err := os.WriteFile(*generate, buf.Bytes(), 0o666); err != nil {
log.Fatal(err)
}
} else {
got, err := ioutil.ReadFile(*check)
got, err := os.ReadFile(*check)
if err != nil {
log.Fatal(err)
}
Expand Down
14 changes: 7 additions & 7 deletions internal/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ limitations under the License.
package bazel_test

import (
"io/ioutil"
"os"
"strings"
"testing"

"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
)

func TestRunner(t *testing.T) {
origBuildData, err := ioutil.ReadFile("BUILD.bazel")
origBuildData, err := os.ReadFile("BUILD.bazel")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := ioutil.WriteFile("BUILD.bazel", origBuildData, 0o666); err != nil {
if err := os.WriteFile("BUILD.bazel", origBuildData, 0o666); err != nil {
t.Fatalf("restoring build file: %v", err)
}
}()
Expand All @@ -54,12 +54,12 @@ func TestRunner(t *testing.T) {
}

func TestRunnerUpdateReposFromGoMod(t *testing.T) {
origWorkspaceData, err := ioutil.ReadFile("WORKSPACE")
origWorkspaceData, err := os.ReadFile("WORKSPACE")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := ioutil.WriteFile("WORKSPACE", origWorkspaceData, 0o666); err != nil {
if err := os.WriteFile("WORKSPACE", origWorkspaceData, 0o666); err != nil {
t.Fatalf("restoring WORKSPACE: %v", err)
}
}()
Expand All @@ -70,12 +70,12 @@ func TestRunnerUpdateReposFromGoMod(t *testing.T) {
}

func TestRunnerUpdateReposCommand(t *testing.T) {
origWorkspaceData, err := ioutil.ReadFile("WORKSPACE")
origWorkspaceData, err := os.ReadFile("WORKSPACE")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := ioutil.WriteFile("WORKSPACE", origWorkspaceData, 0o666); err != nil {
if err := os.WriteFile("WORKSPACE", origWorkspaceData, 0o666); err != nil {
t.Fatalf("restoring WORKSPACE: %v", err)
}
}()
Expand Down
13 changes: 8 additions & 5 deletions internal/wspace/finder_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/* Copyright 2016 The Bazel Authors. All rights reserved.
/*
Copyright 2016 The Bazel Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -12,14 +16,13 @@ limitations under the License.
package wspace

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)

func TestFind(t *testing.T) {
tmp, err := ioutil.TempDir(os.Getenv("TEST_TEMPDIR"), "")
tmp, err := os.MkdirTemp(os.Getenv("TEST_TEMPDIR"), "")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -58,7 +61,7 @@ func TestFind(t *testing.T) {
t.Fatal(err)
}

if err := ioutil.WriteFile(tc.file, nil, 0o755); err != nil {
if err := os.WriteFile(tc.file, nil, 0o755); err != nil {
t.Fatal(err)
}
}
Expand Down
Loading

0 comments on commit 030dba3

Please sign in to comment.