Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Feb 7, 2023
1 parent 6fbb762 commit 1d3c728
Show file tree
Hide file tree
Showing 28 changed files with 100 additions and 115 deletions.
3 changes: 1 addition & 2 deletions csv/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package csv

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

Expand Down Expand Up @@ -42,7 +41,7 @@ func (s *CSVSuite) SetUpSuite(c *C) {

s.dataFile = tmpDir + "/data.csv"

err := ioutil.WriteFile(s.dataFile, []byte(_DATA), 0644)
err := os.WriteFile(s.dataFile, []byte(_DATA), 0644)

if err != nil {
c.Fatal(err.Error())
Expand Down
6 changes: 3 additions & 3 deletions directio/directio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package directio
// ////////////////////////////////////////////////////////////////////////////////// //

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

Expand All @@ -32,7 +32,7 @@ func (s *DirectIOSuite) TestReading(c *C) {
tmpFile := tmpDir + "/tmp_data"
payload := []byte(strings.Repeat("DATA1", 123))

err := ioutil.WriteFile(tmpFile, payload, 0644)
err := os.WriteFile(tmpFile, payload, 0644)

if err != nil {
c.Fatal(err.Error())
Expand Down Expand Up @@ -61,7 +61,7 @@ func (s *DirectIOSuite) TestWriting(c *C) {

c.Assert(err, IsNil)

data, err := ioutil.ReadFile(tmpFile)
data, err := os.ReadFile(tmpFile)

c.Assert(err, IsNil)
c.Assert(len(data), Equals, len(payload))
Expand Down
53 changes: 26 additions & 27 deletions fsutil/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package fsutil
import (
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"testing"
Expand Down Expand Up @@ -42,8 +41,8 @@ func (s *FSSuite) TestList(c *check.C) {

os.Create(tmpDir + "/.file0")

c.Assert(ioutil.WriteFile(tmpDir+"/file1.mp3", []byte("TESTDATA12345678"), 0644), check.IsNil)
c.Assert(ioutil.WriteFile(tmpDir+"/file2.jpg", []byte("TESTDATA"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpDir+"/file1.mp3", []byte("TESTDATA12345678"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpDir+"/file2.jpg", []byte("TESTDATA"), 0644), check.IsNil)

c.Assert(os.Mkdir(tmpDir+"/dir1", 0755), check.IsNil)
c.Assert(os.Mkdir(tmpDir+"/dir2", 0755), check.IsNil)
Expand Down Expand Up @@ -270,7 +269,7 @@ func (s *FSSuite) TestGetSize(c *check.C) {
tmpDir := c.MkDir()
tmpFile := tmpDir + "/test.file"

c.Assert(ioutil.WriteFile(tmpFile, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile, []byte("TEST\n"), 0644), check.IsNil)

c.Assert(GetSize(""), check.Equals, int64(-1))
c.Assert(GetSize("/not_exist"), check.Equals, int64(-1))
Expand All @@ -281,7 +280,7 @@ func (s *FSSuite) TestGetTime(c *check.C) {
tmpDir := c.MkDir()
tmpFile := tmpDir + "/test.file"

c.Assert(ioutil.WriteFile(tmpFile, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile, []byte("TEST\n"), 0644), check.IsNil)

at, mt, ct, err := GetTimes(tmpFile)

Expand Down Expand Up @@ -366,7 +365,7 @@ func (s *FSSuite) TestGetOwner(c *check.C) {
tmpDir := c.MkDir()
tmpFile := tmpDir + "/test.file"

c.Assert(ioutil.WriteFile(tmpFile, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile, []byte("TEST\n"), 0644), check.IsNil)

uid, gid, err := GetOwner(tmpFile)

Expand Down Expand Up @@ -394,7 +393,7 @@ func (s *FSSuite) TestIsEmptyDir(c *check.C) {
tmpDir2 := c.MkDir()
tmpFile := tmpDir1 + "/test.file"

c.Assert(ioutil.WriteFile(tmpFile, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile, []byte("TEST\n"), 0644), check.IsNil)

c.Assert(IsEmptyDir(tmpDir1), check.Equals, false)
c.Assert(IsEmptyDir(tmpDir2), check.Equals, true)
Expand All @@ -407,8 +406,8 @@ func (s *FSSuite) TestIsEmpty(c *check.C) {
tmpFile1 := tmpDir + "/test1.file"
tmpFile2 := tmpDir + "/test2.file"

c.Assert(ioutil.WriteFile(tmpFile1, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(ioutil.WriteFile(tmpFile2, []byte(""), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile1, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile2, []byte(""), 0644), check.IsNil)

c.Assert(IsEmpty(""), check.Equals, false)
c.Assert(IsEmpty("/not_exist"), check.Equals, false)
Expand All @@ -426,7 +425,7 @@ func (s *FSSuite) TestTypeChecks(c *check.C) {
tmpFile := tmpDir + "/test.file"
tmpLink := tmpDir + "/test.link"

c.Assert(ioutil.WriteFile(tmpFile, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(os.Symlink("123", tmpLink), check.IsNil)

c.Assert(IsExist(""), check.Equals, false)
Expand Down Expand Up @@ -494,7 +493,7 @@ func (s *FSSuite) TestPermChecks(c *check.C) {
tmpFile9 := tmpDir + "/test9.file"

for i := 1; i <= 9; i++ {
c.Assert(ioutil.WriteFile(fmt.Sprintf("%s/test%d.file", tmpDir, i), []byte(""), 0644), check.IsNil)
c.Assert(os.WriteFile(fmt.Sprintf("%s/test%d.file", tmpDir, i), []byte(""), 0644), check.IsNil)
}

os.Chmod(tmpFile1, 0400)
Expand Down Expand Up @@ -566,7 +565,7 @@ func (s *FSSuite) TestCheckPerms(c *check.C) {
tmpFile := tmpDir + "/test.file"
tmpLink := tmpDir + "/test.link"

c.Assert(ioutil.WriteFile(tmpFile, []byte(""), 0600), check.IsNil)
c.Assert(os.WriteFile(tmpFile, []byte(""), 0600), check.IsNil)
c.Assert(os.Symlink("123", tmpLink), check.IsNil)

getUserError = true
Expand Down Expand Up @@ -623,7 +622,7 @@ func (s *FSSuite) TestGetMode(c *check.C) {
tmpDir := c.MkDir()
tmpFile := tmpDir + "/test.file"

c.Assert(ioutil.WriteFile(tmpFile, []byte("TEST\n"), 0764), check.IsNil)
c.Assert(os.WriteFile(tmpFile, []byte("TEST\n"), 0764), check.IsNil)

os.Chmod(tmpFile, 0764)

Expand All @@ -635,7 +634,7 @@ func (s *FSSuite) TestCountLines(c *check.C) {
tmpDir := c.MkDir()
tmpFile := tmpDir + "/test.file"

c.Assert(ioutil.WriteFile(tmpFile, []byte("1\n2\n3\n4\n"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile, []byte("1\n2\n3\n4\n"), 0644), check.IsNil)

n, err := CountLines("")

Expand Down Expand Up @@ -663,9 +662,9 @@ func (s *FSSuite) TestCopyFile(c *check.C) {
tmpFile2 := tmpDir2 + "/test2.file"
tmpFile3 := tmpDir1 + "/test3.file"

c.Assert(ioutil.WriteFile(tmpFile1, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(ioutil.WriteFile(tmpFile2, []byte("TEST1234TEST\n"), 0644), check.IsNil)
c.Assert(ioutil.WriteFile(tmpFile3, []byte(""), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile1, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile2, []byte("TEST1234TEST\n"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile3, []byte(""), 0644), check.IsNil)

os.Chmod(tmpFile3, 0111)
os.Chmod(tmpDir3, 0500)
Expand Down Expand Up @@ -717,10 +716,10 @@ func (s *FSSuite) TestCopyAttr(c *check.C) {
tmpFile3 := tmpDir + "/test3.file"
tmpFile4 := tmpDir + "/test4.file"

c.Assert(ioutil.WriteFile(tmpFile1, []byte("TEST\n"), 0600), check.IsNil)
c.Assert(ioutil.WriteFile(tmpFile2, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(ioutil.WriteFile(tmpFile3, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(ioutil.WriteFile(tmpFile4, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile1, []byte("TEST\n"), 0600), check.IsNil)
c.Assert(os.WriteFile(tmpFile2, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile3, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile4, []byte("TEST\n"), 0644), check.IsNil)

os.Chtimes(tmpFile1, time.Unix(946674000, 0), time.Unix(946674000, 0))
os.Chmod(tmpFile3, 0111)
Expand Down Expand Up @@ -804,9 +803,9 @@ func (s *FSSuite) TestMoveFile(c *check.C) {
tmpFile4 := tmpDir + "/test4.file"
tmpFile5 := tmpDir + "/test5.file"

c.Assert(ioutil.WriteFile(tmpFile1, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(ioutil.WriteFile(tmpFile3, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(ioutil.WriteFile(tmpFile4, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile1, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile3, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile4, []byte("TEST\n"), 0644), check.IsNil)

os.Chmod(tmpFile3, 0111)
os.Chmod(tmpDir2, 0500)
Expand Down Expand Up @@ -849,9 +848,9 @@ func (s *FSSuite) TestCopyDir(c *check.C) {
c.Assert(os.Mkdir(tmpDir5, 0200), check.IsNil)
c.Assert(os.Mkdir(tmpDir6, 0400), check.IsNil)

c.Assert(ioutil.WriteFile(tmpFile1, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(ioutil.WriteFile(tmpFile2, []byte("TEST\n"), 0660), check.IsNil)
c.Assert(ioutil.WriteFile(tmpFile3, []byte("TEST\n"), 0600), check.IsNil)
c.Assert(os.WriteFile(tmpFile1, []byte("TEST\n"), 0644), check.IsNil)
c.Assert(os.WriteFile(tmpFile2, []byte("TEST\n"), 0660), check.IsNil)
c.Assert(os.WriteFile(tmpFile3, []byte("TEST\n"), 0600), check.IsNil)

c.Assert(CopyDir(sourceDir, targetDir), check.IsNil)

Expand Down
4 changes: 2 additions & 2 deletions hash/hast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package hash
// ////////////////////////////////////////////////////////////////////////////////// //

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

. "github.com/essentialkaos/check"
Expand Down Expand Up @@ -48,7 +48,7 @@ func (s *HashSuite) TestJCHashNegative(c *C) {
func (s *HashSuite) TestFileHash(c *C) {
tempFile := s.TmpDir + "/test.log"

err := ioutil.WriteFile(tempFile, []byte("ABCDEF12345\n\n"), 0644)
err := os.WriteFile(tempFile, []byte("ABCDEF12345\n\n"), 0644)

hash1 := FileHash(tempFile)
hash2 := FileHash(s.TmpDir + "/not-exist.log")
Expand Down
6 changes: 3 additions & 3 deletions initsystem/initsystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package initsystem
// ////////////////////////////////////////////////////////////////////////////////// //

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

. "github.com/essentialkaos/check"
Expand Down Expand Up @@ -61,8 +61,8 @@ end script`

tmpDir := c.MkDir()

ioutil.WriteFile(tmpDir+"/1.conf", []byte(data1), 0644)
ioutil.WriteFile(tmpDir+"/2.conf", []byte(data2), 0644)
os.WriteFile(tmpDir+"/1.conf", []byte(data1), 0644)
os.WriteFile(tmpDir+"/2.conf", []byte(data2), 0644)

ok, err := parseUpstartEnabledData(tmpDir + "/0.conf")

Expand Down
5 changes: 2 additions & 3 deletions jsonutil/jsonutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package jsonutil

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

Expand Down Expand Up @@ -68,7 +67,7 @@ func (s *JSONSuite) SetUpSuite(c *C) {
func (s *JSONSuite) TestDecoding(c *C) {
jsonFile := s.TmpDir + "/file1.json"

err := ioutil.WriteFile(jsonFile, []byte(_JSON_DATA), 0644)
err := os.WriteFile(jsonFile, []byte(_JSON_DATA), 0644)

c.Assert(err, IsNil)

Expand Down Expand Up @@ -101,7 +100,7 @@ func (s *JSONSuite) TestEncoding(c *C) {
c.Assert(err, IsNil)
c.Assert(fsutil.GetMode(jsonFile), Equals, os.FileMode(0640))

data, err := ioutil.ReadFile(jsonFile)
data, err := os.ReadFile(jsonFile)

c.Assert(err, IsNil)
c.Assert(string(data), Equals, _JSON_DATA)
Expand Down
7 changes: 3 additions & 4 deletions knf/knf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package knf

import (
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -131,19 +130,19 @@ func (s *KNFSuite) SetUpSuite(c *check.C) {
s.NonReadableConfigPath = "/etc/sudoers"
}

err := ioutil.WriteFile(s.ConfigPath, []byte(_CONFIG_DATA), 0644)
err := os.WriteFile(s.ConfigPath, []byte(_CONFIG_DATA), 0644)

if err != nil {
c.Fatal(err.Error())
}

err = ioutil.WriteFile(s.EmptyConfigPath, []byte(""), 0644)
err = os.WriteFile(s.EmptyConfigPath, []byte(""), 0644)

if err != nil {
c.Fatal(err.Error())
}

err = ioutil.WriteFile(s.MalformedConfigPath, []byte(_CONFIG_MALF_DATA), 0644)
err = os.WriteFile(s.MalformedConfigPath, []byte(_CONFIG_MALF_DATA), 0644)

if err != nil {
c.Fatal(err.Error())
Expand Down
3 changes: 1 addition & 2 deletions knf/validators/fs/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package fs

import (
"fmt"
"io/ioutil"
"os"
"runtime"
"testing"
Expand Down Expand Up @@ -230,7 +229,7 @@ func createConfig(c *C, data string) string {
configPath := c.MkDir() + "/config.knf"
configData := fmt.Sprintf(_CONFIG_TEMPLATE, data)

err := ioutil.WriteFile(configPath, []byte(configData), 0644)
err := os.WriteFile(configPath, []byte(configData), 0644)

if err != nil {
c.Fatal(err.Error())
Expand Down
4 changes: 2 additions & 2 deletions knf/validators/network/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package network
// ////////////////////////////////////////////////////////////////////////////////// //

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

"github.com/essentialkaos/ek/v12/knf"
Expand Down Expand Up @@ -174,7 +174,7 @@ func (s *ValidatorSuite) TestURLValidator(c *C) {
func createConfig(c *C, data string) string {
configPath := c.MkDir() + "/config.knf"

err := ioutil.WriteFile(configPath, []byte(data), 0644)
err := os.WriteFile(configPath, []byte(data), 0644)

if err != nil {
c.Fatal(err.Error())
Expand Down
4 changes: 2 additions & 2 deletions knf/validators/regexp/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package regexp
// ////////////////////////////////////////////////////////////////////////////////// //

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

"github.com/essentialkaos/ek/v12/knf"
Expand Down Expand Up @@ -72,7 +72,7 @@ func (s *ValidatorSuite) TestRegexpValidator(c *check.C) {
func createConfig(c *check.C, data string) string {
configPath := c.MkDir() + "/config.knf"

err := ioutil.WriteFile(configPath, []byte(data), 0644)
err := os.WriteFile(configPath, []byte(data), 0644)

if err != nil {
c.Fatal(err.Error())
Expand Down
4 changes: 2 additions & 2 deletions knf/validators/system/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package system
// ////////////////////////////////////////////////////////////////////////////////// //

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

"github.com/essentialkaos/ek/v12/knf"
Expand Down Expand Up @@ -96,7 +96,7 @@ func (s *ValidatorSuite) TestGroupValidator(c *C) {
func createConfig(c *C, data string) string {
configPath := c.MkDir() + "/config.knf"

err := ioutil.WriteFile(configPath, []byte(data), 0644)
err := os.WriteFile(configPath, []byte(data), 0644)

if err != nil {
c.Fatal(err.Error())
Expand Down
4 changes: 2 additions & 2 deletions knf/validators/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package validators
// ////////////////////////////////////////////////////////////////////////////////// //

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

"github.com/essentialkaos/ek/v12/knf"
Expand Down Expand Up @@ -247,7 +247,7 @@ func (s *ValidatorSuite) TestTypeValidators(c *check.C) {
func createConfig(c *check.C, data string) string {
configPath := c.MkDir() + "/config.knf"

err := ioutil.WriteFile(configPath, []byte(data), 0644)
err := os.WriteFile(configPath, []byte(data), 0644)

if err != nil {
c.Fatal(err.Error())
Expand Down
Loading

0 comments on commit 1d3c728

Please sign in to comment.