mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-18 16:17:03 +00:00
Other: pretty print prefs.json
This commit is contained in:
@ -21,6 +21,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
@ -73,13 +74,12 @@ func (p *keyValueStore) save() error {
|
|||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
defer p.lock.Unlock()
|
defer p.lock.Unlock()
|
||||||
|
|
||||||
f, err := os.Create(p.path)
|
b, err := json.MarshalIndent(p.cache, "", "\t")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close() //nolint[errcheck]
|
|
||||||
|
|
||||||
return json.NewEncoder(f).Encode(p.cache)
|
return ioutil.WriteFile(p.path, b, 0600)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *keyValueStore) setDefault(key, value string) {
|
func (p *keyValueStore) setDefault(key, value string) {
|
||||||
|
|||||||
@ -72,20 +72,20 @@ func TestKeyValueStoreSetDefault(t *testing.T) {
|
|||||||
func TestKeyValueStoreSet(t *testing.T) {
|
func TestKeyValueStoreSet(t *testing.T) {
|
||||||
pref := newTestEmptyKeyValueStore(t)
|
pref := newTestEmptyKeyValueStore(t)
|
||||||
pref.Set("str", "value")
|
pref.Set("str", "value")
|
||||||
checkSavedKeyValueStore(t, "{\"str\":\"value\"}")
|
checkSavedKeyValueStore(t, "{\n\t\"str\": \"value\"\n}")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestKeyValueStoreSetInt(t *testing.T) {
|
func TestKeyValueStoreSetInt(t *testing.T) {
|
||||||
pref := newTestEmptyKeyValueStore(t)
|
pref := newTestEmptyKeyValueStore(t)
|
||||||
pref.SetInt("int", 42)
|
pref.SetInt("int", 42)
|
||||||
checkSavedKeyValueStore(t, "{\"int\":\"42\"}")
|
checkSavedKeyValueStore(t, "{\n\t\"int\": \"42\"\n}")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestKeyValueStoreSetBool(t *testing.T) {
|
func TestKeyValueStoreSetBool(t *testing.T) {
|
||||||
pref := newTestEmptyKeyValueStore(t)
|
pref := newTestEmptyKeyValueStore(t)
|
||||||
pref.SetBool("trueBool", true)
|
pref.SetBool("trueBool", true)
|
||||||
pref.SetBool("falseBool", false)
|
pref.SetBool("falseBool", false)
|
||||||
checkSavedKeyValueStore(t, "{\"falseBool\":\"false\",\"trueBool\":\"true\"}")
|
checkSavedKeyValueStore(t, "{\n\t\"falseBool\": \"false\",\n\t\"trueBool\": \"true\"\n}")
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTestEmptyKeyValueStore(t *testing.T) *keyValueStore {
|
func newTestEmptyKeyValueStore(t *testing.T) *keyValueStore {
|
||||||
@ -101,5 +101,5 @@ func newTestKeyValueStore(t *testing.T) *keyValueStore {
|
|||||||
func checkSavedKeyValueStore(t *testing.T, expected string) {
|
func checkSavedKeyValueStore(t *testing.T, expected string) {
|
||||||
data, err := ioutil.ReadFile(testPrefFilePath)
|
data, err := ioutil.ReadFile(testPrefFilePath)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, expected+"\n", string(data))
|
require.Equal(t, expected, string(data))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user