Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using ioutil package #322

Merged
merged 1 commit into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions exporter/fileexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fileexporter_test

import (
"context"
"io/ioutil"
"log"
"os"
"testing"
Expand Down Expand Up @@ -215,7 +214,7 @@ func TestFile_Export(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
outputDir := tt.fields.OutputDir
if tt.fields.OutputDir == "" {
outputDir, _ = ioutil.TempDir("", "fileExporter")
outputDir, _ = os.MkdirTemp("", "fileExporter")
defer os.Remove(outputDir)
}

Expand All @@ -231,12 +230,12 @@ func TestFile_Export(t *testing.T) {
return
}

files, _ := ioutil.ReadDir(outputDir)
files, _ := os.ReadDir(outputDir)
assert.Equal(t, 1, len(files), "Directory %s should have only one file", outputDir)
assert.Regexp(t, tt.expected.fileNameRegex, files[0].Name(), "Invalid file name")

expectedContent, _ := ioutil.ReadFile(tt.expected.content)
gotContent, _ := ioutil.ReadFile(outputDir + "/" + files[0].Name())
expectedContent, _ := os.ReadFile(tt.expected.content)
gotContent, _ := os.ReadFile(outputDir + "/" + files[0].Name())
assert.Equal(t, string(expectedContent), string(gotContent), "Wrong content in the output file")
})
}
Expand Down
5 changes: 2 additions & 3 deletions exporter/gcstorageexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"

Expand Down Expand Up @@ -64,7 +63,7 @@ func (f *Exporter) Export(ctx context.Context, logger *log.Logger, featureEvents
}

// Create a temp directory to store the file we will produce
outputDir, err := ioutil.TempDir("", "go_feature_flag_GoogleCloudStorage_export")
outputDir, err := os.MkdirTemp("", "go_feature_flag_GoogleCloudStorage_export")
if err != nil {
return err
}
Expand All @@ -84,7 +83,7 @@ func (f *Exporter) Export(ctx context.Context, logger *log.Logger, featureEvents
}

// Upload all the files in the folder to google storage
files, err := ioutil.ReadDir(outputDir)
files, err := os.ReadDir(outputDir)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions exporter/logsexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package logsexporter_test

import (
"context"
"io/ioutil"
"log"
"os"
"testing"

"github.com/thomaspoignant/go-feature-flag/exporter/logsexporter"
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestLog_Export(t *testing.T) {
LogFormat: tt.fields.LogFormat,
}

logFile, _ := ioutil.TempFile("", "")
logFile, _ := os.CreateTemp("", "")
logger := log.New(logFile, "", 0)

err := f.Export(context.Background(), logger, tt.args.featureEvents)
Expand All @@ -98,7 +98,7 @@ func TestLog_Export(t *testing.T) {

assert.NoError(t, err, "Exporter exporter should not throw errors")

logContent, _ := ioutil.ReadFile(logFile.Name())
logContent, _ := os.ReadFile(logFile.Name())
assert.Regexp(t, tt.expectedLog, string(logContent))
})
}
Expand Down
5 changes: 2 additions & 3 deletions exporter/s3exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package s3exporter

