From 934f07ae9742b2ec1fa8cf874a0e9f05c12f56d7 Mon Sep 17 00:00:00 2001 From: s-dwinter Date: Mon, 3 Jun 2024 02:44:53 +0900 Subject: [PATCH] add new line --- internal/client/cache/file.go | 17 ++++++++++++----- internal/client/cache/file_test.go | 2 +- .../save-and-load/656e637279707465642d6b6579 | 2 +- .../save-and-load/657870697265642d6b6579 | 2 +- .../testdata/save-and-load/706c61696e2d6b6579 | 2 +- .../save-and-load/test_7072656669782d6b6579 | 2 +- 6 files changed, 17 insertions(+), 10 deletions(-) diff --git a/internal/client/cache/file.go b/internal/client/cache/file.go index e261e11..a50c559 100644 --- a/internal/client/cache/file.go +++ b/internal/client/cache/file.go @@ -95,16 +95,16 @@ func (f *fileCache) Load(_ context.Context, key string, decrypt bool) (*string, // check if cache is expired expired := time.Since(fInfo.ModTime().Local()) > f.expireDuration - cacheByte, err := f.readFile(filename, false) + valueByte, err := f.readFile(filename, false) if err != nil { return nil, false, err } if !decrypt { - cache := string(cacheByte) - return &cache, expired, nil + value := string(valueByte) + return &value, expired, nil } - valueByte, err := f.decryptCache(cacheByte) + valueByte, err = f.decryptCache(valueByte) if err != nil { return nil, false, err } @@ -226,7 +226,13 @@ func (f fileCache) readFile(filename string, hidden bool) ([]byte, error) { filename = "." + filename } - return os.ReadFile(filepath.Join(f.cachePath, filename)) + data, err := os.ReadFile(filepath.Join(f.cachePath, filename)) + if err != nil { + return nil, err + } + data = data[:len(data)-1] + + return data, nil } func (f fileCache) writeToFile(filename string, data []byte, hidden bool) error { @@ -241,6 +247,7 @@ func (f fileCache) writeToFile(filename string, data []byte, hidden bool) error } defer file.Close() + data = append(data, '\n') if _, err := file.Write(data); err != nil { return err } diff --git a/internal/client/cache/file_test.go b/internal/client/cache/file_test.go index a6bf0c0..330ffc3 100644 --- a/internal/client/cache/file_test.go +++ b/internal/client/cache/file_test.go @@ -174,7 +174,7 @@ func Test_fileCache_SaveAndLoad(t *testing.T) { t.Errorf("fileCache.Load() expired = %v, want %v", gotExpired, tt.wantExpired) } if !reflect.DeepEqual(gotValue, tt.args.value) { - t.Errorf("fileCache.Load() got = %v, want %v", gotValue, tt.args.value) + t.Errorf("fileCache.Load() got = %v, want %v", *gotValue, *tt.args.value) } }) } diff --git a/internal/client/cache/testdata/save-and-load/656e637279707465642d6b6579 b/internal/client/cache/testdata/save-and-load/656e637279707465642d6b6579 index a565263..33b9502 100644 --- a/internal/client/cache/testdata/save-and-load/656e637279707465642d6b6579 +++ b/internal/client/cache/testdata/save-and-load/656e637279707465642d6b6579 @@ -1 +1 @@ -¹wBs) \ê.<󨇬eƒ—!*ƒ¯`XS`‰ \ No newline at end of file +¥ò{¦çsê>}Á¼W[2æ6‚otúkœ¥ diff --git a/internal/client/cache/testdata/save-and-load/657870697265642d6b6579 b/internal/client/cache/testdata/save-and-load/657870697265642d6b6579 index 42ed5a6..7268620 100644 --- a/internal/client/cache/testdata/save-and-load/657870697265642d6b6579 +++ b/internal/client/cache/testdata/save-and-load/657870697265642d6b6579 @@ -1 +1 @@ -expired-value \ No newline at end of file +expired-value diff --git a/internal/client/cache/testdata/save-and-load/706c61696e2d6b6579 b/internal/client/cache/testdata/save-and-load/706c61696e2d6b6579 index 722b9a8..efa1f0e 100644 --- a/internal/client/cache/testdata/save-and-load/706c61696e2d6b6579 +++ b/internal/client/cache/testdata/save-and-load/706c61696e2d6b6579 @@ -1 +1 @@ -plain-value \ No newline at end of file +plain-value diff --git a/internal/client/cache/testdata/save-and-load/test_7072656669782d6b6579 b/internal/client/cache/testdata/save-and-load/test_7072656669782d6b6579 index 87ed3d8..40d9fe5 100644 --- a/internal/client/cache/testdata/save-and-load/test_7072656669782d6b6579 +++ b/internal/client/cache/testdata/save-and-load/test_7072656669782d6b6579 @@ -1 +1 @@ -prefix-value \ No newline at end of file +prefix-value