Skip to content

Commit

Permalink
remove deprecated ioutil usages
Browse files Browse the repository at this point in the history
Signed-off-by: sriv <[email protected]>
  • Loading branch information
sriv committed Jan 24, 2024
1 parent 1ac36d8 commit bd6e53f
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 37 deletions.
3 changes: 1 addition & 2 deletions build/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -285,7 +284,7 @@ var (
)

func getPluginProperties(jsonPropertiesFile string) (map[string]interface{}, error) {
pluginPropertiesJson, err := ioutil.ReadFile(jsonPropertiesFile)
pluginPropertiesJson, err := os.ReadFile(jsonPropertiesFile)
if err != nil {
fmt.Printf("Could not read %s: %s\n", filepath.Base(jsonPropertiesFile), err.Error())
return nil, err
Expand Down
9 changes: 4 additions & 5 deletions generator/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package generator

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -27,11 +26,11 @@ func TestEndToEndHTMLGenerationWhenBeforeSuiteFails(t *testing.T) {
if err != nil {
t.Errorf("Expected error to be nil. Got: %s", err.Error())
}
gotContent, err := ioutil.ReadFile(filepath.Join(reportDir, "index.html"))
gotContent, err := os.ReadFile(filepath.Join(reportDir, "index.html"))
if err != nil {
t.Errorf("Error reading generated HTML file: %s", err.Error())
}
wantContent, err := ioutil.ReadFile(filepath.Join("_testdata", "expectedE2E", "before_suite_fail.html"))
wantContent, err := os.ReadFile(filepath.Join("_testdata", "expectedE2E", "before_suite_fail.html"))
if err != nil {
t.Errorf("Error reading expected HTML file: %s", err.Error())
}
Expand Down Expand Up @@ -157,11 +156,11 @@ func cleanUp(t *testing.T, reportDir string) {

func verifyExpectedFiles(t *testing.T, suiteRes, reportDir string, expectedFiles []string) {
for _, expectedFile := range expectedFiles {
gotContent, err := ioutil.ReadFile(filepath.Join(reportDir, expectedFile))
gotContent, err := os.ReadFile(filepath.Join(reportDir, expectedFile))
if err != nil {
t.Errorf("Error reading generated HTML file: %s", err.Error())
}
wantContent, err := ioutil.ReadFile(filepath.Join("_testdata", "expectedE2E", suiteRes, expectedFile))
wantContent, err := os.ReadFile(filepath.Join("_testdata", "expectedE2E", suiteRes, expectedFile))
if err != nil {
t.Errorf("Error reading expected HTML file: %s", err.Error())
}
Expand Down
17 changes: 8 additions & 9 deletions generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -273,7 +272,7 @@ func minifyHTMLFiles(htmlFilePaths []string, reportsDir string) {
tmpDir := os.TempDir()
srcToDest := make(map[string]string)
for _, htmlFilePath := range htmlFilePaths {
htmlBytes, err := ioutil.ReadFile(htmlFilePath)
htmlBytes, err := os.ReadFile(htmlFilePath)
if err != nil {
logger.Warnf("Error while minifying %s", err.Error())
return
Expand All @@ -283,24 +282,24 @@ func minifyHTMLFiles(htmlFilePaths []string, reportsDir string) {
logger.Warnf("Error while minifying %s", err.Error())
return
}
tmpHTMLFile, _ := ioutil.TempFile(tmpDir, "")
tmpHTMLFile, _ := os.CreateTemp(tmpDir, "")
tmpHTMLFile.Close()
tmpHTMLFilePath := tmpHTMLFile.Name()

err = ioutil.WriteFile(tmpHTMLFilePath, htmlBytes, os.ModePerm)
err = os.WriteFile(tmpHTMLFilePath, htmlBytes, os.ModePerm)
if err != nil {
logger.Warnf("Error while writing minified file %s: %s", tmpHTMLFilePath, err.Error())
return
}
srcToDest[tmpHTMLFilePath] = htmlFilePath
}
for src, dest := range srcToDest {
minifiedBytes, err := ioutil.ReadFile(src)
minifiedBytes, err := os.ReadFile(src)
if err != nil {
logger.Warnf("Error while minifying %s", err.Error())
return
}
err = ioutil.WriteFile(dest, minifiedBytes, os.ModePerm)
err = os.WriteFile(dest, minifiedBytes, os.ModePerm)
if err != nil {
logger.Warnf("Error while writing minified file %s: %s", dest, err.Error())
return
Expand Down Expand Up @@ -373,7 +372,7 @@ func readTemplates(themePath string) {
"screenshotOfFailureEnabled": screenshotOfFailureEnabled,
}

f, err := ioutil.ReadFile(filepath.Join(getAbsThemePath(themePath), "views", "partials.tmpl"))
f, err := os.ReadFile(filepath.Join(getAbsThemePath(themePath), "views", "partials.tmpl"))
if err != nil {
logger.Fatalf(err.Error())
}
Expand Down Expand Up @@ -587,9 +586,9 @@ func copyScreenshotFiles(reportsDir string) {
for _, fileName := range screenshotFiles {
srcfp := path.Join(src, fileName)
dstfp := path.Join(reportsDir, "images", fileName)
bytes, err := ioutil.ReadFile(srcfp)
bytes, err := os.ReadFile(srcfp)
if err == nil {
err = ioutil.WriteFile(dstfp, bytes, os.ModePerm)
err = os.WriteFile(dstfp, bytes, os.ModePerm)
if err != nil {
logger.Warnf("Failed to write screenshot %s", err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions generator/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package generator

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
Expand Down Expand Up @@ -862,7 +862,7 @@ func (b myBuf) String() string {

func TestHTMLGeneration(t *testing.T) {
for _, test := range HTMLGenerationTests {
content, err := ioutil.ReadFile(filepath.Join("_testdata", "integration", test.expectedFile))
content, err := os.ReadFile(filepath.Join("_testdata", "integration", test.expectedFile))
if err != nil {
t.Errorf("Error reading expected HTML file: %s", err.Error())
}
Expand All @@ -884,7 +884,7 @@ func TestHTMLGeneration(t *testing.T) {
}

func TestIndexPageGeneration(t *testing.T) {
content, err := ioutil.ReadFile(filepath.Join("_testdata", "integration", "pass_index.html"))
content, err := os.ReadFile(filepath.Join("_testdata", "integration", "pass_index.html"))
if err != nil {
t.Errorf("Error reading expected HTML file: %s", err.Error())
}
Expand Down
3 changes: 1 addition & 2 deletions htmlReport.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package main

import (
"io/ioutil"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -111,7 +110,7 @@ func createBatFileToExecuteHTMLReport(exPath, exTarget string) {
o := []byte(content)
exTarget = strings.TrimSuffix(exTarget, filepath.Ext(exTarget))
outF := exTarget + ".bat"
err := ioutil.WriteFile(outF, o, common.NewFilePermissions)
err := os.WriteFile(outF, o, common.NewFilePermissions)
if err != nil {
logger.Debugf("[Warning] Failed to write to %s. Reason: %s\n", outF, err.Error())
return
Expand Down
11 changes: 5 additions & 6 deletions logger/log_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package logger

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -15,7 +14,7 @@ func TestDebugShoudWiteLogInJsonFormat(t *testing.T) {
defer temp.Close()

Debug("log debug message")
got, _ := ioutil.ReadFile(fname)
got, _ := os.ReadFile(fname)
want := "{\"logLevel\":\"debug\",\"message\":\"log debug message\"}\n"

if want != string(got) {
Expand All @@ -29,7 +28,7 @@ func TestDebugfShoudWiteLogInJsonFormat(t *testing.T) {
defer temp.Close()

Debugf("log %s debug message", "formatted")
got, _ := ioutil.ReadFile(fname)
got, _ := os.ReadFile(fname)
want := "{\"logLevel\":\"debug\",\"message\":\"log formatted debug message\"}\n"

if want != string(got) {
Expand All @@ -43,7 +42,7 @@ func TestInfoShoudWiteLogInJsonFormat(t *testing.T) {
defer temp.Close()

Info("log info message")
got, _ := ioutil.ReadFile(fname)
got, _ := os.ReadFile(fname)
want := "{\"logLevel\":\"info\",\"message\":\"log info message\"}\n"

if want != string(got) {
Expand All @@ -57,7 +56,7 @@ func TestInfofShoudWiteLogInJsonFormat(t *testing.T) {
defer temp.Close()

Infof("log %s info message", "formatted")
got, _ := ioutil.ReadFile(fname)
got, _ := os.ReadFile(fname)
want := "{\"logLevel\":\"info\",\"message\":\"log formatted info message\"}\n"

if want != string(got) {
Expand All @@ -71,7 +70,7 @@ func TestWarnfShoudWiteLogInJsonFormat(t *testing.T) {
defer temp.Close()

Warnf("log %s warning message", "formatted")
got, _ := ioutil.ReadFile(fname)
got, _ := os.ReadFile(fname)
want := "{\"logLevel\":\"warning\",\"message\":\"log formatted warning message\"}\n"

if want != string(got) {
Expand Down
4 changes: 2 additions & 2 deletions regenerate/regenerate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package regenerate

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

"github.com/getgauge/html-report/logger"
Expand All @@ -20,7 +20,7 @@ import (

// Report generates html report from saved result.
func Report(inputFile, reportsDir, themePath, pRoot string) {
b, err := ioutil.ReadFile(inputFile)
b, err := os.ReadFile(inputFile)
if err != nil {
logger.Fatal(err.Error())
}
Expand Down
9 changes: 4 additions & 5 deletions regenerate/regenerate_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package regenerate

import (
"encoding/json"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand All @@ -20,7 +19,7 @@ var templateBasePath, _ = filepath.Abs(filepath.Join("..", "themes", "default"))
// but the actual regenerate requires serialized proto data, hence this conversion.
func setup() {
inputFile := filepath.Join("_testdata", "last_run_result.json")
b, err := ioutil.ReadFile(inputFile)
b, err := os.ReadFile(inputFile)
if err != nil {
log.Fatal(err.Error())
}
Expand All @@ -31,7 +30,7 @@ func setup() {
}
by, _ := proto.Marshal(psr)
f := filepath.Join("_testdata", "last_run_result")
err = ioutil.WriteFile(f, by, 0644)
err = os.WriteFile(f, by, 0644)
if err != nil {
log.Fatalf("Unable to write file %s. Error: %s", f, err.Error())
}
Expand All @@ -45,11 +44,11 @@ func TestEndToEndHTMLGenerationFromSavedResult(t *testing.T) {

Report(inputFile, reportDir, templateBasePath, "/tmp/foo/")
for _, expectedFile := range expectedFiles {
gotContent, err := ioutil.ReadFile(filepath.Join(reportDir, expectedFile))
gotContent, err := os.ReadFile(filepath.Join(reportDir, expectedFile))
if err != nil {
t.Errorf("Error reading generated HTML file: %s", err.Error())
}
wantContent, err := ioutil.ReadFile(filepath.Join("_testdata", "expectedE2E", "simpleSuiteRes", expectedFile))
wantContent, err := os.ReadFile(filepath.Join("_testdata", "expectedE2E", "simpleSuiteRes", expectedFile))
if err != nil {
t.Errorf("Error reading expected HTML file: %s", err.Error())
}
Expand Down
5 changes: 2 additions & 3 deletions test_helper/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package testHelper
import (
"fmt"
"html"
"io/ioutil"
"os"
"regexp"
"testing"
Expand All @@ -25,12 +24,12 @@ func RemoveNewline(s string) string {
func AssertEqual(expected, actual, testName string, t *testing.T) {
if expected != actual {
diffHTML := compare(expected, actual)
tmpFile, err := ioutil.TempFile("", "")
tmpFile, err := os.CreateTemp("", "")
if err != nil {
t.Errorf("Unable to dump to tmp file. Raw content:\n%s\n", diffHTML)
}
fileName := fmt.Sprintf("%s.html", tmpFile.Name())
err = ioutil.WriteFile(fileName, []byte(diffHTML), 0644)
err = os.WriteFile(fileName, []byte(diffHTML), 0644)
if err != nil {
t.Errorf("Unable to write file %s. Error: %s", fileName, err.Error())
}
Expand Down

0 comments on commit bd6e53f

Please sign in to comment.