1// Copyright 2024 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package release_config_lib
16
17import (
18	"os"
19	"path/filepath"
20	"testing"
21
22	rc_proto "android/soong/cmd/release_config/release_config_proto"
23
24	"google.golang.org/protobuf/proto"
25)
26
27type testCaseFlagValueFactory struct {
28	protoPath string
29	name      string
30	data      []byte
31	expected  rc_proto.FlagValue
32	err       error
33}
34
35func (tc testCaseFlagValueFactory) assertProtoEqual(t *testing.T, expected, actual proto.Message) {
36	if !proto.Equal(expected, actual) {
37		t.Errorf("Expected %q found %q", expected, actual)
38	}
39}
40
41func TestFlagValueFactory(t *testing.T) {
42	testCases := []testCaseFlagValueFactory{
43		{
44			name:      "stringVal",
45			protoPath: "build/release/flag_values/test/RELEASE_FOO.textproto",
46			data:      []byte(`name: "RELEASE_FOO" value {string_value: "BAR"}`),
47			expected: rc_proto.FlagValue{
48				Name:  proto.String("RELEASE_FOO"),
49				Value: &rc_proto.Value{Val: &rc_proto.Value_StringValue{"BAR"}},
50			},
51			err: nil,
52		},
53	}
54	for _, tc := range testCases {
55		var err error
56		tempdir := t.TempDir()
57		path := filepath.Join(tempdir, tc.protoPath)
58		if err = os.MkdirAll(filepath.Dir(path), 0755); err != nil {
59			t.Fatal(err)
60		}
61		if err = os.WriteFile(path, tc.data, 0644); err != nil {
62			t.Fatal(err)
63		}
64		actual := FlagValueFactory(path)
65		tc.assertProtoEqual(t, &tc.expected, &actual.proto)
66	}
67}
68
69type testCaseMarshalValue struct {
70	name     string
71	value    *rc_proto.Value
72	expected string
73}
74
75func TestMarshalValue(t *testing.T) {
76	testCases := []testCaseMarshalValue{
77		{
78			name:     "nil",
79			value:    nil,
80			expected: "",
81		},
82		{
83			name:     "unspecified",
84			value:    &rc_proto.Value{},
85			expected: "",
86		},
87		{
88			name:     "false",
89			value:    &rc_proto.Value{Val: &rc_proto.Value_BoolValue{false}},
90			expected: "",
91		},
92		{
93			name:     "true",
94			value:    &rc_proto.Value{Val: &rc_proto.Value_BoolValue{true}},
95			expected: "true",
96		},
97		{
98			name:     "string",
99			value:    &rc_proto.Value{Val: &rc_proto.Value_StringValue{"BAR"}},
100			expected: "BAR",
101		},
102		{
103			name:     "obsolete",
104			value:    &rc_proto.Value{Val: &rc_proto.Value_Obsolete{true}},
105			expected: " #OBSOLETE",
106		},
107	}
108	for _, tc := range testCases {
109		actual := MarshalValue(tc.value)
110		if actual != tc.expected {
111			t.Errorf("Expected %q found %q", tc.expected, actual)
112		}
113	}
114}
115