Skip to content

Commit

Permalink
Stop using deprecated package 'ioutil' (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
joanlopez authored Jul 30, 2024
1 parent cac94e6 commit bca67fd
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package killgrave
import (
"errors"
"fmt"
"io/ioutil"
"io"
"os"
"path"

Expand Down Expand Up @@ -146,7 +146,7 @@ func NewConfigFromFile(cfgPath string) (Config, error) {
defer configFile.Close()

var cfg Config
bytes, _ := ioutil.ReadAll(configFile)
bytes, _ := io.ReadAll(configFile)
if err := yaml.Unmarshal(bytes, &cfg); err != nil {
return Config{}, fmt.Errorf("%w: error while unmarshalling configFile file %s, using default configuration instead", err, cfgPath)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/server/http/handler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package http

import (
"io/ioutil"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -49,7 +49,7 @@ func fetchBodyFromFile(bodyFile string) (bytes []byte) {

f, _ := os.Open(bodyFile)
defer f.Close()
bytes, err := ioutil.ReadAll(f)
bytes, err := io.ReadAll(f)
if err != nil {
log.Printf("imposible read the file %s: %v\n", bodyFile, err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/server/http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package http

import (
"bytes"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -40,7 +40,7 @@ func TestImposterHandler(t *testing.T) {

f, _ := os.Open(bodyFile)
defer f.Close()
expectedBodyFileData, _ := ioutil.ReadAll(f)
expectedBodyFileData, _ := io.ReadAll(f)

var dataTest = []struct {
name string
Expand Down
8 changes: 4 additions & 4 deletions internal/server/http/route_matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -37,15 +37,15 @@ func validateSchema(imposter Imposter, req *http.Request) error {

defer func() {
req.Body.Close()
req.Body = ioutil.NopCloser(bytes.NewBuffer(requestBodyBytes))
req.Body = io.NopCloser(bytes.NewBuffer(requestBodyBytes))
}()

schemaFile := imposter.CalculateFilePath(*imposter.Request.SchemaFile)
if _, err := os.Stat(schemaFile); os.IsNotExist(err) {
return fmt.Errorf("%w: the schema file %s not found", err, schemaFile)
}

requestBodyBytes, err := ioutil.ReadAll(req.Body)
requestBodyBytes, err := io.ReadAll(req.Body)
if err != nil {
return fmt.Errorf("%w: impossible read the request body", err)
}
Expand All @@ -60,7 +60,7 @@ func validateSchema(imposter Imposter, req *http.Request) error {
return fmt.Errorf("%w: impossible find the schema", err)
}

schemaBytes, err := ioutil.ReadFile(schemaFilePath)
schemaBytes, err := os.ReadFile(schemaFilePath)
if err != nil {
return fmt.Errorf("%w: impossible read the schema", err)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/server/http/route_matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package http
import (
"bytes"
"errors"
"io/ioutil"
"io"
"net/http"
"testing"

Expand All @@ -12,10 +12,10 @@ import (
)

func TestMatcherBySchema(t *testing.T) {
bodyA := ioutil.NopCloser(bytes.NewReader([]byte("{\"type\": \"gopher\"}")))
bodyB := ioutil.NopCloser(bytes.NewReader([]byte("{\"type\": \"cat\"}")))
emptyBody := ioutil.NopCloser(bytes.NewReader([]byte("")))
wrongBody := ioutil.NopCloser(errReader(0))
bodyA := io.NopCloser(bytes.NewReader([]byte("{\"type\": \"gopher\"}")))
bodyB := io.NopCloser(bytes.NewReader([]byte("{\"type\": \"cat\"}")))
emptyBody := io.NopCloser(bytes.NewReader([]byte("")))
wrongBody := io.NopCloser(errReader(0))

schemaGopherFile := "test/testdata/imposters/schemas/type_gopher.json"
schemaCatFile := "test/testdata/imposters/schemas/type_cat.json"
Expand Down
4 changes: 2 additions & 2 deletions internal/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package killgrave

import (
"errors"
"io/ioutil"
"io"
"log"
"os"
"testing"
Expand All @@ -13,7 +13,7 @@ import (
)

func TestMain(m *testing.M) {
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
os.Exit(m.Run())
}

Expand Down

0 comments on commit bca67fd

Please sign in to comment.