Skip to content

Commit

Permalink
Migrate storage to use bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
berejant committed Sep 27, 2024
1 parent 5b810d6 commit a9ebead
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 48 deletions.
40 changes: 25 additions & 15 deletions mock_Interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 15 additions & 18 deletions storage.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,47 @@
package storage

import (
"bytes"
"errors"
"os"
)

type Interface interface {
Get() (string, error)
Set(value string) error
Get() ([]byte, error)
Set(value []byte) error
}

type Storage struct {
File string
value string
loaded bool
File string
value []byte
}

func (storage Storage) Get() (string, error) {
if storage.loaded == false {
storage.loaded = true
if _, err := os.Stat(storage.File); errors.Is(err, os.ErrNotExist) {
return "", nil
func (storage *Storage) Get() ([]byte, error) {
if storage.value == nil {
_, err := os.Stat(storage.File)
if errors.Is(err, os.ErrNotExist) {
return nil, nil
}

data, err := os.ReadFile(storage.File)
storage.value, err = os.ReadFile(storage.File)
if err != nil {
return "", err
return nil, err
}

storage.value = string(data)
}

return storage.value, nil
}

func (storage *Storage) Set(value string) error {
if storage.loaded && storage.value == value {
func (storage *Storage) Set(value []byte) error {
if bytes.Equal(storage.value, value) {
return nil
}

err := os.WriteFile(storage.File, []byte(value), 0644)
err := os.WriteFile(storage.File, value, 0644)
if err != nil {
return err
}

storage.value = value
storage.loaded = true
return nil
}
29 changes: 14 additions & 15 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import (

func TestStorageGet(t *testing.T) {
filename := "storage-read-test.txt"
expectedString := "read-value-from-storage"
expected := []byte("read-value-from-storage")

err := os.WriteFile(filename, []byte(expectedString), 0644)
err := os.WriteFile(filename, expected, 0644)
defer os.Remove(filename)
assert.NoErrorf(t, err, `Failed to write test file "%s" %s`, filename, err)

storage := Storage{
File: filename,
}

actualString, err := storage.Get()
actual, err := storage.Get()
assert.NoErrorf(t, err, `storage.Get("") failed: file to read storage file: %s`, err)
assert.Equalf(t, expectedString, actualString, "Expected %s, actual data in file: %s", expectedString, actualString)
assert.Equalf(t, expected, actual, "Expected %s, actual data in file: %s", expected, actual)
}

func TestStorageGetNotExistsFile(t *testing.T) {
Expand All @@ -40,7 +40,7 @@ func TestStorageGetNotExistsFile(t *testing.T) {

func TestStorageSet(t *testing.T) {
filename := "storage-Set-test.txt"
expectedString := "Set-value-to-storage"
expected := []byte("Set-value-to-storage")

storage := Storage{
File: filename,
Expand All @@ -49,27 +49,26 @@ func TestStorageSet(t *testing.T) {
defer os.Remove(filename)

for i := 1; i < 3; i++ {
err := storage.Set(expectedString)
err := storage.Set(expected)

assert.NoErrorf(t, err, `storage.Set("") failed: %v`, err)
assert.FileExists(t, filename, `Storage file not exists after execute storage.Set("")`)

actualData, err := os.ReadFile(filename)
actualString := string(actualData)
assert.NoErrorf(t, err, `storage.Set("") failed: file to read storage file: %s`, err)
assert.Equalf(t, expectedString, actualString, "Data in file is not match with excpected value: %s != %s", expectedString, actualString)
assert.Equalf(t, expected, actualData, "Data in file is not match with excpected value: %s != %s", expected, actualData)
}
}

func TestStorageGetSet(t *testing.T) {
filename := "storage-Get-Set-test.txt"
expectedString := "Set-value-to-Get-from-storage"
expected := []byte("Set-value-to-Get-from-storage")

storage := Storage{
File: filename,
}

err := storage.Set(expectedString)
err := storage.Set(expected)
defer os.Remove(filename)
assert.NoErrorf(t, err, `storage.Set("") failed: %v`, err)

Expand All @@ -78,24 +77,24 @@ func TestStorageGetSet(t *testing.T) {
File: filename,
}

actualString, err := storage.Get()
actual, err := storage.Get()
assert.NoErrorf(t, err, `storage.Get("") failed: file to read storage file: %s`, err)
assert.Equalf(
t, expectedString, actualString,
t, expected, actual,
"Expected %s, actual data in file: %s",
expectedString, actualString,
expected, actual,
)
}

func TestStorageSetWithWrongPath(t *testing.T) {
filename := "not-exists-dir/not-exist/random\n&@random.txt"
expectedString := "Set-value-to-storage"
expected := []byte("Set-value-to-storage")

storage := Storage{
File: filename,
}

err := storage.Set(expectedString)
err := storage.Set(expected)

assert.Errorf(t, err, `storage.Set("") not failed`)
var PathError *os.PathError
Expand Down

0 comments on commit a9ebead

Please sign in to comment.