import (
"context"
"io/ioutil"
"log"
"os"
"sync"
Expand Down Expand Up @@ -69,7 +68,7 @@ func (f *Exporter) Export(ctx context.Context, logger *log.Logger, featureEvents
}

// Create a temp directory to store the file we will produce
outputDir, err := ioutil.TempDir("", "go_feature_flag_s3_export")
outputDir, err := os.MkdirTemp("", "go_feature_flag_s3_export")
if err != nil {
return err
}
Expand All @@ -89,7 +88,7 @@ func (f *Exporter) Export(ctx context.Context, logger *log.Logger, featureEvents
}

// Upload all the files in the folder to Exporter
files, err := ioutil.ReadDir(outputDir)
files, err := os.ReadDir(outputDir)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions exporter/s3exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package s3exporter

import (
"context"
"io/ioutil"
"log"
"os"
"testing"
Expand Down Expand Up @@ -187,7 +186,7 @@ func TestS3_Export(t *testing.T) {

assert.NoError(t, err, "Export should not error")
assert.Equal(t, 1, len(s3ManagerMock.S3ManagerMockFileSystem), "we should have 1 file in our mock")
expectedContent, _ := ioutil.ReadFile(tt.expectedFile)
expectedContent, _ := os.ReadFile(tt.expectedFile)
for k, v := range s3ManagerMock.S3ManagerMockFileSystem {
assert.Equal(t, string(expectedContent), v, "invalid file content")
assert.Regexp(t, tt.expectedName, k, "invalid file name")
Expand Down
4 changes: 2 additions & 2 deletions exporter/webhookexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -94,7 +94,7 @@ func (f *Exporter) Export(ctx context.Context, logger *log.Logger, featureEvents
}

request, err := http.NewRequestWithContext(
ctx, http.MethodPost, f.EndpointURL, ioutil.NopCloser(bytes.NewReader(payload)))
ctx, http.MethodPost, f.EndpointURL, io.NopCloser(bytes.NewReader(payload)))
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions exporter/webhookexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package webhookexporter
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"testing"
Expand Down Expand Up @@ -166,7 +165,7 @@ func TestWebhook_Export(t *testing.T) {

assert.NoError(t, err)
if tt.expected.bodyFilePath != "" {
c, err := ioutil.ReadFile(tt.expected.bodyFilePath)
c, err := os.ReadFile(tt.expected.bodyFilePath)
fmt.Println(err)
assert.JSONEq(t, string(c), tt.fields.httpClient.Body)
}
Expand Down
8 changes: 4 additions & 4 deletions feature_flag_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ffclient_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"
"text/template"
"time"
Expand All @@ -19,7 +19,7 @@ var client *ffclient.GoFeatureFlag

// init is creating a flag file for this test with the expected date.
func init() {
content, _ := ioutil.ReadFile("testdata/benchmark/flag-config.yaml")
content, _ := os.ReadFile("testdata/benchmark/flag-config.yaml")
t, _ := template.New("example-flag-config").Parse(string(content))

var buf bytes.Buffer
Expand All @@ -33,8 +33,8 @@ func init() {
DateAfter: time.Now().Add(3 * time.Second).Format(time.RFC3339),
})

flagFile, _ := ioutil.TempFile("", "")
_ = ioutil.WriteFile(flagFile.Name(), buf.Bytes(), 0o600)
flagFile, _ := os.CreateTemp("", "")
_ = os.WriteFile(flagFile.Name(), buf.Bytes(), 0o600)

client, _ = ffclient.New(ffclient.Config{
PollingInterval: 1 * time.Second,
Expand Down
15 changes: 7 additions & 8 deletions feature_flag_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ffclient_test

import (
"io/ioutil"
"log"
"os"
"testing"
Expand Down Expand Up @@ -174,8 +173,8 @@ func TestUpdateFlag(t *testing.T) {
false: false
default: false`

flagFile, _ := ioutil.TempFile("", "")
_ = ioutil.WriteFile(flagFile.Name(), []byte(initialFileContent), 0o600)
flagFile, _ := os.CreateTemp("", "")
_ = os.WriteFile(flagFile.Name(), []byte(initialFileContent), 0o600)

gffClient1, _ := ffclient.New(ffclient.Config{
PollingInterval: 1 * time.Second,
Expand All @@ -194,7 +193,7 @@ func TestUpdateFlag(t *testing.T) {
false: false
default: false`

_ = ioutil.WriteFile(flagFile.Name(), []byte(updatedFileContent), 0o600)
_ = os.WriteFile(flagFile.Name(), []byte(updatedFileContent), 0o600)

flagValue, _ = gffClient1.BoolVariation("test-flag", ffuser.NewUser("random-key"), false)
assert.True(t, flagValue)
Expand All @@ -213,8 +212,8 @@ func TestImpossibleToLoadfile(t *testing.T) {
false: false
default: false`

flagFile, _ := ioutil.TempFile("", "impossible")
_ = ioutil.WriteFile(flagFile.Name(), []byte(initialFileContent), 0o600)
flagFile, _ := os.CreateTemp("", "impossible")
_ = os.WriteFile(flagFile.Name(), []byte(initialFileContent), 0o600)

gffClient1, _ := ffclient.New(ffclient.Config{
PollingInterval: 1 * time.Second,
Expand Down Expand Up @@ -245,7 +244,7 @@ func TestFlagFileUnreachable(t *testing.T) {
false: "false"
default: "false"`

tempDir, _ := ioutil.TempDir("", "")
tempDir, _ := os.MkdirTemp("", "")
defer os.Remove(tempDir)

flagFilePath := tempDir + "_FlagFileUnreachable.yaml"
Expand All @@ -262,7 +261,7 @@ func TestFlagFileUnreachable(t *testing.T) {
flagValue, _ := gff.StringVariation("test-flag", ffuser.NewUser("random-key"), "SDKdefault")
assert.Equal(t, "SDKdefault", flagValue, "should use the SDK default value")

_ = ioutil.WriteFile(flagFilePath, []byte(initialFileContent), 0o600)
_ = os.WriteFile(flagFilePath, []byte(initialFileContent), 0o600)
time.Sleep(2 * time.Second)

flagValue, _ = gff.StringVariation("test-flag", ffuser.NewUser("random-key"), "SDKdefault")
Expand Down
5 changes: 2 additions & 3 deletions internal/dataexporter/data_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dataexporter_test
import (
"context"
"errors"
"io/ioutil"
"log"
"os"
"testing"
Expand Down Expand Up @@ -79,7 +78,7 @@ func TestDataExporterScheduler_defaultFlush(t *testing.T) {
func TestDataExporterScheduler_exporterReturnError(t *testing.T) {
mockExporter := mock.Exporter{Err: errors.New("random err"), ExpectedNumberErr: 1, Bulk: true}

file, _ := ioutil.TempFile("", "log")
file, _ := os.CreateTemp("", "log")
defer file.Close()
defer os.Remove(file.Name())
logger := log.New(file, "", 0)
Expand All @@ -100,7 +99,7 @@ func TestDataExporterScheduler_exporterReturnError(t *testing.T) {
assert.Equal(t, inputEvents[:201], mockExporter.GetExportedEvents())

// read log
logs, _ := ioutil.ReadFile(file.Name())
logs, _ := os.ReadFile(file.Name())
assert.Regexp(t, "\\["+testutils.RFC3339Regex+"\\] error while exporting data: random err\\n", string(logs))
}

Expand Down
5 changes: 2 additions & 3 deletions notifier/logsnotifier/notifier_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package logsnotifier

import (
"io/ioutil"
"log"
"os"
"sync"
Expand Down Expand Up @@ -156,15 +155,15 @@ func TestLogNotifier_Notify(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logOutput, _ := ioutil.TempFile("", "")
logOutput, _ := os.CreateTemp("", "")
defer os.Remove(logOutput.Name())

c := &Notifier{
Logger: log.New(logOutput, "", 0),
}
tt.args.wg.Add(1)
_ = c.Notify(tt.args.diff, tt.args.wg)
log, _ := ioutil.ReadFile(logOutput.Name())
log, _ := os.ReadFile(logOutput.Name())
assert.Regexp(t, tt.expected, string(log))
})
}
Expand Down
4 changes: 2 additions & 2 deletions notifier/slacknotifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -60,7 +60,7 @@ func (c *Notifier) Notify(diff notifier.DiffCache, wg *sync.WaitGroup) error {
request := http.Request{
Method: http.MethodPost,
URL: slackURL,
Body: ioutil.NopCloser(bytes.NewReader(payload)),
Body: io.NopCloser(bytes.NewReader(payload)),
Header: map[string][]string{"Content-type": {"application/json"}},
}
response, err := c.httpClient.Do(&request)
Expand Down
3 changes: 1 addition & 2 deletions notifier/slacknotifier/notifier_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package slacknotifier

import (
"io/ioutil"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -164,7 +163,7 @@ func TestSlackNotifier_Notify(t *testing.T) {
} else {
assert.NoError(t, err)
hostname, _ := os.Hostname()
content, _ := ioutil.ReadFile(tt.expected.bodyPath)
content, _ := os.ReadFile(tt.expected.bodyPath)
expectedContent := strings.ReplaceAll(string(content), "{{hostname}}", hostname)
assert.JSONEq(t, expectedContent, mockHTTPClient.Body)
assert.Equal(t, tt.expected.signature, mockHTTPClient.Signature)
Expand Down
4 changes: 2 additions & 2 deletions notifier/webhooknotifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -133,7 +133,7 @@ func (c *Notifier) Notify(diff notifier.DiffCache, wg *sync.WaitGroup) error {
Method: "POST",
URL: endpointURL,
Header: headers,
Body: ioutil.NopCloser(bytes.NewReader(payload)),
Body: io.NopCloser(bytes.NewReader(payload)),
}
response, err := c.httpClient.Do(&request)
// Log if something went wrong while calling the webhook.
Expand Down
4 changes: 2 additions & 2 deletions notifier/webhooknotifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package webhooknotifier

import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"sync"
"testing"

Expand Down Expand Up @@ -172,7 +172,7 @@ func Test_webhookNotifier_Notify(t *testing.T) {
assert.ErrorContains(t, err, tt.expected.errorMsg)
} else {
assert.NoError(t, err)
content, _ := ioutil.ReadFile(tt.expected.bodyPath)
content, _ := os.ReadFile(tt.expected.bodyPath)
assert.JSONEq(t, string(content), mockHTTPClient.Body)
assert.Equal(t, tt.expected.signature, mockHTTPClient.Signature)
}
Expand Down
4 changes: 2 additions & 2 deletions retriever/fileretriever/retriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package fileretriever

import (
"context"
"io/ioutil"
"os"
)

// Retriever is a configuration struct for a local flat file.
Expand All @@ -12,7 +12,7 @@ type Retriever struct {

// Retrieve is reading the file and return the content
func (r *Retriever) Retrieve(ctx context.Context) ([]byte, error) {
content, err := ioutil.ReadFile(r.Path)
content, err := os.ReadFile(r.Path)
if err != nil {
return nil, err
}
Expand Down
Loading