|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2019 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package daemon |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/spf13/viper" |
| 25 | + |
| 26 | + "github.com/arduino/arduino-cli/configuration" |
| 27 | + rpc "github.com/arduino/arduino-cli/rpc/settings" |
| 28 | + "github.com/stretchr/testify/require" |
| 29 | +) |
| 30 | + |
| 31 | +var svc = SettingsService{} |
| 32 | + |
| 33 | +func init() { |
| 34 | + configuration.Init("testdata") |
| 35 | +} |
| 36 | + |
| 37 | +func reset() { |
| 38 | + viper.Reset() |
| 39 | + configuration.Init("testdata") |
| 40 | +} |
| 41 | + |
| 42 | +func TestGetAll(t *testing.T) { |
| 43 | + resp, err := svc.GetAll(context.Background(), &rpc.GetAllRequest{}) |
| 44 | + require.Nil(t, err) |
| 45 | + |
| 46 | + content, err := json.Marshal(viper.AllSettings()) |
| 47 | + require.Nil(t, err) |
| 48 | + |
| 49 | + require.Equal(t, string(content), resp.GetJsonData()) |
| 50 | +} |
| 51 | + |
| 52 | +func TestMerge(t *testing.T) { |
| 53 | + bulkSettings := `{"foo": "bar", "daemon":{"port":"420"}}` |
| 54 | + _, err := svc.Merge(context.Background(), &rpc.RawData{JsonData: bulkSettings}) |
| 55 | + require.Nil(t, err) |
| 56 | + |
| 57 | + fmt.Println(viper.AllSettings()) |
| 58 | + require.Equal(t, "420", viper.GetString("daemon.port")) |
| 59 | + require.Equal(t, "bar", viper.GetString("foo")) |
| 60 | + |
| 61 | + reset() |
| 62 | +} |
| 63 | + |
| 64 | +func TestGetValue(t *testing.T) { |
| 65 | + key := &rpc.GetValueRequest{Key: "daemon"} |
| 66 | + resp, err := svc.GetValue(context.Background(), key) |
| 67 | + require.Nil(t, err) |
| 68 | + require.Equal(t, `{"port":"50051"}`, resp.GetJsonData()) |
| 69 | +} |
| 70 | + |
| 71 | +func TestGetValueNotFound(t *testing.T) { |
| 72 | + key := &rpc.GetValueRequest{Key: "DOESNTEXIST"} |
| 73 | + _, err := svc.GetValue(context.Background(), key) |
| 74 | + require.NotNil(t, err) |
| 75 | + require.Equal(t, `key not found in settings`, err.Error()) |
| 76 | +} |
| 77 | + |
| 78 | +func TestSetValue(t *testing.T) { |
| 79 | + val := &rpc.Value{ |
| 80 | + Key: "foo", |
| 81 | + JsonData: `"bar"`, |
| 82 | + } |
| 83 | + _, err := svc.SetValue(context.Background(), val) |
| 84 | + require.Nil(t, err) |
| 85 | + require.Equal(t, "bar", viper.GetString("foo")) |
| 86 | +} |
0 commit comments